forked from libp2p/go-libp2p-kad-dht
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging_test.go
76 lines (65 loc) · 1.94 KB
/
logging_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
package dht
import (
"testing"
cid "github.com/ipfs/go-cid"
)
func TestLoggableRecordKey(t *testing.T) {
c, err := cid.Decode("QmfUvYQhL2GinafMbPDYz7VFoZv4iiuLuR33aRsPurXGag")
if err != nil {
t.Fatal(err)
}
k, err := tryFormatLoggableRecordKey("/proto/" + string(c.Bytes()))
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != "/proto/"+multibaseB32Encode(c.Bytes()) {
t.Error("expected path to be preserved as a loggable key")
}
for _, s := range []string{"/bla", "", "bla bla"} {
if _, err := tryFormatLoggableRecordKey(s); err == nil {
t.Errorf("expected to fail formatting: %s", s)
}
}
for _, s := range []string{"/bla/asdf", "/a/b/c"} {
if _, err := tryFormatLoggableRecordKey(s); err != nil {
t.Errorf("expected to be formatable: %s", s)
}
}
}
func TestLoggableProviderKey(t *testing.T) {
c0, err := cid.Decode("QmfUvYQhL2GinafMbPDYz7VFoZv4iiuLuR33aRsPurXGag")
if err != nil {
t.Fatal(err)
}
// Test logging CIDv0 provider
b32MH := multibaseB32Encode(c0.Hash())
k, err := tryFormatLoggableProviderKey(c0.Bytes())
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != b32MH {
t.Error("expected cidv0 to be converted into base32 multihash")
}
// Test logging CIDv1 provider (from older DHT implementations)
c1 := cid.NewCidV1(cid.DagProtobuf, c0.Hash())
k, err = tryFormatLoggableProviderKey(c1.Hash())
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != b32MH {
t.Error("expected cidv1 to be converted into base32 multihash")
}
// Test logging multihash provider
k, err = tryFormatLoggableProviderKey(c1.Hash())
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != b32MH {
t.Error("expected multihash to be displayed in base32")
}
for _, s := range []string{"/bla", "", "bla bla", "/bla/asdf", "/a/b/c"} {
if _, err := tryFormatLoggableProviderKey([]byte(s)); err == nil {
t.Errorf("expected to fail formatting: %s", s)
}
}
}