-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn_test.go
78 lines (70 loc) · 1.46 KB
/
conn_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
package noiseconn
import (
"bytes"
"crypto/rand"
"errors"
"net"
"testing"
"github.com/flynn/noise"
"golang.org/x/sync/errgroup"
)
func TestConn(t *testing.T) {
p1, p2 := net.Pipe()
clientKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
if err != nil {
t.Fatal(err)
}
serverKey, err := noise.DH25519.GenerateKeypair(rand.Reader)
if err != nil {
t.Fatal(err)
}
client, err := NewConn(p1, noise.Config{
CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2b),
Pattern: noise.HandshakeIK,
Initiator: true,
StaticKeypair: clientKey,
PeerStatic: serverKey.Public,
})
if err != nil {
t.Fatal(err)
}
defer client.Close()
server, err := NewConn(p2, noise.Config{
CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2b),
Pattern: noise.HandshakeIK,
Initiator: false,
StaticKeypair: serverKey,
})
if err != nil {
t.Fatal(err)
}
defer server.Close()
var eg errgroup.Group
data := make([]byte, 65536)
for i := range data {
data[i] = byte(i % 256)
}
eg.Go(func() error {
_, err := client.Write(data)
return err
})
eg.Go(func() error {
b := make([]byte, 655360)
n, err := server.Read(b)
if err != nil {
return err
}
m, err := server.Read(b[n:])
if err != nil {
return err
}
if !bytes.Equal(b[:n+m], data) {
return errors.New("failure")
}
return nil
})
err = eg.Wait()
if err != nil {
t.Fatal(err)
}
}