Skip to content

Commit ba0dff2

Browse files
authored
chore: add more tests (#2803)
* chore: add more tests * chore: add more tests
1 parent 10da5e0 commit ba0dff2

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

core/codec/aesecb.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
3232
return (*ecbEncrypter)(newECB(b))
3333
}
3434

35+
// BlockSize returns the mode's block size.
3536
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
3637

37-
// why we don't return error is because cipher.BlockMode doesn't allow this
38+
// CryptBlocks encrypts a number of blocks. The length of src must be a multiple of
39+
// the block size. Dst and src must overlap entirely or not at all.
3840
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
3941
if len(src)%x.blockSize != 0 {
4042
logx.Error("crypto/cipher: input not full blocks")
@@ -59,11 +61,13 @@ func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
5961
return (*ecbDecrypter)(newECB(b))
6062
}
6163

64+
// BlockSize returns the mode's block size.
6265
func (x *ecbDecrypter) BlockSize() int {
6366
return x.blockSize
6467
}
6568

66-
// why we don't return error is because cipher.BlockMode doesn't allow this
69+
// CryptBlocks decrypts a number of blocks. The length of src must be a multiple of
70+
// the block size. Dst and src must overlap entirely or not at all.
6771
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
6872
if len(src)%x.blockSize != 0 {
6973
logx.Error("crypto/cipher: input not full blocks")

core/codec/aesecb_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codec
22

33
import (
4+
"crypto/aes"
45
"encoding/base64"
56
"testing"
67

@@ -10,7 +11,8 @@ import (
1011
func TestAesEcb(t *testing.T) {
1112
var (
1213
key = []byte("q4t7w!z%C*F-JaNdRgUjXn2r5u8x/A?D")
13-
val = []byte("hello")
14+
val = []byte("helloworld")
15+
valLong = []byte("helloworldlong..")
1416
badKey1 = []byte("aaaaaaaaa")
1517
// more than 32 chars
1618
badKey2 = []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
@@ -31,6 +33,39 @@ func TestAesEcb(t *testing.T) {
3133
src, err := EcbDecrypt(key, dst)
3234
assert.Nil(t, err)
3335
assert.Equal(t, val, src)
36+
block, err := aes.NewCipher(key)
37+
assert.NoError(t, err)
38+
encrypter := NewECBEncrypter(block)
39+
assert.Equal(t, 16, encrypter.BlockSize())
40+
decrypter := NewECBDecrypter(block)
41+
assert.Equal(t, 16, decrypter.BlockSize())
42+
43+
dst = make([]byte, 8)
44+
encrypter.CryptBlocks(dst, val)
45+
for _, b := range dst {
46+
assert.Equal(t, byte(0), b)
47+
}
48+
49+
dst = make([]byte, 8)
50+
encrypter.CryptBlocks(dst, valLong)
51+
for _, b := range dst {
52+
assert.Equal(t, byte(0), b)
53+
}
54+
55+
dst = make([]byte, 8)
56+
decrypter.CryptBlocks(dst, val)
57+
for _, b := range dst {
58+
assert.Equal(t, byte(0), b)
59+
}
60+
61+
dst = make([]byte, 8)
62+
decrypter.CryptBlocks(dst, valLong)
63+
for _, b := range dst {
64+
assert.Equal(t, byte(0), b)
65+
}
66+
67+
_, err = EcbEncryptBase64("cTR0N3dDKkYtSmFOZFJnVWpYbjJyNXU4eC9BP0QK", "aGVsbG93b3JsZGxvbmcuLgo=")
68+
assert.Error(t, err)
3469
}
3570

3671
func TestAesEcbBase64(t *testing.T) {

core/codec/dh_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,17 @@ func TestKeyBytes(t *testing.T) {
8080
assert.Nil(t, err)
8181
assert.True(t, len(key.Bytes()) > 0)
8282
}
83+
84+
func TestDHOnErrors(t *testing.T) {
85+
key, err := GenerateKey()
86+
assert.Nil(t, err)
87+
assert.NotEmpty(t, key.Bytes())
88+
_, err = ComputeKey(key.PubKey, key.PriKey)
89+
assert.NoError(t, err)
90+
_, err = ComputeKey(nil, key.PriKey)
91+
assert.Error(t, err)
92+
_, err = ComputeKey(key.PubKey, nil)
93+
assert.Error(t, err)
94+
95+
assert.NotNil(t, NewPublicKey([]byte("")))
96+
}

0 commit comments

Comments
 (0)