github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/authenticate_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 message 16 17 import ( 18 "bytes" 19 "errors" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 24 "github.com/datastax/go-cassandra-native-protocol/primitive" 25 ) 26 27 func TestAuthenticate_DeepCopy(t *testing.T) { 28 msg := &Authenticate{ 29 Authenticator: "auth", 30 } 31 cloned := msg.DeepCopy() 32 assert.Equal(t, msg, cloned) 33 cloned.Authenticator = "auth2" 34 assert.Equal(t, "auth", msg.Authenticator) 35 assert.Equal(t, "auth2", cloned.Authenticator) 36 assert.NotEqual(t, msg, cloned) 37 } 38 39 func TestAuthenticateCodec_Encode(t *testing.T) { 40 codec := &authenticateCodec{} 41 for _, version := range primitive.SupportedProtocolVersions() { 42 t.Run(version.String(), func(t *testing.T) { 43 tests := []encodeTestCase{ 44 { 45 "simple authenticate", 46 &Authenticate{"dummy"}, 47 []byte{0, 5, d, u, m, m, y}, 48 nil, 49 }, 50 { 51 "not an authenticate", 52 &AuthChallenge{[]byte{0xca, 0xfe, 0xba, 0xbe}}, 53 nil, 54 errors.New("expected *message.Authenticate, got *message.AuthChallenge"), 55 }, 56 { 57 "authenticate nil authenticator", 58 &Authenticate{""}, 59 nil, 60 errors.New("AUTHENTICATE authenticator cannot be empty"), 61 }, 62 } 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 dest := &bytes.Buffer{} 66 err := codec.Encode(tt.input, dest, version) 67 assert.Equal(t, tt.expected, dest.Bytes()) 68 assert.Equal(t, tt.err, err) 69 }) 70 } 71 }) 72 } 73 } 74 75 func TestAuthenticateCodec_EncodedLength(t *testing.T) { 76 codec := &authenticateCodec{} 77 for _, version := range primitive.SupportedProtocolVersions() { 78 t.Run(version.String(), func(t *testing.T) { 79 tests := []encodedLengthTestCase{ 80 { 81 "simple authenticate", 82 &Authenticate{"dummy"}, 83 primitive.LengthOfString("dummy"), 84 nil, 85 }, 86 { 87 "not an authenticate", 88 &AuthChallenge{[]byte{0xca, 0xfe, 0xba, 0xbe}}, 89 -1, 90 errors.New("expected *message.Authenticate, got *message.AuthChallenge"), 91 }, 92 { 93 "authenticate nil authenticator", 94 &Authenticate{""}, 95 primitive.LengthOfString(""), 96 nil, 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 actual, err := codec.EncodedLength(tt.input, version) 102 assert.Equal(t, tt.expected, actual) 103 assert.Equal(t, tt.err, err) 104 }) 105 } 106 }) 107 } 108 } 109 110 func TestAuthenticateCodec_Decode(t *testing.T) { 111 codec := &authenticateCodec{} 112 for _, version := range primitive.SupportedProtocolVersions() { 113 t.Run(version.String(), func(t *testing.T) { 114 tests := []decodeTestCase{ 115 { 116 "simple authenticate", 117 []byte{0, 5, d, u, m, m, y}, 118 &Authenticate{"dummy"}, 119 nil, 120 }, 121 } 122 for _, tt := range tests { 123 t.Run(tt.name, func(t *testing.T) { 124 source := bytes.NewBuffer(tt.input) 125 actual, err := codec.Decode(source, version) 126 assert.Equal(t, tt.expected, actual) 127 assert.Equal(t, tt.err, err) 128 }) 129 } 130 }) 131 } 132 }