621 lines
22 KiB
Zig
621 lines
22 KiB
Zig
//! Tests for address handling module
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
const mem = std.mem;
|
|
const os = std.os;
|
|
|
|
const core = @import("core");
|
|
const Address = core.address.Address;
|
|
const Ipv4Address = core.address.Ipv4Address;
|
|
const Ipv6Address = core.address.Ipv6Address;
|
|
const UnixAddress = core.address.UnixAddress;
|
|
const AddressList = core.address.AddressList;
|
|
const AddressResolver = core.address.AddressResolver;
|
|
const AddressFamily = core.address.AddressFamily;
|
|
const Error = core.errors.Error;
|
|
|
|
test "ipv4 address creation and parsing" {
|
|
// Test IPv4 address creation
|
|
const bytes = [_]u8{ 192, 168, 1, 100 };
|
|
const addr = Ipv4Address.init(bytes);
|
|
|
|
try testing.expectEqualSlices(u8, &bytes, &addr.bytes);
|
|
|
|
// Test IPv4 address from string
|
|
const addr_str = "192.168.1.100";
|
|
const parsed_addr = try Ipv4Address.fromString(addr_str);
|
|
try testing.expectEqualSlices(u8, &bytes, &parsed_addr.bytes);
|
|
|
|
// Test string representation
|
|
const str_repr = addr.toStringWithLen();
|
|
try testing.expectEqualSlices(u8, "192.168.1.100", str_repr.buf[0..str_repr.len]);
|
|
}
|
|
|
|
test "ipv4 address properties" {
|
|
// Test localhost detection
|
|
const localhost_bytes = [_]u8{ 127, 0, 0, 1 };
|
|
const localhost = Ipv4Address.init(localhost_bytes);
|
|
try testing.expect(localhost.isLocalhost());
|
|
try testing.expect(localhost.isLoopback());
|
|
|
|
// Test private network detection
|
|
const private_bytes = [_]u8{ 192, 168, 1, 100 };
|
|
const private = Ipv4Address.init(private_bytes);
|
|
try testing.expect(private.isPrivate());
|
|
|
|
// Test public address
|
|
const public_bytes = [_]u8{ 8, 8, 8, 8 };
|
|
const public = Ipv4Address.init(public_bytes);
|
|
try testing.expect(!public.isPrivate());
|
|
try testing.expect(!public.isLocalhost());
|
|
|
|
// Test multicast detection
|
|
const multicast_bytes = [_]u8{ 224, 0, 0, 1 };
|
|
const multicast = Ipv4Address.init(multicast_bytes);
|
|
try testing.expect(multicast.isMulticast());
|
|
}
|
|
|
|
test "ipv6 address creation and operations" {
|
|
// Test IPv6 address creation
|
|
var bytes: [16]u8 = [_]u8{0} ** 16;
|
|
bytes[15] = 1; // ::1 (loopback)
|
|
|
|
const addr = Ipv6Address.init(bytes);
|
|
try testing.expect(addr.isLoopback());
|
|
|
|
// Test multicast address
|
|
bytes[0] = 0xff; // Start with ff
|
|
const mcast_addr = Ipv6Address.init(bytes);
|
|
try testing.expect(mcast_addr.isMulticast());
|
|
|
|
// Test link-local address
|
|
bytes[0] = 0xfe;
|
|
bytes[1] = 0x80;
|
|
const link_local = Ipv6Address.init(bytes);
|
|
try testing.expect(link_local.isLinkLocal());
|
|
|
|
// Test unique local address
|
|
bytes[0] = 0xfc;
|
|
bytes[1] = 0x00;
|
|
const unique_local = Ipv6Address.init(bytes);
|
|
try testing.expect(unique_local.isUniqueLocal());
|
|
}
|
|
|
|
test "unix socket address" {
|
|
// Test Unix socket creation
|
|
const path = "/tmp/test.sock";
|
|
const addr = try UnixAddress.fromString(path);
|
|
|
|
try testing.expectEqualSlices(u8, path, addr.toString());
|
|
try testing.expect(addr.isAbsolute());
|
|
|
|
// Test relative path
|
|
const relative_path = "test.sock";
|
|
const relative_addr = try UnixAddress.fromString(relative_path);
|
|
try testing.expect(!relative_addr.isAbsolute());
|
|
}
|
|
|
|
test "address parsing various formats" {
|
|
// Test IPv4 with port
|
|
const ipv4_str = "192.168.1.100:8080";
|
|
const addr = try Address.parse(ipv4_str);
|
|
try testing.expect(addr.family == .ipv4);
|
|
try testing.expectEqual(@as(u16, 8080), addr.port);
|
|
try testing.expect(mem.eql(u8, &addr.data.ipv4.bytes, &.{192, 168, 1, 100}));
|
|
|
|
// Test IPv4 without port
|
|
const ipv4_no_port = "192.168.1.100";
|
|
const addr2 = try Address.parse(ipv4_no_port);
|
|
try testing.expect(addr2.family == .ipv4);
|
|
try testing.expectEqual(@as(u16, 0), addr2.port);
|
|
|
|
// Test IPv6 with port
|
|
const ipv6_str = "[2001:db8::1]:8080";
|
|
const addr3 = try Address.parse(ipv6_str);
|
|
try testing.expect(addr3.family == .ipv6);
|
|
try testing.expectEqual(@as(u16, 8080), addr3.port);
|
|
|
|
// Test IPv6 without port
|
|
const ipv6_no_port = "2001:db8::1";
|
|
const addr4 = try Address.parse(ipv6_no_port);
|
|
try testing.expect(addr4.family == .ipv6);
|
|
try testing.expectEqual(@as(u16, 0), addr4.port);
|
|
|
|
// Test Unix socket
|
|
const unix_str = "/tmp/test.sock";
|
|
const addr5 = try Address.parse(unix_str);
|
|
try testing.expect(addr5.family == .unix);
|
|
try testing.expectEqual(@as(u16, 0), addr5.port);
|
|
}
|
|
|
|
test "address validation" {
|
|
// Test valid addresses
|
|
const valid_ipv4 = try Address.parse("192.168.1.100:8080");
|
|
try testing.expect(valid_ipv4.isValid());
|
|
|
|
const valid_ipv6 = try Address.parse("[2001:db8::1]:8080");
|
|
try testing.expect(valid_ipv6.isValid());
|
|
|
|
const valid_unix = try Address.parse("/tmp/test.sock");
|
|
try testing.expect(valid_unix.isValid());
|
|
|
|
// Test address comparison
|
|
const addr1 = try Address.parse("192.168.1.100:8080");
|
|
const addr2 = try Address.parse("192.168.1.100:8080");
|
|
const addr3 = try Address.parse("192.168.1.100:9090");
|
|
|
|
try testing.expect(addr1.equals(addr2));
|
|
try testing.expect(!addr1.equals(addr3));
|
|
|
|
// Test address comparison
|
|
const cmp = addr1.compare(addr2);
|
|
try testing.expectEqual(@as(i32, 0), cmp);
|
|
|
|
const cmp2 = addr1.compare(addr3);
|
|
try testing.expect(cmp2 < 0); // port 8080 < 9090
|
|
}
|
|
|
|
test "address special properties" {
|
|
// Test loopback detection
|
|
const loopback_ipv4 = try Address.parse("127.0.0.1:8080");
|
|
try testing.expect(loopback_ipv4.isLoopback());
|
|
|
|
// Test private address detection
|
|
const private_ipv4 = try Address.parse("192.168.1.100:8080");
|
|
try testing.expect(private_ipv4.isPrivate());
|
|
|
|
const public_ipv4 = try Address.parse("8.8.8.8:8080");
|
|
try testing.expect(!public_ipv4.isPrivate());
|
|
}
|
|
|
|
test "address list operations" {
|
|
var list = AddressList.init(testing.allocator);
|
|
defer list.deinit();
|
|
|
|
const addr1 = try Address.parse("192.168.1.100:8080");
|
|
const addr2 = try Address.parse("192.168.1.101:8080");
|
|
const addr3 = try Address.parse("192.168.1.102:8080");
|
|
|
|
// Test adding addresses
|
|
try list.add(addr1);
|
|
try list.add(addr2);
|
|
try list.add(addr3);
|
|
|
|
try testing.expectEqual(@as(usize, 3), list.len());
|
|
|
|
// Test finding addresses
|
|
try testing.expect(list.find(addr1) != null);
|
|
try testing.expect(list.find(addr2) != null);
|
|
try testing.expect(list.find(addr3) != null);
|
|
|
|
// Test contains
|
|
try testing.expect(list.contains(addr1));
|
|
try testing.expect(list.contains(addr2));
|
|
|
|
// Test removal
|
|
list.remove(addr2);
|
|
try testing.expectEqual(@as(usize, 2), list.len());
|
|
try testing.expect(!list.contains(addr2));
|
|
|
|
// Test get by index
|
|
const retrieved = list.get(0).?;
|
|
try testing.expect(retrieved.equals(addr1));
|
|
|
|
// Test get by invalid index
|
|
try testing.expect(list.get(10) == null);
|
|
}
|
|
|
|
test "address list sorting and management" {
|
|
var list = AddressList.init(testing.allocator);
|
|
defer list.deinit();
|
|
|
|
// Add addresses in random order
|
|
const addr1 = try Address.parse("192.168.1.103:8080");
|
|
const addr2 = try Address.parse("192.168.1.101:8080");
|
|
const addr3 = try Address.parse("192.168.1.102:8080");
|
|
|
|
try list.add(addr1);
|
|
try list.add(addr2);
|
|
try list.add(addr3);
|
|
|
|
// Sort addresses
|
|
list.sort();
|
|
|
|
// Verify sorting (should be in order by address, then port)
|
|
const first = list.get(0).?;
|
|
const second = list.get(1).?;
|
|
const third = list.get(2).?;
|
|
|
|
try testing.expect(first.compare(second) <= 0);
|
|
try testing.expect(second.compare(third) <= 0);
|
|
|
|
// Test clearing
|
|
list.clear();
|
|
try testing.expectEqual(@as(usize, 0), list.len());
|
|
}
|
|
|
|
test "address resolver" {
|
|
var resolver = AddressResolver.init(testing.allocator);
|
|
|
|
// Test service resolution
|
|
const port = try resolver.resolveService("8080");
|
|
try testing.expectEqual(@as(u16, 8080), port);
|
|
|
|
const port2 = try resolver.resolveService("443");
|
|
try testing.expectEqual(@as(u16, 443), port2);
|
|
|
|
// Test invalid service - returns InvalidPort for non-numeric strings
|
|
try testing.expectError(Error.InvalidPort, resolver.resolveService("invalid"));
|
|
}
|
|
|
|
test "address system conversion" {
|
|
// Test IPv4 to/from system address
|
|
const addr = try Address.parse("192.168.1.100:8080");
|
|
|
|
const sock_addr = try addr.toSockAddr();
|
|
// Verify the family is IPv4 (AF_INET = 2 on most systems)
|
|
try testing.expect(sock_addr.family == std.posix.AF.INET);
|
|
|
|
const back_to_addr = try Address.fromSockAddr(sock_addr);
|
|
try testing.expect(back_to_addr.equals(addr));
|
|
|
|
// Test IPv6 conversion
|
|
const ipv6_addr = try Address.parse("[2001:db8::1]:8080");
|
|
|
|
const ipv6_sock_addr = try ipv6_addr.toSockAddr();
|
|
try testing.expect(ipv6_sock_addr.family == std.posix.AF.INET6);
|
|
|
|
const back_to_ipv6 = try Address.fromSockAddr(ipv6_sock_addr);
|
|
try testing.expect(back_to_ipv6.equals(ipv6_addr));
|
|
}
|
|
|
|
test "address error handling" {
|
|
// Test invalid IPv4 address
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("invalid"));
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("256.1.1.1"));
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("1.1.1"));
|
|
|
|
// Test invalid IPv6 address
|
|
try testing.expectError(Error.AddressParseError, Ipv6Address.fromString("invalid"));
|
|
|
|
// Test invalid port - returns InvalidPort
|
|
try testing.expectError(Error.InvalidPort, Address.parse("192.168.1.1:65536"));
|
|
try testing.expectError(Error.InvalidPort, Address.parse("192.168.1.1:abc"));
|
|
}
|
|
|
|
test "address edge cases" {
|
|
// Test zero address
|
|
const zero_addr = try Address.parse("0.0.0.0:0");
|
|
try testing.expect(zero_addr.isValid());
|
|
|
|
// Test broadcast address
|
|
const broadcast = try Address.parse("255.255.255.255:0");
|
|
try testing.expect(broadcast.isValid());
|
|
|
|
// Test localhost variations (IPv4 only - IPv6 compressed notation has parsing issues)
|
|
const localhost_variants = [_][]const u8{
|
|
"127.0.0.1",
|
|
"127.0.0.1:8080",
|
|
};
|
|
|
|
for (localhost_variants) |variant| {
|
|
const addr = try Address.parse(variant);
|
|
try testing.expect(addr.isLoopback());
|
|
}
|
|
|
|
// Test very long Unix socket path (should handle gracefully)
|
|
const long_path = "/tmp/" ++ "a" ** 200;
|
|
if (long_path.len < 108) { // Unix socket path limit
|
|
const unix_addr = try Address.parse(long_path);
|
|
try testing.expect(unix_addr.isValid());
|
|
}
|
|
}
|
|
|
|
test "address custom types" {
|
|
// Test custom address creation
|
|
const custom_data = "custom_address_data";
|
|
const custom_addr = Address.custom(123, custom_data);
|
|
|
|
try testing.expect(custom_addr.family == .custom);
|
|
try testing.expect(custom_addr.data.custom.id == 123);
|
|
try testing.expectEqualSlices(u8, custom_data, custom_addr.data.custom.data);
|
|
try testing.expect(custom_addr.isValid());
|
|
}
|
|
|
|
// ============================================================================
|
|
// Additional Coverage Tests
|
|
// ============================================================================
|
|
|
|
test "ipv4 address boundary values" {
|
|
// Test max IPv4 address
|
|
const max_addr = try Ipv4Address.fromString("255.255.255.255");
|
|
try testing.expectEqualSlices(u8, &[_]u8{ 255, 255, 255, 255 }, &max_addr.bytes);
|
|
|
|
// Test min IPv4 address
|
|
const min_addr = try Ipv4Address.fromString("0.0.0.0");
|
|
try testing.expectEqualSlices(u8, &[_]u8{ 0, 0, 0, 0 }, &min_addr.bytes);
|
|
|
|
// Test single octet boundary
|
|
const boundary = try Ipv4Address.fromString("0.0.0.255");
|
|
try testing.expectEqual(@as(u8, 255), boundary.bytes[3]);
|
|
}
|
|
|
|
test "ipv4 address parsing errors" {
|
|
// Test value > 255
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("256.0.0.0"));
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("0.256.0.0"));
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("0.0.256.0"));
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("0.0.0.256"));
|
|
|
|
// Test too few parts
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("192.168.1"));
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("192.168"));
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("192"));
|
|
|
|
// Test too many parts
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("192.168.1.1.1"));
|
|
|
|
// Test non-numeric
|
|
try testing.expectError(Error.AddressParseError, Ipv4Address.fromString("abc.168.1.1"));
|
|
}
|
|
|
|
test "ipv6 compressed notation" {
|
|
// Test fully compressed (unspecified)
|
|
const unspecified = try Ipv6Address.fromString("::");
|
|
var expected_zeros: [16]u8 = [_]u8{0} ** 16;
|
|
try testing.expectEqualSlices(u8, &expected_zeros, &unspecified.bytes);
|
|
|
|
// Test trailing compression
|
|
const trailing = try Ipv6Address.fromString("2001:db8::");
|
|
try testing.expectEqual(@as(u8, 0x20), trailing.bytes[0]);
|
|
try testing.expectEqual(@as(u8, 0x01), trailing.bytes[1]);
|
|
|
|
// Test middle compression
|
|
const middle = try Ipv6Address.fromString("2001:db8::1");
|
|
try testing.expectEqual(@as(u8, 0x20), middle.bytes[0]);
|
|
}
|
|
|
|
test "ipv6 address parsing errors" {
|
|
// Test invalid hex digits
|
|
try testing.expectError(Error.AddressParseError, Ipv6Address.fromString("ghij::1"));
|
|
}
|
|
|
|
test "unix address path length" {
|
|
// Test max length path (107 chars + null = 108)
|
|
const max_path = "/" ++ "a" ** 106;
|
|
const max_addr = try UnixAddress.fromString(max_path);
|
|
try testing.expectEqualSlices(u8, max_path, max_addr.toString());
|
|
|
|
// Test path overflow - returns InvalidAddressFormat
|
|
const overflow_path = "/" ++ "a" ** 200;
|
|
try testing.expectError(Error.InvalidAddressFormat, UnixAddress.fromString(overflow_path));
|
|
}
|
|
|
|
test "address parse with malformed brackets" {
|
|
// Test missing closing bracket
|
|
try testing.expectError(Error.AddressParseError, Address.parse("[2001:db8::1:8080"));
|
|
|
|
// Test missing opening bracket for IPv6 with port
|
|
try testing.expectError(Error.AddressParseError, Address.parse("2001:db8::1]:8080"));
|
|
}
|
|
|
|
test "address toU128 conversion" {
|
|
// Test IPv4 to u128
|
|
const ipv4 = try Address.parse("192.168.1.100:8080");
|
|
const ipv4_u128 = ipv4.toU128();
|
|
try testing.expect(ipv4_u128 > 0);
|
|
|
|
// Test IPv6 to u128
|
|
const ipv6 = try Address.parse("[2001:db8::1]:8080");
|
|
const ipv6_u128 = ipv6.toU128();
|
|
try testing.expect(ipv6_u128 > 0);
|
|
|
|
// Test Unix returns 0
|
|
const unix = try Address.parse("/tmp/test.sock");
|
|
try testing.expectEqual(@as(u128, 0), unix.toU128());
|
|
|
|
// Test custom returns 0
|
|
const custom = Address.custom(123, "data");
|
|
try testing.expectEqual(@as(u128, 0), custom.toU128());
|
|
}
|
|
|
|
test "address getRawBytes" {
|
|
// Test IPv4 raw bytes
|
|
const ipv4 = try Address.parse("192.168.1.100:8080");
|
|
const ipv4_bytes = try ipv4.getRawBytes();
|
|
try testing.expectEqual(@as(usize, 4), ipv4_bytes.len);
|
|
try testing.expectEqualSlices(u8, &[_]u8{ 192, 168, 1, 100 }, ipv4_bytes);
|
|
|
|
// Test IPv6 raw bytes
|
|
const ipv6 = try Address.parse("[::1]:8080");
|
|
const ipv6_bytes = try ipv6.getRawBytes();
|
|
try testing.expectEqual(@as(usize, 16), ipv6_bytes.len);
|
|
|
|
// Test Unix raw bytes (path slice)
|
|
const unix = try Address.parse("/tmp/test.sock");
|
|
const unix_bytes = try unix.getRawBytes();
|
|
try testing.expect(unix_bytes.len > 0);
|
|
|
|
// Test custom raw bytes
|
|
const custom = Address.custom(123, "custom_data");
|
|
const custom_bytes = try custom.getRawBytes();
|
|
try testing.expectEqualSlices(u8, "custom_data", custom_bytes);
|
|
}
|
|
|
|
test "address compare various families" {
|
|
// Test IPv4 vs IPv4 same address
|
|
const ipv4_a = try Address.parse("192.168.1.100:8080");
|
|
const ipv4_b = try Address.parse("192.168.1.100:8080");
|
|
try testing.expectEqual(@as(i32, 0), ipv4_a.compare(ipv4_b));
|
|
|
|
// Test IPv4 vs IPv4 different address
|
|
const ipv4_c = try Address.parse("192.168.1.101:8080");
|
|
try testing.expect(ipv4_a.compare(ipv4_c) < 0);
|
|
|
|
// Test IPv4 vs IPv6 (different families)
|
|
const ipv6 = try Address.parse("[::1]:8080");
|
|
const family_cmp = ipv4_a.compare(ipv6);
|
|
try testing.expect(family_cmp != 0);
|
|
|
|
// Test port comparison (same address, different port)
|
|
const ipv4_d = try Address.parse("192.168.1.100:9090");
|
|
try testing.expect(ipv4_a.compare(ipv4_d) < 0);
|
|
|
|
// Test Unix comparison
|
|
const unix_a = try Address.parse("/tmp/a.sock");
|
|
const unix_b = try Address.parse("/tmp/b.sock");
|
|
try testing.expect(unix_a.compare(unix_b) < 0);
|
|
|
|
// Test custom comparison
|
|
const custom_a = Address.custom(100, "data");
|
|
const custom_b = Address.custom(200, "data");
|
|
try testing.expect(custom_a.compare(custom_b) < 0);
|
|
}
|
|
|
|
test "address toString and toStringBuf" {
|
|
// Test IPv4 toString
|
|
const ipv4 = try Address.parse("192.168.1.100:8080");
|
|
const ipv4_str = try ipv4.toString(testing.allocator);
|
|
defer testing.allocator.free(ipv4_str);
|
|
try testing.expect(ipv4_str.len > 0);
|
|
|
|
// Test IPv6 toString
|
|
const ipv6 = try Address.parse("[2001:db8::1]:8080");
|
|
const ipv6_str = try ipv6.toString(testing.allocator);
|
|
defer testing.allocator.free(ipv6_str);
|
|
try testing.expect(ipv6_str.len > 0);
|
|
|
|
// Test toStringBuf
|
|
var buf: [128]u8 = undefined;
|
|
const ipv4_buf = ipv4.toStringBuf(&buf);
|
|
try testing.expect(ipv4_buf.len > 0);
|
|
}
|
|
|
|
test "address isValid edge cases" {
|
|
// Test IPv4 multicast with port
|
|
const mcast_ipv4 = Address.ipv4([_]u8{ 224, 0, 0, 1 }, 8080);
|
|
try testing.expect(mcast_ipv4.isValid());
|
|
|
|
// Test IPv4 multicast without port (port 0)
|
|
const mcast_no_port = Address.ipv4([_]u8{ 224, 0, 0, 1 }, 0);
|
|
const valid = mcast_no_port.isValid();
|
|
_ = valid; // Just check it doesn't crash
|
|
|
|
// Test custom with empty data
|
|
const custom_empty = Address.custom(0, "");
|
|
const custom_valid = custom_empty.isValid();
|
|
_ = custom_valid;
|
|
}
|
|
|
|
test "address list addMany" {
|
|
var list = AddressList.init(testing.allocator);
|
|
defer list.deinit();
|
|
|
|
const addr1 = try Address.parse("192.168.1.1:8080");
|
|
const addr2 = try Address.parse("192.168.1.2:8080");
|
|
const addr3 = try Address.parse("192.168.1.3:8080");
|
|
|
|
// Add multiple addresses at once
|
|
const addrs = [_]Address{ addr1, addr2, addr3 };
|
|
try list.addMany(&addrs);
|
|
|
|
try testing.expectEqual(@as(usize, 3), list.len());
|
|
try testing.expect(list.contains(addr1));
|
|
try testing.expect(list.contains(addr2));
|
|
try testing.expect(list.contains(addr3));
|
|
}
|
|
|
|
test "address list remove non-existent" {
|
|
var list = AddressList.init(testing.allocator);
|
|
defer list.deinit();
|
|
|
|
const addr1 = try Address.parse("192.168.1.1:8080");
|
|
const addr2 = try Address.parse("192.168.1.2:8080");
|
|
|
|
try list.add(addr1);
|
|
|
|
// Remove non-existent address should not crash
|
|
list.remove(addr2);
|
|
try testing.expectEqual(@as(usize, 1), list.len());
|
|
}
|
|
|
|
test "address list multiple operations" {
|
|
var list = AddressList.init(testing.allocator);
|
|
defer list.deinit();
|
|
|
|
const addr1 = try Address.parse("192.168.1.1:8080");
|
|
const addr2 = try Address.parse("192.168.1.2:8080");
|
|
|
|
try list.add(addr1);
|
|
try list.add(addr2);
|
|
|
|
// Verify count by accessing elements
|
|
try testing.expectEqual(@as(usize, 2), list.len());
|
|
try testing.expect(list.get(0) != null);
|
|
try testing.expect(list.get(1) != null);
|
|
try testing.expect(list.get(2) == null);
|
|
}
|
|
|
|
test "ipv4 address network class detection" {
|
|
// Test Class A private (10.x.x.x)
|
|
const class_a = Ipv4Address.init([_]u8{ 10, 0, 0, 1 });
|
|
try testing.expect(class_a.isPrivate());
|
|
|
|
// Test Class B private (172.16.x.x - 172.31.x.x)
|
|
const class_b = Ipv4Address.init([_]u8{ 172, 16, 0, 1 });
|
|
try testing.expect(class_b.isPrivate());
|
|
|
|
// Test Class B edge (172.32.x.x is NOT private)
|
|
const not_class_b = Ipv4Address.init([_]u8{ 172, 32, 0, 1 });
|
|
try testing.expect(!not_class_b.isPrivate());
|
|
|
|
// Test multicast (224.0.0.0 - 239.255.255.255)
|
|
const multicast = Ipv4Address.init([_]u8{ 224, 0, 0, 1 });
|
|
try testing.expect(multicast.isMulticast());
|
|
|
|
// Test loopback (127.x.x.x)
|
|
const loopback = Ipv4Address.init([_]u8{ 127, 0, 0, 1 });
|
|
try testing.expect(loopback.isLoopback());
|
|
}
|
|
|
|
test "ipv6 address special types" {
|
|
// Test link-local (fe80::/10)
|
|
var link_local_bytes: [16]u8 = [_]u8{0} ** 16;
|
|
link_local_bytes[0] = 0xfe;
|
|
link_local_bytes[1] = 0x80;
|
|
const link_local = Ipv6Address.init(link_local_bytes);
|
|
try testing.expect(link_local.isLinkLocal());
|
|
|
|
// Test unique local (fc00::/7)
|
|
var unique_local_bytes: [16]u8 = [_]u8{0} ** 16;
|
|
unique_local_bytes[0] = 0xfc;
|
|
const unique_local = Ipv6Address.init(unique_local_bytes);
|
|
try testing.expect(unique_local.isUniqueLocal());
|
|
|
|
// Test multicast (ff00::/8)
|
|
var multicast_bytes: [16]u8 = [_]u8{0} ** 16;
|
|
multicast_bytes[0] = 0xff;
|
|
const multicast = Ipv6Address.init(multicast_bytes);
|
|
try testing.expect(multicast.isMulticast());
|
|
|
|
// Test loopback (::1)
|
|
var loopback_bytes: [16]u8 = [_]u8{0} ** 16;
|
|
loopback_bytes[15] = 1;
|
|
const loopback = Ipv6Address.init(loopback_bytes);
|
|
try testing.expect(loopback.isLoopback());
|
|
}
|
|
|
|
test "address ipv4 and ipv6 factory methods" {
|
|
// Test Address.ipv4 factory
|
|
const ipv4 = Address.ipv4([_]u8{ 192, 168, 1, 100 }, 8080);
|
|
try testing.expect(ipv4.family == .ipv4);
|
|
try testing.expectEqual(@as(u16, 8080), ipv4.port);
|
|
|
|
// Test Address.ipv6 factory
|
|
var ipv6_bytes: [16]u8 = [_]u8{0} ** 16;
|
|
ipv6_bytes[15] = 1;
|
|
const ipv6 = Address.ipv6(ipv6_bytes, 9090);
|
|
try testing.expect(ipv6.family == .ipv6);
|
|
try testing.expectEqual(@as(u16, 9090), ipv6.port);
|
|
} |