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