summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/tests/libspid/utests/src/system_utests.c
diff options
context:
space:
mode:
Diffstat (limited to 'cleopatre/devkit/tests/libspid/utests/src/system_utests.c')
-rw-r--r--cleopatre/devkit/tests/libspid/utests/src/system_utests.c511
1 files changed, 511 insertions, 0 deletions
diff --git a/cleopatre/devkit/tests/libspid/utests/src/system_utests.c b/cleopatre/devkit/tests/libspid/utests/src/system_utests.c
new file mode 100644
index 0000000000..007cda1556
--- /dev/null
+++ b/cleopatre/devkit/tests/libspid/utests/src/system_utests.c
@@ -0,0 +1,511 @@
+/* Cleopatre project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file system_utests.c
+ * \brief Unitary tests for libspid
+ * \ingroup Cleopatre - libspid
+ *
+ * This file content all the unitary tests for libspid system.c,
+ */
+
+#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 "system_utests.h"
+
+//#define DEBUG 1
+
+
+#ifdef DEBUG
+#define TRACE(...) printf("LIBSPID_SYSTEM UTESTS: " __VA_ARGS__)
+#else
+#define TRACE(...)
+#endif
+
+#define VERSION_TST "testfiles/version.tst"
+#define UPTIME_TST "testfiles/uptime.tst"
+#define MEMINFO_TST "testfiles/meminfo.tst"
+#define MTD_TST "testfiles/mtd.tst"
+
+/** local variables */
+
+
+/* fixtures - run before and after each unit test */
+void setup(void)
+{
+ int ret;
+ char buf[256];
+ struct dirent *d;
+ DIR *dir;
+
+ ret = mkdir(UTESTS_TMP_DIR, 0770);
+ if (ret < 0 && errno != EEXIST)
+ {
+ perror("mkdir(UTESTS_TMP_DIR, 0770)");
+ exit(EXIT_FAILURE);
+ }
+
+ /* initialize - delete all files which have left from previous testing */
+ dir = opendir(UTESTS_TMP_DIR);
+
+ while ( (d = readdir(dir)) != NULL )
+ {
+ //printf("%s\n",d->d_name);
+ sprintf(buf, "%s/%s", "/tmp/utests", d->d_name);
+ remove(buf);
+ }
+
+ /* 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);
+ }
+
+ sprintf(buf, "%s/proc", UTESTS_TMP_DIR);
+ ret = mkdir(buf, 0770);
+ if (ret < 0 && errno != EEXIST)
+ {
+ perror("mkdir(UTESTS_TMP_DIR/proc, 0770)");
+ exit(EXIT_FAILURE);
+ }
+
+ sprintf(buf, "%s/local", UTESTS_TMP_DIR);
+ ret = mkdir(buf, 0770);
+ if (ret < 0 && errno != EEXIST)
+ {
+ perror("mkdir(UTESTS_TMP_DIR, 0770)");
+ exit(EXIT_FAILURE);
+ }
+
+ sprintf(buf, "%s/factory", UTESTS_TMP_DIR);
+ ret = mkdir(buf, 0770);
+ if (ret < 0 && errno != EEXIST)
+ {
+ perror("mkdir(UTESTS_TMP_DIR, 0770)");
+ exit(EXIT_FAILURE);
+ }
+
+ sprintf(buf, "%s/dev", UTESTS_TMP_DIR);
+ ret = mkdir(buf, 0770);
+ if (ret < 0 && errno != EEXIST)
+ {
+ perror("mkdir(UTESTS_TMP_DIR, 0770)");
+ exit(EXIT_FAILURE);
+ }
+}
+
+void teardown(void)
+{
+}
+
+
+/* --- TEST PROCEDURES --- */
+
+/* TESTING FALSE PARAMS */
+START_TEST (param_get_kernel_version)
+{
+ int ret;
+ char buffer[LIBSPID_LINE_MAX_LEN];
+
+ /* buffer is NULL */
+ ret = libspid_system_get_kernel_version(NULL, LIBSPID_LINE_MAX_LEN);
+ fail_if(ret != LIBSPID_ERROR_PARAM, "buffer is NULL");
+
+ /* buffer_len is zero */
+ ret = libspid_system_get_kernel_version(buffer, 0);
+ fail_if(ret != LIBSPID_ERROR_PARAM, "buffer_len is 0");
+
+}
+END_TEST
+
+START_TEST (param_get_uptime)
+{
+ int ret;
+ unsigned int total_s;
+ unsigned int idle_s;
+
+ /* total_s is NULL */
+ ret = libspid_system_get_uptime(NULL, &idle_s);
+ fail_if(ret != LIBSPID_ERROR_PARAM, "total_s is NULL");
+
+ /* idle_s is NULL */
+ ret = libspid_system_get_uptime(&total_s, NULL);
+ fail_if(ret != LIBSPID_ERROR_PARAM, "idle_s is NULL");
+
+}
+END_TEST
+
+START_TEST (param_get_meminfo)
+{
+ int ret;
+ char buffer[LIBSPID_LINE_MAX_LEN];
+
+ /* buffer is NULL */
+ ret = libspid_system_get_meminfo(NULL, LIBSPID_LINE_MAX_LEN);
+ fail_if(ret != LIBSPID_ERROR_PARAM, "buffer is NULL");
+
+ /* buffer_len is zero */
+ ret = libspid_system_get_meminfo(buffer, 0);
+ fail_if(ret != LIBSPID_ERROR_PARAM, "buffer_len is 0");
+}
+END_TEST
+
+START_TEST (param_get_nvram)
+{
+ int ret;
+
+ /* nvram is NULL */
+ ret = libspid_system_get_nvram(NULL);
+ fail_if(ret != LIBSPID_ERROR_PARAM, "nvram is NULL");
+}
+END_TEST
+
+TCase *
+tc_param (void)
+{
+ TCase *tc_param = tcase_create ("param");
+ tcase_add_test (tc_param, param_get_kernel_version);
+ tcase_add_test (tc_param, param_get_uptime);
+ tcase_add_test (tc_param, param_get_meminfo);
+ tcase_add_test (tc_param, param_get_nvram);
+ return tc_param;
+}
+
+/** test procedures */
+START_TEST (test_libspid_system_get_kernel_version)
+{
+ int ret;
+ char buffer[LIBSPID_LINE_MAX_LEN];
+ char test_string[LIBSPID_LINE_MAX_LEN];
+ FILE *fp;
+
+ system("cp " VERSION_TST " " LIBSPID_SYSTEM_VERSION_PATH);
+
+ ret = libspid_system_get_kernel_version(buffer, LIBSPID_LINE_MAX_LEN);
+ fail_if(ret != LIBSPID_SUCCESS, "libspid_system_get_kernel_version fail");
+
+ fp = fopen(LIBSPID_SYSTEM_VERSION_PATH, "r");
+ if (fp == NULL)
+ {
+ perror("fopen(VERSION_TST, \"r\")");
+ exit(EXIT_FAILURE);
+ }
+ fgets(test_string, LIBSPID_LINE_MAX_LEN, fp);
+ fail_if( strcmp(buffer, test_string) != 0, "libspid_system_get_kernel_version gets wrong data");
+ fclose(fp);
+}
+END_TEST
+
+START_TEST (test_libspid_system_get_plc_version)
+{
+}
+END_TEST
+
+START_TEST (test_libspid_system_get_av_version)
+{
+}
+END_TEST
+
+START_TEST (test_libspid_system_get_uptime)
+{
+ int ret;
+ char test_string[LIBSPID_LINE_MAX_LEN];
+ FILE *fp;
+ unsigned int total_s;
+ unsigned int idle_s;
+
+ system("cp " UPTIME_TST " " LIBSPID_SYSTEM_UPTIME_PATH);
+ ret = libspid_system_get_uptime(&total_s, &idle_s);
+ fail_if(ret != LIBSPID_SUCCESS, "libspid_system_get_uptime");
+
+ fp = fopen(UPTIME_TST, "r");
+ if (fp == NULL)
+ {
+ perror("fopen(UPTIME_TST, \"r\")");
+ exit(EXIT_FAILURE);
+ }
+
+ fgets(test_string, LIBSPID_LINE_MAX_LEN, fp);
+ fclose(fp); /* flushes I/O buffer into the file */
+ fail_if(total_s != 11, "libspid_system_get_uptime gets wrong total_s");
+ fail_if(idle_s != 333, "libspid_system_get_uptime gets wrong idle_s");
+}
+END_TEST
+
+START_TEST (test_libspid_system_get_meminfo)
+{
+ int ret;
+ char buffer[LIBSPID_LINE_MAX_LEN];
+ char test_string[3*LIBSPID_LINE_MAX_LEN];
+ char line[LIBSPID_LINE_MAX_LEN];
+ FILE *fp;
+
+ system("cp " MEMINFO_TST " " LIBSPID_SYSTEM_MEMINFO_PATH);
+
+ ret = libspid_system_get_meminfo(buffer, LIBSPID_LINE_MAX_LEN);
+
+ fail_if(ret != LIBSPID_SUCCESS, "libspid_system_get_meminfo fail");
+
+ fp = fopen(MEMINFO_TST, "r");
+ if (fp == NULL)
+ {
+ perror("fopen(MEMINFO_TST, \"r\")");
+ exit(EXIT_FAILURE);
+ }
+
+ while ( fgets(line, LIBSPID_LINE_MAX_LEN, fp) != NULL )
+ {
+ strcat(test_string, line);
+ }
+ fclose(fp);
+ fail_if( strcmp(buffer, test_string) != 0, "libspid_system_get_meminfo gets wrong data");
+}
+END_TEST
+
+START_TEST (test_libspid_system_save)
+{
+ int ret;
+ char test_string[LIBSPID_LINE_MAX_LEN] = "test1\ntest2\n"; /* do not forget last '\n' */
+ FILE *fp;
+ char tst_file[128] = {0};
+
+ struct stat st;
+#ifdef DEBUG
+ struct dirent *d;
+ DIR *dir;
+#endif /* DEBUG */
+
+ sprintf(tst_file, "%s/test1", LIBSPID_CONF_ROOT_PATH);
+ mknod(tst_file, 0770, S_IFREG);
+ sprintf(tst_file, "%s/test2", LIBSPID_CONF_ROOT_PATH);
+ mknod(tst_file, 0770, S_IFREG);
+ sprintf(tst_file, "%s/test3.conf", LIBSPID_CONF_ROOT_PATH);
+ mknod(tst_file, 0770, S_IFREG);
+
+ fp = fopen(LIBSPID_SAVE_LIST_PATH, "w");
+ if (fp == NULL)
+ {
+ perror("fopen(LIBSPID_SAVE_LIST_PATH, \"w\")");
+ exit(EXIT_FAILURE);
+ }
+
+ fputs(test_string, fp);
+ fclose(fp); /* flushes I/O buffer into the file */
+
+ ret = libspid_system_save();
+ fail_if(ret != LIBSPID_SUCCESS, "libspid_system_get_meminfo fail");
+
+ /* test if files have been well copied */
+ sprintf(tst_file, "%s/test1", LIBSPID_SAVE_DIR_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_save does not backup files well" );
+ sprintf(tst_file, "%s/test2", LIBSPID_SAVE_DIR_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_save does not backup files well" );
+ sprintf(tst_file, "%s/test3.conf", LIBSPID_SAVE_DIR_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_save does not backup files well" );
+
+#ifdef DEBUG
+ /* print contents for debug */
+ dir = opendir(LIBSPID_SAVE_DIR_PATH);
+ if (dir == NULL)
+ {
+ perror("opendir()");
+ exit(1);
+ }
+ TRACE("Listing LIBSPID_SAVE_DIR_PATH, %s:\n", LIBSPID_SAVE_DIR_PATH);
+ while( (d = readdir(dir)) != NULL )
+ {
+ TRACE("%s\n",d->d_name);
+ }
+#endif
+
+}
+END_TEST
+
+START_TEST (test_libspid_system_factory)
+{
+ int ret;
+ char tst_file[128] = {0};
+
+ struct stat st;
+#ifdef DEBUG
+ struct dirent *d;
+ DIR *dir;
+#endif /* DEBUG */
+
+
+ sprintf(tst_file, "%s/test_F1", LIBSPID_FACTORY_PATH);
+ mknod(tst_file, 0770, S_IFREG);
+ sprintf(tst_file, "%s/test_F2", LIBSPID_FACTORY_PATH);
+ mknod(tst_file, 0770, S_IFREG);
+ sprintf(tst_file, "%s/test_F3.conf", LIBSPID_FACTORY_PATH);
+ mknod(tst_file, 0770, S_IFREG);
+
+ ret = libspid_system_factory();
+ fail_if(ret != LIBSPID_SUCCESS, "libspid_system_get_meminfo fail");
+
+ /* test if files have been well copied */
+ sprintf(tst_file, "%s/test_F1", LIBSPID_CONF_ROOT_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_factory does not backup files well" );
+ sprintf(tst_file, "%s/test_F1", LIBSPID_SAVE_DIR_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_factory does not backup files well" );
+ sprintf(tst_file, "%s/test_F2", LIBSPID_CONF_ROOT_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_factory does not backup files well" );
+ sprintf(tst_file, "%s/test_F2", LIBSPID_SAVE_DIR_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_factory does not backup files well" );
+ sprintf(tst_file, "%s/test_F3.conf", LIBSPID_CONF_ROOT_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_factory does not backup files well" );
+ sprintf(tst_file, "%s/test_F3.conf", LIBSPID_SAVE_DIR_PATH);
+ fail_if ( stat(tst_file,&st) != 0, "libspid_system_factory does not backup files well" );
+
+#ifdef DEBUG
+ /* print contents for debug */
+ dir = opendir(LIBSPID_SAVE_DIR_PATH);
+ if (dir == NULL)
+ {
+ perror("opendir()");
+ exit(1);
+ }
+ TRACE("Listing LIBSPID_SAVE_DIR_PATH, %s:\n", LIBSPID_SAVE_DIR_PATH);
+ while( (d = readdir(dir)) != NULL )
+ {
+ TRACE("%s\n",d->d_name);
+ }
+
+ sprintf(tst_file, "%s/test_F1", LIBSPID_SAVE_DIR_PATH);
+ if ( stat(tst_file,&st) == 0 )
+ {
+ printf("%s is present\n", tst_file);
+ }
+
+ sprintf(tst_file, "%s/test_NON_EXISTANT", LIBSPID_SAVE_DIR_PATH);
+ if ( stat(tst_file,&st) == 0 )
+ {
+ printf("%s is present\n", tst_file);
+ }
+ else
+ {
+ printf("%s is *NOT* present\n", tst_file);
+ }
+#endif /* DEBUG */
+}
+END_TEST
+
+START_TEST (test_libspid_system_get_nvram)
+{
+ static spc300_nvram_t nvram;
+ static spc300_nvram_t ret_nvram;
+ int ret;
+ FILE *fp;
+ char nvram_path[128] = {0};
+
+ /* initialize nvram struct */
+ memset( &nvram, 0x0, sizeof(spc300_nvram_t) );
+ strcpy( nvram.magic, SPC300_NVRAM_MAGIC); /* magic number "NVRAM\0\0\0" */
+ nvram.pkg_cfg = 0x123; /* SPC300 package configuration register */
+ nvram.gpio_0_7_cfg = 0x45; /* SPC300 GPIO 0 to 7 configuration register */
+ nvram.gpio_8_15_cfg = 0x6789; /* SPC300 GPIO 8 to 15 configuration register */
+ nvram.sdram_config = 0xABC; /* SPC300 SDRAM configuration register */
+ nvram.sdram_timing0 = 0x11; /* SPC300 SDRAM timing register 0 */
+ nvram.sdram_timing1 = 0x222; /* SPC300 SDRAM timing register 1 */
+ nvram.sdram_refresh = 0xdef; /* SPC300 SDRAM refresh register */
+ nvram.img_0_offset = 0x140000; /* offset of first image address */
+ nvram.nb_images = 2; /* Max Number of Images present in flash */
+
+ system("cp " MTD_TST " " LIBSPID_SYSTEM_MTD_PATH);
+
+ /* prepare /dev/mtd1 */
+ sprintf(nvram_path, "%s/mtd1", LIBSPID_DEV_PATH);
+ fp = fopen(nvram_path, "w");
+ if (fp == NULL)
+ {
+ perror("fopen(nvram_path, \"w\")");
+ exit(EXIT_FAILURE);
+ }
+
+ fwrite( &nvram, sizeof(spc300_nvram_t), 1, fp );
+ fclose(fp); /* flushes I/O buffer into the file */
+
+
+ memset( &ret_nvram, 0x0, sizeof(spc300_nvram_t) );
+ ret = libspid_system_get_nvram(&ret_nvram);
+ fail_if(ret != LIBSPID_SUCCESS, "libspid_system_get_nvram fail");
+
+ ret = memcmp ( &ret_nvram, &nvram, sizeof(spc300_nvram_t) );
+ fail_if(ret != 0, "libspid_system_get_nvram gets wrong nvram");
+
+#ifdef DEBUG
+ printf("ret_nvram.pkg_cfg = %#x\n", ret_nvram.pkg_cfg);
+ printf("ret_nvram.sdram_config = %#x\n", ret_nvram.sdram_config);
+#endif
+
+}
+END_TEST
+
+START_TEST (test_libspid_system_get_date)
+{
+}
+END_TEST
+
+extern Suite* libspid_system_suite(void)
+{
+ Suite *s = suite_create("LIBSPID_SYSTEM");
+ TCase *tc_core = tcase_create("Core");
+ tcase_add_checked_fixture (tc_core, setup, teardown);
+
+ //Test system_init
+ tcase_add_test(tc_core, test_libspid_system_get_kernel_version);
+ tcase_add_test(tc_core, test_libspid_system_get_plc_version);
+ tcase_add_test(tc_core, test_libspid_system_get_av_version);
+ tcase_add_test(tc_core, test_libspid_system_get_uptime);
+ tcase_add_test(tc_core, test_libspid_system_get_meminfo);
+ tcase_add_test(tc_core, test_libspid_system_save);
+ tcase_add_test(tc_core, test_libspid_system_factory);
+ tcase_add_test(tc_core, test_libspid_system_get_nvram);
+ tcase_add_test(tc_core, test_libspid_system_get_date);
+
+ suite_add_tcase(s, tc_core);
+ suite_add_tcase(s, tc_param());
+ return s;
+}
+
+int main(void)
+{
+ int number_failed = 0;
+ Suite *s;
+
+ //Run system tests
+ s = libspid_system_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);
+
+ /* clean behind */
+ system("rm -r "UTESTS_TMP_DIR);
+
+ return (number_failed == 0) ? 0 : -1;
+}
+