-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver_test.go
143 lines (133 loc) · 3.08 KB
/
server_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
package quic
import (
"bytes"
"net"
"sync"
"testing"
"time"
)
// stubAddr implements net.Addr
type stubAddr string
func (s stubAddr) Network() string {
return "udp"
}
func (s stubAddr) String() string {
return string(s)
}
func TestAddressVerifier(t *testing.T) {
addr1 := stubAddr("1.2.3.4:50")
addr2 := stubAddr("5.6.7.8:90")
odcid := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
rscid := []byte{4, 3, 2, 1}
s, err := newAddressVerifier()
if err != nil {
t.Fatal(err)
}
now := time.Now()
s.timeFn = func() time.Time {
return now
}
token := s.NewToken(addr1, rscid, odcid)
t.Logf("token: %d %x", len(token), token)
cid := s.VerifyToken(addr1, rscid, token)
if !bytes.Equal(odcid, cid) {
t.Fatalf("expect cid: %x\nactual: %x", odcid, cid)
}
// Still valid
now = now.Add(10 * time.Second)
cid = s.VerifyToken(addr1, rscid, token)
if !bytes.Equal(odcid, cid) {
t.Fatalf("expect cid: %x\nactual: %x", odcid, cid)
}
// Wrong address
cid = s.VerifyToken(addr2, rscid, token)
if cid != nil {
t.Fatalf("expect cid: <nil>\nactual: %x", cid)
}
// Wrong SCID
cid = s.VerifyToken(addr1, rscid[:len(rscid)-1], token)
if cid != nil {
t.Fatalf("expect cid: <nil>\nactual: %x", cid)
}
// Wrong token
cid = s.VerifyToken(addr1, rscid, token[:len(token)-1])
if cid != nil {
t.Fatalf("expect cid: <nil>\nactual: %x", cid)
}
// Expired
now = now.Add(1 * time.Second)
cid = s.VerifyToken(addr1, rscid, token)
if cid != nil {
t.Fatalf("expect cid: <nil>\nactual: %x", cid)
}
}
func BenchmarkAddressVerifier(b *testing.B) {
addr := stubAddr("10.0.0.1:7890")
odcid := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
rscid := []byte{0, 0, 0, 0}
s := NewAddressVerifier()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
token := s.NewToken(addr, rscid, odcid)
cid := s.VerifyToken(addr, rscid, token)
if cid == nil {
b.Fatal("invalid token")
}
}
}
func TestServerNoConnection(t *testing.T) {
socket, err := net.ListenPacket("udp", "0.0.0.0:0")
if err != nil {
t.Fatal(err)
}
s := NewServer(newServerConfig())
s.SetListener(socket)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := s.Close()
if err != nil {
t.Errorf("server close: %v", err)
}
// Close again should not panic
err = s.Close()
if err != nil {
t.Logf("server close: %v", err)
}
}()
err = s.Serve()
if err != nil {
t.Logf("server serve: %v", err)
}
wg.Wait()
}
func TestServerCIDIssuer(t *testing.T) {
const id = 10000
s := NewServerCIDIssuer(id)
cid1, err := s.NewCID()
if err != nil {
t.Fatal(err)
}
if int(cid1[0]) != cidLength {
t.Fatalf("expect cid length: %d, actual: %d", cidLength, cid1[0])
}
sid, n := decodeServerID(cid1[1:])
if n != 2 {
t.Fatalf("expect decoded length: %d, actual: %d", 2, n)
}
if sid != id {
t.Fatalf("expect sid: %d, actual: %d", id, sid)
}
cid2, err := s.NewCID()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(cid1[:1+n], cid2[:1+n]) {
t.Fatalf("expect cid same prefix: %x %x", cid1, cid2)
}
if bytes.Equal(cid1[1+n:], cid2[1+n:]) {
t.Fatalf("expect cid suffix different: %x %x", cid1, cid2)
}
}