2019-04-05 17:11:03 +08:00
|
|
|
// 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"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/crypto/gsha1"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-04-05 17:11:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type user struct {
|
|
|
|
name string
|
|
|
|
password string
|
|
|
|
age int
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncrypt(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-05 17:11:03 +08:00
|
|
|
user := &user{
|
|
|
|
name: "派大星",
|
|
|
|
password: "123456",
|
|
|
|
age: 23,
|
|
|
|
}
|
2019-04-12 23:29:31 +08:00
|
|
|
result := "97386736e3ee4adee5ca595c78c12129f6032cad"
|
2019-04-05 17:11:03 +08:00
|
|
|
encrypt := gsha1.Encrypt(user)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(encrypt, result)
|
2019-04-05 17:11:03 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-12 23:29:31 +08:00
|
|
|
result := "5b4c1c2a08ca85ddd031ef8627414f4cb2620b41"
|
2019-04-06 12:18:51 +08:00
|
|
|
s := gsha1.Encrypt("pibigstar")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(s, result)
|
2019-04-06 12:18:51 +08:00
|
|
|
})
|
2019-04-05 17:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncryptFile(t *testing.T) {
|
|
|
|
path := "test.text"
|
2019-04-06 15:06:42 +08:00
|
|
|
errPath := "err.text"
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-12 23:29:31 +08:00
|
|
|
result := "8b05d3ba24b8d2374b8f5149d9f3fbada14ea984"
|
2019-04-05 17:11:03 +08:00
|
|
|
file, err := os.Create(path)
|
2019-04-08 11:53:29 +08:00
|
|
|
defer os.Remove(path)
|
2019-04-05 17:11:03 +08:00
|
|
|
defer file.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-06-21 22:23:07 +08:00
|
|
|
_, _ = file.Write([]byte("Hello Go Frame"))
|
|
|
|
encryptFile, _ := gsha1.EncryptFile(path)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(encryptFile, result)
|
2019-04-12 23:29:31 +08:00
|
|
|
// when the file is not exist,encrypt will return empty string
|
2019-06-21 22:23:07 +08:00
|
|
|
errEncrypt, _ := gsha1.EncryptFile(errPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertEQ(errEncrypt, "")
|
2019-04-05 17:11:03 +08:00
|
|
|
})
|
|
|
|
}
|