Handle mocked GDBusConnection a bit more carefully 50/316550/2 tizen_8.0
authorMateusz Majewski <m.majewski2@samsung.com>
Thu, 22 Aug 2024 12:23:32 +0000 (14:23 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Thu, 22 Aug 2024 13:05:27 +0000 (15:05 +0200)
Some of the tested functions call g_bus_get_sync twice and unreference
the result both times. It seems that without explicit mocking, the
second unreference sometimes (not consistently) results in a segfault.

Change-Id: Ib76e4ab0b36638a4001ac89e91658f4568f621e4

tests/unit_tests/src/test_eventsystem.cc

index 2f67f11fa140494e49b1d4c0e7e81e6e59975df7..48d414c692b803d806f2ae2fde27586704fd0ce4 100644 (file)
@@ -176,6 +176,10 @@ TEST_F(EventSystemTest, eventsystem_request_sending_system_event) {
 
 TEST_F(EventSystemTest, eventsystem_register_application_event) {
   GDBusProxy* _proxy = (GDBusProxy*)g_object_new(G_TYPE_OBJECT, nullptr);
+  GDBusConnection* _conn = (GDBusConnection*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+  EXPECT_CALL(GetMock<GioMock>(), g_bus_get_sync(_, _, _))
+        .WillRepeatedly(Invoke([&](GBusType t, GCancellable* c, GError** e) { return g_object_ref(_conn); }));
 
   EXPECT_CALL(GetMock<GioMock>(),
       g_dbus_is_interface_name(_)).
@@ -210,10 +214,16 @@ TEST_F(EventSystemTest, eventsystem_register_application_event) {
   int ret = eventsystem_register_application_event("tizen.system.event.test",
       &id, &type, system_callback, NULL);
   EXPECT_EQ(ret, ES_R_OK);
+
+  g_object_unref(_conn);
 }
 
 TEST_F(EventSystemTest, eventsystem_unregister_application_event) {
  GDBusProxy* _proxy = (GDBusProxy*)g_object_new(G_TYPE_OBJECT, nullptr);
+  GDBusConnection* _conn = (GDBusConnection*)g_object_new(G_TYPE_OBJECT, nullptr);
+
+  EXPECT_CALL(GetMock<GioMock>(), g_bus_get_sync(_, _, _))
+        .WillRepeatedly(Invoke([&](GBusType t, GCancellable* c, GError** e) { return g_object_ref(_conn); }));
 
   EXPECT_CALL(GetMock<GioMock>(),
       g_dbus_is_interface_name(_)).
@@ -252,6 +262,8 @@ TEST_F(EventSystemTest, eventsystem_unregister_application_event) {
 
   ret = eventsystem_unregister_application_event(id);
   EXPECT_EQ(ret, ES_R_OK);
+
+  g_object_unref(_conn);
 }
 
 TEST_F(EventSystemTest, eventsystem_keep_last_event_data) {