2
* Copyright 2016 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include <tests/utils/test-dbus-fixture.h>
21
#include <tests/utils/adbd-server.h>
23
#include <src/adbd-client.h>
25
class AdbdClientFixture: public TestDBusFixture
28
typedef TestDBusFixture super;
32
static void file_deleter (std::string* s)
34
fprintf(stderr, "remove \"%s\"\n", s->c_str());
39
std::shared_ptr<std::string> m_tmpdir;
45
char tmpl[] = {"adb-client-test-XXXXXX"};
46
m_tmpdir.reset(new std::string{g_mkdtemp(tmpl)}, file_deleter);
47
g_message("using tmpdir '%s'", m_tmpdir->c_str());
52
TEST_F(AdbdClientFixture, SocketPlumbing)
55
const std::string request;
56
const std::string expected_pk;
57
AdbdClient::PKResponse response;
58
const std::string expected_response;
60
{ "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::ALLOW, "OK" },
61
{ "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::DENY, "NO" },
62
{ "PKFooBar", "FooBar", AdbdClient::PKResponse::ALLOW, "OK" },
63
{ "PK", "", AdbdClient::PKResponse::DENY, "NO" }
66
const auto main_thread = g_thread_self();
68
const auto socket_path = *m_tmpdir + "/test-socket-plumbing";
69
g_message("socket_path is %s", socket_path.c_str());
71
for (const auto& test : tests)
73
// start an AdbdClient that listens for PKRequests
75
auto adbd_client = std::make_shared<GAdbdClient>(socket_path);
76
adbd_client->on_pk_request().connect([&pk, main_thread, test](const AdbdClient::PKRequest& req){
77
EXPECT_EQ(main_thread, g_thread_self());
78
g_message("in on_pk_request with %s", req.public_key.c_str());
80
req.respond(test.response);
83
// start a mock AdbdServer with to fire test key requests and wait for a response
84
auto adbd_server = std::make_shared<GAdbdServer>(socket_path, std::vector<std::string>{test.request});
85
wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 2000);
86
EXPECT_EQ(test.expected_pk, pk);
87
ASSERT_EQ(1, adbd_server->m_responses.size());
88
EXPECT_EQ(test.expected_response, adbd_server->m_responses.front());
93
g_unlink(socket_path.c_str());