| ... | @@ -26,6 +26,62 @@ test "parse and render IP addresses at comptime" { | ... | @@ -26,6 +26,62 @@ test "parse and render IP addresses at comptime" { |
| 26 | } | 26 | } |
| 27 | } | 27 | } |
| 28 | | 28 | |
| | 29 | test "format IPv6 address with no zero runs" { |
| | 30 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| | 31 | |
| | 32 | const addr = try std.net.Address.parseIp6("2001:db8:1:2:3:4:5:6", 0); |
| | 33 | |
| | 34 | var buffer: [50]u8 = undefined; |
| | 35 | const result = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable; |
| | 36 | |
| | 37 | try std.testing.expectEqualStrings("[2001:db8:1:2:3:4:5:6]:0", result); |
| | 38 | } |
| | 39 | |
| | 40 | test "parse IPv6 addresses and check compressed form" { |
| | 41 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| | 42 | |
| | 43 | const alloc = testing.allocator; |
| | 44 | |
| | 45 | // 1) Parse an IPv6 address that should compress to [2001:db8::1:0:0:2]:0 |
| | 46 | const addr1 = try std.net.Address.parseIp6("2001:0db8:0000:0000:0001:0000:0000:0002", 0); |
| | 47 | |
| | 48 | // 2) Parse an IPv6 address that should compress to [2001:db8::1:2]:0 |
| | 49 | const addr2 = try std.net.Address.parseIp6("2001:0db8:0000:0000:0000:0000:0001:0002", 0); |
| | 50 | |
| | 51 | // 3) Parse an IPv6 address that should compress to [2001:db8:1:0:1::2]:0 |
| | 52 | const addr3 = try std.net.Address.parseIp6("2001:0db8:0001:0000:0001:0000:0000:0002", 0); |
| | 53 | |
| | 54 | // Print each address in Zig's default "[ipv6]:port" form. |
| | 55 | const printed1 = try std.fmt.allocPrint(alloc, "{any}", .{addr1}); |
| | 56 | defer testing.allocator.free(printed1); |
| | 57 | const printed2 = try std.fmt.allocPrint(alloc, "{any}", .{addr2}); |
| | 58 | defer testing.allocator.free(printed2); |
| | 59 | const printed3 = try std.fmt.allocPrint(alloc, "{any}", .{addr3}); |
| | 60 | defer testing.allocator.free(printed3); |
| | 61 | |
| | 62 | // Check the exact compressed forms we expect. |
| | 63 | try std.testing.expectEqualStrings("[2001:db8::1:0:0:2]:0", printed1); |
| | 64 | try std.testing.expectEqualStrings("[2001:db8::1:2]:0", printed2); |
| | 65 | try std.testing.expectEqualStrings("[2001:db8:1:0:1::2]:0", printed3); |
| | 66 | } |
| | 67 | |
| | 68 | test "parse IPv6 address, check raw bytes" { |
| | 69 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| | 70 | |
| | 71 | const expected_raw: [16]u8 = .{ |
| | 72 | 0x20, 0x01, 0x0d, 0xb8, // 2001:db8 |
| | 73 | 0x00, 0x00, 0x00, 0x00, // :0000:0000 |
| | 74 | 0x00, 0x01, 0x00, 0x00, // :0001:0000 |
| | 75 | 0x00, 0x00, 0x00, 0x02, // :0000:0002 |
| | 76 | }; |
| | 77 | |
| | 78 | const addr = try std.net.Address.parseIp6("2001:db8:0000:0000:0001:0000:0000:0002", 0); |
| | 79 | |
| | 80 | const actual_raw = addr.in6.sa.addr[0..]; |
| | 81 | try std.testing.expectEqualSlices(u8, expected_raw[0..], actual_raw); |
| | 82 | |
| | 83 | } |
| | 84 | |
| 29 | test "parse and render IPv6 addresses" { | 85 | test "parse and render IPv6 addresses" { |
| 30 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 86 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 31 | | 87 | |