From cc21f2c79f8ea94bf88be4e4edb8f6739aa4f77d Mon Sep 17 00:00:00 2001 From: Damir Franusic Date: Tue, 10 Oct 2023 15:13:01 +0200 Subject: [PATCH] feat(tests): check MQTT binary file auto-cleanup --- test/check_mqtt.c | 28 +++++++++++++++++++++++++++- test/mqtt_test_04.lua | 13 +++++++++++-- test/test_bin_upload.sh | 3 +++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/test/check_mqtt.c b/test/check_mqtt.c index cd287bd..c531f04 100644 --- a/test/check_mqtt.c +++ b/test/check_mqtt.c @@ -218,9 +218,26 @@ mqtt_test_04(void **state) assert_int_equal(r, 0); assert_non_null(b); // file uuid returned - if (strlen(b) != 36) { + if (strlen(b) != 73) { fail(); } + + // tokenize + char *valid_uuid = NULL; + char *incomplete_uuid = NULL; + char *token = NULL; + char *saveptr = NULL; + + // get valid uuid (all chunks) + token = strtok_r(b, ":", &saveptr); + assert_non_null(token); + valid_uuid = strdup(token); + + // get incomplete uuid (only first chunk) + token = strtok_r(NULL, ":", &saveptr); + assert_non_null(token); + incomplete_uuid = strdup(token); + // wait for data sleep(7); @@ -243,8 +260,17 @@ mqtt_test_04(void **state) } fclose(f); assert_int_equal(fsz, 1024); + + // incomplete file should have been removed + sleep(5); + strcpy(&fp[strlen(fp) - 36], incomplete_uuid); + FILE *f2 = fopen(fp, "r"); + assert_null(f2); + // cleanup free(b); + free(valid_uuid); + free(incomplete_uuid); } int diff --git a/test/mqtt_test_04.lua b/test/mqtt_test_04.lua index 38396af..18bad4e 100644 --- a/test/mqtt_test_04.lua +++ b/test/mqtt_test_04.lua @@ -1,11 +1,20 @@ -cmd = { +local cmd = { "CMD_MQTT_BINARY_UPLOAD", "mqtt_local", 1024 } + +local cmd2 = { + "CMD_MQTT_BINARY_UPLOAD", + "mqtt_local", + 1024 +} + -- run local d = M.cmd_call(cmd) +local d2 = M.cmd_call(cmd) -- notify upload script to start -- sending 256 byte chunks os.execute("echo \"" .. d[3].file_uuid .. "\" > /tmp/tmp_file_uuid") -return d[3].file_uuid +os.execute("echo \"" .. d2[3].file_uuid .. "\" > /tmp/tmp_file_uuid_timeout") +return d[3].file_uuid .. ":" .. d2[3].file_uuid diff --git a/test/test_bin_upload.sh b/test/test_bin_upload.sh index bb6567c..b3c9d2f 100755 --- a/test/test_bin_upload.sh +++ b/test/test_bin_upload.sh @@ -26,6 +26,9 @@ do # chunk 03 echo "Uploading chunk 03" mosquitto_pub -h 127.0.0.1 -t "mink.bin/DEBUG_UUID/$UUID/upload" -f /tmp/xad -u user -P password + # one chunk for the file upload which is supposed to timeout + UUID2=$(cat /tmp/tmp_file_uuid_timeout) + mosquitto_pub -h 127.0.0.1 -t "mink.bin/DEBUG_UUID/$UUID2/upload" -f /tmp/xad -u user -P password sleep 2 CSUM2=$(sha256sum -z /tmp/mink.bin/$UUID|cut -d' ' -f1) if [ $CSUM1 != $CSUM2 ]; then