Commit 0fb1388031

Andrew Kelley <andrew@ziglang.org>
2019-10-30 17:16:47
std.net: enable test for resolving DNS
1 parent 1639724
Changed files (2)
lib
lib/std/net/test.zig
@@ -29,6 +29,24 @@ test "std.net.parseIp6" {
     std.testing.expect(mem.eql(u8, "[ff01::fb]:80", printed));
 }
 
+test "resolve DNS" {
+    if (std.builtin.os == .windows) {
+        // DNS resolution not implemented on Windows yet.
+        return error.SkipZigTest;
+    }
+    var buf: [1000 * 10]u8 = undefined;
+    const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;
+
+    const address_list = net.getAddressList(a, "example.com", 80) catch |err| switch (err) {
+        // The tests are required to work even when there is no Internet connection,
+        // so some of these errors we must accept and skip the test.
+        error.UnknownHostName => return error.SkipZigTest,
+        error.TemporaryNameServerFailure => return error.SkipZigTest,
+        else => return err,
+    };
+    address_list.deinit();
+}
+
 test "listen on a port, send bytes, receive bytes" {
     if (std.builtin.os != .linux) {
         // TODO build abstractions for other operating systems
lib/std/net.zig
@@ -294,7 +294,7 @@ pub fn connectUnixSocket(path: []const u8) !fs.File {
     if (path.len > @typeOf(sock_addr.un.path).len) return error.NameTooLong;
     mem.copy(u8, sock_addr.un.path[0..], path);
     const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len);
-    try os.connect(sockfd, &sock_addr, size);
+    try os.connect(sockfd, sock_addr, size);
 
     return fs.File.openHandle(sockfd);
 }
@@ -561,7 +561,7 @@ fn linuxLookupName(
         var prefixlen: i32 = 0;
         if (os.socket(addr.family, os.SOCK_DGRAM | os.SOCK_CLOEXEC, os.IPPROTO_UDP)) |fd| syscalls: {
             defer os.close(fd);
-            os.connect(fd, da, dalen) catch break :syscalls;
+            os.connect(fd, da.*, dalen) catch break :syscalls;
             key |= DAS_USABLE;
             os.getsockname(fd, sa, &salen) catch break :syscalls;
             if (addr.family == os.AF_INET) {