-
Notifications
You must be signed in to change notification settings - Fork 15
/
disco_test.go
233 lines (204 loc) · 4.85 KB
/
disco_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package libdisco
import (
"bytes"
"fmt"
"io"
"math"
"net"
"testing"
"time"
)
/*
* These benchmarks were stolen from crypto/tls
*
*/
func newLocalListener(t testing.TB) net.Listener {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
ln, err = net.Listen("tcp6", "[::1]:0")
}
if err != nil {
t.Fatal(err)
}
return ln
}
func throughput(b *testing.B, totalBytes int64) {
serverKeyPair := GenerateKeypair(nil)
ln := newLocalListener(b)
defer ln.Close()
N := b.N
// Less than 64KB because Windows appears to use a TCP rwin < 64KB.
// See Issue #15899.
const bufsize = 32 << 10
go func() {
buf := make([]byte, bufsize)
for i := 0; i < N; i++ {
sconn, err := ln.Accept()
if err != nil {
// panic rather than synchronize to avoid benchmark overhead
// (cannot call b.Fatal in goroutine)
panic(fmt.Errorf("accept: %v", err))
}
serverConfig := &Config{
HandshakePattern: NoiseNK,
KeyPair: serverKeyPair,
}
srv := Server(sconn, serverConfig)
if err := srv.Handshake(); err != nil {
panic(fmt.Errorf("handshake: %v", err))
}
if _, err := io.CopyBuffer(srv, srv, buf); err != nil {
panic(fmt.Errorf("copy buffer: %v", err))
}
}
}()
b.SetBytes(totalBytes)
clientConfig := &Config{
HandshakePattern: NoiseNK,
RemoteKey: serverKeyPair.PublicKey[:],
}
buf := make([]byte, bufsize)
chunks := int(math.Ceil(float64(totalBytes) / float64(len(buf))))
for i := 0; i < N; i++ {
conn, err := Dial("tcp", ln.Addr().String(), clientConfig)
if err != nil {
b.Fatal(err)
}
for j := 0; j < chunks; j++ {
_, err := conn.Write(buf)
if err != nil {
b.Fatal(err)
}
_, err = io.ReadFull(conn, buf)
if err != nil {
b.Fatal(err)
}
}
conn.Close()
}
}
func BenchmarkThroughput(b *testing.B) {
for size := 1; size <= 64; size <<= 1 {
name := fmt.Sprintf("Packet/%dMB", size)
b.Run(name, func(b *testing.B) {
throughput(b, int64(size<<20))
})
}
}
type slowConn struct {
net.Conn
bps int
}
func (c *slowConn) Write(p []byte) (int, error) {
if c.bps == 0 {
panic("too slow")
}
t0 := time.Now()
wrote := 0
for wrote < len(p) {
time.Sleep(100 * time.Microsecond)
allowed := int(time.Since(t0).Seconds()*float64(c.bps)) / 8
if allowed > len(p) {
allowed = len(p)
}
if wrote < allowed {
n, err := c.Conn.Write(p[wrote:allowed])
wrote += n
if err != nil {
return wrote, err
}
}
}
return len(p), nil
}
func latency(b *testing.B, bps int) {
ln := newLocalListener(b)
defer ln.Close()
N := b.N
serverKeyPair := GenerateKeypair(nil)
go func() {
for i := 0; i < N; i++ {
sconn, err := ln.Accept()
if err != nil {
// panic rather than synchronize to avoid benchmark overhead
// (cannot call b.Fatal in goroutine)
panic(fmt.Errorf("accept: %v", err))
}
serverConfig := &Config{
HandshakePattern: NoiseNK,
KeyPair: serverKeyPair,
}
srv := Server(&slowConn{sconn, bps}, serverConfig)
if err := srv.Handshake(); err != nil {
panic(fmt.Errorf("handshake: %v", err))
}
io.Copy(srv, srv)
}
}()
clientConfig := &Config{
HandshakePattern: NoiseNK,
RemoteKey: serverKeyPair.PublicKey[:],
}
buf := make([]byte, 16384)
peek := make([]byte, 1)
for i := 0; i < N; i++ {
conn, err := Dial("tcp", ln.Addr().String(), clientConfig)
if err != nil {
b.Fatal(err)
}
// make sure we're connected and previous connection has stopped
if _, err := conn.Write(buf[:1]); err != nil {
b.Fatal(err)
}
if _, err := io.ReadFull(conn, peek); err != nil {
b.Fatal(err)
}
if _, err := conn.Write(buf); err != nil {
b.Fatal(err)
}
if _, err = io.ReadFull(conn, peek); err != nil {
b.Fatal(err)
}
conn.Close()
}
}
func BenchmarkLatency(b *testing.B) {
for _, kbps := range []int{200, 500, 1000, 2000, 5000} {
name := fmt.Sprintf("%dkbps", kbps)
b.Run(name, func(b *testing.B) {
latency(b, kbps*1000)
})
}
}
func TestSerialize(t *testing.T) {
// init
s := GenerateKeypair(nil)
rs := GenerateKeypair(nil)
hs := Initialize(NoiseIK, true, nil, s, nil, rs, nil)
// write first message
var msg []byte
hs.WriteMessage(nil, &msg)
// serialize
serialized := hs.Serialize()
// unserialize
hs2 := RecoverState(serialized, nil, s)
// let's write a message to parse
hsBob := Initialize(NoiseIK, false, nil, rs, nil, s, nil)
var msg2 []byte
hsBob.ReadMessage(msg, &msg2)
msg2 = msg2[:0]
hsBob.WriteMessage([]byte("hello"), &msg2)
// let's parse it
var msgRcv1, msgRcv2 []byte
c1, c2, _ := hs.ReadMessage(msg2, &msgRcv1)
t1, t2, _ := hs2.ReadMessage(msg2, &msgRcv2)
if !bytes.Equal(msgRcv1, msgRcv2) {
t.Fatal("received message not as expected")
}
if !bytes.Equal(c1.Serialize(), t1.Serialize()) {
t.Fatal("obtained strobeState not matching")
}
if !bytes.Equal(c2.Serialize(), t2.Serialize()) {
t.Fatal("obtained strobeState not matching")
}
}