From 085a28368ba8df42b437b5e2844fbd35909fc267 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Thu, 17 Jun 2021 16:34:01 +0900 Subject: [PATCH] unittest: Replace system command with dbus signal system() command causes the security issue. So that fix issue to replace system command with dbus signal. Change-Id: I83843697c9b64bd1c14fc54d1e9773dcfbdc5a3b Signed-off-by: Chanwoo Choi --- unittest/pass-unittests.cpp | 9 +++++--- unittest/power-haltests.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/unittest/pass-unittests.cpp b/unittest/pass-unittests.cpp index 9dd723e..5711c01 100644 --- a/unittest/pass-unittests.cpp +++ b/unittest/pass-unittests.cpp @@ -228,13 +228,16 @@ TEST_F(PowerMgntTest, RestartsPowerMgntService) { gint32 ret; - ret = system("/bin/systemctl start pass.service"); + ret = pass_test_method_call(DBUS_CORE_PATH, DBUS_CORE_INTERFACE, + "start", NULL); ASSERT_EQ(ret, 0) << "PassServiceStart Failed"; - ret = system("/bin/systemctl stop pass.service"); + ret = pass_test_method_call(DBUS_CORE_PATH, DBUS_CORE_INTERFACE, + "stop", NULL); ASSERT_EQ(ret, 0) << "PassServiceStop Failed"; - ret = system("/bin/systemctl start pass.service"); + ret = pass_test_method_call(DBUS_CORE_PATH, DBUS_CORE_INTERFACE, + "start", NULL); ASSERT_EQ(ret, 0) << "PassServiceStart Failed"; } diff --git a/unittest/power-haltests.cpp b/unittest/power-haltests.cpp index 47b7a30..8e475e5 100644 --- a/unittest/power-haltests.cpp +++ b/unittest/power-haltests.cpp @@ -17,7 +17,9 @@ #include #include +#include #include +#include extern "C" { #include "pass-hal.h" @@ -53,6 +55,54 @@ static int haltest_is_failed(struct pass_resource *res, int ret) return 0; } +static gint32 pass_test_method_call(const gchar *path, const gchar *intf, + const gchar *method, GVariant *body) +{ + const gchar *type; + GVariant *ret; + GError *err = NULL; + GDBusMessage *msg, *reply; + GDBusConnection *conn; + gint32 r; + + conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err); + if (err) + return -1; + + msg = g_dbus_message_new_method_call(DBUS_PASS_BUS_NAME, path, + intf, method); + if (!msg) + return -1; + + if (body) + g_dbus_message_set_body(msg, body); + + reply = g_dbus_connection_send_message_with_reply_sync(conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, + G_MAXINT, NULL, NULL, &err); + if (err) { + g_object_unref(msg); + g_clear_error(&err); + return -1; + } + + ret = g_variant_get_child_value(g_dbus_message_get_body(reply), 0); + type = g_variant_get_type_string(ret); + if (type[0] == 'i') + r = g_variant_get_int32(ret); + else + r = -1; + + g_variant_unref(ret); + + g_dbus_connection_flush(conn, NULL, NULL, NULL); + g_object_unref(msg); + g_object_unref(reply); + g_clear_error(&err); + + return r; +} + TEST_F(PowerHaltest, GetResourceConfig_HandlesValidInput) { int ret = 0; @@ -60,7 +110,8 @@ TEST_F(PowerHaltest, GetResourceConfig_HandlesValidInput) char path[] = "/hal/etc/pass/pass.conf"; /* Stop PASS daemon before HAL testing */ - ret = system("/bin/systemctl stop pass.service"); + ret = pass_test_method_call(DBUS_CORE_PATH, DBUS_CORE_INTERFACE, + "stop", NULL); ASSERT_EQ(ret, 0) << "PassServiceStop Failed"; ret = pass_parser_get_resource_config(&g_pass, path); @@ -497,7 +548,8 @@ TEST_F(PowerHaltest, PutResourceConfig_HandlesValidInput) } /* Restart PASS daemon before HAL testing */ - ret = system("/bin/systemctl start pass.service"); + ret = pass_test_method_call(DBUS_CORE_PATH, DBUS_CORE_INTERFACE, + "start", NULL); ASSERT_EQ(ret, 0) << "PassServiceStart Failed"; } -- 2.7.4