reflector.go/dht/node_test.go

437 lines
10 KiB
Go
Raw Normal View History

2018-03-07 02:15:44 +01:00
package dht
import (
2018-03-09 22:43:30 +01:00
"net"
2018-03-07 02:15:44 +01:00
"testing"
"time"
"github.com/lyoshenka/bencode"
2018-03-07 02:15:44 +01:00
)
func TestPing(t *testing.T) {
dhtNodeID := RandomBitmapP()
testNodeID := RandomBitmapP()
2018-03-07 02:15:44 +01:00
conn := newTestUDPConn("127.0.0.1:21217")
dht, err := New(&Config{Address: "127.0.0.1:21216", NodeID: dhtNodeID.Hex()})
if err != nil {
t.Fatal(err)
}
2018-04-28 02:16:12 +02:00
err = dht.node.Connect(conn)
if err != nil {
t.Fatal(err)
}
2018-03-29 03:05:27 +02:00
defer dht.Shutdown()
2018-03-07 02:15:44 +01:00
messageID := newMessageID()
2018-03-07 02:15:44 +01:00
data, err := bencode.EncodeBytes(map[string]interface{}{
headerTypeField: requestType,
headerMessageIDField: messageID,
headerNodeIDField: testNodeID.rawString(),
2018-03-07 02:15:44 +01:00
headerPayloadField: "ping",
headerArgsField: []string{},
})
if err != nil {
panic(err)
}
conn.toRead <- testUDPPacket{addr: conn.addr, data: data}
timer := time.NewTimer(3 * time.Second)
select {
case <-timer.C:
t.Error("timeout")
case resp := <-conn.writes:
var response map[string]interface{}
err := bencode.DecodeBytes(resp.data, &response)
if err != nil {
2018-03-09 22:43:30 +01:00
t.Fatal(err)
2018-03-07 02:15:44 +01:00
}
if len(response) != 4 {
t.Errorf("expected 4 response fields, got %d", len(response))
}
_, ok := response[headerTypeField]
if !ok {
t.Error("missing type field")
} else {
rType, ok := response[headerTypeField].(int64)
if !ok {
t.Error("type is not an integer")
} else if rType != responseType {
t.Error("unexpected response type")
}
}
_, ok = response[headerMessageIDField]
if !ok {
t.Error("missing message id field")
} else {
rMessageID, ok := response[headerMessageIDField].(string)
if !ok {
t.Error("message ID is not a string")
2018-04-03 19:38:01 +02:00
} else if rMessageID != string(messageID[:]) {
2018-03-07 02:15:44 +01:00
t.Error("unexpected message ID")
}
}
_, ok = response[headerNodeIDField]
if !ok {
t.Error("missing node id field")
} else {
rNodeID, ok := response[headerNodeIDField].(string)
if !ok {
t.Error("node ID is not a string")
} else if rNodeID != dhtNodeID.rawString() {
2018-03-07 02:15:44 +01:00
t.Error("unexpected node ID")
}
}
_, ok = response[headerPayloadField]
if !ok {
t.Error("missing payload field")
} else {
rNodeID, ok := response[headerPayloadField].(string)
if !ok {
t.Error("payload is not a string")
} else if rNodeID != pingSuccessResponse {
t.Error("did not pong")
}
}
}
}
func TestStore(t *testing.T) {
dhtNodeID := RandomBitmapP()
testNodeID := RandomBitmapP()
2018-03-07 02:15:44 +01:00
conn := newTestUDPConn("127.0.0.1:21217")
dht, err := New(&Config{Address: "127.0.0.1:21216", NodeID: dhtNodeID.Hex()})
if err != nil {
t.Fatal(err)
}
2018-04-28 02:16:12 +02:00
err = dht.node.Connect(conn)
if err != nil {
t.Fatal(err)
}
2018-03-29 03:05:27 +02:00
defer dht.Shutdown()
2018-03-07 02:15:44 +01:00
messageID := newMessageID()
blobHashToStore := RandomBitmapP()
2018-03-07 02:15:44 +01:00
2018-03-07 22:15:58 +01:00
storeRequest := Request{
ID: messageID,
2018-04-03 19:38:01 +02:00
NodeID: testNodeID,
2018-03-07 22:15:58 +01:00
Method: storeMethod,
StoreArgs: &storeArgs{
BlobHash: blobHashToStore,
2018-04-03 19:38:01 +02:00
Value: storeArgsValue{
2018-04-28 02:16:12 +02:00
Token: dht.node.tokens.Get(testNodeID, conn.addr),
2018-04-03 19:38:01 +02:00
LbryID: testNodeID,
Port: 9999,
},
NodeID: testNodeID,
2018-03-07 22:15:58 +01:00
},
}
_ = "64 " + // start message
"313A30 693065" + // type: 0
"313A31 3230 3A 6EB490B5788B63F0F7E6D92352024D0CBDEC2D3A" + // message id
"313A32 3438 3A 7CE1B831DEC8689E44F80F547D2DEA171F6A625E1A4FF6C6165E645F953103DABEB068A622203F859C6C64658FD3AA3B" + // node id
"313A33 35 3A 73746F7265" + // method
"313A34 6C" + // start args list
"3438 3A 3214D6C2F77FCB5E8D5FC07EDAFBA614F031CE8B2EAB49F924F8143F6DFBADE048D918710072FB98AB1B52B58F4E1468" + // block hash
"64" + // start value dict
"363A6C6272796964 3438 3A 7CE1B831DEC8689E44F80F547D2DEA171F6A625E1A4FF6C6165E645F953103DABEB068A622203F859C6C64658FD3AA3B" + // lbry id
"343A706F7274 69 33333333 65" + // port
"353A746F6B656E 3438 3A 17C2D8E1E48EF21567FE4AD5C8ED944B798D3B65AB58D0C9122AD6587D1B5FED472EA2CB12284CEFA1C21EFF302322BD" + // token
"65" + // end value dict
"3438 3A 7CE1B831DEC8689E44F80F547D2DEA171F6A625E1A4FF6C6165E645F953103DABEB068A622203F859C6C64658FD3AA3B" + // node id
"693065" + // self store (integer)
"65" + // end args list
"65" // end message
data, err := bencode.EncodeBytes(storeRequest)
2018-03-07 02:15:44 +01:00
if err != nil {
2018-03-09 22:43:30 +01:00
t.Fatal(err)
2018-03-07 02:15:44 +01:00
}
conn.toRead <- testUDPPacket{addr: conn.addr, data: data}
timer := time.NewTimer(3 * time.Second)
2018-03-08 01:49:33 +01:00
var response map[string]interface{}
2018-03-07 02:15:44 +01:00
select {
case <-timer.C:
2018-03-09 22:43:30 +01:00
t.Fatal("timeout")
2018-03-07 02:15:44 +01:00
case resp := <-conn.writes:
err := bencode.DecodeBytes(resp.data, &response)
if err != nil {
2018-03-09 22:43:30 +01:00
t.Fatal(err)
2018-03-07 02:15:44 +01:00
}
2018-03-08 01:49:33 +01:00
}
2018-03-07 02:15:44 +01:00
verifyResponse(t, response, messageID, dhtNodeID.rawString())
2018-03-07 02:15:44 +01:00
_, ok := response[headerPayloadField]
2018-03-08 01:49:33 +01:00
if !ok {
t.Error("missing payload field")
} else {
rNodeID, ok := response[headerPayloadField].(string)
2018-03-07 02:15:44 +01:00
if !ok {
2018-03-08 01:49:33 +01:00
t.Error("payload is not a string")
} else if rNodeID != storeSuccessResponse {
t.Error("did not return OK")
2018-03-07 02:15:44 +01:00
}
}
2018-03-08 01:49:33 +01:00
2018-05-13 22:02:46 +02:00
if dht.node.store.CountStoredHashes() != 1 {
2018-03-08 01:49:33 +01:00
t.Error("dht store has wrong number of items")
}
2018-04-28 02:16:12 +02:00
items := dht.node.store.Get(blobHashToStore)
2018-03-08 01:49:33 +01:00
if len(items) != 1 {
t.Error("list created in store, but nothing in list")
}
if !items[0].ID.Equals(testNodeID) {
2018-03-08 01:49:33 +01:00
t.Error("wrong value stored")
}
2018-03-07 02:15:44 +01:00
}
2018-03-07 22:15:58 +01:00
func TestFindNode(t *testing.T) {
dhtNodeID := RandomBitmapP()
testNodeID := RandomBitmapP()
2018-03-07 22:15:58 +01:00
conn := newTestUDPConn("127.0.0.1:21217")
dht, err := New(&Config{Address: "127.0.0.1:21216", NodeID: dhtNodeID.Hex()})
if err != nil {
t.Fatal(err)
}
2018-04-28 02:16:12 +02:00
err = dht.node.Connect(conn)
if err != nil {
t.Fatal(err)
}
2018-03-29 03:05:27 +02:00
defer dht.Shutdown()
2018-03-07 22:15:58 +01:00
nodesToInsert := 3
2018-04-28 02:16:12 +02:00
var nodes []Contact
for i := 0; i < nodesToInsert; i++ {
n := Contact{ID: RandomBitmapP(), IP: net.ParseIP("127.0.0.1"), Port: 10000 + i}
nodes = append(nodes, n)
2018-04-28 02:16:12 +02:00
dht.node.rt.Update(n)
}
messageID := newMessageID()
blobHashToFind := RandomBitmapP()
request := Request{
ID: messageID,
2018-04-03 19:38:01 +02:00
NodeID: testNodeID,
Method: findNodeMethod,
2018-04-04 18:01:44 +02:00
Arg: &blobHashToFind,
}
data, err := bencode.EncodeBytes(request)
if err != nil {
2018-03-09 22:43:30 +01:00
t.Fatal(err)
}
2018-03-07 22:15:58 +01:00
conn.toRead <- testUDPPacket{addr: conn.addr, data: data}
timer := time.NewTimer(3 * time.Second)
var response map[string]interface{}
2018-03-07 22:15:58 +01:00
select {
case <-timer.C:
2018-03-09 22:43:30 +01:00
t.Fatal("timeout")
2018-03-07 22:15:58 +01:00
case resp := <-conn.writes:
err := bencode.DecodeBytes(resp.data, &response)
if err != nil {
2018-03-09 22:43:30 +01:00
t.Fatal(err)
2018-03-07 22:15:58 +01:00
}
}
2018-03-07 22:15:58 +01:00
verifyResponse(t, response, messageID, dhtNodeID.rawString())
_, ok := response[headerPayloadField]
if !ok {
2018-03-09 22:43:30 +01:00
t.Fatal("missing payload field")
}
contacts, ok := response[headerPayloadField].([]interface{})
2018-03-09 22:43:30 +01:00
if !ok {
t.Fatal("payload is not a list")
2018-03-09 22:43:30 +01:00
}
verifyContacts(t, contacts, nodes)
}
func TestFindValueExisting(t *testing.T) {
dhtNodeID := RandomBitmapP()
testNodeID := RandomBitmapP()
2018-03-09 22:43:30 +01:00
conn := newTestUDPConn("127.0.0.1:21217")
dht, err := New(&Config{Address: "127.0.0.1:21216", NodeID: dhtNodeID.Hex()})
if err != nil {
t.Fatal(err)
}
2018-04-28 02:16:12 +02:00
err = dht.node.Connect(conn)
if err != nil {
t.Fatal(err)
}
2018-03-29 03:05:27 +02:00
defer dht.Shutdown()
2018-03-09 22:43:30 +01:00
nodesToInsert := 3
for i := 0; i < nodesToInsert; i++ {
n := Contact{ID: RandomBitmapP(), IP: net.ParseIP("127.0.0.1"), Port: 10000 + i}
2018-04-28 02:16:12 +02:00
dht.node.rt.Update(n)
2018-03-09 22:43:30 +01:00
}
//data, _ := hex.DecodeString("64313a30693065313a3132303a7de8e57d34e316abbb5a8a8da50dcd1ad4c80e0f313a3234383a7ce1b831dec8689e44f80f547d2dea171f6a625e1a4ff6c6165e645f953103dabeb068a622203f859c6c64658fd3aa3b313a33393a66696e6456616c7565313a346c34383aa47624b8e7ee1e54df0c45e2eb858feb0b705bd2a78d8b739be31ba188f4bd6f56b371c51fecc5280d5fd26ba4168e966565")
messageID := newMessageID()
valueToFind := RandomBitmapP()
2018-03-09 22:43:30 +01:00
nodeToFind := Contact{ID: RandomBitmapP(), IP: net.ParseIP("1.2.3.4"), Port: 1286}
2018-04-28 02:16:12 +02:00
dht.node.store.Upsert(valueToFind, nodeToFind)
dht.node.store.Upsert(valueToFind, nodeToFind)
dht.node.store.Upsert(valueToFind, nodeToFind)
2018-03-09 22:43:30 +01:00
request := Request{
ID: messageID,
2018-04-03 19:38:01 +02:00
NodeID: testNodeID,
2018-03-09 22:43:30 +01:00
Method: findValueMethod,
2018-04-04 18:01:44 +02:00
Arg: &valueToFind,
2018-03-09 22:43:30 +01:00
}
data, err := bencode.EncodeBytes(request)
if err != nil {
t.Fatal(err)
}
conn.toRead <- testUDPPacket{addr: conn.addr, data: data}
timer := time.NewTimer(3 * time.Second)
var response map[string]interface{}
select {
case <-timer.C:
t.Fatal("timeout")
case resp := <-conn.writes:
err := bencode.DecodeBytes(resp.data, &response)
if err != nil {
t.Fatal(err)
}
2018-03-07 22:15:58 +01:00
}
2018-03-09 22:43:30 +01:00
verifyResponse(t, response, messageID, dhtNodeID.rawString())
2018-03-09 22:43:30 +01:00
_, ok := response[headerPayloadField]
if !ok {
t.Fatal("missing payload field")
}
payload, ok := response[headerPayloadField].(map[string]interface{})
if !ok {
t.Fatal("payload is not a dictionary")
}
compactContacts, ok := payload[valueToFind.rawString()]
2018-03-09 22:43:30 +01:00
if !ok {
t.Fatal("payload is missing key for search value")
}
contacts, ok := compactContacts.([]interface{})
if !ok {
t.Fatal("search results are not a list")
}
2018-04-28 02:16:12 +02:00
verifyCompactContacts(t, contacts, []Contact{nodeToFind})
2018-03-07 22:15:58 +01:00
}
2018-03-09 22:43:30 +01:00
func TestFindValueFallbackToFindNode(t *testing.T) {
dhtNodeID := RandomBitmapP()
testNodeID := RandomBitmapP()
2018-03-07 22:15:58 +01:00
conn := newTestUDPConn("127.0.0.1:21217")
dht, err := New(&Config{Address: "127.0.0.1:21216", NodeID: dhtNodeID.Hex()})
if err != nil {
t.Fatal(err)
}
2018-04-28 02:16:12 +02:00
err = dht.node.Connect(conn)
if err != nil {
t.Fatal(err)
}
2018-03-29 03:05:27 +02:00
defer dht.Shutdown()
2018-03-07 22:15:58 +01:00
2018-03-09 22:43:30 +01:00
nodesToInsert := 3
2018-04-28 02:16:12 +02:00
var nodes []Contact
2018-03-09 22:43:30 +01:00
for i := 0; i < nodesToInsert; i++ {
n := Contact{ID: RandomBitmapP(), IP: net.ParseIP("127.0.0.1"), Port: 10000 + i}
2018-03-09 22:43:30 +01:00
nodes = append(nodes, n)
2018-04-28 02:16:12 +02:00
dht.node.rt.Update(n)
2018-03-09 22:43:30 +01:00
}
messageID := newMessageID()
valueToFind := RandomBitmapP()
2018-03-09 22:43:30 +01:00
request := Request{
ID: messageID,
2018-04-03 19:38:01 +02:00
NodeID: testNodeID,
2018-03-09 22:43:30 +01:00
Method: findValueMethod,
2018-04-04 18:01:44 +02:00
Arg: &valueToFind,
2018-03-09 22:43:30 +01:00
}
data, err := bencode.EncodeBytes(request)
if err != nil {
t.Fatal(err)
}
2018-03-07 22:15:58 +01:00
conn.toRead <- testUDPPacket{addr: conn.addr, data: data}
timer := time.NewTimer(3 * time.Second)
2018-03-09 22:43:30 +01:00
var response map[string]interface{}
2018-03-07 22:15:58 +01:00
select {
case <-timer.C:
2018-03-09 22:43:30 +01:00
t.Fatal("timeout")
2018-03-07 22:15:58 +01:00
case resp := <-conn.writes:
err := bencode.DecodeBytes(resp.data, &response)
if err != nil {
2018-03-09 22:43:30 +01:00
t.Fatal(err)
2018-03-07 22:15:58 +01:00
}
2018-03-09 22:43:30 +01:00
}
verifyResponse(t, response, messageID, dhtNodeID.rawString())
2018-03-09 22:43:30 +01:00
_, ok := response[headerPayloadField]
if !ok {
t.Fatal("missing payload field")
}
payload, ok := response[headerPayloadField].(map[string]interface{})
if !ok {
t.Fatal("payload is not a dictionary")
}
contactsList, ok := payload[contactsField]
2018-03-09 22:43:30 +01:00
if !ok {
t.Fatal("payload is missing 'contacts' key")
}
2018-03-07 22:15:58 +01:00
2018-03-09 22:43:30 +01:00
contacts, ok := contactsList.([]interface{})
if !ok {
t.Fatal("'contacts' is not a list")
2018-03-07 22:15:58 +01:00
}
2018-03-09 22:43:30 +01:00
verifyContacts(t, contacts, nodes)
2018-03-07 22:15:58 +01:00
}