aboutsummaryrefslogtreecommitdiff
path: root/test/test_unixdgram_receive_zero.lua
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2026-08-30 12:58:43 +0200
committerThijs Schreijer <thijs@thijsschreijer.nl>2026-08-30 13:46:21 +0200
commit75d638ac9cc613f39ec4513d3dc2e43a6f909da1 (patch)
treee883e854605945a839f208d1ab36c2ae9d20cc1d /test/test_unixdgram_receive_zero.lua
parente21720da69f2a505bcd1405cc7e8b52d5649df55 (diff)
downloadluasocket-75d638ac9cc613f39ec4513d3dc2e43a6f909da1.tar.gz
luasocket-75d638ac9cc613f39ec4513d3dc2e43a6f909da1.tar.bz2
luasocket-75d638ac9cc613f39ec4513d3dc2e43a6f909da1.zip
fix(udp): receive(0) tests and bad call to getnameinfo
Diffstat (limited to 'test/test_unixdgram_receive_zero.lua')
-rw-r--r--test/test_unixdgram_receive_zero.lua200
1 files changed, 200 insertions, 0 deletions
diff --git a/test/test_unixdgram_receive_zero.lua b/test/test_unixdgram_receive_zero.lua
new file mode 100644
index 0000000..c91c3cd
--- /dev/null
+++ b/test/test_unixdgram_receive_zero.lua
@@ -0,0 +1,200 @@
1#!/usr/bin/env lua
2
3-- Same reasoning as test_udp_receive_zero.lua: "receive 0 bytes" on a
4-- datagram socket is really two different requests sharing one call
5-- shape -- (a) a side-effect-free readiness probe, and (b) "my protocol's
6-- messages are always empty-payload signals, receive one normally, I
7-- just don't need a payload back" (a zero-length datagram is a
8-- completely legitimate thing to put on the wire).
9--
10-- receive() has no sender-address field to disambiguate (a) from (b), so
11-- it stays the deterministic no-op it already was via the shared
12-- socket_recv() guard (unaffected here, same as TCP). receivefrom() does
13-- have that field: unlike inet UDP, unixdgram.c already handled an
14-- unpopulated sender path gracefully (it pre-zeroes sun_path and just
15-- returns whatever's there -- see the "may be empty when client sent
16-- without bind" comment in unixdgram.c), so receivefrom(0) here was
17-- never at risk of the getnameinfo crash inet UDP had. It's left making
18-- the real recvfrom() call: nothing pending is platform-dependent
19-- (blocks on Linux/Windows, returns immediately on Darwin/BSD -- see the
20-- platform table in test_udp_receive_zero.lua), but a pending datagram is
21-- always correctly consumed and its sender always correctly reported.
22
23local socket = require "socket"
24local unix = require "socket.unix"
25
26local function new_pair(server_path, client_path)
27 os.remove(server_path)
28 os.remove(client_path)
29 local server = assert(unix.dgram())
30 assert(server:bind(server_path))
31 local client = assert(unix.dgram())
32 assert(client:bind(client_path))
33 assert(client:connect(server_path))
34 server:settimeout(1)
35 return server, client
36end
37
38local function cleanup(server, client, server_path, client_path)
39 client:close()
40 server:close()
41 os.remove(server_path)
42 os.remove(client_path)
43end
44
45-- Detects which of the three documented recvfrom(0)-with-nothing-pending
46-- behaviors applies here, so the test below can assert the right one by
47-- name instead of accepting either shape. "unknown" is the honest answer
48-- when uname isn't available to tell Linux and Darwin/BSD apart (e.g. a
49-- locked-down sandbox); the test falls back to accepting either
50-- documented shape in that case, rather than guessing.
51local function detect_platform()
52 if package.config:sub(1, 1) == "\\" then
53 return "windows"
54 end
55 local handle = io.popen("uname -s 2>/dev/null")
56 if not handle then
57 return "unknown"
58 end
59 local name = handle:read("*l")
60 handle:close()
61 if not name then
62 return "unknown"
63 end
64 name = name:lower()
65 if name:find("linux") then
66 return "linux"
67 elseif name:find("darwin") or name:find("bsd") then
68 return "darwin"
69 end
70 return "unknown"
71end
72
73local platform = detect_platform()
74
75-- === receive(0): deterministic no-op, nothing pending ===
76do
77 local spath, cpath = "/tmp/luasocket-test-udgram-1-srv.sock", "/tmp/luasocket-test-udgram-1-clt.sock"
78 local server, client = new_pair(spath, cpath)
79
80 local t0 = socket.gettime()
81 local rdata, rerr = server:receive(0)
82 local elapsed = socket.gettime() - t0
83 assert(rdata == "" and rerr == nil,
84 "receive(0) with nothing pending returned " .. tostring(rdata) .. ", " .. tostring(rerr))
85 assert(elapsed < 0.5,
86 "receive(0) with nothing pending blocked for " .. elapsed .. "s")
87
88 cleanup(server, client, spath, cpath)
89end
90
91-- === receive(0): deterministic no-op, a datagram pending must be left untouched ===
92do
93 local spath, cpath = "/tmp/luasocket-test-udgram-2-srv.sock", "/tmp/luasocket-test-udgram-2-clt.sock"
94 local server, client = new_pair(spath, cpath)
95
96 assert(client:send("world"))
97 socket.sleep(0.1)
98
99 local data, err = server:receive(0)
100 assert(data == "" and err == nil,
101 "receive(0) with data pending returned " .. tostring(data) .. ", " .. tostring(err))
102
103 data, err = assert(server:receive())
104 assert(data == "world",
105 "receive(0) consumed or corrupted the pending datagram: got " .. tostring(data))
106
107 cleanup(server, client, spath, cpath)
108end
109
110-- === receivefrom(0): nothing pending -- the outcome is platform-specific
111-- (see the comment block at the top of test_udp_receive_zero.lua),
112-- asserted explicitly per platform so it's clear which behavior is
113-- expected where ===
114do
115 local spath, cpath = "/tmp/luasocket-test-udgram-3-srv.sock", "/tmp/luasocket-test-udgram-3-clt.sock"
116 local server, client = new_pair(spath, cpath)
117
118 local data, addr = server:receivefrom(0)
119
120 if platform == "linux" or platform == "windows" then
121 -- both block until a real datagram arrives; nothing does, so the
122 -- call must time out, not fabricate a result
123 assert(data == nil and addr == "timeout",
124 "receivefrom(0) with nothing pending on " .. platform ..
125 " should time out, got " .. tostring(data) .. ", " .. tostring(addr))
126
127 elseif platform == "darwin" then
128 -- the kernel's zero-length recvfrom() is always immediately
129 -- satisfiable, so this must return right away, with no sender path
130 assert(data == "" and addr == "",
131 "receivefrom(0) with nothing pending on darwin should return " ..
132 "immediately with no sender path, got " ..
133 tostring(data) .. ", " .. tostring(addr))
134
135 else
136 -- platform could not be determined: accept either documented
137 -- shape rather than assert one and risk a false failure
138 local timed_out_like_linux_or_windows = data == nil and addr == "timeout"
139 local returned_immediately_like_darwin = data == "" and addr == ""
140 assert(timed_out_like_linux_or_windows or returned_immediately_like_darwin,
141 "receivefrom(0) with nothing pending returned an unexpected shape: " ..
142 tostring(data) .. ", " .. tostring(addr))
143 end
144
145 cleanup(server, client, spath, cpath)
146end
147
148-- === receivefrom(0): a datagram is pending -- must be consumed and its
149-- sender correctly reported, on every platform ===
150do
151 local spath, cpath = "/tmp/luasocket-test-udgram-4-srv.sock", "/tmp/luasocket-test-udgram-4-clt.sock"
152 local server, client = new_pair(spath, cpath)
153
154 assert(client:send("hello"))
155 socket.sleep(0.1)
156
157 local data, addr = server:receivefrom(0)
158 assert(data == "" and addr == cpath,
159 "receivefrom(0) with data pending returned " .. tostring(data) .. ", " .. tostring(addr))
160
161 server:settimeout(0.3)
162 data = server:receive()
163 assert(data == nil,
164 "receivefrom(0) did not actually consume the pending datagram: receive() still got " ..
165 tostring(data))
166
167 cleanup(server, client, spath, cpath)
168end
169
170-- === the real-world case this whole file is about: a protocol whose
171-- messages are always empty-payload signals. receivefrom(0) must work as
172-- a normal receive path for it -- reporting the (empty) payload and the
173-- sender -- not just as a probe for some other, larger message. ===
174do
175 local spath, cpath = "/tmp/luasocket-test-udgram-5-srv.sock", "/tmp/luasocket-test-udgram-5-clt.sock"
176 local server, client = new_pair(spath, cpath)
177
178 -- sanity check: a genuine zero-length datagram, read with a
179 -- normal-sized buffer, already works (untouched by any of this)
180 assert(client:send(""))
181 socket.sleep(0.1)
182 local data, addr = server:receivefrom()
183 assert(data == "" and addr == cpath,
184 "a real zero-length datagram, read normally, returned " ..
185 tostring(data) .. ", " .. tostring(addr))
186
187 -- the actual point: a receiver that knows every message on this
188 -- socket is an empty signal can use receivefrom(0) as its normal
189 -- receive call, and still learn who signaled it
190 assert(client:send(""))
191 socket.sleep(0.1)
192 data, addr = server:receivefrom(0)
193 assert(data == "" and addr == cpath,
194 "receivefrom(0) as a normal receive path for a signal datagram returned " ..
195 tostring(data) .. ", " .. tostring(addr))
196
197 cleanup(server, client, spath, cpath)
198end
199
200print("done!")