diff options
Diffstat (limited to 'lib/std/net/test.zig')
| -rw-r--r-- | lib/std/net/test.zig | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/lib/std/net/test.zig b/lib/std/net/test.zig index 815ee81d7f..8a7b5717f2 100644 --- a/lib/std/net/test.zig +++ b/lib/std/net/test.zig @@ -95,22 +95,79 @@ test "parse and render IPv4 addresses" { } test "resolve DNS" { + if (builtin.os.tag == .wasi) return error.SkipZigTest; + if (std.builtin.os.tag == .windows) { _ = try std.os.windows.WSAStartup(2, 2); } - if (builtin.os.tag == .wasi) { - // DNS resolution not implemented on Windows yet. - return error.SkipZigTest; + defer { + if (std.builtin.os.tag == .windows) { + std.os.windows.WSACleanup() catch unreachable; + } + } + + // Resolve localhost, this should not fail. + { + const localhost_v4 = try net.Address.parseIp("127.0.0.1", 80); + const localhost_v6 = try net.Address.parseIp("::2", 80); + + const result = try net.getAddressList(testing.allocator, "localhost", 80); + defer result.deinit(); + for (result.addrs) |addr| { + if (addr.eql(localhost_v4) or addr.eql(localhost_v6)) break; + } else @panic("unexpected address for localhost"); } - const address_list = net.getAddressList(testing.allocator, "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, + const result = net.getAddressList(testing.allocator, "example.com", 80) catch |err| switch (err) { + error.UnknownHostName => return error.SkipZigTest, + error.TemporaryNameServerFailure => return error.SkipZigTest, + else => return err, + }; + result.deinit(); + } +} + +test "listen on a port, send bytes, receive bytes" { + if (builtin.single_threaded) return error.SkipZigTest; + if (builtin.os.tag == .wasi) return error.SkipZigTest; + + if (std.builtin.os.tag == .windows) { + _ = try std.os.windows.WSAStartup(2, 2); + } + defer { + if (std.builtin.os.tag == .windows) { + std.os.windows.WSACleanup() catch unreachable; + } + } + + // Try only the IPv4 variant as some CI builders have no IPv6 localhost + // configured. + const localhost = try net.Address.parseIp("127.0.0.1", 8080); + + var server = net.StreamServer.init(.{}); + try server.listen(localhost); + + const S = struct { + fn clientFn(server_address: net.Address) !void { + const socket = try net.tcpConnectToAddress(server_address); + defer socket.close(); + + _ = try socket.writer().writeAll("Hello world!"); + } }; - address_list.deinit(); + + const t = try std.Thread.spawn(server.listen_address, S.clientFn); + defer t.wait(); + + var client = try server.accept(); + var buf: [16]u8 = undefined; + const n = try client.file.reader().read(&buf); + + testing.expectEqual(@as(usize, 12), n); + testing.expectEqualSlices(u8, "Hello world!", buf[0..n]); } test "listen on a port, send bytes, receive bytes" { |
