|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#if !NETWORK_NO_SWIFT_QUIC |
| 16 | + |
| 17 | +import XCTest |
| 18 | + |
| 19 | +#if canImport(SwiftNetwork) |
| 20 | +@_spi(Essentials) @_spi(ProtocolProvider) @testable import SwiftNetwork |
| 21 | +#elseif canImport(Network) |
| 22 | +@_spi(Essentials) @_spi(ProtocolProvider) @testable import Network |
| 23 | +#endif |
| 24 | + |
| 25 | +@available(Network 0.1.0, *) |
| 26 | +let migrationTestsLogPrefixer: LogPrefixer = LogPrefixer("[MigrationTests]") |
| 27 | + |
| 28 | +@available(Network 0.1.0, *) |
| 29 | +final class MigrationTests: XCTestCase { |
| 30 | + var connection = QUICConnection(context: .implicitContext) |
| 31 | + |
| 32 | + static let oldCID = QUICConnectionID([0xA1, 0xA2, 0xA3, 0xA4])! |
| 33 | + static let newCID = QUICConnectionID([0xB1, 0xB2, 0xB3, 0xB4])! |
| 34 | + |
| 35 | + override func setUp() { |
| 36 | + let expectation = XCTestExpectation() |
| 37 | + connection.context.async { |
| 38 | + try? self.connection.setup(remote: nil, local: nil, parameters: nil, path: nil) |
| 39 | + self.connection.recovery = Recovery(logPrefixer: migrationTestsLogPrefixer) |
| 40 | + self.connection.recovery.connection = self.connection |
| 41 | + expectation.fulfill() |
| 42 | + } |
| 43 | + wait(for: [expectation], timeout: 5.0) |
| 44 | + } |
| 45 | + |
| 46 | + override func tearDown() { |
| 47 | + self.connection.currentPath = nil |
| 48 | + } |
| 49 | + |
| 50 | + // Builds a path that is open for sending, backed by a lower harness, with its DCID |
| 51 | + // registered in `remoteCIDs` so it can be retired. `validated` drives it to the |
| 52 | + // validated state so `migrate(to:)` will accept it. |
| 53 | + private func makePath(dcid: QUICConnectionID, sequenceNumber: UInt64, validated: Bool) -> QUICPath { |
| 54 | + let lower = DatagramLowerHarness(identifier: "\(sequenceNumber)", context: .implicitContext) |
| 55 | + lower.connect() |
| 56 | + var path = QUICPath(parent: connection) |
| 57 | + path.set(interface: nil, priority: 1, isInitial: true) // -> .routeEstablished |
| 58 | + path.assignDCID(dcid) // -> .cidAssigned (open for sending) |
| 59 | + if validated { |
| 60 | + path.changeState(to: .probing) |
| 61 | + path.changeState(to: .validated) |
| 62 | + } |
| 63 | + try? path.attachLowerProtocol(lower.reference, remote: nil, local: nil, parameters: nil, path: nil) |
| 64 | + try? connection.remoteCIDs.insert( |
| 65 | + sequenceNumber: sequenceNumber, |
| 66 | + connectionID: dcid, |
| 67 | + token: QUICStatelessResetToken(Array(repeating: UInt8(sequenceNumber & 0xff), count: 16))! |
| 68 | + ) |
| 69 | + return path |
| 70 | + } |
| 71 | + |
| 72 | + func testMigrationRemovesOldPathAndRetiresItsCID() { |
| 73 | + let expectation = XCTestExpectation() |
| 74 | + connection.context.async { |
| 75 | + let oldPath = self.makePath(dcid: Self.oldCID, sequenceNumber: 1, validated: false) |
| 76 | + let newPath = self.makePath(dcid: Self.newCID, sequenceNumber: 2, validated: true) |
| 77 | + |
| 78 | + self.connection.currentPath = oldPath |
| 79 | + self.connection.multiplexingPaths[oldPath.identifier] = oldPath |
| 80 | + self.connection.multiplexingPaths[newPath.identifier] = newPath |
| 81 | + let oldPathID = oldPath.identifier |
| 82 | + |
| 83 | + self.connection.migration.migrate(to: newPath, connection: self.connection) |
| 84 | + |
| 85 | + // The path we migrated away from is dropped from the connection and its |
| 86 | + // remote CID is retired. |
| 87 | + XCTAssertNil( |
| 88 | + self.connection.multiplexingPaths[oldPathID], |
| 89 | + "Old path not removed" |
| 90 | + ) |
| 91 | + XCTAssertNil( |
| 92 | + self.connection.remoteCIDs.retire(connectionID: Self.oldCID), |
| 93 | + "Old path CID not retired" |
| 94 | + ) |
| 95 | + |
| 96 | + // Only the new path remains, and it is now the current path. |
| 97 | + XCTAssertEqual( |
| 98 | + self.connection.multiplexingPaths.count, |
| 99 | + 1, |
| 100 | + "Unexpected path count" |
| 101 | + ) |
| 102 | + XCTAssertEqual( |
| 103 | + self.connection.currentPath?.identifier, |
| 104 | + newPath.identifier, |
| 105 | + "Current path not switched" |
| 106 | + ) |
| 107 | + |
| 108 | + expectation.fulfill() |
| 109 | + } |
| 110 | + wait(for: [expectation], timeout: 5.0) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +#endif |
0 commit comments