libgo: update to Go 1.11
[official-gcc.git] / libgo / go / crypto / aes / modes_test.go
bloba3364c9b51b3f705206dc5d97b4d1c8563be7d22
1 // Copyright 2016 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.
5 package aes
7 import (
8 "crypto/cipher"
9 "testing"
12 // Check that the optimized implementations of cipher modes will
13 // be picked up correctly.
15 // testInterface can be asserted to check that a type originates
16 // from this test group.
17 type testInterface interface {
18 InAESPackage() bool
21 // testBlock implements the cipher.Block interface and any *Able
22 // interfaces that need to be tested.
23 type testBlock struct{}
25 func (*testBlock) BlockSize() int { return 0 }
26 func (*testBlock) Encrypt(a, b []byte) {}
27 func (*testBlock) Decrypt(a, b []byte) {}
28 func (*testBlock) NewGCM(int, int) (cipher.AEAD, error) {
29 return &testAEAD{}, nil
31 func (*testBlock) NewCBCEncrypter([]byte) cipher.BlockMode {
32 return &testBlockMode{}
34 func (*testBlock) NewCBCDecrypter([]byte) cipher.BlockMode {
35 return &testBlockMode{}
37 func (*testBlock) NewCTR([]byte) cipher.Stream {
38 return &testStream{}
41 // testAEAD implements the cipher.AEAD interface.
42 type testAEAD struct{}
44 func (*testAEAD) NonceSize() int { return 0 }
45 func (*testAEAD) Overhead() int { return 0 }
46 func (*testAEAD) Seal(a, b, c, d []byte) []byte { return []byte{} }
47 func (*testAEAD) Open(a, b, c, d []byte) ([]byte, error) { return []byte{}, nil }
48 func (*testAEAD) InAESPackage() bool { return true }
50 // Test the gcmAble interface is detected correctly by the cipher package.
51 func TestGCMAble(t *testing.T) {
52 b := cipher.Block(&testBlock{})
53 if _, ok := b.(gcmAble); !ok {
54 t.Fatalf("testBlock does not implement the gcmAble interface")
56 aead, err := cipher.NewGCM(b)
57 if err != nil {
58 t.Fatalf("%v", err)
60 if _, ok := aead.(testInterface); !ok {
61 t.Fatalf("cipher.NewGCM did not use gcmAble interface")
65 // testBlockMode implements the cipher.BlockMode interface.
66 type testBlockMode struct{}
68 func (*testBlockMode) BlockSize() int { return 0 }
69 func (*testBlockMode) CryptBlocks(a, b []byte) {}
70 func (*testBlockMode) InAESPackage() bool { return true }
72 // Test the cbcEncAble interface is detected correctly by the cipher package.
73 func TestCBCEncAble(t *testing.T) {
74 b := cipher.Block(&testBlock{})
75 if _, ok := b.(cbcEncAble); !ok {
76 t.Fatalf("testBlock does not implement the cbcEncAble interface")
78 bm := cipher.NewCBCEncrypter(b, []byte{})
79 if _, ok := bm.(testInterface); !ok {
80 t.Fatalf("cipher.NewCBCEncrypter did not use cbcEncAble interface")
84 // Test the cbcDecAble interface is detected correctly by the cipher package.
85 func TestCBCDecAble(t *testing.T) {
86 b := cipher.Block(&testBlock{})
87 if _, ok := b.(cbcDecAble); !ok {
88 t.Fatalf("testBlock does not implement the cbcDecAble interface")
90 bm := cipher.NewCBCDecrypter(b, []byte{})
91 if _, ok := bm.(testInterface); !ok {
92 t.Fatalf("cipher.NewCBCDecrypter did not use cbcDecAble interface")
96 // testStream implements the cipher.Stream interface.
97 type testStream struct{}
99 func (*testStream) XORKeyStream(a, b []byte) {}
100 func (*testStream) InAESPackage() bool { return true }
102 // Test the ctrAble interface is detected correctly by the cipher package.
103 func TestCTRAble(t *testing.T) {
104 b := cipher.Block(&testBlock{})
105 if _, ok := b.(ctrAble); !ok {
106 t.Fatalf("testBlock does not implement the ctrAble interface")
108 s := cipher.NewCTR(b, []byte{})
109 if _, ok := s.(testInterface); !ok {
110 t.Fatalf("cipher.NewCTR did not use ctrAble interface")