~mir-team/unity-system-compositor/toggle-cursor2

« back to all changes in this revision

Viewing changes to tests/integration-tests/dbus_bus.cpp

  • Committer: Tarmac
  • Author(s): Alexandros Frantzis
  • Date: 2015-04-01 12:44:32 UTC
  • mfrom: (201.1.5 dbus-infrastructure)
  • Revision ID: tarmac-20150401124432-77n2v0a385c05mnp
Introduce custom DBus infrastructure.

Approved by PS Jenkins bot, Kevin DuBois, Alan Griffiths.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "dbus_bus.h"
 
20
 
 
21
#include <sstream>
 
22
#include <stdexcept>
 
23
 
 
24
#include <cstdio>
 
25
#include <cstdlib>
 
26
#include <csignal>
 
27
 
 
28
namespace
 
29
{
 
30
 
 
31
std::string run_command(std::string const& cmd)
 
32
{
 
33
    auto fp = ::popen(cmd.c_str(), "r");
 
34
    if (!fp)
 
35
        throw std::runtime_error("Failed to execute command: " + cmd);
 
36
 
 
37
    std::string reply;
 
38
 
 
39
    char buffer[64];
 
40
    while (!feof(fp))
 
41
    {
 
42
        auto nread = fread(buffer, 1, 64, fp);
 
43
        reply.append(buffer, nread);
 
44
    }
 
45
 
 
46
    pclose(fp);
 
47
 
 
48
    return reply;
 
49
}
 
50
 
 
51
}
 
52
 
 
53
usc::test::DBusBus::DBusBus()
 
54
    : pid{0}
 
55
{
 
56
    auto launch = run_command("dbus-daemon --session --print-address=1 --print-pid=1 --fork");
 
57
    std::stringstream ss{launch};
 
58
 
 
59
    std::getline(ss, address_);
 
60
    ss >> pid;
 
61
 
 
62
    if (address_.empty())
 
63
        throw std::runtime_error("Failed to get dbus bus address");
 
64
 
 
65
    if (pid == 0)
 
66
        throw std::runtime_error("Failed to get dbus bus pid");
 
67
}
 
68
 
 
69
usc::test::DBusBus::~DBusBus()
 
70
{
 
71
    kill(pid, SIGTERM);
 
72
}
 
73
 
 
74
std::string usc::test::DBusBus::address()
 
75
{
 
76
    return address_;
 
77
}