Skip to content

Commit

Permalink
Unified unit test format
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Dec 7, 2022
1 parent 847ba02 commit 422436e
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 109 deletions.
8 changes: 4 additions & 4 deletions 3des_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var tripleDesTests = []struct {
{ECB, PKCS7, "hello world", "b8097975c76319c6172687e0d90fd4d1", "XAEXS5OHMMM4MFZGQ7QNSD6U2E======", "uAl5dcdjGcYXJofg2Q/U0Q=="},
}

func Test3Des_Encrypt_ToString(t *testing.T) {
func Test3Des_Encrypt_String(t *testing.T) {
for index, test := range tripleDesTests {
raw := Decode.FromString(test.toHex).ByHex().ToString()
e := Encrypt.FromString(test.input).By3Des(getCipher(test.mode, test.padding, tripleDesKey, tripleDesIV))
Expand All @@ -70,7 +70,7 @@ func Test3Des_Encrypt_ToString(t *testing.T) {
}
}

func Test3Des_Decrypt_ToString(t *testing.T) {
func Test3Des_Decrypt_String(t *testing.T) {
for index, test := range tripleDesTests {
raw := Decode.FromString(test.toHex).ByHex().ToString()
e := Decrypt.FromRawString(raw).By3Des(getCipher(test.mode, test.padding, tripleDesKey, tripleDesIV))
Expand Down Expand Up @@ -103,7 +103,7 @@ func Test3Des_Decrypt_ToString(t *testing.T) {
}
}

func Test3Des_Encrypt_ToBytes(t *testing.T) {
func Test3Des_Encrypt_Bytes(t *testing.T) {
for index, test := range tripleDesTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Encrypt.FromBytes([]byte(test.input)).By3Des(getCipher(test.mode, test.padding, []byte(tripleDesKey), []byte(tripleDesIV)))
Expand All @@ -123,7 +123,7 @@ func Test3Des_Encrypt_ToBytes(t *testing.T) {
}
}

func Test3Des_Decrypt_ToBytes(t *testing.T) {
func Test3Des_Decrypt_Bytes(t *testing.T) {
for index, test := range tripleDesTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Decrypt.FromRawBytes(raw).By3Des(getCipher(test.mode, test.padding, []byte(tripleDesKey), []byte(tripleDesIV)))
Expand Down
111 changes: 69 additions & 42 deletions baseX_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,58 @@ var baseXTests = []struct {
{"base100", "hello world", "👟👜👣👣👦🐗👮👦👩👣👛"},
}

func TestBaseX_Encode_ToString(t *testing.T) {
func TestBaseX_Encode_String(t *testing.T) {
for index, test := range baseXTests {
e := Encode.FromString(test.input)
e1 := Encode.FromString(test.input)
e2 := Encode.FromBytes([]byte(test.input))

switch test.baseX {
case "hex":
e = e.ByHex()
e1 = e1.ByHex()
e2 = e2.ByHex()
case "base16":
e = e.ByBase16()
e1 = e1.ByBase16()
e2 = e2.ByBase16()
case "base32":
e = e.ByBase32()
e1 = e1.ByBase32()
e2 = e2.ByBase32()
case "base58":
e = e.ByBase58()
e1 = e1.ByBase58()
e2 = e2.ByBase58()
case "base62":
e = e.ByBase62()
e1 = e1.ByBase62()
e2 = e2.ByBase62()
case "base64":
e = e.ByBase64()
e1 = e1.ByBase64()
e2 = e2.ByBase64()
case "base64URL":
e = e.ByBase64URL()
e1 = e1.ByBase64URL()
e2 = e2.ByBase64URL()
case "base85":
e = e.ByBase85()
e1 = e1.ByBase85()
e2 = e2.ByBase85()
case "base91":
e = e.ByBase91()
e1 = e1.ByBase91()
e2 = e2.ByBase91()
case "base100":
e = e.ByBase100()
e1 = e1.ByBase100()
e2 = e2.ByBase100()
}

t.Run(fmt.Sprintf(test.baseX+"_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.output, e.ToString())
assert.Equal(t, test.output, fmt.Sprintf("%s", e))
t.Run(fmt.Sprintf(test.baseX+"_e1_test_%d", index), func(t *testing.T) {
assert.Nil(t, e1.Error)
assert.Equal(t, test.output, e1.ToString())
assert.Equal(t, test.output, fmt.Sprintf("%s", e1))
})

t.Run(fmt.Sprintf(test.baseX+"_e2_%d", index), func(t *testing.T) {
assert.Nil(t, e2.Error)
assert.Equal(t, []byte(test.output), e2.ToBytes())
})
}
}

func TestBaseX_Decode_ToString(t *testing.T) {
func TestBaseX_Decode_String(t *testing.T) {
for index, test := range baseXTests {
d := Decode.FromString(test.output)

Expand All @@ -118,15 +134,12 @@ func TestBaseX_Decode_ToString(t *testing.T) {
d = d.ByBase100()
}

t.Run(fmt.Sprintf(test.baseX+"_test_%d", index), func(t *testing.T) {
assert.Nil(t, d.Error)
assert.Equal(t, test.input, d.ToString())
assert.Equal(t, test.input, fmt.Sprintf("%s", d))
})
assert.Nil(t, d.Error)
assert.Equal(t, test.input, d.ToString(), "Current test index is "+strconv.Itoa(index))
}
}

func TestBaseX_Encode_ToBytes(t *testing.T) {
func TestBaseX_Encode_Bytes(t *testing.T) {
for index, test := range baseXTests {
e := Encode.FromBytes([]byte(test.input))

Expand All @@ -153,14 +166,12 @@ func TestBaseX_Encode_ToBytes(t *testing.T) {
e = e.ByBase100()
}

t.Run(fmt.Sprintf(test.baseX+"_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.output), e.ToBytes())
})
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.output), e.ToBytes(), "Current test index is "+strconv.Itoa(index))
}
}

func TestBaseX_Decode_ToBytes(t *testing.T) {
func TestBaseX_Decode_Bytes(t *testing.T) {
for index, test := range baseXTests {
d := Decode.FromBytes([]byte(test.output))

Expand Down Expand Up @@ -194,7 +205,7 @@ func TestBaseX_Decode_ToBytes(t *testing.T) {
}
}

func TestBaseX_Ciphertext_Error(t *testing.T) {
func TestBaseX_Decoding_Error(t *testing.T) {
tests := []struct {
baseX string
input string // 输入值
Expand All @@ -214,32 +225,48 @@ func TestBaseX_Ciphertext_Error(t *testing.T) {
}

for index, test := range tests {
d := Decode.FromString(test.input)
d1 := Decode.FromString(test.input)
d2 := Decode.FromBytes([]byte(test.input))

switch test.baseX {
case "hex":
d = d.ByHex()
d1 = d1.ByHex()
d2 = d2.ByHex()
case "base16":
d = d.ByBase16()
d1 = d1.ByBase16()
d2 = d2.ByBase16()
case "base32":
d = d.ByBase32()
d1 = d1.ByBase32()
d2 = d2.ByBase32()
case "base58":
d = d.ByBase58()
d1 = d1.ByBase58()
d2 = d2.ByBase58()
case "base62":
d = d.ByBase62()
d1 = d1.ByBase62()
d2 = d2.ByBase62()
case "base64":
d = d.ByBase64()
d1 = d1.ByBase64()
d2 = d2.ByBase64()
case "base64URL":
d = d.ByBase64URL()
d1 = d1.ByBase64URL()
d2 = d2.ByBase64URL()
case "base85":
d = d.ByBase85()
d1 = d1.ByBase85()
d2 = d2.ByBase85()
case "base91":
d = d.ByBase91()
d1 = d1.ByBase91()
d2 = d2.ByBase91()
case "base100":
d = d.ByBase100()
d1 = d1.ByBase100()
d2 = d2.ByBase100()
}

t.Run(fmt.Sprintf(test.baseX+"_test_%d", index), func(t *testing.T) {
assert.Equal(t, invalidDecodingError(test.baseX), d.Error)
t.Run(fmt.Sprintf(test.baseX+"_d1_test_%d", index), func(t *testing.T) {
assert.Equal(t, invalidDecodingError(test.baseX), d1.Error)
})

t.Run(fmt.Sprintf(test.baseX+"_d2_test_%d", index), func(t *testing.T) {
assert.Equal(t, invalidDecodingError(test.baseX), d2.Error)
})
}
}
6 changes: 3 additions & 3 deletions bcrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestBcrypt_ToString(t *testing.T) {
func TestBcrypt_String(t *testing.T) {
s1 := Sign.FromString("").ByBcrypt(10)
v1 := Verify.FromRawString(s1.ToRawString(), "").ByBcrypt()
assert.Equal(t, false, v1.ToBool())
Expand All @@ -24,7 +24,7 @@ func TestBcrypt_ToString(t *testing.T) {
assert.Equal(t, true, v4.ToBool())
}

func TestBcrypt_ToBytes(t *testing.T) {
func TestBcrypt_Bytes(t *testing.T) {
s1 := Sign.FromBytes([]byte("")).ByBcrypt(1)
v1 := Verify.FromRawBytes(s1.ToRawBytes(), []byte("")).ByBcrypt()
assert.Equal(t, false, v1.ToBool())
Expand All @@ -34,7 +34,7 @@ func TestBcrypt_ToBytes(t *testing.T) {
assert.Equal(t, true, v2.ToBool())
}

func TestBcrypt_BcryptRounds_Error(t *testing.T) {
func TestBcrypt_Rounds_Error(t *testing.T) {
s := Sign.FromString("hello world").ByBcrypt(1)
assert.Equal(t, invalidBcryptRoundsError(), s.Error)
}
8 changes: 4 additions & 4 deletions des_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var desTests = []struct {
{ECB, PKCS7, "hello world", "28dba02eb5f6dd475d82e3681c83bb77", "KNugLrX23UddguNoHIO7dw=="},
}

func TestDes_Encrypt_ToString(t *testing.T) {
func TestDes_Encrypt_String(t *testing.T) {
for index, test := range desTests {
raw := Decode.FromString(test.toHex).ByHex().ToString()
e := Encrypt.FromString(test.input).ByDes(getCipher(test.mode, test.padding, desKey, desIV))
Expand All @@ -69,7 +69,7 @@ func TestDes_Encrypt_ToString(t *testing.T) {
}
}

func TestDes_Decrypt_ToString(t *testing.T) {
func TestDes_Decrypt_String(t *testing.T) {
for index, test := range desTests {
raw := Decode.FromString(test.toHex).ByHex().ToString()
e := Decrypt.FromRawString(raw).ByDes(getCipher(test.mode, test.padding, desKey, desIV))
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestDes_Decrypt_ToString(t *testing.T) {
}
}

func TestDes_Encrypt_ToBytes(t *testing.T) {
func TestDes_Encrypt_Bytes(t *testing.T) {
for index, test := range desTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Encrypt.FromBytes([]byte(test.input)).ByDes(getCipher(test.mode, test.padding, []byte(desKey), []byte(desIV)))
Expand All @@ -122,7 +122,7 @@ func TestDes_Encrypt_ToBytes(t *testing.T) {
}
}

func TestDes_Decrypt_ToBytes(t *testing.T) {
func TestDes_Decrypt_Bytes(t *testing.T) {
for index, test := range desTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Decrypt.FromRawBytes(raw).ByDes(getCipher(test.mode, test.padding, []byte(desKey), []byte(desIV)))
Expand Down
6 changes: 3 additions & 3 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var hashTests = []struct {
{"ripemd160", "hello world", "98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f", "mMYVeEzLX+WTb7wMvp39tAjZLw8="},
}

func TestHash_Encrypt_ToString(t *testing.T) {
func TestHash_Encrypt_String(t *testing.T) {
for index, test := range hashTests {
e := Encrypt.FromString(test.input)

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestHash_Encrypt_ToString(t *testing.T) {
}
}

func TestHash_Encrypt_ToBytes(t *testing.T) {
func TestHash_Encrypt_Bytes(t *testing.T) {
for index, test := range hashTests {
e := Encrypt.FromBytes([]byte(test.input))

Expand Down Expand Up @@ -143,7 +143,7 @@ func TestHash_Encrypt_ToBytes(t *testing.T) {
}
}

func TestHash_Digests_Error(t *testing.T) {
func TestHash_Size_Error(t *testing.T) {
e1 := Encrypt.FromString("hello world").BySha3(100)
assert.Equal(t, invalidHashSizeError(), e1.Error)
e2 := Encrypt.FromBytes([]byte("hello world")).BySha3(100)
Expand Down
6 changes: 3 additions & 3 deletions hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var hmacTests = []struct {
{"sm3", "hello world", "dongle", "8c733aae1d553c466a08c3e9e5daac3e99ae220181c7c1bc8c2564961de751b3", "jHM6rh1VPEZqCMPp5dqsPpmuIgGBx8G8jCVklh3nUbM="},
}

func TestHmac_Encrypt_ToString(t *testing.T) {
func TestHmac_Encrypt_String(t *testing.T) {
for index, test := range hmacTests {
e := Encrypt.FromString(test.input)

Expand Down Expand Up @@ -106,7 +106,7 @@ func TestHmac_Encrypt_ToString(t *testing.T) {
}
}

func TestHmac_Encrypt_ToBytes(t *testing.T) {
func TestHmac_Encrypt_Bytes(t *testing.T) {
for index, test := range hmacTests {
e := Encrypt.FromBytes([]byte(test.input)).ByHmacMd4([]byte(test.key))

Expand Down Expand Up @@ -151,7 +151,7 @@ func TestHmac_Encrypt_ToBytes(t *testing.T) {
}
}

func TestHmac_Digests_Error(t *testing.T) {
func TestHmac_Size_Error(t *testing.T) {
e1 := Encrypt.FromString("hello world").ByHmacSha3("dongle", 100)
assert.Equal(t, invalidHashSizeError(), e1.Error)
e2 := Encrypt.FromBytes([]byte("hello world")).ByHmacSha3([]byte("dongle"), 100)
Expand Down
8 changes: 4 additions & 4 deletions morse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var morseTests = []struct {
{"SOS", "/", ".../---/..."},
}

func TestMorse_Encode_ToString(t *testing.T) {
func TestMorse_Encode_String(t *testing.T) {
for index, test := range morseTests {
e := Encode.FromString(test.input).ByMorse(test.separator)

Expand All @@ -31,7 +31,7 @@ func TestMorse_Encode_ToString(t *testing.T) {
}
}

func TestMorse_Decode_ToString(t *testing.T) {
func TestMorse_Decode_String(t *testing.T) {
for index, test := range morseTests {
d := Decode.FromString(test.output).ByMorse(test.separator)

Expand All @@ -42,7 +42,7 @@ func TestMorse_Decode_ToString(t *testing.T) {
}
}

func TestMorse_Encode_ToBytes(t *testing.T) {
func TestMorse_Encode_Bytes(t *testing.T) {
for index, test := range morseTests {
e := Encode.FromBytes([]byte(test.input)).ByMorse(test.separator)

Expand All @@ -53,7 +53,7 @@ func TestMorse_Encode_ToBytes(t *testing.T) {
}
}

func TestMorse_Decode_ToBytes(t *testing.T) {
func TestMorse_Decode_Bytes(t *testing.T) {
for index, test := range morseTests {
d := Decode.FromBytes([]byte(test.output)).ByMorse(test.separator)

Expand Down

0 comments on commit 422436e

Please sign in to comment.