diff --git a/simulations/simulation_single_telegram.txt b/simulations/simulation_single_telegram.txt new file mode 100644 index 0000000..6cc8672 --- /dev/null +++ b/simulations/simulation_single_telegram.txt @@ -0,0 +1 @@ +telegram=|2A442D2C998734761B168D2091D37CAC21576C78_02FF207100041308190000441308190000615B7F616713| diff --git a/src/cmdline.cc b/src/cmdline.cc index 42e39a4..014ba10 100644 --- a/src/cmdline.cc +++ b/src/cmdline.cc @@ -796,16 +796,16 @@ shared_ptr parseCommandLineWithUseConfig(Configuration *c, int ar c->pid_file = argv[i]; i++; - if (i+1 < argc) + if (i < argc) { error("Usage error: you must supply the pid file as the last argument to wmbusmetersd.\n"); } } else { - if (i+1 < argc) + if (i < argc) { - error("Usage error: too many arguments \"%s\".\n", argv[i]); + error("Usage error: too many arguments \"%s\" with --useconfig=...\n", argv[i]); } } return shared_ptr(c); diff --git a/test.sh b/test.sh index 9368a53..ef48259 100755 --- a/test.sh +++ b/test.sh @@ -99,6 +99,9 @@ if [ "$?" != "0" ]; then RC="1"; fi tests/test_linkmodes.sh $PROG if [ "$?" != "0" ]; then RC="1"; fi +tests/test_cmdline.sh $PROG +if [ "$?" != "0" ]; then RC="1"; fi + tests/test_additional_json.sh $PROG if [ "$?" != "0" ]; then RC="1"; fi diff --git a/tests/test_cmdline.sh b/tests/test_cmdline.sh new file mode 100755 index 0000000..5296a18 --- /dev/null +++ b/tests/test_cmdline.sh @@ -0,0 +1,114 @@ +#!/bin/sh + +PROG="$1" +TEST=testoutput +mkdir -p $TEST + +TESTNAME="Test cmdline --overridedevice and --useconfig" +TESTRESULT="ERROR" + +$PROG --overridedevice=simulations/simulation_single_telegram.txt --useconfig=tests/config12 > $TEST/test_output.txt 2> $TEST/test_stderr.txt + +if [ "$?" = "0" ] +then + echo 6.408 > $TEST/test_expected.txt + diff $TEST/test_expected.txt $TEST/test_output.txt + if [ "$?" = "0" ] + then + echo "OK: $TESTNAME" + TESTRESULT="OK" + fi +fi + +if [ "$TESTRESULT" = "ERROR" ]; then echo ERROR: $TESTNAME; exit 1; fi + +TESTNAME="Test cmdline --overridedevice without --useconfig" +TESTRESULT="ERROR" + +$PROG --overridedevice=rtlwmbus > $TEST/test_output.txt 2>&1 + +if [ "$?" != "0" ] +then + echo "You can only use --overridedevice=xyz with --useconfig=xyz" > $TEST/test_expected.txt + diff $TEST/test_expected.txt $TEST/test_output.txt + if [ "$?" = "0" ] + then + echo "OK: $TESTNAME" + TESTRESULT="OK" + fi +fi + +if [ "$TESTRESULT" = "ERROR" ]; then echo ERROR: $TESTNAME; exit 1; fi + + +TESTNAME="Test cmdline --overridedevice with --useconfig and surplus argument" +TESTRESULT="ERROR" + +$PROG --overridedevice=rtlwmbus --useconfig=tests/config12 gurka > $TEST/test_output.txt 2>&1 + +if [ "$?" != "0" ] +then + echo "Usage error: too many arguments \"gurka\" with --useconfig=..." > $TEST/test_expected.txt + diff $TEST/test_expected.txt $TEST/test_output.txt + if [ "$?" = "0" ] + then + echo "OK: $TESTNAME" + TESTRESULT="OK" + fi +fi + +if [ "$TESTRESULT" = "ERROR" ]; then echo ERROR: $TESTNAME; exit 1; fi + +TESTNAME="Test cmdline --overridedevice with --useconfig and surplus arguments" +TESTRESULT="ERROR" + +$PROG --overridedevice=rtlwmbus --useconfig=tests/config12 gurka banan > $TEST/test_output.txt 2>&1 + +if [ "$?" != "0" ] +then + echo "Usage error: too many arguments \"gurka\" with --useconfig=..." > $TEST/test_expected.txt + diff $TEST/test_expected.txt $TEST/test_output.txt + if [ "$?" = "0" ] + then + echo "OK: $TESTNAME" + TESTRESULT="OK" + fi +fi + +if [ "$TESTRESULT" = "ERROR" ]; then echo ERROR: $TESTNAME; exit 1; fi + +TESTNAME="Test cmdline --overridedevice with --useconfig and surplus argument prefixed" +TESTRESULT="ERROR" + +$PROG banan --overridedevice=rtlwmbus --useconfig=tests/config12 > $TEST/test_output.txt 2>&1 + +if [ "$?" != "0" ] +then + echo "Usage error: too many arguments \"banan\" with --useconfig=..." > $TEST/test_expected.txt + diff $TEST/test_expected.txt $TEST/test_output.txt + if [ "$?" = "0" ] + then + echo "OK: $TESTNAME" + TESTRESULT="OK" + fi +fi + +if [ "$TESTRESULT" = "ERROR" ]; then echo ERROR: $TESTNAME; exit 1; fi + +TESTNAME="Test cmdline --overridedevice with --useconfig and surplus argument infixed" +TESTRESULT="ERROR" + +$PROG --overridedevice=rtlwmbus apple --useconfig=tests/config12 > $TEST/test_output.txt 2>&1 + +if [ "$?" != "0" ] +then + echo "Usage error: too many arguments \"apple\" with --useconfig=..." > $TEST/test_expected.txt + diff $TEST/test_expected.txt $TEST/test_output.txt + if [ "$?" = "0" ] + then + echo "OK: $TESTNAME" + TESTRESULT="OK" + fi +fi + +if [ "$TESTRESULT" = "ERROR" ]; then echo ERROR: $TESTNAME; exit 1; fi