11package casblob_test
22
33import (
4+ "bytes"
5+ "crypto/sha256"
6+ "encoding/hex"
7+ "fmt"
8+ "io"
9+ "os"
410 "testing"
511 "unsafe"
12+
13+ "github.com/buchgr/bazel-remote/v2/cache/disk/casblob"
14+ "github.com/buchgr/bazel-remote/v2/cache/disk/zstdimpl"
15+ testutils "github.com/buchgr/bazel-remote/v2/utils"
616)
717
818func TestLenSize (t * testing.T ) {
@@ -17,3 +27,49 @@ func TestLenSize(t *testing.T) {
1727 t .Errorf ("This should silence linters that think slice is never used" )
1828 }
1929}
30+
31+ func TestZstdFromLegacy (t * testing.T ) {
32+ size := 1024
33+ zstd , err := zstdimpl .Get ("go" )
34+ if err != nil {
35+ t .Fatal (err )
36+ }
37+
38+ data , hash := testutils .RandomDataAndHash (int64 (size ))
39+ dir := testutils .TempDir (t )
40+ filename := fmt .Sprintf ("%s/%s" , dir , hash )
41+ file , err := os .OpenFile (filename , os .O_RDWR | os .O_CREATE | os .O_EXCL , 0664 )
42+ if err != nil {
43+ t .Fatal (err )
44+ }
45+ file .Write (data )
46+ file .Close ()
47+
48+ file , err = os .Open (filename )
49+ if err != nil {
50+ t .Fatal (err )
51+ }
52+ zrc , err := casblob .GetLegacyZstdReadCloser (zstd , file )
53+ if err != nil {
54+ t .Fatal (err )
55+ }
56+ rc , err := zstd .GetDecoder (zrc )
57+ if err != nil {
58+ t .Fatal (err )
59+ }
60+ buf := bytes .NewBuffer (nil )
61+ _ , err = io .Copy (buf , rc )
62+ if err != nil {
63+ t .Fatal (err )
64+ }
65+
66+ if buf .Len () != size {
67+ t .Fatalf ("Unexpected buf size %d, expected %d" , buf .Len (), size )
68+ }
69+
70+ h := sha256 .Sum256 (data )
71+ hs := hex .EncodeToString (h [:])
72+ if hs != hash {
73+ t .Fatalf ("Unexpected content sha %s, expected %s" , hs , hash )
74+ }
75+ }
0 commit comments