mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
* removes unused filesize function * removes ioutil usage * updates ioutil.ReadAll to io.ReadAll * updates ioutil.TempFile to os.CreateTemp * updates ioutil.TempDir to os.MkdirTemp * updates ioutil.ReadAll to os.ReadAll * update ioutil.WriteFile to os.WriteFile * updates ioutil.Discard to io.Discard * updates ioutil.ReadDir to os.ReadDir where applicable * removes unused code in idk * creates type to use for context value keys * replaces assert.Nil with assert.NoError for error checks
77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package hash
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"github.com/featurebasedb/featurebase/v3/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 := os.WriteFile(path.Join(b, "b_content"), bmessage, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cmessage := []byte("hello C\n")
|
|
if err := os.WriteFile(path.Join(c, "c_content"), cmessage, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
hsh := HashOfDir(dir)
|
|
|
|
c2message := []byte("hello C2\n")
|
|
if err := os.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")
|
|
}
|
|
}
|