66 "github.com/stretchr/testify/require"
77)
88
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-----
1111MIICmjCCAYICCQCuu1gzY+BBKjANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0
1212ZXN0MB4XDTI1MDgyODEwNDA1NVoXDTI1MDgyOTEwNDA1NVowDzENMAsGA1UEAwwE
1313dGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALTWCm8l3d9nE2QK
@@ -24,62 +24,107 @@ Wo7g6udwyA48doEVJMjThFLPcW7xmsy6Ldew682m1kD8/ag+9qihX1IJyiqiEjha
2424BcoNuBHB65RxQM5fpA7hkEFm1bxBoowGX2hx6VCCeBBwREISRfgvkUxZahUXNg==
2525-----END CERTIFICATE-----`
2626
27- // Invalid PEM data for testing failure cases
28- const invalidTestCertPEM = `-----BEGIN CERTIFICATE-----
27+ // invalidCertificate is an invalid certificate.
28+ const invalidCertificate = `-----BEGIN CERTIFICATE-----
2929This is not a valid certificate
3030-----END CERTIFICATE-----`
3131
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
3836
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
4538
46- creds , err := configureTransportCredentials (config )
39+ tlsConfig * TLSConfig
40+ }
4741
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 ) {
5046
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 )
5448
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 )
6251
63- creds , err := configureTransportCredentials ( config )
52+ protocol := creds . Info (). SecurityProtocol
6453
65- require .NoError (t , err )
66- require .NotNil (t , creds )
54+ if tc .expectInsecure {
55+ require .Equal (t , "insecure" , protocol )
56+ return
57+ }
6758
68- // Verify that we got TLS credentials (not insecure)
69- require .Equal (t , "tls" , creds .Info ().SecurityProtocol )
59+ require .Equal (t , "tls" , protocol )
7060}
7161
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 ,
7666 InsecureSkipVerify : false ,
77- CustomCertificates : nil ,
67+ TrustSystemRootCAs : true ,
7868 }
69+ }
7970
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+ }
81126
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+ }
85130}
0 commit comments