summaryrefslogtreecommitdiff
path: root/lua/setup.lua
blob: 4a21980a5a60837c1c2e7eb5a7187d9e9c2a743c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
print"============================================"

local keepgoing = nil

for _,v in ipairs(arg) do
  if v == "-k" then
    keepgoing = true
  end
end

require"net"
require"pcap"

DEV="en0"

-- Quote a string into lua form (including the non-printable characters from
-- 0-31, and from 127-255).
function q(_)
  local fmt = string.format
  local _ = fmt("%q", _)

  _ = string.gsub(_, "\\\n", "\\n")
  _ = string.gsub(_, "[%z\1-\31,\127-\255]", function (x)
    --print("x=", x)
    return fmt("\\%03d",string.byte(x))
  end)

  return _
end

function h(_)
  local fmt = string.format
  _ = string.gsub(_, ".", function (x)
    return fmt("%02x",string.byte(x))
  end)

  return _
end

function dump(n, size)
  local b = n:block()
  print(">")
  print(n:dump())
  print("size="..#b)
  --print("q=[["..q(b).."]]")
  print("h=[["..h(b).."]]")

  if _dumper then
      assert(_dumper:dump(b))
      assert(_dumper:flush())
  end

  if size then
    assert(#b == size, "block's size is not expected, "..size)
  end
end

function pcap_dumper(fname)
    local cap = assert(pcap.open_dead(pcap.DLT.EN10MB))
    local dmp = assert(cap:dump_open(fname))
    assert(dmp:flush())
    cap:close()
    return dmp
end

function test(n, f)
    local strip = {
        ["+"]="-";
        [" "]="-";
        ["/"]="-";
        [","]="";
    }
    local pcap_name = "out"..string.gsub(n, ".", strip)..".pcap"
    _dumper = pcap_dumper(pcap_name)

    print""
    print""
    print("=test: "..n.." ("..pcap_name..")")

    if not keepgoing then
        f()
        print("+pass: "..n)
    else
        local ok, emsg = pcall(f)
        if not ok then
            print("! FAIL: "..n)
        else
            print("+pass: "..n)
        end
    end
    _dumper = _dumper:close()
end

function hex_dump(s)
    print(h(s))
end