github.com/pion/dtls/v2@v2.2.12/cipher_suite_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package dtls 5 6 import ( 7 "context" 8 "testing" 9 "time" 10 11 "github.com/pion/dtls/v2/internal/ciphersuite" 12 "github.com/pion/transport/v2/dpipe" 13 "github.com/pion/transport/v2/test" 14 ) 15 16 func TestCipherSuiteName(t *testing.T) { 17 testCases := []struct { 18 suite CipherSuiteID 19 expected string 20 }{ 21 {TLS_ECDHE_ECDSA_WITH_AES_128_CCM, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM"}, 22 {CipherSuiteID(0x0000), "0x0000"}, 23 } 24 25 for _, testCase := range testCases { 26 res := CipherSuiteName(testCase.suite) 27 if res != testCase.expected { 28 t.Fatalf("Expected: %s, got %s", testCase.expected, res) 29 } 30 } 31 } 32 33 func TestAllCipherSuites(t *testing.T) { 34 actual := len(allCipherSuites()) 35 if actual == 0 { 36 t.Fatal() 37 } 38 } 39 40 // CustomCipher that is just used to assert Custom IDs work 41 type testCustomCipherSuite struct { 42 ciphersuite.TLSEcdheEcdsaWithAes128GcmSha256 43 authenticationType CipherSuiteAuthenticationType 44 } 45 46 func (t *testCustomCipherSuite) ID() CipherSuiteID { 47 return 0xFFFF 48 } 49 50 func (t *testCustomCipherSuite) AuthenticationType() CipherSuiteAuthenticationType { 51 return t.authenticationType 52 } 53 54 // Assert that two connections that pass in a CipherSuite with a CustomID works 55 func TestCustomCipherSuite(t *testing.T) { 56 type result struct { 57 c *Conn 58 err error 59 } 60 61 // Check for leaking routines 62 report := test.CheckRoutines(t) 63 defer report() 64 65 runTest := func(cipherFactory func() []CipherSuite) { 66 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 67 defer cancel() 68 69 ca, cb := dpipe.Pipe() 70 c := make(chan result) 71 72 go func() { 73 client, err := testClient(ctx, ca, &Config{ 74 CipherSuites: []CipherSuiteID{}, 75 CustomCipherSuites: cipherFactory, 76 }, true) 77 c <- result{client, err} 78 }() 79 80 server, err := testServer(ctx, cb, &Config{ 81 CipherSuites: []CipherSuiteID{}, 82 CustomCipherSuites: cipherFactory, 83 }, true) 84 85 clientResult := <-c 86 87 if err != nil { 88 t.Error(err) 89 } else { 90 _ = server.Close() 91 } 92 93 if clientResult.err != nil { 94 t.Error(clientResult.err) 95 } else { 96 _ = clientResult.c.Close() 97 } 98 } 99 100 t.Run("Custom ID", func(t *testing.T) { 101 runTest(func() []CipherSuite { 102 return []CipherSuite{&testCustomCipherSuite{authenticationType: CipherSuiteAuthenticationTypeCertificate}} 103 }) 104 }) 105 106 t.Run("Anonymous Cipher", func(t *testing.T) { 107 runTest(func() []CipherSuite { 108 return []CipherSuite{&testCustomCipherSuite{authenticationType: CipherSuiteAuthenticationTypeAnonymous}} 109 }) 110 }) 111 }