@@ -133,57 +133,108 @@ func TestNewService(t *testing.T) {
133133 t .Run ("NewServiceWithMutualTLS" , func (t * testing.T ) {
134134 testCertFilePath := createTestCertificate (t )
135135
136- config .TLSSettings = serverTLS.TlsSettings {
137- TLSPort : "10091" ,
138- TLSSubjectName : "localhost" ,
139- TLSCertificatePath : testCertFilePath ,
140- UseMTLS : true ,
141- MinTLSVersion : "TLS 1.2" ,
136+ cases := []struct {
137+ name string
138+ tlsSettings serverTLS.TlsSettings
139+ handshakeFailureExpected bool
140+ }{
141+ {
142+ name : "matching client SANs" ,
143+ tlsSettings : serverTLS.TlsSettings {
144+ TLSPort : "10091" ,
145+ TLSSubjectName : "localhost" ,
146+ TLSCertificatePath : testCertFilePath ,
147+ UseMTLS : true ,
148+ MinTLSVersion : "TLS 1.2" ,
149+ MtlsClientCertSubjectName : "example.com" ,
150+ },
151+ handshakeFailureExpected : false ,
152+ },
153+ {
154+ name : "matching client cert CN" ,
155+ tlsSettings : serverTLS.TlsSettings {
156+ TLSPort : "10093" ,
157+ TLSSubjectName : "localhost" ,
158+ TLSCertificatePath : testCertFilePath ,
159+ UseMTLS : true ,
160+ MinTLSVersion : "TLS 1.2" ,
161+ MtlsClientCertSubjectName : "foo.com" , // Common Name from test certificate
162+ },
163+ handshakeFailureExpected : false ,
164+ },
165+ {
166+ name : "failing to match client SANs and CN" ,
167+ tlsSettings : serverTLS.TlsSettings {
168+ TLSPort : "10092" ,
169+ TLSSubjectName : "localhost" ,
170+ TLSCertificatePath : testCertFilePath ,
171+ UseMTLS : true ,
172+ MinTLSVersion : "TLS 1.2" ,
173+ MtlsClientCertSubjectName : "random.com" ,
174+ },
175+ handshakeFailureExpected : true ,
176+ },
142177 }
143178
144- svc , err := NewService ( config . Name , config . Version , config . ChannelMode , config . Store )
145- require . NoError ( t , err )
146- require . IsType ( t , & Service {}, svc )
179+ for _ , tc := range cases {
180+ t . Run ( tc . name , func ( t * testing. T ) {
181+ config . TLSSettings = tc . tlsSettings
147182
148- svc .SetOption (acn .OptCnsURL , "" )
149- svc .SetOption (acn .OptCnsPort , "" )
183+ svc , err := NewService (config .Name , config .Version , config .ChannelMode , config .Store )
184+ require .NoError (t , err )
185+ require .IsType (t , & Service {}, svc )
150186
151- err = svc .Initialize (config )
152- t .Cleanup (func () {
153- svc .Uninitialize ()
154- })
155- require .NoError (t , err )
187+ svc .SetOption (acn .OptCnsURL , "" )
188+ svc .SetOption (acn .OptCnsPort , "" )
156189
157- err = svc .StartListener (config )
158- require .NoError (t , err )
190+ err = svc .Initialize (config )
191+ require .NoError (t , err )
159192
160- mTLSConfig , err := getTLSConfigFromFile (config . TLSSettings )
161- require .NoError (t , err )
193+ err = svc . StartListener (config )
194+ require .NoError (t , err )
162195
163- client := & http.Client {
164- Transport : & http.Transport {
165- TLSClientConfig : mTLSConfig ,
166- },
167- }
196+ mTLSConfig , err := getTLSConfigFromFile (config .TLSSettings )
197+ require .NoError (t , err )
168198
169- // TLS listener
170- req , err := http .NewRequestWithContext (context .TODO (), http .MethodGet , "https://localhost:10091" , http .NoBody )
171- require .NoError (t , err )
172- resp , err := client .Do (req )
173- t .Cleanup (func () {
174- resp .Body .Close ()
175- })
176- require .NoError (t , err )
199+ client := & http.Client {
200+ Transport : & http.Transport {
201+ TLSClientConfig : mTLSConfig ,
202+ },
203+ }
177204
178- // HTTP listener
179- httpClient := & http.Client {}
180- req , err = http .NewRequestWithContext (context .TODO (), http .MethodGet , "http://localhost:10090" , http .NoBody )
181- require .NoError (t , err )
182- resp , err = httpClient .Do (req )
183- t .Cleanup (func () {
184- resp .Body .Close ()
185- })
186- require .NoError (t , err )
205+ tlsURL := "https://localhost:" + tc .tlsSettings .TLSPort
206+ // TLS listener
207+ req , err := http .NewRequestWithContext (context .TODO (), http .MethodGet , tlsURL , http .NoBody )
208+ require .NoError (t , err )
209+ resp , err := client .Do (req )
210+ if tc .handshakeFailureExpected {
211+ require .Error (t , err )
212+ require .ErrorContains (t , err , "Failed to verify client certificate subject name during mTLS" )
213+ } else {
214+ require .NoError (t , err )
215+ t .Cleanup (func () {
216+ if resp != nil && resp .Body != nil {
217+ resp .Body .Close ()
218+ }
219+ })
220+ }
221+
222+ // HTTP listener
223+ httpClient := & http.Client {}
224+ req , err = http .NewRequestWithContext (context .TODO (), http .MethodGet , "http://localhost:10090" , http .NoBody )
225+ require .NoError (t , err )
226+ resp , err = httpClient .Do (req )
227+ require .NoError (t , err )
228+ t .Cleanup (func () {
229+ if resp != nil && resp .Body != nil {
230+ resp .Body .Close ()
231+ }
232+ })
233+
234+ // Cleanup
235+ svc .Uninitialize ()
236+ })
237+ }
187238 })
188239}
189240
@@ -355,3 +406,28 @@ func TestTLSVersionNumber(t *testing.T) {
355406 require .NoError (t , err )
356407 })
357408}
409+
410+ func TestMaskHalf (t * testing.T ) {
411+ tests := []struct {
412+ name string
413+ in string
414+ want string
415+ }{
416+ {"empty" , "" , "" },
417+ {"one char string" , "e" , "*" },
418+ {"two chars string" , "ex" , "e*" },
419+ {"three chars string" , "exa" , "e**" },
420+ {"four chars string" , "exam" , "ex**" },
421+ {"five chars string" , "examp" , "ex***" },
422+ {"long string" , "example.com" , "examp******" },
423+ }
424+
425+ for _ , tc := range tests {
426+ t .Run (tc .name , func (t * testing.T ) {
427+ got := maskHalf (tc .in )
428+ if got != tc .want {
429+ t .Fatalf ("maskHalf(%s) = %s, want %s" , tc .in , got , tc .want )
430+ }
431+ })
432+ }
433+ }
0 commit comments