cipher_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2013 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 cipher_test
  5. import (
  6. "crypto/aes"
  7. "crypto/cipher"
  8. "testing"
  9. )
  10. func TestCryptBlocks(t *testing.T) {
  11. buf := make([]byte, 16)
  12. block, _ := aes.NewCipher(buf)
  13. mode := cipher.NewCBCDecrypter(block, buf)
  14. mustPanic(t, "crypto/cipher: input not full blocks", func() { mode.CryptBlocks(buf, buf[:3]) })
  15. mustPanic(t, "crypto/cipher: output smaller than input", func() { mode.CryptBlocks(buf[:3], buf) })
  16. mode = cipher.NewCBCEncrypter(block, buf)
  17. mustPanic(t, "crypto/cipher: input not full blocks", func() { mode.CryptBlocks(buf, buf[:3]) })
  18. mustPanic(t, "crypto/cipher: output smaller than input", func() { mode.CryptBlocks(buf[:3], buf) })
  19. }
  20. func mustPanic(t *testing.T, msg string, f func()) {
  21. defer func() {
  22. err := recover()
  23. if err == nil {
  24. t.Errorf("function did not panic, wanted %q", msg)
  25. } else if err != msg {
  26. t.Errorf("got panic %v, wanted %q", err, msg)
  27. }
  28. }()
  29. f()
  30. }