aboutsummaryrefslogtreecommitdiff
path: root/test/test_udp_receive_zero.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_udp_receive_zero.lua')
-rw-r--r--test/test_udp_receive_zero.lua267
1 files changed, 267 insertions, 0 deletions
diff --git a/test/test_udp_receive_zero.lua b/test/test_udp_receive_zero.lua
new file mode 100644
index 0000000..f3ca090
--- /dev/null
+++ b/test/test_udp_receive_zero.lua
@@ -0,0 +1,267 @@
1#!/usr/bin/env lua
2
3-- "receive 0 bytes" on a UDP socket is really two different requests that
4-- happen to share one call shape:
5--
6-- (a) "give me a side-effect-free readiness probe" -- don't touch
7-- anything, just tell me instantly whether I'd have blocked.
8-- (b) "my protocol's messages are always empty-payload signals (UDP
9-- explicitly allows a zero-length datagram on the wire -- see
10-- udp-zero-length-send-recv in this same directory); receive one
11-- like any other, I just don't need a payload back."
12--
13-- These two conflict on at least one real platform. A Darwin/BSD kernel's
14-- recvfrom() returns 0 bytes immediately for a zero-length request
15-- whether or not anything was actually queued -- so byte count alone can
16-- never distinguish (a) from (b) there. Linux and (per MSDN's
17-- recv()/recvfrom() docs -- no exception is carved out for SOCK_DGRAM the
18-- way there is for "byte stream-style sockets" and WSAEINVAL, though this
19-- is not independently confirmed against a live Windows kernel) Windows
20-- both block until something real arrives, so they don't have this
21-- ambiguity at all.
22--
23-- platform | nothing pending | datagram pending
24-- ------------+------------------------------+---------------------------
25-- Linux | blocks until one arrives | consumed & reported
26-- Windows | blocks until one arrives * | consumed & reported *
27-- Darwin/BSD | returns 0 immediately, | consumed & reported
28-- | never blocks |
29--
30-- What breaks the tie: receivefrom() also reports the sender's address,
31-- and that address comes back unpopulated when nothing was queued and
32-- correctly populated when a real datagram was consumed -- on every
33-- platform, Darwin included. So while byte count can't disambiguate (a)
34-- from (b) on Darwin, the address can. receivefrom(0) is therefore left
35-- to make the real recvfrom() call: it correctly serves use case (b)
36-- everywhere, and on Darwin specifically it gives an honest "nil address"
37-- for (a) instead of the alternative this test suite used to ship -- a
38-- crash out of getnameinfo() on a garbage/unpopulated sockaddr.
39--
40-- receive() has no such disambiguator (no address field to check), so
41-- unguarding it would only buy back blocking-until-signal on Linux/
42-- Windows while remaining just as ambiguous as before on Darwin. It's
43-- left as the deterministic no-op it already was (LuaSocket's TCP
44-- receive(0) fix, inherited via the shared socket_recv()) -- and that
45-- happens to be exactly what Python's socket.recv(0) already does
46-- (CPython's sock_recv_guts), for the same reason.
47--
48-- How other cross-platform standard libraries handle this, checked at the
49-- source level rather than the docs:
50--
51-- language | receive()-equivalent | receivefrom()-equivalent
52-- ---------+-------------------------+---------------------------------------
53-- Go | UDPConn.Read: guarded, | UDPConn.ReadFrom: unguarded, and built
54-- | deterministic no-op | the same way LuaSocket's own
55-- | (internal/poll. | socket_recvfrom() is: try the syscall
56-- | FD.Read) | first (internal/poll.FD.ReadFrom calls
57-- | | syscall.Recvfrom directly), only wait
58-- | | after EAGAIN. Almost certainly shares
59-- | | the Darwin quirk above as a result;
60-- | | not confirmed whether Go's sockaddr
61-- | | conversion crashes or silently zeroes
62-- | | on an address the kernel never filled
63-- | | in.
64-- Python | socket.recv(): | socket.recvfrom(): sock_recvfrom_guts
65-- | guarded, determin- | has no len==0 guard either, but does
66-- | istic no-op | NOT hit the Darwin quirk in practice --
67-- | (sock_recv_guts) | it blocks for the full timeout with
68-- | | nothing pending, same as Linux would.
69-- | | CPython's sock_call_ex runs a real
70-- | | select()/poll() readiness gate
71-- | | (internal_select()) before ever
72-- | | calling recvfrom(), for any operation
73-- | | with a timeout. That gate reports
74-- | | "not readable" honestly regardless of
75-- | | the requested length, so the quirk
76-- | | never gets a chance to fire. It's a
77-- | | side effect of Python's general
78-- | | blocking-with-timeout architecture,
79-- | | not anything specific to recvfrom()
80-- | | or to len==0.
81-- Rust | UdpSocket::recv: | UdpSocket::recv_from: unguarded,
82-- | unguarded, platform- | platform-dependent -- straight to
83-- | dependent | libc::recvfrom, no select-gate, no
84-- | | len==0 guard anywhere.
85--
86-- So "platform-dependent" isn't actually the right label for every
87-- unguarded implementation. Go shares LuaSocket's original exposure to
88-- the Darwin quirk because it shares the same try-syscall-first shape;
89-- Python avoids the quirk entirely, but via unrelated select-gating
90-- machinery, not via anything resembling the address-population check
91-- this fix adds. None of the three has that specific fix; LuaSocket adds
92-- it here.
93
94local socket = require "socket"
95
96local host = "127.0.0.1"
97
98local function new_pair(port)
99 local server = assert(socket.udp())
100 assert(server:setsockname(host, port))
101 local client = assert(socket.udp())
102 assert(client:setpeername(host, port))
103 server:settimeout(1)
104 return server, client
105end
106
107-- Detects which of the three documented recvfrom(0)-with-nothing-pending
108-- behaviors applies here, so the test below can assert the right one by
109-- name instead of accepting either shape. "unknown" is the honest answer
110-- when uname isn't available to tell Linux and Darwin/BSD apart (e.g. a
111-- locked-down sandbox); the test falls back to accepting either
112-- documented shape in that case, rather than guessing.
113local function detect_platform()
114 if package.config:sub(1, 1) == "\\" then
115 return "windows"
116 end
117 local handle = io.popen("uname -s 2>/dev/null")
118 if not handle then
119 return "unknown"
120 end
121 local name = handle:read("*l")
122 handle:close()
123 if not name then
124 return "unknown"
125 end
126 name = name:lower()
127 if name:find("linux") then
128 return "linux"
129 elseif name:find("darwin") or name:find("bsd") then
130 return "darwin"
131 end
132 return "unknown"
133end
134
135local platform = detect_platform()
136
137-- === receive(0): deterministic no-op, nothing pending ===
138do
139 local server, client = new_pair("5465")
140
141 local t0 = socket.gettime()
142 local data, err = server:receive(0)
143 local elapsed = socket.gettime() - t0
144 assert(data == "" and err == nil,
145 "receive(0) with nothing pending returned " .. tostring(data) .. ", " .. tostring(err))
146 assert(elapsed < 0.5,
147 "receive(0) with nothing pending blocked for " .. elapsed .. "s")
148
149 client:close()
150 server:close()
151end
152
153-- === receive(0): deterministic no-op, a datagram pending must be left untouched ===
154do
155 local server, client = new_pair("5466")
156
157 assert(client:send("world"))
158 socket.sleep(0.1)
159
160 local data, err = server:receive(0)
161 assert(data == "" and err == nil,
162 "receive(0) with data pending returned " .. tostring(data) .. ", " .. tostring(err))
163
164 data = assert(server:receive())
165 assert(data == "world",
166 "receive(0) consumed or corrupted the pending datagram: got " .. tostring(data))
167
168 client:close()
169 server:close()
170end
171
172-- === receivefrom(0): nothing pending -- the outcome is platform-specific
173-- (see the comment block at the top of this file), asserted explicitly
174-- per platform so it's clear which behavior is expected where ===
175do
176 local server, client = new_pair("5467")
177
178 local data, ip, port = server:receivefrom(0)
179
180 if platform == "linux" or platform == "windows" then
181 -- both block until a real datagram arrives; nothing does, so the
182 -- call must time out, not fabricate a result
183 assert(data == nil and ip == "timeout",
184 "receivefrom(0) with nothing pending on " .. platform ..
185 " should time out, got " .. tostring(data) .. ", " .. tostring(ip))
186
187 elseif platform == "darwin" then
188 -- the kernel's zero-length recvfrom() is always immediately
189 -- satisfiable, so this must return right away, with no address
190 assert(data == "" and ip == nil and port == nil,
191 "receivefrom(0) with nothing pending on darwin should return " ..
192 "immediately with no address, got " ..
193 tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port))
194
195 else
196 -- platform could not be determined: accept either documented
197 -- shape rather than assert one and risk a false failure
198 local timed_out_like_linux_or_windows = data == nil and ip == "timeout"
199 local returned_immediately_like_darwin = data == "" and ip == nil and port == nil
200 assert(timed_out_like_linux_or_windows or returned_immediately_like_darwin,
201 "receivefrom(0) with nothing pending returned an unexpected shape: " ..
202 tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port))
203 end
204
205 client:close()
206 server:close()
207end
208
209-- === receivefrom(0): a datagram is pending -- must be consumed and its
210-- sender correctly reported, on every platform ===
211do
212 local server, client = new_pair("5468")
213
214 assert(client:send("hello"))
215 socket.sleep(0.1)
216
217 local data, ip, port = server:receivefrom(0)
218 assert(data == "" and ip == "127.0.0.1" and type(port) == "number",
219 "receivefrom(0) with data pending returned " ..
220 tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port))
221
222 -- the payload is gone (all that a 0-byte request can ever return), but
223 -- unlike a crash or a phantom no-op, the caller correctly learned that
224 -- something arrived and who sent it
225 server:settimeout(0.3)
226 data = server:receive()
227 assert(data == nil,
228 "receivefrom(0) did not actually consume the pending datagram: receive() still got " ..
229 tostring(data))
230
231 client:close()
232 server:close()
233end
234
235-- === the real-world case this whole file is about: a protocol whose
236-- messages are always empty-payload signals. A 0-length UDP datagram is
237-- a completely legitimate thing to put on the wire (see
238-- udp-zero-length-send-recv), and receivefrom(0) must work as a normal
239-- receive path for it -- reporting the (empty) payload and the sender --
240-- not just as a probe for some other, larger message. ===
241do
242 local server, client = new_pair("5469")
243
244 -- sanity check: a genuine zero-length datagram over the wire, read
245 -- with a normal-sized buffer, already works (untouched by any of this)
246 assert(client:send(""))
247 socket.sleep(0.1)
248 local data, ip, port = server:receivefrom()
249 assert(data == "" and ip == "127.0.0.1" and type(port) == "number",
250 "a real zero-length datagram, read normally, returned " ..
251 tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port))
252
253 -- the actual point: a receiver that knows every message on this
254 -- socket is an empty signal can use receivefrom(0) as its normal
255 -- receive call, and still learn who signaled it
256 assert(client:send(""))
257 socket.sleep(0.1)
258 data, ip, port = server:receivefrom(0)
259 assert(data == "" and ip == "127.0.0.1" and type(port) == "number",
260 "receivefrom(0) as a normal receive path for a signal datagram returned " ..
261 tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port))
262
263 client:close()
264 server:close()
265end
266
267print("done!")