github.com/wangyougui/gf/v2@v2.6.5/crypto/gsha1/gsha1_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/wangyougui/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package gsha1_test
    10  
    11  import (
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/wangyougui/gf/v2/crypto/gsha1"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  )
    18  
    19  type user struct {
    20  	name     string
    21  	password string
    22  	age      int
    23  }
    24  
    25  func TestEncrypt(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		user := &user{
    28  			name:     "派大星",
    29  			password: "123456",
    30  			age:      23,
    31  		}
    32  		result := "97386736e3ee4adee5ca595c78c12129f6032cad"
    33  		encrypt := gsha1.Encrypt(user)
    34  		t.AssertEQ(encrypt, result)
    35  	})
    36  	gtest.C(t, func(t *gtest.T) {
    37  		result := "5b4c1c2a08ca85ddd031ef8627414f4cb2620b41"
    38  		s := gsha1.Encrypt("pibigstar")
    39  		t.AssertEQ(s, result)
    40  	})
    41  }
    42  
    43  func TestEncryptFile(t *testing.T) {
    44  	path := "test.text"
    45  	errPath := "err.text"
    46  	gtest.C(t, func(t *gtest.T) {
    47  		result := "8b05d3ba24b8d2374b8f5149d9f3fbada14ea984"
    48  		file, err := os.Create(path)
    49  		defer os.Remove(path)
    50  		defer file.Close()
    51  		t.AssertNil(err)
    52  		_, _ = file.Write([]byte("Hello Go Frame"))
    53  		encryptFile, _ := gsha1.EncryptFile(path)
    54  		t.AssertEQ(encryptFile, result)
    55  		// when the file is not exist,encrypt will return empty string
    56  		errEncrypt, _ := gsha1.EncryptFile(errPath)
    57  		t.AssertEQ(errEncrypt, "")
    58  	})
    59  }