Skip to content

Commit

Permalink
fix fast set generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Sep 26, 2019
1 parent da66340 commit 598879b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/fast/fast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GenerateFastSet(k int, numPieces uint32, infoHash [20]byte, ip net.IP) []ui
return false
}
h := sha1.New() // nolint: gosec
for len(a) < k {
for j := 0; j < k && len(a) < k; j++ {
_, _ = h.Write(x)
x = h.Sum(x[:0])
h.Reset()
Expand Down
24 changes: 24 additions & 0 deletions internal/fast/fast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,28 @@ func TestGenerateFastSet(t *testing.T) {
t.Log(a)
t.FailNow()
}

a = GenerateFastSet(1, 1313, ih, net.IPv4(80, 4, 4, 200))
expected = []uint32{1059}
if !reflect.DeepEqual(a, expected) {
t.Log(expected)
t.Log(a)
t.FailNow()
}

a = GenerateFastSet(10, 1, ih, net.IPv4(80, 4, 4, 200))
expected = []uint32{0}
if !reflect.DeepEqual(a, expected) {
t.Log(expected)
t.Log(a)
t.FailNow()
}

a = GenerateFastSet(4, 2, ih, net.IPv4(80, 4, 4, 200))
expected = []uint32{1, 0}
if !reflect.DeepEqual(a, expected) {
t.Log(expected)
t.Log(a)
t.FailNow()
}
}

0 comments on commit 598879b

Please sign in to comment.