github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/crypto/cipher/cipher_test.go (about)

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