diff options
-rw-r--r-- | src/usb-manager.cpp | 48 | ||||
-rw-r--r-- | tests/integration/usb-manager-test.cpp | 34 | ||||
-rw-r--r-- | tests/utils/adbd-server.h | 8 |
3 files changed, 51 insertions, 39 deletions
diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp index 0e59ca2..4fdfdc1 100644 --- a/src/usb-manager.cpp +++ b/src/usb-manager.cpp @@ -50,7 +50,7 @@ public: }); m_greeter->is_active().changed().connect([this](bool /*is_active*/) { - maybe_snap_now(); + restart(); }); restart(); @@ -68,7 +68,6 @@ private: // clear out old state m_snap_connections.clear(); m_snap.reset(); - m_req = AdbdClient::PKRequest{}; m_adbd_client.reset(); } @@ -76,44 +75,30 @@ private: { clear(); + // don't prompt in the greeter! + if (m_greeter->is_active().get()) + return; + // set a new client +g_message("creating a new adbd client"); m_adbd_client.reset(new GAdbdClient{m_socket_path}); m_adbd_client->on_pk_request().connect( [this](const AdbdClient::PKRequest& req) { g_debug("%s got pk request", G_STRLOC); - m_req = req; - maybe_snap_now(); + m_snap = std::make_shared<UsbSnap>(req.fingerprint); + m_snap_connections.insert((*m_snap).on_user_response().connect( + [this,req](AdbdClient::PKResponse response, bool remember_choice){ + g_debug("%s user responded! response %d, remember %d", G_STRLOC, int(response), int(remember_choice)); + req.respond(response); + if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) + write_public_key(req.public_key); + g_idle_add([](gpointer gself){static_cast<Impl*>(gself)->restart(); return G_SOURCE_REMOVE;}, this); + } + )); } ); } - bool ready_to_snap() - { - return !m_greeter->is_active().get() && !m_req.public_key.empty(); - } - - void maybe_snap_now() - { - if (ready_to_snap()) - snap_now(); - } - - void snap_now() - { - g_return_if_fail(ready_to_snap()); - - m_snap = std::make_shared<UsbSnap>(m_req.fingerprint); - m_snap_connections.insert((*m_snap).on_user_response().connect( - [this](AdbdClient::PKResponse response, bool remember_choice){ - g_debug("%s user responded! response %d, remember %d", G_STRLOC, int(response), int(remember_choice)); - m_req.respond(response); - if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) - write_public_key(m_req.public_key); - g_idle_add([](gpointer gself){static_cast<Impl*>(gself)->restart(); return G_SOURCE_REMOVE;}, this); - } - )); - } - void write_public_key(const std::string& public_key) { g_debug("%s writing public key '%s' to '%s'", G_STRLOC, public_key.c_str(), m_public_keys_filename.c_str()); @@ -151,7 +136,6 @@ private: const std::shared_ptr<Greeter> m_greeter; std::shared_ptr<GAdbdClient> m_adbd_client; - AdbdClient::PKRequest m_req; std::shared_ptr<UsbSnap> m_snap; std::set<core::ScopedConnection> m_snap_connections; }; diff --git a/tests/integration/usb-manager-test.cpp b/tests/integration/usb-manager-test.cpp index 3ce79d0..03c80c7 100644 --- a/tests/integration/usb-manager-test.cpp +++ b/tests/integration/usb-manager-test.cpp @@ -87,7 +87,7 @@ protected: QtDBusMock::DBusMock dbusMock; std::shared_ptr<std::string> m_tmpdir; std::shared_ptr<MockUsbMonitor> m_usb_monitor; - std::shared_ptr<Greeter> m_greeter; + std::shared_ptr<MockGreeter> m_greeter; }; TEST_F(UsbManagerFixture, Allow) @@ -157,7 +157,7 @@ TEST_F(UsbManagerFixture, Allow) EXPECT_EQ(public_key, lines[0]); } -TEST_F(UsbManagerFixture, AndroidInterruptus) +TEST_F(UsbManagerFixture, USBDisconnectedDuringPrompt) { const std::shared_ptr<std::string> socket_path {new std::string{*m_tmpdir+"/socket"}, file_deleter}; const std::shared_ptr<std::string> public_keys_path {new std::string{*m_tmpdir+"/adb_keys"}, file_deleter}; @@ -171,7 +171,6 @@ TEST_F(UsbManagerFixture, AndroidInterruptus) for (int i=0; i<3; i++) { -g_message("i %d", i); // add a signal spy to listen to the notification daemon QSignalSpy notificationsSpy( ¬ificationsMockInterface(), @@ -196,3 +195,32 @@ g_message("i %d", i); notificationsSpy.clear(); } } + +TEST_F(UsbManagerFixture, Greeter) +{ + const std::shared_ptr<std::string> socket_path {new std::string{*m_tmpdir+"/socket"}, file_deleter}; + const std::shared_ptr<std::string> public_keys_path {new std::string{*m_tmpdir+"/adb_keys"}, file_deleter}; + + // start a mock AdbdServer ready to submit a request + const std::string public_key {"public_key"}; + auto adbd_server = std::make_shared<GAdbdServer>(*socket_path, std::vector<std::string>{"PK"+public_key}); + + // set up a UsbManager to process the request + m_greeter->m_is_active.set(true); + auto usb_manager = std::make_shared<UsbManager>(*socket_path, *public_keys_path, m_usb_monitor, m_greeter); + + // add a signal spy to listen to the notification daemon + QSignalSpy notificationsSpy( + ¬ificationsMockInterface(), + SIGNAL(MethodCalled(const QString &, const QVariantList &)) + ); + + // the greeter is active, so the notification should not appear + EXPECT_FALSE(notificationsSpy.wait(2000)); + + // disable the greeter, the notification should appear + m_greeter->m_is_active.set(false); + wait_for_signals(notificationsSpy, 1); + EXPECT_EQ("Notify", notificationsSpy.at(0).at(0)); + notificationsSpy.clear(); +} diff --git a/tests/utils/adbd-server.h b/tests/utils/adbd-server.h index 968c1f0..5e66379 100644 --- a/tests/utils/adbd-server.h +++ b/tests/utils/adbd-server.h @@ -73,7 +73,7 @@ private: auto client_socket = g_socket_accept(server_socket, m_cancellable, &error); if (error != nullptr) { if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("GAdbdServer: Error accepting socket connection: %s", error->message); + g_message("GAdbdServer: Error accepting socket connection: %s", error->message); g_clear_error(&error); break; } @@ -90,7 +90,7 @@ private: &error); if (error != nullptr) { if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("GAdbdServer: Error sending request: %s", error->message); + g_message("GAdbdServer: Error sending request: %s", error->message); g_clear_error(&error); g_clear_object(&client_socket); break; @@ -106,10 +106,10 @@ private: &error); if (error != nullptr) { if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("GAdbdServer: Error reading response: %s", error->message); + g_message("GAdbdServer: Error reading response: %s", error->message); g_clear_error(&error); g_clear_object(&client_socket); - break; + continue; } const std::string response(buf, std::string::size_type(n_bytes)); g_message("server got response: %s", response.c_str()); |