From 933c0f1760459483f7f1406ca17fb13abcf74fd4 Mon Sep 17 00:00:00 2001 From: Omar Kallel Date: Mon, 4 Oct 2021 17:38:57 +0100 Subject: [PATCH] Ticket refs #6165: icwmp: uci unit tests --- gitlab-ci/unit-test.sh | 5 +- test/cmocka/Makefile | 12 +- test/cmocka/icwmp_uci_unit_test.c | 222 ++++++++++++++++++++++++++++++ test/files/var/state/cwmp | 2 + 4 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 test/cmocka/icwmp_uci_unit_test.c create mode 100644 test/files/var/state/cwmp diff --git a/gitlab-ci/unit-test.sh b/gitlab-ci/unit-test.sh index 1fe0602..afe5162 100755 --- a/gitlab-ci/unit-test.sh +++ b/gitlab-ci/unit-test.sh @@ -19,7 +19,10 @@ exec_cmd cp /builds/iopsys/icwmp/invalid_firmware_v1.0.bin /var/www/html echo "Install Inform json files" exec_cmd mkdir -p /etc/icwmpd -exec_cmd cp test/files/etc/icwmpd/* /etc/icwmpd +exec_cmd cp /builds/iopsys/icwmp/test/files/etc/icwmpd/* /etc/icwmpd + +exec_cmd mkdir -p /var/state +exec_cmd cp /builds/iopsys/icwmp/test/files/var/state/cwmp /var/state echo "Starting dependent services" supervisorctl status all diff --git a/test/cmocka/Makefile b/test/cmocka/Makefile index 4212333..f9c8527 100644 --- a/test/cmocka/Makefile +++ b/test/cmocka/Makefile @@ -6,7 +6,8 @@ CFLAGS = -g -Wall -I./inc LDFLAGS = -lmicroxml -luci -lblobmsg_json -lubox -ljson-c -lubus -lpthread -lcurl -lssl -lcrypto LIBCFLAGS = -g -Wall -fPIC -c -I../../inc LIBOBJ = libobj -UNIT_TESTS = icwmp_datamodel_interface_unit_testd icwmp_soap_msg_unit_testd icwmp_backup_session_unit_testd icwmp_download_unit_testd icwmp_notifications_unit_testd icwmp_cli_unit_testd +UNIT_TESTS = icwmp_datamodel_interface_unit_testd icwmp_soap_msg_unit_testd icwmp_backup_session_unit_testd icwmp_download_unit_testd icwmp_custom_inform_parameters_unit_testd icwmp_notifications_unit_testd icwmp_uci_unit_testd icwmp_cli_unit_testd + VALGRIND = valgrind --xml=yes --xml-file=/builds/iopsys/icwmp/memory-report.xml --leak-check=full --show-reachable=yes --show-leak-kinds=all --errors-for-leak-kinds=all libobj: @@ -47,11 +48,18 @@ icwmp_notifications_unit_testd: icwmp_notifications_unit_test.o icwmp_cli_unit_testd: icwmp_cli_unit_test.o $(CC) -o $@ $^ -lcmocka -licwmp $(LDFLAGS) $(CFLAGS) - + +icwmp_uci_unit_testd: icwmp_uci_unit_test.o + $(CC) -o $@ $^ -lcmocka -licwmp $(LDFLAGS) $(CFLAGS) + +icwmp_custom_inform_parameters_unit_testd: icwmp_custom_inform_parameters_unit_test.o + $(CC) -o $@ $^ -lcmocka -licwmp $(LDFLAGS) $(CFLAGS) + unit-test: $(UNIT_TESTS) for testprog in $(UNIT_TESTS); do \ sudo $(VALGRIND) ./$$testprog; \ cp ../files/etc/config/* /etc/config; \ + cp ../files/var/state/* /var/state; \ done .PHONY: clean diff --git a/test/cmocka/icwmp_uci_unit_test.c b/test/cmocka/icwmp_uci_unit_test.c new file mode 100644 index 0000000..5eeb708 --- /dev/null +++ b/test/cmocka/icwmp_uci_unit_test.c @@ -0,0 +1,222 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Copyright (C) 2013-2020 iopsys Software Solutions AB + * Author Omar Kallel + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define UCI_WRONG_PATH "cwmp.wrong_section.wrong_option" +struct uci_list *list = NULL; + +static int cwmp_uci_unit_tests_init(void **state) +{ + cwmp_uci_init(); + return 0; +} + +static int cwmp_uci_unit_tests_clean(void **state) +{ + icwmp_cleanmem(); + cwmp_uci_exit(); + if (list != NULL) + cwmp_free_uci_list(list); + return 0; +} + +static void cwmp_uci_get_tests(void **state) +{ + + char *value = NULL; + int error; + + error = uci_get_value(UCI_ACS_USERID_PATH, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "iopsys"); + + error = uci_get_value(UCI_WRONG_PATH, &value); + assert_int_equal(error, UCI_OK); + assert_null(value); + + error = uci_get_state_value(UCI_DHCP_ACS_URL, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "http://192.168.103.160:8080/openacs/acs"); + + error = uci_get_state_value(UCI_WRONG_PATH, &value); + assert_int_equal(error, UCI_OK); + assert_null(value); + + value = cwmp_db_get_value_string("device", "deviceinfo", "ProductClass"); + assert_string_equal(value, "FirstClass"); + + + value = cwmp_db_get_value_string("device", "deviceinfo", "wrong_option"); + assert_string_equal(value, ""); + + error = cwmp_uci_get_option_value_string("cwmp", "acs", "dhcp_url", UCI_VARSTATE_CONFIG, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "http://192.168.103.160:8080/openacs/acs"); + + error = cwmp_uci_get_option_value_string("cwmp", "cpe", "userid", UCI_STANDARD_CONFIG, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "iopsys"); + + error = cwmp_uci_get_option_value_string("cwmp", "wrong_section", "wrong_option", UCI_STANDARD_CONFIG, &value); + assert_int_equal(error, UCI_ERR_NOTFOUND); + assert_null(value); + + struct uci_section *s = get_section_by_section_name("cwmp", "acs", "acs", UCI_STANDARD_CONFIG); + assert_non_null(s); + + error = cwmp_uci_get_value_by_section_string(s, "passwd", &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "iopsys"); + + error = cwmp_uci_get_value_by_section_string(s, "wrong_option", &value); + assert_null(value); + assert_int_equal(error, UCI_ERR_NOTFOUND); +} + +static void cwmp_uci_set_tests(void **state) +{ + int error = UCI_OK; + char *value = NULL; + cwmp_uci_set_value("cwmp", "cpe", "exec_download", "1"); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_string("cwmp", "cpe", "exec_download", UCI_STANDARD_CONFIG, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "1"); + value = NULL; + + error = cwmp_uci_set_varstate_value("cwmp", "acs", "varstatopt", "varstatval"); + assert_int_equal(error, UCI_OK); + cwmp_commit_package("cwmp", UCI_VARSTATE_CONFIG); + error = cwmp_uci_get_option_value_string("cwmp", "acs", "varstatopt", UCI_VARSTATE_CONFIG, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "varstatval"); + value = NULL; + + error = cwmp_uci_set_value("cwmp", "wrong_section", "wrong_option", "wrong_value"); + assert_int_equal(error, UCI_ERR_NOTFOUND); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_string("cwmp", "wront_section", "wrong_option", UCI_VARSTATE_CONFIG, &value); + assert_int_equal(error, UCI_ERR_NOTFOUND); + assert_null(value); + value = NULL; + + cwmp_uci_set_value_by_path("cwmp.cpe.userid", "usertest"); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_string("cwmp", "cpe", "userid", UCI_STANDARD_CONFIG, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "usertest"); + value = NULL; + + cwmp_uci_set_varstate_value_by_path("cwmp.acs.opt1", "varstatval1"); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_string("cwmp", "acs", "opt1", UCI_VARSTATE_CONFIG, &value); + assert_int_equal(error, UCI_OK); + assert_string_equal(value, "varstatval1"); + value = NULL; + + error = cwmp_uci_set_value_by_path("cwmp.wront_section.wrong_option", "wrong_value"); + assert_int_equal(error, UCI_ERR_NOTFOUND); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_string("cwmp", "wront_section", "wrong_option", UCI_VARSTATE_CONFIG, &value); + assert_int_equal(error, UCI_ERR_NOTFOUND); + assert_null(value); + + error = cwmp_uci_set_varstate_value_by_path("cwmp.wront_section.wrong_option", "wrong_value"); + assert_int_equal(error, UCI_ERR_NOTFOUND); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_string("cwmp", "wront_section", "wrong_option", UCI_VARSTATE_CONFIG, &value); + assert_int_equal(error, UCI_ERR_NOTFOUND); + assert_null(value); +} + +static void cwmp_uci_add_tests(void **state) +{ + struct uci_section *s = NULL; + int error = UCI_OK; + + error = cwmp_uci_add_section("cwmp", "acs", UCI_STANDARD_CONFIG, &s); + assert_non_null(s); + assert_int_equal(error, UCI_OK); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + assert_int_equal(strncmp(section_name(s), "cfg", 3), 0); + s = NULL; + + error = cwmp_uci_add_section("new_package", "new_section", UCI_STANDARD_CONFIG, &s); + assert_non_null(s); + assert_int_equal(error, UCI_OK); + cwmp_commit_package("new_package", UCI_STANDARD_CONFIG); + assert_int_equal(strncmp(section_name(s), "cfg", 3), 0); + s = NULL; + + error = cwmp_uci_add_section_with_specific_name("cwmp", "acs", "new_acs", UCI_STANDARD_CONFIG); + assert_int_equal(error, UCI_OK); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + + error = cwmp_uci_add_section_with_specific_name("cwmp", "acs", "new_acs", UCI_STANDARD_CONFIG); + assert_int_equal(error, UCI_ERR_DUPLICATE); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + +} + +static void cwmp_uci_list_tests(void **state) +{ + struct uci_section *s = NULL; + int error = UCI_OK; + char *list_string = NULL; + + error = cwmp_uci_add_list_value("cwmp", "cpe", "optionlist", "val1", UCI_STANDARD_CONFIG); + assert_int_equal(error, UCI_OK); + error = cwmp_uci_add_list_value("cwmp", "cpe", "optionlist", "val2", UCI_STANDARD_CONFIG); + assert_int_equal(error, UCI_OK); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_list("cwmp", "cpe", "optionlist", &list); + assert_int_equal(error, UCI_TYPE_LIST); + list_string = cwmp_uci_list_to_string(list, ","); + assert_non_null(list_string); + assert_string_equal(list_string, "val1,val2"); + list_string = NULL; + if(list != NULL) + cwmp_free_uci_list(list); + + error = cwmp_uci_add_list_value("cwmp", "wrong_section", "optionlist", "val1", UCI_STANDARD_CONFIG); + assert_int_equal(error, UCI_ERR_INVAL); + error = cwmp_uci_add_list_value("cwmp", "wrong_section", "optionlist", "val2", UCI_STANDARD_CONFIG); + assert_int_equal(error, UCI_ERR_INVAL); + cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG); + error = cwmp_uci_get_option_value_list("cwmp", "wrong_section", "optionlist", &list); + assert_int_equal(error, UCI_ERR_NOTFOUND); + assert_null(list); + list_string = cwmp_uci_list_to_string(list, ","); + assert_null(list_string); + if(list != NULL) + cwmp_free_uci_list(list); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(cwmp_uci_get_tests), + cmocka_unit_test(cwmp_uci_set_tests), + cmocka_unit_test(cwmp_uci_add_tests), + cmocka_unit_test(cwmp_uci_list_tests) + }; + + return cmocka_run_group_tests(tests, cwmp_uci_unit_tests_init, cwmp_uci_unit_tests_clean); +} diff --git a/test/files/var/state/cwmp b/test/files/var/state/cwmp new file mode 100644 index 0000000..4509d0d --- /dev/null +++ b/test/files/var/state/cwmp @@ -0,0 +1,2 @@ +config acs 'acs' + option dhcp_url 'http://192.168.103.160:8080/openacs/acs' \ No newline at end of file