...
Source file
src/crypto/aes/modes_test.go
1
2
3
4
5 package aes
6
7 import (
8 "crypto/cipher"
9 "testing"
10 )
11
12
13
14
15
16
17 type testInterface interface {
18 InAESPackage() bool
19 }
20
21
22
23 type testBlock struct{}
24
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
30 }
31 func (*testBlock) NewCBCEncrypter([]byte) cipher.BlockMode {
32 return &testBlockMode{}
33 }
34 func (*testBlock) NewCBCDecrypter([]byte) cipher.BlockMode {
35 return &testBlockMode{}
36 }
37 func (*testBlock) NewCTR([]byte) cipher.Stream {
38 return &testStream{}
39 }
40
41
42 type testAEAD struct{}
43
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 }
49
50
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")
55 }
56 aead, err := cipher.NewGCM(b)
57 if err != nil {
58 t.Fatalf("%v", err)
59 }
60 if _, ok := aead.(testInterface); !ok {
61 t.Fatalf("cipher.NewGCM did not use gcmAble interface")
62 }
63 }
64
65
66 type testBlockMode struct{}
67
68 func (*testBlockMode) BlockSize() int { return 0 }
69 func (*testBlockMode) CryptBlocks(a, b []byte) {}
70 func (*testBlockMode) InAESPackage() bool { return true }
71
72
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")
77 }
78 bm := cipher.NewCBCEncrypter(b, []byte{})
79 if _, ok := bm.(testInterface); !ok {
80 t.Fatalf("cipher.NewCBCEncrypter did not use cbcEncAble interface")
81 }
82 }
83
84
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")
89 }
90 bm := cipher.NewCBCDecrypter(b, []byte{})
91 if _, ok := bm.(testInterface); !ok {
92 t.Fatalf("cipher.NewCBCDecrypter did not use cbcDecAble interface")
93 }
94 }
95
96
97 type testStream struct{}
98
99 func (*testStream) XORKeyStream(a, b []byte) {}
100 func (*testStream) InAESPackage() bool { return true }
101
102
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")
107 }
108 s := cipher.NewCTR(b, []byte{})
109 if _, ok := s.(testInterface); !ok {
110 t.Fatalf("cipher.NewCTR did not use ctrAble interface")
111 }
112 }
113
View as plain text