cipher_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. import (
  6. "bytes"
  7. "crypto"
  8. "crypto/aes"
  9. "crypto/rand"
  10. "testing"
  11. )
  12. func TestDefaultCiphersExist(t *testing.T) {
  13. for _, cipherAlgo := range supportedCiphers {
  14. if _, ok := cipherModes[cipherAlgo]; !ok {
  15. t.Errorf("default cipher %q is unknown", cipherAlgo)
  16. }
  17. }
  18. }
  19. func TestPacketCiphers(t *testing.T) {
  20. // Still test aes128cbc cipher althought it's commented out.
  21. cipherModes[aes128cbcID] = &streamCipherMode{16, aes.BlockSize, 0, nil}
  22. defer delete(cipherModes, aes128cbcID)
  23. for cipher := range cipherModes {
  24. kr := &kexResult{Hash: crypto.SHA1}
  25. algs := directionAlgorithms{
  26. Cipher: cipher,
  27. MAC: "hmac-sha1",
  28. Compression: "none",
  29. }
  30. client, err := newPacketCipher(clientKeys, algs, kr)
  31. if err != nil {
  32. t.Errorf("newPacketCipher(client, %q): %v", cipher, err)
  33. continue
  34. }
  35. server, err := newPacketCipher(clientKeys, algs, kr)
  36. if err != nil {
  37. t.Errorf("newPacketCipher(client, %q): %v", cipher, err)
  38. continue
  39. }
  40. want := "bla bla"
  41. input := []byte(want)
  42. buf := &bytes.Buffer{}
  43. if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
  44. t.Errorf("writePacket(%q): %v", cipher, err)
  45. continue
  46. }
  47. packet, err := server.readPacket(0, buf)
  48. if err != nil {
  49. t.Errorf("readPacket(%q): %v", cipher, err)
  50. continue
  51. }
  52. if string(packet) != want {
  53. t.Errorf("roundtrip(%q): got %q, want %q", cipher, packet, want)
  54. }
  55. }
  56. }