2014-05-25 19:34:29 +08:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2014-11-17 15:47:24 +08:00
|
|
|
"math/big"
|
2014-06-24 21:20:49 +08:00
|
|
|
"math/rand"
|
2014-05-25 19:34:29 +08:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-11-17 15:47:24 +08:00
|
|
|
func TestSetNilBigInt(t *testing.T) {
|
|
|
|
i := new(big.Int)
|
|
|
|
i.SetBytes(make([]byte, 2))
|
|
|
|
}
|
|
|
|
|
2014-05-25 19:34:29 +08:00
|
|
|
func TestMarshalCompactNodeInfo(t *testing.T) {
|
2014-05-27 14:28:56 +08:00
|
|
|
cni := NodeInfo{
|
2014-05-25 19:34:29 +08:00
|
|
|
ID: [20]byte{'a', 'b', 'c'},
|
|
|
|
}
|
2014-11-17 11:22:29 +08:00
|
|
|
addr, err := net.ResolveUDPAddr("udp4", "1.2.3.4:5")
|
2014-05-25 19:34:29 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-17 11:22:29 +08:00
|
|
|
cni.Addr = newDHTAddr(addr)
|
2014-05-27 14:28:56 +08:00
|
|
|
var b [CompactNodeInfoLen]byte
|
|
|
|
cni.PutCompact(b[:])
|
2014-05-25 19:34:29 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var bb [26]byte
|
|
|
|
copy(bb[:], []byte("abc"))
|
|
|
|
copy(bb[20:], []byte("\x01\x02\x03\x04\x00\x05"))
|
|
|
|
if b != bb {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 21:20:49 +08:00
|
|
|
|
|
|
|
func recoverPanicOrDie(t *testing.T, f func()) {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
t.Fatal("expected panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
|
|
|
const zeroID = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
|
|
|
|
|
|
var testIDs = []string{
|
|
|
|
zeroID,
|
|
|
|
"\x03" + zeroID[1:],
|
|
|
|
"\x03" + zeroID[1:18] + "\x55\xf0",
|
|
|
|
"\x55" + zeroID[1:17] + "\xff\x55\x0f",
|
2014-08-22 01:45:31 +08:00
|
|
|
"\x54" + zeroID[1:18] + "\x50\x0f",
|
2014-12-09 14:25:49 +08:00
|
|
|
"",
|
2014-08-22 01:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDistances(t *testing.T) {
|
2014-11-17 15:47:24 +08:00
|
|
|
if idDistance(testIDs[3], testIDs[0]).BitCount() != 4+8+4+4 {
|
2014-08-22 01:45:31 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2014-11-17 15:47:24 +08:00
|
|
|
if idDistance(testIDs[3], testIDs[1]).BitCount() != 4+8+4+4 {
|
2014-08-22 01:45:31 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2014-11-17 15:47:24 +08:00
|
|
|
if idDistance(testIDs[3], testIDs[2]).BitCount() != 4+8+8 {
|
2014-08-22 01:45:31 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2014-12-09 14:25:49 +08:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
dist := idDistance(testIDs[i], testIDs[5]).Int
|
|
|
|
if dist.Cmp(&maxDistance) != 0 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaxDistanceString(t *testing.T) {
|
|
|
|
if string(maxDistance.Bytes()) != "\x01"+zeroID {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2014-06-24 21:20:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadIdStrings(t *testing.T) {
|
|
|
|
var a, b string
|
2014-12-09 14:25:49 +08:00
|
|
|
idDistance(a, b)
|
|
|
|
idDistance(a, zeroID)
|
|
|
|
idDistance(zeroID, b)
|
2014-06-24 21:20:49 +08:00
|
|
|
recoverPanicOrDie(t, func() {
|
2014-12-09 14:25:49 +08:00
|
|
|
idDistance("when", a)
|
2014-06-24 21:20:49 +08:00
|
|
|
})
|
|
|
|
recoverPanicOrDie(t, func() {
|
2014-12-09 14:25:49 +08:00
|
|
|
idDistance(a, "bad")
|
2014-06-24 21:20:49 +08:00
|
|
|
})
|
|
|
|
recoverPanicOrDie(t, func() {
|
2014-12-09 14:25:49 +08:00
|
|
|
idDistance("meets", "evil")
|
2014-06-24 21:20:49 +08:00
|
|
|
})
|
2014-12-09 14:25:49 +08:00
|
|
|
for _, id := range testIDs {
|
|
|
|
if !idDistance(id, id).IsZero() {
|
|
|
|
t.Fatal("identical IDs should have distance 0")
|
|
|
|
}
|
2014-06-24 21:20:49 +08:00
|
|
|
}
|
|
|
|
a = "\x03" + zeroID[1:]
|
|
|
|
b = zeroID
|
2014-11-17 15:47:24 +08:00
|
|
|
if idDistance(a, b).BitCount() != 2 {
|
2014-06-24 21:20:49 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
a = "\x03" + zeroID[1:18] + "\x55\xf0"
|
|
|
|
b = "\x55" + zeroID[1:17] + "\xff\x55\x0f"
|
2014-11-17 15:47:24 +08:00
|
|
|
if c := idDistance(a, b).BitCount(); c != 20 {
|
2014-06-24 21:20:49 +08:00
|
|
|
t.Fatal(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClosestNodes(t *testing.T) {
|
2014-08-22 01:45:31 +08:00
|
|
|
cn := newKClosestNodesSelector(2, testIDs[3])
|
2014-06-24 21:20:49 +08:00
|
|
|
for _, i := range rand.Perm(len(testIDs)) {
|
|
|
|
cn.Push(testIDs[i])
|
|
|
|
}
|
|
|
|
if len(cn.IDs()) != 2 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
m := map[string]bool{}
|
|
|
|
for _, id := range cn.IDs() {
|
|
|
|
m[id] = true
|
|
|
|
}
|
2014-08-22 01:45:31 +08:00
|
|
|
if !m[testIDs[3]] || !m[testIDs[4]] {
|
2014-06-24 21:20:49 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2014-07-17 14:05:21 +08:00
|
|
|
|
|
|
|
// func TestUnmarshalGetPeersResponse(t *testing.T) {
|
|
|
|
// var gpr getPeersResponse
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatal(err)
|
|
|
|
// }
|
|
|
|
// err = dec.Decode(map[string]interface{}{
|
|
|
|
// "values": []string{"\x01\x02\x03\x04\x05\x06", "\x07\x08\x09\x0a\x0b\x0c"},
|
|
|
|
// "nodes": "\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d",
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatal(err)
|
|
|
|
// }
|
|
|
|
// if len(gpr.Values) != 2 {
|
|
|
|
// t.FailNow()
|
|
|
|
// }
|
|
|
|
// if len(gpr.Nodes) != 2 {
|
|
|
|
// t.FailNow()
|
|
|
|
// }
|
|
|
|
// }
|
2014-08-21 19:12:50 +08:00
|
|
|
|
|
|
|
func TestDHTDefaultConfig(t *testing.T) {
|
|
|
|
s, err := NewServer(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s.Close()
|
|
|
|
}
|