github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/cache/hash_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cache
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  func TestHash(t *testing.T) {
    15  	oldSalt := hashSalt
    16  	hashSalt = nil
    17  	defer func() {
    18  		hashSalt = oldSalt
    19  	}()
    20  
    21  	h := NewHash("alice")
    22  	h.Write([]byte("hello world"))
    23  	sum := fmt.Sprintf("%x", h.Sum())
    24  	want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
    25  	if sum != want {
    26  		t.Errorf("hash(hello world) = %v, want %v", sum, want)
    27  	}
    28  }
    29  
    30  func TestHashFile(t *testing.T) {
    31  	f, err := ioutil.TempFile("", "cmd-go-test-")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	name := f.Name()
    36  	fmt.Fprintf(f, "hello world")
    37  	defer os.Remove(name)
    38  	if err := f.Close(); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	var h ActionID // make sure hash result is assignable to ActionID
    43  	h, err = FileHash(name)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	sum := fmt.Sprintf("%x", h)
    48  	want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
    49  	if sum != want {
    50  		t.Errorf("hash(hello world) = %v, want %v", sum, want)
    51  	}
    52  }