|
| 1 | +package ssh |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "golang.org/x/crypto/ssh" |
| 7 | +) |
| 8 | + |
| 9 | +func TestValidateAlgorithms(t *testing.T) { |
| 10 | + supported := ssh.SupportedAlgorithms() |
| 11 | + insecure := ssh.InsecureAlgorithms() |
| 12 | + |
| 13 | + t.Run("empty list returns nil", func(t *testing.T) { |
| 14 | + result, err := validateAlgorithms(nil, supported.Ciphers, insecure.Ciphers, "cipher") |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("unexpected error: %v", err) |
| 17 | + } |
| 18 | + if result != nil { |
| 19 | + t.Fatalf("expected nil, got %v", result) |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + t.Run("valid supported ciphers", func(t *testing.T) { |
| 24 | + input := []string{"aes128-gcm@openssh.com", "chacha20-poly1305@openssh.com"} |
| 25 | + result, err := validateAlgorithms(input, supported.Ciphers, insecure.Ciphers, "cipher") |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("unexpected error: %v", err) |
| 28 | + } |
| 29 | + if len(result) != 2 { |
| 30 | + t.Fatalf("expected 2 results, got %d", len(result)) |
| 31 | + } |
| 32 | + if result[0] != "aes128-gcm@openssh.com" || result[1] != "chacha20-poly1305@openssh.com" { |
| 33 | + t.Fatalf("unexpected result: %v", result) |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("valid insecure ciphers", func(t *testing.T) { |
| 38 | + input := []string{"aes128-cbc", "3des-cbc"} |
| 39 | + result, err := validateAlgorithms(input, supported.Ciphers, insecure.Ciphers, "cipher") |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("unexpected error: %v", err) |
| 42 | + } |
| 43 | + if len(result) != 2 { |
| 44 | + t.Fatalf("expected 2 results, got %d", len(result)) |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("invalid cipher returns error", func(t *testing.T) { |
| 49 | + input := []string{"aes128-gcm@openssh.com", "invalid-cipher-123"} |
| 50 | + _, err := validateAlgorithms(input, supported.Ciphers, insecure.Ciphers, "cipher") |
| 51 | + if err == nil { |
| 52 | + t.Fatal("expected error for invalid cipher") |
| 53 | + } |
| 54 | + expected := "unknown cipher: invalid-cipher-123" |
| 55 | + if err.Error() != expected { |
| 56 | + t.Fatalf("expected error %q, got %q", expected, err.Error()) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("case normalization", func(t *testing.T) { |
| 61 | + input := []string{"AES128-GCM@OPENSSH.COM"} |
| 62 | + result, err := validateAlgorithms(input, supported.Ciphers, insecure.Ciphers, "cipher") |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("unexpected error: %v", err) |
| 65 | + } |
| 66 | + if result[0] != "aes128-gcm@openssh.com" { |
| 67 | + t.Fatalf("expected lowercase, got %q", result[0]) |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("order preserved", func(t *testing.T) { |
| 72 | + input := []string{"aes256-ctr", "aes128-gcm@openssh.com", "chacha20-poly1305@openssh.com"} |
| 73 | + result, err := validateAlgorithms(input, supported.Ciphers, insecure.Ciphers, "cipher") |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("unexpected error: %v", err) |
| 76 | + } |
| 77 | + if result[0] != "aes256-ctr" || result[1] != "aes128-gcm@openssh.com" || result[2] != "chacha20-poly1305@openssh.com" { |
| 78 | + t.Fatalf("order not preserved: %v", result) |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("valid MACs", func(t *testing.T) { |
| 83 | + input := []string{"hmac-sha2-256-etm@openssh.com"} |
| 84 | + result, err := validateAlgorithms(input, supported.MACs, insecure.MACs, "mac") |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("unexpected error: %v", err) |
| 87 | + } |
| 88 | + if len(result) != 1 || result[0] != "hmac-sha2-256-etm@openssh.com" { |
| 89 | + t.Fatalf("unexpected result: %v", result) |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("invalid MAC returns error", func(t *testing.T) { |
| 94 | + input := []string{"invalid-mac"} |
| 95 | + _, err := validateAlgorithms(input, supported.MACs, insecure.MACs, "mac") |
| 96 | + if err == nil { |
| 97 | + t.Fatal("expected error for invalid mac") |
| 98 | + } |
| 99 | + expected := "unknown mac: invalid-mac" |
| 100 | + if err.Error() != expected { |
| 101 | + t.Fatalf("expected error %q, got %q", expected, err.Error()) |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + t.Run("valid key exchanges", func(t *testing.T) { |
| 106 | + input := []string{"curve25519-sha256"} |
| 107 | + result, err := validateAlgorithms(input, supported.KeyExchanges, insecure.KeyExchanges, "key exchange") |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("unexpected error: %v", err) |
| 110 | + } |
| 111 | + if len(result) != 1 || result[0] != "curve25519-sha256" { |
| 112 | + t.Fatalf("unexpected result: %v", result) |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("invalid key exchange returns error", func(t *testing.T) { |
| 117 | + input := []string{"invalid-kex"} |
| 118 | + _, err := validateAlgorithms(input, supported.KeyExchanges, insecure.KeyExchanges, "key exchange") |
| 119 | + if err == nil { |
| 120 | + t.Fatal("expected error for invalid key exchange") |
| 121 | + } |
| 122 | + expected := "unknown key exchange: invalid-kex" |
| 123 | + if err.Error() != expected { |
| 124 | + t.Fatalf("expected error %q, got %q", expected, err.Error()) |
| 125 | + } |
| 126 | + }) |
| 127 | + |
| 128 | + t.Run("duplicates passed through", func(t *testing.T) { |
| 129 | + input := []string{"aes128-gcm@openssh.com", "aes128-gcm@openssh.com"} |
| 130 | + result, err := validateAlgorithms(input, supported.Ciphers, insecure.Ciphers, "cipher") |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("unexpected error: %v", err) |
| 133 | + } |
| 134 | + if len(result) != 2 { |
| 135 | + t.Fatalf("expected 2 results (duplicates preserved), got %d", len(result)) |
| 136 | + } |
| 137 | + }) |
| 138 | +} |
0 commit comments