This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
loggables_test.go
75 lines (65 loc) · 1.83 KB
/
loggables_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
package loggables
import (
"crypto/rand"
"errors"
"net"
"testing"
ic "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)
func TestNetConn(t *testing.T) {
connA, _ := net.Pipe()
loggable := NetConn(connA)
if _, ok := loggable.Loggable()["localAddr"]; !ok {
t.Fatalf("loggable missing localAddr")
}
if _, ok := loggable.Loggable()["remoteAddr"]; !ok {
t.Fatalf("loggable missing remoteAddr")
}
}
func TestError(t *testing.T) {
loggable := Error(errors.New("test"))
if _, ok := loggable.Loggable()["error"]; !ok {
t.Fatalf("loggable missing error")
}
}
func TestUuid(t *testing.T) {
loggable := Uuid("testKey")
if _, ok := loggable.Loggable()["testKey"]; !ok {
t.Fatalf("loggable does not contain expected key")
}
}
func TestDial(t *testing.T) {
leftPriv, _, _ := ic.GenerateECDSAKeyPair(rand.Reader)
lid, err := peer.IDFromPrivateKey(leftPriv)
if err != nil {
t.Fatalf("failed to create leftId: %s", err.Error())
}
rightPriv, _, _ := ic.GenerateECDSAKeyPair(rand.Reader)
rid, err := peer.IDFromPrivateKey(rightPriv)
if err != nil {
t.Fatalf("failed to create rightId: %s", err.Error())
}
laddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/80")
if err != nil {
t.Fatalf("failed to create laddr: %s", err.Error())
}
raddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/81")
if err != nil {
t.Fatalf("failed to create raddr: %s", err.Error())
}
metadata := Dial("test", lid, rid, laddr, raddr)
loggable := metadata.Loggable()
expected := [][]string{
{"localPeer", lid.Pretty()},
{"localAddr", laddr.String()},
{"remotePeer", rid.Pretty()},
{"remoteAddr", raddr.String()},
}
for _, tuple := range expected {
if actual, ok := loggable[tuple[0]]; !ok || actual != tuple[1] {
t.Fatalf("Expected %s but got %s", tuple[1], actual)
}
}
}