6
6
"github.com/stretchr/testify/require"
7
7
)
8
8
9
- // Test certificate data - a valid self-signed certificate for testing
10
- const validTestCertPEM = `-----BEGIN CERTIFICATE-----
9
+ // validCertificate is a valid certificate.
10
+ const validCertificate = `-----BEGIN CERTIFICATE-----
11
11
MIICmjCCAYICCQCuu1gzY+BBKjANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0
12
12
ZXN0MB4XDTI1MDgyODEwNDA1NVoXDTI1MDgyOTEwNDA1NVowDzENMAsGA1UEAwwE
13
13
dGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALTWCm8l3d9nE2QK
@@ -24,62 +24,107 @@ Wo7g6udwyA48doEVJMjThFLPcW7xmsy6Ldew682m1kD8/ag+9qihX1IJyiqiEjha
24
24
BcoNuBHB65RxQM5fpA7hkEFm1bxBoowGX2hx6VCCeBBwREISRfgvkUxZahUXNg==
25
25
-----END CERTIFICATE-----`
26
26
27
- // Invalid PEM data for testing failure cases
28
- const invalidTestCertPEM = `-----BEGIN CERTIFICATE-----
27
+ // invalidCertificate is an invalid certificate.
28
+ const invalidCertificate = `-----BEGIN CERTIFICATE-----
29
29
This is not a valid certificate
30
30
-----END CERTIFICATE-----`
31
31
32
- // DefaultTLSConfig returns a default TLS configuration for testing.
33
- func DefaultTLSConfig () * TLSConfig {
34
- return & TLSConfig {
35
- InsecureSkipVerify : true ,
36
- }
37
- }
32
+ // testCaseConfigureTransportCredentials is a test case for the
33
+ // configureTransportCredentials function.
34
+ type testCaseConfigureTransportCredentials struct {
35
+ name string
38
36
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
- }
37
+ expectInsecure bool
45
38
46
- creds , err := configureTransportCredentials (config )
39
+ tlsConfig * TLSConfig
40
+ }
47
41
48
- require .NoError (t , err )
49
- require .NotNil (t , creds )
42
+ // runConfigureTransportCredentialsTest tests that we get the expected
43
+ // security protocol from the provided test case.
44
+ func runConfigureTransportCredentialsTest (t * testing.T ,
45
+ tc * testCaseConfigureTransportCredentials ) {
50
46
51
- // Verify that we got insecure credentials by checking the type
52
- require .Equal (t , "insecure" , creds .Info ().SecurityProtocol )
53
- }
47
+ creds , err := configureTransportCredentials (tc .tlsConfig )
54
48
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
- }
49
+ // We should never see an error here.
50
+ require .Nil (t , err )
62
51
63
- creds , err := configureTransportCredentials ( config )
52
+ protocol := creds . Info (). SecurityProtocol
64
53
65
- require .NoError (t , err )
66
- require .NotNil (t , creds )
54
+ if tc .expectInsecure {
55
+ require .Equal (t , "insecure" , protocol )
56
+ return
57
+ }
67
58
68
- // Verify that we got TLS credentials (not insecure)
69
- require .Equal (t , "tls" , creds .Info ().SecurityProtocol )
59
+ require .Equal (t , "tls" , protocol )
70
60
}
71
61
72
- // TestConfigureTransportCredentials_NoCredentialsConfigured tests the
73
- // function when no credentials are configured.
74
- func TestConfigureTransportCredentials_NoCredentialsConfigured ( t * testing. T ) {
75
- config := & TLSConfig {
62
+ // defaultTLSConfig is the default TLS config.
63
+ func DefaultTLSConfig () * TLSConfig {
64
+ return & TLSConfig {
65
+ Enabled : true ,
76
66
InsecureSkipVerify : false ,
77
- CustomCertificates : nil ,
67
+ TrustSystemRootCAs : true ,
78
68
}
69
+ }
79
70
80
- creds , err := configureTransportCredentials (config )
71
+ // TestConfigureTransportCredentials tests the configureTransportCredentials
72
+ // function.
73
+ func TestConfigureTransportCredentials (t * testing.T ) {
74
+ testCases := []* testCaseConfigureTransportCredentials {
75
+ {
76
+ name : "default configuration" ,
77
+ expectInsecure : false ,
78
+ tlsConfig : DefaultTLSConfig (),
79
+ },
80
+ {
81
+ name : "tls disabled" ,
82
+ expectInsecure : true ,
83
+ tlsConfig : & TLSConfig {
84
+ Enabled : false ,
85
+ },
86
+ },
87
+ {
88
+ name : "trust os root CAs" ,
89
+ expectInsecure : false ,
90
+ tlsConfig : & TLSConfig {
91
+ Enabled : true ,
92
+ InsecureSkipVerify : false ,
93
+ TrustSystemRootCAs : true ,
94
+ },
95
+ },
96
+ {
97
+ name : "no trust os root CAs" ,
98
+ expectInsecure : false ,
99
+ tlsConfig : & TLSConfig {
100
+ Enabled : true ,
101
+ InsecureSkipVerify : false ,
102
+ TrustSystemRootCAs : false ,
103
+ },
104
+ },
105
+ {
106
+ name : "valid custom certificate" ,
107
+ expectInsecure : false ,
108
+ tlsConfig : & TLSConfig {
109
+ Enabled : true ,
110
+ InsecureSkipVerify : false ,
111
+ TrustSystemRootCAs : false ,
112
+ CustomCertificates : []byte (validCertificate ),
113
+ },
114
+ },
115
+ {
116
+ name : "invalid custom certificate" ,
117
+ expectInsecure : false ,
118
+ tlsConfig : & TLSConfig {
119
+ Enabled : true ,
120
+ InsecureSkipVerify : false ,
121
+ TrustSystemRootCAs : false ,
122
+ CustomCertificates : []byte (invalidCertificate ),
123
+ },
124
+ },
125
+ }
81
126
82
- require . NoError ( t , err )
83
- require . NotNil (t , creds )
84
- require . Equal ( t , "tls" , creds . Info (). SecurityProtocol )
127
+ for _ , tc := range testCases {
128
+ runConfigureTransportCredentialsTest (t , tc )
129
+ }
85
130
}
0 commit comments