-
Notifications
You must be signed in to change notification settings - Fork 0
/
shs_test.go
115 lines (91 loc) · 2.08 KB
/
shs_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
package shs
import (
"context"
"math/rand" // deterministic for tests
"testing"
b58 "github.com/jbenet/go-base58"
shs "github.com/keks/secretstream/secrethandshake"
transport "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
)
const (
appKeyLen = 32
testMessage = "Hello, World!"
)
var (
t1, t2 *Transport
k1, k2 *shs.EdKeyPair
appKey [appKeyLen]byte
)
type randReader struct{}
func (r randReader) Read(buf []byte) (int, error) {
return rand.Read(buf)
}
func init() {
rand.Read(appKey[:])
k1, _ = shs.GenEdKeyPair(randReader{})
k2, _ = shs.GenEdKeyPair(randReader{})
}
func TestCreateTransport(t *testing.T) {
t1 = NewTransport(*k1, appKey[:])
t2 = NewTransport(*k2, appKey[:])
}
func TestConnect(t *testing.T) {
// test transport interfaces
var (
tConn transport.Conn
tListener transport.Listener
tTransport transport.Transport
)
t1 = NewTransport(*k1, appKey[:])
t2 = NewTransport(*k2, appKey[:])
tTransport = t1 // compile time type check
_ = tTransport
laddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234/shs/" + b58.Encode(t1.keys.Public[:]))
if err != nil {
t.Fatal(err)
}
daddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235/shs/" + b58.Encode(t2.keys.Public[:]))
if err != nil {
t.Fatal(err)
}
l, err := t1.Listen(laddr)
if err != nil {
t.Fatal(err)
}
// compile time type checks
tListener = l
_ = tListener
go func() {
cs, err := l.Accept()
if err != nil {
t.Fatal(err)
}
// compile time type checks
tConn = cs
buf := make([]byte, 1024)
n, err := cs.Read(buf)
if err != nil {
t.Fatal(err)
}
if string(buf[:n]) != testMessage {
t.Fatal("test message wrong:", string(buf[:n]))
}
}()
d, err := t2.Dialer(daddr)
if err != nil {
t.Fatal(err)
}
cc, err := d.DialContext(context.WithValue(context.Background(), "t", t), laddr)
if err != nil {
t.Log("error dialing", laddr)
t.Fatal(err)
}
n, err := cc.Write([]byte(testMessage))
if err != nil {
t.Fatal(err)
}
if n != len(testMessage) {
t.Fatalf("c.Write returned %v, expected %v", n, len(testMessage))
}
}