All checks were successful
release / build-and-upload (push) Successful in 7m5s
- Rename product/host binary to streamd (drm compat copy still shipped) - Go module path streamd; STREAMD_* env aliases; agent UA streamd-agent - Panel Modules store + agent pull sync (modstore / modsync) - Rolling stable + beta download tags; README/docs channel curl blocks - Release notes in docs/releases/v0.2.0.md attached by Forgejo Actions
154 lines
3.9 KiB
Go
154 lines
3.9 KiB
Go
package modsync
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"streamd/apps/agent/internal/client"
|
|
)
|
|
|
|
func TestPullPackageThenYAMLOnly(t *testing.T) {
|
|
arch := runtime.GOOS + "-" + runtime.GOARCH
|
|
binName := "demo"
|
|
if runtime.GOOS == "windows" {
|
|
binName += ".exe"
|
|
}
|
|
zip1 := mustZip(t, map[string][]byte{
|
|
"module.yaml": []byte("package: a\n"),
|
|
".bin/" + arch + "/" + binName: []byte("bin-v1"),
|
|
})
|
|
sum1 := sha256.Sum256(zip1)
|
|
pkgSHA1 := hex.EncodeToString(sum1[:])
|
|
yaml1SHA := sha256Hex([]byte("package: a\n"))
|
|
|
|
zip2 := mustZip(t, map[string][]byte{
|
|
"module.yaml": []byte("package: a\n"),
|
|
".bin/" + arch + "/" + binName: []byte("bin-v2"),
|
|
})
|
|
sum2 := sha256.Sum256(zip2)
|
|
pkgSHA2 := hex.EncodeToString(sum2[:])
|
|
|
|
yamlEdited := []byte("package: a\nchannels: {}\n")
|
|
yamlEditedSHA := sha256Hex(yamlEdited)
|
|
|
|
state := struct {
|
|
pkg []byte
|
|
pkgSHA string
|
|
yaml []byte
|
|
yamlSHA string
|
|
}{pkg: zip1, pkgSHA: pkgSHA1, yaml: []byte("package: a\n"), yamlSHA: yaml1SHA}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.URL.Path == "/api/modules" && r.Method == http.MethodGet:
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"modules": []map[string]any{{
|
|
"name": "demo",
|
|
"has_yaml": true,
|
|
"yaml_sha256": state.yamlSHA,
|
|
"has_package": true,
|
|
"package_sha256": state.pkgSHA,
|
|
}},
|
|
})
|
|
case r.URL.Path == "/api/modules/demo/package":
|
|
w.Header().Set("Content-Type", "application/zip")
|
|
_, _ = w.Write(state.pkg)
|
|
case r.URL.Path == "/api/modules/demo/yaml":
|
|
w.Header().Set("Content-Type", "text/yaml")
|
|
_, _ = w.Write(state.yaml)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
|
|
dir := t.TempDir()
|
|
api := &client.Client{BaseURL: srv.URL, HTTP: srv.Client()}
|
|
s := &Syncer{API: api, ModulesDir: dir}
|
|
|
|
if err := s.Once(); err != nil {
|
|
t.Fatalf("first sync: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "demo", "module.yaml")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
promoted := filepath.Join(dir, "demo", ".bin", binName)
|
|
if b, err := os.ReadFile(promoted); err != nil || string(b) != "bin-v1" {
|
|
t.Fatalf("promoted binary: %v %q", err, b)
|
|
}
|
|
|
|
// YAML-only change on panel.
|
|
state.yaml = yamlEdited
|
|
state.yamlSHA = yamlEditedSHA
|
|
if err := s.Once(); err != nil {
|
|
t.Fatalf("yaml sync: %v", err)
|
|
}
|
|
raw, _ := os.ReadFile(filepath.Join(dir, "demo", "module.yaml"))
|
|
if !bytes.Equal(raw, yamlEdited) {
|
|
t.Fatalf("yaml not updated: %s", raw)
|
|
}
|
|
if b, _ := os.ReadFile(promoted); string(b) != "bin-v1" {
|
|
t.Fatalf("binary should be unchanged after yaml-only sync, got %q", b)
|
|
}
|
|
|
|
// Package change.
|
|
state.pkg = zip2
|
|
state.pkgSHA = pkgSHA2
|
|
state.yaml = []byte("package: a\n")
|
|
state.yamlSHA = yaml1SHA
|
|
if err := s.Once(); err != nil {
|
|
t.Fatalf("package sync: %v", err)
|
|
}
|
|
if b, _ := os.ReadFile(promoted); string(b) != "bin-v2" {
|
|
t.Fatalf("binary not updated: %q", b)
|
|
}
|
|
}
|
|
|
|
func mustZip(t *testing.T, files map[string][]byte) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
for name, body := range files {
|
|
w, err := zw.Create(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := w.Write(body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func sha256Hex(b []byte) string {
|
|
sum := sha256.Sum256(b)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func TestUnzipSlip(t *testing.T) {
|
|
dir := t.TempDir()
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
w, _ := zw.Create("../evil")
|
|
_, _ = w.Write([]byte("x"))
|
|
_ = zw.Close()
|
|
zipPath := filepath.Join(dir, "bad.zip")
|
|
_ = os.WriteFile(zipPath, buf.Bytes(), 0o644)
|
|
err := unzipSafe(zipPath, filepath.Join(dir, "out"))
|
|
if err == nil || !strings.Contains(err.Error(), "slip") {
|
|
t.Fatalf("want zip slip error, got %v", err)
|
|
}
|
|
}
|