diff --git a/g/crypto/gdes/gdes_test.go b/g/crypto/gdes/gdes_test.go index 075e7d07b..c19153ed1 100644 --- a/g/crypto/gdes/gdes_test.go +++ b/g/crypto/gdes/gdes_test.go @@ -1,6 +1,7 @@ package gdes_test import ( + "github.com/gogf/gf/g/test/gtest" "testing" "bytes" "encoding/hex" @@ -8,8 +9,14 @@ import ( "github.com/gogf/gf/g/crypto/gdes" ) +var( + errKey = []byte("1111111111111234123456789") + errIv = []byte("12345678") + errPadding = 5 +) + func TestDesECB(t *testing.T){ - { + gtest.Case(t, func() { key := []byte("11111111") text := []byte("12345678") padding := gdes.NOPADDING @@ -17,7 +24,7 @@ func TestDesECB(t *testing.T){ if err != nil { t.Errorf("%v", err) } - + clearText, err := gdes.DesECBDecrypt(key, cipherText, padding) if err != nil { t.Errorf("%v", err) @@ -28,12 +35,16 @@ func TestDesECB(t *testing.T){ } fmt.Println("clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) - } + // err test + gdes.DesECBEncrypt(key, text, errPadding) + gdes.DesECBDecrypt(errKey, cipherText, padding) + }) - { + gtest.Case(t, func() { key := []byte("11111111") text := []byte("12345678") padding := gdes.PKCS5PADDING + errPadding := 5 cipherText, err := gdes.DesECBEncrypt(key, text, padding) if err != nil { t.Errorf("%v", err) @@ -48,7 +59,11 @@ func TestDesECB(t *testing.T){ t.Errorf("text:%v, clearText:%v", hex.EncodeToString(text), hex.EncodeToString(clearText)) } fmt.Println("clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) - } + + // err test + gdes.DesECBEncrypt(key, text, errPadding) + gdes.DesECBDecrypt(errKey, cipherText, padding) + }) } func Test3DesECB(t *testing.T){ @@ -70,13 +85,15 @@ func Test3DesECB(t *testing.T){ t.Errorf("text:%v, clearText:%v", hex.EncodeToString(text), hex.EncodeToString(clearText)) } fmt.Println("key:", hex.EncodeToString(key),"clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) - + // err test + gdes.DesECBEncrypt(key, text, errPadding) } - { + gtest.Case(t, func() { key := []byte("111111111111123412345678") text := []byte("123456789") padding := gdes.PKCS5PADDING + errPadding := 5 cipherText, err := gdes.TripleDesECBEncrypt(key, text, padding) if err != nil { t.Errorf("%v", err) @@ -91,11 +108,16 @@ func Test3DesECB(t *testing.T){ t.Errorf("text:%v, clearText:%v", hex.EncodeToString(text), hex.EncodeToString(clearText)) } fmt.Println("key:", hex.EncodeToString(key),"clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) - } + // err test + gdes.TripleDesECBEncrypt(errKey, text, padding) + gdes.TripleDesECBEncrypt(key, text, errPadding) + + gdes.TripleDesECBDecrypt(errKey, text, padding) + }) } func TestDesCBC(t *testing.T){ - { + gtest.Case(t, func() { key := []byte("11111111") text := []byte("1234567812345678") padding := gdes.NOPADDING @@ -114,10 +136,12 @@ func TestDesCBC(t *testing.T){ t.Errorf("text:%v, clearText:%v", hex.EncodeToString(text), hex.EncodeToString(clearText)) } fmt.Println("key:", hex.EncodeToString(key),"clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) + // err test + gdes.DesCBCEncrypt(errKey, text, iv,padding) + gdes.DesCBCEncrypt(errKey, text, iv,errPadding) + }) - } - - { + gtest.Case(t, func() { key := []byte("11111111") text := []byte("12345678") padding := gdes.PKCS5PADDING @@ -136,11 +160,15 @@ func TestDesCBC(t *testing.T){ t.Errorf("text:%v, clearText:%v", hex.EncodeToString(text), hex.EncodeToString(clearText)) } fmt.Println("key:", hex.EncodeToString(key),"clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) - } + + // err test + gdes.DesCBCEncrypt(key, text, errIv, padding) + gdes.DesCBCEncrypt(key, text, errIv, padding) + }) } func Test3DesCBC(t *testing.T){ - { + gtest.Case(t, func() { key := []byte("1111111112345678") text := []byte("1234567812345678") padding := gdes.NOPADDING @@ -160,9 +188,9 @@ func Test3DesCBC(t *testing.T){ } fmt.Println("key:", hex.EncodeToString(key),"clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) - } - - { + gdes.TripleDesCBCEncrypt(errKey, text, iv,padding) + }) + gtest.Case(t, func() { key := []byte("111111111234567812345678") text := []byte("12345678") padding := gdes.PKCS5PADDING @@ -181,5 +209,6 @@ func Test3DesCBC(t *testing.T){ t.Errorf("text:%v, clearText:%v", hex.EncodeToString(text), hex.EncodeToString(clearText)) } fmt.Println("key:", hex.EncodeToString(key),"clearText:", hex.EncodeToString(clearText), "cipherText:", hex.EncodeToString(cipherText)) - } + }) + } \ No newline at end of file diff --git a/g/crypto/gmd5/gmd5_test.go b/g/crypto/gmd5/gmd5_test.go index 7d40626f3..ebc43a498 100644 --- a/g/crypto/gmd5/gmd5_test.go +++ b/g/crypto/gmd5/gmd5_test.go @@ -57,6 +57,7 @@ func TestEncryptString(t *testing.T) { func TestEncryptFile(t *testing.T) { path := "test.text" + errorPath := "err.txt" gtest.Case(t, func() { file, err := os.Create(path) gtest.Assert(err, nil) @@ -64,6 +65,10 @@ func TestEncryptFile(t *testing.T) { file.Write([]byte("Hello Go Frame")) encryptFile := gmd5.EncryptFile(path) gtest.AssertNE(encryptFile, "") + + errEncrypt := gmd5.EncryptFile(errorPath) + gtest.AssertEQ(errEncrypt, "") }) + defer os.Remove(path) } diff --git a/g/crypto/gsha1/gsha1_test.go b/g/crypto/gsha1/gsha1_test.go index bff462482..3593b09a9 100644 --- a/g/crypto/gsha1/gsha1_test.go +++ b/g/crypto/gsha1/gsha1_test.go @@ -47,6 +47,7 @@ func TestEncryptString(t *testing.T) { func TestEncryptFile(t *testing.T) { path := "test.text" + errPath := "err.text" gtest.Case(t, func() { file, err := os.Create(path) gtest.Assert(err, nil) @@ -54,6 +55,8 @@ func TestEncryptFile(t *testing.T) { file.Write([]byte("Hello Go Frame")) encryptFile := gsha1.EncryptFile(path) gtest.AssertNE(encryptFile, "") + errEncrypt := gsha1.EncryptFile(errPath) + gtest.AssertEQ(errEncrypt,"") }) defer os.Remove(path) }