MCPcopy
hub / github.com/nats-io/nats.go / TestTLSHandshakeFirst

Function TestTLSHandshakeFirst

test/conn_test.go:3361–3468  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3359}
3360
3361func TestTLSHandshakeFirst(t *testing.T) {
3362 s, opts := RunServerWithConfig("./configs/tls.conf")
3363 defer s.Shutdown()
3364
3365 secureURL := fmt.Sprintf("tls://derek:porkchop@localhost:%d", opts.Port)
3366 nc, err := nats.Connect(secureURL,
3367 nats.RootCAs("./configs/certs/ca.pem"),
3368 nats.TLSHandshakeFirst())
3369 if err == nil || !strings.Contains(err.Error(), "TLS handshake") {
3370 if err == nil {
3371 nc.Close()
3372 }
3373 t.Fatalf("Expected error about not being a TLS handshake, got %v", err)
3374 }
3375
3376 tc := &server.TLSConfigOpts{
3377 CertFile: "./configs/certs/server.pem",
3378 KeyFile: "./configs/certs/key.pem",
3379 }
3380 tlsConf, err := server.GenTLSConfig(tc)
3381 if err != nil {
3382 t.Fatalf("Can't build TLCConfig: %v", err)
3383 }
3384 tlsConf.ServerName = "localhost"
3385
3386 // Start a mockup server that will do the TLS handshake first
3387 // and then send the INFO protocol.
3388 l, e := net.Listen("tcp", ":0")
3389 if e != nil {
3390 t.Fatal("Could not listen on an ephemeral port")
3391 }
3392 tl := l.(*net.TCPListener)
3393 defer tl.Close()
3394
3395 addr := tl.Addr().(*net.TCPAddr)
3396
3397 errCh := make(chan error, 1)
3398 doneCh := make(chan struct{})
3399 wg := sync.WaitGroup{}
3400 wg.Add(1)
3401 go func() {
3402 defer wg.Done()
3403 conn, err := l.Accept()
3404 if err != nil {
3405 errCh <- fmt.Errorf("error accepting client connection: %v", err)
3406 return
3407 }
3408 defer conn.Close()
3409
3410 // Do the TLS handshake now.
3411 conn = tls.Server(conn, tlsConf)
3412 tlsconn := conn.(*tls.Conn)
3413 if err := tlsconn.Handshake(); err != nil {
3414 errCh <- fmt.Errorf("Server error during handshake: %v", err)
3415 return
3416 }
3417
3418 // Send back the INFO

Callers

nothing calls this directly

Calls 11

ConnectMethod · 0.80
FatalfMethod · 0.80
ErrorfMethod · 0.80
TLSConnectionStateMethod · 0.80
RunServerWithConfigFunction · 0.70
ErrorMethod · 0.65
AddMethod · 0.65
DoneMethod · 0.65
CloseMethod · 0.45
WriteMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected