internal/providers/anipub/validate_test.go

242 lines · 9048 bytes

4 symbols in this file
  1package anipub
  2
  3import (
  4	"net/http"
  5	"net/url"
  6	"strings"
  7	"testing"
  8)
  9
 10func TestSelectMediaPlaylistURL(t *testing.T) {
 11	master := func() *url.URL { u, _ := url.Parse("https://megap.kotocdn.site/abc/master.m3u8"); return u }
 12
 13	t.Run("media playlist with no variants returns master", func(t *testing.T) {
 14		got, err := selectMediaPlaylistURL(master(), "#EXTM3U\n#EXTINF:5,\nhttps://seg/1.ts\n")
 15		if err != nil {
 16			t.Fatalf("unexpected err: %v", err)
 17		}
 18		if got != "https://megap.kotocdn.site/abc/master.m3u8" {
 19			t.Fatalf("unexpected %q", got)
 20		}
 21	})
 22
 23	t.Run("picks highest bandwidth relative variant", func(t *testing.T) {
 24		body := "#EXTM3U\n" +
 25			"#EXT-X-STREAM-INF:BANDWIDTH=500000\nlow.m3u8\n" +
 26			"#EXT-X-STREAM-INF:BANDWIDTH=2000000\nhigh.m3u8\n"
 27		got, err := selectMediaPlaylistURL(master(), body)
 28		if err != nil {
 29			t.Fatalf("unexpected err: %v", err)
 30		}
 31		if got != "https://megap.kotocdn.site/abc/high.m3u8" {
 32			t.Fatalf("unexpected %q", got)
 33		}
 34	})
 35
 36	t.Run("picks highest bandwidth absolute variant", func(t *testing.T) {
 37		body := "#EXTM3U\n" +
 38			"#EXT-X-STREAM-INF:BANDWIDTH=2000000\nhttps://cdn.example/high.m3u8\n" +
 39			"#EXT-X-STREAM-INF:BANDWIDTH=500000\nlow.m3u8\n"
 40		got, err := selectMediaPlaylistURL(master(), body)
 41		if err != nil {
 42			t.Fatalf("unexpected err: %v", err)
 43		}
 44		if got != "https://cdn.example/high.m3u8" {
 45			t.Fatalf("unexpected %q", got)
 46		}
 47	})
 48
 49	t.Run("no variant found errors", func(t *testing.T) {
 50		body := "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=500000\n"
 51		if _, err := selectMediaPlaylistURL(master(), body); err == nil {
 52			t.Fatal("expected error")
 53		}
 54	})
 55}
 56
 57func TestParsePlaylistSegmentsAndDecoyURIs(t *testing.T) {
 58	body := "#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/seg/1.ts\n#EXTINF:5,\n" +
 59		"https://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/decoy\n"
 60	segments := parsePlaylistSegments(body)
 61	if len(segments) != 2 {
 62		t.Fatalf("expected 2 segments, got %d", len(segments))
 63	}
 64	if isDecoySegmentURI(segments[0]) {
 65		t.Fatalf("kotocdn segment misclassified as decoy")
 66	}
 67	if !isDecoySegmentURI(segments[1]) {
 68		t.Fatalf("ibyteimg segment not classified as decoy")
 69	}
 70}
 71
 72func TestLooksLikeDecoySegment(t *testing.T) {
 73	cases := []struct {
 74		name string
 75		data []byte
 76		want bool
 77	}{
 78		{"png", []byte{0x89, 'P', 'N', 'G', 0x0D, 0x0A}, true},
 79		{"jpeg", []byte{0xFF, 0xD8, 0xFF, 0xE0}, true},
 80		{"gif", []byte("GIF89a..."), true},
 81		{"webp", []byte("RIFFxxxxWEBP"), true},
 82		{"html", []byte("<html><body>error</body></html>"), true},
 83		{"doctype", []byte("<!DOCTYPE html>"), true},
 84		{"mpeg ts", []byte{0x47, 0x47, 0x00, 0x1B}, false},
 85		{"fmp4 ftyp", []byte{0x00, 0x00, 0x00, 0x18, 'f', 't', 'y', 'p'}, false},
 86		{"short bytes", []byte{0x47, 0x47}, false},
 87	}
 88	for _, tc := range cases {
 89		t.Run(tc.name, func(t *testing.T) {
 90			if got := looksLikeDecoySegment(tc.data); got != tc.want {
 91				t.Fatalf("looksLikeDecoySegment(%v) = %v, want %v", tc.data, got, tc.want)
 92			}
 93		})
 94	}
 95}
 96
 97func TestValidateResolvedStream(t *testing.T) {
 98	const masterURL = "https://megap.kotocdn.site/abc/master.m3u8"
 99
100	newClient := func(handler func(*http.Request) *http.Response) *http.Client {
101		return &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
102			if resp := handler(req); resp != nil {
103				resp.Request = req
104				return resp, nil
105			}
106			return textResponse(req, http.StatusNotFound, ""), nil
107		})}
108	}
109
110	text := func(body string) string { return body }
111
112	t.Run("non kotocdn host is skipped", func(t *testing.T) {
113		client := newClient(func(req *http.Request) *http.Response {
114			t.Fatalf("no requests expected, got %s", req.URL)
115			return nil
116		})
117		withAnipubTestClient(t, client)
118		if err := validateResolvedStream("https://cdn.example/master.m3u8"); err != nil {
119			t.Fatalf("expected no error, got %v", err)
120		}
121	})
122
123	t.Run("clean stream passes", func(t *testing.T) {
124		client := newClient(func(req *http.Request) *http.Response {
125			switch {
126			case strings.HasSuffix(req.URL.Path, "/master.m3u8"):
127				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n"))
128			case strings.HasSuffix(req.URL.Path, "/index.m3u8"):
129				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/2.ts\n"))
130			default:
131				return textResponse(req, http.StatusOK, string([]byte{0x47, 0x47, 0x00, 0x1B}))
132			}
133		})
134		withAnipubTestClient(t, client)
135		if err := validateResolvedStream(masterURL); err != nil {
136			t.Fatalf("expected clean stream to pass, got %v", err)
137		}
138	})
139
140	t.Run("fully decoy stream is rejected", func(t *testing.T) {
141		client := newClient(func(req *http.Request) *http.Response {
142			switch {
143			case strings.HasSuffix(req.URL.Path, "/master.m3u8"):
144				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n"))
145			default:
146				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/x\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/y\n"))
147			}
148		})
149		withAnipubTestClient(t, client)
150		if err := validateResolvedStream(masterURL); err == nil || !strings.Contains(err.Error(), "decoy") {
151			t.Fatalf("expected decoy error, got %v", err)
152		}
153	})
154
155	t.Run("majority decoy stream is rejected", func(t *testing.T) {
156		client := newClient(func(req *http.Request) *http.Response {
157			switch {
158			case strings.HasSuffix(req.URL.Path, "/master.m3u8"):
159				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n"))
160			default:
161				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/a\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/b\n"))
162			}
163		})
164		withAnipubTestClient(t, client)
165		if err := validateResolvedStream(masterURL); err == nil || !strings.Contains(err.Error(), "decoy") {
166			t.Fatalf("expected decoy error, got %v", err)
167		}
168	})
169
170	t.Run("real segment redirecting to decoy is rejected", func(t *testing.T) {
171		client := newClient(func(req *http.Request) *http.Response {
172			switch {
173			case strings.HasSuffix(req.URL.Path, "/master.m3u8"):
174				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n"))
175			case strings.HasSuffix(req.URL.Path, "/index.m3u8"):
176				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n"))
177			case strings.Contains(req.URL.Host, "ibyteimg.com"):
178				return textResponse(req, http.StatusOK, string([]byte{0x89, 'P', 'N', 'G', 0x0D, 0x0A}))
179			default:
180				resp := textResponse(req, http.StatusFound, "")
181				resp.Header.Set("Location", "https://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/decoy")
182				return resp
183			}
184		})
185		withAnipubTestClient(t, client)
186		if err := validateResolvedStream(masterURL); err == nil || !strings.Contains(err.Error(), "not video content") {
187			t.Fatalf("expected probe rejection, got %v", err)
188		}
189	})
190
191	t.Run("segment probe failure is tolerated", func(t *testing.T) {
192		client := newClient(func(req *http.Request) *http.Response {
193			switch {
194			case strings.HasSuffix(req.URL.Path, "/master.m3u8"):
195				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n"))
196			case strings.HasSuffix(req.URL.Path, "/index.m3u8"):
197				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n"))
198			default:
199				return textResponse(req, http.StatusInternalServerError, "")
200			}
201		})
202		withAnipubTestClient(t, client)
203		if err := validateResolvedStream(masterURL); err != nil {
204			t.Fatalf("expected probe failure to be tolerated, got %v", err)
205		}
206	})
207
208	t.Run("non 200 manifest is rejected", func(t *testing.T) {
209		client := newClient(func(req *http.Request) *http.Response {
210			return textResponse(req, http.StatusForbidden, "")
211		})
212		withAnipubTestClient(t, client)
213		if err := validateResolvedStream(masterURL); err == nil {
214			t.Fatal("expected manifest error")
215		}
216	})
217
218	t.Run("non HLS manifest is rejected", func(t *testing.T) {
219		client := newClient(func(req *http.Request) *http.Response {
220			return textResponse(req, http.StatusOK, text("<html>challenge</html>"))
221		})
222		withAnipubTestClient(t, client)
223		if err := validateResolvedStream(masterURL); err == nil {
224			t.Fatal("expected non-hls error")
225		}
226	})
227
228	t.Run("empty media playlist is rejected", func(t *testing.T) {
229		client := newClient(func(req *http.Request) *http.Response {
230			switch {
231			case strings.HasSuffix(req.URL.Path, "/master.m3u8"):
232				return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n"))
233			default:
234				return textResponse(req, http.StatusOK, text("#EXTM3U\n"))
235			}
236		})
237		withAnipubTestClient(t, client)
238		if err := validateResolvedStream(masterURL); err == nil {
239			t.Fatal("expected empty playlist error")
240		}
241	})
242}