github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/short_bytes_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 primitive
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"io/ioutil"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestReadShortBytes(t *testing.T) {
    28  	tests := []struct {
    29  		name      string
    30  		source    []byte
    31  		expected  []byte
    32  		remaining []byte
    33  		err       error
    34  	}{
    35  		{"empty short bytes", []byte{0, 0}, []byte{}, []byte{}, nil},
    36  		{"singleton short bytes", []byte{0, 1, 1}, []byte{1}, []byte{}, nil},
    37  		{"simple short bytes", []byte{0, 2, 1, 2}, []byte{1, 2}, []byte{}, nil},
    38  		{
    39  			"cannot read short bytes length",
    40  			[]byte{0},
    41  			nil,
    42  			[]byte{},
    43  			fmt.Errorf("cannot read [short bytes] length: %w", fmt.Errorf("cannot read [short]: %w", errors.New("unexpected EOF"))),
    44  		},
    45  		{
    46  			"cannot read short bytes content",
    47  			[]byte{0, 2, 1},
    48  			nil,
    49  			[]byte{},
    50  			fmt.Errorf("cannot read [short bytes] content: %w", errors.New("unexpected EOF")),
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			buf := bytes.NewReader(tt.source)
    56  			actual, err := ReadShortBytes(buf)
    57  			assert.Equal(t, tt.expected, actual)
    58  			assert.Equal(t, tt.err, err)
    59  			remaining, _ := ioutil.ReadAll(buf)
    60  			assert.Equal(t, tt.remaining, remaining)
    61  		})
    62  	}
    63  }
    64  
    65  func TestWriteShortBytes(t *testing.T) {
    66  	tests := []struct {
    67  		name     string
    68  		input    []byte
    69  		expected []byte
    70  		err      error
    71  	}{
    72  		{
    73  			"empty short bytes",
    74  			[]byte{},
    75  			[]byte{0, 0},
    76  			nil,
    77  		},
    78  		// not officially allowed by the specs, but better safe than sorry
    79  		{
    80  			"nil short bytes",
    81  			nil,
    82  			[]byte{0, 0},
    83  			nil,
    84  		},
    85  		{
    86  			"singleton short bytes",
    87  			[]byte{1},
    88  			[]byte{0, 1, 1},
    89  			nil,
    90  		},
    91  		{
    92  			"simple short bytes",
    93  			[]byte{1, 2},
    94  			[]byte{0, 2, 1, 2},
    95  			nil,
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			buf := &bytes.Buffer{}
   101  			err := WriteShortBytes(tt.input, buf)
   102  			assert.Equal(t, tt.expected, buf.Bytes())
   103  			assert.Equal(t, tt.err, err)
   104  		})
   105  	}
   106  }