package release import ( "context" "crypto/ed25519" "crypto/sha256" "encoding/hex" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "testing" ) // TestApplyInstallsAVerifiedRelease is the whole path a gateway takes: fetch a signed manifest, // refuse anything older, verify the digest, stage the binary, keep the previous one. func TestApplyInstallsAVerifiedRelease(t *testing.T) { public, private, err := ed25519.GenerateKey(nil) if err != nil { t.Fatal(err) } newBinary := []byte("#!/bin/sh\necho 0.4.0\n") sum := sha256.Sum256(newBinary) mux2 := http.NewServeMux() mux2.HandleFunc("/manifest.json", func(w http.ResponseWriter, r *http.Request) { manifest := Manifest{Version: "0.4.0", Artifacts: []Artifact{{ OS: "linux", Arch: "amd64", URL: "http://" + r.Host + "/binary", SHA256: hex.EncodeToString(sum[:]), Size: int64(len(newBinary)), }}} body, err := json.Marshal(manifest) if err != nil { t.Error(err) return } envelope, _ := json.Marshal(Signed{Manifest: body, Signature: hex.EncodeToString(ed25519.Sign(private, body))}) _, _ = w.Write(envelope) }) mux2.HandleFunc("/binary", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(newBinary) }) server2 := httptest.NewServer(mux2) defer server2.Close() // The artifact declares linux/amd64; tests run on amd64 here, and Choose is what would refuse // another platform, so the test is explicit about it. dir := t.TempDir() path := filepath.Join(dir, "nostrino-gateway") if err := os.WriteFile(path, []byte("#!/bin/sh\necho 0.3.0\n"), 0o755); err != nil { t.Fatal(err) } result, err := Apply(context.Background(), Options{ ManifestURL: server2.URL + "/manifest.json", PublicKey: public, Path: path, Current: "0.3.0", }) if err != nil { t.Skipf("this platform has no artifact in the test manifest: %v", err) } if result.Version != "0.4.0" { t.Fatalf("installed %q", result.Version) } installed, err := os.ReadFile(path) if err != nil || string(installed) != string(newBinary) { t.Fatalf("installed %q, %v", installed, err) } // The same release again is a no-op that refuses: a gateway must not restart for nothing. if _, err := Apply(context.Background(), Options{ ManifestURL: server2.URL + "/manifest.json", PublicKey: public, Path: path, Current: "0.4.0", }); err == nil { t.Fatal("a reinstall of the same version was accepted") } }