github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/auth_response_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 TestAuthResponse_DeepCopy(t *testing.T) {
    28  	token := []byte{0xca, 0xfe, 0xba, 0xbe}
    29  	msg := &AuthResponse{
    30  		Token: token,
    31  	}
    32  	cloned := msg.DeepCopy()
    33  	assert.Equal(t, msg, cloned)
    34  	cloned.Token = []byte{0xcb, 0xfd, 0xbc, 0xba}
    35  	assert.Equal(t, []byte{0xca, 0xfe, 0xba, 0xbe}, token)
    36  	assert.Equal(t, []byte{0xcb, 0xfd, 0xbc, 0xba}, cloned.Token)
    37  	assert.NotEqual(t, msg, cloned)
    38  }
    39  
    40  func TestAuthResponseCodec_Encode(t *testing.T) {
    41  	token := []byte{0xca, 0xfe, 0xba, 0xbe}
    42  	codec := &authResponseCodec{}
    43  	for _, version := range primitive.SupportedProtocolVersions() {
    44  		t.Run(version.String(), func(t *testing.T) {
    45  			tests := []encodeTestCase{
    46  				{
    47  					"simple auth response",
    48  					&AuthResponse{token},
    49  					[]byte{0, 0, 0, 4, 0xca, 0xfe, 0xba, 0xbe},
    50  					nil,
    51  				},
    52  				{
    53  					"not an auth response",
    54  					&AuthChallenge{token},
    55  					nil,
    56  					errors.New("expected *message.AuthResponse, got *message.AuthChallenge"),
    57  				},
    58  				{
    59  					"auth response empty token",
    60  					&AuthResponse{[]byte{}},
    61  					[]byte{0, 0, 0, 0},
    62  					nil,
    63  				},
    64  				{
    65  					"auth response nil token",
    66  					&AuthResponse{nil},
    67  					[]byte{0xff, 0xff, 0xff, 0xff},
    68  					nil,
    69  				},
    70  			}
    71  			for _, tt := range tests {
    72  				t.Run(tt.name, func(t *testing.T) {
    73  					dest := &bytes.Buffer{}
    74  					err := codec.Encode(tt.input, dest, version)
    75  					assert.Equal(t, tt.expected, dest.Bytes())
    76  					assert.Equal(t, tt.err, err)
    77  				})
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestAuthResponseCodec_EncodedLength(t *testing.T) {
    84  	token := []byte{0xca, 0xfe, 0xba, 0xbe}
    85  	codec := &authResponseCodec{}
    86  	for _, version := range primitive.SupportedProtocolVersions() {
    87  		t.Run(version.String(), func(t *testing.T) {
    88  			tests := []encodedLengthTestCase{
    89  				{
    90  					"simple auth response",
    91  					&AuthResponse{token},
    92  					primitive.LengthOfBytes(token),
    93  					nil,
    94  				},
    95  				{
    96  					"not an auth response",
    97  					&AuthChallenge{token},
    98  					-1,
    99  					errors.New("expected *message.AuthResponse, got *message.AuthChallenge"),
   100  				},
   101  				{
   102  					"auth response nil token",
   103  					&AuthResponse{nil},
   104  					primitive.LengthOfBytes(nil),
   105  					nil,
   106  				},
   107  			}
   108  			for _, tt := range tests {
   109  				t.Run(tt.name, func(t *testing.T) {
   110  					actual, err := codec.EncodedLength(tt.input, version)
   111  					assert.Equal(t, tt.expected, actual)
   112  					assert.Equal(t, tt.err, err)
   113  				})
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  func TestAuthResponseCodec_Decode(t *testing.T) {
   120  	token := []byte{0xca, 0xfe, 0xba, 0xbe}
   121  	codec := &authResponseCodec{}
   122  	for _, version := range primitive.SupportedProtocolVersions() {
   123  		t.Run(version.String(), func(t *testing.T) {
   124  			tests := []decodeTestCase{
   125  				{
   126  					"simple auth response",
   127  					[]byte{0, 0, 0, 4, 0xca, 0xfe, 0xba, 0xbe},
   128  					&AuthResponse{token},
   129  					nil,
   130  				},
   131  			}
   132  			for _, tt := range tests {
   133  				t.Run(tt.name, func(t *testing.T) {
   134  					source := bytes.NewBuffer(tt.input)
   135  					actual, err := codec.Decode(source, version)
   136  					assert.Equal(t, tt.expected, actual)
   137  					assert.Equal(t, tt.err, err)
   138  				})
   139  			}
   140  		})
   141  	}
   142  }