ssh: use t.Run for testing multiple ciphers/macs

Change-Id: I410cecf0269cf2a88a8868d6b88f39f9b7026b6b
Reviewed-on: https://go-review.googlesource.com/87076
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Han-Wen Nienhuys
2018-01-10 14:51:55 +00:00
committed by Brad Fitzpatrick
parent 49373064ff
commit b3c9a1d25c
2 changed files with 52 additions and 50 deletions
+38 -37
View File
@@ -21,47 +21,48 @@ func TestDefaultCiphersExist(t *testing.T) {
} }
func TestPacketCiphers(t *testing.T) { func TestPacketCiphers(t *testing.T) {
// Still test aes128cbc cipher although it's commented out. defaultMac := "hmac-sha2-256"
cipherModes[aes128cbcID] = &streamCipherMode{16, aes.BlockSize, 0, nil} defaultCipher := "aes128-ctr"
defer delete(cipherModes, aes128cbcID)
for cipher := range cipherModes { for cipher := range cipherModes {
for mac := range macModes { t.Run("cipher="+cipher,
kr := &kexResult{Hash: crypto.SHA1} func(t *testing.T) { testPacketCipher(t, cipher, defaultMac) })
algs := directionAlgorithms{ }
Cipher: cipher, for mac := range macModes {
MAC: mac, t.Run("mac="+mac,
Compression: "none", func(t *testing.T) { testPacketCipher(t, defaultCipher, mac) })
} }
client, err := newPacketCipher(clientKeys, algs, kr) }
if err != nil {
t.Errorf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
continue
}
server, err := newPacketCipher(clientKeys, algs, kr)
if err != nil {
t.Errorf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
continue
}
want := "bla bla" func testPacketCipher(t *testing.T, cipher, mac string) {
input := []byte(want) kr := &kexResult{Hash: crypto.SHA1}
buf := &bytes.Buffer{} algs := directionAlgorithms{
if err := client.writePacket(0, buf, rand.Reader, input); err != nil { Cipher: cipher,
t.Errorf("writePacket(%q, %q): %v", cipher, mac, err) MAC: mac,
continue Compression: "none",
} }
client, err := newPacketCipher(clientKeys, algs, kr)
if err != nil {
t.Fatalf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
}
server, err := newPacketCipher(clientKeys, algs, kr)
if err != nil {
t.Fatalf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
}
packet, err := server.readPacket(0, buf) want := "bla bla"
if err != nil { input := []byte(want)
t.Errorf("readPacket(%q, %q): %v", cipher, mac, err) buf := &bytes.Buffer{}
continue if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
} t.Fatalf("writePacket(%q, %q): %v", cipher, mac, err)
}
if string(packet) != want { packet, err := server.readPacket(0, buf)
t.Errorf("roundtrip(%q, %q): got %q, want %q", cipher, mac, packet, want) if err != nil {
} t.Fatalf("readPacket(%q, %q): %v", cipher, mac, err)
} }
if string(packet) != want {
t.Errorf("roundtrip(%q, %q): got %q, want %q", cipher, mac, packet, want)
} }
} }
+14 -13
View File
@@ -333,21 +333,22 @@ func TestCiphers(t *testing.T) {
cipherOrder = append(cipherOrder, "aes128-cbc", "3des-cbc") cipherOrder = append(cipherOrder, "aes128-cbc", "3des-cbc")
for _, ciph := range cipherOrder { for _, ciph := range cipherOrder {
server := newServer(t) t.Run(ciph, func(t *testing.T) {
defer server.Shutdown() server := newServer(t)
conf := clientConfig() defer server.Shutdown()
conf.Ciphers = []string{ciph} conf := clientConfig()
// Don't fail if sshd doesn't have the cipher. conf.Ciphers = []string{ciph}
conf.Ciphers = append(conf.Ciphers, cipherOrder...) // Don't fail if sshd doesn't have the cipher.
conn, err := server.TryDial(conf) conf.Ciphers = append(conf.Ciphers, cipherOrder...)
if err == nil { conn, err := server.TryDial(conf)
conn.Close() if err == nil {
} else { conn.Close()
t.Fatalf("failed for cipher %q", ciph) } else {
} t.Fatalf("failed for cipher %q", ciph)
}
})
} }
} }
func TestMACs(t *testing.T) { func TestMACs(t *testing.T) {
var config ssh.Config var config ssh.Config
config.SetDefaults() config.SetDefaults()