github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/string_map_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 TestReadStringMap(t *testing.T) {
    28  	tests := []struct {
    29  		name      string
    30  		source    []byte
    31  		expected  map[string]string
    32  		remaining []byte
    33  		err       error
    34  	}{
    35  		{"empty string map", []byte{0, 0}, map[string]string{}, []byte{}, nil},
    36  		{"map 1 key", []byte{
    37  			0, 1, // map length
    38  			0, 5, h, e, l, l, o, // key: hello
    39  			0, 5, w, o, r, l, d, // value1: world
    40  		}, map[string]string{"hello": "world"}, []byte{}, nil},
    41  		// FIXME map iteration order
    42  		//{"map 2 keys", []byte{
    43  		//	0, 2, // map length
    44  		//	0, 5, h, e, l, l, o, // key1: hello
    45  		//	0, 5, w, o, r, l, d, // value1: world
    46  		//	0, 6, h, o, l, 0xc3, 0xa0, 0x21, // key2: holà!
    47  		//	0, 5, m, u, n, d, o, // value2: mundo
    48  		//}, map[string]string{
    49  		//	"hello": "world",
    50  		//	"holà!": "mundo",
    51  		//}, []byte{}, nil},
    52  		{
    53  			"cannot read map length",
    54  			[]byte{0},
    55  			nil,
    56  			[]byte{},
    57  			fmt.Errorf("cannot read [string map] length: %w",
    58  				fmt.Errorf("cannot read [short]: %w",
    59  					errors.New("unexpected EOF"))),
    60  		},
    61  		{
    62  			"cannot read key length",
    63  			[]byte{0, 1, 0},
    64  			nil,
    65  			[]byte{},
    66  			fmt.Errorf("cannot read [string map] entry 0 key: %w",
    67  				fmt.Errorf("cannot read [string] length: %w",
    68  					fmt.Errorf("cannot read [short]: %w",
    69  						errors.New("unexpected EOF")))),
    70  		},
    71  		{
    72  			"cannot read key",
    73  			[]byte{0, 1, 0, 2, 0},
    74  			nil,
    75  			[]byte{},
    76  			fmt.Errorf("cannot read [string map] entry 0 key: %w",
    77  				fmt.Errorf("cannot read [string] content: %w",
    78  					errors.New("unexpected EOF"))),
    79  		},
    80  		{
    81  			"cannot read value length",
    82  			[]byte{0, 1, 0, 1, k, 0},
    83  			nil,
    84  			[]byte{},
    85  			fmt.Errorf("cannot read [string map] entry 0 value: %w",
    86  				fmt.Errorf("cannot read [string] length: %w",
    87  					fmt.Errorf("cannot read [short]: %w", errors.New("unexpected EOF")))),
    88  		},
    89  		{
    90  			"cannot read value",
    91  			[]byte{0, 1, 0, 1, k, 0, 2, 0},
    92  			nil,
    93  			[]byte{},
    94  			fmt.Errorf(
    95  				"cannot read [string map] entry 0 value: %w",
    96  				fmt.Errorf("cannot read [string] content: %w", errors.New("unexpected EOF")),
    97  			),
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			buf := bytes.NewReader(tt.source)
   103  			actual, err := ReadStringMap(buf)
   104  			assert.Equal(t, tt.expected, actual)
   105  			assert.Equal(t, tt.err, err)
   106  			remaining, _ := ioutil.ReadAll(buf)
   107  			assert.Equal(t, tt.remaining, remaining)
   108  		})
   109  	}
   110  }
   111  
   112  func TestWriteStringMap(t *testing.T) {
   113  	tests := []struct {
   114  		name     string
   115  		input    map[string]string
   116  		expected []byte
   117  		err      error
   118  	}{
   119  		{
   120  			"empty string map",
   121  			map[string]string{},
   122  			[]byte{0, 0},
   123  			nil,
   124  		},
   125  		// not officially allowed by the specs, but better safe than sorry
   126  		{
   127  			"nil string map",
   128  			nil,
   129  			[]byte{0, 0},
   130  			nil,
   131  		},
   132  		{
   133  			"map 1 key",
   134  			map[string]string{"hello": "world"},
   135  			[]byte{
   136  				0, 1, // map length
   137  				0, 5, h, e, l, l, o, // key: hello
   138  				0, 5, w, o, r, l, d, // value1: world
   139  			},
   140  			nil,
   141  		},
   142  		// FIXME map iteration order
   143  		//{"map 2 keys",
   144  		//	map[string]string{
   145  		//		"hello": "world",
   146  		//		"holà!": "mundo",
   147  		//	},
   148  		//	[]byte{
   149  		//		0, 2, // map length
   150  		//		0, 5, h, e, l, l, o, // key1: hello
   151  		//		0, 5, w, o, r, l, d, // value1: world
   152  		//		0, 6, h, o, l, 0xc3, 0xa0, 0x21, // key2: holà!
   153  		//		0, 5, m, u, n, d, o, // value2: mundo
   154  		//	}, nil},
   155  	}
   156  	for _, tt := range tests {
   157  		t.Run(tt.name, func(t *testing.T) {
   158  			buf := &bytes.Buffer{}
   159  			err := WriteStringMap(tt.input, buf)
   160  			assert.Equal(t, tt.expected, buf.Bytes())
   161  			assert.Equal(t, tt.err, err)
   162  		})
   163  	}
   164  }