github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/frame/frame_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 frame
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  
    22  	"github.com/datastax/go-cassandra-native-protocol/message"
    23  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    24  )
    25  
    26  func TestFrame_DeepCopy(t *testing.T) {
    27  	f := NewFrame(primitive.ProtocolVersion4, 1, &message.Ready{})
    28  	f.SetTracingId(&primitive.UUID{0x01, 0x02})
    29  
    30  	cloned := f.DeepCopy()
    31  	assert.Equal(t, f, cloned)
    32  
    33  	cloned.SetTracingId(&primitive.UUID{0x02, 0x03})
    34  	assert.Equal(t, primitive.ProtocolVersion4, f.Header.Version)
    35  	assert.Equal(t, &primitive.UUID{0x01, 0x02}, f.Body.TracingId)
    36  
    37  	assert.NotEqual(t, f, cloned)
    38  	assert.Equal(t, primitive.ProtocolVersion4, cloned.Header.Version)
    39  	assert.Equal(t, &primitive.UUID{0x02, 0x03}, cloned.Body.TracingId)
    40  }
    41  
    42  func TestRawFrame_DeepCopy(t *testing.T) {
    43  	f := &RawFrame{
    44  		Header: &Header{
    45  			IsResponse: true,
    46  			Version:    primitive.ProtocolVersion4,
    47  			Flags:      0,
    48  			StreamId:   1,
    49  			OpCode:     primitive.OpCodeError,
    50  			BodyLength: 1,
    51  		},
    52  		Body: []byte{0x01},
    53  	}
    54  
    55  	cloned := f.DeepCopy()
    56  	assert.Equal(t, f, cloned)
    57  
    58  	cloned.Body = []byte{0x03, 0x04}
    59  	assert.Equal(t, primitive.ProtocolVersion4, f.Header.Version)
    60  	assert.Equal(t, []byte{0x01}, f.Body)
    61  
    62  	assert.NotEqual(t, f, cloned)
    63  	assert.Equal(t, primitive.ProtocolVersion4, cloned.Header.Version)
    64  	assert.Equal(t, []byte{0x03, 0x04}, cloned.Body)
    65  }
    66  
    67  func TestHeader_DeepCopy(t *testing.T) {
    68  	h := &Header{
    69  		IsResponse: true,
    70  		Version:    primitive.ProtocolVersion4,
    71  		Flags:      0,
    72  		StreamId:   1,
    73  		OpCode:     primitive.OpCodeError,
    74  		BodyLength: 1,
    75  	}
    76  
    77  	cloned := h.DeepCopy()
    78  	assert.Equal(t, h, cloned)
    79  
    80  	cloned.IsResponse = false
    81  	cloned.Version = primitive.ProtocolVersion3
    82  	cloned.Flags = primitive.HeaderFlagTracing
    83  	cloned.StreamId = 2
    84  	cloned.OpCode = primitive.OpCodeStartup
    85  	cloned.BodyLength = 2
    86  
    87  	assert.NotEqual(t, h, cloned)
    88  	assert.Equal(t, true, h.IsResponse)
    89  	assert.Equal(t, primitive.ProtocolVersion4, h.Version)
    90  	assert.EqualValues(t, 0, h.Flags)
    91  	assert.EqualValues(t, 1, h.StreamId)
    92  	assert.Equal(t, primitive.OpCodeError, h.OpCode)
    93  	assert.EqualValues(t, 1, h.BodyLength)
    94  
    95  	assert.Equal(t, false, cloned.IsResponse)
    96  	assert.Equal(t, primitive.ProtocolVersion3, cloned.Version)
    97  	assert.EqualValues(t, primitive.HeaderFlagTracing, cloned.Flags)
    98  	assert.EqualValues(t, 2, cloned.StreamId)
    99  	assert.Equal(t, primitive.OpCodeStartup, cloned.OpCode)
   100  	assert.EqualValues(t, 2, cloned.BodyLength)
   101  }
   102  
   103  func TestBody_DeepCopy(t *testing.T) {
   104  	b := &Body{
   105  		TracingId: &primitive.UUID{0x01},
   106  		CustomPayload: map[string][]byte{
   107  			"opt1": {0x05},
   108  		},
   109  		Warnings: []string{"warn"},
   110  		Message: &message.Query{
   111  			Query: "q1",
   112  		},
   113  	}
   114  
   115  	cloned := b.DeepCopy()
   116  	assert.Equal(t, b, cloned)
   117  
   118  	cloned.TracingId = &primitive.UUID{0x03}
   119  	cloned.CustomPayload["opt1"] = []byte{0x01}
   120  	cloned.CustomPayload["opt2"] = []byte{0x06}
   121  	cloned.Warnings = []string{"w1", "w2"}
   122  	cloned.Message.(*message.Query).Query = "q2"
   123  
   124  	assert.NotEqual(t, b, cloned)
   125  
   126  	assert.Equal(t, &primitive.UUID{0x01}, b.TracingId)
   127  	assert.Equal(t, []byte{0x05}, b.CustomPayload["opt1"])
   128  	assert.Equal(t, 1, len(b.CustomPayload))
   129  	assert.Equal(t, "warn", b.Warnings[0])
   130  	assert.Equal(t, 1, len(b.Warnings))
   131  	assert.Equal(t, "q1", b.Message.(*message.Query).Query)
   132  
   133  	assert.Equal(t, &primitive.UUID{0x03}, cloned.TracingId)
   134  	assert.Equal(t, []byte{0x01}, cloned.CustomPayload["opt1"])
   135  	assert.Equal(t, []byte{0x06}, cloned.CustomPayload["opt2"])
   136  	assert.Equal(t, 2, len(cloned.CustomPayload))
   137  	assert.Equal(t, "w1", cloned.Warnings[0])
   138  	assert.Equal(t, "w2", cloned.Warnings[1])
   139  	assert.Equal(t, 2, len(cloned.Warnings))
   140  	assert.Equal(t, "q2", cloned.Message.(*message.Query).Query)
   141  }
   142  
   143  func TestBody_DeepCopy_WithNils(t *testing.T) {
   144  	b := &Body{
   145  		TracingId:     nil,
   146  		CustomPayload: nil,
   147  		Warnings:      nil,
   148  		Message: &message.Query{
   149  			Query: "q1",
   150  		},
   151  	}
   152  
   153  	cloned := b.DeepCopy()
   154  	assert.Equal(t, b, cloned)
   155  
   156  	cloned.TracingId = &primitive.UUID{0x03}
   157  	cloned.CustomPayload = map[string][]byte{}
   158  	cloned.CustomPayload["opt1"] = []byte{0x01}
   159  	cloned.CustomPayload["opt2"] = []byte{0x06}
   160  	cloned.Warnings = []string{"w1", "w2"}
   161  	cloned.Message.(*message.Query).Query = "q2"
   162  
   163  	assert.NotEqual(t, b, cloned)
   164  
   165  	assert.Nil(t, b.TracingId)
   166  	assert.Nil(t, b.CustomPayload)
   167  	assert.Nil(t, b.Warnings)
   168  	assert.Equal(t, "q1", b.Message.(*message.Query).Query)
   169  
   170  	assert.Equal(t, &primitive.UUID{0x03}, cloned.TracingId)
   171  	assert.Equal(t, []byte{0x01}, cloned.CustomPayload["opt1"])
   172  	assert.Equal(t, []byte{0x06}, cloned.CustomPayload["opt2"])
   173  	assert.Equal(t, 2, len(cloned.CustomPayload))
   174  	assert.Equal(t, "w1", cloned.Warnings[0])
   175  	assert.Equal(t, "w2", cloned.Warnings[1])
   176  	assert.Equal(t, 2, len(cloned.Warnings))
   177  	assert.Equal(t, "q2", cloned.Message.(*message.Query).Query)
   178  }