github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/long_string_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 TestReadLongString(t *testing.T) {
    28  	tests := []struct {
    29  		name      string
    30  		source    []byte
    31  		expected  string
    32  		remaining []byte
    33  		err       error
    34  	}{
    35  		{"simple string", []byte{0, 0, 0, 5, h, e, l, l, o}, "hello", []byte{}, nil},
    36  		{"string with remaining", []byte{0, 0, 0, 5, h, e, l, l, o, 1, 2, 3, 4}, "hello", []byte{1, 2, 3, 4}, nil},
    37  		{"empty string", []byte{0, 0, 0, 0}, "", []byte{}, nil},
    38  		{"non-ASCII string", []byte{
    39  			0, 0, 0, 15, // length
    40  			0xce, 0xb3, 0xce, 0xb5, 0xce, 0xb9, 0xce, 0xac, //γειά
    41  			0x20,                               // space
    42  			0xcf, 0x83, 0xce, 0xbf, 0xcf, 0x85, // σου
    43  		}, "γειά σου", []byte{}, nil},
    44  		{
    45  			"cannot read length",
    46  			[]byte{0, 0, 0},
    47  			"",
    48  			[]byte{},
    49  			fmt.Errorf("cannot read [long string] length: %w", fmt.Errorf("cannot read [int]: %w", errors.New("unexpected EOF"))),
    50  		},
    51  		{
    52  			"cannot read string",
    53  			[]byte{0, 0, 0, 5, h, e, l, l},
    54  			"",
    55  			[]byte{},
    56  			fmt.Errorf("cannot read [long string] content: %w", errors.New("unexpected EOF")),
    57  		},
    58  	}
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			buf := bytes.NewReader(tt.source)
    62  			actual, err := ReadLongString(buf)
    63  			assert.Equal(t, tt.expected, actual)
    64  			assert.Equal(t, tt.err, err)
    65  			remaining, _ := ioutil.ReadAll(buf)
    66  			assert.Equal(t, tt.remaining, remaining)
    67  		})
    68  	}
    69  }
    70  
    71  func TestWriteLongString(t *testing.T) {
    72  	tests := []struct {
    73  		name     string
    74  		input    string
    75  		expected []byte
    76  		err      error
    77  	}{
    78  		{
    79  			"simple string",
    80  			"hello",
    81  			[]byte{0, 0, 0, 5, h, e, l, l, o},
    82  			nil,
    83  		},
    84  		{
    85  			"empty string",
    86  			"",
    87  			[]byte{0, 0, 0, 0},
    88  			nil,
    89  		},
    90  		{
    91  			"non-ASCII string",
    92  			"γειά σου",
    93  			[]byte{
    94  				0, 0, 0, 15, // length
    95  				0xce, 0xb3, 0xce, 0xb5, 0xce, 0xb9, 0xce, 0xac, //γειά
    96  				0x20,                               // space
    97  				0xcf, 0x83, 0xce, 0xbf, 0xcf, 0x85, // σου
    98  			},
    99  			nil,
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			buf := &bytes.Buffer{}
   105  			err := WriteLongString(tt.input, buf)
   106  			assert.Equal(t, tt.expected, buf.Bytes())
   107  			assert.Equal(t, tt.err, err)
   108  		})
   109  	}
   110  }