]> git.meshlink.io Git - meshlink/blobdiff - test/blackbox/run_blackbox_tests/test_cases_import.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_import.c
diff --git a/test/blackbox/run_blackbox_tests/test_cases_import.c b/test/blackbox/run_blackbox_tests/test_cases_import.c
new file mode 100644 (file)
index 0000000..0ab6c04
--- /dev/null
@@ -0,0 +1,316 @@
+/*
+    test_cases_import.c -- Execution of specific meshlink black box test cases
+    Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
+
+    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.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "execute_tests.h"
+#include "test_cases_import.h"
+#include "../common/containers.h"
+#include "../common/test_step.h"
+#include "../common/common_handlers.h"
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+/* Modify this to change the logging level of Meshlink */
+#define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
+
+static void test_case_import_01(void **state);
+static bool test_import_01(void);
+static void test_case_import_02(void **state);
+static bool test_import_02(void);
+static void test_case_import_03(void **state);
+static bool test_import_03(void);
+static void test_case_import_04(void **state);
+static bool test_import_04(void);
+static void test_case_import_05(void **state);
+static bool test_import_05(void);
+static void test_case_import_06(void **state);
+static bool test_import_06(void);
+
+/* State structure for import API Test Case #1 */
+static black_box_state_t test_case_import_01_state = {
+       .test_case_name = "test_case_import_01",
+};
+/* State structure for import API Test Case #2 */
+static black_box_state_t test_case_import_02_state = {
+       .test_case_name = "test_case_import_02",
+};
+/* State structure for import API Test Case #3 */
+static black_box_state_t test_case_import_03_state = {
+       .test_case_name = "test_case_import_03",
+};
+/* State structure for import API Test Case #4 */
+static black_box_state_t test_case_import_04_state = {
+       .test_case_name = "test_case_import_04",
+};
+/* State structure for import API Test Case #5 */
+static black_box_state_t test_case_import_05_state = {
+       .test_case_name = "test_case_import_05",
+};
+/* State structure for import API Test Case #6 */
+static black_box_state_t test_case_import_06_state = {
+       .test_case_name = "test_case_import_06",
+};
+
+
+/* Execute import Test Case # 1 - valid case*/
+static void test_case_import_01(void **state) {
+       execute_test(test_import_01, state);
+}
+/* Test Steps for meshlink_import Test Case # 1 - Valid case
+
+    Test Steps:
+    1. Open NUT(Node Under Test) & bar meshes.
+    2. Export and Import mutually
+
+    Expected Result:
+    Both the nodes imports successfully
+*/
+static bool test_import_01(void) {
+       meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+
+       // Opening NUT and bar nodes
+       meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "test", DEV_CLASS_STATIONARY);
+       assert(mesh1 != NULL);
+       meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "test", DEV_CLASS_STATIONARY);
+       assert(mesh2 != NULL);
+       meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+
+       // Exporting and Importing mutually
+       char *exp1 = meshlink_export(mesh1);
+       assert(exp1 != NULL);
+       char *exp2 = meshlink_export(mesh2);
+       assert(exp2 != NULL);
+       bool imp1 = meshlink_import(mesh1, exp2);
+       bool imp2 = meshlink_import(mesh2, exp1);
+
+       assert_int_equal(imp1 && imp2, true);
+
+       meshlink_close(mesh1);
+       meshlink_close(mesh2);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+       return imp1 && imp2;
+}
+
+/* Execute import Test Case # 2 - invalid case*/
+static void test_case_import_02(void **state) {
+       execute_test(test_import_02, state);
+}
+/* Test Steps for meshlink_import Test Case # 2 - Invalid case
+
+    Test Steps:
+    1. Open NUT(Node Under Test) & bar meshes.
+    2. Passing NULL as mesh handle argument for meshlink_import API
+
+    Expected Result:
+    Reports error successfully by returning false
+*/
+static bool test_import_02(void) {
+       meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+
+       // Opening NUT and bar nodes
+       meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "test", DEV_CLASS_STATIONARY);
+       assert(mesh1 != NULL);
+       meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "test", DEV_CLASS_STATIONARY);
+       assert(mesh2 != NULL);
+       meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+
+       // Exporting & Importing nodes
+       char *exp1 = meshlink_export(mesh1);
+       assert(exp1 != NULL);
+       char *exp2 = meshlink_export(mesh2);
+       assert(exp2 != NULL);
+
+       bool imp1 = meshlink_import(NULL, exp2);
+       bool imp2 = meshlink_import(mesh2, exp1);
+       assert_int_equal((!imp1) && imp2, true);
+
+       meshlink_close(mesh1);
+       meshlink_close(mesh2);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+       return true;
+}
+
+
+/* Execute import Test Case # 3 - invalid case*/
+static void test_case_import_03(void **state) {
+       execute_test(test_import_03, state);
+}
+/* Test Steps for meshlink_import Test Case # 3 - Invalid case
+
+    Test Steps:
+    1. Open NUT(Node Under Test) & bar meshes.
+    2. Passing NULL as exported data argument for meshlink_import API
+
+    Expected Result:
+    Reports error successfully by returning false
+*/
+static bool test_import_03(void) {
+       meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+
+       /* Opening NUT and bar nodes */
+       meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "chat", DEV_CLASS_STATIONARY);
+       assert(mesh1 != NULL);
+       meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "chat", DEV_CLASS_STATIONARY);
+       assert(mesh2 != NULL);
+       meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+
+       /* Exporting & Importing nodes */
+       char *exp1 = meshlink_export(mesh1);
+       assert(exp1 != NULL);
+       char *exp2 = meshlink_export(mesh2);
+       assert(exp2 != NULL);
+
+       bool imp1 = meshlink_import(mesh1, NULL);
+       bool imp2 = meshlink_import(mesh2, exp1);
+
+       assert_int_equal((!imp1) && imp2, true);
+
+       meshlink_close(mesh1);
+       meshlink_close(mesh2);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+       return true;
+}
+
+/* Execute import Test Case # 4 - invalid case garbage string*/
+static void test_case_import_04(void **state) {
+       execute_test(test_import_04, state);
+}
+/* Test Steps for meshlink_import Test Case # 4 - Invalid case
+
+    Test Steps:
+    1. Open NUT(Node Under Test) & bar meshes.
+    2. Passing some garbage string(NULL terminated)
+        as an argument for meshlink_import API
+
+    Expected Result:
+    Reports error successfully by returning false
+*/
+static bool test_import_04(void) {
+       meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+
+       // Opening NUT and bar nodes
+       meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "chat", DEV_CLASS_STATIONARY);
+       assert(mesh1 != NULL);
+       meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "chat", DEV_CLASS_STATIONARY);
+       assert(mesh2 != NULL);
+       meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+
+       // Exporting & Importing nodes
+       char *exp1 = meshlink_export(mesh1);
+       assert(exp1 != NULL);
+       char *exp2 = meshlink_export(mesh2);
+       assert(exp2 != NULL);
+
+       // Importing NUT with garbage string as exported data argument
+       bool imp1 = meshlink_import(mesh1, "1/2/3");
+       bool imp2 = meshlink_import(mesh2, exp1);
+       assert_int_equal((!imp1) && imp2, true);
+
+       meshlink_close(mesh1);
+       meshlink_close(mesh2);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+       return true;
+}
+
+/* Execute import Test Case # 5 - valid case*/
+static void test_case_import_05(void **state) {
+       execute_test(test_import_05, state);
+}
+/* Test Steps for meshlink_import Test Case # 5 - Invalid case
+
+    Test Steps:
+    1. Open NUT(Node Under Test) & bar meshes.
+    2. Export and Import mutually
+    2. Try to import NUT again/twice at 'bar' node
+
+    Expected Result:
+    Reports error successfully by returning false
+*/
+static bool test_import_05(void) {
+       meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+
+       /* Opening NUT and bar nodes */
+       meshlink_handle_t *mesh1 = meshlink_open("importconf1", "nut", "chat", DEV_CLASS_STATIONARY);
+       assert(mesh1 != NULL);
+       meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+       meshlink_handle_t *mesh2 = meshlink_open("importconf2", "bar", "chat", DEV_CLASS_STATIONARY);
+       assert(mesh2 != NULL);
+       meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
+
+       /* Exporting  & Importing nodes */
+       char *exp1 = meshlink_export(mesh1);
+       assert(exp1 != NULL);
+       char *exp2 = meshlink_export(mesh2);
+       assert(exp2 != NULL);
+       bool imp1 = meshlink_import(mesh1, exp2);
+       assert(imp1);
+       bool imp2 = meshlink_import(mesh2, exp1);
+       assert(imp2);
+
+       /** Trying to import twice **/
+       bool imp3 = meshlink_import(mesh2, exp1);
+
+       assert_int_equal(imp3, false);
+
+       meshlink_close(mesh1);
+       meshlink_close(mesh2);
+       meshlink_destroy("importconf1");
+       meshlink_destroy("importconf2");
+       return true;
+}
+
+int test_meshlink_import(void) {
+       const struct CMUnitTest blackbox_import_tests[] = {
+               cmocka_unit_test_prestate_setup_teardown(test_case_import_01, NULL, NULL,
+                               (void *)&test_case_import_01_state),
+               cmocka_unit_test_prestate_setup_teardown(test_case_import_02, NULL, NULL,
+                               (void *)&test_case_import_02_state),
+               cmocka_unit_test_prestate_setup_teardown(test_case_import_03, NULL, NULL,
+                               (void *)&test_case_import_03_state),
+               cmocka_unit_test_prestate_setup_teardown(test_case_import_04, NULL, NULL,
+                               (void *)&test_case_import_04_state),
+               cmocka_unit_test_prestate_setup_teardown(test_case_import_05, NULL, NULL,
+                               (void *)&test_case_import_05_state)
+       };
+       total_tests += sizeof(blackbox_import_tests) / sizeof(blackbox_import_tests[0]);
+
+       return cmocka_run_group_tests(blackbox_import_tests, NULL, NULL);
+}