-
Notifications
You must be signed in to change notification settings - Fork 2
Agent report error #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5466abe
gagent: Always complete Cancel and Release invocations
AndreaRicchi 25a5860
gagent: Answer ReportError and support connman retry
AndreaRicchi 8f4beb7
gconnman_manager: Add onReportError callback
AndreaRicchi 7821b1d
gconnman_agent_test.cpp: Add agent method reply tests
AndreaRicchi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <amarula/dbus/connman/gconnman.hpp> | ||
| #include <amarula/dbus/connman/gservice.hpp> | ||
| #include <chrono> | ||
| #include <condition_variable> | ||
| #include <mutex> | ||
| #include <string> | ||
|
|
||
| #include "thread_bundle.hpp" | ||
|
|
||
| using Amarula::DBus::G::Connman::Connman; | ||
|
|
||
| namespace { | ||
|
|
||
| /* | ||
| * Short on purpose: connman answers a Service.Connect() call only once the | ||
| * agent has answered ReportError, so an agent that never completes the | ||
| * invocation is indistinguishable from a hang. These calls must come back | ||
| * well within the D-Bus default reply timeout of 25s. | ||
| */ | ||
| constexpr int CALL_TIMEOUT_MS = 2000; | ||
|
|
||
| /* | ||
| * The service list is filled by the asynchronous GetServices() issued when the | ||
| * Manager is built, so a test that needs a service has to wait for the | ||
| * ServicesChanged callback instead of reading Manager::services() right away. | ||
| */ | ||
| constexpr int SERVICES_TIMEOUT_MS = 5000; | ||
|
|
||
| constexpr const char* AGENT_INTERFACE = "net.connman.Agent"; | ||
| constexpr const char* RETRY_ERROR = "net.connman.Agent.Error.Retry"; | ||
|
|
||
| auto call_agent(GDBusConnection* bus, const std::string& path, | ||
| const gchar* method, GVariant* args, GError** error) | ||
| -> GVariant* { | ||
| return g_dbus_connection_call_sync( | ||
| bus, g_dbus_connection_get_unique_name(bus), path.c_str(), | ||
| AGENT_INTERFACE, method, args, nullptr, G_DBUS_CALL_FLAGS_NONE, | ||
| CALL_TIMEOUT_MS, nullptr, error); | ||
| } | ||
|
|
||
| /* | ||
| * The Agent constructor exports the agent object on our own bus connection | ||
| * when the Manager is built, so these tests call it directly and never go | ||
| * through connmand - Manager::registerAgent() is a separate step that only | ||
| * tells connmand which path to call. Like the other connman tests they still | ||
| * need a running connmand, because constructing a Manager does. Skip rather | ||
| * than fail so the suite stays usable on a host without it. | ||
| */ | ||
| auto connman_available(GDBusConnection* bus) -> bool { | ||
| GVariant* reply = g_dbus_connection_call_sync( | ||
| bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", | ||
| "org.freedesktop.DBus", "NameHasOwner", | ||
| g_variant_new("(s)", "net.connman"), G_VARIANT_TYPE("(b)"), | ||
| G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, nullptr, nullptr); | ||
| if (reply == nullptr) { | ||
| return false; | ||
| } | ||
|
|
||
| gboolean has_owner = FALSE; | ||
| g_variant_get(reply, "(b)", &has_owner); | ||
| g_variant_unref(reply); | ||
|
|
||
| return has_owner == TRUE; | ||
| } | ||
|
|
||
| auto system_bus_or_skip() -> GDBusConnection* { | ||
| GDBusConnection* bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, nullptr); | ||
| if (bus == nullptr) { | ||
| return nullptr; | ||
| } | ||
| return connman_available(bus) ? bus : nullptr; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(ConnmanAgent, ReportErrorIsAnswered) { | ||
| GDBusConnection* bus = system_bus_or_skip(); | ||
| if (bus == nullptr) { | ||
| GTEST_SKIP() << "connmand not available on the system bus"; | ||
| } | ||
|
|
||
| const ThreadBundle thread_bundle; | ||
| const Connman connman; | ||
| const auto manager = connman.manager(); | ||
|
|
||
| GError* error = nullptr; | ||
|
|
||
| // No handler installed: the agent must still answer, and must not ask | ||
| // connman to retry. | ||
| GVariant* reply = | ||
| call_agent(bus, manager->internalAgentPath(), "ReportError", | ||
| g_variant_new("(os)", "/net/connman/service/does_not_exist", | ||
| "invalid-key"), | ||
| &error); | ||
|
|
||
| ASSERT_NE(reply, nullptr) | ||
| << "ReportError was not answered within " << CALL_TIMEOUT_MS | ||
| << "ms: " << (error != nullptr ? error->message : ""); | ||
| g_variant_unref(reply); | ||
| } | ||
|
|
||
| TEST(ConnmanAgent, ReportErrorRequestsRetry) { | ||
| GDBusConnection* bus = system_bus_or_skip(); | ||
| if (bus == nullptr) { | ||
| GTEST_SKIP() << "connmand not available on the system bus"; | ||
| } | ||
|
|
||
| /* | ||
| * Declared before the Connman instance so that they outlive the callbacks | ||
| * capturing them: ~Connman() waits for the pending asynchronous calls, | ||
| * whose callbacks run on the library GLib thread. | ||
| */ | ||
| std::mutex mtx; | ||
| std::condition_variable services_cv; | ||
| std::string discovered_service_path; | ||
| std::string reported_service_path; | ||
| std::string reported_error; | ||
|
|
||
| const ThreadBundle thread_bundle; | ||
| const Connman connman; | ||
| const auto manager = connman.manager(); | ||
|
|
||
| manager->onServicesChanged( | ||
| [&mtx, &services_cv, &discovered_service_path](const auto& services) { | ||
| { | ||
| std::lock_guard<std::mutex> const lock(mtx); | ||
| if (discovered_service_path.empty() && !services.empty()) { | ||
| discovered_service_path = services.front()->objPath(); | ||
| } | ||
| } | ||
| services_cv.notify_all(); | ||
| }); | ||
|
|
||
| /* | ||
| * The agent answers Retry only for a service it knows about, so wait for | ||
| * the Manager service list to be populated. The waiting has to happen here | ||
| * and not inside the callback: the callback runs on the GLib thread that | ||
| * also dispatches the agent method calls, so a synchronous call from there | ||
| * would never be answered. | ||
| */ | ||
| std::string service_path; | ||
| { | ||
| std::unique_lock<std::mutex> lock(mtx); | ||
| services_cv.wait_for(lock, | ||
| std::chrono::milliseconds(SERVICES_TIMEOUT_MS), | ||
| [&discovered_service_path] { | ||
| return !discovered_service_path.empty(); | ||
| }); | ||
| service_path = discovered_service_path; | ||
| } | ||
|
|
||
| // The callback is missed when GetServices() completes before it is | ||
| // installed, so fall back to what the Manager already holds. | ||
| if (service_path.empty()) { | ||
| const auto services = manager->services(); | ||
| if (!services.empty()) { | ||
| service_path = services.front()->objPath(); | ||
| } | ||
| } | ||
|
|
||
| if (service_path.empty()) { | ||
| GTEST_SKIP() << "No connman services available"; | ||
| } | ||
|
|
||
| manager->onReportError([&mtx, &reported_service_path, &reported_error]( | ||
| const auto& service, const std::string& error) { | ||
| std::lock_guard<std::mutex> const lock(mtx); | ||
| reported_service_path = | ||
| service != nullptr ? service->objPath() : std::string(); | ||
| reported_error = error; | ||
| return true; | ||
| }); | ||
|
|
||
| GError* error = nullptr; | ||
| GVariant* reply = call_agent( | ||
| bus, manager->internalAgentPath(), "ReportError", | ||
| g_variant_new("(os)", service_path.c_str(), "invalid-key"), &error); | ||
|
|
||
| ASSERT_EQ(reply, nullptr) << "Expected " << RETRY_ERROR; | ||
| ASSERT_NE(error, nullptr); | ||
|
|
||
| gchar* remote_error = g_dbus_error_get_remote_error(error); | ||
| EXPECT_STREQ(remote_error, RETRY_ERROR); | ||
| g_free(remote_error); | ||
| g_error_free(error); | ||
|
|
||
| std::lock_guard<std::mutex> const lock(mtx); | ||
| EXPECT_EQ(reported_service_path, service_path); | ||
| EXPECT_EQ(reported_error, "invalid-key"); | ||
| } | ||
|
|
||
| TEST(ConnmanAgent, CancelIsAnswered) { | ||
| GDBusConnection* bus = system_bus_or_skip(); | ||
| if (bus == nullptr) { | ||
| GTEST_SKIP() << "connmand not available on the system bus"; | ||
| } | ||
|
|
||
| const ThreadBundle thread_bundle; | ||
| const Connman connman; | ||
| const auto manager = connman.manager(); | ||
|
|
||
| GError* error = nullptr; | ||
| GVariant* reply = call_agent(bus, manager->internalAgentPath(), "Cancel", | ||
| nullptr, &error); | ||
|
|
||
| ASSERT_NE(reply, nullptr) | ||
| << "Cancel was not answered within " << CALL_TIMEOUT_MS | ||
| << "ms: " << (error != nullptr ? error->message : ""); | ||
| g_variant_unref(reply); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.