aboutsummaryrefslogtreecommitdiff
path: root/tests/utils/adbd-server.h
blob: 740faaa181e97a5f08753b1df89253fe1be85015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * 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 <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>

#include <string>
#include <thread>
#include <vector>


/**
 * A Mock ADBD server.
 *
 * Binds to a local domain socket, sends public key requests across it,
 * and reads back the client's responses.
 */
class GAdbdServer
{
public:

    GAdbdServer(const std::string& socket_path,
                const std::vector<std::string>& requests):
        m_requests{requests},
        m_socket_path{socket_path},
        m_cancellable{g_cancellable_new()},
        m_worker_thread{&GAdbdServer::worker_func, this}
    {
    }

    ~GAdbdServer()
    {
        // tell the worker thread to stop whatever it's doing and exit.
        g_cancellable_cancel(m_cancellable);
        m_worker_thread.join();
        g_clear_object(&m_cancellable);
    }

    const std::vector<std::string> m_requests;
    std::vector<std::string> m_responses;

private:

    void worker_func() // runs in worker thread
    {
        auto server_socket = create_server_socket(m_socket_path);
        auto requests = m_requests;

        GError* error {};
        g_socket_listen (server_socket, &error);
        g_assert_no_error (error);

        while (!g_cancellable_is_cancelled(m_cancellable) && !requests.empty())
        {
            // wait for a client connection
            g_message("GAdbdServer::Impl::worker_func() calling g_socket_accept()");
            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_clear_error(&error);
                break;
            }

            // pop the next request off the stack
            auto request = requests.front();
            requests.erase(requests.begin());

            // send the request
            g_message("GAdbdServer::Impl::worker_func() sending req [%s]", request.c_str());
            g_socket_send(client_socket,
                          request.c_str(),
                          request.size(),
                          m_cancellable,
                          &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_clear_error(&error);
                g_clear_object(&client_socket);
                break;
            }

            // read the response
            g_message("GAdbdServer::Impl::worker_func() reading response");
            char buf[4096];
            const auto n_bytes = g_socket_receive(client_socket,
                                                  buf,
                                                  sizeof(buf),
                                                  m_cancellable,
                                                  &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_clear_error(&error);
                g_clear_object(&client_socket);
                break;
            }
            const std::string response(buf, std::string::size_type(n_bytes));
            g_message("server got response: %s", response.c_str()); 
            m_responses.push_back(response);

            // cleanup
            g_clear_object(&client_socket);
        }

        g_clear_object(&server_socket);
    }

    // bind to a local domain socket
    static GSocket* create_server_socket(const std::string& socket_path)
    {
        GError* error {};
        auto socket = g_socket_new(G_SOCKET_FAMILY_UNIX,
                                   G_SOCKET_TYPE_STREAM,
                                   G_SOCKET_PROTOCOL_DEFAULT,
                                   &error);
        g_assert_no_error(error);
        auto address = g_unix_socket_address_new (socket_path.c_str());
        g_socket_bind (socket, address, false, &error);
        g_assert_no_error (error);
        g_clear_object (&address);

        return socket;
    }

    const std::string m_socket_path;
    GCancellable* m_cancellable = nullptr;
    std::thread m_worker_thread;
};