From 22d6880385a5b1c35aea43833b263e403e5bfe2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Mon, 6 Nov 2023 21:04:40 +0100 Subject: [PATCH] Add test for non-existant driver. --- src/driver_apator08.cc | 2 ++ src/meters.cc | 7 ++++++- test.sh | 3 +++ tests/test_non_existant_driver.sh | 22 ++++++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100755 tests/test_non_existant_driver.sh diff --git a/src/driver_apator08.cc b/src/driver_apator08.cc index 58cd309..9e15d0f 100644 --- a/src/driver_apator08.cc +++ b/src/driver_apator08.cc @@ -56,6 +56,8 @@ namespace vector content; t->extractPayload(&content); + if (content.size() < 4) return; + map> vendor_values; string total; diff --git a/src/meters.cc b/src/meters.cc index 11bccec..b4e810a 100644 --- a/src/meters.cc +++ b/src/meters.cc @@ -225,7 +225,12 @@ bool lookupDriverInfo(const string& driver_name, DriverInfo *out_di) DriverInfo *di = lookupDriver(driver_name); if (di == NULL) { - // Ok, not built in, try to load it from file. + if (!endsWith(driver_name, ".xmq") || !checkFileExists(driver_name.c_str())) + { + return false; + } + + // Ok, not built in, but it ends with .xmq and the file exists! string new_name = loadDriver(driver_name); // Check again if it was registered. diff --git a/test.sh b/test.sh index 006f60c..6681cd5 100755 --- a/test.sh +++ b/test.sh @@ -30,6 +30,9 @@ if [ "$?" != "0" ]; then RC="1"; fi tests/test_s1_meters.sh $PROG if [ "$?" != "0" ]; then RC="1"; fi +tests/test_non_existant_driver.sh $PROG +if [ "$?" != "0" ]; then RC="1"; fi + tests/test_mbus.sh $PROG if [ "$?" != "0" ]; then RC="1"; fi diff --git a/tests/test_non_existant_driver.sh b/tests/test_non_existant_driver.sh new file mode 100755 index 0000000..a8b40b5 --- /dev/null +++ b/tests/test_non_existant_driver.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +PROG="$1" + +rm -rf testoutput +mkdir -p testoutput + +TEST=testoutput + +$PROG 434493157856341233038c2075900f002c25b30a000021924d4f2fb66e017a75002007109058475f4bc91df878b80a1b0f98b629024aac727942bfc549233c0140829b93 MyTapWater q40 12345678 NOKEY > $TEST/test_output.txt 2>&1 + +EXPECT='Not a valid meter driver "q40"' +RES=$(cat $TEST/test_output.txt | grep -o "$EXPECT" | tail -n 1) + +if [ "$RES" = "$EXPECT" ] +then + echo OK: Test non-existant driver +else + cat $TEST/test_output.txt + echo ERROR Failed non-existant driver check! + exit 1 +fi