diff options
author | Hadriel Kaplan <hadrielk@yahoo.com> | 2014-02-09 14:27:51 -0500 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2014-02-13 22:32:58 +0000 |
commit | 58fe488822ef7e59b1f7e85f04d4a19876c39769 (patch) | |
tree | 7661e201e4d2c9194c16e6cff4b98a798b464cb5 /test | |
parent | 1eeb33a7b069311e62b7300b08eb9de5669e7e3f (diff) |
Lua: add ability for scripts loaded from command-line to be passed arguments
This change adds the ability to pass on to lua scripts loaded from the
command-line (tshark or wireshark) additional arguments supplied by the
command-line. This will help us in our testsuites, but also might be
useful for user-created scripts. The additional arguments are passed in
using the '-X' eXtension switch.
Change-Id: Ib94cdf1ffd194ca84692fee7816665e4ff95efbd
Reviewed-on: https://code.wireshark.org/review/156
Reviewed-by: Evan Huus <eapache@gmail.com>
Tested-by: Evan Huus <eapache@gmail.com>
Diffstat (limited to 'test')
-rwxr-xr-x | test/lua/script_args.lua | 36 | ||||
-rwxr-xr-x | test/suite-unittests.sh | 39 |
2 files changed, 75 insertions, 0 deletions
diff --git a/test/lua/script_args.lua b/test/lua/script_args.lua new file mode 100755 index 0000000000..2bf9d47025 --- /dev/null +++ b/test/lua/script_args.lua @@ -0,0 +1,36 @@ +---------------------------------------- +-- This just verifies the number of args it got is what it expected. +-- The first arg should be a number, for how many total args to expect, +-- including itself. + +local function testing(...) + print("---- Testing "..tostring(...).." ----") +end + +local function test(name, result) + io.stdout:write("test "..name.."...") + if result == true then + io.stdout:write("passed\n") + else + io.stdout:write("failed!\n") + error(name.." test failed!") + end +end + +----------------------------- + +testing("Command-line args") + +local arg={...} -- get passed-in args + +test("arg1", arg ~= nil and #arg > 0) + +local numargs = tonumber(arg[1]) +test("arg2", numargs ~= nil) + +test("arg3", #arg == numargs) + +print("\n-----------------------------\n") + +print("All tests passed!\n\n") + diff --git a/test/suite-unittests.sh b/test/suite-unittests.sh index 4d713678e8..f1ae85c227 100755 --- a/test/suite-unittests.sh +++ b/test/suite-unittests.sh @@ -114,6 +114,44 @@ unittests_step_lua_int64_test() { fi } +unittests_step_lua_args_test() { + if [ $HAVE_LUA -ne 0 ]; then + test_step_skipped + return + fi + + # Tshark catches lua script failures, so we have to parse the output. + $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:1 > testout.txt 2>&1 + grep -q "All tests passed!" testout.txt + if [ $? -ne 0 ]; then + cat testout.txt + test_step_failed "lua_args_test test 1 failed" + fi + $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1 + grep -q "All tests passed!" testout.txt + if [ $? -ne 0 ]; then + cat testout.txt + test_step_failed "lua_args_test test 2 failed" + fi + $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script2:1 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1 + grep -q "All tests passed!" testout.txt + if [ $? -ne 0 ]; then + cat testout.txt + test_step_failed "lua_args_test test 3 failed" + fi + $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua > testout.txt 2>&1 + if grep -q "All tests passed!" testout.txt; then + cat testout.txt + test_step_failed "lua_args_test negative test 4 failed" + fi + $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 > testout.txt 2>&1 + if grep -q "All tests passed!" testout.txt; then + cat testout.txt + test_step_failed "lua_args_test negative test 5 failed" + fi + test_step_ok +} + unittests_step_oids_test() { DUT=$SOURCE_DIR/epan/oids_test ARGS= @@ -149,6 +187,7 @@ unittests_suite() { test_step_add "exntest" unittests_step_exntest test_step_add "lua dissector" unittests_step_lua_dissector_test test_step_add "lua int64" unittests_step_lua_int64_test + test_step_add "lua script arguments" unittests_step_lua_args_test test_step_add "oids_test" unittests_step_oids_test test_step_add "reassemble_test" unittests_step_reassemble_test test_step_add "tvbtest" unittests_step_tvbtest |