github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/crypto/tls/handshake_test.go (about)

     1  //go:build js
     2  
     3  package tls
     4  
     5  import (
     6  	"context"
     7  	"runtime"
     8  	"testing"
     9  )
    10  
    11  // Same as upstream, except we check for GOARCH=ecmascript instead of wasm.
    12  // This override can be removed after https://github.com/golang/go/pull/51827
    13  // is available in the upstream (likely in Go 1.19).
    14  func TestServerHandshakeContextCancellation(t *testing.T) {
    15  	c, s := localPipe(t)
    16  	ctx, cancel := context.WithCancel(context.Background())
    17  	unblockClient := make(chan struct{})
    18  	defer close(unblockClient)
    19  	go func() {
    20  		cancel()
    21  		<-unblockClient
    22  		_ = c.Close()
    23  	}()
    24  	conn := Server(s, testConfig)
    25  	// Initiates server side handshake, which will block until a client hello is read
    26  	// unless the cancellation works.
    27  	err := conn.HandshakeContext(ctx)
    28  	if err == nil {
    29  		t.Fatal("Server handshake did not error when the context was canceled")
    30  	}
    31  	if err != context.Canceled {
    32  		t.Errorf("Unexpected server handshake error: %v", err)
    33  	}
    34  	if runtime.GOARCH == "ecmascript" {
    35  		t.Skip("conn.Close does not error as expected when called multiple times on WASM")
    36  	}
    37  	err = conn.Close()
    38  	if err == nil {
    39  		t.Error("Server connection was not closed when the context was canceled")
    40  	}
    41  }
    42  
    43  // Same as upstream, except we check for GOARCH=ecmascript instead of wasm.
    44  // This override can be removed after https://github.com/golang/go/pull/51827
    45  // is available in the upstream (likely in Go 1.19).
    46  func TestClientHandshakeContextCancellation(t *testing.T) {
    47  	c, s := localPipe(t)
    48  	ctx, cancel := context.WithCancel(context.Background())
    49  	unblockServer := make(chan struct{})
    50  	defer close(unblockServer)
    51  	go func() {
    52  		cancel()
    53  		<-unblockServer
    54  		_ = s.Close()
    55  	}()
    56  	cli := Client(c, testConfig)
    57  	// Initiates client side handshake, which will block until the client hello is read
    58  	// by the server, unless the cancellation works.
    59  	err := cli.HandshakeContext(ctx)
    60  	if err == nil {
    61  		t.Fatal("Client handshake did not error when the context was canceled")
    62  	}
    63  	if err != context.Canceled {
    64  		t.Errorf("Unexpected client handshake error: %v", err)
    65  	}
    66  	if runtime.GOARCH == "ecmascript" {
    67  		t.Skip("conn.Close does not error as expected when called multiple times on WASM")
    68  	}
    69  	err = cli.Close()
    70  	if err == nil {
    71  		t.Error("Client connection was not closed when the context was canceled")
    72  	}
    73  }