import XCTest @testable import HydraulophoneCore final class PlayerDecipheringTests: XCTestCase { func testSignatureAndNTransformAreAppliedAndCached() async throws { let solver = JavaScriptCorePlayerStreamCipher() let request = PlayerStreamCipherRequest( url: URL( string: "https://audio.example.googlevideo.com/videoplayback?foo=1&n=abc" )!, signature: "abcdef", signatureParameter: "sp", n: "abc", playerScript: Self.syntheticPlayerScript ) let first = try await solver.decipher(request) let second = try await solver.decipher(request) XCTAssertEqual(first, second) let query = try XCTUnwrap(URLComponents(url: first, resolvingAgainstBaseURL: false)?.queryItems) XCTAssertEqual(query.first(where: { $0.name == "foo" })?.value, "1") XCTAssertEqual(query.first(where: { $0.name == "sp" })?.value, "cdeba") XCTAssertEqual(query.first(where: { $0.name == "n" })?.value, "cba") // A different challenge uses the prepared AST output, not the result cache. let next = try await solver.decipher( PlayerStreamCipherRequest( url: URL(string: "https://audio.example.googlevideo.com/videoplayback?n=abcd")!, signature: nil, n: "abcd", playerScript: Self.syntheticPlayerScript ) ) let nextQuery = try XCTUnwrap( URLComponents(url: next, resolvingAgainstBaseURL: false)?.queryItems ) XCTAssertEqual(nextQuery.first(where: { $0.name == "n" })?.value, "dcba") } func testSignatureParameterIsAddedWhenCipherURLDoesNotContainIt() async throws { let solver = JavaScriptCorePlayerStreamCipher() let request = PlayerStreamCipherRequest( url: URL(string: "https://audio.example.googlevideo.com/videoplayback")!, signature: "abcdef", playerScript: Self.syntheticPlayerScript ) let result = try await solver.decipher(request) let query = try XCTUnwrap( URLComponents(url: result, resolvingAgainstBaseURL: false)?.queryItems) XCTAssertEqual(query.first(where: { $0.name == "sig" })?.value, "cdeba") } func testNOnlyDirectCandidateDoesNotAddASignature() async throws { let solver = JavaScriptCorePlayerStreamCipher() let request = PlayerStreamCipherRequest( url: URL(string: "https://audio.example.googlevideo.com/videoplayback?n=abc")!, signature: nil, n: "abc", playerScript: Self.syntheticPlayerScript ) let result = try await solver.decipher(request) let query = try XCTUnwrap( URLComponents(url: result, resolvingAgainstBaseURL: false)?.queryItems) XCTAssertEqual(query.first(where: { $0.name == "n" })?.value, "cba") XCTAssertNil(query.first(where: { $0.name == "sig" })) } func testCipherReplacementPreservesUnrelatedSignedQueryBytesAndDuplicates() async throws { let solver = JavaScriptCorePlayerStreamCipher() let source = try XCTUnwrap( URL( string: "https://audio.example.googlevideo.com/videoplayback?keep=%2f&opaque=a%2Bb&dup=1&dup=2&sig=old&n=abc#fragment" ) ) let result = try await solver.decipher( PlayerStreamCipherRequest( url: source, signature: "abcdef", n: "abc", playerScript: Self.syntheticPlayerScript ) ) let absolute = result.absoluteString XCTAssertTrue(absolute.contains("keep=%2f")) XCTAssertTrue(absolute.contains("opaque=a%2Bb")) XCTAssertTrue(absolute.contains("dup=1&dup=2")) XCTAssertTrue(absolute.contains("sig=cdeba")) XCTAssertTrue(absolute.contains("n=cba")) XCTAssertTrue(absolute.hasSuffix("#fragment")) } func testCredentialAndNonHTTPSURLsAreRejectedWithoutRunningJavaScript() async { let solver = JavaScriptCorePlayerStreamCipher() let urls = [ URL(string: "http://audio.example.googlevideo.com/videoplayback")!, URL(string: "https://user:password@audio.example.googlevideo.com/videoplayback")!, ] for url in urls { let request = PlayerStreamCipherRequest( url: url, signature: "abc", playerScript: "throw new Error('must not evaluate');" ) do { _ = try await solver.decipher(request) XCTFail("Expected invalid cipher URL") } catch let error as PlayerStreamCipherError { XCTAssertEqual(error, .invalidCipher) } catch { XCTFail("Unexpected error: \(error)") } } } func testMissingChallengeAndMalformedScriptHaveTypedErrors() async { let solver = JavaScriptCorePlayerStreamCipher() let noChallenge = PlayerStreamCipherRequest( url: URL(string: "https://audio.example.googlevideo.com/videoplayback")!, signature: nil, playerScript: Self.syntheticPlayerScript ) do { _ = try await solver.decipher(noChallenge) XCTFail("Expected invalid cipher") } catch let error as PlayerStreamCipherError { XCTAssertEqual(error, .invalidCipher) } catch { XCTFail("Unexpected error: \(error)") } let malformed = PlayerStreamCipherRequest( url: URL(string: "https://audio.example.googlevideo.com/videoplayback")!, signature: "abc", playerScript: "not valid javascript" ) do { _ = try await solver.decipher(malformed) XCTFail("Expected invalid script") } catch let error as PlayerStreamCipherError { XCTAssertEqual(error, .invalidScript) } catch { XCTFail("Unexpected error: \(error)") } } func testCancellationRemainsCancellation() async { let solver = JavaScriptCorePlayerStreamCipher() let request = PlayerStreamCipherRequest( url: URL(string: "https://audio.example.googlevideo.com/videoplayback")!, signature: "abc", playerScript: Self.syntheticPlayerScript ) let task = Task { try await solver.decipher(request) } task.cancel() do { _ = try await task.value XCTFail("Expected cancellation") } catch is CancellationError { // Expected: cancellation is not converted into invalidScript. } catch { XCTFail("Unexpected error: \(error)") } } // Synthetic IIFE/URL-builder shape supported by the pinned yt-ejs parser. // Unlike global-function toy fixtures, this exercises extraction from a // player scope, the bundled parser/generator, and both solver channels. private static let syntheticPlayerScript = #""" (function() { function Bag(signature) { this.values = {s: signature}; } Bag.prototype.set = function(k, v) { this.values[k] = v; }; Bag.prototype.get = function(k) { return this.values[k]; }; Bag.prototype.process = function() { if (this.values.s) { var a = decodeURIComponent(this.values.s).split(""); a.reverse(); a.splice(0, 1); var first = a[0]; a[0] = a[2]; a[2] = first; this.values.s = encodeURIComponent(a.join("")); } if (this.values.n) { this.values.n = this.values.n.split("").reverse().join(""); } }; function build(url, key, signature) { var result = new Bag(signature); result.set("alr", "yes"); return result; } }).call(this); """# }