From f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Mar 2016 12:13:20 -0600 Subject: cmake and test directory cleanup --- tests/unit/adbd-client-test.cpp | 97 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/unit/adbd-client-test.cpp (limited to 'tests/unit/adbd-client-test.cpp') diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp new file mode 100644 index 0000000..1e28cc9 --- /dev/null +++ b/tests/unit/adbd-client-test.cpp @@ -0,0 +1,97 @@ +/* + * Copyright 2016 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include +#include + +#include + +#include // mkdtemp + +class AdbdClientFixture: public TestDBusFixture +{ +private: + typedef TestDBusFixture super; + +protected: + + std::string m_tmpdir; + + void SetUp() + { + super::SetUp(); + + char tmpl[] {"adb-client-test-XXXXXX"}; + m_tmpdir = mkdtemp(tmpl); + g_message("using tmpdir '%s'", m_tmpdir.c_str()); + } + + void TearDown() + { + g_rmdir(m_tmpdir.c_str()); + + super::TearDown(); + } +}; + + +TEST_F(AdbdClientFixture, SocketPlumbing) +{ + struct { + const std::string request; + const std::string expected_pk; + AdbdClient::PKResponse response; + const std::string expected_response; + } tests[] = { + { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::ALLOW, "OK" }, + { "PKHelloWorld", "HelloWorld", AdbdClient::PKResponse::DENY, "NO" }, + { "PKFooBar", "FooBar", AdbdClient::PKResponse::ALLOW, "OK" }, + { "PK", "", AdbdClient::PKResponse::DENY, "NO" } + }; + + const auto main_thread = g_thread_self(); + + const auto socket_path = m_tmpdir + "/test-socket-plumbing"; + g_message("socket_path is %s", socket_path.c_str()); + + for (const auto& test : tests) + { + // start an AdbdClient that listens for PKRequests + std::string pk; + auto adbd_client = std::make_shared(socket_path); + adbd_client->on_pk_request().connect([&pk, main_thread, test](const AdbdClient::PKRequest& req){ + EXPECT_EQ(main_thread, g_thread_self()); + g_message("in on_pk_request with %s", req.public_key.c_str()); + pk = req.public_key; + req.respond(test.response); + }); + + // start a mock AdbdServer with to fire test key requests and wait for a response + auto adbd_server = std::make_shared(socket_path, std::vector{test.request}); + wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 2000); + EXPECT_EQ(test.expected_pk, pk); + ASSERT_EQ(1, adbd_server->m_responses.size()); + EXPECT_EQ(test.expected_response, adbd_server->m_responses.front()); + + // cleanup + adbd_client.reset(); + adbd_server.reset(); + g_unlink(socket_path.c_str()); + } +} -- cgit v1.2.3 From d8ef8e68805ab7f53258427c79ee5aaafec916ba Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Mar 2016 17:09:59 -0600 Subject: add UsbManager --- tests/unit/adbd-client-test.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'tests/unit/adbd-client-test.cpp') diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp index 1e28cc9..6514456 100644 --- a/tests/unit/adbd-client-test.cpp +++ b/tests/unit/adbd-client-test.cpp @@ -31,22 +31,22 @@ private: protected: - std::string m_tmpdir; + static void file_deleter (std::string* s) + { + fprintf(stderr, "remove \"%s\"\n", s->c_str()); + remove(s->c_str()); + delete s; + } + + std::shared_ptr m_tmpdir; void SetUp() { super::SetUp(); char tmpl[] {"adb-client-test-XXXXXX"}; - m_tmpdir = mkdtemp(tmpl); - g_message("using tmpdir '%s'", m_tmpdir.c_str()); - } - - void TearDown() - { - g_rmdir(m_tmpdir.c_str()); - - super::TearDown(); + m_tmpdir.reset(new std::string{mkdtemp(tmpl)}, file_deleter); + g_message("using tmpdir '%s'", m_tmpdir->c_str()); } }; @@ -67,7 +67,7 @@ TEST_F(AdbdClientFixture, SocketPlumbing) const auto main_thread = g_thread_self(); - const auto socket_path = m_tmpdir + "/test-socket-plumbing"; + const auto socket_path = *m_tmpdir + "/test-socket-plumbing"; g_message("socket_path is %s", socket_path.c_str()); for (const auto& test : tests) -- cgit v1.2.3 From 4aeb345821bad91430fe9cdb7244c1135051bd92 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 15 Mar 2016 14:23:40 -0500 Subject: in tests/unit/adbd-client-test.cpp, fix tmpl character buffer intialization for g++ 4.9x --- tests/unit/adbd-client-test.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'tests/unit/adbd-client-test.cpp') diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp index 6514456..c00c07a 100644 --- a/tests/unit/adbd-client-test.cpp +++ b/tests/unit/adbd-client-test.cpp @@ -22,8 +22,6 @@ #include -#include // mkdtemp - class AdbdClientFixture: public TestDBusFixture { private: @@ -44,8 +42,8 @@ protected: { super::SetUp(); - char tmpl[] {"adb-client-test-XXXXXX"}; - m_tmpdir.reset(new std::string{mkdtemp(tmpl)}, file_deleter); + char tmpl[] = {"adb-client-test-XXXXXX"}; + m_tmpdir.reset(new std::string{g_mkdtemp(tmpl)}, file_deleter); g_message("using tmpdir '%s'", m_tmpdir->c_str()); } }; -- cgit v1.2.3 From 5b6008fa3b5a9b0373b2f03791140ca1fa8dc8de Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Mar 2016 10:24:41 -0500 Subject: introduce a QtFixture gtest base class to reduce redundancy in test fixtures' helper/util code --- tests/unit/adbd-client-test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/unit/adbd-client-test.cpp') diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp index c00c07a..754f76c 100644 --- a/tests/unit/adbd-client-test.cpp +++ b/tests/unit/adbd-client-test.cpp @@ -32,7 +32,7 @@ protected: static void file_deleter (std::string* s) { fprintf(stderr, "remove \"%s\"\n", s->c_str()); - remove(s->c_str()); + g_remove(s->c_str()); delete s; } -- cgit v1.2.3