package pushgate import ( "strings" "testing" ) const ( hexA = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" hexB = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210" ) func TestManifestDigestFromIndexTakesTheOneImage(t *testing.T) { index := `{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:` + hexA + `","size":1}]}` got, err := ManifestDigestFromIndex([]byte(index)) if err != nil || got != "sha256:"+hexA { t.Fatalf("got %q, %v", got, err) } docker := strings.Replace(index, "application/vnd.oci.image.manifest.v1+json", "application/vnd.docker.distribution.manifest.v2+json", 1) if _, err := ManifestDigestFromIndex([]byte(docker)); err != nil { t.Fatalf("a docker v2 manifest is a single image too: %v", err) } } func TestManifestDigestFromIndexRefusesWhatIsNotOneImage(t *testing.T) { m := `{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:` + hexA + `"}` for name, index := range map[string]string{ "two manifests": `{"manifests":[` + m + `,` + m + `]}`, "none": `{"manifests":[]}`, "an index": `{"manifests":[{"mediaType":"application/vnd.oci.image.index.v1+json","digest":"sha256:` + hexA + `"}]}`, "sha512": `{"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha512:` + hexA + `"}]}`, "upper hex": `{"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:` + strings.ToUpper(hexA) + `"}]}`, "not json": `manifests`, } { if _, err := ManifestDigestFromIndex([]byte(index)); err == nil { t.Errorf("%s: accepted", name) } } } func TestRegistryDigestFromReadsOnlyItsLine(t *testing.T) { out := "logged in\npushed blob sha256:" + hexB + "\nPUSHED x\n" + RegistryDigestPrefix + "sha256:" + hexA + "\n" if got := RegistryDigestFrom(out); got != "sha256:"+hexA { t.Fatalf("got %q", got) } if got := RegistryDigestFrom("pushed blob sha256:" + hexA); got != "" { t.Fatalf("a blob digest is not the registry's manifest digest, got %q", got) } if got := RegistryDigestFrom(RegistryDigestPrefix + "sha256:abc"); got != "" { t.Fatalf("a short digest is not a digest, got %q", got) } } // Mutant: make SameDigest return nil unconditionally -> this test fails. func TestSameDigestFailsOnAMismatch(t *testing.T) { if err := SameDigest("sha256:"+hexA, "sha256:"+hexA); err != nil { t.Fatalf("equal digests: %v", err) } err := SameDigest("sha256:"+hexA, "sha256:"+hexB) if err == nil || !strings.Contains(err.Error(), "differs") { t.Fatalf("mismatch accepted: %v", err) } if SameDigest("sha256:"+hexA, "") == nil { t.Fatal("a registry that reported no digest was accepted") } } func TestRequestBodyValidates(t *testing.T) { if _, err := RequestBody("svc", "sha256:"+hexA, "0123456789ab"); err != nil { t.Fatalf("legitimate request refused: %v", err) } if _, err := RequestBody("svc", "sha256:"+hexA, "0123456789ab-rc-build.7"); err != nil { t.Fatalf("a UAT rebuild tag is a legitimate version: %v", err) } for name, args := range map[string][3]string{ "no service": {"", "sha256:" + hexA, "v1"}, "tag digest": {"svc", "latest", "v1"}, "bad version": {"svc", "sha256:" + hexA, "../x"}, "empty version": {"svc", "sha256:" + hexA, ""}, } { if _, err := RequestBody(args[0], args[1], args[2]); err == nil { t.Errorf("%s: accepted", name) } } } // Mutant: make Judge push on `refuse` -> this test fails. func TestJudgeRefusesOnlyAnExplicitRefusal(t *testing.T) { refused := Judge(200, []byte(`{"decision":"refuse","result":"block","mode":"enforce","reasons":["cve: 2 critical"],"would_block":[]}`)) if refused.Push || !strings.Contains(refused.Message, "verdict: block (cve)") { t.Fatalf("refusal: %+v", refused) } } // Mutant (legal direction): make Judge refuse everything -> this test fails. func TestJudgePushesInShadowAndSaysWhatWouldBlock(t *testing.T) { shadow := Judge(200, []byte(`{"decision":"push","result":"block","mode":"shadow","reasons":[],"would_block":["cve"]}`)) if !shadow.Push || !strings.Contains(shadow.Message, "would block in enforce: cve") { t.Fatalf("shadow: %+v", shadow) } for name, o := range map[string]Outcome{ "unreachable": Judge(0, nil), "server error": Judge(503, nil), "not found": Judge(404, nil), "garbage": Judge(200, []byte(``)), "unknown word": Judge(200, []byte(`{"decision":"maybe"}`)), } { if !o.Push { t.Errorf("%s: refused a push without an explicit refusal (%s)", name, o.Message) } } } // Mutant: drop the 401/403 branch in Judge -> this test fails (a refused token // binding would push silently). WP-06 review, 2026-11-04. func TestJudgeRefusesAnAuthorizationRefusal(t *testing.T) { for _, status := range []int{401, 403} { o := Judge(status, []byte(`{"type":"scan-token-wrong-revision"}`)) if o.Push || !strings.Contains(o.Message, "image NOT pushed") { t.Errorf("HTTP %d: %+v", status, o) } } }