Files
swift-nio/Tests/NIOTests/UtilitiesTest.swift
T
Cory Benfield f9552beffa Replace NIONetworkInterface with NIONetworkDevice. (#1622)
Motivation:

NIONetworkInterface was a useful type, but its implementation was fairly
fatally flawed in a number of ways. While we could have lived with the
awkward type construction (it's a class, which isn't the semantic we
want), the ultimate flaw is that you cannot create a NIONetworkInterface
for an interface that does not have an IP address.

That's not helpful, given that it's not uncommon for an interface to not
have an IP address!

Modifications:

- Deprecate NIONetworkInterface
- Add NIONetworkDevice
- Provide new methods on MulticastChannel that use NIONetworkDevice
  instead of NIONetworkInterface.
- Provide default implementations of new MulticastChannel protocol
  requirements.
- Implement new MulticastChannel protocol requirements.
- Update the tests.

Result:

Devices will be able to reflect network devices without IP addresses.
2020-08-24 09:37:27 +01:00

80 lines
3.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 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 NIO
import XCTest
class UtilitiesTest: XCTestCase {
func testCoreCountWorks() {
XCTAssertGreaterThan(System.coreCount, 0)
}
@available(*, deprecated)
func testEnumeratingInterfaces() throws {
// This is a tricky test, because we can't really assert much and expect this
// to pass on all systems. The best we can do is assume there is a loopback:
// maybe an IPv4 one, maybe an IPv6 one, but there will be one. We look for
// both.
let interfaces = try System.enumerateInterfaces()
XCTAssertGreaterThan(interfaces.count, 0)
var ipv4LoopbackPresent = false
var ipv6LoopbackPresent = false
for interface in interfaces {
if try interface.address == SocketAddress(ipAddress: "127.0.0.1", port: 0) {
ipv4LoopbackPresent = true
XCTAssertEqual(interface.netmask, try SocketAddress(ipAddress: "255.0.0.0", port: 0))
XCTAssertNil(interface.broadcastAddress)
XCTAssertNil(interface.pointToPointDestinationAddress)
} else if try interface.address == SocketAddress(ipAddress: "::1", port: 0) {
ipv6LoopbackPresent = true
XCTAssertEqual(interface.netmask, try SocketAddress(ipAddress: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", port: 0))
XCTAssertNil(interface.broadcastAddress)
XCTAssertNil(interface.pointToPointDestinationAddress)
}
}
XCTAssertTrue(ipv4LoopbackPresent || ipv6LoopbackPresent)
}
func testEnumeratingDevices() throws {
// This is a tricky test, because we can't really assert much and expect this
// to pass on all systems. The best we can do is assume there is a loopback:
// maybe an IPv4 one, maybe an IPv6 one, but there will be one. We look for
// both.
let devices = try System.enumerateDevices()
XCTAssertGreaterThan(devices.count, 0)
var ipv4LoopbackPresent = false
var ipv6LoopbackPresent = false
for device in devices {
if try device.address == SocketAddress(ipAddress: "127.0.0.1", port: 0) {
ipv4LoopbackPresent = true
XCTAssertEqual(device.netmask, try SocketAddress(ipAddress: "255.0.0.0", port: 0))
XCTAssertNil(device.broadcastAddress)
XCTAssertNil(device.pointToPointDestinationAddress)
} else if try device.address == SocketAddress(ipAddress: "::1", port: 0) {
ipv6LoopbackPresent = true
XCTAssertEqual(device.netmask, try SocketAddress(ipAddress: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", port: 0))
XCTAssertNil(device.broadcastAddress)
XCTAssertNil(device.pointToPointDestinationAddress)
}
}
XCTAssertTrue(ipv4LoopbackPresent || ipv6LoopbackPresent)
}
}