gf/crypto/gmd5/gmd5_test.go

78 lines
1.7 KiB
Go
Raw Normal View History

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 gmd5_test
import (
"os"
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/crypto/gmd5"
"github.com/gogf/gf/test/gtest"
2019-04-05 17:11:03 +08:00
)
var (
s = "pibigstar"
// online generated MD5 value
2019-04-05 17:11:03 +08:00
result = "d175a1ff66aedde64344785f7f7a3df8"
)
2019-04-06 12:18:51 +08:00
type user struct {
name string
password string
age int
}
2019-04-05 17:11:03 +08:00
func TestEncrypt(t *testing.T) {
gtest.Case(t, func() {
2019-06-21 22:23:07 +08:00
encryptString, _ := gmd5.Encrypt(s)
2019-04-05 17:11:03 +08:00
gtest.Assert(encryptString, result)
2019-04-06 12:18:51 +08:00
result := "1427562bb29f88a1161590b76398ab72"
2019-06-21 22:23:07 +08:00
encrypt, _ := gmd5.Encrypt(123456)
2019-06-19 09:06:52 +08:00
gtest.AssertEQ(encrypt, result)
2019-04-06 12:18:51 +08:00
})
gtest.Case(t, func() {
user := &user{
name: "派大星",
password: "123456",
age: 23,
}
result := "70917ebce8bd2f78c736cda63870fb39"
2019-06-21 22:23:07 +08:00
encrypt, _ := gmd5.Encrypt(user)
2019-06-19 09:06:52 +08:00
gtest.AssertEQ(encrypt, result)
2019-04-05 17:11:03 +08:00
})
}
func TestEncryptString(t *testing.T) {
gtest.Case(t, func() {
2019-06-21 22:23:07 +08:00
encryptString, _ := gmd5.EncryptString(s)
2019-04-05 17:11:03 +08:00
gtest.Assert(encryptString, result)
})
}
func TestEncryptFile(t *testing.T) {
path := "test.text"
2019-04-06 15:06:42 +08:00
errorPath := "err.txt"
result := "e6e6e1cd41895beebff16d5452dfce12"
2019-04-05 17:11:03 +08:00
gtest.Case(t, func() {
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()
2019-04-08 11:53:29 +08:00
gtest.Assert(err, nil)
2019-06-21 22:23:07 +08:00
_, _ = file.Write([]byte("Hello Go Frame"))
encryptFile, _ := gmd5.EncryptFile(path)
gtest.AssertEQ(encryptFile, result)
// when the file is not exist,encrypt will return empty string
2019-06-21 22:23:07 +08:00
errEncrypt, _ := gmd5.EncryptFile(errorPath)
2019-04-06 15:06:42 +08:00
gtest.AssertEQ(errEncrypt, "")
2019-04-05 17:11:03 +08:00
})
2019-04-06 15:06:42 +08:00
2019-04-05 17:11:03 +08:00
}