From 1f65ddf6d0c29822d0aee727edeccd8b7fe09ec9 Mon Sep 17 00:00:00 2001 From: Jooseok Song Date: Wed, 12 Jul 2017 10:54:03 +0900 Subject: [PATCH] Initial Version of Account Sample Test Change-Id: I1ea483f6f93efdb5542352665366b447109eb86d --- CMakeLists.txt | 31 +++ account-test.manifest | 5 + packaging/account-test.service | 15 ++ packaging/account-test.spec | 52 +++++ src/CMakeLists.txt | 22 ++ src/main.c | 456 +++++++++++++++++++++++++++++++++++++++++ src/util.h | 59 ++++++ 7 files changed, 640 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 account-test.manifest create mode 100644 packaging/account-test.service create mode 100644 packaging/account-test.spec create mode 100644 src/CMakeLists.txt create mode 100644 src/main.c create mode 100644 src/util.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..28063c1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,31 @@ +# Account test build script +MESSAGE("===== ACCOUNT TEST START") + +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(account-test C) + +INCLUDE(FindPkgConfig) + +SET(LIBNAME "${PROJECT_NAME}") +SET(EXENAME "${PROJECT_NAME}") +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(BINDIR "${PREFIX}/bin") +SET(LIBDIR "${PREFIX}/%{_libdir}") +SET(PIDDIR "/var/run") +SET(TMPDIR "/tmp") + +FOREACH(flag ${service_pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Werror -Wno-deprecated-declarations") +SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Werror -Wno-deprecated-declarations") + +ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"") +ADD_DEFINITIONS("-DPIDDIR=\"${PIDDIR}\"") +ADD_DEFINITIONS("-DTMPDIR=\"${TMPDIR}\"") + +# Pushtest daemon +ADD_SUBDIRECTORY(src) + +MESSAGE("===== ACCOUNT TEST END") diff --git a/account-test.manifest b/account-test.manifest new file mode 100644 index 0000000..97e8c31 --- /dev/null +++ b/account-test.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/account-test.service b/packaging/account-test.service new file mode 100644 index 0000000..b9d0340 --- /dev/null +++ b/packaging/account-test.service @@ -0,0 +1,15 @@ +[Unit] +Description=Account Test daemon +After=dbus.service + +[Service] +Type=simple +ExecStart=/usr/bin/account-test +Restart=always +RestartSec=1 +MemoryLimit=10M +User=system +Group=system + +[Install] +WantedBy=default.target diff --git a/packaging/account-test.spec b/packaging/account-test.spec new file mode 100644 index 0000000..b289026 --- /dev/null +++ b/packaging/account-test.spec @@ -0,0 +1,52 @@ +Name: account-test +Summary: Account test services +Version: 0.0.1 +Release: 1 +Group: Application Framework/Service +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz +Source1: account-test.service +Requires(post): /sbin/ldconfig +Requires(postun): /sbin/ldconfig +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(gthread-2.0) +BuildRequires: pkgconfig(accounts-svc) + +BuildRequires: cmake + +%description +Account test daemon + +%package bin +Summary: Account test daemon +Group: Application Framework/Service + +%description bin +Account test daemon + +%prep +%setup -q + +%build + +cmake . -DCMAKE_INSTALL_PREFIX="/usr" -DVERSION=%{version} +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} + +%make_install +mkdir -p %{buildroot}%{_unitdir}/default.target.wants +install -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}/account-test.service +ln -s ../account-test.service %{buildroot}%{_unitdir}/default.target.wants/account-test.service + +%post bin + +%files +%manifest account-test.manifest +%{_bindir}/account-test + +%{_unitdir}/account-test.service +%{_unitdir}/default.target.wants/account-test.service + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..d9380bf --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,22 @@ +# Account test daemon build script +MESSAGE("======= DAEMON") + +pkg_check_modules(EPKGS REQUIRED glib-2.0 dlog gthread-2.0 accounts-svc) + +SET(SRCS main.c) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread -pie") + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + +STRING(REPLACE ";" " " EXTRA_CFLAGS "${EPKGS_CFLAGS}") + +ADD_DEFINITIONS("-DIS_LITTLE_ENDIAN=1") + +ADD_EXECUTABLE(${EXENAME} ${SRCS}) +TARGET_LINK_LIBRARIES(${EXENAME} ${EPKGS_LIBRARIES}) +SET_TARGET_PROPERTIES(${EXENAME} PROPERTIES COMPILE_FLAGS ${EXTRA_CFLAGS}) + +INSTALL(TARGETS ${EXENAME} DESTINATION ${BINDIR}) +MESSAGE("======= DAEMON END") diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..28530ff --- /dev/null +++ b/src/main.c @@ -0,0 +1,456 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "util.h" + +#define check_fail(exp) \ + if (!(exp)) { \ + return true; \ + } + + +GMainLoop *global_loop = NULL; +static account_h account = NULL; + +const char* user_name = "test_user_name"; +const char* package_name = "account-test"; +const char* display_name = "test_display_name"; +const char* icon_path = "test_icon_path"; +const char* domain_name = "test_domain_name"; +const char* email_address = "test_email_address"; +const char* source = "source"; +const char* key = "key"; +const char* value = "value"; +const char* user_text = "user_text"; +const char* access_token = "test_access_token"; + +const char* contact_capability = ACCOUNT_SUPPORTS_CAPABILITY_CONTACT; + +const account_auth_type_e auth_type = ACCOUNT_AUTH_TYPE_OAUTH; +const account_capability_state_e capability_state = ACCOUNT_CAPABILITY_ENABLED; +const account_secrecy_state_e secrecy_state = ACCOUNT_SECRECY_VISIBLE; +const account_sync_state_e sync_state = ACCOUNT_SYNC_STATUS_OFF; + +static void _account_free_text(char *text) +{ + if (text != NULL) { + free(text); + text = NULL; + } +} + + +static bool account_call_back(account_h account, void *user_data) +{ + const char* key = "key"; + char* str = NULL; + int value = -1; + account_capability_state_e cap_value = 0; + account_auth_type_e auth_value = 0; + account_secrecy_state_e secret_value = 0; + account_sync_state_e sync_value = 0; + int ret = ACCOUNT_ERROR_NONE; + int i = -1; + check_fail(user_data!=NULL); + account_h _account = *((account_h*)user_data); + + ret = account_get_package_name(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Package Name : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_user_name(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] User Name : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_display_name(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Display Name : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_icon_path(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Icon Path : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_domain_name(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Domain Name : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_email_address(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Email Address : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_source(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Get Source : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_custom(_account, key, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Get Custom : %s\n"_RESET, str); + _account_free_text(str); + + ret = account_get_capability(_account, contact_capability, &cap_value); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Get Capability : %d\n"_RESET, cap_value); + + ret = account_get_access_token(_account, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Get Access Token : %s\n"_RESET, str); + _account_free_text(str); + + for (i = 0; i < 5; i++){ + ret = account_get_user_text(_account, i, &str); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] User Text : %s\n"_RESET, str); + _account_free_text(str); + } + + for (i = 0; i < 5; i++){ + ret = account_get_user_int(_account, i, &value); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] User Int : %d\n"_RESET, value); + } + + ret = account_get_auth_type(_account, &auth_value); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Auth Type : %d\n"_RESET, auth_value); + + ret = account_get_secret(_account, &secret_value); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Secret Value : %d\n"_RESET, secret_value); + + ret = account_get_sync_support(_account, &sync_value); + check_fail(ret == ACCOUNT_ERROR_NONE); + printf(_BOLDCYAN"[Account Callback] Sync Value : %d\n"_RESET, sync_value); + + return true; +} + + +int account_update_to_db_by_id_test(void) +{ + int account_id = -1; + int ret = ACCOUNT_ERROR_NONE; + char* update_user_name = "update_user_name"; + + ret = account_set_user_name(account, user_name); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Name\n"_RESET); + goto RETURN; + } + + ret = account_set_package_name(account, package_name); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Name\n"_RESET); + goto RETURN; + } + + ret = account_insert_to_db(account, &account_id); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Name\n"_RESET); + goto RETURN; + } + + ret = account_set_user_name(account, update_user_name); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Name\n"_RESET); + goto RETURN; + } + + ret = account_update_to_db_by_id(account, account_id); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Name\n"_RESET); + goto RETURN; + } + +RETURN: + + if(ACCOUNT_ERROR_NONE != ret) { + account_destroy(account); + printf(_BOLDCYAN"Fail to account_update_to_db_by_id_test\n"_RESET); + } + + return ret; +} + + + + +int account_insert_to_db_test(void) +{ + int account_id = -1; + int ret = ACCOUNT_ERROR_NONE; + + int i = -1; + + ret = account_set_user_name(account, user_name); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Name\n"_RESET); + goto RETURN; + } + + ret = account_set_package_name(account, package_name); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Package Name\n"_RESET); + goto RETURN; + } + + ret = account_set_display_name(account, display_name); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Display Name\n"_RESET); + goto RETURN; + } + + ret = account_set_icon_path(account, icon_path); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Icon Path\n"_RESET); + goto RETURN; + } + + ret = account_set_domain_name(account, domain_name); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Domain Name\n"_RESET); + goto RETURN; + } + + ret = account_set_email_address(account, email_address); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Email Address\n"_RESET); + goto RETURN; + } + + ret = account_set_source(account, source); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Source\n"_RESET); + goto RETURN; + } + + ret = account_set_custom(account, key, value); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Custom\n"_RESET); + goto RETURN; + } + + ret = account_set_capability(account, contact_capability, capability_state); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Capability\n"_RESET); + goto RETURN; + } + + ret = account_set_access_token(account, access_token); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Access Token\n"_RESET); + goto RETURN; + } + + for (i = 0; i < 5; i++){ + ret = account_set_user_text(account, i, user_text); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Text\n"_RESET); + goto RETURN; + } + } + + for (i = 0; i < 5; i++){ + ret = account_set_user_int(account, i, i*10); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account User Int\n"_RESET); + goto RETURN; + } + } + + ret = account_set_auth_type(account, auth_type); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Auth Type\n"_RESET); + goto RETURN; + } + + ret = account_set_secret(account, secrecy_state); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Secret\n"_RESET); + goto RETURN; + } + + ret = account_set_sync_support(account, sync_state); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to set Account Sync Support\n"_RESET); + goto RETURN; + } + + ret = account_insert_to_db(account, &account_id); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to Insert to DB\n"_RESET); + } + +RETURN: + if(ACCOUNT_ERROR_NONE != ret) { + account_destroy(account); + printf(_BOLDCYAN"Fail to account_insert_to_db_test\n"_RESET); + } + + return ret; +} + +void print_menu(void) +{ + sleep(1); + printf(_BOLDRED"\nAccount Test Menu\n"_RESET + _GREEN"1.Account Create\n"_RESET + _GREEN"2.Insert To DB\n"_RESET + _GREEN"3.Update To DB by ID\n"_RESET + _GREEN"4.Account Query by User Name\n"_RESET + _BOLDGREEN"d.Delete DB by User Name\n"_RESET + "q.Exit with Account Destroy:\n"); +} + +/* messaging_api_receive_test */ +int account_api_test(void) +{ + int ret = ACCOUNT_ERROR_NONE; + char a[10]; + + while (1) { + print_menu(); + ret = read(0, a, 10); + + switch (a[0]) { + case '1': + ret = account_create(&account); + if(ACCOUNT_ERROR_NONE != ret) { + printf(_BOLDCYAN"Fail to create account handle\n"_RESET); + } + break; + + case '2': + if (account != NULL) { + ret = account_insert_to_db_test(); + if(ACCOUNT_ERROR_NONE == ret) + printf(_BOLDCYAN"Succeed to Insert to DB Test\n"_RESET); + else + printf(_BOLDCYAN"Fail to Insert to DB Test\n"_RESET); + } else { + printf(_BOLDCYAN"Need to Create Account\n"_RESET); + } + break; + + case '3': + if (account != NULL) { + ret = account_update_to_db_by_id_test(); + if(ACCOUNT_ERROR_NONE == ret) + printf(_BOLDCYAN"Succeed to Update to DB Test\n"_RESET); + else + printf(_BOLDCYAN"Fail to Update to DB Test\n"_RESET); + } else { + printf(_BOLDCYAN"Need to Create Account\n"_RESET); + } + break; + + case '4': + if (account != NULL) { + ret = account_query_account_by_user_name(account_call_back, user_name, &account); + if(ACCOUNT_ERROR_NONE == ret) + printf(_BOLDCYAN"Succeed to Query Account by User Name Test\n"_RESET); + else + printf(_BOLDCYAN"Fail to Query Account by User Name Test\n"_RESET); + } else { + printf(_BOLDCYAN"Need to Create Account\n"_RESET); + } + break; + + case 'd': + /* disconnect from push service*/ + if (account != NULL) { + char* user_name_delete = "test_user_name"; + char* package_name_delete = "account-test"; + ret = account_delete_from_db_by_user_name(user_name_delete, package_name_delete); + if(ACCOUNT_ERROR_NONE == ret) + printf(_BOLDCYAN"Succeed to Delete DB by User Name Test\n"_RESET); + else + printf(_BOLDCYAN"Succeed to Delete DB by User Name Test\n"_RESET); + } else { + printf(_BOLDCYAN"Need to Create Account\n"_RESET); + } + break; + + case 'q': + account_destroy(account); + return 0; + + default: + break; + } + + } +} + +static gint sigterm_callback(void *data) +{ + account_destroy(account); + + printf(_BOLDCYAN"SigTerm\n"_RESET); + + g_main_loop_quit((GMainLoop*)data); + + return FALSE; +} + +void* StartApiTest() +{ + int ret = 0; + + ret = account_api_test(); + + if (ret) + printf(_BOLDCYAN"Test Finished\n"_RESET); + + printf(_BOLDCYAN"Bye\n"_RESET); + + g_main_loop_quit(global_loop); + return NULL; +} + +int main(int argc, char **argv) +{ + GMainLoop *loop; + + pthread_t thd; + + printf("\n"); + printf(_YELLOW"############################\n"); + printf(_YELLOW"######## Account Test ########\n"); + printf(_YELLOW"############################\n"_RESET); + + /* start transaction manager */ + if (pthread_create(&thd, NULL, StartApiTest, NULL) != 0) + return -1; + + /* mainloop of main thread */ + global_loop = loop = g_main_loop_new(NULL, FALSE); + + /* installing signal handlers */ + g_unix_signal_add_full(G_PRIORITY_HIGH, SIGINT, sigterm_callback, loop, NULL); + g_unix_signal_add_full(G_PRIORITY_HIGH, SIGTERM, sigterm_callback, loop, NULL); + + /* start application's main loop */ + g_main_loop_run(loop); + + /* cleanup after mainloop */ + g_main_loop_unref(loop); + + return 0; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..b145373 --- /dev/null +++ b/src/util.h @@ -0,0 +1,59 @@ +/* +* Account Test Client +* +* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd All Rights Reserved +* +* Contact : Sunghyuk Lee +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __TIZEN_ACCOUNT_TEST_UTIL_H__ +#define __TIZEN_ACCOUNT_TEST_UTIL_H__ + +#include + +#undef LOG_TAG +#define LOG_TAG "PUSHTEST" + +#define _RESET "\033[0m" +#define _BLACK "\033[30m" /* Black */ +#define _RED "\033[31m" /* Red */ +#define _GREEN "\033[32m" /* Green */ +#define _YELLOW "\033[33m" /* Yellow */ +#define _BLUE "\033[34m" /* Blue */ +#define _PURPLE "\033[35m" /* Purple */ +#define _CYAN "\033[36m" /* Cyan */ +#define _WHITE "\033[37m" /* White */ +#define _BOLDBLACK "\033[1m\033[30m" /* Bold Black */ +#define _BOLDRED "\033[1m\033[31m" /* Bold Red */ +#define _BOLDGREEN "\033[1m\033[32m" /* Bold Green */ +#define _BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ +#define _BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ +#define _BOLDPURPLE "\033[1m\033[35m" /* Bold Purple */ +#define _BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ +#define _BOLDWHITE "\033[1m\033[37m" /* Bold White */ + +#define _I(fmt, arg...) do { LOGI(fmt, ##arg); } while (0) +#define _W(fmt, arg...) do { LOGW(fmt, ##arg); } while (0) +#define _E(fmt, arg...) do { LOGE(fmt, ##arg); } while (0) +#define _D(fmt, arg...) do { LOGD(fmt, ##arg); } while (0) +#define _SE(fmt, arg...) do { LOGE(fmt, ##arg); } while (0) +#define _SD(fmt, arg...) do { LOGD(fmt, ##arg); } while (0) +#define _SW(fmt, arg...) do { LOGW(fmt, ##arg); } while (0) + +#define _free(p) do { if (p) { free(p); p = NULL; } } while (0) +#define _FUNC_ENTER do { LOGD("start"); } while (0) +#define _FUNC_LEAVE do { LOGD("end"); } while (0) + +#endif /* __TIZEN_ACCOUNT_TEST_UTIL_H__ */ -- 2.7.4