github.com/zhongdalu/gf@v1.0.0/g/crypto/gmd5/gmd5_test.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 // go test *.go -bench=".*" 8 9 package gmd5_test 10 11 import ( 12 "os" 13 "testing" 14 15 "github.com/zhongdalu/gf/g/crypto/gmd5" 16 "github.com/zhongdalu/gf/g/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.Case(t, func() { 33 encryptString, _ := gmd5.Encrypt(s) 34 gtest.Assert(encryptString, result) 35 36 result := "1427562bb29f88a1161590b76398ab72" 37 encrypt, _ := gmd5.Encrypt(123456) 38 gtest.AssertEQ(encrypt, result) 39 }) 40 41 gtest.Case(t, func() { 42 user := &user{ 43 name: "派大星", 44 password: "123456", 45 age: 23, 46 } 47 result := "70917ebce8bd2f78c736cda63870fb39" 48 encrypt, _ := gmd5.Encrypt(user) 49 gtest.AssertEQ(encrypt, result) 50 }) 51 } 52 53 func TestEncryptString(t *testing.T) { 54 gtest.Case(t, func() { 55 encryptString, _ := gmd5.EncryptString(s) 56 gtest.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.Case(t, func() { 65 file, err := os.Create(path) 66 defer os.Remove(path) 67 defer file.Close() 68 gtest.Assert(err, nil) 69 _, _ = file.Write([]byte("Hello Go Frame")) 70 encryptFile, _ := gmd5.EncryptFile(path) 71 gtest.AssertEQ(encryptFile, result) 72 // when the file is not exist,encrypt will return empty string 73 errEncrypt, _ := gmd5.EncryptFile(errorPath) 74 gtest.AssertEQ(errEncrypt, "") 75 }) 76 77 }