| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package e2e |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"math/rand" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/pion/dtls/v3" |
| 13 |
"github.com/pion/dtls/v3/pkg/crypto/selfsign" |
| 14 |
dtlsnet "github.com/pion/dtls/v3/pkg/net" |
| 15 |
transportTest "github.com/pion/transport/v4/test" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
flightInterval = time.Millisecond * 100 |
| 21 |
lossyTestTimeout = 30 * time.Second |
| 22 |
) |
| 23 |
|
| 24 |
// DTLS Client/Server over a lossy transport, just asserts it can handle at increasing increments |
| 25 |
|
| 26 |
func TestPionE2ELossy(t *testing.T) { //nolint:cyclop |
| 27 |
// Check for leaking routines |
| 28 |
report := transportTest.CheckRoutines(t) |
| 29 |
defer report() |
| 30 |
|
| 31 |
type runResult struct { |
| 32 |
dtlsConn *dtls.Conn |
| 33 |
err error |
| 34 |
} |
| 35 |
|
| 36 |
serverCert, err := selfsign.GenerateSelfSigned() |
| 37 |
assert.NoError(t, err) |
| 38 |
|
| 39 |
clientCert, err := selfsign.GenerateSelfSigned() |
| 40 |
assert.NoError(t, err) |
| 41 |
|
| 42 |
for _, test := range []struct { |
| 43 |
LossChanceRange int |
| 44 |
DoClientAuth bool |
| 45 |
CipherSuites []dtls.CipherSuiteID |
| 46 |
MTU int |
| 47 |
DisableServerFlightInterval bool |
| 48 |
}{ |
| 49 |
{ |
| 50 |
LossChanceRange: 0, |
| 51 |
}, |
| 52 |
{ |
| 53 |
LossChanceRange: 10, |
| 54 |
}, |
| 55 |
{ |
| 56 |
LossChanceRange: 20, |
| 57 |
}, |
| 58 |
{ |
| 59 |
LossChanceRange: 50, |
| 60 |
}, |
| 61 |
{ |
| 62 |
LossChanceRange: 0, |
| 63 |
DoClientAuth: true, |
| 64 |
}, |
| 65 |
{ |
| 66 |
LossChanceRange: 10, |
| 67 |
DoClientAuth: true, |
| 68 |
}, |
| 69 |
{ |
| 70 |
LossChanceRange: 20, |
| 71 |
DoClientAuth: true, |
| 72 |
}, |
| 73 |
{ |
| 74 |
LossChanceRange: 50, |
| 75 |
DoClientAuth: true, |
| 76 |
}, |
| 77 |
{ |
| 78 |
LossChanceRange: 0, |
| 79 |
CipherSuites: []dtls.CipherSuiteID{dtls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
LossChanceRange: 10, |
| 83 |
CipherSuites: []dtls.CipherSuiteID{dtls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
LossChanceRange: 20, |
| 87 |
CipherSuites: []dtls.CipherSuiteID{dtls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
LossChanceRange: 50, |
| 91 |
CipherSuites: []dtls.CipherSuiteID{dtls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
LossChanceRange: 10, |
| 95 |
MTU: 100, |
| 96 |
DoClientAuth: true, |
| 97 |
}, |
| 98 |
{ |
| 99 |
LossChanceRange: 20, |
| 100 |
MTU: 100, |
| 101 |
DoClientAuth: true, |
| 102 |
}, |
| 103 |
{ |
| 104 |
LossChanceRange: 50, |
| 105 |
MTU: 100, |
| 106 |
DoClientAuth: true, |
| 107 |
}, |
| 108 |
// Incoming retransmitted handshakes should cause us to retransmit. Disabling the FlightInterval on one side |
| 109 |
// means that a incoming re-transmissions causes the retransmission to be fired |
| 110 |
{ |
| 111 |
LossChanceRange: 10, |
| 112 |
DisableServerFlightInterval: true, |
| 113 |
}, |
| 114 |
{ |
| 115 |
LossChanceRange: 20, |
| 116 |
DisableServerFlightInterval: true, |
| 117 |
}, |
| 118 |
{ |
| 119 |
LossChanceRange: 50, |
| 120 |
DisableServerFlightInterval: true, |
| 121 |
}, |
| 122 |
} { |
| 123 |
name := fmt.Sprintf("Loss%d_MTU%d", test.LossChanceRange, test.MTU) |
| 124 |
if test.DoClientAuth { |
| 125 |
name += "_WithCliAuth" |
| 126 |
} |
| 127 |
for _, ciph := range test.CipherSuites { |
| 128 |
name += "_With" + ciph.String() |
| 129 |
} |
| 130 |
if test.DisableServerFlightInterval { |
| 131 |
name += "_WithNoServerFlightInterval" |
| 132 |
} |
| 133 |
|
| 134 |
test := test |
| 135 |
t.Run(name, func(t *testing.T) { |
| 136 |
// Limit runtime in case of deadlocks |
| 137 |
lim := transportTest.TimeOut(lossyTestTimeout + time.Second) |
| 138 |
defer lim.Stop() |
| 139 |
|
| 140 |
chosenLoss := rand.Intn(9) + test.LossChanceRange //nolint:gosec |
| 141 |
serverDone := make(chan runResult) |
| 142 |
clientDone := make(chan runResult) |
| 143 |
br := transportTest.NewBridge() |
| 144 |
|
| 145 |
assert.NoError(t, br.SetLossChance(chosenLoss)) |
| 146 |
|
| 147 |
go func() { |
| 148 |
clientOpts := []dtls.ClientOption{ |
| 149 |
dtls.WithFlightInterval(flightInterval), |
| 150 |
dtls.WithInsecureSkipVerify(true), |
| 151 |
dtls.WithDisableRetransmitBackoff(true), |
| 152 |
} |
| 153 |
if len(test.CipherSuites) > 0 { |
| 154 |
clientOpts = append(clientOpts, dtls.WithCipherSuites(test.CipherSuites...)) |
| 155 |
} |
| 156 |
if test.MTU > 0 { |
| 157 |
clientOpts = append(clientOpts, dtls.WithMTU(test.MTU)) |
| 158 |
} |
| 159 |
|
| 160 |
if test.DoClientAuth { |
| 161 |
clientOpts = append(clientOpts, dtls.WithCertificates(clientCert)) |
| 162 |
} |
| 163 |
|
| 164 |
client, startupErr := dtls.ClientWithOptions( |
| 165 |
dtlsnet.PacketConnFromConn(br.GetConn0()), |
| 166 |
br.GetConn0().RemoteAddr(), |
| 167 |
clientOpts..., |
| 168 |
) |
| 169 |
clientDone <- runResult{client, startupErr} |
| 170 |
}() |
| 171 |
|
| 172 |
go func() { |
| 173 |
serverOpts := []dtls.ServerOption{ |
| 174 |
dtls.WithCertificates(serverCert), |
| 175 |
dtls.WithFlightInterval(flightInterval), |
| 176 |
dtls.WithDisableRetransmitBackoff(true), |
| 177 |
} |
| 178 |
if test.MTU > 0 { |
| 179 |
serverOpts = append(serverOpts, dtls.WithMTU(test.MTU)) |
| 180 |
} |
| 181 |
|
| 182 |
if test.DoClientAuth { |
| 183 |
serverOpts = append(serverOpts, dtls.WithClientAuth(dtls.RequireAnyClientCert)) |
| 184 |
} |
| 185 |
|
| 186 |
if test.DisableServerFlightInterval { |
| 187 |
serverOpts = append(serverOpts, dtls.WithFlightInterval(time.Hour)) |
| 188 |
} |
| 189 |
|
| 190 |
server, startupErr := dtls.ServerWithOptions( |
| 191 |
dtlsnet.PacketConnFromConn(br.GetConn1()), |
| 192 |
br.GetConn1().RemoteAddr(), |
| 193 |
serverOpts..., |
| 194 |
) |
| 195 |
serverDone <- runResult{server, startupErr} |
| 196 |
}() |
| 197 |
|
| 198 |
testTimer := time.NewTimer(lossyTestTimeout) |
| 199 |
var serverConn, clientConn *dtls.Conn |
| 200 |
defer func() { |
| 201 |
if serverConn != nil { |
| 202 |
assert.NoError(t, serverConn.Close()) |
| 203 |
} |
| 204 |
if clientConn != nil { |
| 205 |
assert.NoError(t, clientConn.Close()) |
| 206 |
} |
| 207 |
}() |
| 208 |
|
| 209 |
for serverConn == nil || clientConn == nil { |
| 210 |
br.Tick() |
| 211 |
select { |
| 212 |
case serverResult := <-serverDone: |
| 213 |
if serverResult.err != nil { |
| 214 |
assert.Failf(t, "Fail, serverError", "clientComplete(%t) serverComplete(%t) LossChance(%d) error(%v)", |
| 215 |
clientConn != nil, serverConn != nil, chosenLoss, serverResult.err) |
| 216 |
|
| 217 |
return |
| 218 |
} |
| 219 |
|
| 220 |
serverConn = serverResult.dtlsConn |
| 221 |
case clientResult := <-clientDone: |
| 222 |
if clientResult.err != nil { |
| 223 |
assert.Failf(t, "Fail, clientError", "clientComplete(%t) serverComplete(%t) LossChance(%d) error(%v)", |
| 224 |
clientConn != nil, serverConn != nil, chosenLoss, clientResult.err) |
| 225 |
|
| 226 |
return |
| 227 |
} |
| 228 |
|
| 229 |
clientConn = clientResult.dtlsConn |
| 230 |
case <-testTimer.C: |
| 231 |
assert.Failf(t, "Test expired", "clientComplete(%t) serverComplete(%t) LossChance(%d)", |
| 232 |
clientConn != nil, serverConn != nil, chosenLoss) |
| 233 |
|
| 234 |
return |
| 235 |
case <-time.After(10 * time.Millisecond): |
| 236 |
} |
| 237 |
} |
| 238 |
}) |
| 239 |
} |
| 240 |
} |