authorgravatar for ian.simonson@protonmail.comIan Simonson <ian.simonson@protonmail.com> 2020-06-07 22:36:35+10:00
committergravatar for ian.simonson@protonmail.comIan Simonson <ian.simonson@protonmail.com> 2020-06-07 22:39:35+10:00
log983f93c84061c667b9286074428d0a0aecaddb15
treec56d81435f829c42008bc6391eae8a4bd0a8c97f
parent499df9680ceea203602ba1aebc475b047714dbf4

Test case for tcpConnectToHost fix


1 files changed, 38 insertions(+), 0 deletions(-)

lib/std/net/test.zig+38
...@@ -130,6 +130,44 @@ test "listen on a port, send bytes, receive bytes" {...@@ -130,6 +130,44 @@ test "listen on a port, send bytes, receive bytes" {
130 try await client_frame;130 try await client_frame;
131}131}
132132
133test "listen on ipv4 try connect on ipv6 then ipv4" {
134 if (!std.io.is_async) return error.SkipZigTest;
135
136 if (std.builtin.os.tag != .linux and !std.builtin.os.tag.isDarwin()) {
137 // TODO build abstractions for other operating systems
138 return error.SkipZigTest;
139 }
140
141 // TODO doing this at comptime crashed the compiler
142 const localhost = try net.Address.parseIp("127.0.0.1", 0);
143
144 var server = net.StreamServer.init(net.StreamServer.Options{});
145 defer server.deinit();
146 try server.listen(localhost);
147
148 var server_frame = async testServer(&server);
149 var client_frame = async testClientToHost(
150 testing.allocator,
151 "localhost",
152 server.listen_address.getPort(),
153 );
154
155 try await server_frame;
156 try await client_frame;
157}
158
159fn testClientToHost(allocator: *mem.Allocator, name: []const u8, port: u16) anyerror!void {
160 if (builtin.os.tag == .wasi) return error.SkipZigTest;
161
162 const connection = try net.tcpConnectToHost(allocator, name, port);
163 defer connection.close();
164
165 var buf: [100]u8 = undefined;
166 const len = try connection.read(&buf);
167 const msg = buf[0..len];
168 testing.expect(mem.eql(u8, msg, "hello from server\n"));
169}
170
133fn testClient(addr: net.Address) anyerror!void {171fn testClient(addr: net.Address) anyerror!void {
134 if (builtin.os.tag == .wasi) return error.SkipZigTest;172 if (builtin.os.tag == .wasi) return error.SkipZigTest;
135173