mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
### Motivation: We have some tests for our Vsock support that only run when we detect the system supports Vsock. These weren't running in CI but, now we've migrated CI to GitHub Actions, they can. In prototyping this though, I discovered that one of our tests was too strict, and will fail for VMs running on Hyper-V, which is the hypervisor used by GitHub actions. Specifically, we have support for getting the local context ID by a channel option. This is implemented by an ioctl, but the semantics of the return value differ between the various Vsock transport kernel modules: - `virtio_transport`: returns the guest CID, which is a number greater than `VMADDR_CID_HOST`, but less than `VMADDR_CID_ANY`, and returns `VMADDR_CID_ANY` as an error. - `vsock_loopback`: returns `VMADDR_CID_LOCAL` always. - `vmci_transport`: returns the guest CID if the guest and host transport are active; `VMADDR_CID_HOST` if only the host transport is active; and `VMCI_INVALID_ID` otherwise, which happens to be the same value as `VMADDR_CID_ANY`. - `hyperv_transport`: returns `VMADDR_CID_ANY` always. For this reason, we should probably remove any attempts to interpret the validity of the value that comes back from the driver and users will need to know what to do with it. ### Modifications: - tests: Only run Vsock echo tests on Linux, when `vsock_loopback` is loaded. - tests: Remove asserts on the value of local Vsock CID. - ci: Add pipeline that runs Vsock tests. ### Result: - Vsock tests will no longer run unless `vsock_loopback` kernel module is loaded. - Vsock tests will no longer fail in Hyper-V VMs. - Vsock tests will now run in CI.
84 lines
3.6 KiB
Swift
84 lines
3.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import XCTest
|
|
|
|
@testable import NIOCore
|
|
@testable import NIOPosix
|
|
|
|
class VsockAddressTest: XCTestCase {
|
|
|
|
func testDescriptionWorks() throws {
|
|
XCTAssertEqual(VsockAddress(cid: .host, port: 12345).description, "[VSOCK]2:12345")
|
|
XCTAssertEqual(VsockAddress(cid: .any, port: 12345).description, "[VSOCK]-1:12345")
|
|
XCTAssertEqual(VsockAddress(cid: .host, port: .any).description, "[VSOCK]2:-1")
|
|
XCTAssertEqual(VsockAddress(cid: .any, port: .any).description, "[VSOCK]-1:-1")
|
|
}
|
|
|
|
func testInitializeFromIntegerLiteral() throws {
|
|
XCTAssertEqual(VsockAddress.ContextID(integerLiteral: 0), 0)
|
|
XCTAssertEqual(VsockAddress.Port(integerLiteral: 0), 0)
|
|
XCTAssertEqual(VsockAddress.ContextID(integerLiteral: 4_294_967_295), 4_294_967_295)
|
|
XCTAssertEqual(VsockAddress.Port(integerLiteral: 4_294_967_295), 4_294_967_295)
|
|
}
|
|
|
|
func testInitializeFromInt() throws {
|
|
XCTAssertEqual(VsockAddress.ContextID(0), 0)
|
|
XCTAssertEqual(VsockAddress.ContextID(4_294_967_295), 4_294_967_295)
|
|
XCTAssertEqual(VsockAddress.Port(0), 0)
|
|
XCTAssertEqual(VsockAddress.Port(4_294_967_295), 4_294_967_295)
|
|
}
|
|
|
|
func testSocketAddressEqualitySpecialValues() throws {
|
|
XCTAssertEqual(
|
|
VsockAddress(cid: .any, port: 12345),
|
|
.init(cid: .init(rawValue: UInt32(bitPattern: -1)), port: 12345)
|
|
)
|
|
XCTAssertEqual(VsockAddress(cid: .hypervisor, port: 12345), .init(cid: 0, port: 12345))
|
|
XCTAssertEqual(VsockAddress(cid: .host, port: 12345), .init(cid: 2, port: 12345))
|
|
}
|
|
|
|
func testSocketAddressEquality() throws {
|
|
XCTAssertEqual(VsockAddress(cid: 0, port: 0), .init(cid: 0, port: 0))
|
|
XCTAssertEqual(VsockAddress(cid: 1, port: 0), .init(cid: 1, port: 0))
|
|
XCTAssertEqual(VsockAddress(cid: 0, port: 1), .init(cid: 0, port: 1))
|
|
|
|
XCTAssertNotEqual(VsockAddress(cid: 0, port: 0), .init(cid: 1, port: 0))
|
|
XCTAssertNotEqual(VsockAddress(cid: 0, port: 0), .init(cid: 0, port: 1))
|
|
}
|
|
|
|
func testGetLocalCID() throws {
|
|
try XCTSkipUnless(System.supportsVsockLoopback, "No vsock loopback transport available")
|
|
|
|
let socket = try ServerSocket(protocolFamily: .vsock, setNonBlocking: true)
|
|
defer { try? socket.close() }
|
|
|
|
// Check we can get the local CID using the static property on ContextID.
|
|
let localCID = try socket.withUnsafeHandle(VsockAddress.ContextID.getLocalContextID)
|
|
|
|
// Check the local CID from the socket matches.
|
|
XCTAssertEqual(try socket.getLocalVsockContextID(), localCID)
|
|
|
|
// Check the local CID from the channel option matches.
|
|
let singleThreadedELG = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
|
defer { XCTAssertNoThrow(try singleThreadedELG.syncShutdownGracefully()) }
|
|
let eventLoop = singleThreadedELG.next()
|
|
let channel = try ServerSocketChannel(
|
|
serverSocket: socket,
|
|
eventLoop: eventLoop as! SelectableEventLoop,
|
|
group: singleThreadedELG
|
|
)
|
|
XCTAssertEqual(try channel.getOption(.localVsockContextID).wait(), localCID)
|
|
}
|
|
}
|