101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseSize(t *testing.T) {
|
|
for in, want := range map[string]int64{"100": 100, "1KB": 1024, "1.5MB": 3 << 19, "2g": 2 << 30} {
|
|
if got, err := ParseSize(in); err != nil || got != want {
|
|
t.Errorf("%s: got %d %v", in, got, err)
|
|
}
|
|
}
|
|
if _, err := ParseSize("abc"); err == nil {
|
|
t.Error("expected error")
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigPrecedence(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if c, _ := LoadConfig(dir, ""); c.MinSize != defaultMinSize {
|
|
t.Errorf("default: %d", c.MinSize)
|
|
}
|
|
os.WriteFile(filepath.Join(dir, configFileName), []byte(`{"min_size":"2MB"}`), 0o644)
|
|
if c, _ := LoadConfig(dir, ""); c.MinSize != 2<<20 {
|
|
t.Errorf("file: %d", c.MinSize)
|
|
}
|
|
if c, _ := LoadConfig(dir, "10KB"); c.MinSize != 10<<10 {
|
|
t.Errorf("flag: %d", c.MinSize)
|
|
}
|
|
}
|
|
|
|
func md5hex(b []byte) string { s := md5.Sum(b); return hex.EncodeToString(s[:]) }
|
|
|
|
func TestHashSmallIsFullMD5(t *testing.T) {
|
|
data := bytes.Repeat([]byte("ab"), 1000)
|
|
got, _ := hashReaderAt(bytes.NewReader(data), int64(len(data)))
|
|
if got != md5hex(data) {
|
|
t.Error("mismatch")
|
|
}
|
|
}
|
|
|
|
func TestHashLarge(t *testing.T) {
|
|
size := 25<<20 + 123
|
|
data := make([]byte, size)
|
|
for i := range data {
|
|
data[i] = byte(i * 7)
|
|
}
|
|
var exp []byte
|
|
exp = append(exp, data[0:1024]...)
|
|
exp = append(exp, data[10<<20:10<<20+1024]...)
|
|
exp = append(exp, data[20<<20:]...)
|
|
got, err := hashReaderAt(bytes.NewReader(data), int64(size))
|
|
if err != nil || got != md5hex(exp) {
|
|
t.Errorf("mismatch %v", err)
|
|
}
|
|
// changing an unsampled byte must not change the hash
|
|
data[5000] ^= 0xff
|
|
got2, _ := hashReaderAt(bytes.NewReader(data), int64(size))
|
|
if got2 != got {
|
|
t.Error("unsampled byte affected hash")
|
|
}
|
|
}
|
|
|
|
func TestBuildIndex(t *testing.T) {
|
|
dir := t.TempDir()
|
|
os.MkdirAll(filepath.Join(dir, "sub"), 0o755)
|
|
os.WriteFile(filepath.Join(dir, "small.txt"), []byte("x"), 0o644)
|
|
os.WriteFile(filepath.Join(dir, "sub", "big.bin"), make([]byte, 2000), 0o644)
|
|
entries, _, err := BuildIndex(context.Background(), dir, 1000, nil)
|
|
if err != nil || len(entries) != 1 {
|
|
t.Fatalf("%v %v", entries, err)
|
|
}
|
|
e := entries[0]
|
|
if e.FileName != "big.bin" || e.FilePath != filepath.Join(dir, "sub", "big.bin") || e.FileSize != 2000 {
|
|
t.Errorf("%+v", e)
|
|
}
|
|
path, err := WriteIndex(dir, entries)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var back []FileEntry
|
|
b, _ := os.ReadFile(path)
|
|
if json.Unmarshal(b, &back) != nil || len(back) != 1 {
|
|
t.Error("bad json")
|
|
}
|
|
// index file itself is excluded on rerun
|
|
entries, _, _ = BuildIndex(context.Background(), dir, 0, nil)
|
|
for _, e := range entries {
|
|
if e.FileName == indexFileName {
|
|
t.Error("index indexed")
|
|
}
|
|
}
|
|
}
|