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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.21
package quic
import (
"bytes"
"context"
"crypto/tls"
"io"
"log/slog"
"net/netip"
"runtime"
"testing"
"time"
"golang.org/x/net/quic/qlog"
)
func TestConnect(t *testing.T) {
newLocalConnPair(t, &Config{}, &Config{})
}
func TestConnectDefaultTLSConfig(t *testing.T) {
serverConfig := newTestTLSConfigWithMoreDefaults(serverSide)
clientConfig := newTestTLSConfigWithMoreDefaults(clientSide)
newLocalConnPair(t, &Config{TLSConfig: serverConfig}, &Config{TLSConfig: clientConfig})
}
func TestStreamTransfer(t *testing.T) {
ctx := context.Background()
cli, srv := newLocalConnPair(t, &Config{}, &Config{})
data := makeTestData(1 << 20)
srvdone := make(chan struct{})
go func() {
defer close(srvdone)
s, err := srv.AcceptStream(ctx)
if err != nil {
t.Errorf("AcceptStream: %v", err)
return
}
b, err := io.ReadAll(s)
if err != nil {
t.Errorf("io.ReadAll(s): %v", err)
return
}
if !bytes.Equal(b, data) {
t.Errorf("read data mismatch (got %v bytes, want %v", len(b), len(data))
}
if err := s.Close(); err != nil {
t.Errorf("s.Close() = %v", err)
}
}()
s, err := cli.NewSendOnlyStream(ctx)
if err != nil {
t.Fatalf("NewStream: %v", err)
}
n, err := io.Copy(s, bytes.NewBuffer(data))
if n != int64(len(data)) || err != nil {
t.Fatalf("io.Copy(s, data) = %v, %v; want %v, nil", n, err, len(data))
}
if err := s.Close(); err != nil {
t.Fatalf("s.Close() = %v", err)
}
}
func newLocalConnPair(t testing.TB, conf1, conf2 *Config) (clientConn, serverConn *Conn) {
switch runtime.GOOS {
case "plan9":
t.Skipf("ReadMsgUDP not supported on %s", runtime.GOOS)
}
t.Helper()
ctx := context.Background()
e1 := newLocalEndpoint(t, serverSide, conf1)
e2 := newLocalEndpoint(t, clientSide, conf2)
conf2 = makeTestConfig(conf2, clientSide)
c2, err := e2.Dial(ctx, "udp", e1.LocalAddr().String(), conf2)
if err != nil {
t.Fatal(err)
}
c1, err := e1.Accept(ctx)
if err != nil {
t.Fatal(err)
}
return c2, c1
}
func newLocalEndpoint(t testing.TB, side connSide, conf *Config) *Endpoint {
t.Helper()
conf = makeTestConfig(conf, side)
e, err := Listen("udp", "127.0.0.1:0", conf)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
e.Close(canceledContext())
})
return e
}
func makeTestConfig(conf *Config, side connSide) *Config {
if conf == nil {
return nil
}
newConf := *conf
conf = &newConf
if conf.TLSConfig == nil {
conf.TLSConfig = newTestTLSConfig(side)
}
if conf.QLogLogger == nil {
conf.QLogLogger = slog.New(qlog.NewJSONHandler(qlog.HandlerOptions{
Level: QLogLevelFrame,
Dir: *qlogdir,
}))
}
return conf
}
type testEndpoint struct {
t *testing.T
e *Endpoint
now time.Time
recvc chan *datagram
idlec chan struct{}
conns map[*Conn]*testConn
acceptQueue []*testConn
configTransportParams []func(*transportParameters)
configTestConn []func(*testConn)
sentDatagrams [][]byte
peerTLSConn *tls.QUICConn
lastInitialDstConnID []byte // for parsing Retry packets
}
func newTestEndpoint(t *testing.T, config *Config) *testEndpoint {
te := &testEndpoint{
t: t,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
recvc: make(chan *datagram),
idlec: make(chan struct{}),
conns: make(map[*Conn]*testConn),
}
var err error
te.e, err = newEndpoint((*testEndpointUDPConn)(te), config, (*testEndpointHooks)(te))
if err != nil {
t.Fatal(err)
}
t.Cleanup(te.cleanup)
return te
}
func (te *testEndpoint) cleanup() {
te.e.Close(canceledContext())
}
func (te *testEndpoint) wait() {
select {
case te.idlec <- struct{}{}:
case <-te.e.closec:
}
for _, tc := range te.conns {
tc.wait()
}
}
// accept returns a server connection from the endpoint.
// Unlike Endpoint.Accept, connections are available as soon as they are created.
func (te *testEndpoint) accept() *testConn {
if len(te.acceptQueue) == 0 {
te.t.Fatalf("accept: expected available conn, but found none")
}
tc := te.acceptQueue[0]
te.acceptQueue = te.acceptQueue[1:]
return tc
}
func (te *testEndpoint) write(d *datagram) {
te.recvc <- d
te.wait()
}
var testClientAddr = netip.MustParseAddrPort("10.0.0.1:8000")
func (te *testEndpoint) writeDatagram(d *testDatagram) {
te.t.Helper()
logDatagram(te.t, "<- endpoint under test receives", d)
var buf []byte
for _, p := range d.packets {
tc := te.connForDestination(p.dstConnID)
if p.ptype != packetTypeRetry && tc != nil {
space := spaceForPacketType(p.ptype)
if p.num >= tc.peerNextPacketNum[space] {
tc.peerNextPacketNum[space] = p.num + 1
}
}
if p.ptype == packetTypeInitial {
te.lastInitialDstConnID = p.dstConnID
}
pad := 0
if p.ptype == packetType1RTT {
pad = d.paddedSize - len(buf)
}
buf = append(buf, encodeTestPacket(te.t, tc, p, pad)...)
}
for len(buf) < d.paddedSize {
buf = append(buf, 0)
}
te.write(&datagram{
b: buf,
peerAddr: d.addr,
})
}
func (te *testEndpoint) connForDestination(dstConnID []byte) *testConn {
for _, tc := range te.conns {
for _, loc := range tc.conn.connIDState.local {
if bytes.Equal(loc.cid, dstConnID) {
return tc
}
}
}
return nil
}
func (te *testEndpoint) connForSource(srcConnID []byte) *testConn {
for _, tc := range te.conns {
for _, loc := range tc.conn.connIDState.remote {
if bytes.Equal(loc.cid, srcConnID) {
return tc
}
}
}
return nil
}
func (te *testEndpoint) read() []byte {
te.t.Helper()
te.wait()
if len(te.sentDatagrams) == 0 {
return nil
}
d := te.sentDatagrams[0]
te.sentDatagrams = te.sentDatagrams[1:]
return d
}
func (te *testEndpoint) readDatagram() *testDatagram {
te.t.Helper()
buf := te.read()
if buf == nil {
return nil
}
p, _ := parseGenericLongHeaderPacket(buf)
tc := te.connForSource(p.dstConnID)
d := parseTestDatagram(te.t, te, tc, buf)
logDatagram(te.t, "-> endpoint under test sends", d)
return d
}
// wantDatagram indicates that we expect the Endpoint to send a datagram.
func (te *testEndpoint) wantDatagram(expectation string, want *testDatagram) {
te.t.Helper()
got := te.readDatagram()
if !datagramEqual(got, want) {
te.t.Fatalf("%v:\ngot datagram: %v\nwant datagram: %v", expectation, got, want)
}
}
// wantIdle indicates that we expect the Endpoint to not send any more datagrams.
func (te *testEndpoint) wantIdle(expectation string) {
if got := te.readDatagram(); got != nil {
te.t.Fatalf("expect: %v\nunexpectedly got: %v", expectation, got)
}
}
// advance causes time to pass.
func (te *testEndpoint) advance(d time.Duration) {
te.t.Helper()
te.advanceTo(te.now.Add(d))
}
// advanceTo sets the current time.
func (te *testEndpoint) advanceTo(now time.Time) {
te.t.Helper()
if te.now.After(now) {
te.t.Fatalf("time moved backwards: %v -> %v", te.now, now)
}
te.now = now
for _, tc := range te.conns {
if !tc.timer.After(te.now) {
tc.conn.sendMsg(timerEvent{})
tc.wait()
}
}
}
// testEndpointHooks implements endpointTestHooks.
type testEndpointHooks testEndpoint
func (te *testEndpointHooks) timeNow() time.Time {
return te.now
}
func (te *testEndpointHooks) newConn(c *Conn) {
tc := newTestConnForConn(te.t, (*testEndpoint)(te), c)
te.conns[c] = tc
}
// testEndpointUDPConn implements UDPConn.
type testEndpointUDPConn testEndpoint
func (te *testEndpointUDPConn) Close() error {
close(te.recvc)
return nil
}
func (te *testEndpointUDPConn) LocalAddr() netip.AddrPort {
return netip.MustParseAddrPort("127.0.0.1:443")
}
func (te *testEndpointUDPConn) Read(f func(*datagram)) {
for {
select {
case d, ok := <-te.recvc:
if !ok {
return
}
f(d)
case <-te.idlec:
}
}
}
func (te *testEndpointUDPConn) Write(dgram datagram) error {
te.sentDatagrams = append(te.sentDatagrams, append([]byte(nil), dgram.b...))
return nil
}
|