github.com/zhongdalu/gf@v1.0.0/g/crypto/gsha1/gsha1_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 gsha1_test
    10  
    11  import (
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/zhongdalu/gf/g/crypto/gsha1"
    16  	"github.com/zhongdalu/gf/g/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.Case(t, func() {
    27  		user := &user{
    28  			name:     "派大星",
    29  			password: "123456",
    30  			age:      23,
    31  		}
    32  		result := "97386736e3ee4adee5ca595c78c12129f6032cad"
    33  		encrypt := gsha1.Encrypt(user)
    34  		gtest.AssertEQ(encrypt, result)
    35  	})
    36  	gtest.Case(t, func() {
    37  		result := "5b4c1c2a08ca85ddd031ef8627414f4cb2620b41"
    38  		s := gsha1.Encrypt("pibigstar")
    39  		gtest.AssertEQ(s, result)
    40  	})
    41  }
    42  
    43  func TestEncryptString(t *testing.T) {
    44  	gtest.Case(t, func() {
    45  		result := "5b4c1c2a08ca85ddd031ef8627414f4cb2620b41"
    46  		s := gsha1.EncryptString("pibigstar")
    47  		gtest.AssertEQ(s, result)
    48  	})
    49  }
    50  
    51  func TestEncryptFile(t *testing.T) {
    52  	path := "test.text"
    53  	errPath := "err.text"
    54  	gtest.Case(t, func() {
    55  		result := "8b05d3ba24b8d2374b8f5149d9f3fbada14ea984"
    56  		file, err := os.Create(path)
    57  		defer os.Remove(path)
    58  		defer file.Close()
    59  		gtest.Assert(err, nil)
    60  		_, _ = file.Write([]byte("Hello Go Frame"))
    61  		encryptFile, _ := gsha1.EncryptFile(path)
    62  		gtest.AssertEQ(encryptFile, result)
    63  		// when the file is not exist,encrypt will return empty string
    64  		errEncrypt, _ := gsha1.EncryptFile(errPath)
    65  		gtest.AssertEQ(errEncrypt, "")
    66  	})
    67  }