[unit] Make more unit tests independent on system config. JB#42956

System gbinder config shouldn't break unit tests.
This commit is contained in:
Slava Monich 2024-07-14 06:07:49 +03:00
parent 545f5bc28d
commit c6b09a10d4
4 changed files with 78 additions and 34 deletions

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2018-2024 Slava Monich <slava@monich.com>
* Copyright (C) 2018-2022 Jolla Ltd. * Copyright (C) 2018-2022 Jolla Ltd.
* Copyright (C) 2018-2022 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of BSD license as follows: * You may use this file under the terms of BSD license as follows:
* *
@ -51,6 +51,7 @@
#include <sys/types.h> #include <sys/types.h>
static TestOpt test_opt; static TestOpt test_opt;
static const char TMP_DIR_TEMPLATE[] = "gbinder-test-ipc-XXXXXX";
static static
gboolean gboolean
@ -1346,6 +1347,9 @@ test_cancel_on_exit(
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
TestConfig test_config;
int result;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS; G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
g_type_init(); g_type_init();
G_GNUC_END_IGNORE_DEPRECATIONS; G_GNUC_END_IGNORE_DEPRECATIONS;
@ -1375,7 +1379,10 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_("drop_remote_refs"), test_drop_remote_refs); g_test_add_func(TEST_("drop_remote_refs"), test_drop_remote_refs);
g_test_add_func(TEST_("cancel_on_exit"), test_cancel_on_exit); g_test_add_func(TEST_("cancel_on_exit"), test_cancel_on_exit);
test_init(&test_opt, argc, argv); test_init(&test_opt, argc, argv);
return g_test_run(); test_config_init(&test_config, TMP_DIR_TEMPLATE);
result = g_test_run();
test_config_cleanup(&test_config);
return result;
} }
/* /*

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2018-2024 Slava Monich <slava@monich.com>
* Copyright (C) 2018-2022 Jolla Ltd. * Copyright (C) 2018-2022 Jolla Ltd.
* Copyright (C) 2018-2022 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of BSD license as follows: * You may use this file under the terms of BSD license as follows:
* *
@ -46,6 +46,7 @@
#include <gutil_intarray.h> #include <gutil_intarray.h>
static TestOpt test_opt; static TestOpt test_opt;
static const char TMP_DIR_TEMPLATE[] = "gbinder-test-local-reply-XXXXXX";
static static
void void
@ -538,6 +539,9 @@ test_remote_reply(
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
TestConfig test_config;
int result;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS; G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
g_type_init(); g_type_init();
G_GNUC_END_IGNORE_DEPRECATIONS; G_GNUC_END_IGNORE_DEPRECATIONS;
@ -558,7 +562,10 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_PREFIX "remote_object", test_remote_object); g_test_add_func(TEST_PREFIX "remote_object", test_remote_object);
g_test_add_func(TEST_PREFIX "remote_reply", test_remote_reply); g_test_add_func(TEST_PREFIX "remote_reply", test_remote_reply);
test_init(&test_opt, argc, argv); test_init(&test_opt, argc, argv);
return g_test_run(); test_config_init(&test_config, TMP_DIR_TEMPLATE);
result = g_test_run();
test_config_cleanup(&test_config);
return result;
} }
/* /*

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2018-2024 Slava Monich <slava@monich.com>
* Copyright (C) 2018-2022 Jolla Ltd. * Copyright (C) 2018-2022 Jolla Ltd.
* Copyright (C) 2018-2022 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of BSD license as follows: * You may use this file under the terms of BSD license as follows:
* *
@ -44,6 +44,7 @@
#include <gutil_intarray.h> #include <gutil_intarray.h>
static TestOpt test_opt; static TestOpt test_opt;
static const char TMP_DIR_TEMPLATE[] = "gbinder-test-local-request-XXXXXX";
static static
void void
@ -600,6 +601,9 @@ test_remote_request_obj(
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
TestConfig test_config;
int result;
g_test_init(&argc, &argv, NULL); g_test_init(&argc, &argv, NULL);
g_test_add_func(TEST_PREFIX "null", test_null); g_test_add_func(TEST_PREFIX "null", test_null);
g_test_add_func(TEST_PREFIX "cleanup", test_cleanup); g_test_add_func(TEST_PREFIX "cleanup", test_cleanup);
@ -618,7 +622,10 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_PREFIX "remote_request", test_remote_request); g_test_add_func(TEST_PREFIX "remote_request", test_remote_request);
g_test_add_func(TEST_PREFIX "remote_request_obj", test_remote_request_obj); g_test_add_func(TEST_PREFIX "remote_request_obj", test_remote_request_obj);
test_init(&test_opt, argc, argv); test_init(&test_opt, argc, argv);
return g_test_run(); test_config_init(&test_config, TMP_DIR_TEMPLATE);
result = g_test_run();
test_config_cleanup(&test_config);
return result;
} }
/* /*

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2018-2023 Slava Monich <slava@monich.com> * Copyright (C) 2018-2024 Slava Monich <slava@monich.com>
* Copyright (C) 2018-2022 Jolla Ltd. * Copyright (C) 2018-2022 Jolla Ltd.
* *
* You may use this file under the terms of BSD license as follows: * You may use this file under the terms of BSD license as follows:
@ -449,10 +449,13 @@ test_invalid(
{ {
int status = 0; int status = 0;
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL); GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
TestConfig config;
gulong id = 0; gulong id = 0;
test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
test_setup_ping(ipc); test_setup_ping(ipc);
g_assert(!gbinder_servicemanager_new2(GBINDER_DEFAULT_HWBINDER, "a", NULL)); g_assert(!gbinder_servicemanager_new2(GBINDER_DEFAULT_HWBINDER, "a", NULL));
sm = gbinder_servicemanager_new(dev); sm = gbinder_servicemanager_new(dev);
@ -484,6 +487,7 @@ test_invalid(
gbinder_servicemanager_unref(sm); gbinder_servicemanager_unref(sm);
gbinder_ipc_unref(ipc); gbinder_ipc_unref(ipc);
test_binder_exit_wait(&test_opt, NULL); test_binder_exit_wait(&test_opt, NULL);
test_config_cleanup(&config);
} }
/*==========================================================================* /*==========================================================================*
@ -496,12 +500,13 @@ test_basic(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL); GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
GBinderLocalObject* obj; GBinderLocalObject* obj;
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_servicemanager_new(dev); sm = gbinder_servicemanager_new(dev);
g_assert(sm); g_assert(sm);
@ -530,11 +535,12 @@ test_legacy(
{ {
const char* otherdev = "/dev/otherbinder"; const char* otherdev = "/dev/otherbinder";
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL); GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_hwservicemanager_new(dev); sm = gbinder_hwservicemanager_new(dev);
g_assert(TEST_IS_HWSERVICEMANAGER(sm)); g_assert(TEST_IS_HWSERVICEMANAGER(sm));
@ -649,12 +655,14 @@ test_not_present(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL); GBinderIpc* ipc;
const int fd = gbinder_driver_fd(ipc->driver);
GBinderServiceManager* sm; GBinderServiceManager* sm;
TestConfig config; TestConfig config;
int fd;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
fd = gbinder_driver_fd(ipc->driver);
/* This makes presence detection PING fail */ /* This makes presence detection PING fail */
test_binder_br_reply_status(fd, THIS_THREAD, -1); test_binder_br_reply_status(fd, THIS_THREAD, -1);
@ -679,16 +687,17 @@ test_wait(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL);
const int fd = gbinder_driver_fd(ipc->driver);
const glong forever = (test_opt.flags & TEST_FLAG_DEBUG) ? const glong forever = (test_opt.flags & TEST_FLAG_DEBUG) ?
(TEST_TIMEOUT_SEC * 1000) : -1; (TEST_TIMEOUT_SEC * 1000) : -1;
GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
gulong id; gulong id;
int count = 0; int fd, count = 0;
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
fd = gbinder_driver_fd(ipc->driver);
/* This makes presence detection PING fail */ /* This makes presence detection PING fail */
test_binder_br_reply_status(fd, THIS_THREAD, -1); test_binder_br_reply_status(fd, THIS_THREAD, -1);
@ -734,14 +743,15 @@ test_wait_long(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL); GBinderIpc* ipc;
const int fd = gbinder_driver_fd(ipc->driver);
GBinderServiceManager* sm; GBinderServiceManager* sm;
gulong id; gulong id;
int count = 0; int fd, count = 0;
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
fd = gbinder_driver_fd(ipc->driver);
/* This makes presence detection PING fail */ /* This makes presence detection PING fail */
test_binder_br_reply_status(fd, THIS_THREAD, -1); test_binder_br_reply_status(fd, THIS_THREAD, -1);
@ -784,15 +794,16 @@ test_wait_async(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL);
const int fd = gbinder_driver_fd(ipc->driver);
GMainLoop* loop = g_main_loop_new(NULL, FALSE); GMainLoop* loop = g_main_loop_new(NULL, FALSE);
GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
gulong id[2]; gulong id[2];
int count = 0; int fd, count = 0;
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
fd = gbinder_driver_fd(ipc->driver);
/* This makes presence detection PING fail */ /* This makes presence detection PING fail */
test_binder_br_reply_status(fd, THIS_THREAD, -1); test_binder_br_reply_status(fd, THIS_THREAD, -1);
@ -832,15 +843,17 @@ void
test_death_run() test_death_run()
{ {
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL);
const int fd = gbinder_driver_fd(ipc->driver);
GMainLoop* loop = g_main_loop_new(NULL, FALSE); GMainLoop* loop = g_main_loop_new(NULL, FALSE);
GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
gulong id[3]; gulong id[3];
int count = 0, reg_count = 0; int fd, count = 0, reg_count = 0;
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
fd = gbinder_driver_fd(ipc->driver);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_servicemanager_new(dev); sm = gbinder_servicemanager_new(dev);
g_assert(sm); g_assert(sm);
@ -909,16 +922,18 @@ test_reanimate(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_HWBINDER; const char* dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL);
const int fd = gbinder_driver_fd(ipc->driver);
GMainLoop* loop = g_main_loop_new(NULL, FALSE); GMainLoop* loop = g_main_loop_new(NULL, FALSE);
GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
gulong id[3]; gulong id[3];
int count = 0, reg_count = 0; int fd, count = 0, reg_count = 0;
TestConfig config; TestConfig config;
/* Create live service manager */ /* Create live service manager */
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
fd = gbinder_driver_fd(ipc->driver);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_servicemanager_new(dev); sm = gbinder_servicemanager_new(dev);
g_assert(sm); g_assert(sm);
@ -965,9 +980,9 @@ test_reuse(
const char* binder_dev = GBINDER_DEFAULT_BINDER; const char* binder_dev = GBINDER_DEFAULT_BINDER;
const char* vndbinder_dev = "/dev/vpnbinder"; const char* vndbinder_dev = "/dev/vpnbinder";
const char* hwbinder_dev = GBINDER_DEFAULT_HWBINDER; const char* hwbinder_dev = GBINDER_DEFAULT_HWBINDER;
GBinderIpc* binder_ipc = gbinder_ipc_new(binder_dev, NULL); GBinderIpc* binder_ipc;
GBinderIpc* vndbinder_ipc = gbinder_ipc_new(vndbinder_dev, NULL); GBinderIpc* vndbinder_ipc;
GBinderIpc* hwbinder_ipc = gbinder_ipc_new(hwbinder_dev, NULL); GBinderIpc* hwbinder_ipc;
GBinderServiceManager* m1; GBinderServiceManager* m1;
GBinderServiceManager* m2; GBinderServiceManager* m2;
GBinderServiceManager* vnd1; GBinderServiceManager* vnd1;
@ -977,6 +992,10 @@ test_reuse(
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
binder_ipc = gbinder_ipc_new(binder_dev, NULL);
vndbinder_ipc = gbinder_ipc_new(vndbinder_dev, NULL);
hwbinder_ipc = gbinder_ipc_new(hwbinder_dev, NULL);
test_setup_ping(binder_ipc); test_setup_ping(binder_ipc);
test_setup_ping(vndbinder_ipc); test_setup_ping(vndbinder_ipc);
test_setup_ping(hwbinder_ipc); test_setup_ping(hwbinder_ipc);
@ -1023,7 +1042,7 @@ test_notify_type(
GType t, GType t,
const char* dev) const char* dev)
{ {
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL); GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
TestHwServiceManager* test; TestHwServiceManager* test;
const char* name = "foo"; const char* name = "foo";
@ -1032,6 +1051,7 @@ test_notify_type(
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_servicemanager_new_with_type(t, NULL, NULL); sm = gbinder_servicemanager_new_with_type(t, NULL, NULL);
test = TEST_SERVICEMANAGER2(sm, t); test = TEST_SERVICEMANAGER2(sm, t);
@ -1094,8 +1114,8 @@ test_list(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_BINDER; const char* dev = GBINDER_DEFAULT_BINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL);
GMainLoop* loop = g_main_loop_new(NULL, FALSE); GMainLoop* loop = g_main_loop_new(NULL, FALSE);
GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
TestHwServiceManager* test; TestHwServiceManager* test;
char** list; char** list;
@ -1103,6 +1123,7 @@ test_list(
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_servicemanager_new(dev); sm = gbinder_servicemanager_new(dev);
test = TEST_SERVICEMANAGER(sm); test = TEST_SERVICEMANAGER(sm);
@ -1146,8 +1167,8 @@ test_get(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_BINDER; const char* dev = GBINDER_DEFAULT_BINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL);
GMainLoop* loop = g_main_loop_new(NULL, FALSE); GMainLoop* loop = g_main_loop_new(NULL, FALSE);
GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
TestHwServiceManager* test; TestHwServiceManager* test;
int status = -1; int status = -1;
@ -1156,6 +1177,7 @@ test_get(
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_servicemanager_new(dev); sm = gbinder_servicemanager_new(dev);
test = TEST_SERVICEMANAGER(sm); test = TEST_SERVICEMANAGER(sm);
@ -1211,8 +1233,8 @@ test_add(
void) void)
{ {
const char* dev = GBINDER_DEFAULT_BINDER; const char* dev = GBINDER_DEFAULT_BINDER;
GBinderIpc* ipc = gbinder_ipc_new(dev, NULL);
GMainLoop* loop = g_main_loop_new(NULL, FALSE); GMainLoop* loop = g_main_loop_new(NULL, FALSE);
GBinderIpc* ipc;
GBinderServiceManager* sm; GBinderServiceManager* sm;
TestHwServiceManager* test; TestHwServiceManager* test;
GBinderLocalObject* obj; GBinderLocalObject* obj;
@ -1220,6 +1242,7 @@ test_add(
TestConfig config; TestConfig config;
test_config_init(&config, TMP_DIR_TEMPLATE); test_config_init(&config, TMP_DIR_TEMPLATE);
ipc = gbinder_ipc_new(dev, NULL);
test_setup_ping(ipc); test_setup_ping(ipc);
sm = gbinder_servicemanager_new(dev); sm = gbinder_servicemanager_new(dev);
test = TEST_SERVICEMANAGER(sm); test = TEST_SERVICEMANAGER(sm);