package releaseupdate import ( "crypto/ed25519" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/samcharles93/archie-core/internal/domain/distribution" "github.com/samcharles93/archie-core/internal/infrastructure/signedoci" ) // A release is trusted only when the source's key signed its statement, the // tag it was found under agrees with it, and a moving channel tag never goes // back to a release issued before one already accepted from it. func TestFetchRelease(t *testing.T) { trusted, signer, _ := ed25519.GenerateKey(nil) stranger, _, _ := ed25519.GenerateKey(nil) archive := []byte("zip") statement := func(version string, issued time.Time) []byte { data, err := json.Marshal(Statement{ Version: version, IssuedAt: issued, Platform: Platform, AgentImage: newImage, Zip: Blob{Name: ZipName(version), Digest: signedoci.Digest(archive), Size: int64(len(archive))}, }) if err != nil { t.Fatal(err) } return data } jan1, jan2 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC) cases := []struct { name string accepted []byte // a statement already accepted from the same tag statement []byte tag string key ed25519.PublicKey want string // error substring, or "" to accept }{ {name: "signed by the source key", statement: statement("2.0.0", jan1), tag: "stable", key: trusted}, {name: "signed by another key", statement: statement("2.0.0", jan1), tag: "stable", key: stranger, want: "signature"}, {name: "a version tag carrying another version", statement: statement("2.0.0", jan1), tag: "v3.0.0", key: trusted, want: "carries"}, {name: "a channel moved back to an older release", accepted: statement("2.1.0", jan2), statement: statement("2.0.0", jan1), tag: "stable", key: trusted, want: "before the newest"}, {name: "an exact pin to an older release", accepted: statement("2.1.0", jan2), statement: statement("2.0.0", jan1), tag: "v2.0.0", key: trusted}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { t.Setenv("XDG_STATE_HOME", t.TempDir()) // One registry, so the freshness mark for the tag is shared; it // serves the accepted statement first, then the one under test. var current http.Handler server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { current.ServeHTTP(w, r) })) defer server.Close() source := distribution.Source{Reference: strings.TrimPrefix(server.URL, "http://") + "/archie", Key: tc.key} if tc.accepted != nil { current = fakeRegistry(t, "archie", "stable", signer, tc.accepted, archive, archive) if _, err := Fetch(t.Context(), distribution.Source{Reference: source.Reference, Key: trusted}, "stable"); err != nil { t.Fatalf("accept the newer release: %v", err) } } current = fakeRegistry(t, "archie", tc.tag, signer, tc.statement, archive, archive) _, err := Fetch(t.Context(), source, tc.tag) switch { case tc.want == "" && err != nil: t.Fatalf("Fetch: %v", err) case tc.want != "" && (err == nil || !strings.Contains(err.Error(), tc.want)): t.Fatalf("Fetch error = %v, want %q", err, tc.want) } }) } }