aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/protofield.lua
blob: c4026e5cf85c03d1cac650c85f59b75382ccbe24 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
----------------------------------------
-- script-name: protofield.lua
-- This is based on the dissector.lua example script, which is also used for testing.
-- Unlike that one, this one is purely for testing even more things, notably
-- the ProtoField API.
----------------------------------------

------------- general test helper funcs ------------
local OTHER = "other"

local total_tests = 0
local function getTotal()
    return total_tests
end

local passed = {}
local function setPassed(name)
    if not passed[name] then
        passed[name] = 1
    else
        passed[name] = passed[name] + 1
    end
    total_tests = total_tests + 1
end

local fail_count = 0
local function setFailed(name)
    fail_count = fail_count + 1
    total_tests = total_tests + 1
end

-- expected number of runs
local taptests = { [OTHER]=28 }
local function getResults()
    print("\n-----------------------------\n")
    for k,v in pairs(taptests) do
        if v ~= 0 and passed[k] ~= v then
            print("Something didn't run or ran too much... tests failed!")
            print("Dissector type "..k.." expected: "..v..", but got: "..tostring(passed[k]))
            return false
        end
    end
    print("All tests passed!\n\n")
    return true
end

local function test(type,name, ...)
    io.stdout:write("test "..type.."-->"..name.."-"..getTotal().."...")
    if (...) == true then
        setPassed(type)
        io.stdout:write("passed\n")
        return true
    else
        setFailed(type)
        io.stdout:write("failed!\n")
        error(name.." test failed!")
    end
end

------------- test script ------------

----------------------------------
-- modify original test function for now, kinda sorta
local orig_test = test
test = function (...)
    return orig_test(OTHER,...)
end

----------------------------------------
local test_proto = Proto.new("test", "Test Proto")
test_proto.fields.time_field = ProtoField.uint16("test.time", "Time", base.UNIT_STRING, {" sec", " secs"})
test_proto.fields.dist_field = ProtoField.uint16("test.dist", "Distance", base.UNIT_STRING, {" km"})

-- Field name: empty, illegal, incompatible
success = pcall(ProtoField.int8, nil, "empty field name 1")
test("ProtoField-empty-field-name-1", not success)

success = pcall(ProtoField.int8, "", "empty field name 2")
test("ProtoField-empty-field-name-2", not success)

success = pcall(ProtoField.int8, "test.$", "illegal field name")
test("ProtoField-illegal-field-name", not success)

success = pcall(ProtoField.int8, "frame.time", "incompatible field name")
test("ProtoField-incompatible-field-name", not success)

-- Actual name: empty
success = pcall(ProtoField.int8, "test.empty_name_1")
test("ProtoField-empty-name-1", success)  -- will use abbrev

success = pcall(ProtoField.int8, "test.empty_name_2", "")
test("ProtoField-empty-name-2", not success)

-- Signed integer base values, only base.DEC should work
success = pcall(ProtoField.int8, "test.int.base_none", "int base NONE", base.NONE)
test("ProtoField-int-base-none", not success)

success = pcall(ProtoField.int8, "test.int.base_dec", "int base DEC", base.DEC)
test("ProtoField-int-base-dec", success)

success = pcall(ProtoField.int8, "test.int.base_hex", "int base HEX", base.HEX)
test("ProtoField-int-base-hex", not success)

success = pcall(ProtoField.int8, "test.int.base_oct", "int base OCT", base.OCT)
test("ProtoField-int-base-oct", not success)

success = pcall(ProtoField.int8, "test.int.base_dec_hex", "int base DEC_HEX", base.DEC_HEX)
test("ProtoField-int-base-dec-hex", not success)

success = pcall(ProtoField.int8, "test.int.base_hex_dec", "int base HEX_DEC", base.HEX_DEC)
test("ProtoField-int-base-hex-dec", not success)

-- Passing no table should not work
success = pcall(ProtoField.uint16, "test.bad0", "Bad0", base.UNIT_STRING)
test("ProtoField-unitstring-no-table", not success)

-- Passing an empty table should not work
success = pcall(ProtoField.uint16, "test.bad1", "Bad1", base.UNIT_STRING, {})
test("ProtoField-unitstring-empty-table", not success)

-- Passing userdata should not work
success = pcall(ProtoField.uint16, "test.bad2", "Bad2", base.UNIT_STRING, {test_proto})
test("ProtoField-unitstring-userdata", not success)

-- Too many items are not supported
success = pcall(ProtoField.uint16, "test.bad3", "Bad3", base.UNIT_STRING, {"too", "many", "items"})
test("ProtoField-unitstring-too-many-items", not success)

local numinits = 0
function test_proto.init()
    numinits = numinits + 1
    if numinits == 2 then
        getResults()
    end
end

-- Test expected text with singular and plural forms
function test_proto.dissector(tvb, pinfo, tree)
    local ti

    local tvb1 = ByteArray.new("00 00"):tvb("Tvb1")
    ti = tree:add(test_proto.fields.time_field, tvb1())
    test("Time: 0 secs", ti.text == "Time: 0 secs")
    ti = tree:add(test_proto.fields.dist_field, tvb1())
    test("Distance: 0 km", ti.text == "Distance: 0 km")

    local tvb2 = ByteArray.new("00 01"):tvb("Tvb2")
    ti = tree:add(test_proto.fields.time_field, tvb2())
    test("Time: 1 sec", ti.text == "Time: 1 sec")
    ti = tree:add(test_proto.fields.dist_field, tvb2())
    test("Distance: 1 km", ti.text == "Distance: 1 km")

    local tvb3 = ByteArray.new("ff ff"):tvb("Tvb3")
    ti = tree:add(test_proto.fields.time_field, tvb3())
    test("Time: 65535 secs", ti.text == "Time: 65535 secs")
    ti = tree:add(test_proto.fields.dist_field, tvb3())
    test("Distance: 65535 km", ti.text == "Distance: 65535 km")
end

DissectorTable.get("udp.port"):add(65333, test_proto)