| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
//go:build !js |
| 5 |
// +build !js |
| 6 |
|
| 7 |
package dtlsscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/ |
| 8 |
|
| 9 |
import ( |
| 10 |
"contextscip-go gomod github.com/golang/go/src go1.24.0 context/" |
| 11 |
"errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 12 |
"netscip-go gomod github.com/golang/go/src go1.24.0 net/" |
| 13 |
"testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 14 |
"timescip-go gomod github.com/golang/go/src go1.24.0 time/" |
| 15 |
|
| 16 |
"github.com/pion/dtls/v3/pkg/crypto/selfsignscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/selfsign`/" |
| 17 |
dtlsnetscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/net`/ "github.com/pion/dtls/v3/pkg/netscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/net`/" |
| •18 |
"github.com/pion/transport/v3/dpipescip-go gomod github.com/golang/go/src go1.24.0 `github.com/pion/transport/v3/dpipe`/" |
| 19 |
"github.com/pion/transport/v3/testscip-go gomod github.com/golang/go/src go1.24.0 `github.com/pion/transport/v3/test`/" |
| 20 |
"github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/" |
| 21 |
) |
| 22 |
|
| 23 |
func TestContextConfigscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/TestContextConfig().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { //nolint:cyclop |
| 24 |
// Limit runtime in case of deadlocks |
| 25 |
lim := testscip-go gomod github.com/golang/go/src go1.24.0 `github.com/pion/transport/v3/test`/.TimeOut(timescip-go gomod github.com/golang/go/src go1.24.0 time/.Secondscip-go gomod github.com/golang/go/src go1.24.0 time/Second. * 20) |
| 26 |
defer lim.Stop() |
| 27 |
|
| 28 |
report := testscip-go gomod github.com/golang/go/src go1.24.0 `github.com/pion/transport/v3/test`/.CheckRoutines(t) |
| 29 |
defer report() |
| 30 |
|
| 31 |
addrListen, err := netscip-go gomod github.com/golang/go/src go1.24.0 net/.ResolveUDPAddrscip-go gomod github.com/golang/go/src go1.24.0 net/ResolveUDPAddr().("udp", "localhost:0") |
| 32 |
assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/NoError().(t, err) |
| 33 |
|
| 34 |
// Dummy listener |
| 35 |
listen, err := netscip-go gomod github.com/golang/go/src go1.24.0 net/.ListenUDPscip-go gomod github.com/golang/go/src go1.24.0 net/ListenUDP().("udp", addrListen) |
| 36 |
assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/NoError().(t, err) |
| 37 |
defer func() { |
| 38 |
_ = listen.Closescip-go gomod github.com/golang/go/src go1.24.0 net/conn#Close().() |
| 39 |
}() |
| 40 |
addr, ok := listen.LocalAddrscip-go gomod github.com/golang/go/src go1.24.0 net/conn#LocalAddr().().(*netscip-go gomod github.com/golang/go/src go1.24.0 net/.UDPAddrscip-go gomod github.com/golang/go/src go1.24.0 net/UDPAddr#) |
| 41 |
assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.Truescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/True().(t, ok) |
| 42 |
|
| 43 |
cert, err := selfsignscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/selfsign`/.GenerateSelfSignedscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/selfsign`/GenerateSelfSigned().() |
| 44 |
assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/NoError().(t, err) |
| 45 |
|
| 46 |
clientOpts := []ClientOptionscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/ClientOption#{ |
| 47 |
WithCertificatesscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/WithCertificates().(cert), |
| 48 |
} |
| 49 |
serverOpts := []ServerOptionscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/ServerOption#{ |
| 50 |
WithCertificatesscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/WithCertificates().(cert), |
| 51 |
} |
| 52 |
|
| 53 |
dials := map[string]struct { |
| 54 |
f func() (func() (netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn#, error), func()) |
| 55 |
order []byte |
| 56 |
}{ |
| 57 |
"Dial": { |
| 58 |
f: func() (func() (netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn#, error), func()) { |
| 59 |
ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.24.0 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.24.0 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.24.0 context/Background().(), 40*timescip-go gomod github.com/golang/go/src go1.24.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.0 time/Millisecond.) |
| 60 |
|
| 61 |
return func() (netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn#, error) { |
| 62 |
conn, err := DialWithOptionsscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/DialWithOptions().("udp", addr, clientOpts...) |
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
|
| 67 |
return conn, conn.HandshakeContextscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/Conn#HandshakeContext().(ctx) |
| 68 |
}, func() { |
| 69 |
cancel() |
| 70 |
} |
| 71 |
}, |
| 72 |
order: []byte{0, 1, 2}, |
| 73 |
}, |
| 74 |
"Client": { |
| 75 |
f: func() (func() (netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn#, error), func()) { |
| 76 |
ca, _ := dpipescip-go gomod github.com/golang/go/src go1.24.0 `github.com/pion/transport/v3/dpipe`/.Pipe() |
| 77 |
ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.24.0 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.24.0 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.24.0 context/Background().(), 40*timescip-go gomod github.com/golang/go/src go1.24.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.0 time/Millisecond.) |
| 78 |
|
| 79 |
return func() (netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn#, error) { |
| 80 |
conn, err := ClientWithOptionsscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/ClientWithOptions().(dtlsnetscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/net`/.PacketConnFromConnscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/net`/PacketConnFromConn().(ca), ca.RemoteAddr(), clientOpts...) |
| 81 |
if err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
|
| 85 |
return conn, conn.HandshakeContextscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/Conn#HandshakeContext().(ctx) |
| 86 |
}, func() { |
| 87 |
_ = ca.Close() |
| 88 |
cancel() |
| 89 |
} |
| 90 |
}, |
| 91 |
order: []byte{0, 1, 2}, |
| 92 |
}, |
| 93 |
"Server": { |
| 94 |
f: func() (func() (netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn#, error), func()) { |
| 95 |
ca, _ := dpipescip-go gomod github.com/golang/go/src go1.24.0 `github.com/pion/transport/v3/dpipe`/.Pipe() |
| 96 |
ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.24.0 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.24.0 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.24.0 context/Background().(), 40*timescip-go gomod github.com/golang/go/src go1.24.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.0 time/Millisecond.) |
| 97 |
|
| 98 |
return func() (netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn#, error) { |
| 99 |
conn, err := ServerWithOptionsscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/ServerWithOptions().(dtlsnetscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/net`/.PacketConnFromConnscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/net`/PacketConnFromConn().(ca), ca.RemoteAddr(), serverOpts...) |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
return conn, conn.HandshakeContextscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3`/Conn#HandshakeContext().(ctx) |
| 105 |
}, func() { |
| 106 |
_ = ca.Close() |
| 107 |
cancel() |
| 108 |
} |
| 109 |
}, |
| 110 |
order: []byte{0, 1, 2}, |
| 111 |
}, |
| 112 |
} |
| 113 |
|
| 114 |
for name, dial := range dials { |
| 115 |
dial := dial |
| 116 |
t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(name, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 117 |
done := make(chan struct{}) |
| 118 |
|
| 119 |
go func() { |
| 120 |
d, cancel := dial.f() |
| 121 |
conn, err := d() |
| 122 |
defer cancel() |
| 123 |
var netError netscip-go gomod github.com/golang/go/src go1.24.0 net/.Errorscip-go gomod github.com/golang/go/src go1.24.0 net/Error# |
| 124 |
if !errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Asscip-go gomod github.com/golang/go/src go1.24.0 errors/As().(err, &netError) || !netError.Temporaryscip-go gomod github.com/golang/go/src go1.24.0 net/Error#Temporary().() { //nolint:staticcheck |
| 125 |
assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.Failscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Fail().(t, "Dial failed with unexpected error", "err: %v", err) |
| 126 |
close(done) |
| 127 |
|
| 128 |
return |
| 129 |
} |
| 130 |
done <- struct{}{} |
| 131 |
if err == nil { |
| 132 |
_ = conn.Closescip-go gomod github.com/golang/go/src go1.24.0 net/Conn#Close().() |
| 133 |
} |
| 134 |
}() |
| 135 |
|
| 136 |
var order []byte |
| 137 |
early := timescip-go gomod github.com/golang/go/src go1.24.0 time/.Afterscip-go gomod github.com/golang/go/src go1.24.0 time/After().(20 * timescip-go gomod github.com/golang/go/src go1.24.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.0 time/Millisecond.) |
| 138 |
late := timescip-go gomod github.com/golang/go/src go1.24.0 time/.Afterscip-go gomod github.com/golang/go/src go1.24.0 time/After().(60 * timescip-go gomod github.com/golang/go/src go1.24.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.0 time/Millisecond.) |
| 139 |
func() { |
| 140 |
for len(order) < 3 { |
| 141 |
select { |
| 142 |
case <-early: |
| 143 |
order = append(order, 0) |
| 144 |
case _, ok := <-done: |
| 145 |
if !ok { |
| 146 |
return |
| 147 |
} |
| 148 |
order = append(order, 1) |
| 149 |
case <-late: |
| 150 |
order = append(order, 2) |
| 151 |
} |
| 152 |
} |
| 153 |
}() |
| 154 |
assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Equal().(t, dial.order, order, "Invalid cancel timing") |
| 155 |
}) |
| 156 |
} |
| 157 |
} |