@@ -12,6 +12,7 @@ import (
1212 "crypto/x509"
1313 "crypto/x509/pkix"
1414 "encoding/pem"
15+ "fmt"
1516 "math/big"
1617 "net/http"
1718 "os"
@@ -133,57 +134,82 @@ func TestNewService(t *testing.T) {
133134 t .Run ("NewServiceWithMutualTLS" , func (t * testing.T ) {
134135 testCertFilePath := createTestCertificate (t )
135136
136- config .TLSSettings = serverTLS.TlsSettings {
137- TLSPort : "10091" ,
138- TLSSubjectName : "localhost" ,
139- TLSCertificatePath : testCertFilePath ,
140- UseMTLS : true ,
141- MinTLSVersion : "TLS 1.2" ,
137+ TLSSetting := serverTLS.TlsSettings {
138+ TLSPort : "10091" ,
139+ TLSSubjectName : "localhost" ,
140+ TLSCertificatePath : testCertFilePath ,
141+ UseMTLS : true ,
142+ MinTLSVersion : "TLS 1.2" ,
143+ AllowedClientSubjectName : "example.com" ,
142144 }
143145
144- svc , err := NewService (config .Name , config .Version , config .ChannelMode , config .Store )
145- require .NoError (t , err )
146- require .IsType (t , & Service {}, svc )
146+ TLSSettingWithDisallowedClientSN := serverTLS.TlsSettings {
147+ TLSPort : "10092" ,
148+ TLSSubjectName : "localhost" ,
149+ TLSCertificatePath : testCertFilePath ,
150+ UseMTLS : true ,
151+ MinTLSVersion : "TLS 1.2" ,
152+ AllowedClientSubjectName : "random.com" ,
153+ }
147154
148- svc .SetOption (acn .OptCnsURL , "" )
149- svc .SetOption (acn .OptCnsPort , "" )
155+ runMutualTLSTest := func (tlsSettings serverTLS.TlsSettings , handshakeFailureExpected bool ) {
156+ config .TLSSettings = tlsSettings
157+ svc , err := NewService (config .Name , config .Version , config .ChannelMode , config .Store )
158+ require .NoError (t , err )
159+ require .IsType (t , & Service {}, svc )
150160
151- err = svc .Initialize (config )
152- t .Cleanup (func () {
153- svc .Uninitialize ()
154- })
155- require .NoError (t , err )
161+ svc .SetOption (acn .OptCnsURL , "" )
162+ svc .SetOption (acn .OptCnsPort , "" )
156163
157- err = svc .StartListener (config )
158- require .NoError (t , err )
164+ err = svc .Initialize (config )
165+ require .NoError (t , err )
159166
160- mTLSConfig , err := getTLSConfigFromFile (config . TLSSettings )
161- require .NoError (t , err )
167+ err = svc . StartListener (config )
168+ require .NoError (t , err )
162169
163- client := & http.Client {
164- Transport : & http.Transport {
165- TLSClientConfig : mTLSConfig ,
166- },
167- }
170+ mTLSConfig , err := getTLSConfigFromFile (config .TLSSettings )
171+ require .NoError (t , err )
168172
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 )
173+ client := & http.Client {
174+ Transport : & http.Transport {
175+ TLSClientConfig : mTLSConfig ,
176+ },
177+ }
177178
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 )
179+ tlsUrl := fmt .Sprintf ("https://localhost:%s" , tlsSettings .TLSPort )
180+ // TLS listener
181+ req , err := http .NewRequestWithContext (context .TODO (), http .MethodGet , tlsUrl , http .NoBody )
182+ require .NoError (t , err )
183+ resp , err := client .Do (req )
184+ t .Cleanup (func () {
185+ if resp != nil && resp .Body != nil {
186+ resp .Body .Close ()
187+ }
188+ })
189+ if handshakeFailureExpected {
190+ require .Error (t , err )
191+ require .ErrorContains (t , err , "failed to verify client certificate hostname" )
192+
193+ } else {
194+ require .NoError (t , err )
195+ }
196+
197+ // HTTP listener
198+ httpClient := & http.Client {}
199+ req , err = http .NewRequestWithContext (context .TODO (), http .MethodGet , "http://localhost:10090" , http .NoBody )
200+ require .NoError (t , err )
201+ resp , err = httpClient .Do (req )
202+ t .Cleanup (func () {
203+ resp .Body .Close ()
204+ })
205+ require .NoError (t , err )
206+
207+ // Cleanup
208+ svc .Uninitialize ()
209+
210+ }
211+ runMutualTLSTest (TLSSetting , false )
212+ runMutualTLSTest (TLSSettingWithDisallowedClientSN , true )
187213 })
188214}
189215
0 commit comments