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