Skip to content

Commit 1c92b5e

Browse files
lnetoclaude
andcommitted
tests/runtime: add socket_data to test send/receive with data objects
Verifies all send/receive combinations via UDP loopback: data → data, string → data, data → string. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ff0b56c commit 1c92b5e

5 files changed

Lines changed: 155 additions & 1 deletion

File tree

tests/runtime/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DIR="$(dirname "$(readlink -f "$0")")"
1111
FAILED=0
1212

1313
SEP=""
14-
for t in "$DIR"/refcnt_leak.sh "$DIR"/resume_shared.sh "$DIR"/resume_mailbox.sh "$DIR"/rcu_shared.sh "$DIR"/opt_guards.sh "$DIR"/opt_skb_single.sh; do
14+
for t in "$DIR"/refcnt_leak.sh "$DIR"/resume_shared.sh "$DIR"/resume_mailbox.sh "$DIR"/rcu_shared.sh "$DIR"/opt_guards.sh "$DIR"/opt_skb_single.sh "$DIR"/socket_data.sh "$DIR"/socket_data_reject.sh; do
1515
echo "${SEP}# --- $(basename "$t") ---"
1616
SEP=$'\n'
1717
bash "$t" || FAILED=$((FAILED+1))

tests/runtime/socket_data.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--
2+
-- SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
3+
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
4+
--
5+
-- Kernel-side script for the socket_data regression test (see socket_data.sh).
6+
7+
local socket = require("socket")
8+
local net = require("net")
9+
local data = require("data")
10+
11+
local af = socket.af
12+
local sock = socket.sock
13+
local ip = socket.ipproto
14+
local PORT = 19876
15+
local MSG = "hello socket data!"
16+
local SIZE = #MSG
17+
18+
local s = socket.new(af.INET, sock.DGRAM, ip.UDP)
19+
s:bind(net.aton("127.0.0.1"), PORT)
20+
21+
local sbuf = data.new(SIZE, "single")
22+
sbuf:setstring(0, MSG)
23+
local rbuf = data.new(SIZE, "single")
24+
25+
-- data → data
26+
s:send(sbuf, net.aton("127.0.0.1"), PORT)
27+
local n = s:receive(rbuf)
28+
assert(n == SIZE, "data→data: expected " .. SIZE .. " bytes, got " .. tostring(n))
29+
assert(rbuf:getstring(0, n) == MSG, "data→data: content mismatch")
30+
31+
-- string → data
32+
s:send(MSG, net.aton("127.0.0.1"), PORT)
33+
local n2 = s:receive(rbuf)
34+
assert(n2 == SIZE, "string→data: expected " .. SIZE .. " bytes, got " .. tostring(n2))
35+
assert(rbuf:getstring(0, n2) == MSG, "string→data: content mismatch")
36+
37+
-- data → string
38+
s:send(sbuf, net.aton("127.0.0.1"), PORT)
39+
local str = s:receive(SIZE)
40+
assert(str == MSG, "data→string: content mismatch")
41+
42+
s:close()
43+

tests/runtime/socket_data.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
#
3+
# SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
4+
# SPDX-License-Identifier: MIT OR GPL-2.0-only
5+
#
6+
# Tests socket send/receive with data objects via a UDP loopback socket.
7+
# Verifies valid combinations: data→data, string→data, data→string.
8+
#
9+
# Usage: sudo bash tests/runtime/socket_data.sh
10+
11+
SCRIPT="tests/runtime/socket_data"
12+
MODULE="luasocket"
13+
14+
source "$(dirname "$(readlink -f "$0")")/../lib.sh"
15+
16+
cleanup() { lunatik stop "$SCRIPT" 2>/dev/null; }
17+
trap cleanup EXIT
18+
cleanup
19+
20+
ktap_header
21+
ktap_plan 1
22+
23+
cat /sys/module/$MODULE/refcnt > /dev/null 2>&1 || {
24+
echo "# SKIP: $MODULE not loaded"
25+
ktap_totals
26+
exit 0
27+
}
28+
29+
mark_dmesg
30+
31+
run_script "$SCRIPT"
32+
33+
check_dmesg || { ktap_totals; exit 1; }
34+
ktap_pass "socket send/receive with data objects: data→data, string→data, data→string"
35+
36+
ktap_totals
37+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--
2+
-- SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
3+
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
4+
--
5+
-- Kernel-side script for the socket_data_reject regression test (see socket_data_reject.sh).
6+
7+
local socket = require("socket")
8+
local net = require("net")
9+
local data = require("data")
10+
11+
local af = socket.af
12+
local sock = socket.sock
13+
local ip = socket.ipproto
14+
local PORT = 19877
15+
local SIZE = 16
16+
17+
local s = socket.new(af.INET, sock.DGRAM, ip.UDP)
18+
s:bind(net.aton("127.0.0.1"), PORT)
19+
20+
-- rejection: wrong-type userdata (socket instead of data)
21+
local ok, err = pcall(function() s:send(s, net.aton("127.0.0.1"), PORT) end)
22+
assert(not ok and err:find("bad argument"), "send: wrong type not rejected: " .. tostring(err))
23+
24+
local ok2, err2 = pcall(function() s:receive(s) end)
25+
assert(not ok2 and err2:find("bad argument"), "receive: wrong type not rejected: " .. tostring(err2))
26+
27+
-- rejection: non-SINGLE (shared) data
28+
local shared = data.new(SIZE, "shared")
29+
30+
local ok3, err3 = pcall(function() s:send(shared, net.aton("127.0.0.1"), PORT) end)
31+
assert(not ok3 and err3:find("bad argument"), "send: non-SINGLE data not rejected: " .. tostring(err3))
32+
33+
local ok4, err4 = pcall(function() s:receive(shared) end)
34+
assert(not ok4 and err4:find("bad argument"), "receive: non-SINGLE data not rejected: " .. tostring(err4))
35+
36+
s:close()
37+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
#
3+
# SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
4+
# SPDX-License-Identifier: MIT OR GPL-2.0-only
5+
#
6+
# Verifies that socket send/receive reject wrong-type userdata and non-SINGLE
7+
# data objects with a proper Lua error rather than crashing the kernel.
8+
#
9+
# Usage: sudo bash tests/runtime/socket_data_reject.sh
10+
11+
SCRIPT="tests/runtime/socket_data_reject"
12+
MODULE="luasocket"
13+
14+
source "$(dirname "$(readlink -f "$0")")/../lib.sh"
15+
16+
cleanup() { lunatik stop "$SCRIPT" 2>/dev/null; }
17+
trap cleanup EXIT
18+
cleanup
19+
20+
ktap_header
21+
ktap_plan 1
22+
23+
cat /sys/module/$MODULE/refcnt > /dev/null 2>&1 || {
24+
echo "# SKIP: $MODULE not loaded"
25+
ktap_totals
26+
exit 0
27+
}
28+
29+
mark_dmesg
30+
31+
run_script "$SCRIPT"
32+
33+
check_dmesg || { ktap_totals; exit 1; }
34+
ktap_pass "socket send/receive correctly reject wrong-type and non-SINGLE data"
35+
36+
ktap_totals
37+

0 commit comments

Comments
 (0)