Create system-info-test 49/203249/4
authorKichan Kwon <k_c.kwon@samsung.com>
Thu, 11 Apr 2019 05:18:06 +0000 (14:18 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Fri, 28 Jun 2019 00:56:32 +0000 (09:56 +0900)
- Input key information, then prints value

Change-Id: I0dac252d8fb3d518508113978def523d4987cca1
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
CMakeLists.txt
packaging/capi-system-info.spec
src/test/CMakeLists.txt [new file with mode: 0755]
src/test/system_info_test.c [new file with mode: 0755]

index 2a74d58..17340cf 100644 (file)
@@ -81,6 +81,7 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/system_info_intf.h DESTINATION
 
 ADD_SUBDIRECTORY(src/tizenid)
 ADD_SUBDIRECTORY(src/init_db)
+ADD_SUBDIRECTORY(src/test)
 
 IF(UNIX)
 
index 220404f..d470a4b 100644 (file)
@@ -27,6 +27,13 @@ Requires: %{name} = %{version}-%{release}
 
 %description devel
 
+%package test
+Summary:  System-info test package
+Group:    Development/System
+Requires: %{name}
+
+%description test
+A System Information library test tool
 
 %prep
 %setup -q
@@ -94,3 +101,6 @@ mkdir -p %{buildroot}/%{model_config_rw_dir}
 %{_includedir}/plugin/system_info_intf.h
 %{_libdir}/pkgconfig/*.pc
 %{_libdir}/libcapi-system-info.so
+
+%files test
+%{_bindir}/system_info_test
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..6c0905f
--- /dev/null
@@ -0,0 +1,22 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(SYSTEM_INFO_TEST "system_info_test")
+SET(SRCS ${CMAKE_SOURCE_DIR}/src/test/system_info_test.c)
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
+
+FOREACH(flag ${SYSTEM_INFO_TEST_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -fno-omit-frame-pointer -finstrument-functions -fpie")
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+
+ADD_EXECUTABLE(${SYSTEM_INFO_TEST} ${SRCS})
+ADD_DEPENDENCIES(${SYSTEM_INFO_TEST} ${fw_name})
+TARGET_LINK_LIBRARIES(${SYSTEM_INFO_TEST} ${SYSTEM_INFO_TEST_pkgs_LDFLAGS} "-L${CMAKE_SOURCE_DIR} -lcapi-system-info")
+
+INSTALL(TARGETS ${SYSTEM_INFO_TEST} DESTINATION bin)
diff --git a/src/test/system_info_test.c b/src/test/system_info_test.c
new file mode 100755 (executable)
index 0000000..a8178a5
--- /dev/null
@@ -0,0 +1,260 @@
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <system_info.h>
+
+#define SUCCESS 0
+
+#define OPT_MAX 16
+#define KEY_MAX 256
+
+#define RUNTIME_ENV "RUNTIME_TYPE"
+
+#define DEFAULT_OPT 0
+
+#define CONVERT_ARGUMENT(input_str, output_num, table) \
+{      \
+       int __idx;      \
+       bool __is_converted = false;    \
+       for (__idx = 0; __idx < sizeof(table) / sizeof(struct str_num_table); __idx++) {        \
+               if (!strncmp(input_str, table[__idx].str, strlen(table[__idx].str) + 1)) {      \
+                       output_num = table[__idx].num;  \
+                       __is_converted = true;  \
+                       break;  \
+               }       \
+       }       \
+       if (!__is_converted) {  \
+               if (input_str[0] == '\0') {     \
+                       output_num = table[DEFAULT_OPT].num;    \
+                       strncpy(input_str, table[DEFAULT_OPT].str, strlen(table[DEFAULT_OPT].str) + 1); \
+               } else {        \
+                       printf("Invalid argument %s\n\n", input_str);   \
+                       show_help();    \
+                       return EINVAL;  \
+               }       \
+       }       \
+}
+
+enum tag {
+       TAG_PLATFORM,
+       TAG_CUSTOM,
+       TAG_MAX
+};
+
+enum type {
+       TYPE_BOOL,
+       TYPE_INT,
+       TYPE_DOUBLE,
+       TYPE_STRING,
+       TYPE_MAX
+};
+
+enum runtime {
+       RUNTIME_C,
+       RUNTIME_WEB,
+       RUNTIME_DOTNET,
+       RUNTIME_MAX
+};
+
+struct str_num_table {
+       const char *str;
+       int num;
+};
+
+/**
+ * @remarks  If there is no input, set to the first element.
+ *           It means the first element is default value.
+ *           To disable default value, check essential options.
+ */
+
+struct str_num_table tag_table[TAG_MAX] = {
+       { "platform", TAG_PLATFORM },
+       { "custom",   TAG_CUSTOM },
+};
+
+struct str_num_table type_table[TYPE_MAX] = {
+       { "bool",   TYPE_BOOL },
+       { "int",    TYPE_INT },
+       { "double", TYPE_DOUBLE },
+       { "string", TYPE_STRING },
+};
+
+struct str_num_table runtime_table[RUNTIME_MAX] = {
+       { "c",      RUNTIME_C },
+       { "web",    RUNTIME_WEB },
+       { "dotnet", RUNTIME_DOTNET },
+};
+
+static char     tag_str[OPT_MAX];
+static enum tag tag_num;
+
+static char      type_str[OPT_MAX];
+static enum type type_num;
+
+static char key[KEY_MAX];
+
+static char         runtime_str[OPT_MAX];
+static enum runtime runtime_num;
+
+static void show_help(void)
+{
+       printf("system_info_test [OPTIONS]\n");
+       printf("  -g TAG      System info tag to read (platform/custom)\n");
+       printf("              (Optional, default value is platform)\n");
+       printf("  -t TYPE     System info type to read (bool/int/double/string)\n");
+       printf("              (Essential)\n");
+       printf("  -k KEY      System info key to read\n");
+       printf("              (Essential)\n");
+       printf("  -r RUNTIME  Runtime type (c/web/dotnet)\n");
+       printf("              (Optional, default value is c)\n");
+       printf("\n");
+       printf("  -h       Show this message\n");
+       printf("\n");
+       printf("Example:\n");
+       printf("  system_info_test -g platform -t string -k tizen.org/system/platform.name -r c\n");
+}
+
+int main(int argc, char *argv[])
+{
+       int ret;
+       int opt;
+
+       bool val_bool;
+       int val_int;
+       double val_double;
+       char *val_string = NULL;
+
+       /* Parse arguments */
+       while ((opt = getopt(argc, argv, "g:t:k:r:h")) != -1) {
+               switch (opt) {
+               case 'g':
+                       snprintf(tag_str, OPT_MAX, "%s", optarg);
+                       break;
+               case 't':
+                       snprintf(type_str, OPT_MAX, "%s", optarg);
+                       break;
+               case 'k':
+                       snprintf(key, KEY_MAX, "%s", optarg);
+                       break;
+               case 'r':
+                       snprintf(runtime_str, OPT_MAX, "%s", optarg);
+                       break;
+               case 'h':
+                       show_help();
+                       return SUCCESS;
+               default:
+                       show_help();
+                       return EINVAL;
+               }
+       }
+
+       /* Check whether essential options are inputted */
+       if (type_str[0] == '\0' || key[0] == '\0') {
+               printf("Essential options aren't inputted\n\n");
+               show_help();
+               return EINVAL;
+       }
+
+       /* Convert string arguments into the enum value */
+       CONVERT_ARGUMENT(tag_str, tag_num, tag_table);
+       CONVERT_ARGUMENT(type_str, type_num, type_table);
+       CONVERT_ARGUMENT(runtime_str, runtime_num, runtime_table);
+
+       /* Set the runtime environment value */
+       switch (runtime_num) {
+       case RUNTIME_C:
+               ret = setenv(RUNTIME_ENV, "capp", 1);
+               break;
+       case RUNTIME_WEB:
+               ret = setenv(RUNTIME_ENV, "webapp", 1);
+               break;
+       case RUNTIME_DOTNET:
+               ret = setenv(RUNTIME_ENV, "dotnet", 1);
+               break;
+       default:
+               printf("Failed to convert runtime argument\n");
+               return EIO;
+               break;
+       }
+
+       if (ret != SUCCESS) {
+               printf("setenv failed : %m\n");
+               return errno;
+       }
+
+       printf("TAG(%s), TYPE(%s), RUNTIME(%s), KEY(%s), RESULT(",
+                       tag_str, type_str, runtime_str, key);
+
+       /* Call appropriate system-info API and print result */
+       switch (tag_num) {
+       case TAG_PLATFORM:
+               switch (type_num) {
+               case TYPE_BOOL:
+                       ret = system_info_get_platform_bool(key, &val_bool);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%d", val_bool);
+                       break;
+               case TYPE_INT:
+                       ret = system_info_get_platform_int(key, &val_int);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%d", val_int);
+                       break;
+               case TYPE_DOUBLE:
+                       ret = system_info_get_platform_double(key, &val_double);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%lf", val_double);
+                       break;
+               case TYPE_STRING:
+                       ret = system_info_get_platform_string(key, &val_string);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%s", val_string);
+                       break;
+               default:
+                       ret = SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+                       break;
+               }
+               break;
+       case TAG_CUSTOM:
+               switch (type_num) {
+               case TYPE_BOOL:
+                       ret = system_info_get_custom_bool(key, &val_bool);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%d", val_bool);
+                       break;
+               case TYPE_INT:
+                       ret = system_info_get_custom_int(key, &val_int);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%d", val_int);
+                       break;
+               case TYPE_DOUBLE:
+                       ret = system_info_get_custom_double(key, &val_double);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%lf", val_double);
+                       break;
+               case TYPE_STRING:
+                       ret = system_info_get_custom_string(key, &val_string);
+                       if (ret == SYSTEM_INFO_ERROR_NONE)
+                               printf("%s", val_string);
+                       break;
+               default:
+                       ret = SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+                       break;
+               }
+               break;
+       default:
+               ret = SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       if (ret != SYSTEM_INFO_ERROR_NONE) {
+               printf("ERROR : %d", ret);
+       }
+
+       printf(")\n");
+
+       return SUCCESS;
+}