github.com/gogf/gf/v2@v2.7.4/crypto/gmd5/gmd5_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" 8 9 package gmd5_test 10 11 import ( 12 "os" 13 "testing" 14 15 "github.com/gogf/gf/v2/crypto/gmd5" 16 "github.com/gogf/gf/v2/test/gtest" 17 ) 18 19 var ( 20 s = "pibigstar" 21 // online generated MD5 value 22 result = "d175a1ff66aedde64344785f7f7a3df8" 23 ) 24 25 type user struct { 26 name string 27 password string 28 age int 29 } 30 31 func TestEncrypt(t *testing.T) { 32 gtest.C(t, func(t *gtest.T) { 33 encryptString, _ := gmd5.Encrypt(s) 34 t.Assert(encryptString, result) 35 36 result := "1427562bb29f88a1161590b76398ab72" 37 encrypt, _ := gmd5.Encrypt(123456) 38 t.AssertEQ(encrypt, result) 39 }) 40 41 gtest.C(t, func(t *gtest.T) { 42 user := &user{ 43 name: "派大星", 44 password: "123456", 45 age: 23, 46 } 47 result := "70917ebce8bd2f78c736cda63870fb39" 48 encrypt, _ := gmd5.Encrypt(user) 49 t.AssertEQ(encrypt, result) 50 }) 51 } 52 53 func TestEncryptString(t *testing.T) { 54 gtest.C(t, func(t *gtest.T) { 55 encryptString, _ := gmd5.EncryptString(s) 56 t.Assert(encryptString, result) 57 }) 58 } 59 60 func TestEncryptFile(t *testing.T) { 61 path := "test.text" 62 errorPath := "err.txt" 63 result := "e6e6e1cd41895beebff16d5452dfce12" 64 gtest.C(t, func(t *gtest.T) { 65 file, err := os.Create(path) 66 defer os.Remove(path) 67 defer file.Close() 68 t.AssertNil(err) 69 _, _ = file.Write([]byte("Hello Go Frame")) 70 encryptFile, _ := gmd5.EncryptFile(path) 71 t.AssertEQ(encryptFile, result) 72 // when the file is not exist,encrypt will return empty string 73 errEncrypt, _ := gmd5.EncryptFile(errorPath) 74 t.AssertEQ(errEncrypt, "") 75 }) 76 77 }