diff --git a/g/crypto/gaes/gaes_test.go b/g/crypto/gaes/gaes_test.go new file mode 100644 index 000000000..43f2b04c1 --- /dev/null +++ b/g/crypto/gaes/gaes_test.go @@ -0,0 +1,62 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" + +package gaes_test + +import ( + "testing" + + "github.com/gogf/gf/g/crypto/gaes" + "github.com/gogf/gf/g/test/gtest" +) + +var ( + content = []byte("pibigstar") + // iv 长度必须等于blockSize,只能为16 + iv = []byte("Hello My GoFrame") + key_16 = []byte("1234567891234567") + key_24 = []byte("123456789123456789123456") + key_32 = []byte("12345678912345678912345678912345") +) + +func TestEncrypt(t *testing.T) { + gtest.Case(t, func() { + _, err := gaes.Encrypt(content, key_16) + gtest.Assert(err, nil) + _, err = gaes.Encrypt(content, key_24) + gtest.Assert(err, nil) + _, err = gaes.Encrypt(content, key_32) + gtest.Assert(err, nil) + _, err = gaes.Encrypt(content, key_16, iv) + gtest.Assert(err, nil) + }) +} + +func TestDecrypt(t *testing.T) { + gtest.Case(t, func() { + encrypt, err := gaes.Encrypt(content, key_16) + decrypt, err := gaes.Decrypt(encrypt, key_16) + gtest.Assert(err, nil) + gtest.Assert(string(decrypt), string(content)) + + encrypt, err = gaes.Encrypt(content, key_24) + decrypt, err = gaes.Decrypt(encrypt, key_24) + gtest.Assert(err, nil) + gtest.Assert(string(decrypt), string(content)) + + encrypt, err = gaes.Encrypt(content, key_32) + decrypt, err = gaes.Decrypt(encrypt, key_32) + gtest.Assert(err, nil) + gtest.Assert(string(decrypt), string(content)) + + encrypt, err = gaes.Encrypt(content, key_32, iv) + decrypt, err = gaes.Decrypt(encrypt, key_32, iv) + gtest.Assert(err, nil) + gtest.Assert(string(decrypt), string(content)) + }) +} diff --git a/g/crypto/gcrc32/gcr32_test.go b/g/crypto/gcrc32/gcr32_test.go new file mode 100644 index 000000000..73219d531 --- /dev/null +++ b/g/crypto/gcrc32/gcr32_test.go @@ -0,0 +1,25 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" + +package gcrc32_test + +import ( + "testing" + + "github.com/gogf/gf/g/crypto/gcrc32" + "github.com/gogf/gf/g/test/gtest" +) + +func TestEncrypt(t *testing.T) { + gtest.Case(t, func() { + s := "pibigstar" + encrypt1 := gcrc32.EncryptString(s) + encrypt2 := gcrc32.EncryptBytes([]byte(s)) + gtest.AssertEQ(encrypt1, encrypt2) + }) +} diff --git a/g/crypto/gmd5/gmd5_test.go b/g/crypto/gmd5/gmd5_test.go new file mode 100644 index 000000000..eaff5a2b1 --- /dev/null +++ b/g/crypto/gmd5/gmd5_test.go @@ -0,0 +1,50 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" + +package gmd5_test + +import ( + "os" + "testing" + + "github.com/gogf/gf/g/crypto/gmd5" + "github.com/gogf/gf/g/test/gtest" +) + +var ( + s = "pibigstar" + // 根据在线工具生成的md5值 + result = "d175a1ff66aedde64344785f7f7a3df8" +) + +func TestEncrypt(t *testing.T) { + gtest.Case(t, func() { + encryptString := gmd5.Encrypt(s) + gtest.Assert(encryptString, result) + }) +} + +func TestEncryptString(t *testing.T) { + gtest.Case(t, func() { + encryptString := gmd5.EncryptString(s) + gtest.Assert(encryptString, result) + }) +} + +func TestEncryptFile(t *testing.T) { + path := "test.text" + gtest.Case(t, func() { + file, err := os.Create(path) + gtest.Assert(err, nil) + defer file.Close() + file.Write([]byte("Hello Go Frame")) + encryptFile := gmd5.EncryptFile(path) + gtest.AssertNE(encryptFile, "") + }) + os.Remove(path) +} diff --git a/g/crypto/gsha1/gsha1_test.go b/g/crypto/gsha1/gsha1_test.go new file mode 100644 index 000000000..d53ae5265 --- /dev/null +++ b/g/crypto/gsha1/gsha1_test.go @@ -0,0 +1,55 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" + +package gsha1_test + +import ( + "os" + "testing" + + "github.com/gogf/gf/g/crypto/gsha1" + "github.com/gogf/gf/g/test/gtest" +) + +type user struct { + name string + password string + age int +} + +func TestEncrypt(t *testing.T) { + gtest.Case(t, func() { + user := &user{ + name: "派大星", + password: "123456", + age: 23, + } + encrypt := gsha1.Encrypt(user) + gtest.AssertNE(encrypt, "") + }) +} + +func TestEncryptString(t *testing.T) { + gtest.Case(t, func() { + s := gsha1.EncryptString("pibigstar") + gtest.AssertNE(s, "") + }) +} + +func TestEncryptFile(t *testing.T) { + path := "test.text" + gtest.Case(t, func() { + file, err := os.Create(path) + gtest.Assert(err, nil) + defer file.Close() + file.Write([]byte("Hello Go Frame")) + encryptFile := gsha1.EncryptFile(path) + gtest.AssertNE(encryptFile, "") + }) + os.Remove(path) +}