-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtestsuite.zig
306 lines (257 loc) · 9.85 KB
/
testsuite.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const std = @import("std");
const builtin = @import("builtin");
const network = @import("network");
const expect = std.testing.expect;
test "Get endpoint list" {
try network.init();
defer network.deinit();
const endpoint_list = try network.getEndpointList(std.testing.allocator, "google.com", 80);
defer endpoint_list.deinit();
for (endpoint_list.endpoints) |endpt| {
std.debug.print("{}\n", .{endpt});
}
}
test "Connect to an echo server" {
const test_host = "tcpbin.com";
try network.init();
defer network.deinit();
const sock = try network.connectToHost(std.testing.allocator, test_host, 4242, .tcp);
defer sock.close();
const msg = "Hi from socket!\n";
try sock.writer().writeAll(msg);
var buf: [128]u8 = undefined;
const read_size = try sock.reader().readAll(buf[0..msg.len]);
try std.testing.expectEqualSlices(u8, msg, buf[0..read_size]);
std.debug.print("Echo: {s}", .{buf[0..read_size]});
}
test "Echo server readiness" {
const test_host = "tcpbin.com";
try network.init();
defer network.deinit();
const sock = try network.connectToHost(
std.testing.allocator,
test_host,
4242,
.tcp,
);
defer sock.close();
var write_set = try network.SocketSet.init(std.testing.allocator);
defer write_set.deinit();
var read_set = try network.SocketSet.init(std.testing.allocator);
defer read_set.deinit();
try write_set.add(sock, .{ .write = true, .read = false });
defer write_set.remove(sock);
try read_set.add(sock, .{ .write = false, .read = true });
defer read_set.remove(sock);
const msg = "Hi from socket!\n";
if (try network.waitForSocketEvent(&write_set, 5 * std.time.ns_per_s) != 1) {
return error.InvalidSocketWriteWait;
}
if (!write_set.isReadyWrite(sock)) {
return error.SocketNotReadyForWrite;
}
try sock.writer().writeAll(msg);
if (try network.waitForSocketEvent(&read_set, 5 * std.time.ns_per_s) != 1) {
return error.InvalidSocketReadWait;
}
if (!read_set.isReadyRead(sock)) {
return error.SocketNotReadyForRead;
}
var buf: [128]u8 = undefined;
const read_size = try sock.reader().readAll(buf[0..msg.len]);
try std.testing.expectEqualSlices(u8, msg, buf[0..read_size]);
}
test "UDP timeout" {
try network.init();
defer network.deinit();
var sock = try network.Socket.create(.ipv4, .udp);
defer sock.close();
try sock.connect(.{
.address = .{ .ipv4 = network.Address.IPv4.init(1, 1, 1, 1) },
.port = 53,
});
try sock.setReadTimeout(3 * std.time.us_per_s);
try std.testing.expectError(error.WouldBlock, sock.reader().readByte());
}
test "IPv4 parse" {
const make = network.Address.IPv4.init;
const parse = network.Address.IPv4.parse;
try std.testing.expectEqual(make(0, 0, 0, 0), try parse("0"));
try std.testing.expectEqual(make(0, 0, 0, 0), try parse("0.0"));
try std.testing.expectEqual(make(0, 0, 0, 0), try parse("0.0.0"));
try std.testing.expectEqual(make(0, 0, 0, 0), try parse("0.0.0.0"));
try std.testing.expectEqual(make(0, 0, 0, 10), try parse("10"));
try std.testing.expectEqual(make(20, 0, 0, 10), try parse("20.10"));
try std.testing.expectEqual(make(30, 20, 0, 10), try parse("30.20.10"));
try std.testing.expectEqual(make(40, 30, 20, 10), try parse("40.30.20.10"));
try std.testing.expectEqual(make(127, 0, 0, 1), try parse("2130706433"));
try std.testing.expectEqual(make(127, 10, 20, 30), try parse("127.660510"));
try std.testing.expectEqual(make(127, 33, 10, 20), try parse("127.33.2580"));
try std.testing.expectEqual(make(255, 255, 255, 255), try parse("255.255.255.255"));
}
test "IPv6 parse" {
const make = network.Address.IPv6.init;
const parse = network.Address.IPv6.parse;
var expected = make(.{0} ** 16, 0);
try std.testing.expectEqual(expected, try parse("::"));
try std.testing.expectEqual(expected, try parse("0:0:0:0:0:0:0:0"));
expected.value[15] = 1;
try std.testing.expectEqual(expected, try parse("::1"));
expected.value = .{0} ** 16;
expected.value[0] = 1;
try std.testing.expectEqual(expected, try parse("100::"));
expected.value[0] = 0xff;
expected.value[1] = 0xff;
expected.value[15] = 1;
try std.testing.expectEqual(expected, try parse("ffff::1"));
expected.value[0] = 0x20;
expected.value[1] = 0x01;
expected.value[2] = 0x0d;
expected.value[3] = 0xb8;
expected.value[4] = 0x0a;
expected.value[5] = 0x0b;
expected.value[6] = 0x11;
expected.value[7] = 0xff;
expected.value[15] = 0x01;
try std.testing.expectEqual(
expected,
try parse("2001:0db8:0a0b:11ff:0:0:0:1"),
);
try std.testing.expectEqual(expected, try parse("2001:db8:a0b:11ff::1"));
try std.testing.expectError(error.InvalidFormat, parse(":"));
try std.testing.expectError(error.InvalidFormat, parse(":1"));
try std.testing.expectError(error.InvalidFormat, parse("1"));
try std.testing.expectError(error.InvalidFormat, parse("0:0:0:0"));
try std.testing.expectError(error.InvalidFormat, parse(":::1"));
try std.testing.expectError(error.InvalidFormat, parse("6::2::1"));
// NOTE: The below is a valid IPv6 address with zone ID that is explicitly
// not parsed.
try std.testing.expectError(error.InvalidFormat, parse("::1%eth0"));
}
// https://github.com/MasterQ32/zig-network/issues/66
test "Windows-only, fix UDP WSAECONNRESET error when calling recvfrom after send failure" {
if (builtin.os.tag != .windows) {
// Skip if not Windows
return;
}
try network.init();
defer network.deinit();
// setup sockets
var server_sock = try network.Socket.create(.ipv4, .udp);
defer server_sock.close();
try server_sock.setReadTimeout(25 * std.time.us_per_ms);
try server_sock.bind(.{
.address = network.Address{ .ipv4 = network.Address.IPv4.any },
.port = 1234,
});
var client_sock = try network.Socket.create(.ipv4, .udp);
var client_sock_closed = false;
defer {
if (!client_sock_closed) {
client_sock.close();
client_sock_closed = true;
}
}
try client_sock.connect(.{
.address = .{ .ipv4 = network.Address.IPv4.init(127, 0, 0, 1) },
.port = 1234,
});
try client_sock.setReadTimeout(25 * std.time.us_per_ms);
// setup buffer
const buflen = 32;
var msg: [buflen]u8 = undefined;
// send and read data back and forth
_ = try client_sock.send("connect_info");
const recvFrom = try server_sock.receiveFrom(msg[0..buflen]);
// close the socket to force the WSAECONNRESET error when we send below
client_sock.close();
client_sock_closed = true;
// If we do not disable SIO_UDP_CONNRESET then a failed "send" will be caught when the
// next "recvfrom" function is called.
//
// MDN: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom
// WSAECONNRESET: "On a UDP-datagram socket this error indicates a previous send operation resulted
// in an ICMP Port Unreachable message."
_ = try server_sock.sendTo(recvFrom.sender, "data");
_ = server_sock.receiveFrom(msg[0..buflen]) catch |err| switch (err) {
error.WouldBlock => {
// fallthrough, expect this to timeout
},
else => {
return err;
},
};
}
test "parse + json parse" {
const json_string =
\\{
\\ "ipv4": "127.0.0.1",
\\ "address": "10.0.0.1",
\\ "endpoint": "8.8.4.4:53"
\\}
;
const Wrapper = struct {
ipv4: network.Address.IPv4,
address: network.Address,
endpoint: network.EndPoint,
};
const wrapper = try std.json.parseFromSliceLeaky(
Wrapper,
std.testing.allocator, // our address parser does not leak
json_string,
.{},
);
try std.testing.expectEqual(network.Address.IPv4.init(127, 0, 0, 1), wrapper.ipv4);
try std.testing.expectEqual(network.Address{ .ipv4 = network.Address.IPv4.init(10, 0, 0, 1) }, wrapper.address);
try std.testing.expectEqual(network.EndPoint{
.address = .{ .ipv4 = network.Address.IPv4.init(8, 8, 4, 4) },
.port = 53,
}, wrapper.endpoint);
}
test "Darwin: connection-mode socket was connected already" {
const port: u16 = 12345;
const Server = struct {
const Self = @This();
port: u16 = port,
socket: network.Socket = undefined,
thread: std.Thread = undefined,
fn start(srv: *Self) !void {
try network.init();
errdefer network.deinit();
srv.socket = try network.Socket.create(.ipv4, .udp);
errdefer srv.socket.close();
_ = try srv.socket.enablePortReuse(true);
const bindAddr = network.EndPoint{
.address = network.Address{ .ipv4 = network.Address.IPv4.any },
.port = srv.port,
};
try srv.socket.bind(bindAddr);
srv.thread = std.Thread.spawn(.{}, run, .{srv}) catch unreachable;
return;
}
fn run(srv: *Self) void {
defer {
srv.socket.close();
network.deinit();
}
var buff: [128]u8 = undefined;
_ = srv.socket.receive(buff[0..]) catch return;
return;
}
fn waitFinish(srv: *Self) void {
srv.thread.join();
}
};
var srv: Server = .{};
try srv.start();
defer srv.waitFinish();
const addr: []const u8 = "127.0.0.1";
try network.init();
defer network.deinit();
const sock = try network.connectToHost(std.testing.allocator, addr, port, .udp);
defer sock.close();
var buff: [128]u8 = undefined;
_ = try sock.send(buff[0..]);
std.debug.print("Darwin: connection-mode socket test - finished\n", .{});
return;
}