Skip to content

Commit 0b445a1

Browse files
committed
More testing
1 parent 3bd47aa commit 0b445a1

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

serialization/serialization.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,13 @@ func decodeVar(r io.ReadSeeker, unsigned bool) (uint64, error) {
240240
return 0, err
241241
}
242242
fieldBytes := make([]byte, tb+1)
243-
_, err = r.Read(fieldBytes)
243+
n, err := r.Read(fieldBytes)
244244
if err != nil {
245245
return 0, err
246246
}
247+
if n != tb+1{
248+
return 0, fmt.Errorf("only read %d bytes, expected %d", n, tb+1)
249+
}
247250
var tNum uint64
248251
switch len(fieldBytes) {
249252
case 1:

serialization/serialization_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,69 @@ func TestDecodeString(t *testing.T) {
112112
if tc.err == "" {
113113
require.NoError(t, err)
114114
require.Equal(t, tc.result, s)
115-
}else {
115+
} else {
116+
require.ErrorContains(t, err, tc.err)
117+
}
118+
}
119+
}
120+
121+
func TestDecodeVar(t *testing.T) {
122+
testcases := []struct {
123+
input []byte
124+
unsigned bool
125+
result uint64
126+
err string
127+
}{
128+
{
129+
[]byte{},
130+
false,
131+
0,
132+
"EOF",
133+
},
134+
{
135+
[]byte{0xd9},
136+
false,
137+
0,
138+
"only read ",
139+
},
140+
{
141+
[]byte{0x4},
142+
false,
143+
1,
144+
"",
145+
},
146+
{
147+
[]byte{0xd9, 0x03},
148+
false,
149+
123,
150+
"",
151+
},
152+
{
153+
[]byte{0xc3, 02, 0x0b},
154+
true,
155+
90200,
156+
"",
157+
},
158+
// {
159+
// []byte{0x5d, 0x03},
160+
// true,
161+
// 215,
162+
// "",
163+
// },
164+
// {
165+
// []byte{0x7f, 0x39, 0x7d, 0x89, 0x70, 0xdb, 0x2d, 0x06},
166+
// true,
167+
// 1739270369410361,
168+
// "",
169+
// },
170+
}
171+
172+
for _, tc := range testcases{
173+
r, err := decodeVar(bytes.NewReader(tc.input), tc.unsigned)
174+
if tc.err == "" {
175+
require.NoError(t, err)
176+
require.Equal(t, tc.result, r)
177+
} else {
116178
require.ErrorContains(t, err, tc.err)
117179
}
118180
}

0 commit comments

Comments
 (0)