github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/client/auth_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
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestAuthCredentials_Copy(t *testing.T) {
    24  
    25  	credentials := &AuthCredentials{
    26  		Username: "user1",
    27  		Password: "pass1",
    28  	}
    29  
    30  	cpy := credentials.Copy()
    31  	assert.Equal(t, credentials, cpy)
    32  
    33  	cpy.Username = "user2"
    34  	cpy.Password = "pass2"
    35  
    36  	assert.NotEqual(t, credentials, cpy)
    37  
    38  }
    39  
    40  func TestAuthCredentials_Marshal(t *testing.T) {
    41  
    42  	credentials := &AuthCredentials{
    43  		Username: "user1",
    44  		Password: "pass1",
    45  	}
    46  
    47  	bytes := credentials.Marshal()
    48  
    49  	assert.Equal(t, []byte{
    50  		0,
    51  		byte('u'), byte('s'), byte('e'), byte('r'), byte('1'),
    52  		0,
    53  		byte('p'), byte('a'), byte('s'), byte('s'), byte('1'),
    54  	}, bytes)
    55  }
    56  
    57  func TestAuthCredentials_Unmarshal(t *testing.T) {
    58  
    59  	credentials := &AuthCredentials{}
    60  
    61  	err := credentials.Unmarshal([]byte{
    62  		0,
    63  		byte('u'), byte('s'), byte('e'), byte('r'), byte('1'),
    64  		0,
    65  		byte('p'), byte('a'), byte('s'), byte('s'), byte('1'),
    66  	})
    67  
    68  	assert.Nil(t, err)
    69  	assert.Equal(t, "user1", credentials.Username)
    70  	assert.Equal(t, "pass1", credentials.Password)
    71  }