-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy-client.cpp
More file actions
558 lines (501 loc) · 19 KB
/
Copy pathproxy-client.cpp
File metadata and controls
558 lines (501 loc) · 19 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// 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 proxy-client.cpp
*
* @brief Very simple and generic D-Bus proxy client using the
* GDBus++ API (DBus::Proxy::Client)
*/
#include <any>
#include <limits>
#include <string>
#include <getopt.h>
#include <glib.h>
#include <unistd.h>
#include "../gdbuspp/connection.hpp"
#include "../gdbuspp/glib2/utils.hpp"
#include "../gdbuspp/proxy.hpp"
#include "../gdbuspp/proxy/utils.hpp"
#include "test-utils.hpp"
namespace Tests::Program {
using namespace DBus;
class ProxyOpts : protected Tests::Utils::OptionParser
{
public:
enum class PropertyMode
{
UNSET,
GET,
SET,
SET_ANY
};
ProxyOpts(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'},
{"object_path", required_argument, nullptr, 'p'},
{"interface", required_argument, nullptr, 'i'},
{"method-call", required_argument, nullptr, 'm'},
{"property-get", required_argument, nullptr, 'g'},
{"property-set", required_argument, nullptr, 's'},
{"property-set-string", required_argument, nullptr, 'S'},
{"property-set-int", required_argument, nullptr, 'I'},
{"property-set-uint", required_argument, nullptr, 'U'},
{"property-set-bool", required_argument, nullptr, 'B'},
{"data-type", required_argument, nullptr, 't'},
{"data-value", required_argument, nullptr, 'v'},
{"expect-type", required_argument, nullptr, 'X'},
{"expect-result", required_argument, nullptr, 'x'},
{"verbose", no_argument, nullptr, 'V'},
{"quiet", no_argument, nullptr, 'q'},
{"introspect", no_argument, nullptr, 'Q'},
{"lookup", no_argument, nullptr, 'L'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}
// clang-format on
};
int opt;
optind = 1;
while ((opt = getopt_long(argc, argv, "YEd:p:i:Lm:g:s:S:I:U:B:t:v:X:x:VqQh", 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 'p':
object_path = DBus::Object::Path(optarg);
break;
case 'i':
object_interface = std::string(optarg);
break;
case 'Q':
introspect = true;
break;
case 'L':
lookup_object = true;
break;
case 'm':
method = std::string(optarg);
break;
case 'g':
property = std::string(optarg);
property_mode = PropertyMode::GET;
break;
case 's':
property = std::string(optarg);
property_mode = PropertyMode::SET;
break;
case 'S':
property = std::string(optarg);
property_mode = PropertyMode::SET_ANY;
break;
case 'I':
case 'U':
property = std::string(optarg);
property_mode = PropertyMode::SET_ANY;
break;
case 'B':
property = std::string(optarg);
property_mode = PropertyMode::SET_ANY;
break;
case 't':
data_type = std::string(optarg);
break;
case 'v':
data_values.push_back(std::string(optarg));
break;
case 'X':
check_type = std::string(optarg);
break;
case 'x':
check_response = std::string(optarg);
break;
case 'V':
verbose = true;
break;
case 'q':
quiet = true;
break;
case 'h':
help(argv[0], long_opts);
exit(0);
}
if (PropertyMode::SET_ANY == property_mode && (optind < argc))
{
switch (opt)
{
case 'S':
prop_val = std::string(argv[optind++]);
break;
case 'I':
case 'U':
prop_val = std::atoi(argv[optind++]);
break;
case 'B':
{
std::string boolstr(argv[optind++]);
prop_val = ("1" == boolstr || "yes" == boolstr || "true" == boolstr);
property_mode = PropertyMode::SET_ANY;
}
}
}
}
if (introspect && lookup_object)
{
throw DBus::Exception("test_proxy", "Cannot use both --introspect and --lookup at the same time");
}
if (verbose && quiet)
{
throw DBus::Exception("test_proxy", "Cannot use both --verbose and --quiet at the same time");
}
preset = Proxy::TargetPreset::Create(object_path, object_interface);
};
DBus::BusType bustype = DBus::BusType::SESSION;
Proxy::TargetPreset::Ptr preset = nullptr;
std::string destination{};
DBus::Object::Path object_path;
std::string object_interface{};
std::string method{};
std::string property{};
std::any prop_val{};
std::string data_type{};
std::string check_type{};
std::string check_response{};
std::vector<std::string> data_values{};
PropertyMode property_mode = PropertyMode::UNSET;
bool introspect = false;
bool lookup_object = false;
bool verbose = false;
bool quiet = false;
};
int test_proxy_client(int argc, char **argv)
{
ProxyOpts options(argc, argv);
bool errors = false;
if (options.destination.empty())
{
fmt::println(stderr, "** ERROR ** Missing --destination");
errors = true;
}
if (options.object_path.empty())
{
fmt::println("** ERROR ** Missing --object-path");
errors = true;
}
if (errors)
{
return 2;
};
Tests::Utils::ResultLog log(options.verbose);
try
{
auto conn = DBus::Connection::Create(options.bustype);
if (options.introspect)
{
auto prx = Proxy::Client::Create(conn, options.destination);
auto prxqry = Proxy::Utils::Query::Create(prx);
fmt::println("{}", prxqry->Introspect(options.object_path));
return 0;
}
if (options.lookup_object)
{
auto prx = Proxy::Client::Create(conn, options.destination);
auto prxqry = Proxy::Utils::Query::Create(prx);
if (prxqry->LookupObject(options.object_path))
{
if (!options.quiet)
{
fmt::println("Found object at {}", options.object_path);
}
return 0;
}
else
{
if (!options.quiet)
{
fmt::println("Object {} not found", options.object_path);
}
return 2;
}
}
// Process all the --data-value values into a GVariant *
// "package" to be used in the proxy call
GVariant *data = nullptr;
try
{
data = Tests::Utils::generate_gvariant(log,
options.data_type,
options.data_values,
ProxyOpts::PropertyMode::SET != options.property_mode);
}
catch (const Tests::Utils::Exception &excp)
{
fmt::println(stderr, "** ERROR ** {}", excp.what());
return 2;
}
auto prx = Proxy::Client::Create(conn, options.destination);
log.AddLogLine(fmt::format("Connected to: {}", prx));
// Check the Proxy::Client::GetDestination() method
if (prx->GetDestination() != options.destination)
{
log.AddLogLine(fmt::format(
"** ERROR ** Proxy::Client::GetDestination() did not return {}",
options.destination));
throw DBus::Exception("Proxy::Client::GetDestination()",
fmt::format("Unexpected result: {}", prx->GetDestination()));
}
if (!options.method.empty())
{
if (options.object_interface.empty())
{
fmt::println(stderr, "** ERROR ** --interface is missing");
return 2;
}
log.AddLogLine(fmt::format(
"Method call: {}, method={}",
options.preset,
options.method));
GVariant *res = prx->Call(options.preset, options.method, data);
log.AddGVariantDump("GVariant response", res);
if (!log.CheckDataTypeValue(options.check_type, options.check_response, res))
{
if (options.quiet)
{
fmt::println("UNEXPECTED RESULT");
log.DumpLogs();
return 3;
}
}
g_variant_unref(res);
}
else if (ProxyOpts::PropertyMode::GET == options.property_mode)
{
if (options.object_interface.empty())
{
fmt::println(stderr, "** ERROR ** --interface is missing");
errors = true;
}
if (options.property.empty())
{
fmt::println(stderr, "** ERROR ** The property name cannot be empty");
errors = true;
}
if (errors)
{
return 2;
}
// This does logic might seem silly, as the 'data' argument already contains all
// the information. But it uses the data type from the GVariant *data to call
// call the appropriate GetProperty*<>() method. This way, there is a check
// that the GetProperty*<>() method extracts the expected data type. This is
// primarily a test for the GetProperty*<>() method.
auto fetch_content = [](DBus::Proxy::Client::Ptr &prx, const Proxy::TargetPreset::Ptr &tgt, const std::string &property, GVariant *data) -> std::string
{
std::string data_type = glib2::DataType::Extract(data);
std::string ret;
uint32_t idx = 0;
if (data_type[0] == 'a')
{
switch (data_type[1])
{
case 'b':
ret = "<bool>: \n";
for (const auto &e : prx->GetPropertyArray<bool>(tgt, property))
{
ret.append(fmt::format(" [{}] {}\n", idx, static_cast<bool>(e)));
idx++;
}
break;
case 'i':
ret = "<int32_t>: \n";
for (const auto &e : prx->GetPropertyArray<int32_t>(tgt, property))
{
ret.append(fmt::format(" [{}] {}\n", idx, e));
idx++;
}
break;
case 'o':
ret = "<DBus::Object::Path>: \n";
for (const auto &e : prx->GetPropertyArray<DBus::Object::Path>(tgt, property))
{
ret.append(fmt::format(" [{}] {:?}\n", idx, e));
idx++;
}
break;
case 's':
ret = "<string>: \n";
for (const auto &e : prx->GetPropertyArray<std::string>(tgt, property))
{
ret.append(fmt::format(" [{}] {:?}\n", idx, e));
idx++;
}
break;
case 't':
ret = "<uint64_t>: \n";
for (const auto &e : prx->GetPropertyArray<uint64_t>(tgt, property))
{
ret.append(fmt::format(" [{}] {}\n", idx, e));
idx++;
}
break;
case 'u':
ret = "<uint32_t>: \n";
for (const auto &e : prx->GetPropertyArray<uint32_t>(tgt, property))
{
ret.append(fmt::format(" [{}] {}\n", idx, e));
idx++;
}
break;
case 'x':
ret = "<int64_t>: \n";
for (const auto &e : prx->GetPropertyArray<uint64_t>(tgt, property))
{
ret.append(fmt::format(" [{}] {}\n", idx, e));
idx++;
}
break;
default:
ret = "";
break;
}
}
else
{
switch (data_type[0])
{
case 'b':
ret = fmt::format("<bool>: {}", prx->GetProperty<bool>(tgt, property));
break;
case 'i':
ret = fmt::format("<int32_t>: {}", prx->GetProperty<int32_t>(tgt, property));
break;
case 'o':
ret = fmt::format("<DBus::Object::Path>: {:?}", prx->GetProperty<DBus::Object::Path>(tgt, property));
break;
case 's':
ret = fmt::format("<std::string>: {:?}", prx->GetProperty<std::string>(tgt, property));
break;
case 't':
ret = fmt::format("<uint64_t>: {}", prx->GetProperty<uint64_t>(tgt, property));
break;
case 'u':
ret = fmt::format("<uint32_t>: {}", prx->GetProperty<uint32_t>(tgt, property));
break;
case 'x':
ret = fmt::format("<int64_t>: {}", prx->GetProperty<int64_t>(tgt, property));
break;
default:
ret = "";
break;
}
}
return ret;
};
log.AddLogLine(fmt::format(
"Get Property: {}, property={}",
options.preset,
options.property));
GVariant *res = prx->GetPropertyGVariant(options.preset, options.property);
log.AddGVariantDump("GVariant response", res);
log.AddLogLine(fmt::format("Get Property {}", fetch_content(prx, options.preset, options.property, res)));
if (!log.CheckDataTypeValue(options.check_type, options.check_response, res))
{
if (options.quiet)
{
fmt::println(stderr, "UNEXPECTED RESULT");
log.DumpLogs();
return 3;
}
}
g_variant_unref(res);
}
else if (ProxyOpts::PropertyMode::SET == options.property_mode
|| ProxyOpts::PropertyMode::SET_ANY == options.property_mode)
{
if (options.object_interface.empty())
{
fmt::println(stderr, "** ERROR ** --interface is missing");
errors = true;
}
if (options.property.empty())
{
fmt::println(stderr, "** ERROR ** The property name cannot be empty");
errors = true;
}
if (ProxyOpts::PropertyMode::SET == options.property_mode && !data)
{
fmt::println(stderr, "** ERROR ** Missing new data value (--data-type, --data-value)");
errors = true;
}
else if (ProxyOpts::PropertyMode::SET_ANY == options.property_mode && !options.prop_val.has_value())
{
fmt::println(stderr, "** ERROR ** Missing property value to --property-set-*");
}
if (errors)
{
return 2;
}
log.AddLogLine(fmt::format("Set Property: {}, property={}", options.preset, options.property));
if (ProxyOpts::PropertyMode::SET == options.property_mode)
{
prx->SetPropertyGVariant(options.preset, options.property, data);
}
else
{
if (typeid(int) == options.prop_val.type())
{
prx->SetProperty(options.preset, options.property, std::any_cast<int &>(options.prop_val));
}
else if (typeid(unsigned int) == options.prop_val.type())
{
prx->SetProperty(options.preset, options.property, std::any_cast<unsigned int &>(options.prop_val));
}
else if (typeid(bool) == options.prop_val.type())
{
prx->SetProperty(options.preset, options.property, std::any_cast<bool &>(options.prop_val));
}
else if (typeid(std::string) == options.prop_val.type())
{
prx->SetProperty(options.preset, options.property, std::any_cast<std::string &>(options.prop_val));
}
}
}
else
{
fmt::println(stderr, "No operation provided: --introspect, --method, --property-get, --property-set");
return 1;
}
if (!options.quiet)
{
log.DumpLogs();
}
g_thread_pool_stop_unused_threads();
}
catch (const DBus::Exception &excp)
{
log.DumpLogs();
fmt::println(stderr, "** EXCEPTION ** {}", excp.what());
return 2;
}
return 0;
}
} // namespace Tests::Program
int main(int argc, char **argv)
{
return Tests::Program::test_proxy_client(argc, argv);
}