mirror of
https://github.com/Vateron-Media/XC_VM.git
synced 2026-10-03 12:02:29 +02:00
Clean up the admin stream loopback scripts (api/live/proxy_api/thumb/
timeshift/vod) for readability and to give their core logic unit-test
coverage, without changing behavior — except the two bugs called out below.
Readability:
- Invert the pervasive empty `if (c) {} else { body }` into `if (!c) { body }`.
- Flatten deep if/else nests into guard-clause / elseif chains (live, vod).
- Drop dead blank lines.
Testability (pure logic extracted, then covered):
- New value object XcVm\Domain\Stream\AdminStreamToken (decode + isValid)
replaces the uitoken decrypt+expiry+IP block duplicated across live/thumb/
timeshift/vod; it reuses the existing-but-unadopted NetworkUtils::ipMatches().
Token start/duration are kept raw (a timeshift date string must not be cast).
- Four pure helpers folded into the existing StreamUtils (not new one-method
classes): sanitizeSegmentName, containerMimeType, timeshiftStartTimestamp,
segmentRetryBudget — replacing inline path sanitization, a 44-line MIME
switch, the timeshift date parser, and the retry-budget math.
- Tests: AdminStreamTokenTest, NetworkUtilsTest (ipMatches had no coverage),
and StreamUtilsTest extended for the four helpers. OPENSSL_EXTRA is defined
in tests/bootstrap.php so token round-trips work.
Bug fixes (behavior changes):
- live: the retry budget used `$rTotalFails < intval(...) ?: 20`, which by
operator precedence parsed as `(... < ...) ?: 20` — always truthy — so the
`seg_time * 2` floor was ignored and the budget was always the configured
wait. Now max(seg_time*2, wait ?: 20) via StreamUtils::segmentRetryBudget().
- proxy_api: the per-second byte-rate divided by `time() - last`, which is 0
for two samples in the same second (division by zero). Clamp to >= 1s.
51 lines
2.1 KiB
PHP
51 lines
2.1 KiB
PHP
<?php
|
|
|
|
use XcVm\Core\Util\StreamUtils;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* @covers StreamUtils
|
|
*/
|
|
final class StreamUtilsTest extends TestCase {
|
|
|
|
public function testCustomOrderPutsInputArgumentsFirst() {
|
|
$this->assertSame(-1, StreamUtils::customOrder('-i input.ts', 'something'));
|
|
$this->assertSame(1, StreamUtils::customOrder('-c:v libx264', '-i input.ts'));
|
|
}
|
|
|
|
public function testDetectXcVmMatchesKnownStreamPaths() {
|
|
$this->assertTrue(StreamUtils::detectXC_VM('http://host/live/user/123'));
|
|
}
|
|
|
|
public function testDetectXcVmRejectsUnrelatedPaths() {
|
|
$this->assertFalse(StreamUtils::detectXC_VM('http://host/dashboard'));
|
|
}
|
|
|
|
public function testSanitizeSegmentNameStripsSeparators() {
|
|
$this->assertSame('seg_0.ts', StreamUtils::sanitizeSegmentName('seg_0.ts'));
|
|
$this->assertSame('....etcpasswd', StreamUtils::sanitizeSegmentName('../../etc/passwd'));
|
|
// URL-encoded traversal: "%2e%2e%2fpasswd" → "../passwd" → strip "/" → "..passwd".
|
|
$this->assertSame('..passwd', StreamUtils::sanitizeSegmentName('%2e%2e%2fpasswd'));
|
|
}
|
|
|
|
public function testContainerMimeType() {
|
|
$this->assertSame('video/mp4', StreamUtils::containerMimeType('mp4'));
|
|
$this->assertSame('video/x-matroska', StreamUtils::containerMimeType('mkv'));
|
|
$this->assertSame('video/mp2t', StreamUtils::containerMimeType('ts'));
|
|
$this->assertSame('application/octet-stream', StreamUtils::containerMimeType('xyz'));
|
|
$this->assertSame('application/octet-stream', StreamUtils::containerMimeType(''));
|
|
}
|
|
|
|
public function testTimeshiftStartTimestamp() {
|
|
$this->assertSame(1700000000, StreamUtils::timeshiftStartTimestamp('1700000000'));
|
|
$this->assertSame(mktime(13, 0, 0, 1, 1, 2025), StreamUtils::timeshiftStartTimestamp('20250101-13'));
|
|
$this->assertSame(mktime(13, 30, 0, 1, 1, 2025), StreamUtils::timeshiftStartTimestamp('2025-01-01:13-30'));
|
|
}
|
|
|
|
public function testSegmentRetryBudget() {
|
|
$this->assertSame(20, StreamUtils::segmentRetryBudget(10, 5)); // seg_time*2 wins (the fixed bug)
|
|
$this->assertSame(30, StreamUtils::segmentRetryBudget(3, 30)); // configured wins
|
|
$this->assertSame(20, StreamUtils::segmentRetryBudget(3, 0)); // 0 → default floor 20
|
|
}
|
|
}
|