-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer_test.go
183 lines (153 loc) · 4.76 KB
/
peer_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package noise
import (
"bytes"
"encoding/hex"
"errors"
"net"
"testing"
"time"
)
// Group of prebuilt peers, public keys and sessions to test purpose
var (
PeerAPb = PublicKey("f46bea91688c3187eebe66f25f1bcfcb6696c90c293b3a9dca749f6218b7bb52")
PeerBPb = PublicKey("d0bf26bed4774c612691fd7a618dd23660e316dde3916da5c7698dc9b685e2ae")
PeerCPb = PublicKey("4c67ad6ef6287f0cf7b1b888c1e93eb4c685e3bc59c33b1ecf79a3ad227219e8")
PeerDPb = PublicKey("83a2dd209b270d19aedaa4e588fd94fee599b510a49988efd067967ce25053d0")
PeerEPb = PublicKey("78112677879bb3922a60cbc12ecbc46fdd33e69447df7186f618a0011056a3c1")
PeerFPb = PublicKey("4c268f42ac66ed02f62d0f8951c7fa042b0a281f57385daf8ee4576b30b8fc00")
)
var (
sessionA = mockSession(&mockConn{}, PeerAPb)
sessionB = mockSession(&mockConn{}, PeerBPb)
sessionC = mockSession(&mockConn{}, PeerCPb)
sessionD = mockSession(&mockConn{}, PeerDPb)
sessionE = mockSession(&mockConn{}, PeerEPb)
sessionF = mockSession(&mockConn{}, PeerFPb)
)
var (
peerA = newPeer(sessionA)
peerB = newPeer(sessionB)
peerC = newPeer(sessionC)
peerD = newPeer(sessionD)
peerE = newPeer(sessionE)
peerF = newPeer(sessionF)
)
// Mock Address from net.Addr
// ref: https://pkg.go.dev/net#Addr
type mockAddr struct {
addr string
}
func (*mockAddr) Network() string {
return "tcp"
}
func (m *mockAddr) String() string {
return m.addr
}
// Mock Address from net.Conn
// ref: https://pkg.go.dev/net#Conn
type mockConn struct {
addr string
shouldFail bool
msg []byte
}
// Read reads data from the connection.
// Read can be made to time out and return an error after a fixed
// time limit; see SetDeadline and SetReadDeadline.
func (c *mockConn) Read(p []byte) (n int, err error) {
return 0, nil
}
// Write writes data to the connection.
// Write can be made to time out and return an error after a fixed
// time limit; see SetDeadline and SetWriteDeadline.
// time limit; see SetDeadline and SetWriteDeadline.
func (c *mockConn) Write(b []byte) (n int, err error) {
return len(c.msg), nil
}
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (c *mockConn) Close() error {
if c.shouldFail {
return errors.New("failing")
}
return nil
}
// LocalAddr returns the local network address, if known.
func (c *mockConn) LocalAddr() net.Addr {
return &mockAddr{c.addr}
}
// RemoteAddr returns the remote network address, if known.
func (c *mockConn) RemoteAddr() net.Addr {
return &mockAddr{c.addr}
}
// A zero value for t means I/O operations will not time out.
func (c *mockConn) SetDeadline(t time.Time) error {
return nil
}
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
func (c *mockConn) SetReadDeadline(t time.Time) error {
return nil
}
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (c *mockConn) SetWriteDeadline(t time.Time) error {
return nil
}
// Mock handshake state for secure session
type mockHandshakeState struct {
addr string
}
func (m *mockHandshakeState) PeerStatic() []byte {
return []byte(m.addr)
}
func (*mockHandshakeState) MessageIndex() int {
return 0
}
func (*mockHandshakeState) WriteMessage(out, payload []byte) ([]byte, CipherState, CipherState, error) {
return nil, nil, nil, nil
}
func (*mockHandshakeState) ReadMessage(out, message []byte) ([]byte, CipherState, CipherState, error) {
return nil, nil, nil, nil
}
// mockSession create a testable session
func mockSession(conn net.Conn, pb PublicKey) *session {
return &session{conn, KeyRing{}, pb, nil, nil}
}
// mockID create a new testable id from public key
func mockID(pb PublicKey) ID {
var id ID
addr := []byte(pb)
copy(id[:], addr)
return id
}
// From content generate a bytes 32 key
func mockBytes(content PublicKey) []byte {
var expected [32]byte
copy(expected[:], content)
return expected[:]
}
func TestByteID(t *testing.T) {
expected := mockBytes(PeerAPb)
id := mockID(PeerAPb)
if !bytes.Equal(id.Bytes(), expected[:]) {
t.Errorf("expected returned bytes equal to %v, got %v", string(expected[:]), string(id.Bytes()))
}
}
func TestStringID(t *testing.T) {
id := mockID(PeerAPb)
expected := mockBytes(PeerAPb)
if id.String() != string(expected) {
t.Errorf("expected returned string equal to %v, got %v", string(PeerAPb), id.String())
}
}
func TestHashID(t *testing.T) {
expected := "fab03245b98fc2491b64810d9ab7fccf86db272a54c038780d88852937d25242"
got := hex.EncodeToString(blake2([]byte(PeerAPb)))
if expected != got {
t.Errorf("expected returned %s equal to %s", got, expected)
}
}