|
| 1 | +package rfq |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +// Test certificate data - a valid self-signed certificate for testing |
| 10 | +const validTestCertPEM = `-----BEGIN CERTIFICATE----- |
| 11 | +MIICmjCCAYICCQCuu1gzY+BBKjANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0 |
| 12 | +ZXN0MB4XDTI1MDgyODEwNDA1NVoXDTI1MDgyOTEwNDA1NVowDzENMAsGA1UEAwwE |
| 13 | +dGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALTWCm8l3d9nE2QK |
| 14 | +TK8HJ36ftO8pK3//nb8Nj/p97FrPFSgzdgL1ZNJs4gP5/ZsU+iE6VeKhalHoSf6/ |
| 15 | +IMLe3ATTL0rWA1M6z7cw6ll8VS8NQMaMSFWNomncsxyoJAQde++SC5f1RwQJBD/0 |
| 16 | +gGB4bJIIqUHtT12m23GLX48d6JGEEi5kEQtk91S/QGnHtglzZ8CQOogDBzDhSHu2 |
| 17 | +jj4mKYDgkXcyAqN7DoDzoEcrpeAaeAwem8k1sFBeTtrqT1ot7Ey5KG+RUyJbdKGt |
| 18 | +5adJiwH782NgsSnISQ2X7Sct6Uu0JzHKx9JzyABsA05tf3cNJkLhh1Is9edYI2e9 |
| 19 | +m0dqedECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAQOCs/7xZVPjabbhdv30mUJMG |
| 20 | +lddi2A+R/5IRXW1MKnpemwiv4ZWYQ9PMTmuR7kqaF7AGLkvx+5sp2evUJN4x7vHP |
| 21 | +ao6wihbdh+vBkrobE+Y9dE7nbkvMQSNi1sXzDnfZB9LqY9Huun2soUwBQNCMPVMa |
| 22 | +Wo7g6udwyA48doEVJMjThFLPcW7xmsy6Ldew682m1kD8/ag+9qihX1IJyiqiEjha |
| 23 | +3uT4CT+zEg0RJorEJKbR38fE4Uhx1wZO4zvjEg6qZeW/I4lw+UzSY5xV7lJ1EQvf |
| 24 | +BcoNuBHB65RxQM5fpA7hkEFm1bxBoowGX2hx6VCCeBBwREISRfgvkUxZahUXNg== |
| 25 | +-----END CERTIFICATE-----` |
| 26 | + |
| 27 | +// Invalid PEM data for testing failure cases |
| 28 | +const invalidTestCertPEM = `-----BEGIN CERTIFICATE----- |
| 29 | +This is not a valid certificate |
| 30 | +-----END CERTIFICATE-----` |
| 31 | + |
| 32 | +// DefaultTLSConfig returns a default TLS configuration for testing. |
| 33 | +func DefaultTLSConfig() *TLSConfig { |
| 34 | + return &TLSConfig{ |
| 35 | + InsecureSkipVerify: true, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// TestConfigureTransportCredentials_InsecureSkipVerify tests the function |
| 40 | +// when InsecureSkipVerify is true. |
| 41 | +func TestConfigureTransportCredentials_InsecureSkipVerify(t *testing.T) { |
| 42 | + config := &TLSConfig{ |
| 43 | + InsecureSkipVerify: true, |
| 44 | + } |
| 45 | + |
| 46 | + creds, err := configureTransportCredentials(config) |
| 47 | + |
| 48 | + require.NoError(t, err) |
| 49 | + require.NotNil(t, creds) |
| 50 | + |
| 51 | + // Verify that we got insecure credentials by checking the type |
| 52 | + require.Equal(t, "insecure", creds.Info().SecurityProtocol) |
| 53 | +} |
| 54 | + |
| 55 | +// TestConfigureTransportCredentials_ValidCustomCertificates tests the |
| 56 | +// function when valid custom certificates are provided. |
| 57 | +func TestConfigureTransportCredentials_ValidCustomCertificates(t *testing.T) { |
| 58 | + config := &TLSConfig{ |
| 59 | + InsecureSkipVerify: false, |
| 60 | + CustomCertificates: []byte(validTestCertPEM), |
| 61 | + } |
| 62 | + |
| 63 | + creds, err := configureTransportCredentials(config) |
| 64 | + |
| 65 | + require.NoError(t, err) |
| 66 | + require.NotNil(t, creds) |
| 67 | + |
| 68 | + // Verify that we got TLS credentials (not insecure) |
| 69 | + require.Equal(t, "tls", creds.Info().SecurityProtocol) |
| 70 | +} |
| 71 | + |
| 72 | +// TestConfigureTransportCredentials_NoCredentialsConfigured tests the |
| 73 | +// function when no credentials are configured. |
| 74 | +func TestConfigureTransportCredentials_NoCredentialsConfigured(t *testing.T) { |
| 75 | + config := &TLSConfig{ |
| 76 | + InsecureSkipVerify: false, |
| 77 | + CustomCertificates: nil, |
| 78 | + } |
| 79 | + |
| 80 | + creds, err := configureTransportCredentials(config) |
| 81 | + |
| 82 | + require.NoError(t, err) |
| 83 | + require.NotNil(t, creds) |
| 84 | + require.Equal(t, "tls", creds.Info().SecurityProtocol) |
| 85 | +} |
0 commit comments