MCPcopy Create free account
hub / github.com/CrowdStrike/csproto / TestDecoderInvalidSkip

Function TestDecoderInvalidSkip

decoder_test.go:1191–1240  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1189}
1190
1191func TestDecoderInvalidSkip(t *testing.T) {
1192 var data = []byte{
1193 // 1 (varint): 42
1194 0x8, 0x2A,
1195 }
1196 var skipErr *csproto.DecoderSkipError
1197
1198 dec := csproto.NewDecoder(data)
1199 // skip with incorrect tag
1200 _, err := dec.Skip(2, csproto.WireTypeVarint)
1201 assert.ErrorAs(t, err, &skipErr)
1202 // skip with incorrect wire types
1203 for _, wt := range []csproto.WireType{csproto.WireTypeFixed64, csproto.WireTypeLengthDelimited, csproto.WireTypeFixed32} {
1204 _, err := dec.Skip(1, wt)
1205 assert.ErrorAs(t, err, &skipErr)
1206 }
1207
1208 t.Run("corrupt messages", func(t *testing.T) {
1209 t.Run("truncated data", func(t *testing.T) {
1210 // length-delimited field value with a length of 3 but only 2 bytes
1211 data := []byte{0x0A, 0x3, 0x42, 0x11}
1212 dec := csproto.NewDecoder(data)
1213
1214 tag, wt, _ := dec.DecodeTag()
1215 got, err := dec.Skip(tag, wt)
1216 assert.Error(t, err)
1217 assert.Nil(t, got)
1218 })
1219 t.Run("negative length", func(t *testing.T) {
1220 // length-delimited field value with a length of -50
1221 data := []byte{0x0A, 0xCE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x3, 0x42, 0x11, 0x38}
1222 dec := csproto.NewDecoder(data)
1223
1224 tag, wt, _ := dec.DecodeTag()
1225 got, err := dec.Skip(tag, wt)
1226 assert.Error(t, err)
1227 assert.Nil(t, got)
1228 })
1229 t.Run("length overflow", func(t *testing.T) {
1230 // length-delimited field with length greater than 2GB
1231 data := []byte{0x0A, 0x80, 0x80, 0x80, 0x80, 0x08}
1232 dec := csproto.NewDecoder(data)
1233
1234 tag, wt, _ := dec.DecodeTag()
1235 got, err := dec.Skip(tag, wt)
1236 assert.Error(t, err)
1237 assert.Nil(t, got)
1238 })
1239 })
1240}
1241
1242func TestDecodePastEndOfBuffer(t *testing.T) {
1243 var data = []byte{

Callers

nothing calls this directly

Calls 4

SkipMethod · 0.95
DecodeTagMethod · 0.95
NewDecoderFunction · 0.92
ErrorMethod · 0.45

Tested by

no test coverage detected