| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | ) |
| 11 | |
| 12 | // Settings plan 4 Task 5d (ruling R-EXEC-CLAIMVERSION): the claimed launch |
| 13 | // names the file it hashed by one resolution of the live link, with the |
| 14 | // version that file is staged under, so the record before the exec names |
| 15 | // the bytes that run. Executable stays the live link (the launch digest and |
| 16 | // root's binding are unchanged), and Version is still its own read. |
| 17 | func TestTheClaimedLaunchNamesTheBuildItHashed(t *testing.T) { |
| 18 | f := newAdminTokenFixture(t, "box-1") |
| 19 | bin := filepath.Join(f.backend.OptDir, "versions", "v1", "bab", "jam") |
| 20 | sum, err := release.FileSHA256(bin) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | want := release.StagedIdentity{Version: "v1", StagedPath: bin, Digest: sum} |
| 25 | l, err := claimServingLaunchSource(f.backend, f.tokens)(t.Context(), f.rt) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if l.Staged != want || l.ExecutableSHA256 != sum || l.Version != "v1" || l.Executable != filepath.Join(f.backend.OptDir, "live", adminTokenHost) { |
| 30 | t.Fatalf("serving launch of %s (sha %s, version %q) staged %+v, want the live link hashed as %+v", l.Executable, l.ExecutableSHA256, l.Version, l.Staged, want) |
| 31 | } |
| 32 | t.Setenv("CARLOS_SECRETS_KEY", base64.StdEncoding.EncodeToString(make([]byte, 32))) |
| 33 | m, _, err := claimMoveLaunchSource(f.backend)(t.Context(), f.rt) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if m.Staged != want || m.ExecutableSHA256 != sum { |
| 38 | t.Fatalf("moves launch staged %+v, want %+v", m.Staged, want) |
| 39 | } |
| 40 | // A live link outside the version store hashes a file no build names. |
| 41 | other := filepath.Join(t.TempDir(), "app") |
| 42 | if err := os.WriteFile(other, []byte("#!/bin/sh\n"), 0o755); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | link := filepath.Join(f.backend.OptDir, "live", adminTokenHost) |
| 46 | if err := os.Remove(link); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if err := os.Symlink(other, link); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | snap, err := f.backend.SnapshotClaimLaunch(t.Context(), f.rt) |
| 53 | if err == nil { |
| 54 | t.Fatalf("an unversioned live link was snapshotted: %+v", snap) |
| 55 | } |
| 56 | } |
| 57 | |