package db import "testing" const ( testKID = "0123456789abcdef0123456789abcdef" testKey = "fedcba9876543210fedcba9876543210" ) var goodKey = testKID + ":" + testKey // withDerived runs the same derivation scanStream does, so these tests // exercise exactly what the API serialises. func withDerived(st Stream) Stream { st.fillDerived() return st } func TestNeedsCaptureDisabledStreamNever(t *testing.T) { st := withDerived(Stream{Enabled: false, MPD: "", Health: "down"}) if st.NeedsCapture { t.Error("a disabled stream must never be queued for capture") } } func TestNeedsCaptureMissingManifest(t *testing.T) { st := withDerived(Stream{Enabled: true, MPD: "", Health: "ok"}) if !st.NeedsCapture { t.Error("a stream with no manifest needs a capture") } } // The regression the key redaction would otherwise cause: a healthy encrypted // stream must not look like it needs a capture just because the API response // no longer carries the key. The server decides, using the derived flags. func TestNeedsCaptureHealthyDRMStream(t *testing.T) { st := withDerived(Stream{ Enabled: true, MPD: "https://x/manifest.mpd", Key: goodKey, Health: "ok", PlaylistAgeS: 2, }) if !st.DRMRequired { t.Error("a stream holding a key should resolve to DRM required") } if !st.KeyValid { t.Error("a well-formed key should be valid") } if st.NeedsCapture { t.Error("a healthy DRM stream must not be queued for capture") } // And after redaction the verdict is unchanged and the key is gone. red := st.Redacted() if red.Key != "" || red.HeadersJSON != "" || red.Proxy != "" { t.Errorf("Redacted left secrets behind: %+v", red) } if red.NeedsCapture != st.NeedsCapture || red.HasKey != true || red.KeyValid != true { t.Error("redaction must preserve the derived flags") } } // A clear stream legitimately has no keys. streamd supports clear content, so // this must not spin the capture queue forever — the bug the old // "key == empty means capture" rule caused. func TestNeedsCaptureClearStreamWithNoKey(t *testing.T) { st := withDerived(Stream{ Enabled: true, MPD: "https://x/manifest.mpd", Key: "", DRMMode: DRMNo, Health: "ok", PlaylistAgeS: 2, }) if st.DRMRequired { t.Error("drm_mode=no must resolve to DRM not required") } if st.NeedsCapture { t.Error("a healthy clear stream must not be queued for capture") } } // In auto mode a stream that has never held a key is assumed clear. func TestNeedsCaptureAutoModeInfersFromKey(t *testing.T) { clear := withDerived(Stream{Enabled: true, MPD: "https://x/a.mpd", Health: "ok"}) if clear.DRMRequired { t.Error("auto with no key should infer clear") } if clear.DRMMode != DRMAuto { t.Errorf("auto should be normalised to %q, got %q", DRMAuto, clear.DRMMode) } encrypted := withDerived(Stream{Enabled: true, MPD: "https://x/a.mpd", Key: goodKey, Health: "ok"}) if !encrypted.DRMRequired { t.Error("auto with a key should infer DRM") } } func TestNeedsCaptureInvalidKey(t *testing.T) { st := withDerived(Stream{ Enabled: true, MPD: "https://x/a.mpd", Key: "garbage", DRMMode: DRMYes, Health: "ok", }) if st.KeyValid { t.Error("garbage should not be a valid key") } if !st.NeedsCapture { t.Error("a DRM stream with an unusable key needs a capture") } } // An on-demand stream sitting idle is doing its job, not failing. Treating // "stopped" as broken would queue a capture for every idle channel. func TestNeedsCaptureOnDemandIdleIsNotBroken(t *testing.T) { st := withDerived(Stream{ Enabled: true, MPD: "https://x/a.mpd", Key: goodKey, OnDemand: true, Health: "stopped", }) if st.NeedsCapture { t.Error("an idle on-demand stream must not be queued for capture") } // The same stream that is not on-demand is genuinely down. always := withDerived(Stream{ Enabled: true, MPD: "https://x/a.mpd", Key: goodKey, OnDemand: false, Health: "stopped", }) if !always.NeedsCapture { t.Error("an always-on stream that is stopped should be refreshed") } } // A stream still inside its bootstrap window must be left alone: capturing // would POST credentials and restart it, which is a slow restart loop. func TestNeedsCaptureStartingRespectsBootstrapGrace(t *testing.T) { young := withDerived(Stream{ Enabled: true, MPD: "https://x/a.mpd", Key: goodKey, Health: "starting", UptimeS: 10, }) if young.NeedsCapture { t.Error("a stream still starting up must not be restarted by a capture") } stuck := withDerived(Stream{ Enabled: true, MPD: "https://x/a.mpd", Key: goodKey, Health: "starting", UptimeS: bootstrapGraceS + 1, }) if !stuck.NeedsCapture { t.Error("a stream stuck in starting past the grace window needs help") } } func TestNeedsCaptureStalePlaylist(t *testing.T) { st := withDerived(Stream{ Enabled: true, MPD: "https://x/a.mpd", Key: goodKey, Health: "ok", PlaylistAgeS: 120, }) if !st.NeedsCapture { t.Error("a stale playlist should trigger a refresh") } } func TestDerivedHeaderAndProxyFlags(t *testing.T) { for _, c := range []struct { headers string want bool }{ {"", false}, {"{}", false}, {"null", false}, {`{"Cookie":"x"}`, true}, } { st := withDerived(Stream{HeadersJSON: c.headers}) if st.HasHeaders != c.want { t.Errorf("HasHeaders(%q) = %v want %v", c.headers, st.HasHeaders, c.want) } } st := withDerived(Stream{Proxy: "socks5://127.0.0.1:1080"}) if !st.HasProxy { t.Error("a configured proxy should set HasProxy") } }