import XCTest @testable import UCVLZonghengjia final class ProxyTransportPolicyTests: XCTestCase { private let fixture = #""" { "config_contract":2,"connection_scope":"public", "tailscale_endpoint":{"contract":1,"node_slug":"fixture-node","address":"100.70.1.2","port":12001}, "node":{"slug":"fixture-node","name":"Fixture","region":"","status":"online"}, "route_mode":"smart", "config":{ "log":{"loglevel":"warning"}, "outbounds":[ {"tag":"proxy","protocol":"vless","settings":{"vnext":[{"address":"node.example.test","port":443,"users":[{"id":"11111111-2222-3333-4444-555555555555","flow":"xtls-rprx-vision","encryption":"none"}]}]},"streamSettings":{"network":"tcp","security":"reality","realitySettings":{"serverName":"server.example.test","publicKey":"fixture-public-key","shortId":"abcd"}}}, {"tag":"direct","protocol":"freedom"} ], "routing":{"rules":[{"type":"field","domain":["example.test"],"outboundTag":"proxy"}]} } } """# private func response(_ change: (inout [String: Any]) -> Void = { _ in }) throws -> ClientConfigResponse { var root = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(fixture.utf8)) as? [String: Any]) change(&root) return try JSONDecoder().decode(ClientConfigResponse.self, from: JSONSerialization.data(withJSONObject: root)) } private func changingEndpoint(_ change: (inout [String: Any]) -> Void) throws -> ClientConfigResponse { try response { root in var endpoint = root["tailscale_endpoint"] as! [String: Any] change(&endpoint) root["tailscale_endpoint"] = endpoint } } private func changingProxy(_ change: (inout [String: Any]) -> Void) throws -> ClientConfigResponse { try response { root in var config = root["config"] as! [String: Any] var outbounds = config["outbounds"] as! [[String: Any]] change(&outbounds[0]) config["outbounds"] = outbounds root["config"] = config } } func testAcceptsOnlyCanonicalAuthorizedInternalIPv4() { for address in ["100.64.0.1", "100.70.1.2", "100.127.255.254"] { XCTAssertTrue(NetworkAddress.isAuthorizedInternalIPv4(address), address) XCTAssertFalse(NetworkAddress.isPublicIPv4(address), address) } for address in ["100.63.255.255", "100.128.0.0", "100.64.0.0", "100.127.255.255", "100.100.100.100", "100.070.1.2", "+100.70.1.2", " 100.70.1.2", "100.70.1.2 ", "100.70.1", "100.70.1.256", "192.168.1.2", "198.18.1.2", "8.8.8.8", "node.example.test"] { XCTAssertFalse(NetworkAddress.isAuthorizedInternalIPv4(address), address) } } func testAuthorizedEndpointAcceptsCompatiblePublicScopesAndRouteModes() throws { for scope in [nil, "", "public"] as [String?] { for route in ["smart", "global"] { let value = try response { root in root["connection_scope"] = scope root["route_mode"] = route } XCTAssertEqual(ProxyTransportPolicy.authorizedInternalEndpoint(response: value)?.address, "100.70.1.2") } } } func testMissingInvalidOrUnauthorizedMetadataNeverCreatesAnInternalCandidate() throws { let changes: [(inout [String: Any]) -> Void] = [ { $0.removeValue(forKey: "tailscale_endpoint") }, { $0["tailscale_endpoint"] = NSNull() }, { $0["config_contract"] = 3 }, { $0.removeValue(forKey: "config_contract") }, { $0["connection_scope"] = "private_auxiliary" }, { $0["connection_scope"] = true }, { $0["route_mode"] = "direct" }, { $0["route_mode"] = "unsupported" } ] for change in changes { XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try response(change))) } for port in [0, -1, 65536] { XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try changingEndpoint { $0["port"] = port })) } for address in ["8.8.8.8", "192.168.1.2", "198.18.1.2", "100.100.100.100", "100.070.1.2"] { XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try changingEndpoint { $0["address"] = address })) } XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try changingEndpoint { $0["node_slug"] = "other-node" })) XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try changingEndpoint { $0["contract"] = 2 })) XCTAssertNotNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try changingEndpoint { $0["port"] = 1 })) XCTAssertNotNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try changingEndpoint { $0["port"] = 65535 })) } func testUnsupportedOrAmbiguousProxyIdentityIsRejected() throws { let changes: [(inout [String: Any]) -> Void] = [ { $0["protocol"] = "socks" }, { $0["streamSettings"] = ["network":"tcp", "security":"tls"] }, { $0["settings"] = ["vnext":[]] }, { $0["settings"] = ["vnext":[["address":"node.example.test", "port":443, "users":[]]]] }, { $0["settings"] = ["vnext":[["address":"node.example.test", "port":443, "users":[["id":" "]]]]] }, { $0["streamSettings"] = ["network":"ws", "security":"reality", "realitySettings":["serverName":"server.example.test", "publicKey":"fixture-public-key"]] }, { $0["streamSettings"] = ["network":"tcp", "security":"reality", "realitySettings":["serverName":" ", "publicKey":"fixture-public-key"]] }, { $0["streamSettings"] = ["network":"tcp", "security":"reality", "realitySettings":["serverName":"server.example.test", "publicKey":" "]] } ] for change in changes { XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: try changingProxy(change))) } let duplicate = try response { root in var config = root["config"] as! [String: Any] var outbounds = config["outbounds"] as! [[String: Any]] outbounds.append(outbounds[0]) config["outbounds"] = outbounds root["config"] = config } XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: duplicate)) let multipleServers = try changingProxy { proxy in var settings = proxy["settings"] as! [String: Any] var servers = settings["vnext"] as! [[String: Any]] servers.append(servers[0]) settings["vnext"] = servers proxy["settings"] = settings } XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: multipleServers)) } func testRawNetworkAndRealityPasswordRemainCompatible() throws { let value = try changingProxy { proxy in var reality = ["serverName":"server.example.test"] reality["password"] = "fixture-public-key" proxy["streamSettings"] = ["network":"raw", "security":"reality", "realitySettings":reality] } XCTAssertNotNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: value)) } func testPrivateOrPreviouslyPreparedConfigurationsDoNotCreateAnInternalCandidate() throws { for marker in ["ucvl_private_auxiliary", "ucvl_tailnet_transport"] { let value = try response { root in var config = root["config"] as! [String: Any] config[marker] = ["address":"100.70.1.2"] root["config"] = config } XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(response: value)) } } func testRuntimeReplacementChangesOnlyAddressAndPort() throws { let value = try response() let endpoint = try XCTUnwrap(ProxyTransportPolicy.authorizedInternalEndpoint(response: value)) let original = try value.configJSONString() let runtime = try ProxyTransportPolicy.internalConfigJSON(rawJSON: original, endpoint: endpoint) var output = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(runtime.utf8)) as? [String: Any]) var outbounds = try XCTUnwrap(output["outbounds"] as? [[String: Any]]) var settings = try XCTUnwrap(outbounds[0]["settings"] as? [String: Any]) var servers = try XCTUnwrap(settings["vnext"] as? [[String: Any]]) XCTAssertEqual(servers[0]["address"] as? String, "100.70.1.2") XCTAssertEqual(servers[0]["port"] as? Int, 12001) servers[0]["address"] = "node.example.test" servers[0]["port"] = 443 settings["vnext"] = servers outbounds[0]["settings"] = settings output["outbounds"] = outbounds let expected = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(original.utf8)) as? [String: Any]) XCTAssertTrue(NSDictionary(dictionary: output).isEqual(to: expected)) XCTAssertEqual(try value.configJSONString(), original) } func testRuntimeReplacementRejectsInvalidEndpointOrProxy() throws { let value = try response() let endpoint = try XCTUnwrap(value.tailscaleEndpoint) XCTAssertThrowsError(try ProxyTransportPolicy.internalConfigJSON(rawJSON: "{}", endpoint: endpoint)) XCTAssertThrowsError(try ProxyTransportPolicy.internalConfigJSON(rawJSON: "invalid", endpoint: endpoint)) let publicAddress = ProxyTransportEndpoint(contract: 1, nodeSlug: "fixture-node", address: "8.8.8.8", port: 12001) XCTAssertThrowsError(try ProxyTransportPolicy.internalConfigJSON(rawJSON: try value.configJSONString(), endpoint: publicAddress)) } func testProviderValidationRepeatsNodeContractAndConfigurationGates() throws { let value = try response() let endpoint = try XCTUnwrap(value.tailscaleEndpoint) let config = try value.configJSONString() XCTAssertEqual(ProxyTransportPolicy.authorizedInternalEndpoint(endpoint: endpoint, nodeSlug: "fixture-node", configContract: 2, connectionScope: "public", routeMode: "smart", configJSON: config), endpoint) XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(endpoint: endpoint, nodeSlug: "other-node", configContract: 2, connectionScope: "public", routeMode: "smart", configJSON: config)) XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(endpoint: endpoint, nodeSlug: "fixture-node", configContract: 3, connectionScope: "public", routeMode: "smart", configJSON: config)) XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(endpoint: endpoint, nodeSlug: "fixture-node", configContract: 2, connectionScope: "private_auxiliary", routeMode: "smart", configJSON: config)) XCTAssertNil(ProxyTransportPolicy.authorizedInternalEndpoint(endpoint: endpoint, nodeSlug: "fixture-node", configContract: 2, connectionScope: "public", routeMode: "smart", configJSON: "{}")) } func testPublicFallbackRemainsLockedForTheEntireSession() { var session = ProxyTransportSession() XCTAssertFalse(session.domainOnly) session.commitPublicFallback() XCTAssertTrue(session.domainOnly) session.commitPublicFallback() XCTAssertTrue(session.domainOnly) XCTAssertFalse(ProxyTransportSession().domainOnly) } }