-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher_suite_test.go
112 lines (92 loc) · 2.63 KB
/
cipher_suite_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package dtls
import (
"context"
"testing"
"time"
"github.com/pion/dtls/v3/internal/ciphersuite"
dtlsnet "github.com/pion/dtls/v3/pkg/net"
"github.com/pion/transport/v3/dpipe"
"github.com/pion/transport/v3/test"
)
func TestCipherSuiteName(t *testing.T) {
testCases := []struct {
suite CipherSuiteID
expected string
}{
{TLS_ECDHE_ECDSA_WITH_AES_128_CCM, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM"},
{CipherSuiteID(0x0000), "0x0000"},
}
for _, testCase := range testCases {
res := CipherSuiteName(testCase.suite)
if res != testCase.expected {
t.Fatalf("Expected: %s, got %s", testCase.expected, res)
}
}
}
func TestAllCipherSuites(t *testing.T) {
actual := len(allCipherSuites())
if actual == 0 {
t.Fatal()
}
}
// CustomCipher that is just used to assert Custom IDs work
type testCustomCipherSuite struct {
ciphersuite.TLSEcdheEcdsaWithAes128GcmSha256
authenticationType CipherSuiteAuthenticationType
}
func (t *testCustomCipherSuite) ID() CipherSuiteID {
return 0xFFFF
}
func (t *testCustomCipherSuite) AuthenticationType() CipherSuiteAuthenticationType {
return t.authenticationType
}
// Assert that two connections that pass in a CipherSuite with a CustomID works
func TestCustomCipherSuite(t *testing.T) {
type result struct {
c *Conn
err error
}
// Check for leaking routines
report := test.CheckRoutines(t)
defer report()
runTest := func(cipherFactory func() []CipherSuite) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ca, cb := dpipe.Pipe()
c := make(chan result)
go func() {
client, err := testClient(ctx, dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), &Config{
CipherSuites: []CipherSuiteID{},
CustomCipherSuites: cipherFactory,
}, true)
c <- result{client, err}
}()
server, err := testServer(ctx, dtlsnet.PacketConnFromConn(cb), cb.RemoteAddr(), &Config{
CipherSuites: []CipherSuiteID{},
CustomCipherSuites: cipherFactory,
}, true)
clientResult := <-c
if err != nil {
t.Error(err)
} else {
_ = server.Close()
}
if clientResult.err != nil {
t.Error(clientResult.err)
} else {
_ = clientResult.c.Close()
}
}
t.Run("Custom ID", func(*testing.T) {
runTest(func() []CipherSuite {
return []CipherSuite{&testCustomCipherSuite{authenticationType: CipherSuiteAuthenticationTypeCertificate}}
})
})
t.Run("Anonymous Cipher", func(*testing.T) {
runTest(func() []CipherSuite {
return []CipherSuite{&testCustomCipherSuite{authenticationType: CipherSuiteAuthenticationTypeAnonymous}}
})
})
}