From 8739185f873a087b5e8581a6200750945a6f3ef6 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 21 Jul 2017 19:15:05 +0100 Subject: [PATCH 2/8] tests: Use dbus_clear_connection etc. in a couple of tests This is just enough to demonstrate that they work - I'm deliberately not doing a mass change throughout all tests, and we should definitely not rush to introduce these into production code, because it would hinder cherry-picking and merging fixes between branches. However, new code on master can use them freely. Signed-off-by: Simon McVittie --- test/dbus-daemon.c | 169 +++++++++++++++++++++++------------------------------ test/loopback.c | 30 ++++------ 2 files changed, 82 insertions(+), 117 deletions(-) diff --git a/test/dbus-daemon.c b/test/dbus-daemon.c index 17405678..45619e2d 100644 --- a/test/dbus-daemon.c +++ b/test/dbus-daemon.c @@ -125,7 +125,7 @@ echo_filter (DBusConnection *connection, if (!dbus_connection_send (connection, reply, NULL)) g_error ("OOM"); - dbus_message_unref (reply); + dbus_clear_message (&reply); return DBUS_HANDLER_RESULT_HANDLED; } @@ -287,8 +287,8 @@ test_echo (Fixture *f, NULL)) g_error ("OOM"); - dbus_pending_call_unref (pc); - dbus_message_unref (m); + dbus_clear_pending_call (&pc); + dbus_clear_message (&m); } while (received < count) @@ -341,8 +341,8 @@ test_no_reply (Fixture *f, &reply, NULL)) g_error ("OOM"); - dbus_pending_call_unref (pc); - dbus_message_unref (m); + dbus_clear_pending_call (&pc); + dbus_clear_message (&m); if (mode == DISCONNECT) { @@ -351,8 +351,7 @@ test_no_reply (Fixture *f, dbus_connection_remove_filter (f->right_conn, echo_filter, f); dbus_connection_close (f->right_conn); - dbus_connection_unref (f->right_conn); - f->right_conn = NULL; + dbus_clear_connection (&f->right_conn); } while (reply == NULL) @@ -373,7 +372,7 @@ test_no_reply (Fixture *f, g_assert_cmpstr (f->e.message, ==, "Message did not receive a reply (timeout by message bus)"); - dbus_message_unref (reply); + dbus_clear_message (&reply); } static void @@ -408,8 +407,7 @@ test_creds (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -534,8 +532,8 @@ test_creds (Fixture *f, g_assert (seen & SEEN_WINDOWS_SID); #endif - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -562,8 +560,7 @@ test_processid (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -607,8 +604,8 @@ test_processid (Fixture *f, g_error ("Unexpected error: %s: %s", error.name, error.message); } - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -637,8 +634,7 @@ test_canonical_path_uae (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -653,9 +649,8 @@ test_canonical_path_uae (Fixture *f, g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_METHOD_RETURN); - dbus_message_unref (m); - dbus_pending_call_unref (pc); - pc = NULL; + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); /* Now try with the wrong object path */ m = dbus_message_new_method_call (DBUS_SERVICE_DBUS, @@ -677,8 +672,7 @@ test_canonical_path_uae (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -695,8 +689,8 @@ test_canonical_path_uae (Fixture *f, DBUS_ERROR_ACCESS_DENIED); g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -733,13 +727,11 @@ test_max_connections (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_LIMITS_EXCEEDED); if (failing_conn != NULL) - { - dbus_connection_close (failing_conn); - dbus_connection_unref (failing_conn); - } + dbus_connection_close (failing_conn); + dbus_clear_connection (&failing_conn); dbus_connection_close (third_conn); - dbus_connection_unref (third_conn); + dbus_clear_connection (&third_conn); } static void @@ -827,8 +819,8 @@ test_max_replies_per_connection (Fixture *f, if (!dbus_connection_send (f->right_conn, reply, NULL)) g_error ("OOM"); - dbus_message_unref (reply); - dbus_message_unref (m); + dbus_clear_message (&m); + dbus_clear_message (&reply); } /* Wait for all 5 replies to come in. */ @@ -844,7 +836,7 @@ test_max_replies_per_connection (Fixture *f, if (dbus_set_error_from_message (&error, m)) g_error ("Unexpected error: %s: %s", error.name, error.message); - dbus_message_unref (m); + dbus_clear_message (&m); } /* The last two failed. */ @@ -858,7 +850,7 @@ test_max_replies_per_connection (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_LIMITS_EXCEEDED); dbus_error_free (&error); - dbus_message_unref (m); + dbus_clear_message (&m); } g_assert_cmpuint (g_queue_get_length (&received), ==, 0); @@ -1093,8 +1085,7 @@ test_peer_get_machine_id (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1114,8 +1105,8 @@ test_peer_get_machine_id (Fixture *f, g_assert_nonnull (what_daemon_thinks); g_assert_cmpuint (strlen (what_daemon_thinks), ==, 32); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); dbus_free (what_i_think); } @@ -1140,8 +1131,7 @@ test_peer_ping (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1155,8 +1145,8 @@ test_peer_ping (Fixture *f, if (!dbus_message_get_args (m, &error, DBUS_TYPE_INVALID)) g_error ("%s: %s", error.name, error.message); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1183,8 +1173,7 @@ test_get_invalid_path (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1202,8 +1191,8 @@ test_get_invalid_path (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1230,8 +1219,7 @@ test_get_invalid_iface (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1248,8 +1236,8 @@ test_get_invalid_iface (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1276,8 +1264,7 @@ test_get_invalid (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1294,8 +1281,8 @@ test_get_invalid (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_PROPERTY); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1320,8 +1307,7 @@ test_get_all_invalid_iface (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1338,8 +1324,8 @@ test_get_all_invalid_iface (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1364,8 +1350,7 @@ test_get_all_invalid_path (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1383,8 +1368,8 @@ test_get_all_invalid_path (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1422,8 +1407,7 @@ test_set_invalid_iface (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1440,8 +1424,8 @@ test_set_invalid_iface (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1479,8 +1463,7 @@ test_set_invalid_path (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1497,8 +1480,8 @@ test_set_invalid_path (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_INTERFACE); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1536,8 +1519,7 @@ test_set_invalid (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1554,8 +1536,8 @@ test_set_invalid (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_UNKNOWN_PROPERTY); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1593,8 +1575,7 @@ test_set (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1611,8 +1592,8 @@ test_set (Fixture *f, g_assert_cmpstr (error.name, ==, DBUS_ERROR_PROPERTY_READ_ONLY); dbus_error_free (&error); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1678,8 +1659,7 @@ test_features (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1702,8 +1682,8 @@ test_features (Fixture *f, if (dbus_message_iter_next (&args_iter)) g_error ("Reply has too many arguments"); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1784,8 +1764,7 @@ test_interfaces (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1808,8 +1787,8 @@ test_interfaces (Fixture *f, if (dbus_message_iter_next (&args_iter)) g_error ("Reply has too many arguments"); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1839,8 +1818,7 @@ test_get_all (Fixture *f, pc == NULL) g_error ("OOM"); - dbus_message_unref (m); - m = NULL; + dbus_clear_message (&m); if (dbus_pending_call_get_completed (pc)) test_pending_call_store_reply (pc, &m); @@ -1891,8 +1869,8 @@ test_get_all (Fixture *f, if (dbus_message_iter_next (&args_iter)) g_error ("Reply has too many arguments"); - dbus_message_unref (m); - dbus_pending_call_unref (pc); + dbus_clear_message (&m); + dbus_clear_pending_call (&pc); } static void @@ -1903,11 +1881,7 @@ teardown (Fixture *f, g_clear_error (&f->ge); if (f->left_conn != NULL) - { - dbus_connection_close (f->left_conn); - dbus_connection_unref (f->left_conn); - f->left_conn = NULL; - } + dbus_connection_close (f->left_conn); if (f->right_conn != NULL) { @@ -1927,10 +1901,11 @@ teardown (Fixture *f, g_queue_clear (&f->held_messages); dbus_connection_close (f->right_conn); - dbus_connection_unref (f->right_conn); - f->right_conn = NULL; } + dbus_clear_connection (&f->left_conn); + dbus_clear_connection (&f->right_conn); + if (f->daemon_pid != 0) { test_kill_pid (f->daemon_pid); diff --git a/test/loopback.c b/test/loopback.c index 567cf801..59f4a28b 100644 --- a/test/loopback.c +++ b/test/loopback.c @@ -316,8 +316,7 @@ test_bad_guid (Fixture *f, g_assert_cmpstr (dbus_message_get_signature (incoming), ==, ""); g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL); - dbus_message_unref (incoming); - + dbus_clear_message (&incoming); dbus_free (address); } @@ -360,9 +359,8 @@ test_message (Fixture *f, g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello"); g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial); - dbus_message_unref (incoming); - - dbus_message_unref (outgoing); + dbus_clear_message (&incoming); + dbus_clear_message (&outgoing); } static void @@ -370,26 +368,18 @@ teardown (Fixture *f, gconstpointer addr G_GNUC_UNUSED) { if (f->client_conn != NULL) - { - dbus_connection_close (f->client_conn); - dbus_connection_unref (f->client_conn); - f->client_conn = NULL; - } + dbus_connection_close (f->client_conn); if (f->server_conn != NULL) - { - dbus_connection_close (f->server_conn); - dbus_connection_unref (f->server_conn); - f->server_conn = NULL; - } + dbus_connection_close (f->server_conn); + + dbus_clear_connection (&f->client_conn); + dbus_clear_connection (&f->server_conn); if (f->server != NULL) - { - dbus_server_disconnect (f->server); - dbus_server_unref (f->server); - f->server = NULL; - } + dbus_server_disconnect (f->server); + dbus_clear_server (&f->server); test_main_context_unref (f->ctx); } -- 2.13.3