github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/client/apln_websocket_test.go (about)

     1  /*
     2  Copyright 2024 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package client
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/gobwas/ws"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func Test_websocketALPNClientConn(t *testing.T) {
    29  	clientRawConn, serverRawConn := net.Pipe()
    30  	t.Cleanup(func() {
    31  		clientRawConn.Close()
    32  		serverRawConn.Close()
    33  	})
    34  
    35  	clientConn := newWebSocketALPNClientConn(clientRawConn)
    36  
    37  	t.Run("Read", func(t *testing.T) {
    38  		wait := make(chan struct{}, 1)
    39  
    40  		// Send a ping and some text from server.
    41  		go func() {
    42  			require.NoError(t, ws.WriteFrame(serverRawConn, ws.NewPingFrame([]byte("foo"))))
    43  			frame, err := ws.ReadFrame(serverRawConn)
    44  			require.NoError(t, err)
    45  			require.Equal(t, ws.OpPong, frame.Header.OpCode)
    46  			require.NoError(t, ws.WriteFrame(serverRawConn, ws.NewBinaryFrame([]byte("hello client"))))
    47  			wait <- struct{}{}
    48  		}()
    49  
    50  		mustReadWebsocketALPNClientConn(t, clientConn, "hello c")
    51  		mustReadWebsocketALPNClientConn(t, clientConn, "lient")
    52  
    53  		<-wait
    54  	})
    55  
    56  	t.Run("Write", func(t *testing.T) {
    57  		wait := make(chan struct{}, 1)
    58  		text := "hello server"
    59  
    60  		go func() {
    61  			n, err := clientConn.Write([]byte(text))
    62  			require.NoError(t, err)
    63  			require.Equal(t, len(text), n)
    64  			wait <- struct{}{}
    65  		}()
    66  
    67  		wantFrame := ws.NewBinaryFrame([]byte(text))
    68  		wantFrame.Header.Masked = true
    69  
    70  		actualFrame, err := ws.ReadFrame(serverRawConn)
    71  		require.NoError(t, err)
    72  		require.Equal(t, wantFrame, actualFrame)
    73  
    74  		<-wait
    75  	})
    76  }
    77  
    78  func mustReadWebsocketALPNClientConn(t *testing.T, conn *websocketALPNClientConn, wantText string) {
    79  	t.Helper()
    80  
    81  	actualTextChan := make(chan string, 1)
    82  	errChan := make(chan error, 1)
    83  
    84  	go func() {
    85  		readBuff := make([]byte, len(wantText))
    86  		_, err := conn.Read(readBuff)
    87  		if err != nil {
    88  			errChan <- err
    89  		} else {
    90  			actualTextChan <- string(readBuff)
    91  		}
    92  	}()
    93  
    94  	select {
    95  	case actualText := <-actualTextChan:
    96  		require.Equal(t, wantText, actualText)
    97  	case err := <-errChan:
    98  		require.NoError(t, err)
    99  	case <-time.After(time.Second):
   100  		require.Fail(t, "timed out waiting for %v from Read", wantText)
   101  	}
   102  }