~dobey/indicator-display/fix-i18n

« back to all changes in this revision

Viewing changes to tests/unit/adbd-client-test.cpp

  • Committer: CI Train Bot
  • Author(s): Charles Kerr
  • Date: 2016-03-24 16:16:53 UTC
  • mfrom: (16.1.50 adb-key-auth)
  • Revision ID: ci-train-bot@canonical.com-20160324161653-47tdm05osyr45ab0
When a new device appears to ADB, prompt the user whether or not to allow the connection.
Approved by: PS Jenkins bot, Charles Kerr, Xavi Garcia

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2016 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authors:
 
17
 *   Charles Kerr <charles.kerr@canonical.com>
 
18
 */
 
19
 
 
20
#include <tests/utils/test-dbus-fixture.h>
 
21
#include <tests/utils/adbd-server.h>
 
22
 
 
23
#include <src/adbd-client.h>
 
24
 
 
25
class AdbdClientFixture: public TestDBusFixture
 
26
{
 
27
private:
 
28
    typedef TestDBusFixture super;
 
29
 
 
30
protected:
 
31
 
 
32
    static void file_deleter (std::string* s)
 
33
    {
 
34
        fprintf(stderr, "remove \"%s\"\n", s->c_str());
 
35
        g_remove(s->c_str());
 
36
        delete s;
 
37
    }
 
38
 
 
39
    std::shared_ptr<std::string> m_tmpdir;
 
40
 
 
41
    void SetUp()
 
42
    {
 
43
        super::SetUp();
 
44
 
 
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());
 
48
    }
 
49
};
 
50
 
 
51
 
 
52
TEST_F(AdbdClientFixture, SocketPlumbing)
 
53
{
 
54
    struct {
 
55
        const std::string request;
 
56
        const std::string expected_pk;
 
57
        AdbdClient::PKResponse response;
 
58
        const std::string expected_response;
 
59
    } tests[] = {
 
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" }
 
64
    };
 
65
 
 
66
    const auto main_thread = g_thread_self();
 
67
 
 
68
    const auto socket_path = *m_tmpdir + "/test-socket-plumbing";
 
69
    g_message("socket_path is %s", socket_path.c_str());
 
70
 
 
71
    for (const auto& test : tests)
 
72
    {
 
73
        // start an AdbdClient that listens for PKRequests
 
74
        std::string pk;
 
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());
 
79
            pk = req.public_key;
 
80
            req.respond(test.response);
 
81
        });
 
82
 
 
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());
 
89
   
 
90
        // cleanup
 
91
        adbd_client.reset();
 
92
        adbd_server.reset();
 
93
        g_unlink(socket_path.c_str());
 
94
    }
 
95
}