You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(network): fix AvailableCommands packet for mods (minekube#534)
* fix(network): fix AvailableCommands packet for mods
Fixes: minekube#322Closes: minekube#527
Co-authored-by: @uberswe
* test(util): enhance ReadVarInt tests with additional cases and fuzzing
- Expanded unit tests for ReadVarInt to cover various scenarios including edge cases like maximum varint, empty buffer, and incomplete varint.
- Added fuzz testing for ReadVarInt to ensure robustness against malformed input.
- Introduced a benchmark for ReadVarInt to measure performance.
* revert to original ReadVarInt
* update go.sum
* fix: lint
---------
Co-authored-by: Román Benjámin <svetch.game@gmail.com>
// The read UUID should be the same as the original UUID
40
97
require.Equal(t, id, readID)
41
98
}
99
+
100
+
funcFuzzReadVarInt(f*testing.F) {
101
+
testCases:= [][]byte{
102
+
{0x01},
103
+
{0xAC, 0x02},
104
+
{0x00},
105
+
{0xff, 0xff, 0xff, 0xff, 0x07},
106
+
{0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, // too big
107
+
{}, // empty
108
+
{0xff}, // incomplete
109
+
{0x80, 0x80, 0x80, 0x80, 0x01},
110
+
}
111
+
for_, tc:=rangetestCases {
112
+
f.Add(tc)
113
+
}
114
+
115
+
f.Fuzz(func(t*testing.T, data []byte) {
116
+
buf:=bytes.NewBuffer(data)
117
+
val, err:=ReadVarInt(buf)
118
+
119
+
// We expect an error if the VarInt is too big or incomplete
120
+
// or if the input data is malformed in a way ReadVarInt can detect.
121
+
// Otherwise, we expect the read to consume some bytes and potentially return a value.
122
+
123
+
iferr!=nil {
124
+
if!errors.Is(err, io.EOF) &&err.Error() !="decode: VarInt is too big" {
125
+
// If it's another type of error, we might want to investigate.
126
+
// For now, we just ensure it doesn't panic.
127
+
t.Fatalf("ReadVarInt returned an unexpected error: %v for input %x", err, data)
128
+
}
129
+
return// Error is expected in some cases
130
+
}
131
+
132
+
// If no error, ensure val is non-negative as VarInts typically represent sizes or counts
133
+
ifval<0 {
134
+
// This case should ideally be covered by the VarInt format itself,
135
+
// as standard VarInts don't represent negative numbers directly
136
+
// unless a zigzag encoding is used (which is not the case here).
137
+
// The current ReadVarInt decodes into an int, so it *could* be negative
138
+
// if a 5-byte sequence results in a value > MaxInt32 when interpreted as uint32.
139
+
// For example: []byte{0xff, 0xff, 0xff, 0xff, 0x0f} would be -1 if read as int32.
140
+
// Let's verify this behavior or decide if ReadVarInt should error.
141
+
// The current implementation of ReadVarInt casts to int(result) where result is int32.
142
+
// If the 5th byte makes the int32 value negative, it will be negative.
143
+
t.Logf("ReadVarInt returned a negative value: %d for input %x. This may be expected due to int(int32) conversion for certain 5-byte sequences.", val, data)
144
+
}
145
+
146
+
// Check remaining bytes if any
147
+
// If the read was successful (no error), some bytes should have been consumed
148
+
// unless the input was empty and ReadVarInt errored out before this check.
0 commit comments