github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/client/handshake_test.go (about) 1 // Copyright 2020 DataStax 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package client_test 16 17 import ( 18 "context" 19 "github.com/datastax/go-cassandra-native-protocol/client" 20 "github.com/datastax/go-cassandra-native-protocol/primitive" 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 "testing" 24 "time" 25 ) 26 27 func TestHandshakeHandler_NoAuth(t *testing.T) { 28 29 server := client.NewCqlServer("127.0.0.1:9043", nil) 30 server.RequestHandlers = []client.RequestHandler{client.HandshakeHandler} 31 32 clt := client.NewCqlClient("127.0.0.1:9043", nil) 33 34 ctx, cancelFn := context.WithCancel(context.Background()) 35 36 err := server.Start(ctx) 37 require.NoError(t, err) 38 39 clientConn, err := clt.Connect(ctx) 40 require.NoError(t, err) 41 require.NotNil(t, clientConn) 42 43 err = clientConn.InitiateHandshake(primitive.ProtocolVersion4, client.ManagedStreamId) 44 require.NoError(t, err) 45 46 cancelFn() 47 48 assert.Eventually(t, clientConn.IsClosed, time.Second*10, time.Millisecond*10) 49 assert.Eventually(t, server.IsClosed, time.Second*10, time.Millisecond*10) 50 51 } 52 53 func TestHandshakeHandler_Auth(t *testing.T) { 54 55 server := client.NewCqlServer("127.0.0.1:9043", &client.AuthCredentials{ 56 Username: "user1", 57 Password: "pass1", 58 }) 59 server.RequestHandlers = []client.RequestHandler{client.HandshakeHandler} 60 61 clt := client.NewCqlClient("127.0.0.1:9043", &client.AuthCredentials{ 62 Username: "user1", 63 Password: "pass1", 64 }) 65 66 ctx, cancelFn := context.WithCancel(context.Background()) 67 68 err := server.Start(ctx) 69 require.NoError(t, err) 70 71 clientConn, err := clt.Connect(ctx) 72 require.NoError(t, err) 73 require.NotNil(t, clientConn) 74 75 err = clientConn.InitiateHandshake(primitive.ProtocolVersion4, client.ManagedStreamId) 76 require.NoError(t, err) 77 78 cancelFn() 79 80 assert.Eventually(t, clientConn.IsClosed, time.Second*10, time.Millisecond*10) 81 assert.Eventually(t, server.IsClosed, time.Second*10, time.Millisecond*10) 82 83 }