|
| 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 | + |
0 commit comments