Files
zapret-autotune/cmd/zapret-autotune/managed_googlevideo_samples_test.go
T

66 lines
2.0 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"testing"
"time"
)
func TestManagedGooglevideoFreshPrivateSamples(t *testing.T) {
ids := []string{"dQw4w9WgXcQ", "aqz-KE-bpKQ", "jNQXAC9IVRw"}
hosts := []string{"rr1---sn-alpha.googlevideo.com", "rr2---sn-beta.googlevideo.com", "rr5---sn-gamma.googlevideo.com"}
for _, scenario := range []string{"diverse", "duplicate", "expired", "sabr"} {
t.Run(scenario, func(t *testing.T) {
calls := 0
collect := managedGooglevideoSamples(ids, func(_ context.Context, id string) ([]byte, error) {
i := calls % len(ids)
calls++
if id != ids[i] {
t.Fatal("configured ID changed", id)
}
if scenario == "sabr" {
return []byte(`{"playabilityStatus":{"status":"OK"},"streamingData":{"serverAbrStreamingUrl":"private-secret"}}`), nil
}
host := hosts[i]
if scenario == "duplicate" {
host = hosts[0]
}
expires := time.Now().Add(time.Hour).Unix()
if scenario == "expired" {
expires = time.Now().Unix() + 30
}
url, _ := json.Marshal(fmt.Sprintf("https://%s/videoplayback?expire=%d&sig=private-secret", host, expires))
return []byte(fmt.Sprintf(`{"playabilityStatus":{"status":"OK"},"streamingData":{"adaptiveFormats":[{"mimeType":"video/mp4","url":%s}]}}`, url)), nil
})
for n := 0; n < 2; n++ {
resources, err := collect(context.Background())
if scenario != "diverse" {
var insufficient *InsufficientAdaptiveSamples
if !errors.As(err, &insufficient) || strings.Contains(err.Error(), "private-secret") || len(resources) != 0 {
t.Fatal("unresolved samples accepted or leaked", resources, err)
}
continue
}
var actual []string
for _, r := range resources {
actual = append(actual, r.host)
if !strings.Contains(r.url, "sig=private-secret") {
t.Fatal("signed URL changed")
}
}
if err != nil || !reflect.DeepEqual(actual, hosts) {
t.Fatal(actual, err)
}
}
if calls != 2*len(ids) {
t.Fatal("samples were reused", calls)
}
})
}
}