Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroPatti authored and mostynb committed Aug 30, 2023
1 parent 9844fa5 commit da088f9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cache/disk/casblob/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ go_library(
go_test(
name = "go_default_test",
srcs = ["casblob_test.go"],
deps = [
":go_default_library",
"//cache/disk/zstdimpl:go_default_library",
"//utils:go_default_library",
],
)
62 changes: 62 additions & 0 deletions cache/disk/casblob/casblob_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package casblob_test

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"testing"
"unsafe"

"github.com/buchgr/bazel-remote/v2/cache/disk/casblob"
"github.com/buchgr/bazel-remote/v2/cache/disk/zstdimpl"
testutils "github.com/buchgr/bazel-remote/v2/utils"
)

func TestLenSize(t *testing.T) {
Expand All @@ -17,3 +27,55 @@ func TestLenSize(t *testing.T) {
t.Errorf("This should silence linters that think slice is never used")
}
}

func TestZstdFromLegacy(t *testing.T) {
size := 1024
zstd, err := zstdimpl.Get("go")
if err != nil {
t.Fatal(err)
}

data, hash := testutils.RandomDataAndHash(int64(size))
dir := testutils.TempDir(t)
filename := fmt.Sprintf("%s/%s", dir, hash)
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0664)
if err != nil {
t.Fatal(err)
}
n, err := file.Write(data)
if err != nil {
t.Fatal(err)
}
if n != size {
t.Fatalf("Unexpected short write %d, expected %d", n, size)
}
file.Close()

file, err = os.Open(filename)
if err != nil {
t.Fatal(err)
}
zrc, err := casblob.GetLegacyZstdReadCloser(zstd, file)
if err != nil {
t.Fatal(err)
}
rc, err := zstd.GetDecoder(zrc)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(nil)
_, err = io.Copy(buf, rc)
if err != nil {
t.Fatal(err)
}

if buf.Len() != size {
t.Fatalf("Unexpected buf size %d, expected %d", buf.Len(), size)
}

h := sha256.Sum256(data)
hs := hex.EncodeToString(h[:])
if hs != hash {
t.Fatalf("Unexpected content sha %s, expected %s", hs, hash)
}
}

0 comments on commit da088f9

Please sign in to comment.