Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/ContainerizationExtras/IPv4Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct IPv4Address: Sendable, Hashable, CustomStringConvertible, Equatabl
self.value =
(UInt32(bytes[0]) << 24)
| (UInt32(bytes[1]) << 16)
| (UInt32(bytes[2]) << 16)
| (UInt32(bytes[2]) << 8)
| UInt32(bytes[3])
}

Expand Down
32 changes: 32 additions & 0 deletions Tests/ContainerizationExtrasTests/TestIPv4Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ struct IPv4AddressTests {
try IPv4Address(invalidAddress)
}
}

@Test(
"byte array initializer - valid addresses",
arguments: [
([UInt8(127), 0, 0, 1], UInt32(0x7F00_0001)), // localhost
([UInt8(0), 0, 0, 0], UInt32(0x0000_0000)), // zero address
([UInt8(255), 255, 255, 255], UInt32(0xFFFF_FFFF)), // broadcast
([UInt8(192), 168, 1, 1], UInt32(0xC0A8_0101)), // private network
([UInt8(0x12), 0x34, 0x56, 0x78], UInt32(0x1234_5678)), // byte order test
]
)
func testByteArrayInitializerValid(bytes: [UInt8], expectedValue: UInt32) throws {
let address = try IPv4Address(bytes)
#expect(address.value == expectedValue)
// The byte array initializer must round-trip with the bytes property.
#expect(address.bytes == bytes)
}

@Test(
"byte array initializer - invalid lengths",
arguments: [
[], // empty
[UInt8(1)], // too short
[UInt8(1), 2, 3], // too short
[UInt8(1), 2, 3, 4, 5], // too long
]
)
func testByteArrayInitializerInvalid(bytes: [UInt8]) {
#expect(throws: AddressError.self) {
try IPv4Address(bytes)
}
}
}

// MARK: - Property Tests
Expand Down