authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-30 12:33:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-30 12:33:47-04:00
logf5ff36271bce54eaec6f20cdff4fa4efb833c0d4
tree8773174d52c67f23b723a1f59a437fc6c6d52b58
parent0fb1388031ec98af77fe5e0ae8459c983010d1a8
parent4e0c2ed443a0c13030decf8a9d53aff28f415005
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'testAddresses' of https://github.com/marler8997/zig into std.net


2 files changed, 38 insertions(+), 10 deletions(-)

lib/std/net.zig+22-8
...@@ -29,16 +29,30 @@ pub const Address = struct {...@@ -29,16 +29,30 @@ pub const Address = struct {
29 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);29 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
3030
31 pub fn initIp4(ip4: u32, _port: u16) Address {31 pub fn initIp4(ip4: u32, _port: u16) Address {
32 return Address{32 switch (builtin.os) {
33 .os_addr = os.sockaddr{33 .macosx, .ios, .watchos, .tvos, .freebsd, .netbsd => return Address{
34 .in = os.sockaddr_in{34 .os_addr = os.sockaddr{
35 .family = os.AF_INET,35 .in = os.sockaddr_in{
36 .port = mem.nativeToBig(u16, _port),36 .len = @sizeOf(os.sockaddr_in),
37 .addr = ip4,37 .family = os.AF_INET,
38 .zero = [_]u8{0} ** 8,38 .port = mem.nativeToBig(u16, _port),
39 .addr = ip4,
40 .zero = [_]u8{0} ** 8,
41 },
39 },42 },
40 },43 },
41 };44 .linux => return Address{
45 .os_addr = os.sockaddr{
46 .in = os.sockaddr_in{
47 .family = os.AF_INET,
48 .port = mem.nativeToBig(u16, _port),
49 .addr = ip4,
50 .zero = [_]u8{0} ** 8,
51 },
52 },
53 },
54 else => @compileError("Address.initIp4 not implemented for this platform"),
55 }
42 }56 }
4357
44 pub fn initIp6(ip6: Ip6Addr, _port: u16) Address {58 pub fn initIp6(ip6: Ip6Addr, _port: u16) Address {
lib/std/net/test.zig+16-2
...@@ -3,7 +3,7 @@ const net = std.net;...@@ -3,7 +3,7 @@ const net = std.net;
3const mem = std.mem;3const mem = std.mem;
4const testing = std.testing;4const testing = std.testing;
55
6test "std.net.parseIp4" {6test "parseIp4" {
7 testing.expect((try net.parseIp4("127.0.0.1")) == mem.bigToNative(u32, 0x7f000001));7 testing.expect((try net.parseIp4("127.0.0.1")) == mem.bigToNative(u32, 0x7f000001));
88
9 testParseIp4Fail("256.0.0.1", error.Overflow);9 testParseIp4Fail("256.0.0.1", error.Overflow);
...@@ -21,7 +21,7 @@ fn testParseIp4Fail(buf: []const u8, expected_err: anyerror) void {...@@ -21,7 +21,7 @@ fn testParseIp4Fail(buf: []const u8, expected_err: anyerror) void {
21 }21 }
22}22}
2323
24test "std.net.parseIp6" {24test "parseIp6" {
25 const ip6 = try net.parseIp6("FF01:0:0:0:0:0:0:FB");25 const ip6 = try net.parseIp6("FF01:0:0:0:0:0:0:FB");
26 const addr = net.Address.initIp6(ip6, 80);26 const addr = net.Address.initIp6(ip6, 80);
27 var buf: [100]u8 = undefined;27 var buf: [100]u8 = undefined;
...@@ -29,6 +29,20 @@ test "std.net.parseIp6" {...@@ -29,6 +29,20 @@ test "std.net.parseIp6" {
29 std.testing.expect(mem.eql(u8, "[ff01::fb]:80", printed));29 std.testing.expect(mem.eql(u8, "[ff01::fb]:80", printed));
30}30}
3131
32test "ip4s" {
33 var buffer: [18]u8 = undefined;
34 for ([_][]const u8{
35 "0.0.0.0",
36 "255.255.255.255",
37 "1.2.3.4",
38 "123.255.0.91",
39 }) |ip| {
40 var addr = Address.initIp4(parseIp4(ip) catch unreachable, 0);
41 var newIp = std.fmt.bufPrint(buffer[0..], "{}", addr) catch unreachable;
42 std.testing.expect(std.mem.eql(u8, ip, newIp[0 .. newIp.len - 2]));
43 }
44}
45
32test "resolve DNS" {46test "resolve DNS" {
33 if (std.builtin.os == .windows) {47 if (std.builtin.os == .windows) {
34 // DNS resolution not implemented on Windows yet.48 // DNS resolution not implemented on Windows yet.