package release import ( "context" "crypto/ed25519" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "net/http" "net/http/httptest" "os" "path/filepath" "testing" ) func signWith(t *testing.T, private ed25519.PrivateKey, manifest Manifest) []byte { t.Helper() body, err := json.Marshal(manifest) if err != nil { t.Fatal(err) } envelope, err := json.Marshal(Signed{Manifest: body, Signature: hex.EncodeToString(ed25519.Sign(private, body))}) if err != nil { t.Fatal(err) } return envelope } func TestASignedManifestIsAcceptedAndATamperedOneIsNot(t *testing.T) { public, private, err := ed25519.GenerateKey(nil) if err != nil { t.Fatal(err) } envelope := signWith(t, private, Manifest{Version: "0.3.0", Artifacts: []Artifact{{OS: "linux", Arch: "amd64", URL: "https://x", SHA256: "00"}}}) manifest, _, err := Verify(envelope, public) if err != nil { t.Fatal(err) } if manifest.Version != "0.3.0" { t.Fatalf("version %q", manifest.Version) } // A different manifest, signed by the same key, verifies; the same bytes under another key do // not. other := signWith(t, private, Manifest{Version: "0.3.1"}) if _, _, err := Verify(other, public); err != nil { t.Fatal("a freshly signed manifest failed:", err) } if _, _, err := Verify(envelope, ed25519.PublicKey(make([]byte, 32))); !errors.Is(err, ErrSignature) { t.Fatalf("err %v, want ErrSignature", err) } } func TestAnArtifactMustMatchItsDigest(t *testing.T) { body := []byte("#!/bin/sh\necho hi\n") sum := sha256.Sum256(body) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(body) })) defer server.Close() good := Artifact{URL: server.URL, SHA256: hex.EncodeToString(sum[:])} if got, err := Download(context.Background(), nil, good); err != nil || string(got) != string(body) { t.Fatalf("download: %v", err) } bad := good bad.SHA256 = "00" if _, err := Download(context.Background(), nil, bad); !errors.Is(err, ErrDigest) { t.Fatalf("err %v, want ErrDigest", err) } // A size mismatch is also a digest failure, even when the hash was right. short := good short.Size = int64(len(body) + 1) if _, err := Download(context.Background(), nil, short); !errors.Is(err, ErrDigest) { t.Fatalf("err %v, want ErrDigest", err) } } func TestInstallKeepsThePreviousBinaryAndRollsBack(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "nostrino-gateway") if err := os.WriteFile(path, []byte("old"), 0o755); err != nil { t.Fatal(err) } previous, err := Install(path, []byte("new")) if err != nil { t.Fatal(err) } installed, err := os.ReadFile(path) if err != nil || string(installed) != "new" { t.Fatalf("installed %q, %v", installed, err) } if kept, err := os.ReadFile(previous); err != nil || string(kept) != "old" { t.Fatalf("previous %q, %v", kept, err) } if err := Rollback(path); err != nil { t.Fatal(err) } if rolled, err := os.ReadFile(path); err != nil || string(rolled) != "old" { t.Fatalf("rolled back %q, %v", rolled, err) } } func TestADowngradeIsRefused(t *testing.T) { if err := Newer("0.3.0", Manifest{Version: "0.3.0"}); !errors.Is(err, ErrVersion) { t.Fatalf("same version: %v", err) } if err := Newer("0.3.0", Manifest{Version: "0.2.9"}); !errors.Is(err, ErrVersion) { t.Fatalf("older version: %v", err) } if err := Newer("0.3.0", Manifest{Version: "0.3.10"}); err != nil { t.Fatalf("newer version: %v", err) } if Compare("0.2.10", "0.2.9") <= 0 { t.Fatal("0.2.10 must sort above 0.2.9") } if err := Newer("0.3.0", Manifest{Version: "0.4.0", MinVersion: "0.3.5"}); !errors.Is(err, ErrVersion) { t.Fatalf("floor: %v", err) } } // TestAnIndentedEnvelopeIsNotVerifiable documents the trap that signing and pretty-printing share: // the signature covers the manifest bytes as they appear in the envelope, so re-spacing them makes // the release unverifiable. Manifests are published compact for this reason. func TestAnIndentedEnvelopeIsNotVerifiable(t *testing.T) { public, private, err := ed25519.GenerateKey(nil) if err != nil { t.Fatal(err) } body, err := json.Marshal(Manifest{Version: "0.4.0"}) if err != nil { t.Fatal(err) } envelope, err := json.Marshal(Signed{Manifest: body, Signature: hex.EncodeToString(ed25519.Sign(private, body))}) if err != nil { t.Fatal(err) } var parsed Signed if err := json.Unmarshal(envelope, &parsed); err != nil { t.Fatal(err) } indented, err := json.MarshalIndent(parsed, "", " ") if err != nil { t.Fatal(err) } if _, _, err := Verify(indented, public); !errors.Is(err, ErrSignature) { t.Fatalf("err %v, want ErrSignature: an indented envelope must not verify", err) } }