-
Notifications
You must be signed in to change notification settings - Fork 158
/
conn_go_test.go
157 lines (144 loc) · 3.55 KB
/
conn_go_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package dtls
import (
"bytes"
"context"
"crypto/tls"
"errors"
"net"
"testing"
"time"
"github.com/pion/dtls/v3/pkg/crypto/selfsign"
dtlsnet "github.com/pion/dtls/v3/pkg/net"
"github.com/pion/transport/v3/dpipe"
"github.com/pion/transport/v3/test"
)
func TestContextConfig(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
report := test.CheckRoutines(t)
defer report()
addrListen, err := net.ResolveUDPAddr("udp", "localhost:0")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Dummy listener
listen, err := net.ListenUDP("udp", addrListen)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer func() {
_ = listen.Close()
}()
addr, ok := listen.LocalAddr().(*net.UDPAddr)
if !ok {
t.Fatal("Failed to cast net.UDPAddr")
}
cert, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
config := &Config{
Certificates: []tls.Certificate{cert},
}
dials := map[string]struct {
f func() (func() (net.Conn, error), func())
order []byte
}{
"Dial": {
f: func() (func() (net.Conn, error), func()) {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Millisecond)
return func() (net.Conn, error) {
conn, err := Dial("udp", addr, config)
if err != nil {
return nil, err
}
return conn, conn.HandshakeContext(ctx)
}, func() {
cancel()
}
},
order: []byte{0, 1, 2},
},
"Client": {
f: func() (func() (net.Conn, error), func()) {
ca, _ := dpipe.Pipe()
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Millisecond)
return func() (net.Conn, error) {
conn, err := Client(dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), config)
if err != nil {
return nil, err
}
return conn, conn.HandshakeContext(ctx)
}, func() {
_ = ca.Close()
cancel()
}
},
order: []byte{0, 1, 2},
},
"Server": {
f: func() (func() (net.Conn, error), func()) {
ca, _ := dpipe.Pipe()
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Millisecond)
return func() (net.Conn, error) {
conn, err := Server(dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), config)
if err != nil {
return nil, err
}
return conn, conn.HandshakeContext(ctx)
}, func() {
_ = ca.Close()
cancel()
}
},
order: []byte{0, 1, 2},
},
}
for name, dial := range dials {
dial := dial
t.Run(name, func(t *testing.T) {
done := make(chan struct{})
go func() {
d, cancel := dial.f()
conn, err := d()
defer cancel()
var netError net.Error
if !errors.As(err, &netError) || !netError.Temporary() { //nolint:staticcheck
t.Errorf("Client error exp(Temporary network error) failed(%v)", err)
close(done)
return
}
done <- struct{}{}
if err == nil {
_ = conn.Close()
}
}()
var order []byte
early := time.After(20 * time.Millisecond)
late := time.After(60 * time.Millisecond)
func() {
for len(order) < 3 {
select {
case <-early:
order = append(order, 0)
case _, ok := <-done:
if !ok {
return
}
order = append(order, 1)
case <-late:
order = append(order, 2)
}
}
}()
if !bytes.Equal(dial.order, order) {
t.Errorf("Invalid cancel timing, expected: %v, got: %v", dial.order, order)
}
})
}
}