tls_curves_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package esutils
  14. import (
  15. "crypto/tls"
  16. "testing"
  17. "github.com/stretchr/testify/require"
  18. )
  19. func TestParseCurvePreferences(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. input []string
  23. want []tls.CurveID
  24. wantErr bool
  25. }{
  26. {
  27. name: "well-known curve names",
  28. input: []string{"X25519", "CurveP256"},
  29. want: []tls.CurveID{tls.X25519, tls.CurveP256},
  30. },
  31. {
  32. name: "curve name aliases",
  33. input: []string{"P-256", "P384"},
  34. want: []tls.CurveID{tls.CurveP256, tls.CurveP384},
  35. },
  36. {
  37. name: "whitespace is trimmed",
  38. input: []string{" X25519 ", "CurveP256"},
  39. want: []tls.CurveID{tls.X25519, tls.CurveP256},
  40. },
  41. {
  42. name: "nil input uses Go defaults",
  43. input: nil,
  44. want: nil,
  45. },
  46. {
  47. name: "empty input uses Go defaults",
  48. input: []string{},
  49. want: nil,
  50. },
  51. {
  52. name: "unknown curve name",
  53. input: []string{"not-a-curve"},
  54. wantErr: true,
  55. },
  56. {
  57. name: "empty curve entry",
  58. input: []string{"X25519", ""},
  59. wantErr: true,
  60. },
  61. {
  62. name: "decimal curve ID",
  63. input: []string{"29"},
  64. want: []tls.CurveID{tls.X25519},
  65. },
  66. {
  67. name: "all four standard curves",
  68. input: []string{"X25519", "CurveP256", "CurveP384", "CurveP521"},
  69. want: []tls.CurveID{tls.X25519, tls.CurveP256, tls.CurveP384, tls.CurveP521},
  70. },
  71. {
  72. name: "P-521 alias",
  73. input: []string{"P-521"},
  74. want: []tls.CurveID{tls.CurveP521},
  75. },
  76. {
  77. name: "invalid numeric curve ID",
  78. input: []string{"9999"},
  79. wantErr: true,
  80. },
  81. {
  82. name: "zero is not a valid curve ID",
  83. input: []string{"0"},
  84. wantErr: true,
  85. },
  86. {
  87. name: "valid post-quantum hybrid curve by number",
  88. input: []string{"4588"},
  89. want: []tls.CurveID{tls.X25519MLKEM768},
  90. },
  91. }
  92. for _, tt := range tests {
  93. t.Run(tt.name, func(t *testing.T) {
  94. got, err := ParseCurvePreferences(tt.input)
  95. if tt.wantErr {
  96. require.Error(t, err)
  97. return
  98. }
  99. require.NoError(t, err)
  100. require.Equal(t, tt.want, got)
  101. })
  102. }
  103. }