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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// GDBus++ - glib2 GDBus C++ wrapper
//
// SPDX-License-Identifier: AGPL-3.0-only
//
// Copyright (C) OpenVPN Inc <sales@openvpn.net>
// Copyright (C) David Sommerseth <davids@openvpn.net>
//
/**
* @file credentials.cpp
*
* @brief Test program using the DBus::Credentials interface
* sending D-Bus signals
*/
#include <iostream>
#include <string>
#include <getopt.h>
#include "../gdbuspp/connection.hpp"
#include "../gdbuspp/credentials/query.hpp"
#include "test-utils.hpp"
#include "test-constants.hpp"
using namespace DBus;
using namespace Test;
class Options : public TestUtils::OptionParser
{
public:
DBus::BusType bustype = DBus::BusType::SESSION;
std::string destination{};
std::string expect_result{};
bool get_ubusname = false;
bool get_uid = false;
bool get_pid = false;
Options(const int argc, char **argv)
{
static struct option long_opts[] = {
// clang-format off
{"system", no_argument, nullptr, 'Y'},
{"session", no_argument, nullptr, 'E'},
{"destination", required_argument, nullptr, 'd'},
{"get-bus-name", no_argument, nullptr, 'b'},
{"get-pid", no_argument, nullptr, 'p'},
{"get-uid", no_argument, nullptr, 'u'},
{"expect-result", required_argument, nullptr, 'x'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}
// clang-format on
};
int opt;
optind = 1;
while ((opt = getopt_long(argc, argv, "YEd:bpux:h", long_opts, nullptr)) != -1)
{
switch (opt)
{
case 'Y':
bustype = DBus::BusType::SYSTEM;
break;
case 'E':
bustype = DBus::BusType::SESSION;
break;
case 'd':
destination = std::string(optarg);
break;
case 'b':
get_ubusname = true;
break;
case 'p':
get_pid = true;
break;
case 'u':
get_uid = true;
break;
case 'x':
expect_result = std::string(optarg);
break;
case 'h':
help(argv[0], long_opts);
exit(0);
}
}
if (destination.empty())
{
std::cerr << "Missing destination to check (--destination)" << std::endl;
exit(1);
}
if (!get_ubusname && !get_uid && !get_pid)
{
if (!expect_result.empty())
{
std::cerr << "Using --expect-result requires one of --get-bus-name, --get-uid or --get-pid"
<< std::endl;
exit(1);
}
// If no specific query is defined, query them all by default
get_ubusname = true;
get_uid = true;
get_pid = true;
}
}
};
void check_expectations(const std::string &expect, const std::string &result)
{
if (!expect.empty())
{
if (expect != result)
{
std::cerr << "Unexpected result; expected "
<< "'" << expect << "'"
<< std::endl;
exit(3);
}
exit(0);
}
}
int main(int argc, char **argv)
{
try
{
Options opts(argc, argv);
auto conn = DBus::Connection::Create(opts.bustype);
auto creds = DBus::Credentials::Query::Create(conn);
if (opts.get_ubusname)
{
std::string result = creds->GetUniqueBusName(opts.destination);
std::cout << "Unique bus name: " << result << std::endl;
check_expectations(opts.expect_result, result);
}
if (opts.get_uid)
{
std::string result = std::to_string(creds->GetUID(opts.destination));
std::cout << "Owning user ID (uid): " << result << std::endl;
check_expectations(opts.expect_result, result);
}
if (opts.get_pid)
{
std::string result = std::to_string(creds->GetPID(opts.destination));
std::cout << "Owning process ID (pid): " << result << std::endl;
check_expectations(opts.expect_result, result);
}
}
catch (const DBus::Exception &excp)
{
std::cerr << "** EXCEPTION ** " << excp.what() << std::endl;
return 2;
}
return 0;
}
|