Skip to content

Commit 70f0e00

Browse files
Copilotnomeguy
andcommitted
Address code review feedback: refactor tests to eliminate duplication
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
1 parent bff0e99 commit 70f0e00

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

service/tls_config_test.go

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@ package service
1616

1717
import (
1818
"crypto/tls"
19+
"strings"
1920
"testing"
2021
)
2122

22-
// TestTLSConfigurationExcludes3DES verifies that the TLS configuration
23-
// excludes vulnerable 3DES cipher suites to prevent Sweet32 attack
24-
func TestTLSConfigurationExcludes3DES(t *testing.T) {
25-
// Vulnerable 3DES cipher suites that should NOT be present
26-
vulnerable3DESCiphers := []uint16{
27-
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, // 0x000A
28-
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, // 0xC012
29-
}
30-
31-
// Get the cipher suites that would be used by the HTTPS server
32-
// This matches the configuration in the Start() function
33-
configuredCiphers := []uint16{
23+
// getConfiguredCipherSuites returns the cipher suites configured in the HTTPS server
24+
// This matches the configuration in the Start() function in proxy.go
25+
func getConfiguredCipherSuites() []uint16 {
26+
return []uint16{
3427
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
3528
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
3629
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
@@ -42,6 +35,18 @@ func TestTLSConfigurationExcludes3DES(t *testing.T) {
4235
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
4336
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
4437
}
38+
}
39+
40+
// TestTLSConfigurationExcludes3DES verifies that the TLS configuration
41+
// excludes vulnerable 3DES cipher suites to prevent Sweet32 attack
42+
func TestTLSConfigurationExcludes3DES(t *testing.T) {
43+
// Vulnerable 3DES cipher suites that should NOT be present
44+
vulnerable3DESCiphers := []uint16{
45+
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, // 0x000A
46+
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, // 0xC012
47+
}
48+
49+
configuredCiphers := getConfiguredCipherSuites()
4550

4651
// Verify that no vulnerable 3DES ciphers are in the configured list
4752
for _, vulnerableCipher := range vulnerable3DESCiphers {
@@ -67,18 +72,7 @@ func TestTLSMinimumVersion(t *testing.T) {
6772
// TestConfiguredCiphersAreSecure verifies that all configured cipher suites
6873
// are from the secure list (not from InsecureCipherSuites)
6974
func TestConfiguredCiphersAreSecure(t *testing.T) {
70-
configuredCiphers := []uint16{
71-
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
72-
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
73-
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
74-
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
75-
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
76-
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
77-
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
78-
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
79-
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
80-
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
81-
}
75+
configuredCiphers := getConfiguredCipherSuites()
8276

8377
// Get list of insecure cipher suites
8478
insecureCiphers := tls.InsecureCipherSuites()
@@ -98,18 +92,7 @@ func TestConfiguredCiphersAreSecure(t *testing.T) {
9892
// TestAllConfiguredCiphersHaveForwardSecrecy verifies that all configured
9993
// cipher suites use ECDHE for forward secrecy
10094
func TestAllConfiguredCiphersHaveForwardSecrecy(t *testing.T) {
101-
configuredCiphers := []uint16{
102-
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
103-
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
104-
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
105-
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
106-
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
107-
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
108-
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
109-
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
110-
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
111-
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
112-
}
95+
configuredCiphers := getConfiguredCipherSuites()
11396

11497
// Get the list of all secure cipher suites that Go supports
11598
secureCiphers := tls.CipherSuites()
@@ -128,13 +111,8 @@ func TestAllConfiguredCiphersHaveForwardSecrecy(t *testing.T) {
128111
}
129112

130113
// Verify the cipher name contains "ECDHE" for forward secrecy
131-
if !contains(suite.Name, "ECDHE") {
114+
if !strings.Contains(suite.Name, "ECDHE") {
132115
t.Errorf("Cipher suite %s (0x%04X) does not use ECDHE for forward secrecy", suite.Name, cipherID)
133116
}
134117
}
135118
}
136-
137-
// Helper function to check if a string contains a substring
138-
func contains(s, substr string) bool {
139-
return len(s) >= len(substr) && s[:len(substr)] == substr || len(s) > len(substr) && contains(s[1:], substr)
140-
}

0 commit comments

Comments
 (0)