2018-07-30 20:25:18 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-09-11 14:57:12 +01:00
|
|
|
"path/filepath"
|
2018-07-30 20:25:18 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-07-30 20:25:18 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-19 19:37:28 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-09-18 05:39:06 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-07-30 20:25:18 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2018-09-11 14:57:12 +01:00
|
|
|
func tempdir(t testing.TB) (dir string, cleanup func()) {
|
|
|
|
dir, err := ioutil.TempDir("", "storj-kademlia")
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-09-11 14:57:12 +01:00
|
|
|
t.Fatal(err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2018-09-11 14:57:12 +01:00
|
|
|
return dir, func() {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:57:12 +01:00
|
|
|
func createRoutingTable(t *testing.T, localNodeID []byte) (*RoutingTable, func()) {
|
|
|
|
tempdir, cleanup := tempdir(t)
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
if localNodeID == nil {
|
|
|
|
localNodeID = []byte("AA")
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
localNode := &pb.Node{Id: string(localNodeID)}
|
2018-08-09 20:20:39 +01:00
|
|
|
options := &RoutingOptions{
|
2018-09-11 14:57:12 +01:00
|
|
|
kpath: filepath.Join(tempdir, "Kadbucket"),
|
|
|
|
npath: filepath.Join(tempdir, "Nodebucket"),
|
2018-08-27 18:28:16 +01:00
|
|
|
idLength: 16,
|
|
|
|
bucketSize: 6,
|
2018-08-17 20:11:46 +01:00
|
|
|
rcBucketSize: 2,
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, err := NewRoutingTable(localNode, options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return rt, func() {
|
|
|
|
err := rt.Close()
|
|
|
|
cleanup()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func mockNode(id string) *pb.Node {
|
|
|
|
var node pb.Node
|
2018-07-30 20:25:18 +01:00
|
|
|
node.Id = id
|
|
|
|
return &node
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddNode(t *testing.T) {
|
2018-09-19 19:37:28 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, []byte("OO"))
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
bucket, err := rt.kadBucketDB.Get(storage.Key([]byte{255, 255}))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, bucket)
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
node *pb.Node
|
|
|
|
added bool
|
|
|
|
kadIDs [][]byte
|
|
|
|
nodeIDs [][]string
|
|
|
|
}{
|
|
|
|
{testID: "PO: add node to unfilled kbucket",
|
|
|
|
node: mockNode("PO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "NO: add node to full kbucket and split",
|
|
|
|
node: mockNode("NO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"NO", "OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "MO",
|
|
|
|
node: mockNode("MO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"MO", "NO", "OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "LO",
|
|
|
|
node: mockNode("LO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"LO", "MO", "NO", "OO", "PO"}},
|
|
|
|
},
|
|
|
|
{testID: "QO",
|
|
|
|
node: mockNode("QO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"LO", "MO", "NO", "OO", "PO", "QO"}},
|
|
|
|
},
|
|
|
|
{testID: "SO: split bucket",
|
|
|
|
node: mockNode("SO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
|
|
|
nodeIDs: [][]string{{}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
|
|
|
},
|
|
|
|
{testID: "?O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("?O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{{63, 255}, {79, 255}, {95, 255}, {127, 255}, {255, 255}},
|
|
|
|
nodeIDs: [][]string{{"?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: ">O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode(">O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "=O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("=O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: ";O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode(";O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: ":O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode(":O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{":O", ";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "9O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("9O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "8O: should drop",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("8O"),
|
|
|
|
added: false,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "KO",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("KO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "JO",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("JO"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "]O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("]O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "^O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("^O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O", "^O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "_O",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("_O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O", "^O", "_O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
{testID: "@O: split bucket 2",
|
2018-10-11 21:25:54 +01:00
|
|
|
node: mockNode("@O"),
|
|
|
|
added: true,
|
|
|
|
kadIDs: [][]byte{[]byte{63, 255}, []byte{71, 255}, []byte{79, 255}, []byte{95, 255}, []byte{127, 255}, []byte{255, 255}},
|
|
|
|
nodeIDs: [][]string{{"9O", ":O", ";O", "=O", ">O", "?O"}, {"@O"}, {"JO", "KO", "LO", "MO", "NO", "OO"}, {"PO", "QO", "SO", "]O", "^O", "_O"}, {}, {}},
|
2018-09-19 19:37:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
ok, err := rt.addNode(c.node)
|
|
|
|
assert.Equal(t, c.added, ok)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
kadKeys, err := rt.kadBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for i, v := range kadKeys {
|
|
|
|
assert.Equal(t, storage.Key(c.kadIDs[i]), v)
|
|
|
|
a, err := rt.getNodeIDsWithinKBucket(v)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for j, w := range a {
|
|
|
|
assert.Equal(t, c.nodeIDs[i][j], string(w))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.testID == "8O" {
|
|
|
|
n := rt.replacementCache["8O"]
|
|
|
|
assert.Equal(t, "8O", n[0].Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 20:11:46 +01:00
|
|
|
func TestUpdateNode(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, []byte("AA"))
|
|
|
|
defer cleanup()
|
2018-08-17 20:11:46 +01:00
|
|
|
node := mockNode("BB")
|
|
|
|
ok, err := rt.addNode(node)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
val, err := rt.nodeBucketDB.Get(storage.Key(node.Id))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
unmarshaled, err := unmarshalNodes(storage.Keys{storage.Key(node.Id)}, []storage.Value{val})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
x := unmarshaled[0].Address
|
|
|
|
assert.Nil(t, x)
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
node.Address = &pb.NodeAddress{Address: "BB"}
|
2018-08-17 20:11:46 +01:00
|
|
|
err = rt.updateNode(node)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
val, err = rt.nodeBucketDB.Get(storage.Key(node.Id))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
unmarshaled, err = unmarshalNodes(storage.Keys{storage.Key(node.Id)}, []storage.Value{val})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
y := unmarshaled[0].Address.Address
|
|
|
|
assert.Equal(t, "BB", y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveNode(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, []byte("AA"))
|
|
|
|
defer cleanup()
|
2018-08-17 20:11:46 +01:00
|
|
|
kadBucketID := []byte{255, 255}
|
|
|
|
node := mockNode("BB")
|
|
|
|
ok, err := rt.addNode(node)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
val, err := rt.nodeBucketDB.Get(storage.Key(node.Id))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, val)
|
|
|
|
node2 := mockNode("CC")
|
|
|
|
rt.addToReplacementCache(kadBucketID, node2)
|
|
|
|
err = rt.removeNode(kadBucketID, storage.Key(node.Id))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
val, err = rt.nodeBucketDB.Get(storage.Key(node.Id))
|
|
|
|
assert.Nil(t, val)
|
|
|
|
assert.Error(t, err)
|
|
|
|
val2, err := rt.nodeBucketDB.Get(storage.Key(node2.Id))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, val2)
|
|
|
|
assert.Equal(t, 0, len(rt.replacementCache[string(kadBucketID)]))
|
2018-08-21 19:44:42 +01:00
|
|
|
|
|
|
|
//try to remove node not in rt
|
|
|
|
err = rt.removeNode(kadBucketID, storage.Key("DD"))
|
|
|
|
assert.NoError(t, err)
|
2018-08-17 20:11:46 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
func TestCreateOrUpdateKBucket(t *testing.T) {
|
|
|
|
id := []byte{255, 255}
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nil)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
err := rt.createOrUpdateKBucket(storage.Key(id), time.Now())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
val, e := rt.kadBucketDB.Get(storage.Key(id))
|
|
|
|
assert.NotNil(t, val)
|
|
|
|
assert.NoError(t, e)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetKBucketID(t *testing.T) {
|
|
|
|
kadIDA := storage.Key([]byte{255, 255})
|
2018-08-09 20:20:39 +01:00
|
|
|
nodeIDA := []byte("AA")
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2018-08-09 20:20:39 +01:00
|
|
|
keyA, err := rt.getKBucketID(nodeIDA)
|
|
|
|
assert.NoError(t, err)
|
2018-07-30 20:25:18 +01:00
|
|
|
assert.Equal(t, kadIDA, keyA)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXorTwoIds(t *testing.T) {
|
|
|
|
x := xorTwoIds([]byte{191}, []byte{159})
|
|
|
|
assert.Equal(t, []byte{32}, x) //00100000
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortByXOR(t *testing.T) {
|
|
|
|
node1 := []byte{127, 255} //xor 0
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, node1)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
node2 := []byte{143, 255} //xor 240
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node2, []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
node3 := []byte{255, 255} //xor 128
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node3, []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
node4 := []byte{191, 255} //xor 192
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node4, []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
node5 := []byte{133, 255} //xor 250
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node5, []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
nodes, err := rt.nodeBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
expectedNodes := storage.Keys{node1, node5, node2, node4, node3}
|
|
|
|
assert.Equal(t, expectedNodes, nodes)
|
2018-08-09 20:20:39 +01:00
|
|
|
sortedNodes := sortByXOR(nodes, node1)
|
2018-07-30 20:25:18 +01:00
|
|
|
expectedSorted := storage.Keys{node1, node3, node4, node2, node5}
|
|
|
|
assert.Equal(t, expectedSorted, sortedNodes)
|
|
|
|
nodes, err = rt.nodeBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedNodes, nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetermineFurthestIDWithinK(t *testing.T) {
|
2018-09-19 19:37:28 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, []byte{127, 255})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
nodeID []byte
|
|
|
|
expectedFurthest []byte
|
|
|
|
}{
|
|
|
|
{testID: "xor 0",
|
|
|
|
nodeID: []byte{127, 255},
|
|
|
|
expectedFurthest: []byte{127, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 240",
|
|
|
|
nodeID: []byte{143, 255},
|
|
|
|
expectedFurthest: []byte{143, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 128",
|
|
|
|
nodeID: []byte{255, 255},
|
|
|
|
expectedFurthest: []byte{143, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 192",
|
|
|
|
nodeID: []byte{191, 255},
|
|
|
|
expectedFurthest: []byte{143, 255},
|
|
|
|
},
|
|
|
|
{testID: "xor 250",
|
|
|
|
nodeID: []byte{133, 255},
|
|
|
|
expectedFurthest: []byte{133, 255},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(c.nodeID, []byte("")))
|
|
|
|
nodes, err := rt.nodeBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
furthest, err := rt.determineFurthestIDWithinK(nodes)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.expectedFurthest, furthest)
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeIsWithinNearestK(t *testing.T) {
|
2018-09-19 19:37:28 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, []byte{127, 255})
|
2018-09-11 14:57:12 +01:00
|
|
|
defer cleanup()
|
2018-08-09 20:20:39 +01:00
|
|
|
rt.bucketSize = 2
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
nodeID []byte
|
|
|
|
closest bool
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
nodeID: []byte{127, 255},
|
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
nodeID: []byte{143, 255},
|
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "C",
|
|
|
|
nodeID: []byte{255, 255},
|
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "D",
|
|
|
|
nodeID: []byte{191, 255},
|
|
|
|
closest: true,
|
|
|
|
},
|
|
|
|
{testID: "E",
|
|
|
|
nodeID: []byte{133, 255},
|
|
|
|
closest: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
result, err := rt.nodeIsWithinNearestK(c.nodeID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.closest, result)
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(c.nodeID, []byte("")))
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKadBucketContainsLocalNode(t *testing.T) {
|
2018-08-09 20:20:39 +01:00
|
|
|
nodeIDA := []byte{183, 255} //[10110111, 1111111]
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
kadIDA := storage.Key([]byte{255, 255})
|
|
|
|
kadIDB := storage.Key([]byte{127, 255})
|
|
|
|
now := time.Now()
|
2018-08-09 20:20:39 +01:00
|
|
|
err := rt.createOrUpdateKBucket(kadIDB, now)
|
2018-07-30 20:25:18 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
resultTrue, err := rt.kadBucketContainsLocalNode(kadIDA)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
resultFalse, err := rt.kadBucketContainsLocalNode(kadIDB)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, resultTrue)
|
|
|
|
assert.False(t, resultFalse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKadBucketHasRoom(t *testing.T) {
|
|
|
|
node1 := []byte{255, 255}
|
2018-08-09 20:20:39 +01:00
|
|
|
kadIDA := storage.Key([]byte{255, 255})
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, node1)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
node2 := []byte{191, 255}
|
|
|
|
node3 := []byte{127, 255}
|
|
|
|
node4 := []byte{63, 255}
|
|
|
|
node5 := []byte{159, 255}
|
|
|
|
node6 := []byte{0, 127}
|
|
|
|
resultA, err := rt.kadBucketHasRoom(kadIDA)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, resultA)
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node2, []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node3, []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node4, []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node5, []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(node6, []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
resultB, err := rt.kadBucketHasRoom(kadIDA)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, resultB)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNodeIDsWithinKBucket(t *testing.T) {
|
2018-08-09 20:20:39 +01:00
|
|
|
nodeIDA := []byte{183, 255} //[10110111, 1111111]
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
kadIDA := storage.Key([]byte{255, 255})
|
|
|
|
kadIDB := storage.Key([]byte{127, 255})
|
|
|
|
now := time.Now()
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.createOrUpdateKBucket(kadIDB, now))
|
2018-07-30 20:25:18 +01:00
|
|
|
|
|
|
|
nodeIDB := []byte{111, 255} //[01101111, 1111111]
|
|
|
|
nodeIDC := []byte{47, 255} //[00101111, 1111111]
|
|
|
|
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDB, []byte("")))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDC, []byte("")))
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
kadID []byte
|
|
|
|
expected storage.Keys
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
kadID: kadIDA,
|
|
|
|
expected: storage.Keys{nodeIDA},
|
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
kadID: kadIDB,
|
|
|
|
expected: storage.Keys{nodeIDC, nodeIDB},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
n, err := rt.getNodeIDsWithinKBucket(c.kadID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.expected, n)
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
func TestGetNodesFromIDs(t *testing.T) {
|
|
|
|
nodeA := mockNode("AA")
|
|
|
|
nodeB := mockNode("BB")
|
|
|
|
nodeC := mockNode("CC")
|
|
|
|
nodeIDA := []byte(nodeA.Id)
|
|
|
|
nodeIDB := []byte(nodeB.Id)
|
|
|
|
nodeIDC := []byte(nodeC.Id)
|
2018-09-18 05:39:06 +01:00
|
|
|
a, err := proto.Marshal(nodeA)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
b, err := proto.Marshal(nodeB)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
c, err := proto.Marshal(nodeC)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2018-08-09 20:20:39 +01:00
|
|
|
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDA, a))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDB, b))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDC, c))
|
2018-08-09 20:20:39 +01:00
|
|
|
expected := []storage.Value{a, b, c}
|
|
|
|
|
|
|
|
nodeIDs, err := rt.nodeBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, values, err := rt.getNodesFromIDs(nodeIDs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, values)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNodes(t *testing.T) {
|
|
|
|
nodeA := mockNode("AA")
|
|
|
|
nodeB := mockNode("BB")
|
|
|
|
nodeC := mockNode("CC")
|
|
|
|
|
|
|
|
nodeIDA := []byte(nodeA.Id)
|
|
|
|
nodeIDB := []byte(nodeB.Id)
|
|
|
|
nodeIDC := []byte(nodeC.Id)
|
2018-09-18 05:39:06 +01:00
|
|
|
a, err := proto.Marshal(nodeA)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
b, err := proto.Marshal(nodeB)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
c, err := proto.Marshal(nodeC)
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nodeIDA)
|
|
|
|
defer cleanup()
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDA, a))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDB, b))
|
|
|
|
assert.NoError(t, rt.nodeBucketDB.Put(nodeIDC, c))
|
2018-08-09 20:20:39 +01:00
|
|
|
nodeIDs, err := rt.nodeBucketDB.List(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ids, values, err := rt.getNodesFromIDs(nodeIDs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
nodes, err := unmarshalNodes(ids, values)
|
|
|
|
assert.NoError(t, err)
|
2018-09-18 05:39:06 +01:00
|
|
|
expected := []*pb.Node{nodeA, nodeB, nodeC}
|
2018-08-09 20:20:39 +01:00
|
|
|
for i, v := range expected {
|
2018-09-18 05:39:06 +01:00
|
|
|
assert.True(t, proto.Equal(v, nodes[i]))
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUnmarshaledNodesFromBucket(t *testing.T) {
|
|
|
|
bucketID := []byte{255, 255}
|
|
|
|
nodeA := mockNode("AA")
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, []byte(nodeA.Id))
|
|
|
|
defer cleanup()
|
2018-08-09 20:20:39 +01:00
|
|
|
nodeB := mockNode("BB")
|
|
|
|
nodeC := mockNode("CC")
|
2018-09-11 14:13:25 +01:00
|
|
|
var err error
|
|
|
|
_, err = rt.addNode(nodeB)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = rt.addNode(nodeC)
|
|
|
|
assert.NoError(t, err)
|
2018-08-09 20:20:39 +01:00
|
|
|
nodes, err := rt.getUnmarshaledNodesFromBucket(bucketID)
|
2018-09-18 05:39:06 +01:00
|
|
|
expected := []*pb.Node{nodeA, nodeB, nodeC}
|
2018-08-09 20:20:39 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for i, v := range expected {
|
2018-09-18 05:39:06 +01:00
|
|
|
assert.True(t, proto.Equal(v, nodes[i]))
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
func TestGetKBucketRange(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nil)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
idA := []byte{255, 255}
|
|
|
|
idB := []byte{127, 255}
|
|
|
|
idC := []byte{63, 255}
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, rt.kadBucketDB.Put(idA, []byte("")))
|
|
|
|
assert.NoError(t, rt.kadBucketDB.Put(idB, []byte("")))
|
|
|
|
assert.NoError(t, rt.kadBucketDB.Put(idC, []byte("")))
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
id []byte
|
|
|
|
expected storage.Keys
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
id: idA,
|
|
|
|
expected: storage.Keys{idB, idA},
|
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
id: idB,
|
|
|
|
expected: storage.Keys{idC, idB}},
|
|
|
|
{testID: "C",
|
|
|
|
id: idC,
|
|
|
|
expected: storage.Keys{rt.createZeroAsStorageKey(), idC},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
ep, err := rt.getKBucketRange(c.id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.expected, ep)
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFirstBucketID(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nil)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
x := rt.createFirstBucketID()
|
|
|
|
expected := []byte{255, 255}
|
|
|
|
assert.Equal(t, x, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateZeroAsStorageKey(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nil)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
zero := rt.createZeroAsStorageKey()
|
|
|
|
expected := []byte{0, 0}
|
|
|
|
assert.Equal(t, zero, storage.Key(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetermineLeafDepth(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nil)
|
|
|
|
defer cleanup()
|
2018-07-30 20:25:18 +01:00
|
|
|
idA := []byte{255, 255}
|
|
|
|
idB := []byte{127, 255}
|
|
|
|
idC := []byte{63, 255}
|
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
id []byte
|
|
|
|
depth int
|
|
|
|
addNode func()
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
id: idA,
|
|
|
|
depth: 0,
|
|
|
|
addNode: func() {
|
|
|
|
e := rt.kadBucketDB.Put(idA, []byte(""))
|
|
|
|
assert.NoError(t, e)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
id: idB,
|
|
|
|
depth: 1,
|
|
|
|
addNode: func() {
|
|
|
|
e := rt.kadBucketDB.Put(idB, []byte(""))
|
|
|
|
assert.NoError(t, e)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "C",
|
|
|
|
id: idA,
|
|
|
|
depth: 1,
|
|
|
|
addNode: func() {
|
|
|
|
e := rt.kadBucketDB.Put(idC, []byte(""))
|
|
|
|
assert.NoError(t, e)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{testID: "D",
|
|
|
|
id: idB,
|
|
|
|
depth: 2,
|
|
|
|
addNode: func() {},
|
|
|
|
},
|
|
|
|
{testID: "E",
|
|
|
|
id: idC,
|
|
|
|
depth: 2,
|
|
|
|
addNode: func() {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
c.addNode()
|
|
|
|
d, err := rt.determineLeafDepth(c.id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.depth, d)
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetermineDifferingBitIndex(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nil)
|
|
|
|
defer cleanup()
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
bucketID []byte
|
|
|
|
key []byte
|
|
|
|
expected int
|
|
|
|
err *errs.Class
|
|
|
|
}{
|
|
|
|
{testID: "A",
|
|
|
|
bucketID: []byte{191, 255},
|
|
|
|
key: []byte{255, 255},
|
|
|
|
expected: 1,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "B",
|
|
|
|
bucketID: []byte{255, 255},
|
|
|
|
key: []byte{191, 255},
|
|
|
|
expected: 1,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "C",
|
|
|
|
bucketID: []byte{95, 255},
|
|
|
|
key: []byte{127, 255},
|
|
|
|
expected: 2,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "D",
|
|
|
|
bucketID: []byte{95, 255},
|
|
|
|
key: []byte{79, 255},
|
|
|
|
expected: 3,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "E",
|
|
|
|
bucketID: []byte{95, 255},
|
|
|
|
key: []byte{63, 255},
|
|
|
|
expected: 2,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "F",
|
|
|
|
bucketID: []byte{95, 255},
|
|
|
|
key: []byte{79, 255},
|
|
|
|
expected: 3,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "G",
|
|
|
|
bucketID: []byte{255, 255},
|
|
|
|
key: []byte{255, 255},
|
|
|
|
expected: -2,
|
|
|
|
err: &RoutingErr,
|
|
|
|
},
|
|
|
|
{testID: "H",
|
|
|
|
bucketID: []byte{255, 255},
|
|
|
|
key: []byte{0, 0},
|
|
|
|
expected: -1,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "I",
|
|
|
|
bucketID: []byte{127, 255},
|
|
|
|
key: []byte{0, 0},
|
|
|
|
expected: 0,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "J",
|
|
|
|
bucketID: []byte{63, 255},
|
|
|
|
key: []byte{0, 0},
|
|
|
|
expected: 1,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "K",
|
|
|
|
bucketID: []byte{31, 255},
|
|
|
|
key: []byte{0, 0},
|
|
|
|
expected: 2,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{testID: "L",
|
|
|
|
bucketID: []byte{95, 255},
|
|
|
|
key: []byte{63, 255},
|
|
|
|
expected: 2,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
diff, err := rt.determineDifferingBitIndex(c.bucketID, c.key)
|
|
|
|
assertErrClass(t, c.err, err)
|
|
|
|
assert.Equal(t, c.expected, diff)
|
|
|
|
})
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitBucket(t *testing.T) {
|
2018-09-11 14:57:12 +01:00
|
|
|
rt, cleanup := createRoutingTable(t, nil)
|
|
|
|
defer cleanup()
|
2018-09-19 19:37:28 +01:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
idA []byte
|
|
|
|
idB []byte
|
|
|
|
depth int
|
|
|
|
}{
|
|
|
|
{testID: "A: [11111111, 11111111] -> [10111111, 11111111]",
|
|
|
|
idA: []byte{255, 255},
|
|
|
|
idB: []byte{191, 255},
|
|
|
|
depth: 1,
|
|
|
|
},
|
|
|
|
{testID: "B: [10111111, 11111111] -> [10011111, 11111111]",
|
|
|
|
idA: []byte{191, 255},
|
|
|
|
idB: []byte{159, 255},
|
|
|
|
depth: 2,
|
|
|
|
},
|
|
|
|
{testID: "C: [01111111, 11111111] -> [00111111, 11111111]",
|
|
|
|
idA: []byte{127, 255},
|
|
|
|
idB: []byte{63, 255},
|
|
|
|
depth: 1,
|
|
|
|
},
|
|
|
|
{testID: "D: [00000000, 11111111] -> [00000000, 01111111]",
|
|
|
|
idA: []byte{0, 255},
|
|
|
|
idB: []byte{0, 127},
|
|
|
|
depth: 8,
|
|
|
|
},
|
|
|
|
{testID: "E: [01011111, 11111111] -> [01010111, 11111111]",
|
|
|
|
idA: []byte{95, 255},
|
|
|
|
idB: []byte{87, 255},
|
|
|
|
depth: 4,
|
|
|
|
},
|
|
|
|
{testID: "F: [01011111, 11111111] -> [01001111, 11111111]",
|
|
|
|
idA: []byte{95, 255},
|
|
|
|
idB: []byte{79, 255},
|
|
|
|
depth: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
newID := rt.splitBucket(c.idA, c.depth)
|
|
|
|
assert.Equal(t, c.idB, newID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2018-09-19 19:37:28 +01:00
|
|
|
func assertErrClass(t *testing.T, class *errs.Class, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if class != nil {
|
|
|
|
assert.True(t, class.Has(err))
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|