diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 730e80e..98d4ff6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,26 +1,52 @@ # Unified test pipeline: -# - userspace: fast LuaJIT run of the ipparse suite (no VM) +# - userspace: fast Lua suite (LuaJIT, 5.1, 5.4, 5.5) of the ipparse tests +# (no VM) # - kernel: full suite (userspace + lunatik kernel modules) inside a # virtme-ng microVM on the pinned LTS kernel (tools/vmtest/) +# - kernel-lunatik-head: same as kernel, but against a Lunatik ref supplied +# via workflow_dispatch (default: upstream master); non-blocking. +# +# Lunatik upgrade procedure: +# 1. run kernel-lunatik-head with lunatik_ref=; +# 2. once green, update LUNATIK_REF and LUNATIK_COMMIT in +# tools/vmtest/versions.env to that tag and its commit. name: test on: push: branches: [master] pull_request: + workflow_dispatch: + inputs: + lunatik_ref: + description: Lunatik ref to test in kernel-lunatik-head + required: false + default: master + type: string jobs: userspace: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + lua: [luajit, lua5.1, lua5.4, lua5.5] steps: - uses: actions/checkout@v6 - - name: Install LuaJIT + MoonScript - run: | - sudo apt-get update - sudo apt-get install -y luajit luarocks - sudo luarocks install moonscript + - uses: cachix/install-nix-action@v31 + with: + nix_path: nixpkgs=channel:nixos-unstable + - name: Compile MoonScript sources + run: nix-shell -p luajitPackages.moonscript --run "make -C ipparse all" - name: Run test suite - run: make -C ipparse test + run: | + case "${{ matrix.lua }}" in + luajit) pkg=luajit; bin=luajit ;; + lua5.1) pkg=lua5_1; bin=lua ;; + lua5.4) pkg=lua5_4; bin=lua ;; + lua5.5) pkg=lua5_5; bin=lua ;; + esac + nix-shell -p "$pkg" --run "make -C ipparse test-nobuild LUA=$bin" kernel: runs-on: ubuntu-latest @@ -44,11 +70,17 @@ jobs: sudo udevadm control --reload-rules sudo udevadm trigger --name-match=kvm || true + - name: Read pinned kernel version + id: pinned + run: | + . tools/vmtest/versions.env + echo "kernel=$KERNEL_VERSION" >> "$GITHUB_OUTPUT" + - name: Restore kernel build cache uses: actions/cache@v5 with: path: tools/vmtest/build/linux-* - key: vmtest-kernel-${{ hashFiles('tools/vmtest/versions.env', 'tools/vmtest/kernel.fragment') }} + key: vmtest-kernel-${{ steps.pinned.outputs.kernel }}-${{ hashFiles('tools/vmtest/kernel.fragment') }} - name: Restore ccache uses: actions/cache@v5 @@ -58,16 +90,78 @@ jobs: restore-keys: vmtest-ccache- - name: Run full suite in VM + env: + VMTEST_RW_DIR: ${{ runner.temp }}/vmtest-rw run: make vm-test - - name: Collect dmesg on failure + - name: Upload logs on failure if: failure() - run: dmesg > vm-dmesg.log 2>/dev/null || true + uses: actions/upload-artifact@v7 + with: + name: vm-logs + path: | + ${{ runner.temp }}/vmtest-rw/console.log + ${{ runner.temp }}/vmtest-rw/out + if-no-files-found: ignore + + kernel-lunatik-head: + runs-on: ubuntu-latest + continue-on-error: true + env: + LUNATIK_REF: ${{ github.event.inputs.lunatik_ref || 'master' }} + LUNATIK_COMMIT: '' + steps: + - uses: actions/checkout@v6 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y \ + build-essential flex bison bc libelf-dev libssl-dev dwarves \ + ccache cpio zstd qemu-system-x86 busybox-static \ + python3-venv rustc cargo \ + luajit luarocks lua5.4 git curl kmod \ + libbpf-dev pkg-config + sudo luarocks install moonscript + + - name: Enable KVM on the runner + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666"' \ + | sudo tee /etc/udev/rules.d/99-kvm.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm || true + + - name: Read pinned kernel version + id: pinned + run: | + . tools/vmtest/versions.env + echo "kernel=$KERNEL_VERSION" >> "$GITHUB_OUTPUT" + + - name: Restore kernel build cache + uses: actions/cache@v5 + with: + path: tools/vmtest/build/linux-* + key: vmtest-kernel-${{ steps.pinned.outputs.kernel }}-${{ hashFiles('tools/vmtest/kernel.fragment') }} + + - name: Restore ccache + uses: actions/cache@v5 + with: + path: ~/.cache/ccache + key: vmtest-ccache-${{ hashFiles('tools/vmtest/versions.env') }} + restore-keys: vmtest-ccache- + + - name: Run full suite in VM + env: + LUNATIK_SRC: ${{ runner.temp }}/lunatik-head + VMTEST_RW_DIR: ${{ runner.temp }}/vmtest-rw-head + run: make vm-test - name: Upload logs on failure if: failure() uses: actions/upload-artifact@v7 with: - name: vm-logs - path: vm-dmesg.log + name: vm-logs-lunatik-head + path: | + ${{ runner.temp }}/vmtest-rw-head/console.log + ${{ runner.temp }}/vmtest-rw-head/out if-no-files-found: ignore diff --git a/ipparse/Makefile b/ipparse/Makefile index 8163d15..bbe24f2 100644 --- a/ipparse/Makefile +++ b/ipparse/Makefile @@ -3,6 +3,7 @@ NAME = ipparse LUA_MODULE_DIR = /lib/modules/lua +LUA ?= luajit all: @@ -24,10 +25,10 @@ uninstall: test: all test-nobuild test-nobuild: - cd .. && LUA_PATH="./?.lua;./?/init.lua" luajit ipparse/tests/run_all.lua + cd .. && LUA_PATH="./?.lua;./?/init.lua" $(LUA) ipparse/tests/run_all.lua example: all - cd .. && LUA_PATH="./?.lua;./?/init.lua" luajit ipparse/examples/parse_real_quic.lua + cd .. && LUA_PATH="./?.lua;./?/init.lua" $(LUA) ipparse/examples/parse_real_quic.lua test-lunatik: all @$(MAKE) install @@ -47,7 +48,7 @@ test-lunatik-run: @start=$$(dmesg | wc -l); \ echo "Running kernel Lunatik crypto tests..."; \ status=0; \ - for test in ipparse/tests/lib/crypto/test_lunatik_kernel_quic_session_e2e ipparse/tests/lib/crypto/test_lunatik_kernel ipparse/tests/lib/crypto/test_lunatik_kernel_quic_simple ipparse/tests/lib/crypto/test_lunatik_kernel_quic_comprehensive ipparse/tests/lib/crypto/test_lunatik_kernel_integration ipparse/tests/lib/crypto/test_lunatik_kernel_crypto_pipeline ipparse/tests/lib/crypto/test_lunatik_kernel_quic_google_capture; do \ + for test in ipparse/tests/lib/crypto/test_lunatik_kernel_quic_session_e2e ipparse/tests/lib/crypto/test_lunatik_kernel ipparse/tests/lib/crypto/test_lunatik_kernel_quic_simple ipparse/tests/lib/crypto/test_lunatik_kernel_quic_comprehensive ipparse/tests/lib/crypto/test_lunatik_kernel_integration ipparse/tests/lib/crypto/test_lunatik_kernel_crypto_pipeline ipparse/tests/lib/crypto/test_lunatik_kernel_quic_google_capture ipparse/tests/test_lunatik_kernel_portable; do \ echo "Running $$test..."; \ if lunatik run "$$test"; then \ : ; \ diff --git a/ipparse/fun.moon b/ipparse/fun.moon index 9b4af7e..9e5f99f 100644 --- a/ipparse/fun.moon +++ b/ipparse/fun.moon @@ -63,8 +63,10 @@ memoN = => bidirmt_gen = -> _mem = setmetatable {}, __mode: "kv" (val) => - if _k = _mem[val] - return _k + _k = _mem[val] + -- rawget (not @[_k]) validates without re-entering __index, which would + -- recurse on an absent key. + return _k if _k and rawget(@, _k) == val for k, v in pairs @ if v == val _mem[val] = k @@ -269,6 +271,15 @@ _ = nil -- on the hot path (per-packet), and a vararg implementation would allocate a -- table and call `unpack` on every invocation. The trade-off is a hard limit -- of 26 arguments and 26 return values. +-- +-- Implementation note: the error handler and the returned function are defined +-- as flat locals rather than inline in the `xpcall` call. The Lua parser is +-- recursive in C, and Lunatik runs on a 16 KiB kernel stack with a C-call +-- guard derived from the stack actually consumed: inline function expressions +-- nested two deep here pushed `require "ipparse.fun"` over that guard when the +-- module was loaded one `require` level deeper than the top level. Defining +-- them at block level reduces the parser's C recursion; the API and semantics +-- are unchanged. -- @tparam function fn -- @tparam[opt] function op -- @treturn function A new, protected function (max 26 args/returns). @@ -278,15 +289,16 @@ _ = nil -- -- @treturn ...any|nil Results from `fn` or `op` (see main description for conditions). protected = (fn, op) -> leak_debug = _ and _.leak_debug - (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) -> - ok, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = xpcall ( - -> fn a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z - ), (err) -> - print err - print debug.traceback! if leak_debug - op! if op + onerror = (err) -> + print err + print debug.traceback! if leak_debug + op! if op + run = (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) -> + call = -> fn a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z + ok, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = xpcall call, onerror if ok a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z + run --- The main export table for the `fun` module. diff --git a/ipparse/l4/quic/frames.moon b/ipparse/l4/quic/frames.moon index b66707b..8e5f175 100644 --- a/ipparse/l4/quic/frames.moon +++ b/ipparse/l4/quic/frames.moon @@ -32,6 +32,11 @@ pack: sp, unpack: su = require "ipparse.lib.pack_compat" :bidirectional = require"ipparse.fun" {:band, :bor, :bnot, :lshift, :rshift} = require"ipparse.lib.bit_compat" +-- True when values are native 64-bit integers (Lua 5.3+/Lunatik kernel), where +-- bitwise shifts cover the full 64-bit range and floats/math.floor may be absent. +-- False on LuaJIT, where numbers are doubles and bit ops are 32-bit. +native_int = math.floor == nil or math.type ~= nil + --- QUIC Frame Types -- Mapping of frame type codes to their names frame_types = bidirectional { @@ -126,19 +131,33 @@ encode_varint = (value) -> band(value, 0xFF) ) else - high = math.floor(value / 4294967296) - low = value % 4294967296 - -- Same issue: encode manually with top 2 bits = 11 - string.char( - bor(0xC0, band(math.floor(high / 16777216), 0x3F)) - band(math.floor(high / 65536), 0xFF) - band(math.floor(high / 256), 0xFF) - band(high, 0xFF) - band(math.floor(low / 16777216), 0xFF) - band(math.floor(low / 65536), 0xFF) - band(math.floor(low / 256), 0xFF) - band(low, 0xFF) - ) + if native_int + -- Native 64-bit integers: drive the byte split with shifts/masks only + -- (no floats, no math.floor, no division). + string.char( + bor(0xC0, band(rshift(value, 56), 0x3F)) + band(rshift(value, 48), 0xFF) + band(rshift(value, 40), 0xFF) + band(rshift(value, 32), 0xFF) + band(rshift(value, 24), 0xFF) + band(rshift(value, 16), 0xFF) + band(rshift(value, 8), 0xFF) + band(value, 0xFF) + ) + else + -- LuaJIT: doubles with 32-bit bit ops, so split high/low by division. + high = math.floor(value / 4294967296) + low = value % 4294967296 + string.char( + bor(0xC0, band(math.floor(high / 16777216), 0x3F)) + band(math.floor(high / 65536), 0xFF) + band(math.floor(high / 256), 0xFF) + band(high, 0xFF) + band(math.floor(low / 16777216), 0xFF) + band(math.floor(low / 65536), 0xFF) + band(math.floor(low / 256), 0xFF) + band(low, 0xFF) + ) --- Parses a PADDING frame -- PADDING frames consist only of the frame type (0x00) diff --git a/ipparse/l4/tcp_stream.moon b/ipparse/l4/tcp_stream.moon index 9419843..f55af6b 100644 --- a/ipparse/l4/tcp_stream.moon +++ b/ipparse/l4/tcp_stream.moon @@ -18,11 +18,31 @@ -- true (each segment with payload is immediately complete). When provided, -- feed returns nil until the predicate passes, then auto-clears the session. -- @tparam[opt] table opts Options: `max_buf` (bytes per session, default 1 MiB), --- `max_sessions` (default 1024). +-- `max_sessions` (default 1024), `clock` (zero-arg function returning an +-- integer instant in fixed units) and `clock_hz` (units per second, default +-- 1 when `clock` is given; `clock_hz` without `clock` is an error). -- @treturn table State with :feed, :clear, :reset and :purge methods. +-- @raise Error at creation when no clock is available and none is provided. new = (check_complete=(-> true), opts={}) -> max_buf = opts.max_buf or 0x100000 max_sessions = opts.max_sessions or 1024 + clock = opts.clock + clock_hz = opts.clock_hz + if clock + clock_hz = clock_hz or 1 + else + error "tcp_stream: opts.clock_hz requires opts.clock" if clock_hz + if os and os.time + clock = os.time + clock_hz = 1 + else + ok, linux = pcall require, "linux" + if ok and linux and linux.time + clock = linux.time + clock_hz = 1000000000 + else + error "tcp_stream: no clock available (pass opts.clock/opts.clock_hz)" + clock_hz = 1 if not clock_hz sessions = {} count = 0 @@ -56,7 +76,7 @@ new = (check_complete=(-> true), opts={}) -> first_seg = entry == nil if first_seg drop_oldest! if count >= max_sessions - entry = { segments: {payload}, total: #payload, :init_seq, timestamp: os.time! } + entry = { segments: {payload}, total: #payload, :init_seq, timestamp: clock! } sessions[key] = entry count += 1 else @@ -87,9 +107,10 @@ new = (check_complete=(-> true), opts={}) -> --- Purge sessions older than max_age seconds. -- @tparam[opt=300] number max_age purge: (max_age=300) -> - now = os.time! + now = clock! + threshold = max_age * clock_hz for key, entry in pairs sessions - if now - entry.timestamp > max_age + if now - entry.timestamp > threshold drop key } diff --git a/ipparse/l7/dns.moon b/ipparse/l7/dns.moon index 47d82b6..9bf3d74 100644 --- a/ipparse/l7/dns.moon +++ b/ipparse/l7/dns.moon @@ -72,6 +72,7 @@ :bidirectional, :zero_indexed = require"ipparse.fun" pack: sp, unpack: su, :sub = require "ipparse.lib.pack_compat" :concat = table +:byte = string {:band, :bor, :bnot, :lshift, :rshift} = require"ipparse.lib.bit_compat" need_bytes = (data, off, len) -> @@ -162,13 +163,13 @@ local labels -- @treturn[opt] boolean `true` if the label is compressed, `nil` otherwise. label = (off, l7_off=1, visited=nil, depth=0) => return nil, off, nil, "invalid label offset #{off}" unless need_bytes @, off, 1 - size = su "B", @, off + size = byte @, off _off = off + 1 if size == 0 -- End of label return nil, off+1 if band(size, 0xC0) == 0xC0 -- DNS label compression pointer return nil, off, nil, "truncated DNS compression pointer" unless need_bytes @, off, 2 - pos = su "B", @, off + 1 + pos = byte @, off + 1 ptr = lshift(band(size, 0x3F), 8) + pos target_off = l7_off + ptr return nil, _off + 1, nil, "DNS compression pointer out of bounds (#{ptr})" unless target_off >= l7_off and target_off <= #@ @@ -328,13 +329,32 @@ pack = => @header.ancount = #@answers @header.nscount = #@authorities @header.arcount = #@additionals - questions = concat([pack_question q for q in *@questions]) - answers = concat([pack_rr r for r in *@answers]) - authorities = concat([pack_rr r for r in *@authorities]) - additionals = concat([pack_rr r for r in *@additionals]) - body = questions .. answers .. authorities .. additionals - @header.size = 12 + #body - pack_header(@header) .. body + parts = {} + n = 1 + body_len = 0 + for q in *@questions + s = pack_question q + n += 1 + parts[n] = s + body_len += #s + for r in *@answers + s = pack_rr r + n += 1 + parts[n] = s + body_len += #s + for r in *@authorities + s = pack_rr r + n += 1 + parts[n] = s + body_len += #s + for r in *@additionals + s = pack_rr r + n += 1 + parts[n] = s + body_len += #s + @header.size = 12 + body_len + parts[1] = pack_header @header + concat parts _mt = __tostring: pack diff --git a/ipparse/lib/bit53.moon b/ipparse/lib/bit53.moon index 62b32a6..1a79289 100644 --- a/ipparse/lib/bit53.moon +++ b/ipparse/lib/bit53.moon @@ -5,16 +5,43 @@ --- Bitwise operations compatibility layer (Lua 5.3 style). -- This module provides Lua 5.3-style bitwise operations using LuaJIT's native operators. +-- Like LuaJIT's `bit` library, `band`, `bor` and `bxor` are variadic: `bor(a)` +-- returns `a`, and operations fold left to right over their arguments. -- MoonScript parses `~` ambiguously, so XOR is expressed using boolean identities. -- -- @module lib.bit53 -- @return table Bitwise operations table bit = - band: (a, b) -> a & b - bor: (a, b) -> a | b + band: (...) -> + n = select "#", ... + return ... if n <= 1 + a, b = ... + return a & b if n == 2 + r = a + for i = 2, n + r &= (select i, ...) + r + bor: (...) -> + n = select "#", ... + return ... if n <= 1 + a, b = ... + return a | b if n == 2 + r = a + for i = 2, n + r |= (select i, ...) + r -- MoonScript parses `a ~ b` ambiguously; express XOR with identities. - bxor: (a, b) -> (a | b) & (~(a & b)) + bxor: (...) -> + n = select "#", ... + return ... if n <= 1 + a, b = ... + return (a | b) & (~(a & b)) if n == 2 + r = a + for i = 2, n + v = select i, ... + r = (r | v) & (~(r & v)) + r bnot: (a) -> ~a lshift: (a, n) -> a << n rshift: (a, n) -> a >> n diff --git a/ipparse/lib/bit_compat.moon b/ipparse/lib/bit_compat.moon index 375b34e..ed52829 100644 --- a/ipparse/lib/bit_compat.moon +++ b/ipparse/lib/bit_compat.moon @@ -41,4 +41,7 @@ return normalize(bit) if ok and bit -- Use native operators for Lua 5.3+ (through helper module) ok, bit = pcall require, "ipparse.lib.bit53" return normalize(bit) if ok and bit +-- Pure-Lua arithmetic fallback (Lua 5.1 without LuaJIT's `bit`) +ok, bit = pcall require, "ipparse.lib.bit_pure" +return normalize(bit) if ok and bit error "no bitwise compatibility backend available" diff --git a/ipparse/lib/bit_pure.moon b/ipparse/lib/bit_pure.moon new file mode 100644 index 0000000..0bc61eb --- /dev/null +++ b/ipparse/lib/bit_pure.moon @@ -0,0 +1,94 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +--- Pure-Lua bitwise fallback (32-bit, LuaJIT `bit` semantics). +-- Arithmetic only: no FFI, no `bit`/`bit32`, no Lua 5.3 operators, so it +-- loads under Lua 5.1. Results are signed 32-bit values, exactly like +-- LuaJIT's `bit` library (`bor(0x80000000, 0) == -2147483648`). ipparse's +-- real paths rely on that signedness: fields are masked afterwards, and the +-- QUIC VarInt encoder deliberately avoids the negative int32 produced by +-- `bor(0x80000000, value)`. +-- +-- Like `bit` (and `bit53`), `band`, `bor` and `bxor` are variadic and fold +-- left to right; shift counts are masked to 5 bits. +-- +-- @module lib.bit_pure +-- @return table Bitwise operations table + +TWO32 = 0x100000000 +MASK32 = 0xFFFFFFFF +SIGN32 = 0x80000000 + +pow2 = (n) -> + p = 1 + p *= 2 for _ = 1, n + p + +--- Normalizes a number to an unsigned 32-bit value. +to_u32 = (n) -> + math.floor(n % TWO32) + +--- Reinterprets an unsigned 32-bit value as a signed 32-bit value. +to_s32 = (u) -> + return u - TWO32 if u >= SIGN32 + u + +and2 = (a, b) -> + result, weight = 0, 1 + for _ = 1, 32 + result += weight if a % 2 == 1 and b % 2 == 1 + a = math.floor(a / 2) + b = math.floor(b / 2) + weight *= 2 + result + +or2 = (a, b) -> + result, weight = 0, 1 + for _ = 1, 32 + result += weight if a % 2 == 1 or b % 2 == 1 + a = math.floor(a / 2) + b = math.floor(b / 2) + weight *= 2 + result + +xor2 = (a, b) -> + result, weight = 0, 1 + for _ = 1, 32 + result += weight if (a % 2) != (b % 2) + a = math.floor(a / 2) + b = math.floor(b / 2) + weight *= 2 + result + +fold = (op, ...) -> + n = select("#", ...) + r = to_u32((...)) + for i = 2, n + r = op(r, to_u32((select(i, ...)))) + to_s32(r) + +bit = + band: (...) -> fold(and2, ...) + bor: (...) -> fold(or2, ...) + bxor: (...) -> fold(xor2, ...) + bnot: (a) -> to_s32(MASK32 - to_u32(a)) + lshift: (a, n) -> + n %= 32 + u = to_u32(a) + return to_s32(u) if n == 0 + -- Keep the product below 2^32 so it stays exactly representable. + to_s32((u % pow2(32 - n)) * pow2(n)) + rshift: (a, n) -> + n %= 32 + u = to_u32(a) + return to_s32(u) if n == 0 + to_s32(math.floor(u / pow2(n))) + arshift: (a, n) -> + n %= 32 + s = to_s32(to_u32(a)) + return s if n == 0 + to_s32(math.floor(s / pow2(n))) + +return bit diff --git a/ipparse/lib/crypto/backend/lunatik.moon b/ipparse/lib/crypto/backend/lunatik.moon index d7ad7ce..05a40ab 100644 --- a/ipparse/lib/crypto/backend/lunatik.moon +++ b/ipparse/lib/crypto/backend/lunatik.moon @@ -27,6 +27,32 @@ close_tfm = (tfm) -> return unless tfm and tfm.__close pcall tfm.__close, tfm +-- EBADMSG error names produced by Lunatik's lunatik_pusherrname +-- (lunatik_aux.c, kernel >= 6.7): errname(abs(err)) == "EBADMSG". +-- On older kernels it formats "%pE" of ERR_PTR(-err): with +-- CONFIG_SYMBOLIC_ERRNAME this is errname(-err) == "-EBADMSG" +-- (lib/errname.c), otherwise the signed decimal "-74" (EBADMSG == 74, +-- include/uapi/asm-generic/errno.h), see lib/vsprintf.c err_ptr(). +ebadmsg_names = { EBADMSG: true, ["-EBADMSG"]: true, ["-74"]: true } + +--- Checks whether an error value denotes EBADMSG. +-- Compares the whole string so digits embedded in other errnos (e.g. -174) +-- are not matched. Lua's error() prepends "chunk:line: "; Lunatik's +-- lunatik_throw() calls lua_error() directly, without such a prefix. +-- @tparam string err Error value +-- @treturn boolean true if the error is EBADMSG +is_ebadmsg = (err) -> + s = tostring err + bare = (s\match "^.*:%d+: (.*)$") or s + ebadmsg_names[bare] == true + +--- Normalizes an EBADMSG from either Lunatik convention to the public error. +-- @tparam string err Error value (name, "-NAME" or signed decimal) +-- @treturn string|nil Authentication error message, or nil if not EBADMSG +ebadmsg_error = (err) -> + return unless is_ebadmsg err + "AES-128-GCM authentication failed (tag mismatch)" + cached_gcm_enc_tfm = nil cached_gcm_enc_key = nil cached_gcm_dec_tfm = nil @@ -79,18 +105,24 @@ aes_128_gcm_decrypt = (key, nonce, ciphertext_with_tag, aad = "") -> if #ciphertext_with_tag < 16 return nil, "ciphertext_with_tag too short (need at least 16-byte tag)" c = get_gcm_tfm "decrypt", key - -- Wrap decrypt call to catch EBADMSG as a handled error, not an exception - ok, pt_or_err, err = pcall -> c\decrypt nonce, ciphertext_with_tag, aad + -- Two Lunatik conventions must be handled: + -- * old API (fork fix_crypto_enomem): decrypt raises (lunatik_throw) on + -- bad tag, so the error lands in the pcall status slot; + -- * upstream v5.1+ (lib/luacrypto_aead.c: luacrypto_aead_decrypt calls + -- lunatik_pushfail(L, -EBADMSG)): decrypt returns nil, "EBADMSG" and + -- nothing is raised. Only non-EBADMSG failures still raise. + ok, pt, err = pcall -> c\decrypt nonce, ciphertext_with_tag, aad unless ok - -- Decrypt failed with Lua error - if (tostring pt_or_err)\match "EBADMSG" - return nil, "AES-128-GCM authentication failed (tag mismatch)" - return nil, "aead(gcm(aes)) decrypt failed: #{tostring pt_or_err}" - -- ok=true, pt_or_err is plaintext, err is error code - if pt_or_err == "EBADMSG" - return nil, "AES-128-GCM authentication failed (tag mismatch)" - -- pt_or_err is plaintext (may be empty string, which is valid) - pt_or_err, nil + msg = ebadmsg_error pt + return nil, msg if msg + return nil, "aead(gcm(aes)) decrypt failed: #{tostring pt}" + -- Success returns the plaintext (possibly ""); only inspect the second + -- value when the plaintext is nil (a plaintext equal to "EBADMSG" is kept). + if pt == nil + msg = ebadmsg_error err + return nil, msg if msg + return nil, "aead(gcm(aes)) decrypt failed: #{tostring err}" + pt, nil --- Encrypts single AES-128-ECB block. -- Uses a cached skcipher transform to avoid frequent allocations under load. diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index 55f87bd..d2b2a8f 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -22,19 +22,29 @@ return string if string.unpack tunpack = unpack or table.unpack -ffi = require "ffi" - -ffi.cdef [[ - void *memcpy(void *dest, const void *src, size_t n); - void *memset(void *s, int c, size_t n); -]] +-- FFI est facultatif : LuaJIT le fournit, Lua 5.1 (PUC) non. Les chemins +-- rapides FFI ci-dessous restent inchangés pour LuaJIT ; les équivalents en +-- Lua pur ne servent que lorsque `require "ffi"` échoue. +ok, ffi = pcall require, "ffi" +HAS_FFI = ok +unless HAS_FFI + ffi = nil + +if HAS_FFI + ffi.cdef [[ + void *memcpy(void *dest, const void *src, size_t n); + void *memset(void *s, int c, size_t n); + ]] -- ─── Détection de l'endianness native ─────────────────────────────────────── -NATIVE_LE = do +-- Sans FFI, aucune sonde fiable n'est disponible : on suppose le +-- petit-boutiste (x86/ARM), qui couvre les runtimes sans FFI visés. +NATIVE_LE = true +if HAS_FFI probe = ffi.new "uint16_t[1]", 0x0102 pb = ffi.cast "uint8_t *", probe - pb[0] == 0x02 -- little-endian si l'octet de poids faible est en premier + NATIVE_LE = pb[0] == 0x02 -- little-endian si l'octet de poids faible est en premier -- ─── Utilitaires ──────────────────────────────────────────────────────────── @@ -50,18 +60,40 @@ read_uint = (s, offset, size, le) -> if size == 4 a, b, c, d = string.byte s, offset + 1, offset + 4 return le and (d * 16777216 + c * 65536 + b * 256 + a) or (a * 16777216 + b * 65536 + c * 256 + d) - val = ffi.cast "uint64_t", 0 - if le - for i = size - 1, 0, -1 - val = val * 256 + string.byte(s, offset + i + 1) + if HAS_FFI + val = ffi.cast "uint64_t", 0 + if le + for i = size - 1, 0, -1 + val = val * 256 + string.byte(s, offset + i + 1) + else + for i = 0, size - 1 + val = val * 256 + string.byte(s, offset + i + 1) + val else - for i = 0, size - 1 - val = val * 256 + string.byte(s, offset + i + 1) - val + -- Arithmétique en double : exacte jusqu'à 2^53, comme `tonumber` sur un + -- uint64_t FFI pour les valeurs plus grandes. + val = 0 + if le + for i = size - 1, 0, -1 + val = val * 256 + string.byte(s, offset + i + 1) + else + for i = 0, size - 1 + val = val * 256 + string.byte(s, offset + i + 1) + val + +-- Convertit un nombre en uint64_t sans conversion indéfinie pour un négatif : +-- la conversion double → int64_t est définie dans la plage représentable, puis +-- on réinterprète les mêmes bits en non signé. Certains LuaJIT saturaient la +-- conversion directe double négatif → uint64_t à 0x8000000000000000. +cast_uint = (val) -> + if val < 0 + ffi.cast "uint64_t", ffi.cast("int64_t", val) + else + ffi.cast "uint64_t", val -- Écrit un entier non signé vers un buffer ffi uint8_t* (offset 0-based) write_uint = (buf, offset, val, size, le) -> - v = ffi.cast "uint64_t", val + v = cast_uint val if le for i = 0, size - 1 buf[offset + i] = tonumber(v % 256) @@ -73,7 +105,7 @@ write_uint = (buf, offset, val, size, le) -> -- Conversion unsigned → signed (complément à deux) to_signed = (val, size) -> - uval = tonumber(ffi.cast "uint64_t", val) + uval = if HAS_FFI then tonumber(ffi.cast "uint64_t", val) else val bits = size * 8 limit = 2 ^ (bits - 1) if uval >= limit @@ -81,6 +113,26 @@ to_signed = (val, size) -> else uval +-- Lit un entier signé en complément à deux (Lua pur). Pour un octet de poids +-- fort à 1, la magnitude est calculée par inversion + 1 octet par octet, ce +-- qui garde exacts les petits négatifs même au-delà de 2^53 (ex. -2 = ff..fe). +read_int = (s, offset, size, le) -> + u = read_uint s, offset, size, le + limit = 2 ^ (size * 8 - 1) + return u if u < limit + mag = 0 + carry = 1 + for i = 0, size - 1 + idx = if le then offset + i + 1 else offset + size - i + b = 255 - string.byte(s, idx) + carry + if b >= 256 + b -= 256 + carry = 1 + else + carry = 0 + mag += b * 2 ^ (8 * i) + -mag + -- ─── Parseur de format ─────────────────────────────────────────────────────── -- Renvoie un itérateur sur {option, count} depuis une chaîne de format. @@ -185,144 +237,451 @@ parse_format_to_ops = (fmt) -> _fmt_cache[fmt] = ops ops --- ─── Encodage IEEE 754 double ──────────────────────────────────────────────── +-- ─── Encodage IEEE 754 ────────────────────────────────────────────────────── -double_to_bytes = (n, le) -> - buf = ffi.new "double[1]", n - b = ffi.cast "uint8_t *", buf - bytes = {b[i] for i = 0, 7} - if not le - bytes = {bytes[8], bytes[7], bytes[6], bytes[5], - bytes[4], bytes[3], bytes[2], bytes[1]} - bytes - -bytes_to_double = (s, offset, le) -> - buf = ffi.new "uint8_t[8]" - if le - for i = 0, 7 - buf[i] = string.byte s, offset + i + 1 +-- Arrondi au plus proche, pair en cas d'égalité (round-to-nearest-even). +round_even = (x) -> + f = math.floor x + d = x - f + if d > 0.5 or (d == 0.5 and f % 2 == 1) + f + 1 else - for i = 0, 7 - buf[i] = string.byte s, offset + 7 - i + 1 - d = ffi.cast "double *", buf - d[0] - -float_to_bytes = (n, le) -> - buf = ffi.new "float[1]", n - b = ffi.cast "uint8_t *", buf - bytes = {b[i] for i = 0, 3} - if not le - bytes = {bytes[4], bytes[3], bytes[2], bytes[1]} + f + +-- Encode un flottant binaire (expbits/fracbits/bias : 11/52/1023 pour un +-- double, 8/23/127 pour un float). Produit les octets de poids fort d'abord, +-- puis les inverse si `le`. Gère zéro, -0, infinis, NaN, normaux et dénormaux. +ieee_encode = (n, expbits, fracbits, bias, le) -> + maxexp = 2 ^ expbits - 1 + sign = 0 + exp = 0 + frac = 0 + if n ~= n -- NaN (n'importe quel NaN convient) + exp = maxexp + frac = 2 ^ (fracbits - 1) + elseif n == math.huge or n == -math.huge + sign = 1 if n < 0 + exp = maxexp + frac = 0 + elseif n == 0 + sign = 1 if 1 / n < 0 + exp = 0 + frac = 0 + else + sign = 1 if n < 0 + a = math.abs n + m, e = math.frexp a + E = e - 1 + bias + if E <= 0 + -- dénormal : fraction = a / 2^(1-bias-fracbits) + frac = round_even math.ldexp m, e + fracbits + bias - 1 + if frac >= 2 ^ fracbits + exp = 1 + frac -= 2 ^ fracbits + else + exp = 0 + else + frac = round_even math.ldexp (m * 2 - 1), fracbits + if frac >= 2 ^ fracbits + frac -= 2 ^ fracbits + E += 1 + if E >= maxexp + exp = maxexp + frac = 0 + else + exp = E + + bytes = {} + if fracbits == 52 + bytes[1] = sign * 128 + math.floor exp / 16 + bytes[2] = (exp % 16) * 16 + math.floor frac / 2 ^ 48 + bytes[3] = math.floor(frac / 2 ^ 40) % 256 + bytes[4] = math.floor(frac / 2 ^ 32) % 256 + bytes[5] = math.floor(frac / 2 ^ 24) % 256 + bytes[6] = math.floor(frac / 2 ^ 16) % 256 + bytes[7] = math.floor(frac / 2 ^ 8) % 256 + bytes[8] = frac % 256 + else + bytes[1] = sign * 128 + math.floor exp / 2 + bytes[2] = (exp % 2) * 128 + math.floor frac / 2 ^ 16 + bytes[3] = math.floor(frac / 2 ^ 8) % 256 + bytes[4] = frac % 256 + if le + out = {} + for i = 1, #bytes + out[i] = bytes[#bytes + 1 - i] + bytes = out bytes -bytes_to_float = (s, offset, le) -> - buf = ffi.new "uint8_t[4]" +-- Décode un flottant binaire. `bytes[1]` est l'octet de poids fort. +ieee_decode = (s, offset, expbits, fracbits, bias, le) -> + nbytes = (expbits + fracbits + 1) / 8 + error "data string too short" if offset + nbytes > #s + bytes = {} if le - for i = 0, 3 - buf[i] = string.byte s, offset + i + 1 + for i = 1, nbytes + bytes[i] = string.byte s, offset + nbytes - i + 1 else - for i = 0, 3 - buf[i] = string.byte s, offset + 3 - i + 1 - f = ffi.cast "float *", buf - tonumber f[0] + for i = 1, nbytes + bytes[i] = string.byte s, offset + i + b0 = bytes[1] + sign = b0 >= 128 and -1 or 1 + exp = 0 + frac = 0 + maxexp = 2 ^ expbits - 1 + denorm_shift = 1 - bias - fracbits + if fracbits == 52 + exp = (b0 % 128) * 16 + math.floor bytes[2] / 16 + frac = (bytes[2] % 16) * 2 ^ 48 + bytes[3] * 2 ^ 40 + bytes[4] * 2 ^ 32 + bytes[5] * 2 ^ 24 + bytes[6] * 2 ^ 16 + bytes[7] * 2 ^ 8 + bytes[8] + else + exp = (b0 % 128) * 2 + math.floor bytes[2] / 128 + frac = (bytes[2] % 128) * 2 ^ 16 + bytes[3] * 2 ^ 8 + bytes[4] + if exp == maxexp + if frac == 0 + sign * math.huge + else + 0 / 0 -- NaN + elseif exp == 0 + sign * math.ldexp frac, denorm_shift + else + sign * math.ldexp (1 + frac / 2 ^ fracbits), exp - bias + +-- Déclarés ici pour rester visibles hors des blocs `if HAS_FFI` ci-dessous. +double_to_bytes = nil +bytes_to_double = nil +float_to_bytes = nil +bytes_to_float = nil + +if HAS_FFI + double_to_bytes = (n, le) -> + buf = ffi.new "double[1]", n + b = ffi.cast "uint8_t *", buf + bytes = [b[i] for i = 0, 7] + if not le + bytes = {bytes[8], bytes[7], bytes[6], bytes[5], + bytes[4], bytes[3], bytes[2], bytes[1]} + bytes + + bytes_to_double = (s, offset, le) -> + buf = ffi.new "uint8_t[8]" + if le + for i = 0, 7 + buf[i] = string.byte s, offset + i + 1 + else + for i = 0, 7 + buf[i] = string.byte s, offset + 7 - i + 1 + d = ffi.cast "double *", buf + d[0] + + float_to_bytes = (n, le) -> + buf = ffi.new "float[1]", n + b = ffi.cast "uint8_t *", buf + bytes = [b[i] for i = 0, 3] + if not le + bytes = {bytes[4], bytes[3], bytes[2], bytes[1]} + bytes + + bytes_to_float = (s, offset, le) -> + buf = ffi.new "uint8_t[4]" + if le + for i = 0, 3 + buf[i] = string.byte s, offset + i + 1 + else + for i = 0, 3 + buf[i] = string.byte s, offset + 3 - i + 1 + f = ffi.cast "float *", buf + tonumber f[0] +else + double_to_bytes = (n, le) -> ieee_encode n, 11, 52, 1023, le + bytes_to_double = (s, offset, le) -> ieee_decode s, offset, 11, 52, 1023, le + float_to_bytes = (n, le) -> ieee_encode n, 8, 23, 127, le + bytes_to_float = (s, offset, le) -> ieee_decode s, offset, 8, 23, 127, le -- ─── string.pack ───────────────────────────────────────────────────────────── -pack = (fmt, ...) -> - args = {...} - argi = 1 - parts = {} - ops = _fmt_cache[fmt] or parse_format_to_ops(fmt) - - for j = 1, #ops - {opt, count, le} = ops[j] - switch opt +-- Tampon d'octets partagé au niveau du module. `pack` n'appelle aucun callback +-- utilisateur pendant la construction : il n'est donc pas réentrant, ce qui est +-- acceptable ici. Les writers écrivent dans _buf[n+1 .. n+size] et renvoient le +-- nouvel index (0-based), sans allouer de chaîne. +_buf = {} + +-- Tableau d'arguments réutilisé par `pack` : allouer une table de 8 entrées à +-- chaque appel dominait le coût des petits formats (`>H H`, préfixe d'en-tête). +-- Comme `_buf`, ce tableau n'est pas réentrant, mais `pack` ne cède jamais la +-- main : aucun callback utilisateur n'est appelé pendant la construction. +_args = {} + +-- Convertit _buf[1..n] en chaîne, par morceaux de 4096 max pour rester sous la +-- limite pratique de `unpack`/`string.char`. Fonction de module (et non closure +-- locale à pack) pour que LuaJIT puisse compiler le chemin rapide : créer une +-- closure par appel (bytecode FNEW) est NYI et annule la compilation du trace. +buf_string = (buf, n) -> + if n <= 4096 + string.char tunpack buf, 1, n + else + out = {} + i = 0 + while i < n + len = math.min 4096, n - i + out[#out+1] = string.char tunpack buf, i + 1, i + len + i += len + table.concat out + +-- Ajoute `s` à `parts` (crée la liste au besoin) et renvoie la liste. +append_string = (parts, s) -> + parts = {} unless parts + parts[#parts+1] = s + parts + +-- Construit un writer d'entier non signé de `sz` octets : écrit les octets dans +-- `buf` à partir de l'index `n` et renvoie `n + sz`. +-- Les tailles 1 à 4 sont déroulées, 5-6 utilisent une petite boucle, et au-delà +-- de 6 octets on passe par uint64_t pour conserver la précision actuelle. +make_uint = (sz, le) -> + if sz == 1 + (buf, n, v) -> + buf[n + 1] = v % 256 + n + 1 + elseif sz == 2 + if le + (buf, n, v) -> + u = v % 65536 + buf[n + 1] = u % 256 + buf[n + 2] = math.floor(u / 256) % 256 + n + 2 + else + (buf, n, v) -> + u = v % 65536 + buf[n + 1] = math.floor(u / 256) % 256 + buf[n + 2] = u % 256 + n + 2 + elseif sz == 3 + mod = 16777216 + if le + (buf, n, v) -> + u = v % mod + buf[n + 1] = u % 256 + buf[n + 2] = math.floor(u / 256) % 256 + buf[n + 3] = math.floor(u / 65536) % 256 + n + 3 + else + (buf, n, v) -> + u = v % mod + buf[n + 1] = math.floor(u / 65536) % 256 + buf[n + 2] = math.floor(u / 256) % 256 + buf[n + 3] = u % 256 + n + 3 + elseif sz == 4 + mod = 4294967296 + if le + (buf, n, v) -> + u = v % mod + buf[n + 1] = u % 256 + buf[n + 2] = math.floor(u / 256) % 256 + buf[n + 3] = math.floor(u / 65536) % 256 + buf[n + 4] = math.floor(u / 16777216) % 256 + n + 4 + else + (buf, n, v) -> + u = v % mod + buf[n + 1] = math.floor(u / 16777216) % 256 + buf[n + 2] = math.floor(u / 65536) % 256 + buf[n + 3] = math.floor(u / 256) % 256 + buf[n + 4] = u % 256 + n + 4 + elseif sz <= 6 + mod = 2 ^ (8 * sz) + if le + (buf, n, v) -> + u = v % mod + for k = 1, sz + buf[n + k] = u % 256 + u = math.floor(u / 256) + n + sz + else + (buf, n, v) -> + u = v % mod + for k = sz, 1, -1 + buf[n + k] = u % 256 + u = math.floor(u / 256) + n + sz + elseif not HAS_FFI + -- 7-8 octets : calcul octet par octet pour rester exact sur les petits + -- négatifs (2^64 - 2 n'est pas représentable en double). + if le + (buf, n, v) -> + p = 1 + for k = 1, sz + buf[n + k] = math.floor(v / p) % 256 + p *= 256 + n + sz + else + (buf, n, v) -> + p = 256 ^ (sz - 1) + for k = 1, sz + buf[n + k] = math.floor(v / p) % 256 + p /= 256 + n + sz + else + if le + (buf, n, v) -> + u = cast_uint v + for k = 1, sz + buf[n + k] = tonumber(u % 256) + u = u / 256 + n + sz + else + (buf, n, v) -> + u = cast_uint v + for k = sz, 1, -1 + buf[n + k] = tonumber(u % 256) + u = u / 256 + n + sz + +make_float = (le) -> + (v) -> string.char(tunpack(float_to_bytes(v, le))) + +make_double = (le) -> + (v) -> string.char(tunpack(double_to_bytes(v, le))) + +make_c = (sz) -> + (v) -> + if #v < sz + v .. string.rep "\0", sz - #v + else + v\sub 1, sz - when "b" -- int8 - v = args[argi] - argi += 1 - v = v % 256 - parts[#parts+1] = string.char v +-- Choisit l'encodeur d'une option au moment de la compilation du format. +pack_op = (opt, count, le) -> + switch opt - when "B" -- uint8 - v = args[argi] - argi += 1 - parts[#parts+1] = string.char(v % 256) + when "b", "B" -- int8 / uint8 + { consume: true, write: make_uint(1, le) } - when "h" -- int16 - v = args[argi] - argi += 1 - uv = v % 65536 - if le - parts[#parts+1] = string.char(uv % 256, math.floor(uv/256) % 256) - else - parts[#parts+1] = string.char(math.floor(uv/256) % 256, uv % 256) + when "h", "H" -- int16 / uint16 + { consume: true, write: make_uint(2, le) } - when "H" -- uint16 - v = args[argi] - argi += 1 - uv = v % 65536 - if le - parts[#parts+1] = string.char(uv % 256, math.floor(uv/256) % 256) - else - parts[#parts+1] = string.char(math.floor(uv/256) % 256, uv % 256) + when "i", "I" -- int/uint N octets (défaut 4) + { consume: true, write: make_uint((count or 4), le) } - when "i", "I" -- int/uint N octets (défaut 4) - sz = count or 4 - v = args[argi] - argi += 1 - buf = ffi.new "uint8_t[?]", sz - write_uint buf, 0, v, sz, le - parts[#parts+1] = ffi.string buf, sz + when "l", "L", "j", "J", "T" -- 64 bits + { consume: true, write: make_uint(8, le) } - when "l", "L", "j", "J", "T" -- 64 bits - v = args[argi] - argi += 1 - buf = ffi.new "uint8_t[8]" - write_uint buf, 0, v, 8, le - parts[#parts+1] = ffi.string buf, 8 + when "f" -- float 32 + { consume: true, emit: make_float le } - when "f" -- float 32 - v = args[argi] - argi += 1 - bytes = float_to_bytes v, le - parts[#parts+1] = string.char tunpack bytes + when "d", "n" -- double 64 + { consume: true, emit: make_double le } - when "d", "n" -- double 64 - v = args[argi] - argi += 1 - bytes = double_to_bytes v, le - parts[#parts+1] = string.char tunpack bytes + when "c" -- chaîne de longueur fixe + { consume: true, emit: make_c(count or 1) } - when "c" -- chaîne de longueur fixe - sz = count or 1 - v = args[argi] - argi += 1 - if #v < sz - parts[#parts+1] = v .. string.rep "\0", sz - #v - else - parts[#parts+1] = v\sub 1, sz + when "s" -- chaîne préfixée longueur (size_t = 8 octets par défaut) + { consume: true, write_len: make_uint((count or 8), le), emit: (v) -> v } - when "s" -- chaîne préfixée par une longueur (size_t = 8 octets ici) - sz = count or 8 - v = args[argi] - argi += 1 - buf = ffi.new "uint8_t[?]", sz - write_uint buf, 0, #v, sz, le - parts[#parts+1] = ffi.string(buf, sz) .. v + when "z" -- chaîne null-terminated + { consume: true, emit: (v) -> v .. "\0" } - when "z" -- chaîne null-terminated - v = args[argi] - argi += 1 - parts[#parts+1] = v .. "\0" + when "x" -- padding (un octet nul) + { consume: false, write: (buf, n) -> + buf[n + 1] = 0 + n + 1 } - when "x" -- padding (un octet nul) - parts[#parts+1] = "\0" + else -- "X" (alignement) et options inconnues : no-op + { consume: false, write: (buf, n) -> n } - when "X" -- alignement (pas d'octet émis dans ce contexte simplifié) - nil -- no-op +-- Cache des formats compilés en liste d'encodeurs : fmt → plan. +_pack_cache = {} - table.concat parts +compile_pack = (fmt) -> + plan = {} + consumed = 0 + iter = parse_format fmt + opt, count, le = iter! + while opt != nil + op = pack_op opt, count, le + plan[#plan+1] = op + consumed += 1 if op.consume + opt, count, le = iter! + plan.count = consumed + + -- Chemin rapide « une seule chaîne en dernière position » (cas de `s2` dans + -- pack_rr, `Hs2` de l'EDNS, ou une option `z`/`c`/`f`/`d` finale) : toutes + -- les options précédentes n'écrivent que des octets entiers, la dernière + -- produit une chaîne. On évite alors la table `parts` et `table.concat`. + tail = nil + if #plan > 0 + last = plan[#plan] + if last.consume and last.emit and not last.write + ok = true + for k = 1, #plan - 1 + prev = plan[k] + ok = false unless prev.write and not prev.emit + tail = last if ok + plan.tail = tail + + _pack_cache[fmt] = plan + plan + +-- `a1`..`a8` couvrent la grande majorité des formats sans vararg ni `select` +-- dans la boucle ; au-delà de 8 valeurs consommées, on bascule sur le chemin +-- générique qui collecte le vararg une seule fois. +pack = (fmt, a1, a2, a3, a4, a5, a6, a7, a8, ...) -> + plan = _pack_cache[fmt] or compile_pack(fmt) + args = nil + if plan.count > 8 + args = {a1, a2, a3, a4, a5, a6, a7, a8, ...} + else + args = _args + args[1] = a1 + args[2] = a2 + args[3] = a3 + args[4] = a4 + args[5] = a5 + args[6] = a6 + args[7] = a7 + args[8] = a8 + + n = 0 + parts = nil + + argi = 1 + last = plan.tail + limit = last and (#plan - 1) or #plan + for j = 1, limit + op = plan[j] + if op.write + if op.consume + n = op.write _buf, n, args[argi] + argi += 1 + else + n = op.write _buf, n + else + v = args[argi] + argi += 1 + if op.write_len + n = op.write_len _buf, n, #v + if n > 0 + parts = append_string parts, buf_string(_buf, n) + n = 0 + parts = append_string parts, op.emit v + + -- Assign before returning: `return table.concat parts` would be a tail call, + -- whose return to the caller frame LuaJIT cannot record (NYI: return to + -- lower frame), aborting the trace that exits the loop above. + result = nil + if last + v = args[argi] + if last.write_len + n = last.write_len _buf, n, #v + result = buf_string(_buf, n) .. last.emit v + elseif parts + if n > 0 + parts = append_string parts, buf_string(_buf, n) + result = table.concat parts + else + result = buf_string _buf, n + result -- ─── string.unpack ─────────────────────────────────────────────────────────── @@ -362,9 +721,14 @@ unpack = (fmt, s, pos) -> when "i" -- intN signé sz = count or 4 - uv = read_uint s, pos, sz, le n += 1 - _results[n] = to_signed uv, sz + if not HAS_FFI + _results[n] = read_int s, pos, sz, le + elseif sz == 8 + _results[n] = tonumber(ffi.cast "int64_t", read_uint(s, pos, 8, le)) + else + uv = read_uint s, pos, sz, le + _results[n] = to_signed uv, sz pos += sz when "I" -- uintN non signé @@ -374,9 +738,11 @@ unpack = (fmt, s, pos) -> pos += sz when "l" -- int64 signé - uv = read_uint s, pos, 8, le n += 1 - _results[n] = tonumber ffi.cast "int64_t", uv + if HAS_FFI + _results[n] = tonumber(ffi.cast "int64_t", read_uint(s, pos, 8, le)) + else + _results[n] = read_int s, pos, 8, le pos += 8 when "L", "J", "T" -- uint64 @@ -385,9 +751,11 @@ unpack = (fmt, s, pos) -> pos += 8 when "j" -- lua_Integer (int64 signé) - uv = read_uint s, pos, 8, le n += 1 - _results[n] = tonumber ffi.cast "int64_t", uv + if HAS_FFI + _results[n] = tonumber(ffi.cast "int64_t", read_uint(s, pos, 8, le)) + else + _results[n] = read_int s, pos, 8, le pos += 8 when "f" -- float 32 diff --git a/ipparse/lib/sha.lua b/ipparse/lib/sha.lua index 96494e6..007ccae 100644 --- a/ipparse/lib/sha.lua +++ b/ipparse/lib/sha.lua @@ -31,7 +31,7 @@ local AND, OR, XOR, SHL, SHR, ROL, ROR, NOT AND = bit.band or function(x, y) return x & y end OR = bit.bor or function(x, y) return x | y end -XOR = bit.bxor or function(x, y) return x ~ y end +XOR = bit.bxor or function(x, ...) local r = x; for i = 1, select('#', ...) do r = r ~ select(i, ...) end; return r end NOT = bit.bnot or function(x) return ~x end -- Lua 5.3+ shifts handle negative numbers correctly for signed int32, @@ -79,11 +79,11 @@ local sha256_H = { local sha256_K = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xef983bda, 0xfc930062, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9ca4f7, 0x682e6ff3, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 } @@ -236,7 +236,7 @@ local function hmac(hash_func, key, message) -- Inner hash: H(ipad || message) local inner_hash_chunker = hash_func() -- Get chunker for inner hash inner_hash_chunker(ipad) -- Feed ipad - local inner_hash_partial = inner_hash_chunker(message) -- Feed message (or return chunker) + local inner_hash_partial = inner_hash_chunker -- Chunker after ipad; message is fed once in partial() local function partial(message_part) if message_part then diff --git a/ipparse/tests/l2/test_ethernet.moon b/ipparse/tests/l2/test_ethernet.moon index 4a70698..4eeb559 100644 --- a/ipparse/tests/l2/test_ethernet.moon +++ b/ipparse/tests/l2/test_ethernet.moon @@ -9,27 +9,27 @@ eth = require"ipparse.l2.ethernet" sp = require("ipparse.lib.pack_compat").pack -- Build a 14-byte Ethernet header: dst=aa:bb:cc:dd:ee:ff, src=00:11:22:33:44:55, proto=0x0800 -eth_raw = sp("c6c6>H", "\xaa\xbb\xcc\xdd\xee\xff", "\x00\x11\x22\x33\x44\x55", 0x0800) +eth_raw = sp("c6c6>H", "\170\187\204\221\238\255", "\000\017\034\051\068\085", 0x0800) test "mac2s converts binary to string", -> - result = eth.mac2s "\xaa\xbb\xcc\xdd\xee\xff" + result = eth.mac2s "\170\187\204\221\238\255" assert result == "aa:bb:cc:dd:ee:ff", "expected 'aa:bb:cc:dd:ee:ff', got '#{result}'" test "s2mac converts string to binary", -> result = eth.s2mac "aa:bb:cc:dd:ee:ff" - assert result == "\xaa\xbb\xcc\xdd\xee\xff", "s2mac failed" + assert result == "\170\187\204\221\238\255", "s2mac failed" test "mac2s/s2mac round-trip", -> - original = "\x12\x34\x56\x78\x9a\xbc" + original = "\018\052\086\120\154\188" assert eth.s2mac(eth.mac2s(original)) == original, "mac2s/s2mac round-trip failed" test "parse extracts dst MAC", -> frame, next_off = eth.parse eth_raw, 1 - assert frame.dst == "\xaa\xbb\xcc\xdd\xee\xff", "dst MAC mismatch" + assert frame.dst == "\170\187\204\221\238\255", "dst MAC mismatch" test "parse extracts src MAC", -> frame, next_off = eth.parse eth_raw, 1 - assert frame.src == "\x00\x11\x22\x33\x44\x55", "src MAC mismatch" + assert frame.src == "\000\017\034\051\068\085", "src MAC mismatch" test "parse extracts protocol", -> frame, next_off = eth.parse eth_raw, 1 @@ -54,18 +54,18 @@ test "proto IP6 == 0x86DD", -> test "new + tostring round-trip", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 } raw = tostring frame parsed, _ = eth.parse raw, 1 - assert parsed.dst == "\xaa\xbb\xcc\xdd\xee\xff", "round-trip dst mismatch" - assert parsed.src == "\x00\x11\x22\x33\x44\x55", "round-trip src mismatch" + assert parsed.dst == "\170\187\204\221\238\255", "round-trip dst mismatch" + assert parsed.src == "\000\017\034\051\068\085", "round-trip src mismatch" assert parsed.protocol == 0x0800, "round-trip protocol mismatch" -- Build an 18-byte 802.1Q-tagged frame: same MACs, VLAN 6, inner proto=0x0800 -eth_vlan_raw = sp("c6c6>HHH", "\xaa\xbb\xcc\xdd\xee\xff", "\x00\x11\x22\x33\x44\x55", 0x8100, 6, 0x0800) +eth_vlan_raw = sp("c6c6>HHH", "\170\187\204\221\238\255", "\000\017\034\051\068\085", 0x8100, 6, 0x0800) test "parse detects 802.1Q tag and extracts vlan", -> frame, next_off = eth.parse eth_vlan_raw, 1 @@ -86,8 +86,8 @@ test "parse untagged frame: vlan is nil", -> test "new with vlan: tostring produces 802.1Q frame", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 vlan: 6 } @@ -95,8 +95,8 @@ test "new with vlan: tostring produces 802.1Q frame", -> test "new with vlan=0: tostring produces plain frame (no tag)", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 vlan: 0 } @@ -104,14 +104,14 @@ test "new with vlan=0: tostring produces plain frame (no tag)", -> test "new + tostring round-trip with vlan", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 vlan: 42 } parsed, _ = eth.parse tostring(frame), 1 assert parsed.vlan == 42, "round-trip vlan mismatch: got #{parsed.vlan}" assert parsed.protocol == 0x0800, "round-trip protocol mismatch" - assert parsed.dst == "\xaa\xbb\xcc\xdd\xee\xff", "round-trip dst mismatch" + assert parsed.dst == "\170\187\204\221\238\255", "round-trip dst mismatch" util.summary "l2/ethernet" diff --git a/ipparse/tests/l3/test_checksum.moon b/ipparse/tests/l3/test_checksum.moon index 912e381..3d45f36 100644 --- a/ipparse/tests/l3/test_checksum.moon +++ b/ipparse/tests/l3/test_checksum.moon @@ -10,11 +10,11 @@ pack: sp = require "ipparse.lib.pack_compat" ip4 = require"ipparse.l3.ip4" test "checksum of 0xffff is 0", -> - result = checksum "\xff\xff" + result = checksum "\255\255" assert result == 0, "checksum(0xffff) should be 0, got #{result}" test "checksum of 0x0000 is 0xffff", -> - result = checksum "\x00\x00" + result = checksum "\000\000" assert result == 0xffff, "checksum(0x0000) should be 0xffff, got #{result}" test "checksum of packed header is 0", -> @@ -32,9 +32,9 @@ test "checksum of packed header is 0", -> assert result == 0, "checksum of valid IP header should be 0, got #{result}" test "checksum odd-length pads with zero", -> - -- Odd-length input should be padded; "\xff" = "\xff\x00" padded → checksum = 0xff00 → bnot = 0x00ff - result = checksum "\xff" - assert result == 0x00ff, "checksum(\\xff) should be 0x00ff, got #{result}" + -- Odd-length input should be padded; "\255" = "\255\000" padded → checksum = 0xff00 → bnot = 0x00ff + result = checksum "\255" + assert result == 0x00ff, "checksum(\\255) should be 0x00ff, got #{result}" test "checksum6 matches IPv6 pseudo-header formula", -> src = string.char 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 diff --git a/ipparse/tests/l3/test_ip4.moon b/ipparse/tests/l3/test_ip4.moon index 39ccd9a..87f6821 100644 --- a/ipparse/tests/l3/test_ip4.moon +++ b/ipparse/tests/l3/test_ip4.moon @@ -10,26 +10,26 @@ ip4 = require"ipparse.l3.ip4" has_data, fragmented_ip4 = pcall require, "ipparse.l3.fragmented_ip4" test "ip42s converts 4 bytes to dotted decimal", -> - result = ip4.ip42s "\xc0\xa8\x01\x01" + result = ip4.ip42s "\192\168\001\001" assert result == "192.168.1.1", "expected '192.168.1.1', got '#{result}'" test "s2ip4 converts dotted decimal to 4 bytes", -> result = ip4.s2ip4 "192.168.1.1" - assert result == "\xc0\xa8\x01\x01", "s2ip4 failed" + assert result == "\192\168\001\001", "s2ip4 failed" test "ip42s/s2ip4 round-trip", -> - original = "\x0a\x00\x00\x01" + original = "\010\000\000\001" assert ip4.s2ip4(ip4.ip42s(original)) == original, "ip42s/s2ip4 round-trip failed" test "net42s converts 5 bytes to CIDR notation", -> -- mask=24, then 192.168.1.0 - bin = "\x18\xc0\xa8\x01\x00" + bin = "\024\192\168\001\000" result = ip4.net42s bin assert result == "192.168.1.0/24", "expected '192.168.1.0/24', got '#{result}'" test "s2net4 converts CIDR to 5 bytes", -> result = ip4.s2net4 "192.168.1.0/24" - assert result == "\x18\xc0\xa8\x01\x00", "s2net4 failed" + assert result == "\024\192\168\001\000", "s2net4 failed" test "net42s/s2net4 round-trip", -> original = "10.0.0.0/8" diff --git a/ipparse/tests/l3/test_ip6.moon b/ipparse/tests/l3/test_ip6.moon index 608adf6..de09cc6 100644 --- a/ipparse/tests/l3/test_ip6.moon +++ b/ipparse/tests/l3/test_ip6.moon @@ -14,7 +14,7 @@ test "ip62s compresses zero runs per RFC 5952", -> test "s2ip6 converts address to 16 bytes", -> result = ip6.s2ip6 "0:0:0:0:0:0:0:1" - expected = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" + expected = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001" assert result == expected, "s2ip6(0:0:0:0:0:0:0:1) mismatch" test "s2ip6 full address", -> @@ -22,7 +22,7 @@ test "s2ip6 full address", -> assert #result == 16, "s2ip6 should return 16 bytes, got #{#result}" test "ip62s/s2ip6 round-trip preserves bytes", -> - original = "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" + original = "\032\001\013\184\000\000\000\000\000\000\000\000\000\000\000\001" s = ip6.ip62s original assert s == "2001:db8::1", "expected '2001:db8::1', got '#{s}'" converted = ip6.s2ip6 s diff --git a/ipparse/tests/l4/quic/test_frames.moon b/ipparse/tests/l4/quic/test_frames.moon index c0f7aa9..dd30bb0 100644 --- a/ipparse/tests/l4/quic/test_frames.moon +++ b/ipparse/tests/l4/quic/test_frames.moon @@ -12,17 +12,17 @@ util = require "ipparse.lib.util" -- PADDING frame (0x00) test "frame: PADDING parsed", -> - f, off = parse_frame "\x00", 1 + f, off = parse_frame "\000", 1 assert f.type == 0x00 and f.name == "PADDING", "expected PADDING" -- PING frame (0x01) test "frame: PING parsed", -> - f, off = parse_frame "\x01", 1 + f, off = parse_frame "\001", 1 assert f.type == 0x01 and f.name == "PING", "expected PING" -- ACK frame (0x02) — minimal: largest_acked=0, delay=0, count=0, first=0 test "frame: ACK minimal parsed", -> - data = "\x02\x00\x00\x00\x00" -- type + 4 VarInts of value 0 + data = "\002\000\000\000\000" -- type + 4 VarInts of value 0 f, _ = parse_frame data, 1 assert f.type == 0x02 and f.name == "ACK" assert f.largest_acked == 0 @@ -31,7 +31,7 @@ test "frame: ACK minimal parsed", -> test "frame: CRYPTO frame parsed", -> payload = "hello" -- type(0x06) + offset(0) + length(5) + data - data = "\x06" .. encode_varint(0) .. encode_varint(#payload) .. payload + data = "\006" .. encode_varint(0) .. encode_varint(#payload) .. payload f, _ = parse_frame data, 1 assert f.type == 0x06 and f.name == "CRYPTO" assert f.data == payload, "crypto data mismatch" @@ -41,7 +41,7 @@ test "frame: CRYPTO frame parsed", -> test "frame: STREAM frame parsed", -> payload = "world" -- 0x0E: STREAM with offset(0x04) and length(0x02) bits set - data = "\x0E" .. encode_varint(1) .. encode_varint(0) .. encode_varint(#payload) .. payload + data = "\014" .. encode_varint(1) .. encode_varint(0) .. encode_varint(#payload) .. payload f, _ = parse_frame data, 1 assert f.type == 0x0E and f.name == "STREAM" assert f.id == 1 @@ -49,51 +49,51 @@ test "frame: STREAM frame parsed", -> -- RESET_STREAM (0x04) test "frame: RESET_STREAM parsed", -> - data = "\x04" .. encode_varint(3) .. encode_varint(0) .. encode_varint(100) + data = "\004" .. encode_varint(3) .. encode_varint(0) .. encode_varint(100) f, _ = parse_frame data, 1 assert f.type == 0x04 and f.name == "RESET_STREAM" assert f.stream_id == 3 and f.final_size == 100 -- STOP_SENDING (0x05) test "frame: STOP_SENDING parsed", -> - data = "\x05" .. encode_varint(7) .. encode_varint(42) + data = "\005" .. encode_varint(7) .. encode_varint(42) f, _ = parse_frame data, 1 assert f.type == 0x05 and f.name == "STOP_SENDING" assert f.stream_id == 7 and f.app_error_code == 42 -- MAX_DATA (0x10) test "frame: MAX_DATA parsed", -> - data = "\x10" .. encode_varint(1000) + data = "\016" .. encode_varint(1000) f, _ = parse_frame data, 1 assert f.type == 0x10 and f.name == "MAX_DATA" assert f.maximum_data == 1000 -- MAX_STREAM_DATA (0x11) test "frame: MAX_STREAM_DATA parsed", -> - data = "\x11" .. encode_varint(2) .. encode_varint(2000) + data = "\017" .. encode_varint(2) .. encode_varint(2000) f, _ = parse_frame data, 1 assert f.type == 0x11 and f.name == "MAX_STREAM_DATA" assert f.stream_id == 2 and f.maximum_stream_data == 2000 -- MAX_STREAMS bidi (0x12) test "frame: MAX_STREAMS_BIDI parsed", -> - data = "\x12" .. encode_varint(10) + data = "\018" .. encode_varint(10) f, _ = parse_frame data, 1 assert f.type == 0x12 and f.name == "MAX_STREAMS_BIDI" assert f.maximum_streams == 10 -- DATA_BLOCKED (0x14) test "frame: DATA_BLOCKED parsed", -> - data = "\x14" .. encode_varint(500) + data = "\020" .. encode_varint(500) f, _ = parse_frame data, 1 assert f.type == 0x14 and f.name == "DATA_BLOCKED" assert f.maximum_data == 500 -- NEW_CONNECTION_ID (0x18) test "frame: NEW_CONNECTION_ID parsed", -> - cid = "\xAA\xBB\xCC\xDD\xEE\xFF\x11\x22" - rst = string.rep("\x00", 16) -- 16-byte stateless reset token - data = "\x18" .. encode_varint(1) .. encode_varint(0) .. "\x08" .. cid .. rst + cid = "\170\187\204\221\238\255\017\034" + rst = string.rep("\000", 16) -- 16-byte stateless reset token + data = "\024" .. encode_varint(1) .. encode_varint(0) .. "\008" .. cid .. rst f, _ = parse_frame data, 1 assert f.type == 0x18 and f.name == "NEW_CONNECTION_ID" assert f.sequence_number == 1 @@ -101,51 +101,51 @@ test "frame: NEW_CONNECTION_ID parsed", -> -- RETIRE_CONNECTION_ID (0x19) test "frame: RETIRE_CONNECTION_ID parsed", -> - data = "\x19" .. encode_varint(5) + data = "\025" .. encode_varint(5) f, _ = parse_frame data, 1 assert f.type == 0x19 and f.name == "RETIRE_CONNECTION_ID" assert f.sequence_number == 5 -- PATH_CHALLENGE (0x1a) — 8 bytes of data test "frame: PATH_CHALLENGE parsed", -> - data = "\x1a" .. string.rep("\xAB", 8) + data = "\026" .. string.rep("\171", 8) f, _ = parse_frame data, 1 assert f.type == 0x1a and f.name == "PATH_CHALLENGE" assert #f.data == 8 -- PATH_RESPONSE (0x1b) test "frame: PATH_RESPONSE parsed", -> - data = "\x1b" .. string.rep("\xCD", 8) + data = "\027" .. string.rep("\205", 8) f, _ = parse_frame data, 1 assert f.type == 0x1b and f.name == "PATH_RESPONSE" -- CONNECTION_CLOSE (0x1c) test "frame: CONNECTION_CLOSE parsed", -> reason = "test error" - data = "\x1c" .. encode_varint(0) .. encode_varint(0) .. encode_varint(#reason) .. reason + data = "\028" .. encode_varint(0) .. encode_varint(0) .. encode_varint(#reason) .. reason f, _ = parse_frame data, 1 assert f.type == 0x1c and f.name == "CONNECTION_CLOSE" assert f.reason_phrase == reason -- HANDSHAKE_DONE (0x1e) — no fields test "frame: HANDSHAKE_DONE parsed", -> - f, _ = parse_frame "\x1e", 1 + f, _ = parse_frame "\030", 1 assert f.type == 0x1e and f.name == "HANDSHAKE_DONE" test "frame: CRYPTO truncated payload returns nil", -> - data = "\x06\x00\x05\xaa" -- offset=0, len=5, only 1 byte payload + data = "\006\000\005\170" -- offset=0, len=5, only 1 byte payload f, _, err = parse_frame data, 1 assert f == nil, "expected parse failure" assert err and err\match("CRYPTO"), "expected CRYPTO error, got #{err}" test "frame: PATH_CHALLENGE truncated returns nil", -> - f, _, err = parse_frame "\x1a\xaa", 1 + f, _, err = parse_frame "\026\170", 1 assert f == nil, "expected parse failure" assert err and err\match("PATH_CHALLENGE"), "expected PATH_CHALLENGE error, got #{err}" -- iter_frames: two frames test "frame: iter_frames over two frames", -> - data = "\x01\x01" -- PING, PING + data = "\001\001" -- PING, PING count = 0 for f in iter_frames data count += 1 @@ -153,12 +153,12 @@ test "frame: iter_frames over two frames", -> -- validate_frames test "frame: validate_frames succeeds on valid data", -> - data = "\x01\x01" + data = "\001\001" ok, msg = validate_frames data assert ok == true, "expected valid, got: #{msg}" test "frame: validate_frames fails on malformed data", -> - data = "\x06\x00\x05\xaa" + data = "\006\000\005\170" ok, msg = validate_frames data assert ok == false, "expected invalid data" assert msg and msg\match("CRYPTO"), "expected CRYPTO validation error, got #{msg}" @@ -169,7 +169,7 @@ test "frame: UNKNOWN frame does not get stuck", -> -- Actually the remaining frames we haven't mapped: -- Let's use a reserved/unknown type like 0x20 -- encode_varint(0x20) = 0x20 (< 64, 1-byte) - data = "\x20\x01" -- unknown type, then PING + data = "\032\001" -- unknown type, then PING f1, off1 = parse_frame data, 1 assert f1.name == "UNKNOWN", "expected UNKNOWN, got #{f1.name}" f2, off2 = parse_frame data, off1 diff --git a/ipparse/tests/l4/quic/test_header.moon b/ipparse/tests/l4/quic/test_header.moon index 9428c5b..6debbe2 100644 --- a/ipparse/tests/l4/quic/test_header.moon +++ b/ipparse/tests/l4/quic/test_header.moon @@ -20,7 +20,7 @@ v1 = require "ipparse.l4.quic.v1" -- token_length: 0 (VarInt 1-byte) -- length: 1 (VarInt 1-byte, represents pn(1) + empty payload) -- packet number: 0x00 (1 byte, unprotected for test) -dcid = "\x01\x02\x03\x04\x05\x06\x07\x08" +dcid = "\001\002\003\004\005\006\007\008" build_initial_pkt = -> sp(">B I4 s1 s1 B B B", 0xC0, 1, dcid, "", 0, 1, 0) @@ -69,7 +69,7 @@ test "header: pn_off points past fixed fields", -> -- Short header: HEADER_FORM=0, FIXED_BIT=1 test "header: parse short header", -> - pkt = "\x40" .. dcid -- byte1=0x40 (short), then dst_connection_id + pkt = "\064" .. dcid -- byte1=0x40 (short), then dst_connection_id h, _ = quic.parse pkt, 1, dcid assert h.long_header == nil or h.long_header == false, "expected short header" @@ -97,7 +97,7 @@ test "header: split_datagrams splits coalesced Initial packets", -> assert datagrams[2].data == p2, "second datagram mismatch" test "header: split_datagrams rejects short header payload", -> - short_pkt = "\x40" .. dcid + short_pkt = "\064" .. dcid datagrams, err = quic.split_datagrams short_pkt, 1, dcid assert datagrams == nil, "expected split failure on short header" assert err and err\find("short header"), "expected short-header error, got #{err}" diff --git a/ipparse/tests/l4/quic/test_integration.moon b/ipparse/tests/l4/quic/test_integration.moon index 3cfbc97..ba5e935 100644 --- a/ipparse/tests/l4/quic/test_integration.moon +++ b/ipparse/tests/l4/quic/test_integration.moon @@ -77,16 +77,16 @@ ip_total = 20 + udp_len -- IPv4 header: placeholder checksum ip_no_csum = sp(">BBHHHBBH", 0x45, 0, ip_total, 0x1234, 0x4000, 64, 17, 0) .. - "\xc0\xa8\x01\x01" .. -- src 192.168.1.1 - "\x01\x01\x01\x01" -- dst 1.1.1.1 + "\192\168\001\001" .. -- src 192.168.1.1 + "\001\001\001\001" -- dst 1.1.1.1 ip_csum = checksum ip_no_csum ip_hdr = sp(">BBHHHBBH", 0x45, 0, ip_total, 0x1234, 0x4000, 64, 17, ip_csum) .. - "\xc0\xa8\x01\x01" .. "\x01\x01\x01\x01" + "\192\168\001\001" .. "\001\001\001\001" -- Ethernet header -eth_hdr = "\xaa\xbb\xcc\xdd\xee\xff" .. -- dst mac - "\x11\x22\x33\x44\x55\x66" .. -- src mac - "\x08\x00" -- ethertype IPv4 +eth_hdr = "\170\187\204\221\238\255" .. -- dst mac + "\017\034\051\068\085\102" .. -- src mac + "\008\000" -- ethertype IPv4 -- UDP header udp_hdr = sp ">HHHH", 1234, 443, udp_len, 0 @@ -110,10 +110,10 @@ pn_off_quic = q.pn_off - off3 + 1 -- 1-based offset within the QUIC packet -- ── Layer 2 ────────────────────────────────────────────────────────────────── test "integration: L2 dst MAC = aa:bb:cc:dd:ee:ff", -> - assert e.dst == "\xaa\xbb\xcc\xdd\xee\xff", "dst MAC mismatch" + assert e.dst == "\170\187\204\221\238\255", "dst MAC mismatch" test "integration: L2 src MAC = 11:22:33:44:55:66", -> - assert e.src == "\x11\x22\x33\x44\x55\x66", "src MAC mismatch" + assert e.src == "\017\034\051\068\085\102", "src MAC mismatch" test "integration: L2 EtherType = 0x0800 (IPv4)", -> assert e.protocol == 0x0800, "expected 0x0800, got #{string.format('0x%04x', e.protocol)}" diff --git a/ipparse/tests/l4/quic/test_protection.moon b/ipparse/tests/l4/quic/test_protection.moon index 9723e46..7a8ca9a 100644 --- a/ipparse/tests/l4/quic/test_protection.moon +++ b/ipparse/tests/l4/quic/test_protection.moon @@ -57,13 +57,13 @@ test "protection: recover_packet_number lower half wraps back", -> test "protection: sample_from_packet 16 bytes at correct position", -> -- pkt = 40 zeros, enc_off = 20 -- sample starts at 20+4=24, length 16 → bytes 24..39 - pkt = (string.rep "\x00", 20) .. (string.rep "\xFF", 20) + pkt = (string.rep "\000", 20) .. (string.rep "\255", 20) sample = prot.sample_from_packet pkt, 20 assert #sample == 16, "sample must be 16 bytes" -- bytes 24..39 of the packet: all 0xFF (we have 20 zeros then 20 FFs) -- wait: pkt[1..20]=0x00, pkt[21..40]=0xFF -- sample = pkt[24..39] = all 0xFF - assert sample == string.rep("\xFF", 16), "sample bytes mismatch" + assert sample == string.rep("\255", 16), "sample bytes mismatch" -- Backend-dependent tests (skip if libcrypto not available) ok_backend, backend = pcall require, "ipparse.lib.crypto.backend.ffi_openssl" @@ -71,8 +71,8 @@ ok_backend, backend = pcall require, "ipparse.lib.crypto.backend.ffi_openssl" if ok_backend -- AES-128-GCM round-trip test "protection: AES-128-GCM encrypt/decrypt round-trip", -> - key = string.rep "\x01", 16 - iv = string.rep "\x02", 12 + key = string.rep "\001", 16 + iv = string.rep "\002", 12 plain = "Hello, QUIC!" aad = "header" ct = backend.aes_128_gcm_encrypt key, iv, plain, aad @@ -80,8 +80,8 @@ if ok_backend assert pt == plain, "round-trip failed: #{err}" test "protection: AES-128-GCM decrypt fails on bad tag", -> - key = string.rep "\x01", 16 - iv = string.rep "\x02", 12 + key = string.rep "\001", 16 + iv = string.rep "\002", 12 ct = backend.aes_128_gcm_encrypt key, iv, "plaintext", "" -- corrupt last byte (auth tag) bad_ct = ct\sub(1, #ct - 1) .. string.char((string.byte(ct, #ct) + 1) % 256) diff --git a/ipparse/tests/l4/quic/test_varint.moon b/ipparse/tests/l4/quic/test_varint.moon index 0f3133d..f187663 100644 --- a/ipparse/tests/l4/quic/test_varint.moon +++ b/ipparse/tests/l4/quic/test_varint.moon @@ -12,39 +12,39 @@ util = require "ipparse.lib.util" -- parse_varint: 1-byte (00xxxxxx) test "varint: parse 1-byte value 0", -> - v, off = parse_varint "\x00", 1 + v, off = parse_varint "\000", 1 assert v == 0 and off == 2, "expected 0, got #{v}" test "varint: parse 1-byte value 63", -> - v, off = parse_varint "\x3F", 1 + v, off = parse_varint "\063", 1 assert v == 63 and off == 2, "expected 63, got #{v}" -- parse_varint: 2-byte (01xxxxxx) test "varint: parse 2-byte value 64", -> - v, off = parse_varint "\x40\x40", 1 + v, off = parse_varint "\064\064", 1 assert v == 64 and off == 3, "expected 64, got #{v}" test "varint: parse 2-byte value 16383", -> - v, off = parse_varint "\x7F\xFF", 1 + v, off = parse_varint "\127\255", 1 assert v == 16383 and off == 3, "expected 16383, got #{v}" -- parse_varint: 4-byte (10xxxxxx) test "varint: parse 4-byte value 16384", -> - v, off = parse_varint "\x80\x00\x40\x00", 1 + v, off = parse_varint "\128\000\064\000", 1 assert v == 16384 and off == 5, "expected 16384, got #{v}" -- encode_varint test "varint: encode 0", -> - assert encode_varint(0) == "\x00" + assert encode_varint(0) == "\000" test "varint: encode 63", -> - assert encode_varint(63) == "\x3F" + assert encode_varint(63) == "\063" test "varint: encode 64", -> - assert encode_varint(64) == "\x40\x40" + assert encode_varint(64) == "\064\064" test "varint: encode 16383", -> - assert encode_varint(16383) == "\x7F\xFF" + assert encode_varint(16383) == "\127\255" -- round-trip test "varint: round-trip 500", -> @@ -60,16 +60,28 @@ test "varint: round-trip 70000", -> test "varint: round-trip 8-byte value", -> value = 4886718345 -- 0x123456789 encoded = encode_varint value + assert encoded == "\192\000\000\001\035\069\103\137", "8-byte encoding mismatch" v, _ = parse_varint encoded, 1 assert v == value, "round-trip 8-byte value failed: got #{v}" +-- 2^62-1 is not exactly representable as a double (LuaJIT): only run where +-- native integers exist. Same condition as frames.moon's native_int: in the +-- kernel math.type is absent but native 64-bit integers do exist. +if math.floor == nil or math.type ~= nil + test "varint: round-trip 8-byte max value", -> + value = 4611686018427387903 -- 0x3FFFFFFFFFFFFFFF (2^62-1) + encoded = encode_varint value + assert encoded == "\255\255\255\255\255\255\255\255", "max 8-byte encoding mismatch" + v, _ = parse_varint encoded, 1 + assert v == value, "round-trip max 8-byte value failed: got #{v}" + test "varint: truncated 8-byte returns nil", -> - v, off = parse_varint "\xC0\x00", 1 + v, off = parse_varint "\192\000", 1 assert v == nil and off == 1, "expected nil with unchanged offset" -- offset arithmetic test "varint: parse at non-1 offset", -> - data = "\xFF" .. "\x3F" -- junk byte, then value 63 + data = "\255" .. "\063" -- junk byte, then value 63 v, off = parse_varint data, 2 assert v == 63 and off == 3 diff --git a/ipparse/tests/l4/test_tcp_stream.moon b/ipparse/tests/l4/test_tcp_stream.moon index a0df3a0..caddeaf 100644 --- a/ipparse/tests/l4/test_tcp_stream.moon +++ b/ipparse/tests/l4/test_tcp_stream.moon @@ -49,4 +49,23 @@ test "tcp_stream: reset drops everything", -> buf = s.feed "k", "123456", 0x10, 2 assert buf == "123456", "buffer should only contain post-reset data, got #{buf}" +test "tcp_stream: purge drops sessions older than max_age", -> + now = 0 + s = tcp_stream.new ((buf) -> #buf >= 6), clock: -> now, clock_hz: 1000 + s.feed "old", "abc", 0x10, 1 + now = 5000 -- 5 s later + s.feed "new", "abc", 0x10, 1 + s.purge 3 -- max_age = 3 s => 3000 ticks + -- The recent session survived: appending completes its buffer. + buf = s.feed "new", "def", 0x10, 2 + assert buf == "abcdef", "recent session should survive purge, got #{buf}" + -- The old session was dropped: a new segment starts from scratch. + buf = s.feed "old", "def", 0x10, 2 + assert buf == nil, "old session should have been purged, got #{buf}" + +test "tcp_stream: clock_hz without clock raises", -> + ok, err = pcall tcp_stream.new, nil, clock_hz: 1000 + assert not ok, "expected an error when clock_hz is given without clock" + assert tostring(err)\find("clock_hz"), "error should mention clock_hz, got #{err}" + util.summary "l4/tcp_stream" diff --git a/ipparse/tests/l7/quic/test_sni.moon b/ipparse/tests/l7/quic/test_sni.moon index 22e0ae4..2b9f91e 100644 --- a/ipparse/tests/l7/quic/test_sni.moon +++ b/ipparse/tests/l7/quic/test_sni.moon @@ -26,9 +26,9 @@ make_hs = (hs_type, body) -> -- Build a minimal ClientHello body ready to be wrapped in make_hs. -- extensions_bin: raw concatenated extension bytes (already packed). make_ch_body = (extensions_bin) -> - client_random = string.rep "\x00", 32 - ciphers = "\x13\x01" -- TLS_AES_128_GCM_SHA256 - compressions = "\x00" + client_random = string.rep "\000", 32 + ciphers = "\019\001" -- TLS_AES_128_GCM_SHA256 + compressions = "\000" sp ">H c32 s1 s2 s1 s2", 0x0303, client_random, "", ciphers, compressions, extensions_bin -- Build a server_name extension (ext type = 0x0000). @@ -65,7 +65,7 @@ test "reassemble_crypto: two frames out of order", -> test "reassemble_crypto: non-CRYPTO frames are ignored", -> frames = { - {name: "PADDING", offset: 0, data: "\x00\x00"} + {name: "PADDING", offset: 0, data: "\000\000"} {name: "CRYPTO", offset: 0, data: "data"} {name: "ACK", offset: 0, data: "ack"} } diff --git a/ipparse/tests/l7/test_dns.moon b/ipparse/tests/l7/test_dns.moon index 0a6775c..53c36f7 100644 --- a/ipparse/tests/l7/test_dns.moon +++ b/ipparse/tests/l7/test_dns.moon @@ -98,7 +98,7 @@ test "answer ttl is 3600", -> test "answer rdata is 93.184.216.34", -> msg, _ = dns.parse dns_response, 1, false - assert msg.answers[1].rdata == "\x5d\xb8\xd8\x22", "rdata mismatch" + assert msg.answers[1].rdata == "\093\184\216\034", "rdata mismatch" test "types bidirectional: types[1] == 'A'", -> assert dns.types[1] == "A", "types[1] should be 'A', got '#{dns.types[1]}'" @@ -121,4 +121,69 @@ test "parse fails safely on truncated label", -> msg, _, err = dns.parse dns_truncated_label, 1, false assert msg == nil, "expected parse failure" assert err and err\match("truncated DNS label"), "expected truncated label error, got #{err}" + +-- Response with two A answers (compressed owner names) and an EDNS OPT record. +dns_response_2a_opt = hex2bin( + "abcd81800001000200000001" .. + "076578616d706c6503636f6d00" .. + "00010001" .. + "c00c000100010000012c00045db8d822" .. + "c00c000100010000012c00045db8d823" .. + "0000291000000000000000" +) + +-- Response with non-empty authority and additional sections. +dns_response_sections = hex2bin( + "123481800001000100010001" .. + "076578616d706c6503636f6d00" .. + "00010001" .. + "c00c000100010000003c000401020304" .. + "c00c000200010000003c0011" .. + "036e7331076578616d706c6503636f6d00" .. + "c00c000100010000003c000405060708" +) + +-- Question with several uncompressed labels. +dns_long_name_query = hex2bin( + "000101000001000000000000" .. + "03777777076578616d706c6509737562646f6d61696e0474657374056c6f63616c00" .. + "00010001" +) + +-- Same long question, answer owner name is a pointer to offset 12. +dns_long_name_answer = hex2bin( + "000181800001000100000000" .. + "03777777076578616d706c6509737562646f6d61696e0474657374056c6f63616c00" .. + "00010001" .. + "c00c000100010000003c000401020304" +) + +-- A simple query prefixed with its 2-byte TCP length (29 bytes). +dns_tcp_query = hex2bin("001d" .. "123401000001000000000000076578616d706c6503636f6d0000010001") + +test "byte-exact round-trip: two A answers + EDNS OPT", -> + assert tostring(dns.parse(dns_response_2a_opt, 1)) == dns_response_2a_opt + +test "byte-exact round-trip: simple query", -> + assert tostring(dns.parse(dns_query, 1)) == dns_query + +test "byte-exact round-trip: authority and additional sections", -> + assert tostring(dns.parse(dns_response_sections, 1)) == dns_response_sections + +test "byte-exact round-trip: TCP message with size prefix", -> + assert tostring(dns.parse(dns_tcp_query, 1, true)) == dns_tcp_query + +test "byte-exact round-trip: long uncompressed name", -> + assert tostring(dns.parse(dns_long_name_query, 1)) == dns_long_name_query + +test "long uncompressed name decodes all labels", -> + msg, _ = dns.parse dns_long_name_query, 1, false + assert msg.question.name == "www.example.subdomain.test.local", + "got '#{msg.question.name}'" + +test "compression pointer answer decodes full name", -> + msg, _ = dns.parse dns_long_name_answer, 1, false + assert msg.answers[1].name == "www.example.subdomain.test.local", + "got '#{msg.answers[1].name}'" + util.summary "l7/dns" diff --git a/ipparse/tests/l7/tls/test_handshake.moon b/ipparse/tests/l7/tls/test_handshake.moon index ca0b015..9a8a1b4 100644 --- a/ipparse/tests/l7/tls/test_handshake.moon +++ b/ipparse/tests/l7/tls/test_handshake.moon @@ -22,7 +22,7 @@ test "extension: parse round-trips pack", -> assert tostring(e) == raw, "pack should round-trip" test "extension: truncated header returns nil", -> - e, off = ext.parse "\x00", 1 + e, off = ext.parse "\000", 1 assert e == nil and off == 1, "should return nil, input offset" test "extension: truncated data returns nil", -> @@ -40,11 +40,11 @@ test "server_name: parses single host_name", -> assert not s.incomplete, "should be complete" test "server_name: truncated list returns nil with error", -> - s, off, err = sn.parse "\x00", 1 + s, off, err = sn.parse "\000", 1 assert s == nil and err, "should return nil, off, err" test "server_name: truncated entry flagged incomplete", -> - bad = sp(">H", 20) .. "\x00\x00\x10ab" -- announces 16-byte name, has 2 + bad = sp(">H", 20) .. "\000\000\016ab" -- announces 16-byte name, has 2 s = sn.parse bad, 1 assert s and s.incomplete, "should be flagged incomplete" diff --git a/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon b/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon index 27b8f49..c4691a6 100644 --- a/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon +++ b/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon @@ -23,23 +23,23 @@ unless ok -- AES-128-GCM: NIST vector (zero key, zero nonce, empty plaintext). -- AES-128-GCM(key=0^16, nonce=0^12, pt="", aad="") → tag=58e2fccefa7e3061367f1d57a4e7455a test "ffi_mbedtls: GCM encrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "", "" assert #ct == 16, "ciphertext should be 16-byte tag only" assert bin2hex(ct) == "58e2fccefa7e3061367f1d57a4e7455a", "tag mismatch: #{bin2hex ct}" test "ffi_mbedtls: GCM decrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 tag = hex_to_bin "58e2fccefa7e3061367f1d57a4e7455a" pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == "", "plaintext should be empty, got: #{tostring err}" test "ffi_mbedtls: GCM encrypt/decrypt round-trip", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 plain = "hello, mbedtls!!" -- 16 bytes ct = b.aes_128_gcm_encrypt key, nonce, plain, "aad-data" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "aad-data" @@ -48,9 +48,9 @@ test "ffi_mbedtls: GCM encrypt/decrypt round-trip", -> -- NIST GCM vector: key=0^16, nonce=0^12, pt=0^16, aad="" -- ct=0388dace60b6a392f328c2b971b2fe78, tag=ab6e47d42cec13bdf53a67b21257bddf test "ffi_mbedtls: GCM encrypt 16-byte plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 - pt = string.rep "\x00", 16 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 + pt = string.rep "\000", 16 ct_and_tag = b.aes_128_gcm_encrypt key, nonce, pt, "" assert #ct_and_tag == 32, "expected 16 ct + 16 tag" assert bin2hex(ct_and_tag\sub 1, 16) == "0388dace60b6a392f328c2b971b2fe78", @@ -59,23 +59,23 @@ test "ffi_mbedtls: GCM encrypt 16-byte plaintext (NIST)", -> "tag mismatch: #{bin2hex ct_and_tag\sub 17, 32}" test "ffi_mbedtls: GCM decrypt fails on bad tag", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 - tag = string.rep "\xff", 16 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 + tag = string.rep "\255", 16 pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == nil, "should return nil on bad tag" assert err ~= nil, "should return error message" test "ffi_mbedtls: GCM decrypt fails on tampered AAD", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "test data...!!!!", "correct-aad" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "wrong-aad" assert pt == nil, "should fail with wrong AAD" test "ffi_mbedtls: GCM ciphertext too short returns nil", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, "tooshort", "" assert pt == nil, "should return nil" assert err ~= nil, "should return error" @@ -91,8 +91,8 @@ test "ffi_mbedtls: ECB RFC 9001 §A.2 header-protection mask", -> "first 5 bytes mismatch: #{bin2hex(mask)\sub 1, 10}" test "ffi_mbedtls: ECB known NIST vector (key=0^16, block=0^16)", -> - key = string.rep "\x00", 16 - block = string.rep "\x00", 16 + key = string.rep "\000", 16 + block = string.rep "\000", 16 out = b.aes_128_ecb_block key, block -- AES-128-ECB(0^16, 0^16) = 66e94bd4ef8a2c3b884cfa59ca342b2e assert bin2hex(out) == "66e94bd4ef8a2c3b884cfa59ca342b2e", @@ -110,7 +110,7 @@ test "ffi_mbedtls: construct_nonce pn=1 XORs last byte", -> assert nonce\byte(12) == 0x5d, "last byte 0x5c XOR 0x01 = 0x5d" test "ffi_mbedtls: construct_nonce pn=0x0102 XORs last two bytes", -> - iv = string.rep "\x00", 12 + iv = string.rep "\000", 12 nonce = b.construct_nonce iv, 0x0102 assert nonce\byte(11) == 0x01, "byte 11 should be 0x01" assert nonce\byte(12) == 0x02, "byte 12 should be 0x02" diff --git a/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon b/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon index 8088ab9..2ffd291 100644 --- a/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon +++ b/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon @@ -23,39 +23,39 @@ unless ok -- AES-128-GCM: RFC 8452 / NIST vectors (zero key, zero nonce, empty plaintext). -- AES-128-GCM(key=0^16, nonce=0^12, pt="", aad="") → tag = 58e2fccefa7e3061367f1d57a4e7455a test "ffi_wolfssl: GCM encrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "", "" assert #ct == 16, "ciphertext should be 16-byte tag only" assert bin2hex(ct) == "58e2fccefa7e3061367f1d57a4e7455a", "tag mismatch: #{bin2hex ct}" test "ffi_wolfssl: GCM decrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 tag = hex_to_bin "58e2fccefa7e3061367f1d57a4e7455a" pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == "", "plaintext should be empty, got: #{tostring err}" test "ffi_wolfssl: GCM encrypt/decrypt round-trip", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 plain = "hello, wolfssl!!" -- 16 bytes ct = b.aes_128_gcm_encrypt key, nonce, plain, "aad-data" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "aad-data" assert pt == plain, "round-trip failed: #{tostring err}" test "ffi_wolfssl: GCM decrypt fails on bad tag", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 - tag = string.rep "\xff", 16 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 + tag = string.rep "\255", 16 pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == nil, "should return nil on bad tag" assert err ~= nil, "should return error message" test "ffi_wolfssl: GCM decrypt fails on tampered AAD", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "test data...!!!!", "correct-aad" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "wrong-aad" assert pt == nil, "should fail with wrong AAD" @@ -71,8 +71,8 @@ test "ffi_wolfssl: ECB RFC 9001 §A.2 header-protection mask", -> "first 5 bytes mismatch: #{bin2hex(mask)\sub 1, 10}" test "ffi_wolfssl: ECB known NIST vector (key=0^16, block=0^16)", -> - key = string.rep "\x00", 16 - block = string.rep "\x00", 16 + key = string.rep "\000", 16 + block = string.rep "\000", 16 out = b.aes_128_ecb_block key, block -- AES-128-ECB(0^16, 0^16) = 66e94bd4ef8a2c3b884cfa59ca342b2e assert bin2hex(out) == "66e94bd4ef8a2c3b884cfa59ca342b2e", @@ -90,7 +90,7 @@ test "ffi_wolfssl: construct_nonce pn=1 XORs last byte", -> assert nonce\byte(12) == 0x5d, "last byte 0x5c XOR 0x01 = 0x5d" test "ffi_wolfssl: construct_nonce pn=0x0102 XORs last two bytes", -> - iv = string.rep "\x00", 12 + iv = string.rep "\000", 12 nonce = b.construct_nonce iv, 0x0102 assert nonce\byte(11) == 0x01, "byte 11 should be 0x01" assert nonce\byte(12) == 0x02, "byte 12 should be 0x02" diff --git a/ipparse/tests/lib/crypto/test_lunatik.moon b/ipparse/tests/lib/crypto/test_lunatik.moon index 8c61fc8..c5c5ce9 100644 --- a/ipparse/tests/lib/crypto/test_lunatik.moon +++ b/ipparse/tests/lib/crypto/test_lunatik.moon @@ -11,6 +11,21 @@ util = require "ipparse.lib.util" {:test, :summary} = util {:hex_to_bin} = require "ipparse.lib.hkdf" +-- Snapshot package state before installing the stubs below, so nothing leaks +-- into the modules loaded after this one in the same test run. +preload_before = { + crypto: package.preload.crypto + crypto_ecb: package.preload["crypto.ecb"] +} +loaded_before = { + crypto: package.loaded["crypto"] + crypto_ecb: package.loaded["crypto.ecb"] + backend: package.loaded["ipparse.lib.crypto.backend.lunatik"] +} +loaded_keys_before = {} +for k in pairs package.loaded + loaded_keys_before[k] = true + calls = { aead_new: 0 aead_close: 0 @@ -24,15 +39,42 @@ mk_aead = -> setkey: (k) => key = k setauthsize: (n) => authsize = n encrypt: (nonce, plaintext, aad="") => - assert key == string.rep("\x11", 16), "unexpected test key" + assert key == string.rep("\017", 16), "unexpected test key" assert authsize == 16, "authsize must be 16" assert #nonce == 12, "nonce length should be 12" "ct:" .. plaintext .. "|aad:" .. aad .. "|tag:" .. string.rep("T", 16) decrypt: (nonce, ciphertext_with_tag, aad="") => - if ciphertext_with_tag\sub(1, 8) == "AUTHFAIL" + prefix = ciphertext_with_tag\sub 1, 8 + if prefix == "AUTHFAIL" error "EBADMSG" + if prefix == "AUTHMIN1" + error "-EBADMSG" + if prefix == "AUTHNUM1" + error "-74" + if prefix == "AUTHNUM3" + error "-174" + if prefix == "AUTHNUM4" + error "-740" + if prefix == "NOTAUTH2" + error "-22" if ciphertext_with_tag\sub(1, 6) == "BROKEN" error "EINVAL" + if prefix == "PLAINBAD" + return "EBADMSG" + -- Upstream Lunatik v5.1 convention (luacrypto_aead_decrypt -> + -- lunatik_pushfail): return nil, instead of raising. + if prefix == "NEWFAIL1" + return nil, "EBADMSG" + if prefix == "NEWFAIL2" + return nil, "-EBADMSG" + if prefix == "NEWFAIL3" + return nil, "-74" + if prefix == "NEWFAIL4" + return nil, "unknown" + if prefix == "NEWERR01" + return nil, "EINVAL" + if prefix == "NEWERR02" + return nil, "-174" "pt:" .. ciphertext_with_tag .. "|aad:" .. aad __close: => calls.aead_close += 1 @@ -42,8 +84,9 @@ mk_ecb = -> key = nil { setkey: (k) => key = k - encryptblock: (block) => - assert key == string.rep("\x22", 16), "unexpected ECB key" + encrypt: (iv, block) => + assert key == string.rep("\034", 16), "unexpected ECB key" + assert iv == "", "ECB expects an empty IV" assert #block == 16, "ECB block must be 16 bytes" "Z" .. block\sub(2) __close: => @@ -56,11 +99,8 @@ package.preload.crypto = -> assert name == "gcm(aes)", "unexpected AEAD algorithm: #{name}" calls.aead_new += 1 mk_aead! - } - -package.preload["crypto.ecb"] = -> - { - new: -> + skcipher: (name) -> + assert name == "ecb(aes)", "unexpected skcipher algorithm: #{name}" calls.ecb_new += 1 mk_ecb! } @@ -79,44 +119,146 @@ test "lunatik: construct_nonce pn=1 XORs last byte", -> assert nonce\byte(12) == 0x5d, "last byte 0x5c XOR 0x01 = 0x5d" test "lunatik: AES-128-GCM encrypt uses crypto.aead", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 out = b.aes_128_gcm_encrypt key, nonce, "hello", "aad" assert out == ("ct:hello|aad:aad|tag:" .. string.rep("T", 16)), "unexpected ciphertext" test "lunatik: AES-128-GCM decrypt returns plaintext", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("cipher" .. string.rep("T", 16)), "aad" assert err == nil, "unexpected decrypt error: #{tostring err}" assert pt == ("pt:cipher" .. string.rep("T", 16) .. "|aad:aad"), "unexpected plaintext" test "lunatik: AES-128-GCM decrypt maps EBADMSG to nil,error", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHFAIL" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on auth failure" assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" +test "lunatik: AES-128-GCM decrypt maps -EBADMSG (pre-6.7 symbolic) to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHMIN1" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps -74 (pre-6.7 numeric) to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM1" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,EBADMSG to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL1" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,-EBADMSG to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL2" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,-74 to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL3" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,unknown to decrypt failed", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL4" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,EINVAL to decrypt failed", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWERR01" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt does not treat v5.1 nil,-174 as EBADMSG", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWERR02" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt does not treat -174 as EBADMSG", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM3" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt does not treat -740 as EBADMSG", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM4" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps -22 (numeric EINVAL) to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NOTAUTH2" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt returns plaintext equal to EBADMSG verbatim", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("PLAINBAD" .. string.rep("T", 16)), "" + assert err == nil, "unexpected decrypt error: #{tostring err}" + assert pt == "EBADMSG", "plaintext should be returned verbatim, got: #{tostring pt}" + test "lunatik: AES-128-GCM ciphertext too short returns nil", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, "short", "" assert pt == nil, "pt should be nil" assert err ~= nil, "err should be provided" -test "lunatik: AES-128-GCM decrypt raises on non-auth error", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 - ok, err = pcall b.aes_128_gcm_decrypt, key, nonce, ("BROKEN" .. string.rep("T", 16)), "" - assert not ok, "decrypt should raise on non-auth failure" +test "lunatik: AES-128-GCM decrypt returns nil,error on non-auth error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("BROKEN" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on non-auth failure" assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" -test "lunatik: AES-128-ECB encrypt uses crypto.ecb", -> - key = string.rep "\x22", 16 - block = string.rep "\x33", 16 +test "lunatik: AES-128-ECB encrypt uses crypto.skcipher", -> + key = string.rep "\034", 16 + block = string.rep "\051", 16 out = b.aes_128_ecb_block key, block assert #out == 16, "output must be 16 bytes" assert out\byte(1) == string.byte("Z"), "first byte should be transformed by stub" +-- Restore package state altered by the stubs above. +leaked = {} +for k in pairs package.loaded + leaked[#leaked + 1] = k unless loaded_keys_before[k] +for k in *leaked + package.loaded[k] = nil +package.preload.crypto = preload_before.crypto +package.preload["crypto.ecb"] = preload_before.crypto_ecb +package.loaded["crypto"] = loaded_before.crypto +package.loaded["crypto.ecb"] = loaded_before.crypto_ecb +package.loaded["ipparse.lib.crypto.backend.lunatik"] = loaded_before.backend + summary "lib.crypto.lunatik" diff --git a/ipparse/tests/lib/crypto/test_lunatik_kernel.moon b/ipparse/tests/lib/crypto/test_lunatik_kernel.moon index cf30ee6..4378493 100644 --- a/ipparse/tests/lib/crypto/test_lunatik_kernel.moon +++ b/ipparse/tests/lib/crypto/test_lunatik_kernel.moon @@ -35,24 +35,24 @@ test "lunatik-kernel: construct_nonce pn=2 (RFC 9001 A.3)", -> assert bin2hex(nonce) == "fa044b2f42a3fd3b46fb255e", "nonce mismatch: #{bin2hex nonce}" test "lunatik-kernel: GCM encrypt empty plaintext (NIST)", -> - key = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" - nonce = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + key = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + nonce = "\000\000\000\000\000\000\000\000\000\000\000\000" ct = backend.aes_128_gcm_encrypt key, nonce, "", "" assert #ct == 16, "ciphertext should be 16-byte tag only" assert bin2hex(ct) == "58e2fccefa7e3061367f1d57a4e7455a", "tag mismatch: #{bin2hex ct}" test "lunatik-kernel: GCM decrypt empty plaintext (NIST)", -> - key = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" - nonce = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + key = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + nonce = "\000\000\000\000\000\000\000\000\000\000\000\000" tag = hex_to_bin "58e2fccefa7e3061367f1d57a4e7455a" pt, err = backend.aes_128_gcm_decrypt key, nonce, tag, "" assert err == nil, "unexpected decrypt error: #{tostring err}" assert pt == "", "plaintext should be empty" test "lunatik-kernel: GCM decrypt fails on bad tag", -> - key = "\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" - nonce = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + key = "\066\066\066\066\066\066\066\066\066\066\066\066\066\066\066\066" + nonce = "\000\000\000\000\000\000\000\000\000\000\000\000" ct = backend.aes_128_gcm_encrypt key, nonce, "test data...!!!!", "aad" bad_ct = ct\sub(1, #ct - 1) .. string.char((string.byte(ct, #ct) + 1) % 256) pt, err = backend.aes_128_gcm_decrypt key, nonce, bad_ct, "aad" diff --git a/ipparse/tests/lib/test_bit_compat.moon b/ipparse/tests/lib/test_bit_compat.moon new file mode 100644 index 0000000..e29331f --- /dev/null +++ b/ipparse/tests/lib/test_bit_compat.moon @@ -0,0 +1,116 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_bit_compat.moon +-- Tests for the bitwise compatibility layer, notably variadic band/bor/bxor + +util = require "ipparse.lib.util" +{:test} = util +{ :band, :bor, :bxor } = require "ipparse.lib.bit_compat" +-- Test the pure-Lua fallback directly wherever it can run, so it is exercised +-- even when the host has LuaJIT's `bit` or Lua 5.2+'s `bit32`. +-- It is only the last-resort fallback of `bit_compat` (Lua 5.1 without `bit`) +-- and relies on `math.floor`, which the Lua 5.5 kernel runtime lacks: the +-- `bit_pure` cases below therefore only run where `math.floor` exists. +pure = require "ipparse.lib.bit_pure" + +test_pure = (name, fn) -> + test name, fn if math.floor + +test "bit_compat: bor with 1 argument", -> + assert bor(5) == 5, "bor(5) != 5" + +test "bit_compat: bor with 2 arguments", -> + assert bor(1, 2) == 3, "bor(1,2) != 3" + +test "bit_compat: bor with 7 arguments", -> + assert bor(1, 2, 4, 8, 16, 32, 64) == 127, "bor(1,2,4,8,16,32,64) != 127" + +test "bit_compat: bor with 7 TCP flag arguments", -> + assert bor(0, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01) == 63, "TCP flags != 63" + +test "bit_compat: band with 3 arguments", -> + assert band(0xFF, 0x3C, 0x1E) == 28, "band(0xFF,0x3C,0x1E) != 28" + +test "bit_compat: bxor with 3 arguments", -> + assert bxor(0xF0, 0x3C, 0xFF) == 51, "bxor(0xF0,0x3C,0xFF) != 51" + +-- Expected values below are hardcoded from LuaJIT's `bit` library, which +-- returns SIGNED 32-bit results (e.g. bit.bor(0x80000000, 0) == -2147483648). +-- ipparse relies on that signedness in the QUIC VarInt encoder, which avoids +-- the negative int32 produced by `bor(0x80000000, value)`. + +test_pure "bit_pure: band with 1 argument", -> + assert pure.band(0xFF) == 255, "band(0xFF) != 255" + +test_pure "bit_pure: band with 2 arguments", -> + assert pure.band(-1, 0xFF) == 255, "band(-1,0xFF) != 255" + assert pure.band(0xFFFFFFFF, 0xFFFF) == 65535, "band(0xFFFFFFFF,0xFFFF) != 65535" + assert pure.band(0x80000000, 0x80000000) == -2147483648, "band of bit 31 != -2147483648" + +test_pure "bit_pure: band with 3 arguments", -> + assert pure.band(0xFF, 0x3C, 0x1E) == 28, "band(0xFF,0x3C,0x1E) != 28" + +test_pure "bit_pure: bor with 1 argument", -> + assert pure.bor(5) == 5, "bor(5) != 5" + assert pure.bor(0x80000000) == -2147483648, "bor(0x80000000) != -2147483648" + +test_pure "bit_pure: bor with 2 arguments", -> + assert pure.bor(1, 2) == 3, "bor(1,2) != 3" + assert pure.bor(0x80000000, 0) == -2147483648, "bor(0x80000000,0) != -2147483648" + assert pure.bor(0x80000000, 0xFF) == -2147483393, "bor(0x80000000,0xFF) != -2147483393" + +test_pure "bit_pure: bor with 3 arguments", -> + assert pure.bor(1, 2, 4) == 7, "bor(1,2,4) != 7" + +test_pure "bit_pure: bor with 7 arguments", -> + assert pure.bor(1, 2, 4, 8, 16, 32, 64) == 127, "bor(1,2,4,8,16,32,64) != 127" + +test_pure "bit_pure: bxor with 1 argument", -> + assert pure.bxor(5) == 5, "bxor(5) != 5" + +test_pure "bit_pure: bxor with 2 arguments", -> + assert pure.bxor(0x80000000, 0xFFFFFFFF) == 2147483647, "bxor(0x80000000,0xFFFFFFFF) != 2147483647" + assert pure.bxor(0x80000000, 0x80000000) == 0, "bxor(0x80000000,0x80000000) != 0" + assert pure.bxor(-1, 0xFF) == -256, "bxor(-1,0xFF) != -256" + +test_pure "bit_pure: bxor with 3 arguments", -> + assert pure.bxor(0xF0, 0x3C, 0xFF) == 51, "bxor(0xF0,0x3C,0xFF) != 51" + +test_pure "bit_pure: bnot", -> + assert pure.bnot(0) == -1, "bnot(0) != -1" + assert pure.bnot(0xFFFFFFFF) == 0, "bnot(0xFFFFFFFF) != 0" + assert pure.bnot(-1) == 0, "bnot(-1) != 0" + +test_pure "bit_pure: lshift by 0, 1 and 31 bits", -> + assert pure.lshift(3, 0) == 3, "lshift(3,0) != 3" + assert pure.lshift(1, 1) == 2, "lshift(1,1) != 2" + assert pure.lshift(1, 31) == -2147483648, "lshift(1,31) != -2147483648" + +test_pure "bit_pure: lshift masks the shift count to 5 bits", -> + assert pure.lshift(1, 32) == 1, "lshift(1,32) != 1" + assert pure.lshift(1, 33) == 2, "lshift(1,33) != 2" + +test_pure "bit_pure: rshift by 0, 1 and 31 bits", -> + assert pure.rshift(3, 0) == 3, "rshift(3,0) != 3" + assert pure.rshift(0x80000000, 1) == 1073741824, "rshift(0x80000000,1) != 1073741824" + assert pure.rshift(0x80000000, 31) == 1, "rshift(0x80000000,31) != 1" + assert pure.rshift(-1, 1) == 2147483647, "rshift(-1,1) != 2147483647" + assert pure.rshift(0x80000000, 32) == -2147483648, "rshift(0x80000000,32) != -2147483648" + +test_pure "bit_pure: arshift sign-extends", -> + assert pure.arshift(3, 0) == 3, "arshift(3,0) != 3" + assert pure.arshift(0x80000000, 1) == -1073741824, "arshift(0x80000000,1) != -1073741824" + assert pure.arshift(-1, 1) == -1, "arshift(-1,1) != -1" + assert pure.arshift(0x40000000, 1) == 536870912, "arshift(0x40000000,1) != 536870912" + +test_pure "bit_pure: IPv6 version/traffic class/flow label round-trip", -> + vtf = pure.bor(pure.lshift(6, 28), pure.lshift(0x12, 20), 0x34567) + assert vtf == 1629701479, "vtf != 1629701479" + assert pure.rshift(vtf, 28) == 6, "version != 6" + assert pure.band(pure.rshift(vtf, 20), 0xFF) == 18, "traffic class != 18" + assert pure.band(vtf, 0xFFFFF) == 214375, "flow label != 214375" + +util.summary "bit_compat" diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon new file mode 100644 index 0000000..720038e --- /dev/null +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -0,0 +1,164 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_pack_compat.moon +-- Byte-for-byte equivalence tests for the LuaJIT pack fallback. + +util = require "ipparse.lib.util" +{:test} = util +pack_compat = require "ipparse.lib.pack_compat" +pack = pack_compat.pack or string.pack +-- Native string.pack rejects the two fallback-only behaviours tested below. +is_fallback = pack != string.pack + +from_hex = (h) -> h\gsub "..", (cc) -> string.char tonumber cc, 16 + +-- Big-endian header-like mix of 16-bit and 8-bit fields. +test "pack_compat: >H B B H H H H", -> + got = pack ">H B B H H H H", 1, 2, 3, 4, 5, 6, 7 + expected = from_hex "000102030004000500060007" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Big-endian mix with a 4-byte integer and a 2-byte-prefixed string. +test "pack_compat: >H H I4 s2", -> + got = pack ">H H I4 s2", 1, 1, 300, "abcd" + expected = from_hex "000100010000012c000461626364" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Little-endian 4-byte integer. +test "pack_compat: + got = pack "i2 negative", -> + got = pack ">i2", -2 + expected = from_hex "fffe" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Fixed-width string shorter than the field is NUL-padded. +test "pack_compat: >c4 short", -> + got = pack ">c4", "ab" + expected = from_hex "61620000" + assert got == expected, "got: #{got}\nexp: #{expected}" + +if is_fallback + -- Fixed-width string longer than the field is truncated. + test "pack_compat: >c4 long", -> + got = pack ">c4", "abcdef" + expected = from_hex "61626364" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- NUL-terminated string gets a trailing \0. +test "pack_compat: z", -> + got = pack "z", "hello" + expected = from_hex "68656c6c6f00" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Padding emits one NUL byte. +test "pack_compat: x", -> + got = pack "x" + expected = from_hex "00" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- 3-byte big-endian unsigned integer. +test "pack_compat: >I3", -> + got = pack ">I3", 0x010203 + expected = from_hex "010203" + assert got == expected, "got: #{got}\nexp: #{expected}" + +if is_fallback + -- uint8 keeps the low byte of out-of-range values (300 % 256 == 44). + test "pack_compat: B 300 wraps", -> + got = pack "B", 300 + expected = from_hex "2c" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- More than eight arguments exercises the generic vararg path. +test "pack_compat: >B x10 more than 8 args", -> + got = pack ">B B B B B B B B B B", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 + expected = from_hex "0102030405060708090a" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Padding byte between integers. +test "pack_compat: >H x H", -> + got = pack ">H x H", 1, 2 + expected = from_hex "0001000002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- String options in the middle of integer fields. +test "pack_compat: >H c2 H", -> + got = pack ">H c2 H", 1, "ab", 2 + expected = from_hex "000161620002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: >H z H", -> + got = pack ">H z H", 1, "ab", 2 + expected = from_hex "00016162000002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: >H s2 H", -> + got = pack ">H s2 H", 1, "ab", 2 + expected = from_hex "0001000261620002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Signed 32-bit negative value wraps to two's complement. +test "pack_compat: >i4 negative", -> + got = pack ">i4", -2 + expected = from_hex "fffffffe" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- 64-bit little-endian value above the 32-bit range keeps its high word. +test "pack_compat: + got = pack " + got = pack "" + assert got == "", "got: #{got}" + +-- ─── 64-bit signed and unsigned values ────────────────────────────────────── + +unpack = pack_compat.unpack or string.unpack + +-- -2 as a signed 64-bit value is 0xff..fe in two's complement. +test "pack_compat: >i8 negative bytes", -> + got = pack ">i8", -2 + expected = from_hex "fffffffffffffffe" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- -1 as a signed 64-bit value is all ones in two's complement. +test "pack_compat: + got = pack "i8 negative 2^53 bytes", -> + got = pack ">i8", -9007199254740992 + expected = from_hex "ffe0000000000000" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: + got = pack " + got = pack "=I4", 0x01020304 + expected = from_hex "04030201" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: >i8 negative round-trip", -> + v, pos = unpack ">i8", pack(">i8", -2) + assert v == -2, "got: #{v}" + assert pos == 9, "pos: #{pos}" + +util.summary "pack_compat" diff --git a/ipparse/tests/lib/test_pack_compat_float.moon b/ipparse/tests/lib/test_pack_compat_float.moon new file mode 100644 index 0000000..86cc3db --- /dev/null +++ b/ipparse/tests/lib/test_pack_compat_float.moon @@ -0,0 +1,78 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_pack_compat_float.moon +-- Floating-point tests for the pack/unpack compatibility layer. +-- Kept in a separate module because the Lua 5.5 kernel runtime has no floats: +-- a single float literal would make the whole chunk fail to compile +-- ("malformed number"), which would also drop the integer tests. This module is +-- therefore NOT listed in test_lunatik_kernel_portable.moon. + +util = require "ipparse.lib.util" +{:test} = util +pack_compat = require "ipparse.lib.pack_compat" +pack = pack_compat.pack or string.pack +unpack = pack_compat.unpack or string.unpack + +from_hex = (h) -> h\gsub "..", (cc) -> string.char tonumber cc, 16 + +-- 1.5 is exactly 0x3ff8000000000000; little-endian byte order. +test "pack_compat_float: + got = pack "d -2.5 bytes", -> + got = pack ">d", -2.5 + expected = from_hex "c004000000000000" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- 1.5 is exactly 0x3fc00000 as a 32-bit float; little-endian byte order. +test "pack_compat_float: + got = pack " + v, pos = unpack "d round-trip", -> + v, pos = unpack ">d", pack(">d", 123.456) + assert v == 123.456, "got: #{v}" + assert pos == 9, "pos: #{pos}" + +test "pack_compat_float: + v, pos = unpack " + assert unpack(" + got = pack " + v = 2 ^ -1074 + got = pack " + nan = math.huge - math.huge + v = unpack " +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_sha.moon +-- Tests for the hand-written ipparse/lib/sha.lua (SHA-256 + HMAC-SHA256). +-- sha.lua requires Lua 5.3+ native bitwise operators; skip on LuaJIT/5.1. + +util = require "ipparse.lib.util" +{:test, :summary} = util + +ok, sha = pcall require, "ipparse.lib.sha" +unless ok and sha and sha.sha256 and sha.hmac + test "sha: unavailable on this runtime (skipped)", -> true + summary "sha" + return + +{:sha256, :hmac, :bin_to_hex} = sha + +hex = (s) -> bin_to_hex s + +-- FIPS 180-2 / NIST short message vectors. +test "sha: FIPS 180-2 'abc'", -> + assert hex(sha256 "abc") == + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + +test "sha: FIPS 180-2 empty message", -> + assert hex(sha256 "") == + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + +test "sha: FIPS 180-2 64 x 'a'", -> + assert hex(sha256 (string.rep "a", 64)) == + "ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb" + +test "sha: NIST 100 x 'a'", -> + assert hex(sha256 (string.rep "a", 100)) == + "2816597888e4a0d3a36b82b83316ab32680eb8f00f8cd3b904d681246d285a0e" + +test "sha: NIST long message", -> + assert hex(sha256 string.rep "a", 1000) == + "41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3" + +-- HMAC-SHA256 (RFC 4231). +test "hmac: RFC 4231 test case 1", -> + key = string.rep "\011", 20 + digest = hmac sha256, key, "Hi There" + assert #digest == 32, "hmac must return a 32-byte binary digest" + assert hex(digest) == + "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" + +test "hmac: RFC 4231 test case 2", -> + digest = hmac sha256, "Jefe", "what do ya want for nothing?" + assert hex(digest) == + "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" + +summary "sha" diff --git a/ipparse/tests/run_all.moon b/ipparse/tests/run_all.moon index 3b98cad..993d35a 100644 --- a/ipparse/tests/run_all.moon +++ b/ipparse/tests/run_all.moon @@ -27,7 +27,11 @@ mods = { "ipparse.tests.l7.test_dns" "ipparse.tests.l7.tls.test_handshake" "ipparse.tests.test_malformed" + "ipparse.tests.lib.test_sha" "ipparse.tests.lib.test_hkdf" + "ipparse.tests.lib.test_bit_compat" + "ipparse.tests.lib.test_pack_compat" + "ipparse.tests.lib.test_pack_compat_float" "ipparse.tests.lib.crypto.test_ffi_wolfssl" "ipparse.tests.lib.crypto.test_ffi_mbedtls" "ipparse.tests.l4.quic.test_varint" diff --git a/ipparse/tests/test_fun.moon b/ipparse/tests/test_fun.moon index d5ee470..0fa8b56 100644 --- a/ipparse/tests/test_fun.moon +++ b/ipparse/tests/test_fun.moon @@ -91,4 +91,46 @@ test "opairs returns sorted keys", -> test "zero_indexed copies t[1] to t[0]", -> t = zero_indexed {"x", "y", "z"} assert t[0] == "x", "t[0] should be 'x', got '#{t[0]}'" + +test "bidirectional invalidates stale cache on key modification", -> + t = bidirectional {a: 1} + assert t[1] == "a", "t[1] should be 'a', got '#{t[1]}'" + t.a = 2 + assert t[1] == nil, "t[1] should be nil after t.a changed, got '#{t[1]}'" + assert t[2] == "a", "t[2] should be 'a' after change, got '#{t[2]}'" + +test "bidirectional drops cache for deleted key", -> + t = bidirectional {a: 1} + assert t[1] == "a", "t[1] should be 'a', got '#{t[1]}'" + t.a = nil + assert t[1] == nil, "t[1] should be nil after key deletion, got '#{t[1]}'" + +test "bidirectional cache cycle after deletion", -> + t = bidirectional {x: "y", y: "x"} + assert t["y"] == "x", "t['y'] should be 'x', got '#{t['y']}'" + assert t["x"] == "y", "t['x'] should be 'y', got '#{t['x']}'" + t.x = nil + t.y = nil + assert t["y"] == nil, "t['y'] should be nil after deletion, got '#{t['y']}'" + +test "bidirectional non-regression forward and reverse lookup", -> + t = bidirectional {a: 1, b: 2} + assert t.a == 1, "t.a should be 1" + assert t.b == 2, "t.b should be 2" + assert t[1] == "a", "t[1] should be 'a'" + assert t[2] == "b", "t[2] should be 'b'" + +test "bidirectional reverse sees key added later", -> + t = bidirectional {a: 1} + t.b = 2 + assert t[2] == "b", "t[2] should be 'b' after adding t.b, got '#{t[2]}'" + +test "bidirectional pairs only sees real entries", -> + t = bidirectional {a: 1, b: 2} + t[1] + t[2] + n = 0 + for k, v in pairs t + n += 1 + assert n == 2, "pairs should see 2 entries, got #{n}" util.summary "fun" diff --git a/ipparse/tests/test_init.moon b/ipparse/tests/test_init.moon index 8a1d7c5..27c6ded 100644 --- a/ipparse/tests/test_init.moon +++ b/ipparse/tests/test_init.moon @@ -12,14 +12,14 @@ util = require"ipparse.lib.util" test "hex2bin converts hex to bytes", -> result = hex2bin "ff00" - assert result == "\xff\x00", "hex2bin failed" + assert result == "\255\000", "hex2bin failed" test "hex2bin single byte", -> result = hex2bin "ab" - assert result == "\xab", "hex2bin single byte failed" + assert result == "\171", "hex2bin single byte failed" test "filterascii replaces non-printable with dot", -> - result = filterascii "\x01hello\xff" + result = filterascii "\001hello\255" assert result == ".hello.", "expected '.hello.', got '#{result}'" test "filterascii keeps printable chars", -> diff --git a/ipparse/tests/test_lunatik_kernel_portable.moon b/ipparse/tests/test_lunatik_kernel_portable.moon new file mode 100644 index 0000000..4ebf957 --- /dev/null +++ b/ipparse/tests/test_lunatik_kernel_portable.moon @@ -0,0 +1,64 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/test_lunatik_kernel_portable.moon +-- Runs the parsing/packing test modules that are portable to the Lunatik +-- kernel runtime: no FFI, no file/pcap access, no simulated package.preload. +-- FFI-dependent modules are still listed where their FFI use is optional and +-- guarded by pcall; they then only exercise their pure-Lua assertions. + +util = require "ipparse.lib.util" + +-- Preload ipparse.fun at the first level: almost every module below requires +-- it (often two or three `require` levels down), and Lunatik's 16 KiB kernel +-- stack makes loading it from inside a pcall(require …) chain tight. Loading +-- it once here, where the C stack is shallowest, keeps the suite independent +-- of the chain depth. Real usage requires ipparse.fun at depth 3 at most +-- (e.g. script → ipparse.l7.quic.session → ipparse.l4.quic → fun). +require "ipparse.fun" + +mods = { + "ipparse.tests.test_fun" + "ipparse.tests.test_init" + "ipparse.tests.l2.test_ethernet" + "ipparse.tests.l3.test_checksum" + "ipparse.tests.l3.test_ip4" + "ipparse.tests.l3.test_ip6" + "ipparse.tests.l3.test_ip" + "ipparse.tests.l4.test_tcp" + "ipparse.tests.l4.test_udp" + "ipparse.tests.l4.test_tcp_stream" + "ipparse.tests.l7.test_dns" + "ipparse.tests.l7.tls.test_handshake" + "ipparse.tests.test_malformed" + "ipparse.tests.lib.test_sha" + "ipparse.tests.lib.test_hkdf" + "ipparse.tests.lib.test_bit_compat" + "ipparse.tests.lib.test_pack_compat" + "ipparse.tests.l4.quic.test_varint" + "ipparse.tests.l4.quic.test_header" + "ipparse.tests.l4.quic.test_frames" + "ipparse.tests.l4.quic.test_keys" + "ipparse.tests.l4.quic.test_protection" + "ipparse.tests.l4.quic.test_integration" + "ipparse.tests.l7.quic.test_session" +} + +total_pass, total_all = 0, 0 +load_errors = 0 +for mod in *mods + ok, err = pcall require, mod + if ok + total_pass += util._last_pass + total_all += util._last_total + else + load_errors += 1 + print "ERROR loading #{mod}: #{err}" + +print "\n==> Kernel portable total: #{total_pass}/#{total_all}" +failed = total_pass != total_all or load_errors > 0 +if failed + print "FAIL kernel portable suite: #{load_errors} load error(s), #{total_pass}/#{total_all} passed" + error "kernel portable suite failed" diff --git a/ipparse/tests/test_malformed.moon b/ipparse/tests/test_malformed.moon index 1c78b61..86c5873 100644 --- a/ipparse/tests/test_malformed.moon +++ b/ipparse/tests/test_malformed.moon @@ -18,12 +18,12 @@ quic = require"ipparse.l4.quic" frames = require"ipparse.l4.quic.frames" test "ip6: truncated header returns nil", -> - res, off = ip6.parse "\x60\x00\x00\x00", 1 + res, off = ip6.parse "\096\000\000\000", 1 assert res == nil and off == 1, "truncated IPv6 header should return nil" test "ip: get_version out of bounds returns nil", -> assert ip.get_version("", 1) == nil, "empty buffer" - assert ip.get_version("\x45", 5) == nil, "offset past end" + assert ip.get_version("\069", 5) == nil, "offset past end" test "ip: parse empty buffer returns nil, err", -> res, err = ip.parse "", 1 @@ -35,25 +35,25 @@ test "quic: parse empty buffer returns nil", -> test "quic: truncated long header returns nil", -> -- Long-header flag set, then nothing else. - q, off = quic.parse "\xc0\x00", 1 + q, off = quic.parse "\192\000", 1 assert q == nil, "truncated long header should return nil" test "quic: ACK frame with huge range count is rejected", -> -- type=0x02, largest=0, delay=0, range_count=0x3FFFFFFF (varint 4 bytes), first_range=0 - raw = "\x02\x00\x00" .. "\xbf\xff\xff\xff" .. "\x00" + raw = "\002\000\000" .. "\191\255\255\255" .. "\000" frame, _, err = frames.parse_frame raw, 1 assert frame == nil and err and err\match("too large"), "expected range-count rejection, got #{err}" test "quic: validate_frames detects truncated CRYPTO frame", -> -- CRYPTO frame: type=0x06, offset=0, length=100, but no data - raw = "\x06\x00\x40\x64" + raw = "\006\000\064\100" ok, err = frames.validate_frames raw assert ok == false and err, "truncated CRYPTO should fail validation" test "dns: circular compression pointer is rejected", -> -- Minimal DNS header (12 bytes) + a name that is a pointer to itself. hdr = sp ">HHHHHH", 0x1234, 0x0100, 1, 0, 0, 0 - name = "\xc0\x0c" -- pointer to offset 12 = itself + name = "\192\012" -- pointer to offset 12 = itself qd = name .. sp ">HH", 1, 1 msg = hdr .. qd res, _, err = dns.parse msg, 1 @@ -65,11 +65,11 @@ test "dns: circular compression pointer is rejected", -> test "dns: truncated label returns error", -> -- Label announces 10 bytes, provides 2. - lbls, _, err = dns.labels "\x0aab", 1, 1 + lbls, _, err = dns.labels "\010ab", 1, 1 assert lbls == nil and err, "truncated label should return an error" test "pack_compat: unpack on short data raises a clear error", -> - ok, err = pcall su, ">I4", "\x01\x02", 1 + ok, err = pcall su, ">I4", "\001\002", 1 assert not ok and tostring(err)\match("too short"), "expected 'data string too short', got #{err}" util.summary "malformed" diff --git a/tools/vmtest/.gitignore b/tools/vmtest/.gitignore index e7f9d25..a71dd17 100644 --- a/tools/vmtest/.gitignore +++ b/tools/vmtest/.gitignore @@ -1,2 +1,5 @@ -build/ -.venv/ +# Leading slashes anchor to this directory and also match a symlink +# (e.g. `build -> ../../elsewhere/tools/vmtest/build` for a shared cache), +# which a trailing-slash pattern (`build/`) does not. +/build +/.venv diff --git a/tools/vmtest/README.md b/tools/vmtest/README.md index db72b5a..afc1156 100644 --- a/tools/vmtest/README.md +++ b/tools/vmtest/README.md @@ -44,11 +44,19 @@ Variables utiles : du clone épinglé ; - `VMTEST_BUILD_DIR` — déplacer le cache de build. +Les deux sont résolus en chemins absolus et transmis à l'invité, qui monte le +système de fichiers de l'hôte en lecture seule : un cache situé hors du dépôt +(par exemple `tools/vmtest/build` en lien symbolique vers un cache partagé) +fonctionne tel quel. Chaque lancement utilise son propre répertoire temporaire +pour rapatrier sortie/code de retour, donc deux `run.sh` simultanés ne se +marchèvent pas (`VMTEST_RW_DIR` permet de forcer un répertoire commun, à +réserver à un usage séquentiel). + ## Fichiers | Fichier | Rôle | |---|---| -| `versions.env` | Versions épinglées (noyau, dépôt/branche lunatik) | +| `versions.env` | Versions épinglées (noyau, dépôt/étiquette/commit lunatik) | | `kernel.fragment` | Options de config noyau requises par Lunatik | | `flake.nix` | Toolchain reproductible (gcc, qemu, luajit, moonscript…) | | `ensure-vng.sh` | Installe virtme-ng dans `.venv/` (absent de nixpkgs) | diff --git a/tools/vmtest/build-lunatik.sh b/tools/vmtest/build-lunatik.sh index 0a28a6c..ce8a4e0 100755 --- a/tools/vmtest/build-lunatik.sh +++ b/tools/vmtest/build-lunatik.sh @@ -6,17 +6,51 @@ [ -e "$KERNEL_IMAGE" ] || die "kernel not built yet — run kernel.sh first" -if [ ! -d "$LUNATIK_SRC" ]; then +# LUNATIK_REF is a tag by default; verify it still points at LUNATIK_COMMIT. +# LUNATIK_COMMIT is empty when testing an unpinned reference (master, or an +# arbitrary ref supplied by the CI workflow). +clone_pinned() { log "Cloning $LUNATIK_REPO@$LUNATIK_REF…" + rm -rf "$LUNATIK_SRC" git clone --depth 1 --branch "$LUNATIK_REF" --recurse-submodules \ "$LUNATIK_REPO" "$LUNATIK_SRC" + if [ -n "${LUNATIK_COMMIT:-}" ]; then + head="$(git -C "$LUNATIK_SRC" rev-parse HEAD)" + if [ "$head" != "$LUNATIK_COMMIT" ]; then + die "$LUNATIK_REF resolved to $head, expected pinned commit $LUNATIK_COMMIT (was the tag moved?)" + fi + fi +} + +lunatik_head() { + git -C "$LUNATIK_SRC" rev-parse HEAD 2>/dev/null || echo unknown +} + +if [ ! -d "$LUNATIK_SRC" ]; then + # An absent directory is cloned even when LUNATIK_SRC was named explicitly. + clone_pinned +elif [ -z "$LUNATIK_SRC_USER" ]; then + # Harness-managed cache: never trust a stale checkout silently. + if [ -n "${LUNATIK_COMMIT:-}" ] && [ "$(lunatik_head)" != "$LUNATIK_COMMIT" ]; then + log "Cached Lunatik checkout is at $(lunatik_head), expected $LUNATIK_COMMIT — re-cloning…" + clone_pinned + fi +else + head="$(lunatik_head)" + if [ -n "${LUNATIK_COMMIT:-}" ] && [ "$head" != "$LUNATIK_COMMIT" ]; then + log "WARNING: user-provided LUNATIK_SRC $LUNATIK_SRC is at $head, not the pinned $LUNATIK_COMMIT — using it as-is" + fi fi KREL="$(make -s -C "$KERNEL_SRC" kernelrelease)" log "Building lunatik against $KREL ($KERNEL_SRC)…" # cd rather than make -C: lunatik's Makefile passes M=$(PWD) to Kbuild, # and PWD is the caller's directory unless we actually chdir. +# Upstream v5.1 defaults LUA to lua5.5 (bundled kernel Lua). LUA here only +# drives host-side code generation and the CLI, so use the 5.4 toolchain +# already required by CI; the kernel module still embeds Lua 5.5. (cd "$LUNATIK_SRC" && make \ + LUA="${LUNATIK_LUA:-lua5.4}" \ KERNEL_RELEASE="$KREL" \ MODULES_BUILD_PATH="$KERNEL_SRC" \ -j"$(nproc)") diff --git a/tools/vmtest/flake.nix b/tools/vmtest/flake.nix index c9b4096..061dbca 100644 --- a/tools/vmtest/flake.nix +++ b/tools/vmtest/flake.nix @@ -43,6 +43,9 @@ git curl kmod + # Lunatik master builds its eBPF loader against libbpf. + libbpf + pkg-config ]; shellHook = '' # Nix binaries carry their rpath; an inherited LD_LIBRARY_PATH only diff --git a/tools/vmtest/guest-test.sh b/tools/vmtest/guest-test.sh index 1c08a2a..a3309be 100755 --- a/tools/vmtest/guest-test.sh +++ b/tools/vmtest/guest-test.sh @@ -8,17 +8,22 @@ KREL="$(cat "$BUILD_DIR/kernelrelease")" [ "$(uname -r)" = "$KREL" ] || die "VM kernel $(uname -r) != expected $KREL" log "Installing lunatik modules + scripts into the overlay…" -# LUA_PATH (make variable): where the lunatik.config symlink goes. The -# default derived from lua5.4's package.path points into the read-only -# /nix/store; use the writable overlay instead. +# LUA_PATH/LUA_CPATH (make variables): where lunatik installs its modules and +# eBPF loader. Their defaults are derived from package.path/package.cpath, +# which point into the read-only /nix/store; use the writable overlay instead. +# Upstream master derives both from $(LUA) (default lua5.5), hence passing +# LUA explicitly as well. GUEST_LUA_DIR=/usr/local/share/lua/5.4 +GUEST_LUA_CPATH=/usr/local/lib/lua/5.4 # cd rather than make -C: lunatik's Makefile uses $(PWD) # INSTALL: /lib/modules is a 9p mount mapped to the host user; chown to # root is impossible there (and irrelevant for tests). (cd "$LUNATIK_SRC" && make install \ + LUA=lua5.4 \ KERNEL_RELEASE="$KREL" \ MODULES_BUILD_PATH="$KERNEL_SRC" \ LUA_PATH="$GUEST_LUA_DIR" \ + LUA_CPATH="$GUEST_LUA_CPATH" \ INSTALL="install" \ >/dev/null) depmod "$KREL" @@ -29,6 +34,7 @@ mkdir -p /usr/local/bin cat > /usr/local/bin/lunatik <&2; } die() { printf '\033[1;31m[vmtest]\033[0m %s\n' "$*" >&2; exit 1; } diff --git a/tools/vmtest/run.sh b/tools/vmtest/run.sh index f5c2074..d6b127c 100755 --- a/tools/vmtest/run.sh +++ b/tools/vmtest/run.sh @@ -25,8 +25,17 @@ export PATH="$VMTEST_DIR/.venv/bin:$PATH" # code into a shared writable directory instead. BASH_ABS="$(command -v bash)" # Must live OUTSIDE the repository: nested inside --rodir it would be -# shadowed by the read-only mount. -RW_DIR="${VMTEST_RW_DIR:-${TMPDIR:-/tmp}/vmtest-rw}" +# shadowed by the read-only mount. Default to a fresh directory per run so +# concurrent invocations do not clobber each other's out/rc/console.log. +if [ -n "${VMTEST_RW_DIR:-}" ]; then + RW_DIR="$VMTEST_RW_DIR" + RW_DIR_OWNED=0 +else + RW_DIR="$(mktemp -d "${TMPDIR:-/tmp}/vmtest-rw.XXXXXX")" + RW_DIR_OWNED=1 + # Keep the console log around on failure (the error message points to it). + trap 'st=$?; if [ "$st" -eq 0 ] && [ "$RW_DIR_OWNED" -eq 1 ]; then rm -rf "$RW_DIR"; fi' EXIT +fi # vng bind-mounts the kernel's module dir read-only over /lib/modules; # lunatik and ipparse need to install there, so mount a writable 9p # (host-backed, reusable across boots) on top instead. @@ -39,6 +48,10 @@ in_vm() { { printf '#!%s\n' "$BASH_ABS" printf 'export PATH=%q\n' "$PATH" + # The guest re-sources lib.sh, which would otherwise fall back to the + # build dir inside the (read-only) repo instead of the host's cache. + printf 'export VMTEST_BUILD_DIR=%q\n' "$BUILD_DIR" + printf 'export LUNATIK_SRC=%q\n' "$LUNATIK_SRC" printf '( %s ) > %q/out 2>&1\n' "$*" "$RW_DIR" printf 'echo $? > %q/rc\n' "$RW_DIR" } > "$wrapper" diff --git a/tools/vmtest/versions.env b/tools/vmtest/versions.env index d338d88..419c432 100644 --- a/tools/vmtest/versions.env +++ b/tools/vmtest/versions.env @@ -1,10 +1,16 @@ # Pinned versions for the VM test environment. # Single source of truth — sourced by the scripts and hashed for CI caching. +# Values are defaults: an environment variable with the same name wins, so a +# job can override e.g. LUNATIK_REF to test a new Lunatik reference. # Linux LTS kernel the tests run against. -KERNEL_VERSION=6.18.35 +: "${KERNEL_VERSION:=6.18.35}" -# Lunatik source: pinned on jperon's fork until the fix_crypto_enomem PR -# is merged into luainkernel/lunatik master. -LUNATIK_REPO=https://github.com/jperon/lunatik -LUNATIK_REF=fix_crypto_enomem +# Lunatik source: upstream release tag, with the exact commit it must resolve +# to so a moved tag is detected. The fix_crypto_enomem branch of the fork is +# abandoned: its crypto preallocation fix (commit 95318191) is in v5.1. +: "${LUNATIK_REPO:=https://github.com/luainkernel/lunatik}" +: "${LUNATIK_REF:=v5.1}" +# `=` (not `:=`) so an explicitly empty value stays empty when testing an +# unpinned reference. +: "${LUNATIK_COMMIT=e9b48135f45ca8a950015a3ee7280d54368d09c4}"