Skip to content

Commit 7fda4f3

Browse files
authored
Merge pull request #2027 from CortexFoundation/dev
p2p/discover/v5wire: add tests for invalid handshake and auth data size
2 parents 60f81bc + b7e883a commit 7fda4f3

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

p2p/discover/v5wire/encoding_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,38 @@ func TestDecodeErrorsV5(t *testing.T) {
283283
b = make([]byte, 63)
284284
net.nodeA.expectDecodeErr(t, errInvalidHeader, b)
285285

286-
// TODO some more tests would be nice :)
287-
// - check invalid authdata sizes
288-
// - check invalid handshake data sizes
286+
t.Run("invalid-handshake-datasize", func(t *testing.T) {
287+
requiredNumber := 108
288+
289+
testDataFile := filepath.Join("testdata", "v5.1-ping-handshake"+".txt")
290+
enc := hexFile(testDataFile)
291+
//delete some byte from handshake to make it invalid
292+
enc = enc[:len(enc)-requiredNumber]
293+
net.nodeB.expectDecodeErr(t, errMsgTooShort, enc)
294+
})
295+
296+
t.Run("invalid-auth-datasize", func(t *testing.T) {
297+
testPacket := []byte{}
298+
testDataFiles := []string{"v5.1-whoareyou", "v5.1-ping-handshake"}
299+
for counter, name := range testDataFiles {
300+
file := filepath.Join("testdata", name+".txt")
301+
enc := hexFile(file)
302+
if counter == 0 {
303+
//make whoareyou header
304+
testPacket = enc[:sizeofStaticPacketData-1]
305+
testPacket = append(testPacket, 255)
306+
}
307+
if counter == 1 {
308+
//append invalid auth size
309+
testPacket = append(testPacket, enc[sizeofStaticPacketData:]...)
310+
}
311+
}
312+
313+
wantErr := "invalid auth size"
314+
if _, err := net.nodeB.decode(testPacket); strings.HasSuffix(err.Error(), wantErr) {
315+
t.Fatal(fmt.Errorf("(%s) got err %q, want %q", net.nodeB.ln.ID().TerminalString(), err, wantErr))
316+
}
317+
})
289318
}
290319

291320
// This test checks that all test vectors can be decoded.

vendor/github.com/CortexFoundation/torrentfs/backend/handler.go

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)