| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/release" |
| 14 | ) |
| 15 | |
| 16 | // The version string is what a program says about itself. A bubblewrap |
| 17 | // that says "0.11.0" and is other bytes than the pinned build is refused |
| 18 | // before anything runs in it. |
| 19 | func TestNamespaceBubblewrapDigest(t *testing.T) { |
| 20 | m, _ := fixture(t) |
| 21 | cfg := m.cfg |
| 22 | real, err := os.ReadFile(cfg.Binary) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | // The same program with one byte after it: it still runs and still |
| 27 | // reports its version, and it is not the pinned build. |
| 28 | other := filepath.Join(t.TempDir(), "bwrap") |
| 29 | if err = os.WriteFile(other, append(real, 0), 0700); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | cfg.Binary = other |
| 33 | if _, err = New(cfg); err == nil || !strings.Contains(err.Error(), "namespace_bubblewrap_digest") { |
| 34 | t.Fatalf("a bubblewrap with another digest started: %v", err) |
| 35 | } |
| 36 | if err = os.WriteFile(other, real, 0700); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if _, err = New(cfg); err != nil { |
| 40 | t.Fatalf("the pinned build at another path refused: %v", err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // The release pin is the manifest's: the amd64 build a release runs on. |
| 45 | // The arm64 build is another digest, for tests on an arm64 machine. |
| 46 | func TestBubblewrapPins(t *testing.T) { |
| 47 | pinned := "" |
| 48 | for _, b := range release.BuildManifest().Binaries { |
| 49 | if b.Name == "bwrap" { |
| 50 | pinned = b.SHA256 |
| 51 | } |
| 52 | } |
| 53 | if pin, ok := BubblewrapPin("amd64"); !ok || pin != pinned || pin != BubblewrapSHA256 { |
| 54 | t.Fatalf("amd64 pin %q, manifest %q", pin, pinned) |
| 55 | } |
| 56 | arm, ok := BubblewrapPin("arm64") |
| 57 | if !ok || arm == pinned || len(arm) != 64 { |
| 58 | t.Fatalf("arm64 pin %q", arm) |
| 59 | } |
| 60 | if _, ok := BubblewrapPin("riscv64"); ok { |
| 61 | t.Fatal("a pin for a machine with no pinned build") |
| 62 | } |
| 63 | if _, ok := BubblewrapPin(runtime.GOARCH); !ok { |
| 64 | t.Skip("no pinned bubblewrap for this machine") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // The CI box installs exactly the two pinned builds (scripts/ci-box.json). |
| 69 | // It reads the source tree, so it is a TestSource test. |
| 70 | func TestSourceBubblewrapPinsMatchCIBox(t *testing.T) { |
| 71 | raw, err := os.ReadFile("../../../scripts/ci-box.json") |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | var box struct { |
| 76 | Tools map[string]map[string]json.RawMessage |
| 77 | } |
| 78 | if err = json.Unmarshal(raw, &box); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | for _, arch := range []string{"amd64", "arm64"} { |
| 82 | var tool struct{ SHA256 string } |
| 83 | if err = json.Unmarshal(box.Tools["bwrap"][arch], &tool); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if pin, _ := BubblewrapPin(arch); tool.SHA256 != pin { |
| 87 | t.Fatalf("the CI box installs another %s bubblewrap than the pin: %s", arch, tool.SHA256) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |