go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/caching/cache/hash_test.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cache
    16  
    17  import (
    18  	"bytes"
    19  	"crypto"
    20  	"reflect"
    21  	"sort"
    22  	"testing"
    23  )
    24  
    25  func TestHexDigestValid(t *testing.T) {
    26  	data := []struct {
    27  		v HexDigest
    28  		h crypto.Hash
    29  	}{
    30  		{"0123456789012345678901234567890123456789", crypto.SHA1},
    31  		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", crypto.SHA1},
    32  		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", crypto.SHA256},
    33  		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", crypto.SHA512},
    34  	}
    35  	for i, line := range data {
    36  		if !line.v.Validate(line.h) {
    37  			t.Fatalf("#%d: expected success: %v", i, line)
    38  		}
    39  	}
    40  }
    41  
    42  func TestHexDigestInvalid(t *testing.T) {
    43  	data := []struct {
    44  		v HexDigest
    45  		h crypto.Hash
    46  	}{
    47  		{"012345678901234567890123456789012345678X", crypto.SHA1},
    48  		{"012345678901234567890123456789012345678", crypto.SHA1},
    49  		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", crypto.SHA1},
    50  		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", crypto.SHA256},
    51  		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", crypto.SHA512},
    52  	}
    53  	for i, line := range data {
    54  		if line.v.Validate(line.h) {
    55  			t.Fatalf("#%d: expected failure: %v", i, line)
    56  		}
    57  	}
    58  }
    59  
    60  func TestHexDigests(t *testing.T) {
    61  	v := HexDigests{"a", "c", "b", "d"}
    62  	sort.Sort(v)
    63  	if !reflect.DeepEqual(v, HexDigests{"a", "b", "c", "d"}) {
    64  		t.Fatal("sort failed")
    65  	}
    66  }
    67  
    68  func TestSum(t *testing.T) {
    69  	h := crypto.SHA1.New()
    70  	if _, err := h.Write([]byte("foo")); err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	expected := HexDigest("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
    74  	if s := Sum(h); s != expected {
    75  		t.Fatalf("%s != %s", s, expected)
    76  	}
    77  }
    78  
    79  func TestHash(t *testing.T) {
    80  	b := bytes.NewBufferString("foo")
    81  	s, err := Hash(crypto.SHA1, b)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	expected := HexDigest("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
    86  	if s != expected {
    87  		t.Fatalf("%s != %s", s, expected)
    88  	}
    89  }
    90  
    91  func TestHashBytes(t *testing.T) {
    92  	s := HashBytes(crypto.SHA1, []byte("foo"))
    93  	expected := HexDigest("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
    94  	if s != expected {
    95  		t.Fatalf("%s != %s", s, expected)
    96  	}
    97  }