221 lines
6.8 KiB
Go
221 lines
6.8 KiB
Go
package check
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"m3u8-downloader/pkg/config"
|
|
"m3u8-downloader/pkg/httpClient"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const masterPlaylistBody = `#EXTM3U
|
|
#EXT-X-STREAM-INF:BANDWIDTH=5500000,RESOLUTION=1920x1080
|
|
1080p/chunk.m3u8
|
|
#EXT-X-STREAM-INF:BANDWIDTH=3200000,RESOLUTION=1280x720
|
|
720p/chunk.m3u8
|
|
`
|
|
|
|
const mediaPlaylistBody = `#EXTM3U
|
|
#EXT-X-TARGETDURATION:4
|
|
#EXT-X-MEDIA-SEQUENCE:10
|
|
#EXTINF:4.0,
|
|
seg1.ts
|
|
#EXTINF:4.0,
|
|
seg2.ts
|
|
`
|
|
|
|
// playlistServer serves body at /playlist.m3u8 with the given status, and
|
|
// records the request headers so we can assert the check sends the same ones
|
|
// the real downloader does.
|
|
func playlistServer(t *testing.T, status int, body string) (*httptest.Server, *http.Header) {
|
|
t.Helper()
|
|
var got http.Header
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
got = r.Header.Clone()
|
|
w.WriteHeader(status)
|
|
fmt.Fprint(w, body)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv, &got
|
|
}
|
|
|
|
func joinDetails(details []string) string {
|
|
return strings.Join(details, "\n")
|
|
}
|
|
|
|
func TestCheckMasterPlaylistMaster(t *testing.T) {
|
|
srv, headers := playlistServer(t, http.StatusOK, masterPlaylistBody)
|
|
|
|
details, err := checkMasterPlaylist(srv.URL + "/playlist.m3u8")
|
|
if err != nil {
|
|
t.Fatalf("expected master playlist check to pass, got: %v", err)
|
|
}
|
|
|
|
joined := joinDetails(details)
|
|
for _, want := range []string{"master playlist with 2 variant(s)", "1080p (5500000 bps)", "720p (3200000 bps)", "HTTP 200"} {
|
|
if !strings.Contains(joined, want) {
|
|
t.Errorf("details missing %q:\n%s", want, joined)
|
|
}
|
|
}
|
|
|
|
if ua := headers.Get("User-Agent"); !strings.Contains(ua, "Mozilla/5.0") {
|
|
t.Errorf("expected downloader User-Agent to be sent, got %q", ua)
|
|
}
|
|
if ref := headers.Get("Referer"); ref == "" {
|
|
t.Error("expected Referer header to be sent")
|
|
}
|
|
}
|
|
|
|
func TestCheckMasterPlaylistMedia(t *testing.T) {
|
|
srv, _ := playlistServer(t, http.StatusOK, mediaPlaylistBody)
|
|
|
|
details, err := checkMasterPlaylist(srv.URL + "/playlist.m3u8")
|
|
if err != nil {
|
|
t.Fatalf("expected media playlist check to pass, got: %v", err)
|
|
}
|
|
|
|
if joined := joinDetails(details); !strings.Contains(joined, "media playlist with 2 segment(s)") {
|
|
t.Errorf("expected media playlist segment count in details:\n%s", joined)
|
|
}
|
|
}
|
|
|
|
func TestCheckMasterPlaylistNon200(t *testing.T) {
|
|
srv, _ := playlistServer(t, http.StatusForbidden, "denied")
|
|
|
|
details, err := checkMasterPlaylist(srv.URL + "/playlist.m3u8")
|
|
if err == nil {
|
|
t.Fatal("expected a 403 response to fail the check")
|
|
}
|
|
if code := httpClient.GetHTTPStatusCode(err); code != http.StatusForbidden {
|
|
t.Errorf("expected a structured HTTP 403 error, got code %d from %v", code, err)
|
|
}
|
|
// Details collected before the failure should still be reported.
|
|
if joined := joinDetails(details); !strings.Contains(joined, "HTTP 403") {
|
|
t.Errorf("expected partial details on failure:\n%s", joined)
|
|
}
|
|
}
|
|
|
|
func TestCheckMasterPlaylistNotAPlaylist(t *testing.T) {
|
|
srv, _ := playlistServer(t, http.StatusOK, "this is not a playlist")
|
|
|
|
if _, err := checkMasterPlaylist(srv.URL + "/playlist.m3u8"); err == nil {
|
|
t.Fatal("expected a non-m3u8 body to fail the check")
|
|
}
|
|
}
|
|
|
|
func TestCheckMasterPlaylistEmptyMaster(t *testing.T) {
|
|
srv, _ := playlistServer(t, http.StatusOK, "#EXTM3U\n")
|
|
|
|
if _, err := checkMasterPlaylist(srv.URL + "/playlist.m3u8"); err == nil {
|
|
t.Fatal("expected a playlist with no variants or segments to fail the check")
|
|
}
|
|
}
|
|
|
|
func TestCheckMasterPlaylistInvalidURL(t *testing.T) {
|
|
for _, bad := range []string{"", "notaurl", "/relative/path.m3u8", "://missing-scheme"} {
|
|
if _, err := checkMasterPlaylist(bad); err == nil {
|
|
t.Errorf("expected %q to be rejected as an invalid URL", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCheckMasterPlaylistUnreachable(t *testing.T) {
|
|
// Port 9 (discard) refuses connections without a listener.
|
|
if _, err := checkMasterPlaylist("http://127.0.0.1:9/playlist.m3u8"); err == nil {
|
|
t.Fatal("expected an unreachable host to fail the check")
|
|
}
|
|
}
|
|
|
|
func writeFile(path string) error {
|
|
return os.WriteFile(path, []byte("blocker"), 0644)
|
|
}
|
|
|
|
func nasConfig(path string) *config.Config {
|
|
cfg := &config.Config{}
|
|
cfg.NAS.EnableTransfer = true
|
|
cfg.NAS.OutputPath = path
|
|
cfg.NAS.Timeout = 10 * time.Second
|
|
cfg.NAS.RetryLimit = 3
|
|
return cfg
|
|
}
|
|
|
|
func TestCheckNASLocalPath(t *testing.T) {
|
|
target := filepath.Join(t.TempDir(), "streams")
|
|
|
|
details, err := checkNAS(nasConfig(target))
|
|
if err != nil {
|
|
t.Fatalf("expected a writable local path to pass the NAS check, got: %v", err)
|
|
}
|
|
|
|
joined := joinDetails(details)
|
|
for _, want := range []string{"path: " + target, "write probe succeeded"} {
|
|
if !strings.Contains(joined, want) {
|
|
t.Errorf("details missing %q:\n%s", want, joined)
|
|
}
|
|
}
|
|
// The probe must not leave its test file behind.
|
|
if matches, _ := filepath.Glob(filepath.Join(target, ".connection_test")); len(matches) != 0 {
|
|
t.Errorf("connection test file was not cleaned up: %v", matches)
|
|
}
|
|
}
|
|
|
|
func TestCheckNASUnusableTarget(t *testing.T) {
|
|
// A regular file cannot be turned into the destination directory.
|
|
blocker := filepath.Join(t.TempDir(), "not-a-dir")
|
|
if err := writeFile(blocker); err != nil {
|
|
t.Fatalf("failed to set up test file: %v", err)
|
|
}
|
|
|
|
details, err := checkNAS(nasConfig(filepath.Join(blocker, "streams")))
|
|
if err == nil {
|
|
t.Fatal("expected an unusable NAS target to fail the check")
|
|
}
|
|
if joined := joinDetails(details); !strings.Contains(joined, "path: ") {
|
|
t.Errorf("expected path detail on failure:\n%s", joined)
|
|
}
|
|
}
|
|
|
|
func TestProbeAsyncTimesOut(t *testing.T) {
|
|
start := time.Now()
|
|
result := <-probeAsync(50*time.Millisecond, func() ([]string, error) {
|
|
time.Sleep(5 * time.Second)
|
|
return nil, nil
|
|
})
|
|
|
|
if result.err == nil {
|
|
t.Fatal("expected a hung probe to report a timeout")
|
|
}
|
|
if !strings.Contains(result.err.Error(), "timed out") {
|
|
t.Errorf("expected a timeout error, got: %v", result.err)
|
|
}
|
|
if elapsed := time.Since(start); elapsed > time.Second {
|
|
t.Errorf("probe took %s, expected it to abandon the hung call promptly", elapsed)
|
|
}
|
|
}
|
|
|
|
func TestProbeAsyncPassesThroughResults(t *testing.T) {
|
|
sentinel := errors.New("probe failed")
|
|
|
|
if result := <-probeAsync(time.Second, func() ([]string, error) { return []string{"ok"}, nil }); result.err != nil || len(result.details) != 1 {
|
|
t.Errorf("expected success to pass through, got details=%v err=%v", result.details, result.err)
|
|
}
|
|
if result := <-probeAsync(time.Second, func() ([]string, error) { return nil, sentinel }); !errors.Is(result.err, sentinel) {
|
|
t.Errorf("expected probe error to pass through, got: %v", result.err)
|
|
}
|
|
}
|
|
|
|
func TestFormatDuration(t *testing.T) {
|
|
if got := formatDuration(100 * time.Microsecond); got != "<1ms" {
|
|
t.Errorf("sub-millisecond duration = %q, want \"<1ms\"", got)
|
|
}
|
|
if got := formatDuration(1500 * time.Millisecond); got != "1.5s" {
|
|
t.Errorf("1.5s duration = %q", got)
|
|
}
|
|
}
|