//go:build linux package containment import ( "encoding/json" "os" "path/filepath" "runtime" "strings" "testing" "amadan.net/oficina/equipo/internal/release" ) // The version string is what a program says about itself. A bubblewrap // that says "0.11.0" and is other bytes than the pinned build is refused // before anything runs in it. func TestNamespaceBubblewrapDigest(t *testing.T) { m, _ := fixture(t) cfg := m.cfg real, err := os.ReadFile(cfg.Binary) if err != nil { t.Fatal(err) } // The same program with one byte after it: it still runs and still // reports its version, and it is not the pinned build. other := filepath.Join(t.TempDir(), "bwrap") if err = os.WriteFile(other, append(real, 0), 0700); err != nil { t.Fatal(err) } cfg.Binary = other if _, err = New(cfg); err == nil || !strings.Contains(err.Error(), "namespace_bubblewrap_digest") { t.Fatalf("a bubblewrap with another digest started: %v", err) } if err = os.WriteFile(other, real, 0700); err != nil { t.Fatal(err) } if _, err = New(cfg); err != nil { t.Fatalf("the pinned build at another path refused: %v", err) } } // The release pin is the manifest's: the amd64 build a release runs on. // The arm64 build is another digest, for tests on an arm64 machine. func TestBubblewrapPins(t *testing.T) { pinned := "" for _, b := range release.BuildManifest().Binaries { if b.Name == "bwrap" { pinned = b.SHA256 } } if pin, ok := BubblewrapPin("amd64"); !ok || pin != pinned || pin != BubblewrapSHA256 { t.Fatalf("amd64 pin %q, manifest %q", pin, pinned) } arm, ok := BubblewrapPin("arm64") if !ok || arm == pinned || len(arm) != 64 { t.Fatalf("arm64 pin %q", arm) } if _, ok := BubblewrapPin("riscv64"); ok { t.Fatal("a pin for a machine with no pinned build") } if _, ok := BubblewrapPin(runtime.GOARCH); !ok { t.Skip("no pinned bubblewrap for this machine") } } // The CI box installs exactly the two pinned builds (scripts/ci-box.json). // It reads the source tree, so it is a TestSource test. func TestSourceBubblewrapPinsMatchCIBox(t *testing.T) { raw, err := os.ReadFile("../../../scripts/ci-box.json") if err != nil { t.Fatal(err) } var box struct { Tools map[string]map[string]json.RawMessage } if err = json.Unmarshal(raw, &box); err != nil { t.Fatal(err) } for _, arch := range []string{"amd64", "arm64"} { var tool struct{ SHA256 string } if err = json.Unmarshal(box.Tools["bwrap"][arch], &tool); err != nil { t.Fatal(err) } if pin, _ := BubblewrapPin(arch); tool.SHA256 != pin { t.Fatalf("the CI box installs another %s bubblewrap than the pin: %s", arch, tool.SHA256) } } }