diff --git a/Package.swift b/Package.swift index 453eb8c6..2a2d975a 100644 --- a/Package.swift +++ b/Package.swift @@ -77,6 +77,8 @@ var targets: [PackageDescription.Target] = [ dependencies: ["NIOPosix", "NIOCore", "NIOEmbedded", "NIOHTTP1", "NIOWebSocket", "NIOFoundationCompat"]), .target(name: "NIOAsyncAwaitDemo", dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]), + .testTarget(name: "NIOCoreTests", + dependencies: ["NIOCore"]), .testTarget(name: "NIOEmbeddedTests", dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]), .testTarget(name: "NIOPosixTests", diff --git a/Sources/NIOCore/AddressedEnvelope.swift b/Sources/NIOCore/AddressedEnvelope.swift index 9b356a51..2f6f86b2 100644 --- a/Sources/NIOCore/AddressedEnvelope.swift +++ b/Sources/NIOCore/AddressedEnvelope.swift @@ -58,6 +58,10 @@ extension AddressedEnvelope: CustomStringConvertible { } } +extension AddressedEnvelope: Equatable where DataType: Equatable {} + +extension AddressedEnvelope: Hashable where DataType: Hashable {} + /// Possible Explicit Congestion Notification States public enum NIOExplicitCongestionNotificationState: Hashable { /// Non-ECN Capable Transport. diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index e421efed..60b55b0c 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -24,6 +24,7 @@ import XCTest #if os(Linux) || os(FreeBSD) || os(Android) @testable import NIOConcurrencyHelpersTests + @testable import NIOCoreTests @testable import NIODataStructuresTests @testable import NIOEmbeddedTests @testable import NIOFoundationCompatTests @@ -45,6 +46,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner { XCTMain([ testCase(AcceptBackoffHandlerTest.allTests), testCase(AdaptiveRecvByteBufferAllocatorTest.allTests), + testCase(AddressedEnvelopeTests.allTests), testCase(ApplicationProtocolNegotiationHandlerTests.allTests), testCase(Base64Test.allTests), testCase(BaseObjectTest.allTests), diff --git a/Tests/NIOCoreTests/AddressedEnvelopeTests+XCTest.swift b/Tests/NIOCoreTests/AddressedEnvelopeTests+XCTest.swift new file mode 100644 index 00000000..c3374692 --- /dev/null +++ b/Tests/NIOCoreTests/AddressedEnvelopeTests+XCTest.swift @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 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 +// +//===----------------------------------------------------------------------===// +// +// AddressedEnvelopeTests+XCTest.swift +// +import XCTest + +/// +/// NOTE: This file was generated by generate_linux_tests.rb +/// +/// Do NOT edit this file directly as it will be regenerated automatically when needed. +/// + +extension AddressedEnvelopeTests { + + @available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings") + static var allTests : [(String, (AddressedEnvelopeTests) -> () throws -> Void)] { + return [ + ("testHashable_whenEqual", testHashable_whenEqual), + ("testHashable_whenDifferentData", testHashable_whenDifferentData), + ("testHashable_whenDifferentAddress", testHashable_whenDifferentAddress), + ("testHashable_whenDifferentMetadata", testHashable_whenDifferentMetadata), + ("testHashable_whenDifferentData_andDifferentAddress", testHashable_whenDifferentData_andDifferentAddress), + ("testHashable_whenDifferentData_andDifferentMetadata", testHashable_whenDifferentData_andDifferentMetadata), + ("testHashable_whenDifferentAddress_andDifferentMetadata", testHashable_whenDifferentAddress_andDifferentMetadata), + ] + } +} + diff --git a/Tests/NIOCoreTests/AddressedEnvelopeTests.swift b/Tests/NIOCoreTests/AddressedEnvelopeTests.swift new file mode 100644 index 00000000..c2878cd7 --- /dev/null +++ b/Tests/NIOCoreTests/AddressedEnvelopeTests.swift @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 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 NIOCore +import XCTest + +final class AddressedEnvelopeTests: XCTestCase { + func testHashable_whenEqual() throws { + let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443) + let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo") + let envelope2 = AddressedEnvelope(remoteAddress: address, data: "foo") + + XCTAssertEqual(envelope1, envelope2) + XCTAssertEqual(envelope1.hashValue, envelope2.hashValue) + } + + func testHashable_whenDifferentData() throws { + let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443) + let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo") + let envelope2 = AddressedEnvelope(remoteAddress: address, data: "bar") + + XCTAssertNotEqual(envelope1, envelope2) + } + + func testHashable_whenDifferentAddress() throws { + let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443) + let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444) + let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo") + let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "foo") + + XCTAssertNotEqual(envelope1, envelope2) + } + + func testHashable_whenDifferentMetadata() throws { + let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443) + let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .congestionExperienced)) + let envelope2 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .transportCapableFlag0)) + + XCTAssertNotEqual(envelope1, envelope2) + } + + func testHashable_whenDifferentData_andDifferentAddress() throws { + let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443) + let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444) + let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo") + let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "bar") + + XCTAssertNotEqual(envelope1, envelope2) + } + + func testHashable_whenDifferentData_andDifferentMetadata() throws { + let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443) + let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .congestionExperienced)) + let envelope2 = AddressedEnvelope(remoteAddress: address, data: "bar", metadata: .init(ecnState: .transportCapableFlag0)) + + XCTAssertNotEqual(envelope1, envelope2) + } + + func testHashable_whenDifferentAddress_andDifferentMetadata() throws { + let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443) + let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444) + let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo", metadata: .init(ecnState: .congestionExperienced)) + let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "bar", metadata: .init(ecnState: .transportCapableFlag0)) + + XCTAssertNotEqual(envelope1, envelope2) + } +}