import Foundation import XCTest @testable import suixinkan /// 验证真实协议的参数边界;样例只有虚构值,测试从不连接 RTC。 final class DroneDockLiveConnectionTests: XCTestCase { /// 保留 Token 中的加号与等号,百分号只解码一次。 func testVolcQueryDecodesOnceAndPreservesTokenCharacters() throws { let value = try DroneDockVolcLiveConfiguration.parse(DroneDockRTCFixture.response(), now: DroneDockRTCFixture.now) XCTAssertEqual(value.appID, "test-app") XCTAssertEqual(value.roomID, "test-room") XCTAssertEqual(value.userID, "test-user") XCTAssertEqual(value.token, "fake+token+/%2B==") XCTAssertEqual(value.role, "drone") XCTAssertEqual(value.expiresAt.timeIntervalSince1970, 2_000_000_600) XCTAssertFalse(value.debugDescription.contains(value.token)) } /// 缺失、重复和非法转义的参数不进入 SDK,也不在错误中泄露原值。 func testInvalidQueriesAreRejectedWithoutLeakingSecrets() throws { let valid = DroneDockRTCFixture.query for query in [valid + "&app_id=second", valid + "&token=secret", "bad=%ZZ", "https://test.invalid/a", "app_id=x&room_id=y&user_id=z&token=", "app_id=x&room_id=y&user_id=z&token=%0Asecret", "app_id=x&room_id=y&user_id=z", valid + "&expire_time=bad"] { XCTAssertThrowsError(try DroneDockVolcLiveConfiguration.parse( DroneDockRTCFixture.response(play: ["url": query]), now: DroneDockRTCFixture.now)) { error in XCTAssertFalse(error.localizedDescription.contains("secret")) } } } /// 只接受已明确支持的 volc,绝不把未知 RTC 连接串当 HTTP 视频播放。 func testUnsupportedVendorsAreExplicitlyRejected() throws { for type in ["agora", "tencent", "srs", "", "unknown"] { XCTAssertThrowsError(try DroneDockVolcLiveConfiguration.parse( DroneDockRTCFixture.response(play: ["url_type": type]), now: DroneDockRTCFixture.now)) { error in XCTAssertTrue(error.localizedDescription.contains("暂不支持")) } } } /// 不可播放回执优先展示后端原因,不强求成功态中的设备字段。 func testUnavailableResponsePreservesReason() throws { let response = try DroneDockLiveTestData.response([ "order_flight_task_id": 501, "playable": false, "play": NSNull(), "unavailable_message": "飞行器暂未上报视频流"]) XCTAssertThrowsError(try DroneDockVolcLiveConfiguration.parse(response, now: DroneDockRTCFixture.now)) { XCTAssertEqual($0.localizedDescription, "飞行器暂未上报视频流") } } /// 可播放标志不替代流可用、设备身份、角色的验证。 func testMissingStreamOrDeviceFieldsCannotConnect() throws { for change: [String: Any] in [["available": false], ["available": NSNull()], ["role": "other"], ["url": NSNull()], ["expire_ts": NSNull()]] { XCTAssertThrowsError(try DroneDockVolcLiveConfiguration.parse( DroneDockRTCFixture.response(play: change), now: DroneDockRTCFixture.now)) } for change: [String: Any] in [["plane_id": 0], ["airport_sn": ""], ["play": NSNull()]] { XCTAssertThrowsError(try DroneDockVolcLiveConfiguration.parse( DroneDockRTCFixture.response(root: change), now: DroneDockRTCFixture.now)) } } /// 使用两个有效期中较早者,过期或即将过期的凭证不能进房。 func testExpiryUsesEarlierTimestampAndRejectsNearExpiry() throws { let response = try DroneDockRTCFixture.response(play: ["url": DroneDockRTCFixture.query + "&expire_time=2000000100"]) XCTAssertEqual(try DroneDockVolcLiveConfiguration.parse(response, now: DroneDockRTCFixture.now) .expiresAt.timeIntervalSince1970, 2_000_000_100) for expiry in [0, 1_999_999_999, 2_000_000_005] { XCTAssertThrowsError(try DroneDockVolcLiveConfiguration.parse( DroneDockRTCFixture.response(play: ["expire_ts": expiry]), now: DroneDockRTCFixture.now)) } } /// 后端优先流为机巢时明确保留角色,不冒充无人机摄像头。 func testAirportFallbackKeepsSourceRole() throws { let response = try DroneDockRTCFixture.response(play: ["role": "airport"]) XCTAssertEqual(try DroneDockVolcLiveConfiguration.parse(response, now: DroneDockRTCFixture.now).role, "airport") } /// 展示 JSON 中的 play/raw/airport/drone 均不能暴露连接串。 func testDiagnosticJSONRedactsNestedConnectionSecrets() throws { let response = try DroneDockRTCFixture.response(root: ["drone": ["url": "secret-drone", "token": "secret-token"]], play: ["raw": ["url": "secret-raw", "token": "secret-token"]]) XCTAssertFalse(response.dataJSON.contains("fake+token")) XCTAssertFalse(response.dataJSON.contains("secret-")) XCTAssertTrue(response.dataJSON.contains("")) XCTAssertTrue(response.play?.url?.contains("fake+token") == true) } } /// 固定时间与合成凭证;仅供内存解析和播放器替身使用。 enum DroneDockRTCFixture { static let now = Date(timeIntervalSince1970: 2_000_000_000) static let query = "app_id=test-app&room_id=test-room&user_id=test-user&token=fake+token%2B%2F%252B==" /// 支持覆写错误字段,所有样例均没有真实设备或可用凭证。 static func response(root: [String: Any] = [:], play: [String: Any] = [:]) throws -> DroneDockLiveResponse { let stream: [String: Any] = ["available": true, "role": "drone", "device_sn": "TEST-DRONE", "url_type": "volc", "url": query, "expire_ts": 2_000_000_600] let data = DroneDockLiveTestData.payload().merging([ "plane_id": 5, "playable": true, "play": stream.merging(play) { _, new in new } ]) { _, new in new }.merging(root) { _, new in new } return try DroneDockLiveTestData.response(data) } }