Files
ZeusinaandGPT-6 Bot f4853173b3 feat: extract versioned YouTube service adapter
Wire pure Player parsing and strict qualification planning through a compiled-in kind/version registry. Fail closed before discovery requests when the required module is absent or mismatched.

Require sealed module-bound v2 evidence for new writes while preserving fresh trusted v1 apply eligibility and historical rollback. Keep legacy SOCKS5h routing, separate suffix/asset policy and public configuration unchanged; generic controller cutover and field acceptance remain pending.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-15 16:23:54 +03:00

91 lines
3.6 KiB
Go

package youtube
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
)
func TestDiscoverResponse(t *testing.T) {
m := Module{}
resources, err := m.DiscoverResponse([]byte(`{"playabilityStatus":{"status":"OK"},"streamingData":{"formats":[{"url":"https://rr1.unit.googlevideo.com/videoplayback?sig=secret","mimeType":"audio/mp4"}],"adaptiveFormats":[{"url":"https://rr2.unit.googlevideo.com/videoplayback?sig=secret","mimeType":"video/mp4; codecs=avc1"}]}}`))
if err != nil || len(resources) != 2 || resources[0].Kind != "audio/mp4" || resources[1].Kind != "video/mp4" {
t.Fatal(resources, err)
}
resources, err = m.DiscoverResponse([]byte(`{"playabilityStatus":{"status":"OK"},"streamingData":{"serverAbrStreamingUrl":"https://rr1.unit.googlevideo.com/sabr?sig=secret"}}`))
if err != nil || len(resources) != 0 {
t.Fatal("SABR fabricated ordinary readiness")
}
for _, body := range []string{"{", strings.Repeat(" ", (1<<20)+1), `{"playabilityStatus":{"status":"ERROR"}}`} {
if _, err := m.DiscoverResponse([]byte(body)); err == nil || strings.Contains(err.Error(), "secret") {
t.Fatal("invalid metadata accepted or disclosed")
}
}
}
func TestQualificationPlan(t *testing.T) {
m, now := Module{}, time.Unix(1800000000, 0)
if m.Binding().PolicySHA256 != "2cfc521a9784c574ceee16430aa485b9c0e9e64795486a23df5117b6265d89d5" {
t.Fatal("immutable youtube/1 policy changed: introduce a new version")
}
hosts := []string{"rr1---sn-alpha.googlevideo.com", "rr2---sn-beta.googlevideo.com", "rr5---sn-gamma.googlevideo.com"}
var resources []Resource
for _, host := range hosts {
resources = append(resources, Resource{URL: fmt.Sprintf("https://%s/videoplayback?expire=%d&sig=secret", host, now.Unix()+121), Host: host, Kind: "video/mp4"})
}
p, err := m.PlanQualification(hosts, resources, now)
if err != nil || strings.Join(p.Hosts, ",") != strings.Join(hosts, ",") || p.Protocols != [2]string{"tls12", "tls13"} || p.Samples != 3 || p.MinBytes != 1024 || p.WarmupLimit != 10 || p.WarmupTimeout != 120*time.Second {
t.Fatal(p, err)
}
for _, mode := range []string{"expired", "unsigned", "sabr", "missing", "diversity"} {
t.Run(mode, func(t *testing.T) {
r, h := append([]Resource(nil), resources...), append([]string(nil), hosts...)
switch mode {
case "expired":
r[0].URL = strings.Replace(r[0].URL, "1800000121", "1800000120", 1)
case "unsigned":
r[0].URL = strings.Split(r[0].URL, "&sig=")[0]
case "sabr":
r[0].URL = strings.Replace(r[0].URL, "videoplayback", "sabr", 1)
case "missing":
r = r[:2]
case "diversity":
h[1] = h[0]
}
if _, err := m.PlanQualification(h, r, now); err == nil {
t.Fatal("invalid sample accepted")
}
})
}
encoded, err := json.Marshal(struct {
Resources []Resource
Plan Plan
}{resources, p})
if err != nil || strings.Contains(string(encoded), "secret") || strings.Contains(string(encoded), hosts[0]) {
t.Fatal("ephemeral data serialized", string(encoded), err)
}
}
func TestSignedSampleFresh(t *testing.T) {
now := time.Unix(1800000000, 900000000)
for _, tc := range []struct {
query string
want bool
}{
{"expire=1800000121&sig=secret", true},
{"expire=1800000120&sig=secret", false},
{"expire=1800000121&signature=secret", true},
{"expire=1800000121&lsig=secret", true},
{"expire=1800000121", false},
{"expire=invalid&sig=secret", false},
{"expire=9223372036854775808&sig=secret", false},
} {
r := Resource{URL: "https://rr1---sn-alpha.googlevideo.com/videoplayback?" + tc.query, Kind: "video/mp4"}
if SignedSampleFresh(r, now, 120*time.Second) != tc.want {
t.Fatal("signed expiry boundary changed")
}
}
}