-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
133 lines (101 loc) · 3.86 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
package minq
import (
"net"
"testing"
"time"
)
// fake TransportFactory that comes populated with
// a set of pre-fab transports keyed by name.
type testTransportFactory struct {
transports map[string]*testTransport
}
func (f *testTransportFactory) MakeTransport(remote *net.UDPAddr) (Transport, error) {
return f.transports[remote.String()], nil
}
func (f *testTransportFactory) addTransport(remote *net.UDPAddr, t *testTransport) {
f.transports[remote.String()] = t
}
func serverInputAll(t *testing.T, trans *testTransport, s *Server, u net.UDPAddr) (*Connection, error) {
var clast *Connection
for {
p, err := trans.Recv()
if err != nil && err != ErrorWouldBlock {
return nil, err
}
if p == nil {
return clast, nil
}
c, err := s.Input(&u, p)
if err != nil {
return nil, err
}
if clast == nil {
clast = c
}
assertEquals(t, c, clast)
}
}
func TestServer(t *testing.T) {
ah := NewAddressHelper()
// Have the client and server do a handshake.
u, _ := net.ResolveUDPAddr("udp", "127.0.0.1:4443") // Just a fixed address
cTrans, sTrans := newTestTransportPair(true)
factory := &testTransportFactory{make(map[string]*testTransport)}
factory.addTransport(u, sTrans)
server := NewServer(factory, testTlsConfig(), nil)
assertNotNil(t, server, "Couldn't make server")
client := NewConnection(cTrans, RoleClient, testTlsConfig(), nil)
assertNotNil(t, client, "Couldn't make client")
n, err := client.CheckTimer()
assertEquals(t, 1, n)
assertNotError(t, err, "Couldn't send client initial")
s1, err := serverInputAll(t, sTrans, server, *u)
assertNotError(t, err, "Couldn't consume client initial")
err = inputAll(client)
assertNotError(t, err, "Error processing SH")
s2, err := serverInputAll(t, sTrans, server, *u)
assertNotError(t, err, "Error processing CFIN")
// Make sure we get the same server back.
assertEquals(t, s1, s2)
// Now make a new client and ensure we get a different server connection
u2, _ := net.ResolveUDPAddr("udp", "127.0.0.1:4444") // Just a fixed address
cTrans2, sTrans2 := newTestTransportPair(true)
factory.addTransport(u2, sTrans2)
client = NewConnection(cTrans2, RoleClient, testTlsConfig(), nil)
assertNotNil(t, client, "Couldn't make client")
n, err = client.CheckTimer()
assertEquals(t, 1, n)
assertNotError(t, err, "Couldn't send client initial")
s3, err := serverInputAll(t, sTrans2, server, *u2)
assertNotError(t, err, "Couldn't consume client initial")
assertX(t, s1 != s3, "Got the same server connection back with a different address")
assertEquals(t, 2, len(server.addrTable))
}
func TestServerIdleTimeout(t *testing.T) {
ah := NewAddressHelper()
// Have the client and server do a handshake.
u, _ := net.ResolveUDPAddr("udp", "127.0.0.1:4443") // Just a fixed address
cTrans, sTrans := newTestTransportPair(true)
factory := &testTransportFactory{make(map[string]*testTransport)}
factory.addTransport(u, sTrans)
server := NewServer(factory, testTlsConfig(), nil)
assertNotNil(t, server, "Couldn't make server")
client := NewConnection(cTrans, RoleClient, testTlsConfig(), nil)
assertNotNil(t, client, "Couldn't make client")
n, err := client.CheckTimer()
assertEquals(t, 1, n)
assertNotError(t, err, "Couldn't send client initial")
sconn, err := serverInputAll(t, sTrans, server, *u)
assertNotError(t, err, "Couldn't consume client initial")
assertNotNil(t, sconn, "no server connection")
assertEquals(t, 1, server.ConnectionCount())
// This pokes into internal state of the server to avoid having to include
// sleep calls in tests. Don't do this at home kids.
// Wind the timer on the connection back to short-circuit the idle timeout.
sconn.lastInput = sconn.lastInput.Add(-1 - sconn.idleTimeout)
server.CheckTimer()
// A second nap to allow for draining period.
sconn.closingEnd = sconn.closingEnd.Add(-1 - time.Second)
server.CheckTimer()
assertEquals(t, 0, server.ConnectionCount())
}