-
Notifications
You must be signed in to change notification settings - Fork 143
/
aes_test.go
96 lines (91 loc) · 2.22 KB
/
aes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2021 Tencent Inc. All rights reserved.
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testAESUtilAPIV3Key = "testAPIv3Key0000"
testAESUtilCiphertext = "FsdXzxryWfKwvLJKf8LG/ToRPTRh8RN9wROC"
testAESUtilPlaintext = "Hello World"
testAESUtilNonce = "wCq51qZv4Yfg"
testAESUtilAssociatedData = "Dl56FrqWQJF1t9LtC3vEUsniZKvbqdR8"
)
func TestDecryptAes256Gcm(t *testing.T) {
type args struct {
apiv3Key string
associatedData string
nonce string
ciphertext string
}
tests := []struct {
name string
args args
plaintext string
wantErr bool
}{
{
name: "decrypt success",
args: args{
apiv3Key: testAESUtilAPIV3Key,
associatedData: testAESUtilAssociatedData,
nonce: testAESUtilNonce,
ciphertext: testAESUtilCiphertext,
},
wantErr: false,
plaintext: testAESUtilPlaintext,
},
{
name: "invalid base64 ciphertext",
args: args{
apiv3Key: testAESUtilAPIV3Key,
associatedData: testAESUtilAssociatedData,
nonce: testAESUtilNonce,
ciphertext: "invalid cipher",
},
wantErr: true,
},
{
name: "invalid ciphertext",
args: args{
apiv3Key: testAESUtilAPIV3Key,
associatedData: testAESUtilAssociatedData,
nonce: testAESUtilNonce,
ciphertext: "SGVsbG8gV29ybGQK",
},
wantErr: true,
},
{
name: "invalid aes key",
args: args{
apiv3Key: "not a aes key",
associatedData: testAESUtilAssociatedData,
nonce: testAESUtilNonce,
ciphertext: testAESUtilCiphertext,
},
wantErr: true,
},
{
name: "wrong aes key",
args: args{
apiv3Key: "testAPIv3Key1111",
associatedData: testAESUtilAssociatedData,
nonce: testAESUtilNonce,
ciphertext: testAESUtilCiphertext,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
plaintext, err := DecryptAES256GCM(
tt.args.apiv3Key, tt.args.associatedData, tt.args.nonce, tt.args.ciphertext,
)
require.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, tt.plaintext, plaintext)
},
)
}
}