aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/pkg_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/pkg/pkg_test.go')
-rw-r--r--internal/pkg/pkg_test.go153
1 files changed, 153 insertions, 0 deletions
diff --git a/internal/pkg/pkg_test.go b/internal/pkg/pkg_test.go
index f295ef0b..46084ff4 100644
--- a/internal/pkg/pkg_test.go
+++ b/internal/pkg/pkg_test.go
@@ -3,6 +3,7 @@ package pkg_test
import (
"bytes"
"crypto/sha512"
+ "encoding/binary"
"io/fs"
"net/http"
"os"
@@ -303,6 +304,158 @@ func TestCache(t *testing.T) {
t.Fatalf("LoadFile: error = %#v, want %#v", err, wantErrNonexistentZero)
}
}, pkg.MustDecode("G4u4W77C3u46oSAzwPTERKbS9h76iIvcd7Zl8p8Y6hTMb4_QGpH0Glg_DIJg-Usa")},
+
+ {"directory", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
+ id := pkg.KindTar.Ident(
+ binary.LittleEndian.AppendUint64(nil, 1),
+ stubArtifact{pkg.KindHTTP, testdataChecksum},
+ )
+ makeSample := func(work *check.Absolute) error {
+ if err := os.WriteFile(
+ work.Append("check").String(),
+ []byte{0, 0},
+ 0400,
+ ); err != nil {
+ return err
+ }
+
+ if err := os.MkdirAll(work.Append(
+ "lib",
+ "pkgconfig",
+ ).String(), 0700); err != nil {
+ return err
+ }
+
+ return os.Symlink(
+ "/proc/nonexistent/libedac.so",
+ work.Append(
+ "lib",
+ "libedac.so",
+ ).String(),
+ )
+ }
+ wantChecksum := pkg.MustDecode(
+ "1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP",
+ )
+ wantPathname := base.Append(
+ "identifier",
+ pkg.Encode(id),
+ )
+
+ if pathname, store, err := c.Store(
+ id,
+ makeSample,
+ &wantChecksum,
+ true,
+ ); err != nil {
+ t.Fatalf("Store: error = %v", err)
+ } else if !store {
+ t.Fatal("Store did not store nonpresent entry")
+ } else if !pathname.Is(wantPathname) {
+ t.Fatalf("Store: pathname = %q, want %q", pathname, wantPathname)
+ }
+
+ // check lookup
+ if pathname, store, err := c.Store(
+ id,
+ nil,
+ &wantChecksum,
+ true,
+ ); err != nil {
+ t.Fatalf("Store: error = %v", err)
+ } else if store {
+ t.Fatal("Store stored over present entry")
+ } else if !pathname.Is(wantPathname) {
+ t.Fatalf("Store: pathname = %q, want %q", pathname, wantPathname)
+ }
+
+ // check exist
+ id0 := pkg.KindTar.Ident(
+ binary.LittleEndian.AppendUint64(nil, 1),
+ stubArtifact{pkg.KindHTTP, pkg.ID{}},
+ )
+ wantPathname0 := base.Append(
+ "identifier",
+ pkg.Encode(id0),
+ )
+ if pathname, store, err := c.Store(
+ id0,
+ makeSample,
+ &wantChecksum,
+ true,
+ ); err != nil {
+ t.Fatalf("Store: error = %v", err)
+ } else if !store {
+ t.Fatal("Store did not store nonpresent entry")
+ } else if !pathname.Is(wantPathname0) {
+ t.Fatalf("Store: pathname = %q, want %q", pathname, wantPathname0)
+ }
+
+ var wantErrMakeGarbage error
+ makeGarbage := func(work *check.Absolute) error {
+ mode := fs.FileMode(0)
+ if wantErrMakeGarbage == nil {
+ mode = 0500
+ }
+
+ if err := os.MkdirAll(work.Append(
+ "lib",
+ "pkgconfig",
+ ).String(), 0700); err != nil {
+ return err
+ }
+
+ if err := os.WriteFile(work.Append(
+ "lib",
+ "check",
+ ).String(), nil, 0400&mode); err != nil {
+ return err
+ }
+
+ if err := os.Chmod(work.Append(
+ "lib",
+ "pkgconfig",
+ ).String(), 0500&mode); err != nil {
+ return err
+ }
+ if err := os.Chmod(work.Append(
+ "lib",
+ ).String(), 0500&mode); err != nil {
+ return err
+ }
+
+ return wantErrMakeGarbage
+ }
+
+ // check makeArtifact fault
+ wantErrMakeGarbage = stub.UniqueError(0xcafe)
+ if _, store, err := c.Store(
+ pkg.ID{},
+ makeGarbage,
+ nil,
+ false,
+ ); !reflect.DeepEqual(err, wantErrMakeGarbage) {
+ t.Fatalf("Store: error = %#v, want %#v", err, wantErrMakeGarbage)
+ } else if !store {
+ t.Fatal("Store did not store nonpresent entry")
+ }
+
+ // checksum mismatch
+ wantErrMakeGarbage = nil
+ wantErrMismatch := &pkg.ChecksumMismatchError{
+ Got: pkg.MustDecode("GbjlYMcHQANdfwL6qNGopBF99IscPTvCy95HSH1_kIF3eKjFDSLP0_iUUT0z8hiw"),
+ }
+ if _, store, err := c.Store(
+ pkg.ID{},
+ makeGarbage,
+ new(pkg.Checksum),
+ true,
+ ); !reflect.DeepEqual(err, wantErrMismatch) {
+ t.Fatalf("Store: error = %v, want %v", err, wantErrMismatch)
+ } else if !store {
+ t.Fatal("Store did not store nonpresent entry")
+ }
+ }, pkg.MustDecode("N7dntFYbOq9V4iC-rjAQ-By6ofPIQVZkA8V0r0G07M_sdB7Zh42Ttrspsc38ioYa")},
}
checkWithCache(t, testCases)
}