/* Pipeline-only native client contract test on a private bus. */ #include "gs-plugin-archipelago.c" static GMainLoop *loop; static GsAppList *result_list; static GError *result_error; static gboolean update_result, reject_stage; static guint prepares, downloads, stages, progress_polls; static gchar *last_method; static GArray *app_progress, *related_progress; static void record_progress(GObject *object, GParamSpec *pspec, gpointer data) { guint value = gs_app_get_progress(GS_APP(object)); g_array_append_val((GArray *) data, value); } static gboolean saw_progress(GArray *values, guint expected) { for (guint i = 0; i < values->len; i++) if (g_array_index(values, guint, i) == expected) return TRUE; return FALSE; } static const gchar target[] = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"; static const gchar plan_id[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; static const gchar tx_path[] = ARCH_PATH "/transactions/op_00000000000000000000000000000001"; static GVariant *map1(const gchar *key, const gchar *value) { GVariantBuilder b; g_variant_builder_init(&b, G_VARIANT_TYPE_VARDICT); g_variant_builder_add(&b, "{sv}", key, g_variant_new_string(value)); return g_variant_builder_end(&b); } static void method(GDBusConnection *bus, const gchar *sender, const gchar *path, const gchar *interface, const gchar *name, GVariant *args, GDBusMethodInvocation *invocation, gpointer data) { if (g_str_equal(name, "GetStatus")) { GVariantBuilder status, update, details, changes, row; g_variant_builder_init(&status, G_VARIANT_TYPE_VARDICT); g_variant_builder_init(&update, G_VARIANT_TYPE_VARDICT); g_variant_builder_init(&details, G_VARIANT_TYPE_VARDICT); g_variant_builder_init(&changes, G_VARIANT_TYPE("aa{sv}")); g_variant_builder_init(&row, G_VARIANT_TYPE_VARDICT); g_variant_builder_add(&row, "{sv}", "name", g_variant_new_string("linux")); g_variant_builder_add(&row, "{sv}", "old_version", g_variant_new_string("1")); g_variant_builder_add(&row, "{sv}", "new_version", g_variant_new_string("2")); g_variant_builder_add_value(&changes, g_variant_builder_end(&row)); g_variant_builder_add(&details, "{sv}", "package_changes", g_variant_builder_end(&changes)); g_variant_builder_add(&update, "{sv}", "details", g_variant_builder_end(&details)); g_variant_builder_add(&update, "{sv}", "state", g_variant_new_string("available")); g_variant_builder_add(&update, "{sv}", "target", g_variant_new_string(target)); g_variant_builder_add(&update, "{sv}", "download_size", g_variant_new_uint64(1414614430)); g_variant_builder_add(&update, "{sv}", "installed_size", g_variant_new_uint64(2092868303)); g_variant_builder_add(&status, "{sv}", "update", g_variant_builder_end(&update)); g_variant_builder_add(&status, "{sv}", "booted", map1("checksum", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")); g_dbus_method_invocation_return_value(invocation, g_variant_new("(a{sv})", &status)); } else if (g_str_equal(name, "GetSnapshot")) { GVariantBuilder snapshot, result; gboolean failed = reject_stage && g_str_equal(last_method, "Stage"); /* The first transaction reports two in-flight snapshots so the client's * progress marshalling is exercised before the terminal state. */ gboolean running = !failed && progress_polls < 2; g_variant_builder_init(&snapshot, G_VARIANT_TYPE_VARDICT); g_variant_builder_init(&result, G_VARIANT_TYPE_VARDICT); g_variant_builder_add(&snapshot, "{sv}", "State", g_variant_new_string(failed ? "failed" : running ? "running" : "succeeded")); if (running) { GVariantBuilder progress; g_variant_builder_init(&progress, G_VARIANT_TYPE_VARDICT); g_variant_builder_add(&progress, "{sv}", "percent", g_variant_new_uint32(progress_polls == 0 ? 25 : 60)); g_variant_builder_add(&snapshot, "{sv}", "Progress", g_variant_builder_end(&progress)); progress_polls++; } if (failed) g_variant_builder_add(&snapshot, "{sv}", "Error", map1("message", "fixture transaction rejected")); g_variant_builder_add(&result, "{sv}", "plan_id", g_variant_new_string(plan_id)); g_variant_builder_add(&result, "{sv}", "outcome", g_variant_new_string("planned")); g_variant_builder_add(&snapshot, "{sv}", "Result", g_variant_builder_end(&result)); g_dbus_method_invocation_return_value(invocation, g_variant_new("(a{sv})", &snapshot)); } else { const gchar *id; g_autoptr(GVariant) options = NULL; g_variant_get(args, "(&s@a{sv})", &id, &options); g_assert_true(g_uuid_string_is_valid(bus_string(options, "request_id"))); if (g_str_equal(name, "PrepareUpdate")) { prepares++; g_assert_cmpstr(id, ==, target); } else { g_assert_cmpstr(id, ==, plan_id); if (g_str_equal(name, "Stage")) stages++; else if (g_str_equal(name, "Download")) downloads++; else g_assert_not_reached(); } g_free(last_method); last_method = g_strdup(name); g_dbus_method_invocation_return_value(invocation, g_variant_new("(o)", tx_path)); } } static const GDBusInterfaceVTable vtable = {.method_call = method}; static void shutdown_completed(GObject *source, GAsyncResult *result, gpointer data) { g_assert_true(gs_plugin_shutdown_finish(GS_PLUGIN(source), result, &result_error)); g_assert_no_error(result_error); g_main_loop_quit(loop); } static void updated(GObject *source, GAsyncResult *result, gpointer data) { update_result = update_finish(GS_PLUGIN(source), result, &result_error); g_main_loop_quit(loop); } static void completed(GObject *source, GAsyncResult *result, gpointer data) { result_list = list_apps_finish(GS_PLUGIN(source), result, &result_error); g_main_loop_quit(loop); } int main(void) { g_autoptr(GTestDBus) test_bus = g_test_dbus_new(G_TEST_DBUS_NONE); g_test_dbus_up(test_bus); g_setenv("DBUS_SYSTEM_BUS_ADDRESS", g_test_dbus_get_bus_address(test_bus), TRUE); g_autoptr(GDBusConnection) bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL); g_assert_nonnull(bus); g_autoptr(GVariant) owned = g_dbus_connection_call_sync(bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName", g_variant_new("(su)", ARCH_BUS, 0u), NULL, 0, -1, NULL, NULL); g_assert_nonnull(owned); const gchar *xml = "" "" "" "" "" "" ""; g_autoptr(GDBusNodeInfo) info = g_dbus_node_info_new_for_xml(xml, NULL); guint manager = g_dbus_connection_register_object(bus, ARCH_PATH, info->interfaces[0], &vtable, NULL, NULL, NULL); guint transaction = g_dbus_connection_register_object(bus, tx_path, info->interfaces[1], &vtable, NULL, NULL, NULL); g_assert_cmpuint(manager, >, 0); g_assert_cmpuint(transaction, >, 0); g_autoptr(GsPlugin) plugin = g_object_new(gs_plugin_archipelago_get_type(), "session-bus-connection", bus, "system-bus-connection", bus, NULL); g_autoptr(GsAppQuery) query = gs_app_query_new("is-for-update", GS_APP_QUERY_TRISTATE_TRUE, NULL); loop = g_main_loop_new(NULL, FALSE); list_apps_async(plugin, query, 0, NULL, NULL, NULL, completed, NULL); g_main_loop_run(loop); g_assert_no_error(result_error); g_assert_cmpuint(gs_app_list_length(result_list), ==, 1); GsApp *app = gs_app_list_index(result_list, 0); g_assert_true(gs_app_has_management_plugin(app, plugin)); g_assert_true(gs_app_has_quirk(app, GS_APP_QUIRK_NEEDS_REBOOT)); g_assert_cmpuint(gs_app_list_length(gs_app_get_related(app)), ==, 1); g_assert_nonnull(strstr(gs_app_get_update_details_markup(app), "linux")); /* Software centers show the image size before any download; the licence is * static distributor metadata, not part of the transaction payload. */ guint64 download = 0; g_assert_cmpint(gs_app_get_size_download(app, &download), ==, GS_SIZE_TYPE_VALID); g_assert_cmpuint(download, ==, 1414614430); g_assert_cmpstr(gs_app_get_license(app), ==, "MIT"); app_progress = g_array_new(FALSE, FALSE, sizeof(guint)); related_progress = g_array_new(FALSE, FALSE, sizeof(guint)); g_signal_connect(app, "notify::progress", G_CALLBACK(record_progress), app_progress); g_signal_connect(gs_app_list_index(gs_app_get_related(app), 0), "notify::progress", G_CALLBACK(record_progress), related_progress); update_async(plugin, result_list, GS_PLUGIN_UPDATE_APPS_FLAGS_NO_APPLY, NULL, NULL, NULL, NULL, NULL, NULL, NULL, updated, NULL); g_main_loop_run(loop); g_assert_no_error(result_error); g_assert_true(update_result); g_assert_cmpuint(prepares, ==, 1); g_assert_cmpuint(downloads, ==, 1); g_assert_cmpuint(stages, ==, 0); /* Progress snapshots are marshalled onto the caller's main context; the * related package rows must mirror them or the updates list averages an * unknown percentage and hides the aggregate bar. The terminal transition * then sets 100, so assert on the observed sequence, not the final value. */ g_assert_cmpuint(progress_polls, ==, 2); g_assert_true(saw_progress(app_progress, 25)); g_assert_true(saw_progress(app_progress, 60)); g_assert_true(saw_progress(related_progress, 25)); g_assert_true(saw_progress(related_progress, 60)); g_assert_cmpuint(gs_app_get_progress(app), ==, 100); reject_stage = TRUE; update_async(plugin, result_list, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, updated, NULL); g_main_loop_run(loop); g_assert_false(update_result); g_assert_nonnull(result_error); g_assert_nonnull(strstr(result_error->message, "fixture transaction rejected")); g_assert_cmpint(gs_app_get_state(app), ==, GS_APP_STATE_UPDATABLE_LIVE); g_clear_error(&result_error); reject_stage = FALSE; update_async(plugin, result_list, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, updated, NULL); g_main_loop_run(loop); g_assert_no_error(result_error); g_assert_true(update_result); g_assert_cmpuint(prepares, ==, 3); g_assert_cmpuint(stages, ==, 2); /* Match the GNOME Software 51 loader lifecycle: shutdown clears pending * updates-changed emissions before dispose. */ gs_plugin_shutdown_async(plugin, NULL, shutdown_completed, NULL); g_main_loop_run(loop); g_clear_object(&result_list); g_clear_object(&plugin); g_dbus_connection_unregister_object(bus, manager); g_dbus_connection_unregister_object(bus, transaction); g_main_loop_unref(loop); g_free(last_method); g_clear_pointer(&app_progress, g_array_unref); g_clear_pointer(&related_progress, g_array_unref); g_clear_object(&bus); g_test_dbus_down(test_bus); g_print("PLUGIN_DBUS_DIFF_DOWNLOAD_STAGE_ERROR_STATES_PASS\n"); return 0; }