forked from pion/dtls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resume_test.go
218 lines (185 loc) · 4.4 KB
/
resume_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
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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package dtls
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/pion/dtls/v3/pkg/crypto/selfsign"
dtlsnet "github.com/pion/dtls/v3/pkg/net"
"github.com/pion/transport/v3/test"
)
var (
errMessageMissmatch = errors.New("messages missmatch")
errInvalidConnectionState = errors.New("failed to get connection state")
)
func TestResumeClient(t *testing.T) {
DoTestResume(t, Client, Server)
}
func TestResumeServer(t *testing.T) {
DoTestResume(t, Server, Client)
}
func fatal(t *testing.T, errChan chan error, err error) {
close(errChan)
t.Fatal(err)
}
func DoTestResume(t *testing.T, newLocal, newRemote func(net.PacketConn, net.Addr, *Config) (*Conn, error)) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
// Check for leaking routines
report := test.CheckRoutines(t)
defer report()
certificate, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatal(err)
}
// Generate connections
localConn1, rc1 := net.Pipe()
localConn2, rc2 := net.Pipe()
remoteConn := &backupConn{curr: rc1, next: rc2}
// Launch remote in another goroutine
errChan := make(chan error, 1)
defer func() {
err = <-errChan
if err != nil {
t.Fatal(err)
}
}()
config := &Config{
Certificates: []tls.Certificate{certificate},
InsecureSkipVerify: true,
ExtendedMasterSecret: RequireExtendedMasterSecret,
}
go func() {
var remote *Conn
var errR error
remote, errR = newRemote(dtlsnet.PacketConnFromConn(remoteConn), remoteConn.RemoteAddr(), config)
if errR != nil {
errChan <- errR
}
// Loop of read write
for i := 0; i < 2; i++ {
recv := make([]byte, 1024)
var n int
n, errR = remote.Read(recv)
if errR != nil {
errChan <- errR
}
if _, errR = remote.Write(recv[:n]); errR != nil {
errChan <- errR
}
}
errChan <- nil
}()
var local *Conn
local, err = newLocal(dtlsnet.PacketConnFromConn(localConn1), localConn1.RemoteAddr(), config)
if err != nil {
fatal(t, errChan, err)
}
defer func() {
_ = local.Close()
}()
// Test write and read
message := []byte("Hello")
if _, err = local.Write(message); err != nil {
fatal(t, errChan, err)
}
recv := make([]byte, 1024)
var n int
n, err = local.Read(recv)
if err != nil {
fatal(t, errChan, err)
}
if !bytes.Equal(message, recv[:n]) {
fatal(t, errChan, fmt.Errorf("%w: %s != %s", errMessageMissmatch, message, recv[:n]))
}
if err = localConn1.Close(); err != nil {
fatal(t, errChan, err)
}
// Serialize and deserialize state
state, ok := local.ConnectionState()
if !ok {
fatal(t, errChan, errInvalidConnectionState)
}
var b []byte
b, err = state.MarshalBinary()
if err != nil {
fatal(t, errChan, err)
}
deserialized := &State{}
if err = deserialized.UnmarshalBinary(b); err != nil {
fatal(t, errChan, err)
}
// Resume dtls connection
var resumed net.Conn
resumed, err = Resume(deserialized, dtlsnet.PacketConnFromConn(localConn2), localConn2.RemoteAddr(), config)
if err != nil {
fatal(t, errChan, err)
}
defer func() {
_ = resumed.Close()
}()
// Test write and read on resumed connection
if _, err = resumed.Write(message); err != nil {
fatal(t, errChan, err)
}
recv = make([]byte, 1024)
n, err = resumed.Read(recv)
if err != nil {
fatal(t, errChan, err)
}
if !bytes.Equal(message, recv[:n]) {
fatal(t, errChan, fmt.Errorf("%w: %s != %s", errMessageMissmatch, message, recv[:n]))
}
}
type backupConn struct {
curr net.Conn
next net.Conn
mux sync.Mutex
}
func (b *backupConn) Read(data []byte) (n int, err error) {
n, err = b.curr.Read(data)
if err != nil && b.next != nil {
b.mux.Lock()
b.curr = b.next
b.next = nil
b.mux.Unlock()
return b.Read(data)
}
return n, err
}
func (b *backupConn) Write(data []byte) (n int, err error) {
n, err = b.curr.Write(data)
if err != nil && b.next != nil {
b.mux.Lock()
b.curr = b.next
b.next = nil
b.mux.Unlock()
return b.Write(data)
}
return n, err
}
func (b *backupConn) Close() error {
return nil
}
func (b *backupConn) LocalAddr() net.Addr {
return nil
}
func (b *backupConn) RemoteAddr() net.Addr {
return nil
}
func (b *backupConn) SetDeadline(time.Time) error {
return nil
}
func (b *backupConn) SetReadDeadline(time.Time) error {
return nil
}
func (b *backupConn) SetWriteDeadline(time.Time) error {
return nil
}