github.com/m3db/m3@v1.5.0/src/x/serialize/unchecked_decoder_test.go (about) 1 // Copyright (c) 2020 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package serialize 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/require" 27 ) 28 29 func TestUncheckedEmptyDecode(t *testing.T) { 30 var b []byte 31 b = append(b, headerMagicBytes...) 32 b = append(b, []byte{0x0, 0x0}...) 33 34 d := newTestUncheckedTagDecoder() 35 d.reset(b) 36 require.False(t, d.next()) 37 require.NoError(t, d.err) 38 } 39 40 func TestUncheckedNilDecode(t *testing.T) { 41 d := newTestUncheckedTagDecoder() 42 d.reset(nil) 43 require.False(t, d.next()) 44 require.NoError(t, d.err) 45 } 46 47 func TestUncheckedEmptyTagNameDecode(t *testing.T) { 48 var b []byte 49 b = append(b, headerMagicBytes...) 50 b = append(b, encodeUInt16(1, make([]byte, 2))...) /* num tags */ 51 b = append(b, encodeUInt16(0, make([]byte, 2))...) /* len empty string */ 52 b = append(b, encodeUInt16(4, make([]byte, 2))...) /* len defg */ 53 b = append(b, []byte("defg")...) 54 55 d := newTestUncheckedTagDecoder() 56 d.reset(b) 57 require.False(t, d.next()) 58 require.Error(t, d.err) 59 } 60 61 func TestUncheckedEmptyTagValueDecode(t *testing.T) { 62 var b []byte 63 b = append(b, headerMagicBytes...) 64 b = append(b, encodeUInt16(1, make([]byte, 2))...) /* num tags */ 65 b = append(b, encodeUInt16(1, make([]byte, 2))...) /* len "1" */ 66 b = append(b, []byte("a")...) /* tag name */ 67 b = append(b, encodeUInt16(0, make([]byte, 2))...) /* len tag value */ 68 69 d := newTestUncheckedTagDecoder() 70 d.reset(b) 71 assertUncheckedNextTag(t, d, "a", "") 72 require.False(t, d.next()) 73 require.NoError(t, d.err) 74 } 75 76 func TestUncheckedDecodeHeaderMissing(t *testing.T) { 77 var b []byte 78 b = append(b, []byte{0x0, 0x0}...) 79 b = append(b, []byte{0x0, 0x0}...) 80 81 d := newTestUncheckedTagDecoder() 82 d.reset(b) 83 require.False(t, d.next()) 84 require.Error(t, d.err) 85 } 86 87 func TestUncheckedDecodeResetsErrState(t *testing.T) { 88 var b []byte 89 b = append(b, []byte{0x0, 0x0}...) 90 b = append(b, []byte{0x0, 0x0}...) 91 92 d := newTestUncheckedTagDecoder() 93 d.reset(b) 94 require.False(t, d.next()) 95 require.Error(t, d.err) 96 97 d.reset(nil) 98 require.NoError(t, d.err) 99 } 100 101 func TestUncheckedDecodeSimple(t *testing.T) { 102 b := testTagDecoderBytesRaw() 103 d := newTestUncheckedTagDecoder() 104 d.reset(b) 105 require.NoError(t, d.err) 106 107 assertUncheckedNextTag(t, d, "abc", "defg") 108 assertUncheckedNextTag(t, d, "x", "bar") 109 110 require.False(t, d.next()) 111 require.NoError(t, d.err) 112 } 113 114 func TestUncheckedDecodeTooManyTags(t *testing.T) { 115 b := testTagDecoderBytesRaw() 116 d := newUncheckedTagDecoder(NewTagSerializationLimits().SetMaxNumberTags(1)) 117 d.reset(b) 118 require.Error(t, d.err) 119 } 120 121 func TestUncheckedDecodeLiteralTooLong(t *testing.T) { 122 b := testTagDecoderBytesRaw() 123 d := newUncheckedTagDecoder(NewTagSerializationLimits(). 124 SetMaxNumberTags(2). 125 SetMaxTagLiteralLength(3)) 126 d.reset(b) 127 require.NoError(t, d.err) 128 require.False(t, d.next()) 129 require.Error(t, d.err) 130 } 131 132 func TestUncheckedDecodeMissingTags(t *testing.T) { 133 var b []byte 134 b = append(b, headerMagicBytes...) 135 b = append(b, encodeUInt16(2, make([]byte, 2))...) /* num tags */ 136 137 d := newTestUncheckedTagDecoder() 138 d.reset(b) 139 require.NoError(t, d.err) 140 141 require.False(t, d.next()) 142 require.Error(t, d.err) 143 } 144 145 func TestUncheckedDecodeMissingValue(t *testing.T) { 146 var b []byte 147 b = append(b, headerMagicBytes...) 148 b = append(b, encodeUInt16(2, make([]byte, 2))...) /* num tags */ 149 b = append(b, encodeUInt16(3, make([]byte, 2))...) /* len abc */ 150 b = append(b, []byte("abc")...) 151 152 b = append(b, encodeUInt16(4, make([]byte, 2))...) /* len defg */ 153 b = append(b, []byte("defg")...) 154 155 b = append(b, encodeUInt16(1, make([]byte, 2))...) /* len x */ 156 b = append(b, []byte("x")...) 157 158 d := newTestUncheckedTagDecoder() 159 d.reset(b) 160 require.NoError(t, d.err) 161 162 assertUncheckedNextTag(t, d, "abc", "defg") 163 require.False(t, d.next()) 164 require.Error(t, d.err) 165 } 166 167 // assert the next tag in the iteration. ensures the decoder and faster decoder match. 168 func assertUncheckedNextTag(t *testing.T, d *uncheckedDecoder, name, value string) { 169 require.True(t, d.next()) 170 curName, curValue := d.current() 171 require.Equal(t, name, string(curName)) 172 require.Equal(t, value, string(curValue)) 173 } 174 175 func newTestUncheckedTagDecoder() *uncheckedDecoder { 176 return newUncheckedTagDecoder(NewTagSerializationLimits()) 177 }