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
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package dtls
import (
"testing"
"time"
"github.com/pion/dtls/v3/pkg/protocol"
"github.com/pion/dtls/v3/pkg/protocol/extension"
"github.com/pion/dtls/v3/pkg/protocol/handshake"
"github.com/pion/dtls/v3/pkg/protocol/recordlayer"
"github.com/stretchr/testify/assert"
)
func TestRandomConnectionIDGenerator(t *testing.T) {
cases := map[string]struct {
reason string
size int
}{
"LengthMatch": {
reason: "Zero size should match length of generated CID.",
size: 0,
},
"LengthMatchSome": {
reason: "Non-zero size should match length of generated CID with non-zero.",
size: 8,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.size, len(RandomCIDGenerator(tc.size)()), "%s\nRandomCIDGenerator mismatch", tc.reason)
})
}
}
func TestOnlySendCIDGenerator(t *testing.T) {
cases := map[string]struct {
reason string
}{
"LengthMatch": {
reason: "CID length should always be zero.",
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
assert.Equalf(t, 0, len(OnlySendCIDGenerator()()), "%s\nOnlySendCIDGenerator mismatch", tc.reason)
})
}
}
func TestCIDDatagramRouter(t *testing.T) {
cid := []byte("abcd1234")
cidLen := 8
appRecord, err := (&recordlayer.RecordLayer{
Header: recordlayer.Header{
Epoch: 1,
Version: protocol.Version1_2,
},
Content: &protocol.ApplicationData{
Data: []byte("application data"),
},
}).Marshal()
assert.NoError(t, err)
appData, err := (&protocol.ApplicationData{
Data: []byte("some data"),
}).Marshal()
assert.NoError(t, err)
inner, err := (&recordlayer.InnerPlaintext{
Content: appData,
RealType: protocol.ContentTypeApplicationData,
}).Marshal()
assert.NoError(t, err)
cidHeader, err := (&recordlayer.Header{
Epoch: 1,
Version: protocol.Version1_2,
ContentType: protocol.ContentTypeConnectionID,
ContentLen: uint16(len(inner)), //nolint:gosec // G115
ConnectionID: cid,
SequenceNumber: 1,
}).Marshal()
assert.NoError(t, err)
cases := map[string]struct {
reason string
size int
datagram []byte
ok bool
want string
}{
"EmptyDatagram": {
reason: "If datagram is empty, we cannot extract an identifier",
size: cidLen,
datagram: []byte{},
ok: false,
want: "",
},
"NotADTLSRecord": {
reason: "If datagram is not a DTLS record, we cannot extract an identifier",
size: cidLen,
datagram: []byte("not a DTLS record"),
ok: false,
want: "",
},
"NotAConnectionIDDatagram": {
reason: "If datagram does not contain any Connection ID records, we cannot extract an identifier",
size: cidLen,
datagram: appRecord,
ok: false,
want: "",
},
"OneRecordConnectionID": {
reason: "If datagram contains one Connection ID record, we should be able to extract it.",
size: cidLen,
datagram: append(cidHeader, inner...),
ok: true,
want: string(cid),
},
"OneRecordConnectionIDAltLength": {
//nolint:lll
reason: "If datagram contains one Connection ID record, but it has the wrong length we should not be able to extract it.",
size: cidLen,
datagram: func() []byte {
altCIDHeader, err := (&recordlayer.Header{
Epoch: 1,
Version: protocol.Version1_2,
ContentType: protocol.ContentTypeConnectionID,
ContentLen: uint16(len(inner)), //nolint:gosec // G115
ConnectionID: []byte("abcd"),
SequenceNumber: 1,
}).Marshal()
assert.NoError(t, err)
return append(altCIDHeader, inner...)
}(),
ok: false,
want: "",
},
"MultipleRecordOneConnectionID": {
//nolint:lll
reason: "If datagram contains multiple records and one is a Connection ID record, we should be able to extract it.",
size: 8,
datagram: append(append(appRecord, cidHeader...), inner...),
ok: true,
want: string(cid),
},
"MultipleRecordMultipleConnectionID": {
//nolint:lll
reason: "If datagram contains multiple records and multiple are Connection ID records, we should extract the first one.",
size: 8,
datagram: append(append(append(appRecord, func() []byte {
altCIDHeader, err := (&recordlayer.Header{
Epoch: 1,
Version: protocol.Version1_2,
ContentType: protocol.ContentTypeConnectionID,
ContentLen: uint16(len(inner)), //nolint:gosec // G115
ConnectionID: []byte("1234abcd"),
SequenceNumber: 1,
}).Marshal()
assert.NoError(t, err)
return append(altCIDHeader, inner...)
}()...), cidHeader...), inner...),
ok: true,
want: "1234abcd",
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
cid, ok := cidDatagramRouter(tc.size)(tc.datagram)
assert.Equal(t, tc.ok, ok, "%s\ncidDatagramRouter mismatch", tc.reason)
assert.Equal(t, tc.want, cid, "%s\ncidDatagramRouter mismatch", tc.reason)
})
}
}
func TestCIDConnIdentifier(t *testing.T) {
cid := []byte("abcd1234")
cs := uint16(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
sh, err := (&recordlayer.RecordLayer{
Header: recordlayer.Header{
Epoch: 0,
Version: protocol.Version1_2,
},
Content: &handshake.Handshake{
Message: &handshake.MessageServerHello{
Version: protocol.Version1_2,
Random: handshake.Random{GMTUnixTime: time.Unix(500, 0), RandomBytes: [28]byte{}},
SessionID: []byte("hello"),
CipherSuiteID: &cs,
CompressionMethod: defaultCompressionMethods()[0],
Extensions: []extension.Extension{
&extension.ConnectionID{
CID: cid,
},
},
},
},
}).Marshal()
assert.NoError(t, err)
appRecord, err := (&recordlayer.RecordLayer{
Header: recordlayer.Header{
Epoch: 1,
Version: protocol.Version1_2,
},
Content: &protocol.ApplicationData{
Data: []byte("application data"),
},
}).Marshal()
assert.NoError(t, err)
cases := map[string]struct {
reason string
datagram []byte
ok bool
want string
}{
"EmptyDatagram": {
reason: "If datagram is empty, we cannot extract an identifier",
datagram: []byte{},
ok: false,
want: "",
},
"NotADTLSRecord": {
reason: "If datagram is not a DTLS record, we cannot extract an identifier",
datagram: []byte("not a DTLS record"),
ok: false,
want: "",
},
"NotAServerhelloDatagram": {
reason: "If datagram does not contain any ServerHello record, we cannot extract an identifier",
datagram: appRecord,
ok: false,
want: "",
},
"OneRecordServerHello": {
reason: "If datagram contains one ServerHello record, we should be able to extract an identifier.",
datagram: sh,
ok: true,
want: string(cid),
},
"MultipleRecordFirstServerHello": {
//nolint:lll
reason: "If datagram contains multiple records and the first is a ServerHello record, we should be able to extract an identifier.",
datagram: append(sh, appRecord...),
ok: true,
want: string(cid),
},
"MultipleRecordNotFirstServerHello": {
//nolint:lll
reason: "If datagram contains multiple records and the first is not a ServerHello record, we should not be able to extract an identifier.",
datagram: append(appRecord, sh...),
ok: false,
want: "",
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
cid, ok := cidConnIdentifier()(tc.datagram)
assert.Equalf(t, tc.ok, ok, "%s\ncidConnIdentifier mismatch", tc.reason)
assert.Equalf(t, tc.want, cid, "%s\ncidConnIdentifier mismatch", tc.reason)
})
}
}
|