-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsidecar_test.go
More file actions
749 lines (704 loc) · 26.6 KB
/
Copy pathsidecar_test.go
File metadata and controls
749 lines (704 loc) · 26.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package waxtap
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/colespringer/waxtap/v3/potoken"
)
func TestRedactURL(t *testing.T) {
cases := []struct {
in string
want string
}{
{"http://127.0.0.1:4416/player-context", "http://127.0.0.1:4416/player-context"},
{"http://user:pass@host:4416/p?token=secret#frag", "http://host:4416/p"},
{"https://example.com/path?a=b&c=d", "https://example.com/path"},
}
for _, tc := range cases {
if got := redactURL(tc.in); got != tc.want {
t.Errorf("redactURL(%q) = %q, want %q", tc.in, got, tc.want)
}
}
// Invalid URLs are replaced rather than echoed.
if got := redactURL("http://h/%zz"); got != "<unparseable-url>" {
t.Errorf("redactURL(bad) = %q, want <unparseable-url>", got)
}
}
func TestBuildSidecarURL(t *testing.T) {
cases := []struct {
base, def, want string
}{
// A base with no path (or just "/") gets the default path appended.
{"http://127.0.0.1:4417", "/get_pot", "http://127.0.0.1:4417/get_pot"},
{"http://127.0.0.1:4417/", "/get_pot", "http://127.0.0.1:4417/get_pot"},
// A full endpoint URL is preserved.
{"http://host:4417/session", "/session", "http://host:4417/session"},
{"http://host:4417/custom", "/get_pot", "http://host:4417/custom"},
// Existing query parameters are retained.
{"http://host:4417?key=K", "/get_pot", "http://host:4417/get_pot?key=K"},
{"https://host/session?key=K", "/session", "https://host/session?key=K"},
}
for _, tc := range cases {
got, err := buildSidecarURL(tc.base, tc.def)
if err != nil {
t.Errorf("buildSidecarURL(%q,%q) error: %v", tc.base, tc.def, err)
continue
}
if got != tc.want {
t.Errorf("buildSidecarURL(%q,%q) = %q, want %q", tc.base, tc.def, got, tc.want)
}
}
// Non-HTTP and unparseable bases are rejected so a misconfiguration surfaces.
for _, bad := range []string{"", "ftp://host/x", "not-a-url", "http://%zz"} {
if _, err := buildSidecarURL(bad, "/get_pot"); err == nil {
t.Errorf("buildSidecarURL(%q) = nil error, want a validation error", bad)
}
}
}
// TestNewSidecarProvidersRejectBadURL confirms the public constructors surface a
// URL validation error instead of returning an unusable provider.
func TestNewSidecarProvidersRejectBadURL(t *testing.T) {
if _, err := NewSidecarPOTokenProvider("ftp://host/x"); err == nil {
t.Error("NewSidecarPOTokenProvider(bad) = nil error, want a validation error")
}
if _, err := NewSidecarPlayerContextProvider("not-a-url"); err == nil {
t.Error("NewSidecarPlayerContextProvider(bad) = nil error, want a validation error")
}
if _, err := NewSidecarSessionProvider(""); err == nil {
t.Error("NewSidecarSessionProvider(empty) = nil error, want a validation error")
}
}
func TestCapRunes(t *testing.T) {
if got := capRunes("short", 10); got != "short" {
t.Errorf("capRunes short = %q", got)
}
if got := capRunes("abcdef", 3); got != "abc…" {
t.Errorf("capRunes truncate = %q, want abc…", got)
}
// Multibyte runes are not split.
if got := capRunes("héllo", 2); got != "hé…" {
t.Errorf("capRunes multibyte = %q, want hé…", got)
}
}
func TestSidecarReason(t *testing.T) {
if got := sidecarReason(strings.NewReader(`{"error":"bad scope"}`)); got != "bad scope" {
t.Errorf("reason from error field = %q", got)
}
if got := sidecarReason(strings.NewReader(`{"message":"try later"}`)); got != "try later" {
t.Errorf("reason from message field = %q", got)
}
// Non-JSON and fieldless bodies must not be echoed.
if got := sidecarReason(strings.NewReader("<html>secret token</html>")); got != "" {
t.Errorf("reason from HTML = %q, want empty (no leak)", got)
}
if got := sidecarReason(strings.NewReader(`{"other":"x"}`)); got != "" {
t.Errorf("reason from fieldless JSON = %q, want empty", got)
}
}
func TestBgutilProviderPlayerScope(t *testing.T) {
var gotBinding string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/get_pot" {
t.Errorf("path = %q, want /get_pot", r.URL.Path)
}
var req bgutilRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Fatal(err)
}
gotBinding = req.ContentBinding
_ = json.NewEncoder(w).Encode(bgutilResponse{POToken: "TOKEN-P", ExpiresAt: "2026-06-09T07:25:25Z"})
}))
defer srv.Close()
p, err := NewSidecarPOTokenProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
resp, err := p.ProvidePOToken(context.Background(), potoken.Request{
Scope: potoken.ScopePlayer,
VideoID: "vid123",
})
if err != nil {
t.Fatal(err)
}
if gotBinding != "vid123" {
t.Errorf("content_binding = %q, want vid123 (player scope binds to the video ID)", gotBinding)
}
if resp.Token != "TOKEN-P" {
t.Errorf("token = %q, want TOKEN-P", resp.Token)
}
if want := time.Date(2026, 6, 9, 7, 25, 25, 0, time.UTC); !resp.ExpiresAt.Equal(want) {
t.Errorf("expiresAt = %v, want %v (RFC3339)", resp.ExpiresAt, want)
}
}
// TestBgutilProviderSendsAPIKey verifies the X-API-Key header is sent only when a
// key is configured with WithSidecarAPIKey.
func TestBgutilProviderSendsAPIKey(t *testing.T) {
for _, key := range []string{"secret-key", ""} {
var gotKey string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotKey = r.Header.Get("X-API-Key")
_ = json.NewEncoder(w).Encode(bgutilResponse{POToken: "T"})
}))
p, err := NewSidecarPOTokenProvider(srv.URL, WithSidecarAPIKey(key))
if err != nil {
srv.Close()
t.Fatalf("key %q: %v", key, err)
}
_, err = p.ProvidePOToken(context.Background(),
potoken.Request{Scope: potoken.ScopePlayer, VideoID: "v"})
srv.Close()
if err != nil {
t.Fatalf("key %q: %v", key, err)
}
if gotKey != key {
t.Errorf("X-API-Key = %q, want %q", gotKey, key)
}
}
}
func TestBgutilProviderGVSScopeAndEpochExpiry(t *testing.T) {
var gotBinding string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req bgutilRequest
_ = json.NewDecoder(r.Body).Decode(&req)
gotBinding = req.ContentBinding
_ = json.NewEncoder(w).Encode(bgutilResponse{POToken: "TOKEN-G", ExpiresAt: "1812345925"})
}))
defer srv.Close()
p, err := NewSidecarPOTokenProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
resp, err := p.ProvidePOToken(context.Background(), potoken.Request{
Scope: potoken.ScopeGVS,
VisitorData: "VISITOR==",
})
if err != nil {
t.Fatal(err)
}
if gotBinding != "VISITOR==" {
t.Errorf("content_binding = %q, want the visitor data (GVS scope)", gotBinding)
}
if resp.Token != "TOKEN-G" {
t.Errorf("token = %q, want TOKEN-G", resp.Token)
}
if want := time.Unix(1812345925, 0).UTC(); !resp.ExpiresAt.Equal(want) {
t.Errorf("expiresAt = %v, want %v (epoch tolerated)", resp.ExpiresAt, want)
}
}
// TestBgutilProviderServerError verifies a non-200 becomes a SidecarResponseError
// carrying the HTTP status, which the CLI classifier maps to an exit code.
func TestBgutilProviderServerError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "no integrity token", http.StatusServiceUnavailable)
}))
defer srv.Close()
p, err := NewSidecarPOTokenProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
_, provErr := p.ProvidePOToken(context.Background(),
potoken.Request{Scope: potoken.ScopePlayer, VideoID: "v"})
if provErr == nil {
t.Fatal("expected an error on a non-200 response")
}
sre, ok := errors.AsType[*SidecarResponseError](provErr)
if !ok || sre.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("err = %v, want a SidecarResponseError with StatusCode 503", provErr)
}
}
// TestBgutilProviderMalformedJSONReason confirms a 200 with an undecodable body
// surfaces the decode detail (a structured json error, not raw body bytes) so a
// custom sidecar integration is debuggable, and classifies as an invalid 200
// (StatusCode 0).
func TestBgutilProviderMalformedJSONReason(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("<html>not json</html>"))
}))
defer srv.Close()
p, err := NewSidecarPOTokenProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
_, provErr := p.ProvidePOToken(context.Background(),
potoken.Request{Scope: potoken.ScopePlayer, VideoID: "v"})
sre, ok := errors.AsType[*SidecarResponseError](provErr)
if !ok {
t.Fatalf("err = %v, want a SidecarResponseError", provErr)
}
if sre.StatusCode != 0 {
t.Errorf("StatusCode = %d, want 0 (a 200 with invalid content)", sre.StatusCode)
}
if !strings.HasPrefix(sre.Reason, "malformed JSON response:") {
t.Errorf("Reason = %q, want the malformed-JSON prefix with the decode detail", sre.Reason)
}
}
func TestBgutilProviderBindingErrorsBeforeRequest(t *testing.T) {
// These must fail in contentBinding, before any HTTP call, so the unroutable
// address is never contacted.
p, err := NewSidecarPOTokenProvider("http://127.0.0.1:0/get_pot")
if err != nil {
t.Fatal(err)
}
cases := []potoken.Request{
{Scope: potoken.ScopePlayer}, // no video ID
{Scope: potoken.ScopeGVS}, // no visitor data
{Scope: potoken.ScopeSubtitles, VideoID: "v"}, // unsupported scope
}
for _, req := range cases {
if _, err := p.ProvidePOToken(context.Background(), req); err == nil {
t.Errorf("scope %s: expected an error before any request", req.Scope)
}
}
}
// TestSidecarClientDoesNotFollowRedirects confirms the dedicated client pins
// credentials to the endpoint: a 3xx becomes a SidecarResponseError instead of
// being chased to another host.
func TestSidecarClientDoesNotFollowRedirects(t *testing.T) {
var targetHits atomic.Int32
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
targetHits.Add(1)
_ = json.NewEncoder(w).Encode(bgutilResponse{POToken: "LEAKED"})
}))
defer target.Close()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target.URL+"/get_pot", http.StatusFound)
}))
defer srv.Close()
p, err := NewSidecarPOTokenProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
_, provErr := p.ProvidePOToken(context.Background(),
potoken.Request{Scope: potoken.ScopePlayer, VideoID: "v"})
if provErr == nil {
t.Fatal("expected an error: a redirect must not be followed to another host")
}
sre, ok := errors.AsType[*SidecarResponseError](provErr)
if !ok || sre.StatusCode != http.StatusFound {
t.Fatalf("err = %v, want a SidecarResponseError with StatusCode 302", provErr)
}
if n := targetHits.Load(); n != 0 {
t.Errorf("redirect target contacted %d times; credentials must stay bound to the endpoint", n)
}
}
const validPlayerContextJSON = `{
"playability_status": "OK",
"player_url": "https://www.youtube.com/s/player/444511ca/player_es6.vflset/en_US/base.js",
"server_abr_streaming_url": "https://rr3.googlevideo.com/videoplayback?n=SCRAMBLED&sabr=1",
"video_playback_ustreamer_config": "dXN0cmVhbWVy",
"visitor_data": "CgtWSVNJVE9S",
"client_version": "2.20260606.02.00",
"title": "Big Buck Bunny",
"author": "Blender",
"length_seconds": 634,
"audio_formats": [
{"itag":251,"lmt":"1719185012384481","xtags":"","mime_type":"audio/webm; codecs=\"opus\"","bitrate":143452,"audio_channels":2,"audio_sample_rate":48000,"content_length":9700000,"approx_duration_ms":634624}
],
"session_generation": 7
}`
func newPlayerContextServer(t *testing.T, status int, body string) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/player-context" {
t.Errorf("path = %q, want /player-context", r.URL.Path)
}
w.WriteHeader(status)
_, _ = w.Write([]byte(body))
}))
}
func TestPlayerContextProviderDecode(t *testing.T) {
srv := newPlayerContextServer(t, http.StatusOK, validPlayerContextJSON)
defer srv.Close()
p, err := NewSidecarPlayerContextProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
pc, err := p.ProvidePlayerContext(context.Background(), "aqz-KE-bpKQ")
if err != nil {
t.Fatalf("ProvidePlayerContext: %v", err)
}
if pc.ServerAbrURL == "" || pc.VisitorData != "CgtWSVNJVE9S" || pc.ClientVersion != "2.20260606.02.00" {
t.Errorf("decoded context = %+v", pc)
}
if pc.PlayerURL != "https://www.youtube.com/s/player/444511ca/player_es6.vflset/en_US/base.js" {
t.Errorf("player_url = %q", pc.PlayerURL)
}
if pc.Title != "Big Buck Bunny" || pc.LengthSeconds != 634 {
t.Errorf("metadata = title %q length %d", pc.Title, pc.LengthSeconds)
}
if len(pc.AudioFormats) != 1 {
t.Fatalf("audio formats = %d, want 1", len(pc.AudioFormats))
}
f := pc.AudioFormats[0]
if f.Itag != 251 || f.LMT != "1719185012384481" || f.ContentLength != 9700000 || f.AudioSampleRate != 48000 {
t.Errorf("format = %+v", f)
}
if pc.Generation != 7 {
t.Errorf("Generation = %d, want 7 (session_generation)", pc.Generation)
}
}
func TestPlayerContextProviderErrors(t *testing.T) {
cases := []struct {
name string
status int
body string
want string
}{
{"non-200", http.StatusInternalServerError, "boom", "returned"},
{"status not OK", http.StatusOK, `{"playability_status":"ERROR: bot check"}`, "playability_status"},
{"missing url", http.StatusOK, `{"playability_status":"OK","visitor_data":"v","audio_formats":[{"itag":251}]}`, "missing"},
{"missing visitor", http.StatusOK, `{"playability_status":"OK","server_abr_streaming_url":"u","audio_formats":[{"itag":251}]}`, "missing"},
{"no formats", http.StatusOK, `{"playability_status":"OK","server_abr_streaming_url":"u","visitor_data":"v"}`, "missing"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
srv := newPlayerContextServer(t, tc.status, tc.body)
defer srv.Close()
p, err := NewSidecarPlayerContextProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
_, provErr := p.ProvidePlayerContext(context.Background(), "v")
if provErr == nil {
t.Fatal("expected an error")
}
if !strings.Contains(provErr.Error(), tc.want) {
t.Errorf("err = %q, want substring %q", provErr.Error(), tc.want)
}
})
}
}
func TestParseNetscapeCookies(t *testing.T) {
// A real-world-ish file: header comment, a blank line, a normal cookie, an
// #HttpOnly_ cookie, a six-field empty-value cookie (trailing tab dropped by
// some exporters), and a too-short line that must be skipped.
const content = "# Netscape HTTP Cookie File\n" +
"\n" +
".youtube.com\tTRUE\t/\tFALSE\t1799999999\tPREF\tval123\n" +
"#HttpOnly_.youtube.com\tTRUE\t/\tTRUE\t0\tVISITOR_INFO1_LIVE\tabc\n" +
"# a comment line\n" +
".youtube.com\tTRUE\t/\tFALSE\t0\tEMPTYVAL\n" +
"too\tfew\tfields\n"
path := filepath.Join(t.TempDir(), "cookies.txt")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
cookies, err := ParseNetscapeCookies(path)
if err != nil {
t.Fatal(err)
}
if len(cookies) != 3 {
t.Fatalf("got %d cookies, want 3 (comment/under-six-field lines skipped, six-field empty-value kept): %+v", len(cookies), cookies)
}
pref := cookies[0]
if pref.Name != "PREF" || pref.Value != "val123" || pref.Domain != ".youtube.com" || pref.Path != "/" {
t.Errorf("PREF cookie = %+v", pref)
}
if pref.Secure {
t.Error("PREF secure flag should be FALSE")
}
if pref.Expires.IsZero() {
t.Error("PREF should have a non-zero expiry (unix 1799999999)")
}
vis := cookies[1]
if vis.Name != "VISITOR_INFO1_LIVE" || !vis.HttpOnly {
t.Errorf("#HttpOnly_ cookie not parsed as HttpOnly: %+v", vis)
}
if vis.Domain != ".youtube.com" {
t.Errorf("#HttpOnly_ domain = %q, want .youtube.com (prefix stripped)", vis.Domain)
}
if !vis.Secure {
t.Error("VISITOR_INFO1_LIVE secure flag should be TRUE")
}
if !vis.Expires.IsZero() {
t.Error("expiry 0 should be a session cookie (zero time)")
}
// A six-field line (empty value, trailing tab dropped) parses with an empty value.
empty := cookies[2]
if empty.Name != "EMPTYVAL" || empty.Value != "" {
t.Errorf("six-field cookie = %+v, want name EMPTYVAL with an empty value", empty)
}
if empty.Domain != ".youtube.com" || empty.Path != "/" {
t.Errorf("six-field cookie = %+v, want domain/path still parsed", empty)
}
}
func TestParseSessionExpiry(t *testing.T) {
if got := parseSessionExpiry([]byte(`1799999999`)); got.IsZero() {
t.Error("unix-seconds expiry should parse to a non-zero time")
}
if got := parseSessionExpiry([]byte(`"2030-01-02T03:04:05Z"`)); got.IsZero() {
t.Error("RFC3339 expiry should parse to a non-zero time")
}
for _, raw := range []string{``, `0`, `null`, `"garbage"`} {
if got := parseSessionExpiry([]byte(raw)); !got.IsZero() {
t.Errorf("parseSessionExpiry(%q) = %v, want zero (session cookie)", raw, got)
}
}
}
// TestHTTPSessionProvider covers the reference minter's snake_case /session shape
// (visitor_data, http_only, extra keys) plus a session cookie with no expires.
func TestHTTPSessionProvider(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
"visitor_data": "CgtBROWSER%3D%3D",
"user_agent": "Mozilla/5.0 ignored",
"client_version": "2.x",
"cookie_header": "ignored=1",
"cookies": [
{"name":"PREF","value":"p","domain":".youtube.com","path":"/","secure":true,"http_only":false,"expires":1799999999},
{"name":"YSC","value":"y","domain":".youtube.com","path":"/","secure":true,"http_only":true}
]
}`))
}))
defer srv.Close()
p, err := NewSidecarSessionProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
sess, err := p.ProvideSession(context.Background())
if err != nil {
t.Fatal(err)
}
if sess.VisitorData != "CgtBROWSER%3D%3D" {
t.Errorf("visitorData = %q, want the verbatim literal", sess.VisitorData)
}
if len(sess.Cookies) != 2 {
t.Fatalf("cookies = %d, want 2", len(sess.Cookies))
}
if sess.Cookies[0].Name != "PREF" || !sess.Cookies[0].Secure || sess.Cookies[0].Expires.IsZero() {
t.Errorf("PREF cookie = %+v", sess.Cookies[0])
}
if sess.Cookies[1].Name != "YSC" || !sess.Cookies[1].HttpOnly {
t.Errorf("YSC http_only not parsed: %+v", sess.Cookies[1])
}
}
// TestHTTPSessionProviderCamelCase confirms the documented camelCase variant is
// still accepted (visitorData/httpOnly), so the contract does not break on casing.
func TestHTTPSessionProviderCamelCase(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"visitorData":"CgtX%3D%3D","cookies":[{"name":"PREF","value":"p","domain":".youtube.com","httpOnly":true}]}`))
}))
defer srv.Close()
p, err := NewSidecarSessionProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
sess, err := p.ProvideSession(context.Background())
if err != nil {
t.Fatal(err)
}
if sess.VisitorData != "CgtX%3D%3D" || len(sess.Cookies) != 1 || !sess.Cookies[0].HttpOnly {
t.Errorf("camelCase not accepted: vd=%q cookies=%+v", sess.VisitorData, sess.Cookies)
}
}
func TestHTTPSessionProviderEmptyVisitorDataErrors(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"visitorData":"","cookies":[]}`))
}))
defer srv.Close()
p, err := NewSidecarSessionProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
if _, err := p.ProvideSession(context.Background()); err == nil {
t.Fatal("expected an error for an empty visitorData")
}
}
func TestHTTPSessionProviderRetriesOnceThenFails(t *testing.T) {
var hits int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
hits++
http.Error(w, "boom", http.StatusInternalServerError)
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
p, err := NewSidecarSessionProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
if _, err := p.ProvideSession(ctx); err == nil {
t.Fatal("expected failure after retries")
}
if hits != 2 {
t.Errorf("server hits = %d, want 2 (one retry)", hits)
}
}
// TestHTTPSessionProviderInvalidateSession covers the delivery-cap escape for an
// adopted session: the generation from /session is what gets reported, and the
// report goes to the /report sibling of the configured session endpoint.
func TestHTTPSessionProviderInvalidateSession(t *testing.T) {
var reportPath string
var reportBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/report" {
reportPath = r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&reportBody)
_, _ = w.Write([]byte(`{"accepted":true,"retired":true,"generation":7}`))
return
}
_, _ = w.Write([]byte(`{"visitor_data":"CgtX%3D%3D","session_generation":7,"cookies":[]}`))
}))
defer srv.Close()
// Configure the full session endpoint, not the base, to pin the sibling
// derivation rather than a lucky default-path append.
p, err := NewSidecarSessionProvider(srv.URL + "/session")
if err != nil {
t.Fatal(err)
}
sess, err := p.ProvideSession(context.Background())
if err != nil {
t.Fatal(err)
}
if sess.Generation != 7 {
t.Fatalf("Generation = %d, want 7", sess.Generation)
}
inv, ok := p.(potoken.SessionInvalidator)
if !ok {
t.Fatal("the sidecar session provider must implement potoken.SessionInvalidator")
}
if err := inv.InvalidateSession(context.Background(), potoken.SessionInvalidation{
Generation: sess.Generation,
VideoID: "dummyVideo0",
Reason: "delivery-cap",
}); err != nil {
t.Fatalf("InvalidateSession: %v", err)
}
if reportPath != "/report" {
t.Errorf("report path = %q, want /report", reportPath)
}
if got, _ := reportBody["session_generation"].(float64); got != 7 {
t.Errorf("session_generation = %v, want 7", reportBody["session_generation"])
}
if reportBody["video_id"] != "dummyVideo0" || reportBody["reason"] != "delivery-cap" {
t.Errorf("report body = %v, want the video and reason forwarded", reportBody)
}
}
// TestHTTPSessionProviderInvalidateSessionRateLimited pins that a sidecar asking
// for backoff fails the invalidation: rotating anyway would re-adopt the same
// capped session.
func TestHTTPSessionProviderInvalidateSessionRateLimited(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"accepted":false,"generation":7,"retry_after_seconds":42}`))
}))
defer srv.Close()
p, err := NewSidecarSessionProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
err = p.(potoken.SessionInvalidator).InvalidateSession(context.Background(), potoken.SessionInvalidation{Generation: 7})
if err == nil {
t.Fatal("expected an error when the report is rate-limited")
}
if !strings.Contains(err.Error(), "42") {
t.Errorf("error should carry the retry delay: %v", err)
}
}
// TestHTTPSessionProviderInvalidateSessionUnversioned pins that a sidecar which
// does not version its sessions cannot be reported to: /report requires a
// generation, so the call fails locally instead of sending a rejected request.
func TestHTTPSessionProviderInvalidateSessionUnversioned(t *testing.T) {
var hits int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
hits++
_, _ = w.Write([]byte(`{}`))
}))
defer srv.Close()
p, err := NewSidecarSessionProvider(srv.URL)
if err != nil {
t.Fatal(err)
}
if err := p.(potoken.SessionInvalidator).InvalidateSession(context.Background(), potoken.SessionInvalidation{}); err == nil {
t.Fatal("expected an error when the session carries no generation")
}
if hits != 0 {
t.Errorf("report requests = %d, want 0", hits)
}
}
// TestPlayerContextProviderInvalidateSession pins that the player-context
// provider reports capped sessions to the /report sibling of its endpoint,
// sharing the session provider's report contract.
func TestPlayerContextProviderInvalidateSession(t *testing.T) {
var reportBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/report" {
t.Errorf("path = %q, want /report", r.URL.Path)
}
_ = json.NewDecoder(r.Body).Decode(&reportBody)
_, _ = w.Write([]byte(`{"accepted":true,"retired":true,"generation":7}`))
}))
defer srv.Close()
// Configure the full endpoint, not the base, to pin the sibling derivation.
p, err := NewSidecarPlayerContextProvider(srv.URL + "/player-context")
if err != nil {
t.Fatal(err)
}
inv, ok := p.(potoken.SessionInvalidator)
if !ok {
t.Fatal("the sidecar player-context provider must implement potoken.SessionInvalidator")
}
if err := inv.InvalidateSession(context.Background(), potoken.SessionInvalidation{
Generation: 7,
VideoID: "dummyVideo0",
Reason: "stream-capped",
}); err != nil {
t.Fatalf("InvalidateSession: %v", err)
}
if got, _ := reportBody["session_generation"].(float64); got != 7 {
t.Errorf("session_generation = %v, want 7", reportBody["session_generation"])
}
if reportBody["video_id"] != "dummyVideo0" || reportBody["reason"] != "stream-capped" {
t.Errorf("report body = %v, want the video and reason forwarded", reportBody)
}
}
func TestSiblingSidecarURL(t *testing.T) {
cases := []struct {
endpoint, name, want string
}{
{"http://h:4417/session", "report", "http://h:4417/report"},
{"http://h/api/session", "report", "http://h/api/report"},
// A trailing slash must not shift the sibling down a level.
{"http://h/api/session/", "report", "http://h/api/report"},
{"http://h/session?key=K", "report", "http://h/report?key=K"},
}
for _, tc := range cases {
got, err := siblingSidecarURL(tc.endpoint, tc.name)
if err != nil {
t.Errorf("siblingSidecarURL(%q,%q) error: %v", tc.endpoint, tc.name, err)
continue
}
if got != tc.want {
t.Errorf("siblingSidecarURL(%q,%q) = %q, want %q", tc.endpoint, tc.name, got, tc.want)
}
}
}
// redactURLsIn guards the messages assembled from wrapped causes, where a signed
// stream URL can arrive inside anything. It runs where those strings are built,
// so the result is safe in a playlist run's NDJSON as well as on the terminal.
func TestRedactURLsIn(t *testing.T) {
cases := map[string]string{
"": "",
"no urls here": "no urls here",
"hostless: https://?x=1": "hostless: <url>",
`Get "https://rr3---sn-x.googlevideo.com/videoplayback?expire=1&sig=SECRET": EOF`: `Get "https://rr3---sn-x.googlevideo.com": EOF`,
"re-resolve after refresh: http://127.0.0.1:4417/get_pot?key=hunter2": "re-resolve after refresh: http://127.0.0.1:4417",
"two: https://a.example/x and https://b.example/y.": "two: https://a.example and https://b.example.",
}
for in, want := range cases {
if got := redactURLsIn(in); got != want {
t.Errorf("redactURLsIn(%q) = %q, want %q", in, got, want)
}
}
if got := redactURLsIn("pot=X only in https://host/p?pot=X"); strings.Contains(got, "?pot=X") {
t.Errorf("query string survived: %q", got)
}
}