mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 03:02:28 +08:00
refactor: rewrite all unit test functions with assert
This commit is contained in:
@@ -13,10 +13,8 @@ func TestAesEcbEncrypt(t *testing.T) {
|
|||||||
aesEcbEncrypt := AesEcbEncrypt([]byte(data), []byte(key))
|
aesEcbEncrypt := AesEcbEncrypt([]byte(data), []byte(key))
|
||||||
aesEcbDecrypt := AesEcbDecrypt(aesEcbEncrypt, []byte(key))
|
aesEcbDecrypt := AesEcbDecrypt(aesEcbEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(aesEcbDecrypt) != data {
|
assert := internal.NewAssert(t, "TestAesEcbEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "AesEcbEncrypt/AesEcbDecrypt", data, data, string(aesEcbDecrypt))
|
assert.Equal(data, string(aesEcbDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAesCbcEncrypt(t *testing.T) {
|
func TestAesCbcEncrypt(t *testing.T) {
|
||||||
@@ -26,10 +24,8 @@ func TestAesCbcEncrypt(t *testing.T) {
|
|||||||
aesCbcEncrypt := AesCbcEncrypt([]byte(data), []byte(key))
|
aesCbcEncrypt := AesCbcEncrypt([]byte(data), []byte(key))
|
||||||
aesCbcDecrypt := AesCbcDecrypt(aesCbcEncrypt, []byte(key))
|
aesCbcDecrypt := AesCbcDecrypt(aesCbcEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(aesCbcDecrypt) != data {
|
assert := internal.NewAssert(t, "TestAesCbcEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "AesCbcEncrypt/AesCbcDecrypt", data, data, string(aesCbcDecrypt))
|
assert.Equal(data, string(aesCbcDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAesCtrCrypt(t *testing.T) {
|
func TestAesCtrCrypt(t *testing.T) {
|
||||||
@@ -39,10 +35,8 @@ func TestAesCtrCrypt(t *testing.T) {
|
|||||||
aesCtrCrypt := AesCtrCrypt([]byte(data), []byte(key))
|
aesCtrCrypt := AesCtrCrypt([]byte(data), []byte(key))
|
||||||
aesCtrDeCrypt := AesCtrCrypt(aesCtrCrypt, []byte(key))
|
aesCtrDeCrypt := AesCtrCrypt(aesCtrCrypt, []byte(key))
|
||||||
|
|
||||||
if string(aesCtrDeCrypt) != data {
|
assert := internal.NewAssert(t, "TestAesCtrCrypt")
|
||||||
internal.LogFailedTestInfo(t, "AesCtrCrypt", data, data, string(aesCtrDeCrypt))
|
assert.Equal(data, string(aesCtrDeCrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAesCfbEncrypt(t *testing.T) {
|
func TestAesCfbEncrypt(t *testing.T) {
|
||||||
@@ -52,10 +46,8 @@ func TestAesCfbEncrypt(t *testing.T) {
|
|||||||
aesCfbEncrypt := AesCfbEncrypt([]byte(data), []byte(key))
|
aesCfbEncrypt := AesCfbEncrypt([]byte(data), []byte(key))
|
||||||
aesCfbDecrypt := AesCfbDecrypt(aesCfbEncrypt, []byte(key))
|
aesCfbDecrypt := AesCfbDecrypt(aesCfbEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(aesCfbDecrypt) != data {
|
assert := internal.NewAssert(t, "TestAesCfbEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "AesCfbEncrypt/AesCfbDecrypt", data, data, string(aesCfbDecrypt))
|
assert.Equal(data, string(aesCfbDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAesOfbEncrypt(t *testing.T) {
|
func TestAesOfbEncrypt(t *testing.T) {
|
||||||
@@ -65,8 +57,6 @@ func TestAesOfbEncrypt(t *testing.T) {
|
|||||||
aesOfbEncrypt := AesOfbEncrypt([]byte(data), []byte(key))
|
aesOfbEncrypt := AesOfbEncrypt([]byte(data), []byte(key))
|
||||||
aesOfbDecrypt := AesOfbDecrypt(aesOfbEncrypt, []byte(key))
|
aesOfbDecrypt := AesOfbDecrypt(aesOfbEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(aesOfbDecrypt) != data {
|
assert := internal.NewAssert(t, "TestAesOfbEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "AesOfbEncrypt/AesOfbDecrypt", data, data, string(aesOfbDecrypt))
|
assert.Equal(data, string(aesOfbDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,66 +1,36 @@
|
|||||||
package cryptor
|
package cryptor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBase64StdEncode(t *testing.T) {
|
func TestBase64StdEncode(t *testing.T) {
|
||||||
s := "hello world"
|
assert := internal.NewAssert(t, "TestBase64StdEncode")
|
||||||
bs := Base64StdEncode(s)
|
assert.Equal("aGVsbG8gd29ybGQ=", Base64StdEncode("hello world"))
|
||||||
|
|
||||||
if bs != "aGVsbG8gd29ybGQ=" {
|
|
||||||
internal.LogFailedTestInfo(t, "Base64StdEncode", s, "aGVsbG8gd29ybGQ=", bs)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBase64StdDecode(t *testing.T) {
|
func TestBase64StdDecode(t *testing.T) {
|
||||||
bs := "aGVsbG8gd29ybGQ="
|
assert := internal.NewAssert(t, "TestBase64StdDecode")
|
||||||
s := Base64StdDecode(bs)
|
assert.Equal("hello world", Base64StdEncode("aGVsbG8gd29ybGQ="))
|
||||||
|
|
||||||
if s != "hello world" {
|
|
||||||
internal.LogFailedTestInfo(t, "Base64StdDecode", bs, "hello world=", s)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMd5String(t *testing.T) {
|
func TestMd5String(t *testing.T) {
|
||||||
s := "hello"
|
assert := internal.NewAssert(t, "TestMd5String")
|
||||||
smd5 := Md5String(s)
|
assert.Equal("5d41402abc4b2a76b9719d911017c592", Md5String("hello"))
|
||||||
expected := "5d41402abc4b2a76b9719d911017c592"
|
|
||||||
|
|
||||||
if smd5 != expected {
|
|
||||||
internal.LogFailedTestInfo(t, "Md5String", s, expected, smd5)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMd5File(t *testing.T) {
|
func TestMd5File(t *testing.T) {
|
||||||
file, _ := os.Create("./hello.txt")
|
fileMd5, err := Md5File("./basic.go")
|
||||||
defer file.Close()
|
assert := internal.NewAssert(t, "TestMd5File")
|
||||||
file.WriteString("hello\n")
|
assert.IsNotNil(fileMd5)
|
||||||
|
assert.IsNil(err)
|
||||||
fileMd5, err := Md5File("./hello.txt")
|
|
||||||
if err != nil {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
fmt.Println(fileMd5)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHmacMd5(t *testing.T) {
|
func TestHmacMd5(t *testing.T) {
|
||||||
s := "hello world"
|
assert := internal.NewAssert(t, "TestHmacMd5")
|
||||||
key := "12345"
|
assert.Equal("5f4c9faaff0a1ad3007d9ddc06abe36d", HmacMd5("hello world", "12345"))
|
||||||
hmacMd5 := HmacMd5(s, key)
|
|
||||||
expected := "5f4c9faaff0a1ad3007d9ddc06abe36d"
|
|
||||||
|
|
||||||
if hmacMd5 != expected {
|
|
||||||
internal.LogFailedTestInfo(t, "HmacMd5", s, expected, hmacMd5)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHmacSha1(t *testing.T) {
|
func TestHmacSha1(t *testing.T) {
|
||||||
@@ -69,10 +39,8 @@ func TestHmacSha1(t *testing.T) {
|
|||||||
hmacSha1 := HmacSha1(s, key)
|
hmacSha1 := HmacSha1(s, key)
|
||||||
expected := "3826f812255d8683f051ee97346d1359234d5dbd"
|
expected := "3826f812255d8683f051ee97346d1359234d5dbd"
|
||||||
|
|
||||||
if hmacSha1 != expected {
|
assert := internal.NewAssert(t, "TestHmacSha1")
|
||||||
internal.LogFailedTestInfo(t, "HmacSha1", s, expected, hmacSha1)
|
assert.Equal(expected, hmacSha1)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHmacSha256(t *testing.T) {
|
func TestHmacSha256(t *testing.T) {
|
||||||
@@ -81,10 +49,8 @@ func TestHmacSha256(t *testing.T) {
|
|||||||
hmacSha256 := HmacSha256(s, key)
|
hmacSha256 := HmacSha256(s, key)
|
||||||
expected := "9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8"
|
expected := "9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8"
|
||||||
|
|
||||||
if hmacSha256 != expected {
|
assert := internal.NewAssert(t, "TestHmacSha256")
|
||||||
internal.LogFailedTestInfo(t, "HmacSha256", s, expected, hmacSha256)
|
assert.Equal(expected, hmacSha256)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHmacSha512(t *testing.T) {
|
func TestHmacSha512(t *testing.T) {
|
||||||
@@ -93,10 +59,8 @@ func TestHmacSha512(t *testing.T) {
|
|||||||
hmacSha512 := HmacSha512(s, key)
|
hmacSha512 := HmacSha512(s, key)
|
||||||
expected := "5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175"
|
expected := "5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175"
|
||||||
|
|
||||||
if hmacSha512 != expected {
|
assert := internal.NewAssert(t, "TestHmacSha512")
|
||||||
internal.LogFailedTestInfo(t, "HmacSha512", s, expected, hmacSha512)
|
assert.Equal(expected, hmacSha512)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSha1(t *testing.T) {
|
func TestSha1(t *testing.T) {
|
||||||
@@ -104,10 +68,8 @@ func TestSha1(t *testing.T) {
|
|||||||
sha1 := Sha1(s)
|
sha1 := Sha1(s)
|
||||||
expected := "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
|
expected := "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
|
||||||
|
|
||||||
if sha1 != expected {
|
assert := internal.NewAssert(t, "TestSha1")
|
||||||
internal.LogFailedTestInfo(t, "Sha1", s, expected, sha1)
|
assert.Equal(expected, sha1)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSha256(t *testing.T) {
|
func TestSha256(t *testing.T) {
|
||||||
@@ -115,10 +77,8 @@ func TestSha256(t *testing.T) {
|
|||||||
sha256 := Sha256(s)
|
sha256 := Sha256(s)
|
||||||
expected := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
|
expected := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
|
||||||
|
|
||||||
if sha256 != expected {
|
assert := internal.NewAssert(t, "TestSha256")
|
||||||
internal.LogFailedTestInfo(t, "Sha256", s, expected, sha256)
|
assert.Equal(expected, sha256)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSha512(t *testing.T) {
|
func TestSha512(t *testing.T) {
|
||||||
@@ -126,8 +86,6 @@ func TestSha512(t *testing.T) {
|
|||||||
sha512 := Sha512(s)
|
sha512 := Sha512(s)
|
||||||
expected := "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"
|
expected := "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"
|
||||||
|
|
||||||
if sha512 != expected {
|
assert := internal.NewAssert(t, "TestSha512")
|
||||||
internal.LogFailedTestInfo(t, "Sha512", s, expected, sha512)
|
assert.Equal(expected, sha512)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,8 @@ func TestDesEcbEncrypt(t *testing.T) {
|
|||||||
desEcbEncrypt := DesEcbEncrypt([]byte(data), []byte(key))
|
desEcbEncrypt := DesEcbEncrypt([]byte(data), []byte(key))
|
||||||
desEcbDecrypt := DesEcbDecrypt(desEcbEncrypt, []byte(key))
|
desEcbDecrypt := DesEcbDecrypt(desEcbEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(desEcbDecrypt) != data {
|
assert := internal.NewAssert(t, "TestDesEcbEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "DesEcbEncrypt/DesEcbDecrypt", data, data, string(desEcbDecrypt))
|
assert.Equal(data, string(desEcbDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDesCbcEncrypt(t *testing.T) {
|
func TestDesCbcEncrypt(t *testing.T) {
|
||||||
@@ -26,10 +24,8 @@ func TestDesCbcEncrypt(t *testing.T) {
|
|||||||
desCbcEncrypt := DesCbcEncrypt([]byte(data), []byte(key))
|
desCbcEncrypt := DesCbcEncrypt([]byte(data), []byte(key))
|
||||||
desCbcDecrypt := DesCbcDecrypt(desCbcEncrypt, []byte(key))
|
desCbcDecrypt := DesCbcDecrypt(desCbcEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(desCbcDecrypt) != data {
|
assert := internal.NewAssert(t, "TestDesCbcEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "DesCbcEncrypt/DesCbcDecrypt", data, data, string(desCbcDecrypt))
|
assert.Equal(data, string(desCbcDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDesCtrCrypt(t *testing.T) {
|
func TestDesCtrCrypt(t *testing.T) {
|
||||||
@@ -39,10 +35,8 @@ func TestDesCtrCrypt(t *testing.T) {
|
|||||||
desCtrCrypt := DesCtrCrypt([]byte(data), []byte(key))
|
desCtrCrypt := DesCtrCrypt([]byte(data), []byte(key))
|
||||||
desCtrDeCrypt := DesCtrCrypt(desCtrCrypt, []byte(key))
|
desCtrDeCrypt := DesCtrCrypt(desCtrCrypt, []byte(key))
|
||||||
|
|
||||||
if string(desCtrDeCrypt) != data {
|
assert := internal.NewAssert(t, "TestDesCtrCrypt")
|
||||||
internal.LogFailedTestInfo(t, "DesCtrCrypt", data, data, string(desCtrDeCrypt))
|
assert.Equal(data, string(desCtrDeCrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDesCfbEncrypt(t *testing.T) {
|
func TestDesCfbEncrypt(t *testing.T) {
|
||||||
@@ -52,10 +46,8 @@ func TestDesCfbEncrypt(t *testing.T) {
|
|||||||
desCfbEncrypt := DesCfbEncrypt([]byte(data), []byte(key))
|
desCfbEncrypt := DesCfbEncrypt([]byte(data), []byte(key))
|
||||||
desCfbDecrypt := DesCfbDecrypt(desCfbEncrypt, []byte(key))
|
desCfbDecrypt := DesCfbDecrypt(desCfbEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(desCfbDecrypt) != data {
|
assert := internal.NewAssert(t, "TestDesCfbEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "DesCfbEncrypt/DesCfbDecrypt", data, data, string(desCfbDecrypt))
|
assert.Equal(data, string(desCfbDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDesOfbEncrypt(t *testing.T) {
|
func TestDesOfbEncrypt(t *testing.T) {
|
||||||
@@ -65,8 +57,6 @@ func TestDesOfbEncrypt(t *testing.T) {
|
|||||||
desOfbEncrypt := DesOfbEncrypt([]byte(data), []byte(key))
|
desOfbEncrypt := DesOfbEncrypt([]byte(data), []byte(key))
|
||||||
desOfbDecrypt := DesOfbDecrypt(desOfbEncrypt, []byte(key))
|
desOfbDecrypt := DesOfbDecrypt(desOfbEncrypt, []byte(key))
|
||||||
|
|
||||||
if string(desOfbDecrypt) != data {
|
assert := internal.NewAssert(t, "TestDesOfbEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "DesOfbEncrypt/DesOfbDecrypt", data, data, string(desOfbDecrypt))
|
assert.Equal(data, string(desOfbDecrypt))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ func TestRsaEncrypt(t *testing.T) {
|
|||||||
encrypted := RsaEncrypt(data, "rsa_public.pem")
|
encrypted := RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
|
|
||||||
if string(data) != string(decrypted) {
|
assert := internal.NewAssert(t, "TestRsaEncrypt")
|
||||||
internal.LogFailedTestInfo(t, "RsaEncrypt/RsaDecrypt", string(data), string(data), string(decrypted))
|
assert.Equal(string(data), string(decrypted))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user