-
Notifications
You must be signed in to change notification settings - Fork 228
/
handlers_test.go
141 lines (119 loc) · 2.9 KB
/
handlers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package dht
import (
"bytes"
"context"
"fmt"
"math/rand"
"testing"
"time"
proto "github.com/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p"
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
recpb "github.com/libp2p/go-libp2p-record/pb"
crypto "github.com/libp2p/go-libp2p/core/crypto"
peer "github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
)
func TestCleanRecordSigned(t *testing.T) {
actual := new(recpb.Record)
actual.TimeReceived = "time"
actual.Value = []byte("value")
actual.Key = []byte("key")
cleanRecord(actual)
actualBytes, err := proto.Marshal(actual)
if err != nil {
t.Fatal(err)
}
expected := new(recpb.Record)
expected.Value = []byte("value")
expected.Key = []byte("key")
expectedBytes, err := proto.Marshal(expected)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(actualBytes, expectedBytes) {
t.Error("failed to clean record")
}
}
func TestCleanRecord(t *testing.T) {
actual := new(recpb.Record)
actual.TimeReceived = "time"
actual.Key = []byte("key")
actual.Value = []byte("value")
cleanRecord(actual)
actualBytes, err := proto.Marshal(actual)
if err != nil {
t.Fatal(err)
}
expected := new(recpb.Record)
expected.Key = []byte("key")
expected.Value = []byte("value")
expectedBytes, err := proto.Marshal(expected)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(actualBytes, expectedBytes) {
t.Error("failed to clean record")
}
}
func TestBadMessage(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dht := setupDHT(ctx, t, false)
for _, typ := range []pb.Message_MessageType{
pb.Message_PUT_VALUE, pb.Message_GET_VALUE, pb.Message_ADD_PROVIDER,
pb.Message_GET_PROVIDERS, pb.Message_FIND_NODE,
} {
msg := &pb.Message{
Type: typ,
// explicitly avoid the key.
}
_, err := dht.handlerForMsgType(typ)(ctx, dht.Host().ID(), msg)
if err == nil {
t.Fatalf("expected processing message to fail for type %s", pb.Message_FIND_NODE)
}
}
}
func BenchmarkHandleFindPeer(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h, err := libp2p.New()
if err != nil {
b.Fatal(err)
}
defer h.Close()
d, err := New(ctx, h)
if err != nil {
b.Fatal(err)
}
rng := rand.New(rand.NewSource(150))
var peers []peer.ID
for i := 0; i < 1000; i++ {
_, pubk, _ := crypto.GenerateEd25519Key(rng)
id, err := peer.IDFromPublicKey(pubk)
if err != nil {
panic(err)
}
d.peerFound(id)
peers = append(peers, id)
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 2000+i))
if err != nil {
panic(err)
}
d.host.Peerstore().AddAddr(id, a, time.Minute*50)
}
var reqs []*pb.Message
for i := 0; i < b.N; i++ {
reqs = append(reqs, &pb.Message{
Key: []byte("asdasdasd"),
})
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = d.handleFindPeer(ctx, peers[0], reqs[i])
if err != nil {
b.Error(err)
}
}
}