Add handle list for vpn service 80/68080/1
authorhyunuktak <hyunuk.tak@samsung.com>
Mon, 2 May 2016 06:37:10 +0000 (15:37 +0900)
committerhyunuktak <hyunuk.tak@samsung.com>
Mon, 2 May 2016 06:37:14 +0000 (15:37 +0900)
Modify some code for test

Change-Id: Ie137b4b223cf4b847b1200ecd136b5bb7a1c82f2
Signed-off-by: hyunuktak <hyunuk.tak@samsung.com>
framework/src/capi_vpn_service.c
packaging/capi-vpn-service.spec
test/CMakeLists.txt
test/vpn_service_test.c

index 5ad13e6..d83ce10 100755 (executable)
@@ -34,6 +34,7 @@
 
 GVariant *op = NULL;
 
+static __thread GSList *vpn_handle_list = NULL;
 static __thread bool is_feature_checked = false;
 static __thread bool feature_supported = false;
 
@@ -60,6 +61,17 @@ int _vpnsvc_check_feature_supported(const char *feature_name)
        return VPNSVC_ERROR_NONE;
 }
 
+static bool _vpn_check_handle_validity(vpnsvc_h vpnsvc)
+{
+       if (vpnsvc == NULL)
+               return false;
+
+       if (g_slist_find(vpn_handle_list, vpnsvc) != NULL)
+               return true;
+       else
+               return false;
+}
+
 static void _vpnsvc_init_vpnsvc_tun_s(vpnsvc_tun_s **s)
 {
        LOGD(" tun_s: %p", s);
@@ -243,11 +255,11 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle)
 
        /* parameter check */
        if (iface_name == NULL || strlen(iface_name) <= 0) {
-               LOGE("iface_name is a NULL");//LCOV_EXCL_LINE
-               return VPNSVC_ERROR_INVALID_PARAMETER;//LCOV_EXCL_LINE
-       } else if (handle == NULL) {
-               LOGE("handle is a NULL");//LCOV_EXCL_LINE
-               return VPNSVC_ERROR_INVALID_PARAMETER;//LCOV_EXCL_LINE
+               LOGE("iface_name is a NULL"); //LCOV_EXCL_LINE
+               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       } else if (_vpn_check_handle_validity(*handle)) {
+               LOGE("Invalid parameter"); //LCOV_EXCL_LINE
+               return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
        vpnsvc_tun_s *tmp_s = NULL;
@@ -324,6 +336,7 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle)
                        strncpy(tmp_s->name, tmp_name, VPNSVC_VPN_IFACE_NAME_LEN);
                        tmp_s->name[VPNSVC_VPN_IFACE_NAME_LEN-1] = '\0';
                        *handle = tmp_s;
+                       vpn_handle_list = g_slist_prepend(vpn_handle_list, *handle);
                        LOGD("handle : %p, handle->fd : %d, handle->index : %d, handle->name : %s",
                                (*handle), ((vpnsvc_tun_s*)*handle)->fd, ((vpnsvc_tun_s*)*handle)->index, ((vpnsvc_tun_s*)*handle)->name);
                }
@@ -383,6 +396,7 @@ EXPORT_API int vpnsvc_deinit(vpnsvc_h handle)
 
                /* free allocared handle memory */
                _vpnsvc_deinit_vpnsvc_tun_s(tun_s);
+               vpn_handle_list = g_slist_remove(vpn_handle_list, handle);
        }
 
        return result;
index 6c43f74..50c11a2 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       vpnsvc-pkg
 Summary:    VPN service library in TIZEN C API
-Version:    1.0.13
+Version:    1.0.14
 Release:    1
 Group:      System/Network
 License:    Apache-2.0
@@ -43,12 +43,12 @@ Group:    Development/Libraries
 %description -n capi-vpnsvc-devel
 capi-vpnsvc CAPI devel package
 
-%package -n vpnsvc-test
-Summary:  Vpnsvc test
-Group:    Development/Libraries
+#%package -n vpnsvc_test
+#Summary:  Vpnsvc test
+#Group:    Development/Libraries
 
-%description -n vpnsvc-test
-vpnsvc test package
+#%description -n vpnsvc_test
+#vpnsvc test package
 
 %package -n vpnsvc-daemon
 Summary:  Vpnsvc daemon
@@ -129,13 +129,14 @@ fi
 %manifest framework/capi-vpnsvc.manifest
 %{_libdir}/libcapi-vpnsvc.so.*
 %{_datadir}/license/capi-vpnsvc
+%{_bindir}/vpnsvc_test
 
 %files -n capi-vpnsvc-devel
 %{_includedir}/*.h
 %{_libdir}/pkgconfig/capi-vpnsvc.pc
 %{_libdir}/libcapi-vpnsvc.so
 
-%files -n vpnsvc-test
-%manifest test/vpnsvc-test.manifest
-/usr/sbin/vpnsvc-test
+#%files -n vpnsvc_test
+#%manifest test/vpnsvc-test.manifest
+#%{_bindir}/vpnsvc_test
 
index e23d651..f2146f8 100755 (executable)
@@ -1,5 +1,5 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-SET(PACKAGE_NAME vpnsvc-test)
+SET(PACKAGE_NAME vpnsvc_test)
 SET(LIB_NAME ${PACKAGE_NAME})
 PROJECT(${LIB_NAME})
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
@@ -38,4 +38,4 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath="${LIBDIR})
 ADD_EXECUTABLE(${PACKAGE_NAME} ${TEST_SRCS})
 TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} "-ldl" capi-vpnsvc)
 
-INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION sbin)
+INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION bin/)
index d36bfac..e1b4dbb 100755 (executable)
 
 vpnsvc_h handle = NULL;
 
+static const char *test_print_error(vpnsvc_error_e error)
+{
+       switch (error) {
+       case VPNSVC_ERROR_NONE:
+               return "VPNSVC_ERROR_NONE";
+       case VPNSVC_ERROR_INVALID_PARAMETER:
+               return "VPNSVC_ERROR_INVALID_PARAMETER";
+       case VPNSVC_ERROR_OUT_OF_MEMORY:
+               return "VPNSVC_ERROR_OUT_OF_MEMORY";
+       case VPNSVC_ERROR_PERMISSION_DENIED:
+               return "VPNSVC_ERROR_PERMISSION_DENIED";
+       case VPNSVC_ERROR_NO_SUCH_FILE:
+               return "VPNSVC_ERROR_NO_SUCH_FILE";
+       case VPNSVC_ERROR_IO_ERROR:
+               return "VPNSVC_ERROR_IO_ERROR";
+       case VPNSVC_ERROR_TIMEOUT:
+               return "VPNSVC_ERROR_TIMEOUT";
+       case VPNSVC_ERROR_IPC_FAILED:
+               return "VPNSVC_ERROR_IPC_FAILED";
+       case VPNSVC_ERROR_NOT_SUPPORTED:
+               return "VPNSVC_ERROR_NOT_SUPPORTED";
+       default:
+               return "VPNSVC_ERROR_UNKNOWN";
+       }
+}
+
 int test_vpnsvc_init()
 {
        char *name = TEST_VPN_IF_NAME;
-       int ret = VPNSVC_ERROR_NONE;
+       int rv = VPNSVC_ERROR_NONE;
        int int_value;
 
-       printf("test vpnsvc_init\n");
+       rv = vpnsvc_init(name, &handle);
 
-       ret = vpnsvc_init(name, &handle);
-
-       if (ret != VPNSVC_ERROR_NONE) {
-               printf("vpnsvc_init failed : %d\n", ret);
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc initialize fail [%s]\n", test_print_error(rv));
+               return -1;
        } else {
                char* result_name = NULL;
-               printf("vpnsvc_init Succeed : %d\n", ret);
 
                if (vpnsvc_get_iface_fd(handle, &int_value) == VPNSVC_ERROR_NONE)
                        printf("iface_fd : %d\n", int_value);
@@ -66,32 +90,40 @@ int test_vpnsvc_init()
                else
                        printf("Fail to get iface_index\n");
 
-               ret = vpnsvc_get_iface_name(handle, &result_name);
-               if (ret == VPNSVC_ERROR_NONE)
+               if (vpnsvc_get_iface_name(handle, &result_name) == VPNSVC_ERROR_NONE)
                        printf("iface_name : %s\n", result_name);
+               else
+                       printf("Fail to get iface_name\n");
        }
 
-       return 0;
+       printf("vpnsvc initialize success\n");
+       return 1;
 }
 
 int test_vpnsvc_deinit()
 {
-       printf("test vpnsvc_deinit\n");
+       int rv = 0;
 
        if (handle)
-               vpnsvc_deinit(handle);
-
-       handle = NULL;
+               rv = vpnsvc_deinit(handle);
+       else {
+               printf("cannot deinitialize : handle is NULL\n");
+               rv = VPNSVC_ERROR_INVALID_PARAMETER;
+       }
 
-       return 0;
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc deinitialize fail [%s]\n", test_print_error(rv));
+               return -1;
+       }
 
+       handle = NULL;
+       printf("vpnsvc deinitialize success\n");
+       return 1;
 }
 
 int test_vpnsvc_protect()
 {
-       int sock, ret;
-
-       printf("test vpnsvc_protect\n");
+       int sock, rv;
 
        if (!handle) {
                printf("invalid handle\n");
@@ -103,20 +135,20 @@ int test_vpnsvc_protect()
                return -2;
        }
 
-       ret = vpnsvc_protect(handle, sock, "wlan0");
-       if (ret != VPNSVC_ERROR_NONE)
-               printf("vpnsvc_protect failed!\n");
+       rv = vpnsvc_protect(handle, sock, "wlan0");
+       if (rv != VPNSVC_ERROR_NONE)
+               printf("vpnsvc protect fail [%s]\n", test_print_error(rv));
        else
-               printf("vpnsvc_protect Succeed!\n");
+               printf("vpnsvc protect success\n");
 
        close(sock);
 
-       return 0;
+       return 1;
 }
 
 int test_vpnsvc_up()
 {
-       int ret;
+       int rv;
        char local[VPNSVC_IP4_STRING_LEN] = {'\0',};
        char remote[VPNSVC_IP4_STRING_LEN] = {'\0',};
        char *routes[2];
@@ -152,11 +184,7 @@ int test_vpnsvc_up()
        dns_server[0] = dns1;
        dns_server[1] = dns2;
 
-       ret = vpnsvc_up(handle, local, remote, routes, prefix, nr_routes, dns_server, nr_dns, dns_suffix);
-       if (ret != VPNSVC_ERROR_NONE)
-               printf("vpnsvc_up failed!\n");
-       else
-               printf("vpnsvc_up Succeed!\n");
+       rv = vpnsvc_up(handle, local, remote, routes, prefix, nr_routes, dns_server, nr_dns, dns_suffix);
 
        free(routes[0]);
        free(routes[1]);
@@ -164,37 +192,41 @@ int test_vpnsvc_up()
        routes[0] = NULL;
        routes[1] = NULL;
 
-       return 0;
+       if (rv != VPNSVC_ERROR_NONE)
+               printf("vpnsvc up fail [%s]\n", test_print_error(rv));
+       else
+               printf("vpnsvc up success\n");
+
+       return 1;
 }
 
 int test_vpnsvc_down()
 {
-       int ret;
+       int rv;
 
        if (!handle) {
                printf("invalid handle\n");
                return -1;
        }
 
-       ret = vpnsvc_down(handle);
-
-       if (ret != VPNSVC_ERROR_NONE)
-               printf("vpnsvc_down failed!\n");
-       else
-               printf("vpnsvc_down Succeed!\n");
-
-       return 0;
-
+       rv = vpnsvc_down(handle);
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc down fail [%s]\n", test_print_error(rv));
+               return -1;
+       } else {
+               printf("vpnsvc down success\n");
+               return 1;
+       }
 }
 
 int test_vpnsvc_read()
 {
-       return 0;
+       return -1;
 }
 
 int test_vpnsvc_write()
 {
-       return 0;
+       return -1;
 }
 
 int test_vpnsvc_block_networks()
@@ -205,7 +237,7 @@ int test_vpnsvc_block_networks()
        char* allow_nets[2];
        int allow_prefix[2];
        int allow_nr_nets = 2;
-       int ret;
+       int rv;
 
        if (!handle) {
                printf("invalid handle\n");
@@ -230,12 +262,7 @@ int test_vpnsvc_block_networks()
        strncpy(allow_nets[1], "206.190.36.45", VPNSVC_IP4_STRING_LEN);
        allow_prefix[1] = 32;
 
-       ret = vpnsvc_block_networks(handle, block_nets, block_prefix, block_nr_nets, allow_nets, allow_prefix, allow_nr_nets);
-
-       if (ret != VPNSVC_ERROR_NONE)
-               printf("vpnsvc_block_networks failed!\n");
-       else
-               printf("vpnsvc_block_networks Succeed!\n");
+       rv = vpnsvc_block_networks(handle, block_nets, block_prefix, block_nr_nets, allow_nets, allow_prefix, allow_nr_nets);
 
        free(block_nets[0]);
        free(block_nets[1]);
@@ -247,78 +274,84 @@ int test_vpnsvc_block_networks()
        allow_nets[0] = NULL;
        allow_nets[1] = NULL;
 
-       return 0;
-
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc block networks fail [%s]\n", test_print_error(rv));
+               return -1;
+       } else {
+               printf("vpnsvc block networks success\n");
+               return 1;
+       }
 }
 
 int test_vpnsvc_unblock_networks()
 {
-       int ret;
+       int rv;
 
        if (!handle) {
                printf("invalid handle\n");
                return -1;
        }
 
-       ret = vpnsvc_unblock_networks(handle);
-
-       if (ret != VPNSVC_ERROR_NONE)
-               printf("vpnsvc_unblock_networks failed!\n");
-       else
-               printf("vpnsvc_unblock_networks Succeed!\n");
-
-       return 0;
+       rv = vpnsvc_unblock_networks(handle);
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc unblock networks fail [%s]\n", test_print_error(rv));
+               return -1;
+       } else {
+               printf("vpnsvc unblock networks success");
+               return 1;
+       }
 }
 
 int test_vpnsvc_set_mtu()
 {
-       int ret;
+       int rv;
 
-       ret = vpnsvc_set_mtu(handle, 9000);
-
-       if (ret != VPNSVC_ERROR_NONE)
-               printf("vpnsvc_set_mtu failed!\n");
-       else
-               printf("vpnsvc_set_mtu Succeed!\n");
-
-       return 0;
+       rv = vpnsvc_set_mtu(handle, 9000);
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc set mtu fail [%s]\n", test_print_error(rv));
+               return -1;
+       } else {
+               printf("vpnsvc set mtu success\n");
+               return 1;
+       }
 }
 
 bool g_blocking = false;
 
 int test_vpnsvc_set_blocking()
 {
-       int ret;
+       int rv;
        g_blocking = !g_blocking;
 
        printf("Blocking Parameter: %s\n", g_blocking ? "true" : "false");
-       ret = vpnsvc_set_blocking(handle, g_blocking);
-
-       if (ret != VPNSVC_ERROR_NONE)
-               printf("vpnsvc_set_blocking failed!\n");
-       else
-               printf("vpnsvc_set_blocking Succeed!\n");
+       rv = vpnsvc_set_blocking(handle, g_blocking);
 
-       return 0;
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc set blocking fail [%s]\n", test_print_error(rv));
+               return -1;
+       } else {
+               printf("vpnsvc set blocking success\n");
+               return 1;
+       }
 }
 
 int test_vpnsvc_set_session()
 {
-       int ret;
+       int rv;
        char *set_session = "vpnsvc_test VPN Session";
        char *get_session = NULL;
 
-       ret = vpnsvc_set_session(handle, set_session);
+       rv = vpnsvc_set_session(handle, set_session);
 
-       if (ret != VPNSVC_ERROR_NONE) {
-               printf("vpnsvc_set_session failed!\n");
+       if (rv != VPNSVC_ERROR_NONE) {
+               printf("vpnsvc set session fail [%s]\n", test_print_error(rv));
+               return -1;
        } else {
-               ret = vpnsvc_get_session(handle, &get_session);
-               printf("Session Name = %s\n", get_session);
-               printf("vpnsvc_set_session Succeed!\n");
+               rv = vpnsvc_get_session(handle, &get_session);
+               printf("session name = %s\n", get_session);
+               printf("vpnsvc set session Success\n");
+               return 1;
        }
-
-       return 0;
 }
 
 int test_exit()
@@ -376,7 +409,11 @@ int main()
                        continue;
                }
 
-               test_function_table[comm-1]();
+               int rv = test_function_table[comm-1]();
+               if (rv == 1)
+                       printf("Operation succeeded!\n");
+               else
+                       printf("Operation failed!\n");
        }
        return 0;
 }