Adapt clientExample to char * 73/277573/6
authorMateusz Majewski <m.majewski2@samsung.com>
Fri, 8 Jul 2022 09:21:36 +0000 (11:21 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Fri, 8 Jul 2022 12:00:57 +0000 (12:00 +0000)
Change-Id: Ic68fc382f0d1e91f062ff6d40ac7885f1a864fdb

CMakeLists.txt
clientExample/CMakeLists.txt
clientExample/app/main.cpp
packaging/sessiond.spec

index 7bd6eea..79592d3 100644 (file)
@@ -13,7 +13,7 @@ set(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 add_subdirectory(common)
 add_subdirectory(libsessiond)
 add_subdirectory(sessiond)
-#add_subdirectory(clientExample)
+add_subdirectory(clientExample)
 
 option(ENABLE_TARGET_TESTS "On target tests" ON)
 if(ENABLE_TARGET_TESTS)
index a5c20fd..2b729b3 100644 (file)
@@ -8,5 +8,6 @@ find_package(PkgConfig)
 pkg_check_modules(DEPS REQUIRED IMPORTED_TARGET glib-2.0 gio-2.0 gobject-2.0)
 
 add_executable(subsession-client-example app/main.cpp)
+target_compile_features(subsession-client-example PUBLIC cxx_std_20)
 target_link_libraries(subsession-client-example PRIVATE PkgConfig::DEPS libsessiond)
 install(TARGETS subsession-client-example)
index 7392c81..c82dff8 100644 (file)
@@ -1,3 +1,4 @@
+#include <array>
 #include <iostream>
 #include <gio/gio.h>
 #include <cassert>
@@ -6,9 +7,18 @@
 
 static constexpr int SESSION_UID = 5001; // default Tizen's 'owner' uid
 
-static constexpr int firstUser = 1;  // N.B. Starting from 1, as user number 0 is reserved
-static constexpr int lastUser = 10;
-static constexpr int noOfUsers = lastUser - firstUser + 1;
+static constexpr std::array usernames = {
+       "papa_smurf",
+       "smurfette",
+       "baby_smurf",
+       "brainy_smurf",
+       "clumsy_smurf",
+       "greedy_smurf",
+       "grouchy_smurf",
+       "hefty_smurf",
+       "jokey_smurf",
+       "lazy_smurf",
+};
 
 static constexpr int CALLBACK_DATA_MAGIC = 0xDEAD50UL; // arbitrary; different from any SUBSESSION_ERROR calls
 
@@ -22,17 +32,15 @@ typedef struct {
 
 typedef struct {
        int session_uid;
-       int user_id;
+       subsession_user_t user_id;
        uint64_t switch_id;
        int callback_result;
        int callback_reference;
-       int prev_user_id;
+       subsession_user_t prev_user_id;
        subsession_event_type_e event;
 } test_user_data_cb_t;
 
-// N.B. There are `noOfUsers` users added, but we're treating
-// the array as 1-based, so we need to add 1 more element.
-volatile test_user_data userD[noOfUsers + 1];
+volatile test_user_data userD[usernames.size()];
 
 int callback_pending_reference = 0;
 
@@ -47,8 +55,8 @@ void test_subsession_switch_user_completion_callback(subsession_event_info info,
        test_user_data_cb_t *user_data = (test_user_data_cb_t *)cb_data;
        user_data->session_uid = info.session_uid;
        user_data->switch_id = info.switch_user.switch_id;
-       user_data->user_id = info.switch_user.next_user;
-       user_data->prev_user_id = info.switch_user.prev_user;
+       memcpy(user_data->user_id, info.switch_user.next_user, SUBSESSION_USER_MAXLEN);
+       memcpy(user_data->prev_user_id, info.switch_user.prev_user, SUBSESSION_USER_MAXLEN);
        user_data->callback_result = 0;
 }
 
@@ -64,7 +72,7 @@ gboolean callback_pending(gpointer data)
 
        // N.B. There are `noOfUsers` add and remove operations. Each add/remove
        // increments the value of the `callback_pending_reference` variable.
-       if (ctrl_value >= noOfUsers * 2) {
+       if ((unsigned)ctrl_value >= usernames.size() * 2) {
                g_main_loop_quit(loop);
                g_main_loop_unref(loop);
        }
@@ -124,7 +132,7 @@ void test_reply_switchuser_callback(int result, void *cb_data)
        g_mutex_unlock(&mutex);
 }
 
-bool switch_user_test(int user_id)
+bool switch_user_test(const subsession_user_t user_id)
 {
        volatile test_user_data test_switch_ud;
        test_switch_ud.callback_result = CALLBACK_DATA_MAGIC;
@@ -153,11 +161,11 @@ bool switch_user_test(int user_id)
 
        g_mutex_unlock(&mutex);
 
-       int new_user;
-       int r = subsession_get_current_user(SESSION_UID, &new_user);
+       subsession_user_t new_user;
+       int r = subsession_get_current_user(SESSION_UID, new_user);
        if (r != SUBSESSION_ERROR_NONE)
                return false;
-       return new_user == user_id;
+       return strncmp(new_user, user_id, SUBSESSION_USER_MAXLEN) == 0;
 }
 
 bool switch_to_each_user_to_generate_events()
@@ -166,8 +174,8 @@ bool switch_to_each_user_to_generate_events()
         * to a single specific user; here we are running the gamut
         * just to generate a bunch of events to show the handlers. */
 
-       for (int i = firstUser; i <= lastUser; ++i) {
-               if (!switch_user_test(i)) {
+       for (auto user : usernames) {
+               if (!switch_user_test(user)) {
                        printf("Failed to switch user\n");
                        return false;
                }
@@ -215,11 +223,11 @@ int main(int argc, char *argv[])
        ///===================================///
        printf("Test program start\nCreating test users...");
 
-       for (int i = firstUser; i <= lastUser; ++i)
+       for (size_t i = 0; i < usernames.size(); ++i)
        {
                g_mutex_lock(&mutex);
                userD[i].callback_result = CALLBACK_DATA_MAGIC;
-               int add_user_res = subsession_add_user(SESSION_UID, i,
+               int add_user_res = subsession_add_user(SESSION_UID, usernames[i],
                        test_reply_adduser_callback, (void *)&userD[i]);
 
                if (add_user_res != SUBSESSION_ERROR_NONE)
@@ -238,30 +246,25 @@ int main(int argc, char *argv[])
        green_print("done");
 
        int registered_users;
-       int *userlist;
+       subsession_user_t *userlist;
        ///===================================///
-       int r = subsession_get_user_list(SESSION_UID, (int **)&userlist, &registered_users);
+       int r = subsession_get_user_list(SESSION_UID, &userlist, &registered_users);
        if (r != SUBSESSION_ERROR_NONE) {
                printf("Error getting user list: %d\n", r);
                return EXIT_FAILURE;
        }
 
-       printf("No of registered users [%d/%d]...", registered_users, noOfUsers);
-       if (noOfUsers == registered_users)
+       printf("No of registered users [%d/%zu]...", registered_users, usernames.size());
+       if (usernames.size() == registered_users)
                green_print("ok");
        else
        {
-               printf("Failed to register some users (%d expected, got %d)\n", noOfUsers, registered_users);
+               printf("Failed to register some users (%zu expected, got %d)\n", usernames.size(), registered_users);
                return EXIT_FAILURE;
        }
 
-       printf("Lp. ");
        for (int i = 0; i < registered_users; ++i)
-               printf(" %2d ", i);
-       printf("\nId. ");
-       for (int i = 0; i < registered_users; ++i)
-               printf(" %2d ", userlist[i]);
-       printf("\n");
+               printf("%d -> %s\n", i, userlist[i]);
        ///===================================///
        printf("Switching users test...");
 
@@ -295,19 +298,18 @@ int main(int argc, char *argv[])
 
        ///======================================///
        printf("Removing users...");
-       if (!switch_user_test(0)) {
-               printf("Error setting user to 0\n");
+       if (!switch_user_test("")) {
+               printf("Error setting user to starting\n");
                return EXIT_FAILURE;
        }
-       for (int i = firstUser; i <= lastUser; ++i)
-       {
+       for (auto user : usernames) {
                test_user_data test_remove_ud;
                test_remove_ud.callback_result = -1;
-               int remove_user_res = subsession_remove_user(SESSION_UID, i,
+               int remove_user_res = subsession_remove_user(SESSION_UID, user,
                        test_reply_removeuser_callback, (void *)&test_remove_ud);
                if (remove_user_res != SUBSESSION_ERROR_NONE)
                {
-                       printf("removing user %d failed code: %d\n", i,remove_user_res);
+                       printf("removing user %s failed code: %d\n", user, remove_user_res);
                        return EXIT_FAILURE;
                }
        }
index 002c743..d585d4e 100644 (file)
@@ -33,10 +33,10 @@ Summary:    Subsession support library - integration tests
 Group:      Development/Libraries
 %description -n libsessiond-tests
 
-#%package -n subsession-client-example
-#Summary:    Example program using libsessiond API
-#Group:      Development/Libraries
-#%description -n subsession-client-example
+%package -n subsession-client-example
+Summary:    Example program using libsessiond API
+Group:      Development/Libraries
+%description -n subsession-client-example
 
 %prep
 %setup -q
@@ -92,7 +92,7 @@ popd
 %license LICENSE.MIT
 %{_bindir}/test_*
 
-#%files -n subsession-client-example
-#%manifest sessiond.manifest
-#%license LICENSE.MIT
-#%{_bindir}/subsession-client-example
+%files -n subsession-client-example
+%manifest sessiond.manifest
+%license LICENSE.MIT
+%{_bindir}/subsession-client-example