featurebase/hash/blake3_test.go
Antonio Navarro Perez ca340f7e62 [hash] Remove duplicated blake3 code
blake3 code is used in several places on the code. The file was
duplicated on root and rbf package.

To avoid cyclic dependencies, I moved it to hash package. Some methods
must be public to use them in different places.

HashOfDir method was removed. Not used.

Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
2020-09-07 12:24:22 +02:00

90 lines
2.2 KiB
Go

// Copyright 2020 Pilosa Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hash
import (
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path"
"testing"
"github.com/pilosa/pilosa/v2/testhook"
)
func TestBlake3Hasher(t *testing.T) {
hasher := NewBlake3Hasher()
hash := make([]byte, 16)
input := []byte("hello world")
hash = hasher.CryptoHash(input, hash)
expected := "d74981efa70a0c880b8d8c1985d075db"
observed := hex.EncodeToString(hash)
if observed != expected {
panic(fmt.Sprintf("expected hash:'%v' but observed hash '%v'", expected, observed))
}
obs2 := Blake3sum16(input)
if obs2 != expected {
panic(fmt.Sprintf("expected hash:'%v' but observed hash from blake2sum16: '%v'", expected, obs2))
}
}
func TestCryptoRandInt64(t *testing.T) {
rnd := CryptoRandInt64()
if rnd == 0 {
panic("cryptoRandInt64() gave 0, very high odds it has broken")
}
}
func TestHashOfDir(t *testing.T) {
dir, err := testhook.TempDir(t, "TestHashOfDir-dir")
if err != nil {
t.Fatal(err)
}
b := path.Join(dir, "A", "B")
if err := os.MkdirAll(b, 0755); err != nil {
t.Fatal(err)
}
c := path.Join(dir, "A", "C")
if err := os.MkdirAll(c, 0755); err != nil {
t.Fatal(err)
}
bmessage := []byte("hello B\n")
if err := ioutil.WriteFile(path.Join(b, "b_content"), bmessage, 0644); err != nil {
t.Fatal(err)
}
cmessage := []byte("hello C\n")
if err := ioutil.WriteFile(path.Join(c, "c_content"), cmessage, 0644); err != nil {
t.Fatal(err)
}
hsh := HashOfDir(dir)
c2message := []byte("hello C2\n")
if err := ioutil.WriteFile(path.Join(c, "c_content"), c2message, 0644); err != nil {
t.Fatal(err)
}
hsh2 := HashOfDir(dir)
if hsh2 == hsh {
t.Fatal("HashOfDir did not detect 1 byte change")
}
}