cipher_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "testing"
  8. )
  9. // TestCipherReversal tests that each cipher factory produces ciphers that can
  10. // encrypt and decrypt some data successfully.
  11. func TestCipherReversal(t *testing.T) {
  12. testData := []byte("abcdefghijklmnopqrstuvwxyz012345")
  13. testKey := []byte("AbCdEfGhIjKlMnOpQrStUvWxYz012345")
  14. testIv := []byte("sdflkjhsadflkjhasdflkjhsadfklhsa")
  15. cryptBuffer := make([]byte, 32)
  16. for name, cipherMode := range cipherModes {
  17. encrypter, err := cipherMode.createCipher(testKey, testIv)
  18. if err != nil {
  19. t.Errorf("failed to create encrypter for %q: %s", name, err)
  20. continue
  21. }
  22. decrypter, err := cipherMode.createCipher(testKey, testIv)
  23. if err != nil {
  24. t.Errorf("failed to create decrypter for %q: %s", name, err)
  25. continue
  26. }
  27. copy(cryptBuffer, testData)
  28. encrypter.XORKeyStream(cryptBuffer, cryptBuffer)
  29. if name == "none" {
  30. if !bytes.Equal(cryptBuffer, testData) {
  31. t.Errorf("encryption made change with 'none' cipher")
  32. continue
  33. }
  34. } else {
  35. if bytes.Equal(cryptBuffer, testData) {
  36. t.Errorf("encryption made no change with %q", name)
  37. continue
  38. }
  39. }
  40. decrypter.XORKeyStream(cryptBuffer, cryptBuffer)
  41. if !bytes.Equal(cryptBuffer, testData) {
  42. t.Errorf("decrypted bytes not equal to input with %q", name)
  43. continue
  44. }
  45. }
  46. }
  47. func TestDefaultCiphersExist(t *testing.T) {
  48. for _, cipherAlgo := range DefaultCipherOrder {
  49. if _, ok := cipherModes[cipherAlgo]; !ok {
  50. t.Errorf("default cipher %q is unknown", cipherAlgo)
  51. }
  52. }
  53. }