lbry.go/dht/routing_table_test.go

86 lines
3.2 KiB
Go
Raw Normal View History

2018-03-07 02:15:44 +01:00
package dht
import (
"net"
2018-03-09 22:43:30 +01:00
"reflect"
"testing"
)
2018-03-07 02:15:44 +01:00
2018-03-29 03:05:27 +02:00
func TestRoutingTable_bucketFor(t *testing.T) {
target := BitmapFromHexP("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
2018-03-29 03:05:27 +02:00
var tests = []struct {
id Bitmap
target Bitmap
2018-03-29 03:05:27 +02:00
expected int
}{
{BitmapFromHexP("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"), target, 0},
{BitmapFromHexP("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002"), target, 1},
{BitmapFromHexP("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003"), target, 1},
{BitmapFromHexP("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004"), target, 2},
{BitmapFromHexP("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005"), target, 2},
{BitmapFromHexP("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f"), target, 3},
{BitmapFromHexP("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010"), target, 4},
{BitmapFromHexP("F00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), target, 383},
{BitmapFromHexP("F0000000000000000000000000000000F0000000000000000000000000F0000000000000000000000000000000000000"), target, 383},
2018-03-29 03:05:27 +02:00
}
for _, tt := range tests {
bucket := bucketFor(tt.id, tt.target)
if bucket != tt.expected {
t.Errorf("bucketFor(%s, %s) => %d, want %d", tt.id.Hex(), tt.target.Hex(), bucket, tt.expected)
}
}
}
2018-03-07 02:15:44 +01:00
func TestRoutingTable(t *testing.T) {
n1 := BitmapFromHexP("FFFFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
n2 := BitmapFromHexP("FFFFFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
n3 := BitmapFromHexP("111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
2018-04-28 02:16:12 +02:00
rt := newRoutingTable(n1)
rt.Update(Contact{n2, net.ParseIP("127.0.0.1"), 8001})
rt.Update(Contact{n3, net.ParseIP("127.0.0.1"), 8002})
2018-03-07 02:15:44 +01:00
contacts := rt.GetClosest(BitmapFromHexP("222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), 1)
2018-03-07 02:15:44 +01:00
if len(contacts) != 1 {
t.Fail()
return
}
if !contacts[0].id.Equals(n3) {
t.Error(contacts[0])
}
2018-03-29 03:05:27 +02:00
contacts = rt.GetClosest(n2, 10)
2018-03-07 02:15:44 +01:00
if len(contacts) != 2 {
t.Error(len(contacts))
return
}
if !contacts[0].id.Equals(n2) {
t.Error(contacts[0])
}
if !contacts[1].id.Equals(n3) {
t.Error(contacts[1])
}
}
func TestCompactEncoding(t *testing.T) {
2018-04-28 02:16:12 +02:00
c := Contact{
id: BitmapFromHexP("1c8aff71b99462464d9eeac639595ab99664be3482cb91a29d87467515c7d9158fe72aa1f1582dab07d8f8b5db277f41"),
2018-03-09 22:43:30 +01:00
ip: net.ParseIP("1.2.3.4"),
port: int(55<<8 + 66),
}
var compact []byte
2018-04-28 02:16:12 +02:00
compact, err := c.MarshalCompact()
if err != nil {
t.Fatal(err)
}
2018-03-09 22:43:30 +01:00
if len(compact) != compactNodeInfoLength {
t.Fatalf("got length of %d; expected %d", len(compact), compactNodeInfoLength)
}
2018-04-28 02:16:12 +02:00
if !reflect.DeepEqual(compact, append([]byte{1, 2, 3, 4, 55, 66}, c.id[:]...)) {
2018-03-09 22:43:30 +01:00
t.Errorf("compact bytes not encoded correctly")
}
}