|
| 1 | +import Testing |
| 2 | +import Foundation |
| 3 | +@testable import AetherEngine |
| 4 | + |
| 5 | +/// The listener binds 0.0.0.0 so an AirPlay receiver can reach it over the LAN (#86). That also |
| 6 | +/// exposes it to every other host on that network, and the endpoint names are fixed, so the |
| 7 | +/// ephemeral port was the only thing standing between a port scan and the stream. These pin the |
| 8 | +/// per-session path token that replaced that assumption. |
| 9 | +struct HLSLocalServerSessionTokenTests { |
| 10 | + |
| 11 | + // MARK: - Path check |
| 12 | + |
| 13 | + @Test("The token is stripped and the remainder routes") |
| 14 | + func tokenIsStripped() { |
| 15 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "/abc123/media.m3u8") == "/media.m3u8") |
| 16 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "/abc123/seg7.mp4") == "/seg7.mp4") |
| 17 | + } |
| 18 | + |
| 19 | + @Test("A request without the token is refused") |
| 20 | + func unprefixedIsRefused() { |
| 21 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "/media.m3u8") == nil) |
| 22 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "/") == nil) |
| 23 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "") == nil) |
| 24 | + } |
| 25 | + |
| 26 | + @Test("A wrong token is refused") |
| 27 | + func wrongTokenIsRefused() { |
| 28 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "/def456/media.m3u8") == nil) |
| 29 | + } |
| 30 | + |
| 31 | + @Test("A token that only prefixes the first segment is refused, not truncated") |
| 32 | + func partialSegmentMatchIsRefused() { |
| 33 | + // "/abc123extra/..." starts with "/abc123" as a STRING but is a different path segment. |
| 34 | + // Comparing on the string alone would let it through with a mangled remainder. |
| 35 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "/abc123extra/media.m3u8") == nil) |
| 36 | + } |
| 37 | + |
| 38 | + @Test("The bare token with nothing after it is refused") |
| 39 | + func bareTokenIsRefused() { |
| 40 | + #expect(HLSLocalServer.pathAfterToken("abc123", in: "/abc123") == nil) |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Token shape |
| 44 | + |
| 45 | + @Test("Each server draws its own 128-bit token") |
| 46 | + func tokensAreDistinctAndFullWidth() { |
| 47 | + let a = HLSLocalServer(provider: StubProvider()) |
| 48 | + let b = HLSLocalServer(provider: StubProvider()) |
| 49 | + #expect(a.pathToken.count == 32) |
| 50 | + #expect(a.pathToken.allSatisfy { $0.isHexDigit }) |
| 51 | + #expect(a.pathToken != b.pathToken) |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - Over a real socket |
| 55 | + |
| 56 | + @Test("The served URL carries the token and an unprefixed request 404s") |
| 57 | + func unprefixedRequestIsRefusedOverTheSocket() throws { |
| 58 | + let server = HLSLocalServer(provider: StubProvider()) |
| 59 | + try server.start() |
| 60 | + defer { server.stop() } |
| 61 | + |
| 62 | + let served = try #require(server.mediaPlaylistURL) |
| 63 | + #expect(served.path == "/\(server.pathToken)/media.m3u8") |
| 64 | + |
| 65 | + #expect(Self.status(port: server.port, path: "/\(server.pathToken)/media.m3u8") == 200) |
| 66 | + // The shape a LAN scanner would try: right port, right endpoint name, no token. |
| 67 | + #expect(Self.status(port: server.port, path: "/media.m3u8") == 404) |
| 68 | + #expect(Self.status(port: server.port, path: "/init.mp4") == 404) |
| 69 | + #expect(Self.status(port: server.port, path: "/seg0.mp4") == 404) |
| 70 | + } |
| 71 | + |
| 72 | + // MARK: - Helpers |
| 73 | + |
| 74 | + /// Status line of a plain GET, or 0 when the request could not be completed. |
| 75 | + private static func status(port: UInt16, path: String) -> Int { |
| 76 | + let fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) |
| 77 | + guard fd >= 0 else { return 0 } |
| 78 | + defer { close(fd) } |
| 79 | + var addr = sockaddr_in() |
| 80 | + addr.sin_len = UInt8(MemoryLayout<sockaddr_in>.size) |
| 81 | + addr.sin_family = sa_family_t(AF_INET) |
| 82 | + addr.sin_port = port.bigEndian |
| 83 | + addr.sin_addr.s_addr = inet_addr("127.0.0.1") |
| 84 | + let connected = withUnsafePointer(to: &addr) { ptr -> Int32 in |
| 85 | + ptr.withMemoryRebound(to: sockaddr.self, capacity: 1) { |
| 86 | + connect(fd, $0, socklen_t(MemoryLayout<sockaddr_in>.size)) |
| 87 | + } |
| 88 | + } |
| 89 | + guard connected == 0 else { return 0 } |
| 90 | + let request = "GET \(path) HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n" |
| 91 | + let sent = Array(request.utf8).withUnsafeBytes { send(fd, $0.baseAddress, $0.count, 0) } |
| 92 | + guard sent > 0 else { return 0 } |
| 93 | + var buffer = [UInt8](repeating: 0, count: 256) |
| 94 | + let received = recv(fd, &buffer, buffer.count, 0) |
| 95 | + guard received > 0, |
| 96 | + let line = String(bytes: buffer[0..<received], encoding: .utf8)? |
| 97 | + .components(separatedBy: "\r\n").first else { return 0 } |
| 98 | + let parts = line.split(separator: " ") |
| 99 | + return parts.count >= 2 ? (Int(parts[1]) ?? 0) : 0 |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// Smallest provider that lets the server build and serve a media playlist. |
| 104 | +private final class StubProvider: HLSSegmentProvider, @unchecked Sendable { |
| 105 | + func initSegment() -> Data? { Data([0x00]) } |
| 106 | + func mediaSegment(at index: Int) -> Data? { Data([0x00]) } |
| 107 | + var segmentCount: Int { 1 } |
| 108 | + func segmentDuration(at index: Int) -> Double { 4.0 } |
| 109 | + var playlistType: HLSPlaylistType { .vod } |
| 110 | +} |
0 commit comments