| 1 | package adopt |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/base64" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // productionVersions is what each app served in production on |
| 10 | // 2026-11-17: the /api/version body and X-Carlos-Version, the same build |
| 11 | // named two ways by four of the six. |
| 12 | var productionVersions = map[string]struct{ body, header string }{ |
| 13 | "docs": {"v0.1.0-80-g9de84e4", "v0.1.0-80-g9de84e4"}, |
| 14 | "sheets": {"v0.1.0-53-gef9c313", "ef9c313"}, |
| 15 | "slides": {"v0.1.0-42-gcb30df5", "cb30df5"}, |
| 16 | "calendar": {"v0.1.0-53-g800406a", "800406a"}, |
| 17 | "calls": {"ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca", "ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca"}, |
| 18 | "memoria": {"v0.1.0-47-g174fb45", "174fb45"}, |
| 19 | } |
| 20 | |
| 21 | func TestSameReleaseAcceptsProductionPairs(t *testing.T) { |
| 22 | for app, v := range productionVersions { |
| 23 | if !SameRelease(v.body, v.header) { |
| 24 | t.Errorf("%s: body %q and header %q refused", app, v.body, v.header) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestSameRelease(t *testing.T) { |
| 30 | full := "ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca" |
| 31 | cases := []struct { |
| 32 | name, body, header string |
| 33 | want bool |
| 34 | }{ |
| 35 | {"equal names", "v1.4.0", "v1.4.0", true}, |
| 36 | {"equal commit ids", full, full, true}, |
| 37 | {"describe, short header", "v0.1.0-53-gef9c313", "ef9c313", true}, |
| 38 | {"describe, full header", "v0.1.0-53-gef2335e", full, true}, |
| 39 | {"describe, longer abbreviation than header", "v0.1.0-53-gef2335e9df94", "ef2335e9", true}, |
| 40 | {"describe, many-digit parts", "v12.40.3-1207-g800406a", "800406a", true}, |
| 41 | |
| 42 | {"different hex", "v0.1.0-53-gef9c313", "ef9c314", false}, |
| 43 | {"different first digit", "v0.1.0-53-gef9c313", "af9c313", false}, |
| 44 | {"header of six hex digits", "v0.1.0-53-gef9c313", "ef9c31", false}, |
| 45 | {"describe of six hex digits", "v0.1.0-53-gef9c31", "ef9c313", false}, |
| 46 | {"six in common, both longer", "v0.1.0-53-gef9c31a", "ef9c31b", false}, |
| 47 | {"non-describe body, short header", "v1.4.0", "ef9c313", false}, |
| 48 | {"bare-commit body, its own prefix as header", full, "ef2335e", false}, |
| 49 | {"build-name body, short header", "rel-ef9c313", "ef9c313", false}, |
| 50 | {"header not hex", "v0.1.0-53-gef9c313", "ef9c31z", false}, |
| 51 | {"header a dirty describe", "v0.1.0-53-gef9c313", "v0.1.0-53-gef9c313-dirty", false}, |
| 52 | {"header a tag", "v0.1.0-53-gef9c313", "v0.1.0", false}, |
| 53 | {"body describes another commit", "v0.1.0-53-gcb30df5", "ef9c313", false}, |
| 54 | {"uppercase header", "v0.1.0-53-gef9c313", "EF9C313", false}, |
| 55 | {"uppercase describe hex", "v0.1.0-53-gEF9C313", "ef9c313", false}, |
| 56 | {"dirty describe", "v0.1.0-53-gef9c313-dirty", "ef9c313", false}, |
| 57 | {"describe without v", "0.1.0-53-gef9c313", "ef9c313", false}, |
| 58 | {"describe without count", "v0.1.0-gef9c313", "ef9c313", false}, |
| 59 | {"describe of a two-part tag", "v0.1-53-gef9c313", "ef9c313", false}, |
| 60 | {"describe with a pre-release tag", "v0.1.0-rc1-53-gef9c313", "ef9c313", false}, |
| 61 | {"describe with a newline", "v0.1.0-53-gef9c313\n", "ef9c313", false}, |
| 62 | {"header with a newline", "v0.1.0-53-gef9c313", "ef9c313\n", false}, |
| 63 | {"header with a space", "v0.1.0-53-gef9c313", " ef9c313", false}, |
| 64 | {"empty header", "v0.1.0-53-gef9c313", "", false}, |
| 65 | {"empty body", "", "ef9c313", false}, |
| 66 | {"names swapped", "ef9c313", "v0.1.0-53-gef9c313", false}, |
| 67 | {"different names", "v1.4.0", "v1.4.1", false}, |
| 68 | } |
| 69 | for _, c := range cases { |
| 70 | t.Run(c.name, func(t *testing.T) { |
| 71 | if got := SameRelease(c.body, c.header); got != c.want { |
| 72 | t.Fatalf("SameRelease(%q, %q) = %v, want %v", c.body, c.header, got, c.want) |
| 73 | } |
| 74 | }) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Each app serving its production names passes the CARLOS preflight, |
| 79 | // configure, the delivery check and the activation gate, which sends the |
| 80 | // body as app_version and hashes the header for artifact_digest. |
| 81 | func TestObserverAcceptsProductionVersionNames(t *testing.T) { |
| 82 | for app, v := range productionVersions { |
| 83 | t.Run(app, func(t *testing.T) { |
| 84 | x := newObserverFixture(t, app, func(w *writeFixture) { w.site.version, w.site.header = v.body, v.header }) |
| 85 | if app != CanaryApp { |
| 86 | canaryActive(t, x.s) |
| 87 | } |
| 88 | obs, err := x.observe() |
| 89 | if err != nil { |
| 90 | t.Fatalf("Observe: %v", err) |
| 91 | } |
| 92 | if obs.AppVersion != v.body { |
| 93 | t.Fatalf("app_version = %q, want the body %q", obs.AppVersion, v.body) |
| 94 | } |
| 95 | sum := sha256.Sum256([]byte(v.header)) |
| 96 | if obs.ArtifactDigest != base64.RawURLEncoding.EncodeToString(sum[:]) { |
| 97 | t.Fatal("artifact_digest is not the SHA-256 of X-Carlos-Version") |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestPreflightAcceptsProductionVersionNames(t *testing.T) { |
| 104 | for app, v := range productionVersions { |
| 105 | t.Run(app, func(t *testing.T) { |
| 106 | x := newCarlosFixture(t, app) |
| 107 | x.site.version, x.site.header = v.body, v.header |
| 108 | _, codes := x.preflight(t, nil) |
| 109 | wantCodes(t, codes) |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // A lookalike pair is refused at both guards: by the preflight before |
| 115 | // anything is written, and by the activation gate when the app moved to |
| 116 | // it afterwards (the journal rewritten to agree, so only the pair is |
| 117 | // wrong). |
| 118 | func TestLookalikeVersionNamesRefused(t *testing.T) { |
| 119 | lookalikes := map[string]string{ |
| 120 | "another commit": "ef9c314", |
| 121 | "six hex digits": "ef9c31", |
| 122 | "uppercase": "EF9C313", |
| 123 | "not hex": "ef9c31g", |
| 124 | "dirty describe": "v0.1.0-53-gef9c313-dirty", |
| 125 | "other describe": "v0.1.0-54-gef9c313", |
| 126 | "another app's": "cb30df5", |
| 127 | "full id, other": "ef9c314" + "000000000000000000000000000000000", |
| 128 | "seven, one off": "ef9c312", |
| 129 | } |
| 130 | const body = "v0.1.0-53-gef9c313" |
| 131 | for name, header := range lookalikes { |
| 132 | t.Run("preflight "+name, func(t *testing.T) { |
| 133 | x := newCarlosFixture(t, "sheets") |
| 134 | x.site.version, x.site.header = body, header |
| 135 | _, codes := x.preflight(t, nil) |
| 136 | wantCodes(t, codes, "version-header-mismatch") |
| 137 | }) |
| 138 | t.Run("gate "+name, func(t *testing.T) { |
| 139 | x := newObserverFixture(t, "sheets") |
| 140 | x.site.version, x.site.header = body, header |
| 141 | x.o.Artifacts = map[string]string{"sheets": sha256Hex(header)} |
| 142 | x.rejournal(t, func(e *evidence) { |
| 143 | e.base.Delivery.Version, e.after.Delivery.Version, e.check.Delivery.Version = body, body, body |
| 144 | e.base.Delivery.Header, e.after.Delivery.Header, e.check.Delivery.Header = header, header, header |
| 145 | }) |
| 146 | canaryActive(t, x.s) |
| 147 | wantCodes(t, x.refused(t, "version-header-mismatch"), "version-header-mismatch") |
| 148 | }) |
| 149 | } |
| 150 | } |
| 151 | |