summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/tests/libspid/utests/src/vlan_port_utests.c
diff options
context:
space:
mode:
Diffstat (limited to 'cleopatre/devkit/tests/libspid/utests/src/vlan_port_utests.c')
-rw-r--r--cleopatre/devkit/tests/libspid/utests/src/vlan_port_utests.c362
1 files changed, 362 insertions, 0 deletions
diff --git a/cleopatre/devkit/tests/libspid/utests/src/vlan_port_utests.c b/cleopatre/devkit/tests/libspid/utests/src/vlan_port_utests.c
new file mode 100644
index 0000000000..ad5d2d78df
--- /dev/null
+++ b/cleopatre/devkit/tests/libspid/utests/src/vlan_port_utests.c
@@ -0,0 +1,362 @@
+/* Cleopatre project {{{
+ *
+ * Copyright (C) 2012 MStar
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file vlan_port_utests.c
+ * \brief Unitary tests for libspid
+ * \ingroup Cleopatre - libspid
+ *
+ * This file content all the unitary tests for EoC SPiDLib.
+ */
+
+#include <check.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <linux/if_tun.h>
+#include <linux/if_ether.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <net/if.h>
+#include <libgen.h> /* for basename() */
+#include <string.h>
+
+#include "eoc_utests.h"
+#include "libspid.h"
+
+/* local constants */
+#define TEST_VLAN_PORT_CONF "testfiles/vlanport.conf"
+#define ETC_VLAN_PORT_CONF "/etc/vlanport.conf"
+#define VLAN_PORT_NO_VLANID "testfiles/vlanport_no_vlanid.conf"
+#define VLAN_PORT_LINE2 "testfiles/vlan_port_line2.conf"
+#define VLAN_PORT_LINE7 "testfiles/vlan_port_line7.conf"
+#define VLAN_PORT_ERROR1 "testfiles/vlan_port_error1.conf"
+#define VLAN_PORT_ERROR2 "testfiles/vlan_port_error2.conf"
+#define VLAN_PORT_ERROR3 "testfiles/vlan_port_error3.conf"
+#define WHITE_LIST_CONF "testfiles/white_list.conf"
+#define VLAN_CONF "testfiles/vlan_table.conf"
+
+
+
+void
+test_create_test_file_2 ()
+{
+ system ("cp " VLAN_PORT_LINE2 " " LIBSPID_EOC_PORT_V2_FILE);
+ system ("cp " WHITE_LIST_CONF " " UTESTS_TMP_DIR "/etc");
+ system ("cp " VLAN_CONF " " LIBSPID_VLAN_TABLE_CONF_PATH);
+}
+
+void
+test_create_test_file_7 ()
+{
+ system ("cp " VLAN_PORT_LINE7 " " LIBSPID_EOC_PORT_V2_FILE);
+ system ("cp " WHITE_LIST_CONF " " UTESTS_TMP_DIR "/etc");
+ system ("cp " VLAN_CONF " " LIBSPID_VLAN_TABLE_CONF_PATH);
+}
+
+void
+table_print (libspid_eoc_vlan_port_table_entry_t * entry)
+{
+ fprintf (stderr, "%s %s %s %s %s %s\r\n",
+ entry->vlan_port_mac,
+ entry->vlan_port_index,
+ entry->vlan_port_pvid,
+ entry->vlan_port_tpid,
+ entry->vlan_port_prio, entry->vlan_port_mode);
+}
+
+static void
+setup (void)
+{
+ int ret;
+ struct stat st;
+ char buf[256];
+
+ /* initialize - delete testing dir if it exists */
+ if ((stat (UTESTS_TMP_DIR, &st) == 0))
+ {
+ system ("rm -r " UTESTS_TMP_DIR);
+ }
+
+ ret = mkdir (UTESTS_TMP_DIR, 0770);
+
+ if (ret < 0 && errno != EEXIST)
+ {
+ perror ("mkdir(UTESTS_TMP_DIR, 0770)");
+ exit (EXIT_FAILURE);
+ }
+
+ /* create other subdirs */
+ sprintf (buf, "%s/etc", UTESTS_TMP_DIR);
+ ret = mkdir (buf, 0770);
+
+ if (ret < 0 && errno != EEXIST)
+ {
+ perror ("mkdir(UTESTS_TMP_DIR/etc, 0770)");
+ exit (EXIT_FAILURE);
+ }
+}
+
+void
+teardown (void)
+{
+}
+
+START_TEST (test_libspid_eoc_vlan_port_get)
+{
+ int ret;
+ char index[2];
+ char mac[LIBSPID_MAC_STR_LEN];
+ libspid_eoc_vlan_port_table_entry_t entry;
+
+ strcpy (index, "1");
+
+ ret = libspid_eoc_vlan_port_get (0, index, &entry);
+ fail_if (LIBSPID_SUCCESS == ret, "param 1 is 0,but return suc");
+
+ strcpy (mac, "00:13:d7:00:00:01");
+ ret = libspid_eoc_vlan_port_get (mac, 0, &entry);
+ fail_if (LIBSPID_SUCCESS == ret, "param 2 is 0,but return suc");
+
+ ret = libspid_eoc_vlan_port_get (mac, index, 0);
+ fail_if (LIBSPID_SUCCESS == ret, "param 3 is 0,but return suc");
+ test_create_test_file_7 ();
+ strcpy (mac, "00:13:d7:00:00:01");
+
+ strcpy (index, "3");
+ ret = libspid_eoc_vlan_port_get (mac, index, &entry);
+ fail_if (LIBSPID_SUCCESS != ret, "must get it,but not");
+ fail_if (3 != atoi (entry.vlan_port_index), "Get the port index error");
+ fail_if (2 != atoi (entry.vlan_port_pvid), "Get the port pvid error");
+ fail_if (1 != atoi (entry.vlan_port_tpid), "Get the port tvid error");
+ fail_if (2 != atoi (entry.vlan_port_prio), "Get the port prio error");
+ fail_if (1 != atoi (entry.vlan_port_mode), "Get the port mode error");
+
+ strcpy (mac, "00:13:d7:44:00:10");
+ strcpy (index, "3");
+ ret = libspid_eoc_vlan_port_get (mac, index, &entry);
+ fail_if (LIBSPID_SUCCESS == ret, "vaild mac ,but return suc");
+
+ strcpy (mac, "00:13:d7:00:00:02");
+ strcpy (index, "4");
+ ret = libspid_eoc_vlan_port_get (mac, index, &entry);
+ fail_if (LIBSPID_SUCCESS == ret, "vaild index ,but return suc");
+}
+END_TEST
+
+START_TEST (test_libspid_eoc_vlan_port_get_list)
+{
+ int ret;
+ int cnt = LIBSPID_PORT_TOTAL_MAX;
+ static libspid_eoc_vlan_port_table_entry_t entry[LIBSPID_PORT_TOTAL_MAX];
+ ret = libspid_eoc_vlan_port_get_list (0, &cnt);
+ fail_if (LIBSPID_SUCCESS == ret, "param 1 is 0,but return suc");
+
+ ret = libspid_eoc_vlan_port_get_list (entry, 0);
+ fail_if (LIBSPID_SUCCESS == ret, "param 2 is 0,but return suc");
+
+ test_create_test_file_2 ();
+ ret = libspid_eoc_vlan_port_get_list (entry, &cnt);
+ fail_if (LIBSPID_SUCCESS != ret, "Get List must suc,but fail");
+ fail_if (2 != cnt, "There are two entries in file,but return cnt is "
+ "error");
+
+ test_create_test_file_7 ();
+ ret = libspid_eoc_vlan_port_get_list (entry, &cnt);
+ fail_if (LIBSPID_SUCCESS != ret, "Get List must suc,but fail");
+ fail_if (7 != cnt, "There are seven entries in file,but return cnt is"
+ " error");
+ ret = strcasecmp (entry[5].vlan_port_mac, "00:13:d7:00:00:02");
+ fail_if (0 != ret, "Get the mac error");
+ fail_if (0 != atoi (entry[5].vlan_port_index),
+ "Get the port index error");
+ fail_if (2 != atoi (entry[5].vlan_port_pvid),
+ "Get the port pvid error");
+ fail_if (3 != atoi (entry[5].vlan_port_tpid),
+ "Get the port tvid error");
+ fail_if (2 != atoi (entry[5].vlan_port_prio),
+ "Get the port prio error");
+ fail_if (1 != atoi (entry[5].vlan_port_mode),
+ "Get the port mode error");
+}
+END_TEST
+
+START_TEST (test_libspid_eoc_vlan_port_set)
+{
+ int ret;
+ char mac[LIBSPID_MAC_STR_LEN];
+ char index[2];
+
+ strcpy (index, "2");
+ strcpy (mac, "00:13:D7:00:00:20");
+
+ libspid_eoc_vlan_port_table_entry_t entry, temp_entry;
+
+ memset (&entry , 0x0, sizeof (libspid_eoc_vlan_port_table_entry_t));
+ memset (&temp_entry , 0x0, sizeof (libspid_eoc_vlan_port_table_entry_t));
+
+ ret = libspid_eoc_vlan_port_set (0, index, &entry);
+ fail_if (LIBSPID_SUCCESS == ret, "param 1 is zero,but return suc");
+
+ ret = libspid_eoc_vlan_port_set (mac, 0, &entry);
+ fail_if (LIBSPID_SUCCESS == ret, "param 2 is zero,but return suc");
+
+ ret = libspid_eoc_vlan_port_set (mac, index, 0);
+ fail_if (LIBSPID_SUCCESS == ret, "param 3 is zero,but return suc");
+
+ test_create_test_file_7 ();
+
+ strcpy (mac, "00:13:D7:00:00:02");
+ strcpy (index, "1");
+
+ strcpy (entry.vlan_port_pvid, "10");
+ strcpy (entry.vlan_port_tpid, "11");
+ strcpy (entry.vlan_port_prio, "2");
+ strcpy (entry.vlan_port_mode, "99");
+
+ ret = libspid_eoc_vlan_port_set (mac, index, &entry);
+
+ fail_if (LIBSPID_SUCCESS != ret, "must set suc");
+
+ ret = libspid_eoc_vlan_port_get (mac, index, &temp_entry);
+ fail_if (LIBSPID_SUCCESS != ret, "must get suc");
+
+ ret = strcasecmp (mac, temp_entry.vlan_port_mac);
+ fail_if (0 != ret, "mac not right");
+
+ ret = strcasecmp (index, temp_entry.vlan_port_index);
+ fail_if (0 != ret, "index not right");
+
+ ret = strcasecmp (entry.vlan_port_pvid, temp_entry.vlan_port_pvid);
+ fail_if (0 != ret, "vlan_port_pvid not right");
+
+ ret = strcasecmp (entry.vlan_port_tpid, temp_entry.vlan_port_tpid);
+ fail_if (0 != ret, "vlan_port_tpid not right");
+
+ ret = strcasecmp (entry.vlan_port_prio, temp_entry.vlan_port_prio);
+ fail_if (0 != ret, "vlan_port_prio not right");
+
+ ret = strcasecmp (entry.vlan_port_mode, temp_entry.vlan_port_mode);
+ fail_if (0 != ret, "vlan_port_mode not right");
+ strcpy(mac, "00:13:D7:00:00:20");
+ strcpy (index, "1");
+
+ strcpy (entry.vlan_port_pvid, "10");
+ strcpy (entry.vlan_port_tpid, "11");
+ strcpy (entry.vlan_port_prio, "2");
+ strcpy (entry.vlan_port_mode, "99");
+ ret = libspid_eoc_vlan_port_set (mac, index, &entry);
+ fail_if (LIBSPID_SUCCESS == ret,
+ "mac doesn't exist but function return successfu");
+}
+END_TEST
+
+void
+test_create_port_check_test_file_ok_1 ()
+{
+ system ("cp " TEST_VLAN_PORT_CONF " " LIBSPID_EOC_PORT_V2_FILE);
+ system ("cp " WHITE_LIST_CONF " " UTESTS_TMP_DIR "/etc");
+ system ("cp " VLAN_CONF " " LIBSPID_VLAN_TABLE_CONF_PATH);
+
+}
+
+void
+test_create_port_check_test_file_no_vlanid ()
+{
+ system ("cp " VLAN_PORT_NO_VLANID " " LIBSPID_EOC_PORT_V2_FILE);
+ system ("cp " WHITE_LIST_CONF " " UTESTS_TMP_DIR "/etc");
+ system ("cp " VLAN_CONF " " LIBSPID_VLAN_TABLE_CONF_PATH);
+
+}
+
+void
+test_create_port_check_test_file_error_1 ()
+{
+ system ("cp " VLAN_PORT_ERROR1 " " LIBSPID_EOC_PORT_V2_FILE);
+ system ("cp " WHITE_LIST_CONF " " UTESTS_TMP_DIR "/etc");
+ system ("cp " VLAN_CONF " " LIBSPID_VLAN_TABLE_CONF_PATH);
+}
+
+void
+test_create_port_check_test_file_error_2 ()
+{
+ system ("cp " VLAN_PORT_ERROR2 " " LIBSPID_EOC_PORT_V2_FILE);
+ system ("cp " WHITE_LIST_CONF " " UTESTS_TMP_DIR "/etc");
+ system ("cp " VLAN_CONF " " LIBSPID_VLAN_TABLE_CONF_PATH);
+}
+
+void
+test_create_port_check_test_file_error_3 ()
+{
+ system ("cp " VLAN_PORT_ERROR3 " " LIBSPID_EOC_PORT_V2_FILE);
+ system ("cp " WHITE_LIST_CONF " " UTESTS_TMP_DIR "/etc");
+ system ("cp " VLAN_CONF " " LIBSPID_VLAN_TABLE_CONF_PATH);
+}
+
+START_TEST (test_libspid_eoc_vlan_port_check)
+{
+ int ret;
+ test_create_port_check_test_file_ok_1 ();
+ ret = libspid_eoc_vlan_port_check ();
+ fail_if (LIBSPID_SUCCESS != ret, "must right");
+
+ test_create_port_check_test_file_no_vlanid ();
+ ret = libspid_eoc_vlan_port_check ();
+ fail_if (LIBSPID_SUCCESS == ret, "must error,no vlanid");
+
+ test_create_port_check_test_file_error_1 ();
+ ret = libspid_eoc_vlan_port_check ();
+ fail_if (LIBSPID_SUCCESS == ret, "must error");
+
+ test_create_port_check_test_file_error_2 ();
+ ret = libspid_eoc_vlan_port_check ();
+ fail_if (LIBSPID_SUCCESS == ret, "must error");
+
+ test_create_port_check_test_file_error_3 ();
+ ret = libspid_eoc_vlan_port_check ();
+ fail_if (LIBSPID_SUCCESS == ret, "must error");
+}
+END_TEST
+
+Suite * libspid_eoc_suite ()
+{
+ Suite *s = suite_create ("LIBSPID_EOC");
+ TCase *tc_core = tcase_create ("Core");
+ tcase_add_checked_fixture (tc_core, setup, teardown);
+ /* Test eoc libspid functions */
+ tcase_add_test (tc_core, test_libspid_eoc_vlan_port_get);
+ tcase_add_test (tc_core, test_libspid_eoc_vlan_port_get_list);
+ tcase_add_test (tc_core, test_libspid_eoc_vlan_port_set);
+ tcase_add_test (tc_core, test_libspid_eoc_vlan_port_check);
+ suite_add_tcase (s, tc_core);
+ return s;
+}
+
+int
+main (void)
+{
+ int number_failed = 0;
+ Suite *s;
+ //Run EoC tests
+ s = libspid_eoc_suite ();
+ SRunner *sr = srunner_create (s);
+ srunner_set_fork_status (sr, CK_NOFORK);
+ //srunner_set_fork_status (sr, CK_FORK);
+ srunner_run_all (sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed (sr);
+ srunner_free (sr);
+ return (number_failed == 0) ? 0 : -1;
+}