| ... | @@ -46,46 +46,26 @@ test "parse IPv6 address, check raw bytes" { | ... | @@ -46,46 +46,26 @@ test "parse IPv6 address, check raw bytes" { |
| 46 | } | 46 | } |
| 47 | | 47 | |
| 48 | test "parse and render IPv6 addresses" { | 48 | test "parse and render IPv6 addresses" { |
| 49 | // TODO make this test parsing and rendering only, then it doesn't need I/O | 49 | try testParseAndRenderIp6Address("FF01:0:0:0:0:0:0:FB", "ff01::fb"); |
| 50 | const io = testing.io; | 50 | try testParseAndRenderIp6Address("FF01::Fb", "ff01::fb"); |
| | 51 | try testParseAndRenderIp6Address("::1", "::1"); |
| | 52 | try testParseAndRenderIp6Address("::", "::"); |
| | 53 | try testParseAndRenderIp6Address("1::", "1::"); |
| | 54 | try testParseAndRenderIp6Address("2001:db8::", "2001:db8::"); |
| | 55 | try testParseAndRenderIp6Address("::1234:5678", "::1234:5678"); |
| | 56 | try testParseAndRenderIp6Address("2001:db8::1234:5678", "2001:db8::1234:5678"); |
| | 57 | try testParseAndRenderIp6Address("FF01::FB%1234", "ff01::fb%1234"); |
| | 58 | try testParseAndRenderIp6Address("::ffff:123.5.123.5", "::ffff:123.5.123.5"); |
| | 59 | } |
| 51 | | 60 | |
| | 61 | fn testParseAndRenderIp6Address(input: []const u8, expected_output: []const u8) !void { |
| 52 | var buffer: [100]u8 = undefined; | 62 | var buffer: [100]u8 = undefined; |
| 53 | const ips = [_][]const u8{ | 63 | const parsed = net.Ip6Address.Unresolved.parse(input); |
| 54 | "FF01:0:0:0:0:0:0:FB", | 64 | const actual_printed = try std.fmt.bufPrint(&buffer, "{f}", .{parsed.success}); |
| 55 | "FF01::Fb", | 65 | try testing.expectEqualStrings(expected_output, actual_printed); |
| 56 | "::1", | 66 | } |
| 57 | "::", | | |
| 58 | "1::", | | |
| 59 | "2001:db8::", | | |
| 60 | "::1234:5678", | | |
| 61 | "2001:db8::1234:5678", | | |
| 62 | "FF01::FB%1234", | | |
| 63 | "::ffff:123.5.123.5", | | |
| 64 | }; | | |
| 65 | const printed = [_][]const u8{ | | |
| 66 | "ff01::fb", | | |
| 67 | "ff01::fb", | | |
| 68 | "::1", | | |
| 69 | "::", | | |
| 70 | "1::", | | |
| 71 | "2001:db8::", | | |
| 72 | "::1234:5678", | | |
| 73 | "2001:db8::1234:5678", | | |
| 74 | "ff01::fb%1234", | | |
| 75 | "::ffff:123.5.123.5", | | |
| 76 | }; | | |
| 77 | for (ips, 0..) |ip, i| { | | |
| 78 | const addr = net.IpAddress.parseIp6(ip, 0) catch unreachable; | | |
| 79 | var newIp = std.fmt.bufPrint(buffer[0..], "{f}", .{addr}) catch unreachable; | | |
| 80 | try testing.expect(std.mem.eql(u8, printed[i], newIp[1 .. newIp.len - 3])); | | |
| 81 | | | |
| 82 | if (builtin.os.tag == .linux) { | | |
| 83 | const addr_via_resolve = net.IpAddress.resolveIp6(io, ip, 0) catch unreachable; | | |
| 84 | var newResolvedIp = std.fmt.bufPrint(buffer[0..], "{f}", .{addr_via_resolve}) catch unreachable; | | |
| 85 | try testing.expect(std.mem.eql(u8, printed[i], newResolvedIp[1 .. newResolvedIp.len - 3])); | | |
| 86 | } | | |
| 87 | } | | |
| 88 | | 67 | |
| | 68 | test "IPv6 address parse failures" { |
| 89 | try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6(":::", 0)); | 69 | try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6(":::", 0)); |
| 90 | try testing.expectError(error.Overflow, net.IpAddress.parseIp6("FF001::FB", 0)); | 70 | try testing.expectError(error.Overflow, net.IpAddress.parseIp6("FF001::FB", 0)); |
| 91 | try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6("FF01::Fb:zig", 0)); | 71 | try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6("FF01::Fb:zig", 0)); |
| ... | @@ -93,13 +73,9 @@ test "parse and render IPv6 addresses" { | ... | @@ -93,13 +73,9 @@ test "parse and render IPv6 addresses" { |
| 93 | try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("FF01:", 0)); | 73 | try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("FF01:", 0)); |
| 94 | try testing.expectError(error.InvalidIpv4Mapping, net.IpAddress.parseIp6("::123.123.123.123", 0)); | 74 | try testing.expectError(error.InvalidIpv4Mapping, net.IpAddress.parseIp6("::123.123.123.123", 0)); |
| 95 | try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("1", 0)); | 75 | try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("1", 0)); |
| 96 | // TODO Make this test pass on other operating systems. | 76 | try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("ff01::fb%", 0)); |
| 97 | if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin() or builtin.os.tag == .windows) { | 77 | try testing.expectError(error.Overflow, net.IpAddress.parseIp6("ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2), 0)); |
| 98 | try testing.expectError(error.Incomplete, net.IpAddress.resolveIp6(io, "ff01::fb%", 0)); | 78 | try testing.expectError(error.Overflow, net.IpAddress.parseIp6("ff01::fb%12345678901234", 0)); |
| 99 | // Assumes IFNAMESIZE will always be a multiple of 2 | | |
| 100 | try testing.expectError(error.Overflow, net.IpAddress.resolveIp6(io, "ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2), 0)); | | |
| 101 | try testing.expectError(error.Overflow, net.IpAddress.resolveIp6(io, "ff01::fb%12345678901234", 0)); | | |
| 102 | } | | |
| 103 | } | 79 | } |
| 104 | | 80 | |
| 105 | test "invalid but parseable IPv6 scope ids" { | 81 | test "invalid but parseable IPv6 scope ids" { |