tls_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 controller
  14. import (
  15. "crypto/tls"
  16. "testing"
  17. "github.com/stretchr/testify/require"
  18. )
  19. func applyTLSOpts(opts []func(*tls.Config)) *tls.Config {
  20. cfg := &tls.Config{}
  21. for _, fn := range opts {
  22. fn(cfg)
  23. }
  24. return cfg
  25. }
  26. func TestBuildTLSConfigFuncs(t *testing.T) {
  27. tests := []struct {
  28. name string
  29. ciphers string
  30. minVer string
  31. curves []string
  32. http2 bool
  33. wantErr bool
  34. wantMinVersion uint16
  35. wantCipherSuites bool
  36. wantCurvePreferences bool
  37. wantHTTP2Disabled bool
  38. }{
  39. {
  40. name: "all empty uses Go defaults, HTTP/2 disabled",
  41. wantHTTP2Disabled: true,
  42. },
  43. {
  44. name: "empty minVersion does not set MinVersion",
  45. minVer: "",
  46. wantMinVersion: 0,
  47. wantHTTP2Disabled: true,
  48. },
  49. {
  50. name: "explicit minVersion sets MinVersion",
  51. minVer: "1.3",
  52. wantMinVersion: tls.VersionTLS13,
  53. wantHTTP2Disabled: true,
  54. },
  55. {
  56. name: "explicit minVersion 1.2",
  57. minVer: "1.2",
  58. wantMinVersion: tls.VersionTLS12,
  59. wantHTTP2Disabled: true,
  60. },
  61. {
  62. name: "valid cipher suites are applied",
  63. ciphers: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  64. wantCipherSuites: true,
  65. wantHTTP2Disabled: true,
  66. },
  67. {
  68. name: "invalid cipher suite returns error",
  69. ciphers: "NOT_A_REAL_CIPHER",
  70. wantErr: true,
  71. },
  72. {
  73. name: "valid curve preferences are applied",
  74. curves: []string{"X25519", "CurveP256"},
  75. wantCurvePreferences: true,
  76. wantHTTP2Disabled: true,
  77. },
  78. {
  79. name: "invalid curve preference returns error",
  80. curves: []string{"not-a-curve"},
  81. wantErr: true,
  82. },
  83. {
  84. name: "invalid minVersion returns error",
  85. minVer: "1.4",
  86. wantErr: true,
  87. },
  88. {
  89. name: "HTTP/2 enabled does not add disableHTTP2",
  90. http2: true,
  91. },
  92. {
  93. name: "all settings together",
  94. ciphers: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  95. minVer: "1.2",
  96. curves: []string{"X25519"},
  97. http2: false,
  98. wantMinVersion: tls.VersionTLS12,
  99. wantCipherSuites: true,
  100. wantCurvePreferences: true,
  101. wantHTTP2Disabled: true,
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. opts, err := buildTLSConfigFuncs(tt.ciphers, tt.minVer, tt.curves, tt.http2)
  107. if tt.wantErr {
  108. require.Error(t, err)
  109. return
  110. }
  111. require.NoError(t, err)
  112. cfg := applyTLSOpts(opts)
  113. require.Equal(t, tt.wantMinVersion, cfg.MinVersion,
  114. "MinVersion mismatch")
  115. if tt.wantCipherSuites {
  116. require.NotEmpty(t, cfg.CipherSuites,
  117. "expected CipherSuites to be set")
  118. } else {
  119. require.Empty(t, cfg.CipherSuites,
  120. "expected CipherSuites to be empty")
  121. }
  122. if tt.wantCurvePreferences {
  123. require.NotEmpty(t, cfg.CurvePreferences,
  124. "expected CurvePreferences to be set")
  125. } else {
  126. require.Empty(t, cfg.CurvePreferences,
  127. "expected CurvePreferences to be empty")
  128. }
  129. if tt.wantHTTP2Disabled {
  130. require.Equal(t, []string{"http/1.1"}, cfg.NextProtos,
  131. "expected HTTP/2 to be disabled")
  132. } else {
  133. require.Empty(t, cfg.NextProtos,
  134. "expected NextProtos to be empty (HTTP/2 enabled)")
  135. }
  136. })
  137. }
  138. }