cipher_suite_test.go 2.5 KB

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