Null-DRM-Official/apps/streamd/internal/modstore/modstore_test.go
404errordeveloper 8fc7e4c0b8 streamd: serve start/stop/update, streamd.yaml, egress, module panel fixes
- Add streamd serve start|stop|restart|status|update with auto-written streamd.yaml
- Panel modules: STREAMD_MODULES_DIR for describe, YAML channel fallback, textarea value fix
- Per-stream WireGuard/SOCKS egress profiles + Settings UI
- Drop drm-* from release builds; track streamd-bg.sh / streamd-update.sh wrappers
- Docs: streamd-server-setup lifecycle and update flow
2026-10-10 18:05:51 +02:00

170 lines
4.3 KiB
Go

package modstore
import (
"archive/zip"
"bytes"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func makeZip(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 TestInstallZipInfersNameNotArchDir(t *testing.T) {
// Flat release zips list .bin/darwin-arm64/ before the binary; that arch
// folder must not become the module name.
root := t.TempDir()
s := New(root)
zipBytes := makeZip(t, map[string][]byte{
"module.yaml": []byte("package: air.RTE\nchannels:\n rteone: {label: One}\n"),
".bin/": nil, // directory markers via path only
".bin/darwin-arm64/rte": []byte("mac-bin"),
".bin/linux-amd64/rte": []byte("linux-bin"),
".bin/windows-amd64/rte.exe": []byte("win-bin"),
})
info, err := s.InstallZip(zipBytes, "")
if err != nil {
t.Fatalf("InstallZip: %v", err)
}
if info.Name != "rte" {
t.Fatalf("inferred name=%q want rte", info.Name)
}
}
func TestInstallZipFlatAndYAMLOnly(t *testing.T) {
root := t.TempDir()
s := New(root)
arch := runtime.GOOS + "-" + runtime.GOARCH
binName := "demo"
if runtime.GOOS == "windows" {
binName += ".exe"
}
zipBytes := makeZip(t, map[string][]byte{
"module.yaml": []byte("package: com.example\nchannels: {}\n"),
".bin/" + arch + "/" + binName: []byte("fake-binary"),
".bin/linux-amd64/demo": []byte("linux-bin"),
})
info, err := s.InstallZip(zipBytes, "demo")
if err != nil {
t.Fatalf("InstallZip: %v", err)
}
if info.Name != "demo" || !info.HasYAML || !info.HasPackage {
t.Fatalf("info = %+v", info)
}
if info.PackageSHA256 == "" || info.YAMLSHA256 == "" {
t.Fatalf("missing hashes: %+v", info)
}
pkgHash := info.PackageSHA256
yamlHash := info.YAMLSHA256
// Host binary should be promoted.
promoted := filepath.Join(root, "demo", ".bin", binName)
if !fileExists(promoted) {
t.Fatalf("expected promoted binary at %s", promoted)
}
// YAML-only update must not change package hash.
info2, err := s.WriteYAML("demo", []byte("package: com.example\nchannels:\n one: {label: One}\n"))
if err != nil {
t.Fatalf("WriteYAML: %v", err)
}
if info2.PackageSHA256 != pkgHash {
t.Fatalf("package hash changed on yaml edit: %s -> %s", pkgHash, info2.PackageSHA256)
}
if info2.YAMLSHA256 == yamlHash {
t.Fatal("yaml hash should change after edit")
}
raw, err := s.ReadYAML("demo")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(raw), "one:") {
t.Fatalf("yaml not updated: %s", raw)
}
f, size, err := s.OpenPackage("demo")
if err != nil {
t.Fatal(err)
}
defer f.Close()
if size != int64(len(zipBytes)) {
t.Fatalf("package size %d want %d", size, len(zipBytes))
}
}
func TestInstallZipTopLevelFolder(t *testing.T) {
root := t.TempDir()
s := New(root)
zipBytes := makeZip(t, map[string][]byte{
"bbc/module.yaml": []byte("package: bbc.iplayer.android\n"),
"bbc/.bin/linux-amd64/bbc": []byte("bin"),
"bbc/.bin/windows-amd64/bbc.exe": []byte("winbin"),
})
info, err := s.InstallZip(zipBytes, "")
if err != nil {
t.Fatalf("InstallZip: %v", err)
}
if info.Name != "bbc" {
t.Fatalf("name=%q", info.Name)
}
list, err := s.List()
if err != nil || len(list) != 1 || list[0].Name != "bbc" {
t.Fatalf("list=%v err=%v", list, err)
}
}
func TestZipSlipRejected(t *testing.T) {
root := t.TempDir()
s := New(root)
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
w, err := zw.Create("../evil.yaml")
if err != nil {
t.Fatal(err)
}
_, _ = w.Write([]byte("x"))
_ = zw.Close()
_, err = s.InstallZip(buf.Bytes(), "demo")
if err == nil {
t.Fatal("expected zip-slip error")
}
}
func TestDelete(t *testing.T) {
root := t.TempDir()
s := New(root)
zipBytes := makeZip(t, map[string][]byte{
"module.yaml": []byte("package: x\n"),
})
if _, err := s.InstallZip(zipBytes, "xmod"); err != nil {
t.Fatal(err)
}
if err := s.Delete("xmod"); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(root, "xmod")); !os.IsNotExist(err) {
t.Fatalf("dir still present: %v", err)
}
}