Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ import (
"context"
"crypto/rand"
"net"
"regexp"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"

"github.com/go-quicktest/qt"
)

func TestAnnounceNoStartingNodes(t *testing.T) {
s, err := NewServer(&ServerConfig{
Conn: mustListen(":0"),
NoSecurity: true,
})
require.NoError(t, err)
qt.Assert(t, qt.IsNil(err))
defer s.Close()
var ih [20]byte
copy(ih[:], "blah")
_, err = s.Announce(ih, 0, true)
require.EqualError(t, err, "no initial nodes")
qt.Assert(t, qt.ErrorMatches(err, regexp.QuoteMeta("no initial nodes")))
}

func randomInfohash() (ih [20]byte) {
Expand All @@ -37,9 +38,9 @@ func TestAnnounceStopsNoPending(t *testing.T) {
return []Addr{NewAddr(&net.TCPAddr{})}, nil
},
})
require.NoError(t, err)
qt.Assert(t, qt.IsNil(err))
a, err := s.Announce(randomInfohash(), 0, true)
require.NoError(t, err)
qt.Assert(t, qt.IsNil(err))
defer a.Close()
<-a.Peers
}
Expand All @@ -49,10 +50,10 @@ func TestAnnounceStopsNoPending(t *testing.T) {
// are successful.
func TestRateLimiterInadequate(t *testing.T) {
rl := rate.NewLimiter(rate.Every(time.Hour), 1)
assert.NoError(t, rl.Wait(context.Background()))
qt.Check(t, qt.IsNil(rl.Wait(context.Background())))
time.AfterFunc(time.Millisecond, func() { rl.AllowN(time.Now(), -1) })
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
time.AfterFunc(2*time.Millisecond, cancel)
assert.EqualValues(t, context.Canceled, rl.Wait(ctx))
qt.Check(t, qt.Equals(rl.Wait(ctx), context.Canceled))
}
43 changes: 12 additions & 31 deletions bep44/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,27 @@ import (
"testing"

"filippo.io/edwards25519"
qt "github.com/frankban/quicktest"
"github.com/go-quicktest/qt"
)

func TestPrivateKeyIsSeed(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
c := qt.New(t)
c.Assert(err, qt.IsNil)
qt.Assert(t, qt.IsNil(err))
t.Logf("generated private key %x", priv)
seed := priv.Seed()
t.Logf("it has seed %x", seed)
seedPriv := ed25519.NewKeyFromSeed(seed)
t.Logf("private key from seed: %x", seedPriv)
c.Check(seedPriv.Equal(priv), qt.IsTrue)
c.Check(
seedPriv.Public().(ed25519.PublicKey),
qt.ContentEquals,
priv.Public().(ed25519.PublicKey))
qt.Check(t, qt.IsTrue(seedPriv.Equal(priv)))
qt.Check(t, qt.ContentEquals(seedPriv.Public().(ed25519.PublicKey), priv.Public().(ed25519.PublicKey)))
t.Logf("public keys:\n%x\n%x", seedPriv.Public(), priv.Public())
}

func TestPublicKeyCopy(t *testing.T) {
pub, _, err := ed25519.GenerateKey(nil)
c := qt.New(t)
c.Assert(err, qt.IsNil)
qt.Assert(t, qt.IsNil(err))
b := (*[32]byte)(pub)
c.Check(b[:], qt.DeepEquals, []byte(pub))
qt.Check(t, qt.DeepEquals(b[:], []byte(pub)))
}

func mustDecodeHex(s string) []byte {
Expand All @@ -44,25 +39,18 @@ func mustDecodeHex(s string) []byte {
}

func TestVectorMutableWithSalt(t *testing.T) {
c := qt.New(t)
salt := []byte("foobar")
bv := []byte("12:Hello World!")
var seq int64 = 1
c.Check(
bufferToSign(salt, bv, seq),
qt.DeepEquals,
[]byte("4:salt6:foobar3:seqi1e1:v12:Hello World!"))
qt.Check(t, qt.DeepEquals(bufferToSign(salt, bv, seq), []byte("4:salt6:foobar3:seqi1e1:v12:Hello World!")))
edwardsPrivateKey := mustDecodeHex(
"e06d3183d14159228433ed599221b80bd0a5ce8352e4bdf0262f76786ef1c74d" +
"b7e7a9fea2c0eb269d61e3b38e450a22e754941ac78479d6c54e1faf6037881d")
sig := mustDecodeHex(
"6834284b6b24c3204eb2fea824d82f88883a3d95e8b4a21b8c0ded553d17d17d" +
"df9a8a7104b1258f30bed3787e6cb896fca78c58f8e03b5f18f14951a87d9a08")
pubKey := mustDecodeHex("77ff84905a91936367c01360803104f92432fcd904a43511876df5cdf3e7e548")
c.Check(
EdwardsSignSha512(*(*[64]byte)(edwardsPrivateKey), pubKey, bufferToSign(salt, bv, seq)),
qt.DeepEquals,
sig)
qt.Check(t, qt.DeepEquals(EdwardsSignSha512(*(*[64]byte)(edwardsPrivateKey), pubKey, bufferToSign(salt, bv, seq)), sig))
expectedTargetId := mustDecodeHex("411eba73b6f087ca51a3795d9c8c938d365e32c1")
put := Put{
V: "Hello World!",
Expand All @@ -73,29 +61,22 @@ func TestVectorMutableWithSalt(t *testing.T) {
Seq: 0,
}
target := put.Target()
c.Check(target[:], qt.DeepEquals, expectedTargetId)
qt.Check(t, qt.DeepEquals(target[:], expectedTargetId))
}

func TestVectorMutable(t *testing.T) {
c := qt.New(t)
var salt []byte
bv := []byte("12:Hello World!")
var seq int64 = 1
c.Check(
bufferToSign(salt, bv, seq),
qt.DeepEquals,
[]byte("3:seqi1e1:v12:Hello World!"))
qt.Check(t, qt.DeepEquals(bufferToSign(salt, bv, seq), []byte("3:seqi1e1:v12:Hello World!")))
edwardsPrivateKey := mustDecodeHex(
"e06d3183d14159228433ed599221b80bd0a5ce8352e4bdf0262f76786ef1c74d" +
"b7e7a9fea2c0eb269d61e3b38e450a22e754941ac78479d6c54e1faf6037881d")
sig := mustDecodeHex(
"305ac8aeb6c9c151fa120f120ea2cfb923564e11552d06a5d856091e5e853cff" +
"1260d3f39e4999684aa92eb73ffd136e6f4f3ecbfda0ce53a1608ecd7ae21f01")
pubKey := mustDecodeHex("77ff84905a91936367c01360803104f92432fcd904a43511876df5cdf3e7e548")
c.Check(
EdwardsSignSha512(*(*[64]byte)(edwardsPrivateKey), pubKey, bufferToSign(salt, bv, seq)),
qt.DeepEquals,
sig)
qt.Check(t, qt.DeepEquals(EdwardsSignSha512(*(*[64]byte)(edwardsPrivateKey), pubKey, bufferToSign(salt, bv, seq)), sig))
expectedTargetId := mustDecodeHex("4a533d47ec9c7d95b1ad75f576cffc641853b750")
put := Put{
V: "Hello World!",
Expand All @@ -106,7 +87,7 @@ func TestVectorMutable(t *testing.T) {
Seq: 0,
}
target := put.Target()
c.Check(target[:], qt.DeepEquals, expectedTargetId)
qt.Check(t, qt.DeepEquals(target[:], expectedTargetId))
}

// ed25519 sign with pre-sha512-summed private key. I believe this means we also need the public key
Expand Down
20 changes: 9 additions & 11 deletions bep44/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,36 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/go-quicktest/qt"
)

func TestWrapper(t *testing.T) {
require := require.New(t)
w := NewWrapper(NewMemory(), 10*time.Hour)

i, err := NewItem([]byte("Hello World!"), nil, 0, 0, nil)
require.NoError(err)
qt.Assert(t, qt.IsNil(err))

err = w.Put(i)
require.NoError(err)
qt.Assert(t, qt.IsNil(err))

target := i.Target()

targetStr := hex.EncodeToString(target[:])
require.Equal("e5f96f6f38320f0f33959cb4d3d656452117aadb", targetStr)
qt.Assert(t, qt.Equals(targetStr, "e5f96f6f38320f0f33959cb4d3d656452117aadb"))

i2, err := w.Get(target)
require.NoError(err)
require.Equal(i, i2)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(i2, i))
}

func TestWrapperTimeout(t *testing.T) {
require := require.New(t)
w := NewWrapper(NewMemory(), 0*time.Second)

i, err := NewItem([]byte("Hello World!"), nil, 0, 0, nil)
require.NoError(err)
qt.Assert(t, qt.IsNil(err))

err = w.Put(i)
require.NoError(err)
qt.Assert(t, qt.IsNil(err))
_, err = w.Get(i.Target())
require.Equal(ErrItemNotFound, err)
qt.Assert(t, qt.Equals(err, ErrItemNotFound))
}
28 changes: 12 additions & 16 deletions containers/addr-maybe-ids-by-distance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package containers
import (
"testing"

qt "github.com/frankban/quicktest"
"github.com/stretchr/testify/assert"
"github.com/go-quicktest/qt"

"github.com/anacrolix/dht/v2/int160"
"github.com/anacrolix/dht/v2/internal/testutil"
)

func TestSampleAddrsDiffer(t *testing.T) {
c := qt.New(t)
for i, a := range testutil.SampleAddrMaybeIds {
for j, b := range testutil.SampleAddrMaybeIds[i+1:] {
c.Assert(a, qt.Not(qt.Equals), b,
qt.Commentf("%v, %v", i, j+i+1))
qt.Assert(t, qt.Not(qt.Equals(a, b)), qt.Commentf("%v, %v", i, j+i+1))
}
}
}
Expand All @@ -26,28 +23,27 @@ func TestNodesByDistance(t *testing.T) {
a = a.Add(testutil.SampleAddrMaybeIds[i])
}
push(4)
c := qt.New(t)
c.Assert(a.Len(), qt.Equals, 1)
qt.Assert(t, qt.Equals(a.Len(), 1))
push(2)
c.Assert(a.Len(), qt.Equals, 2)
qt.Assert(t, qt.Equals(a.Len(), 2))
push(0)
c.Assert(a.Len(), qt.Equals, 3)
qt.Assert(t, qt.Equals(a.Len(), 3))
push(3)
c.Assert(a.Len(), qt.Equals, 4)
qt.Assert(t, qt.Equals(a.Len(), 4))
push(0)
c.Assert(a.Len(), qt.Equals, 4)
qt.Assert(t, qt.Equals(a.Len(), 4))
push(1)
c.Assert(a.Len(), qt.Equals, 5)
qt.Assert(t, qt.Equals(a.Len(), 5))
pop := func(is ...int) {
ok := a.Len() != 0
assert.True(t, ok)
qt.Check(t, qt.IsTrue(ok))
first := a.Next()
assert.Contains(t, func() (ret []addrMaybeId) {
qt.Check(t, qt.SliceContains(func() (ret []addrMaybeId) {
for _, i := range is {
ret = append(ret, testutil.SampleAddrMaybeIds[i])
}
return
}(), first)
}(), first))
a = a.Delete(first)
}
pop(1)
Expand All @@ -56,5 +52,5 @@ func TestNodesByDistance(t *testing.T) {
pop(0, 4)
pop(0, 4)
// pop(0, 4)
assert.EqualValues(t, 0, a.Len())
qt.Check(t, qt.Equals(a.Len(), 0))
}
Loading
Loading