-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplGdbus.h
1475 lines (1338 loc) · 42.4 KB
/
ImplGdbus.h
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TEST_GDBUS_H
#define TEST_GDBUS_H
#include <cstdlib>
#include <gio/gio.h>
#include <glib.h>
#include <gio/gunixfdlist.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <map>
#include "../lib/testlib.h"
#include "ImplCommon.h"
namespace ImplGdbus {
/*
* **** BusTypeGetter interface for ImplGdbus. ****
* **** For use with Setups (client-side) and Services (server-side) ****
* BusTypeGetter is responsible for determining bus types. Required methods are:
* BusType getBusType()
* returns proper bus type
*
* Note: this is meant to be used with high-level gdbus interface, which accepts
* bus type as a parameter. For lower level interface, we will need
* to create connections.
*/
/*
* BusTypeGetterFromEnv is an implementation of BusTypeGetter interface.
* It takes data needed to determine the bus type from environment variables.
*/
class BusTypeGetterFromEnv
{
public:
GBusType getBusType() {
if (getenv("DBUS_STARTER_BUS_TYPE"))
return G_BUS_TYPE_STARTER;
char *busType = getenv("DBUS_BUS_TYPE");
if (busType != nullptr) {
if (strcmp(busType, "1") == 0)
return G_BUS_TYPE_SYSTEM;
else if (strcmp(busType, "0") == 0)
return G_BUS_TYPE_SESSION;
}
busType = getenv("DBUS_SESSION_BUS_ADDRESS");
if (busType != nullptr)
return G_BUS_TYPE_SESSION;
return G_BUS_TYPE_SYSTEM;
}
};
NO_TEMPLATE void unref(GVariant *v) { g_variant_unref(v); }
NO_TEMPLATE void unref(GMainLoop *l) { g_main_loop_unref(l); }
NO_TEMPLATE void unref(GMainContext *c) { g_main_context_unref(c); }
NO_TEMPLATE void unref(GDBusProxy *p) { g_object_unref(p); }
NO_TEMPLATE void unref(GDBusNodeInfo *n) { g_dbus_node_info_unref(n); }
NO_TEMPLATE void unref(GDBusConnection *c) { g_object_unref(c); }
NO_TEMPLATE void unref(GUnixFDList *l) { g_object_unref(l); }
/******************************** CLIENT-SIDE ***************************/
/*
* ClientSetupData is a wrapper for data needed to pass between Setup and SendMethod interfaces (see below).
*/
struct ClientSetupData
{
GBusType busType;
};
/*
* **** Setup interface for ImplGdbus. ****
* **** For direct use by test scenarios ****
* Setup manages lifetime of bus connection, and may perform additional actions
* needed before using SendMethod, e.g. waiting for a specific bus name (see below).
* Required methods are:
* auto Setup::setup(Name name)
* prepares bus connection for SendMethod
* returns ClientSetupData
* void tearDown()
* closes connections, performs clean up
*/
/*
* GetBusTypeSetup is an implementation of Setup interface. Its single purpose
* is the management of bus connection type.
*/
template <typename BusTypeGetter = BusTypeGetterFromEnv>
class GetBusTypeSetup
{
BusTypeGetter busTypeGetter;
public:
ClientSetupData &setup(const testlib::Name &name) {
_setup.busType = busTypeGetter.getBusType();
return _setup;
}
void tearDown() {}
protected:
ClientSetupData _setup;
};
/*
* SetupWaitForName is an implementation of Setup interface. It extends GetBusTypeSetup
* with waiting for a given bus name on the bus.
*/
template <typename BusTypeGetter = BusTypeGetterFromEnv>
class SetupWaitForName : protected GetBusTypeSetup<BusTypeGetter>
{
public:
ClientSetupData &setup(const testlib::Name &name) {
GetBusTypeSetup<BusTypeGetter>::setup(name);
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_assert_nonnull(loop);
auto name_appeared_callback =
[] (GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data) {
g_main_loop_quit(reinterpret_cast<GMainLoop*>(user_data));
};
tinfo("waiting for name: ", name);
guint watcher_id = g_bus_watch_name(GetBusTypeSetup<BusTypeGetter>::_setup.busType, name(),
G_BUS_NAME_WATCHER_FLAGS_NONE,
name_appeared_callback,
NULL, loop,
NULL);
g_assert_cmpuint(watcher_id, !=, 0);
g_main_loop_run(loop);
tinfo("spotted name: ", name);
g_bus_unwatch_name(watcher_id);
unref(loop);
return GetBusTypeSetup<BusTypeGetter>::_setup;
}
};
/*
* **** MessageFeeder interface for ImplGdbus. ****
* **** For direct use by test scenarios ****
* MessageFeeder is responsible for messages creation and disposal. Required methods:
* Message next()
* creates a message
* returns the created message
* void dispose(Message message)
* gets rid of the message
*/
/*
* There are two types of Messages used in ImplGdbus. In fact they do not strictly represent
* message types:
* GVariant * - it represents simple contents of the message
* VariantWithFds - see definition velow - it represents contents of the messsage along
* with list of unix file descriptors - for use with specialized tests
*/
typedef std::pair<GVariant *, GUnixFDList *> VariantWithFds;
/*
* MessageFeeder is a base class with common operations for implementation of MessageFeeder interface.
*/
class MessageFeeder
{
public:
void dispose(GVariant *v) {
unref(v);
}
void dispose(VariantWithFds &v) {
unref(v.first);
unref(v.second);
}
};
/*
* MessageVariantFeeder is an implementation of MessageFeeder interface.
* It provides simple constant content for messages: a hard-coded string.
*/
class MessageVariantFeeder : public MessageFeeder
{
public:
GVariant *next(const testlib::TestSpec &) {
return g_variant_ref_sink(g_variant_new("(s)", "This is message"));
}
void payload_size(uint64_t) {} // ignored
private:
};
/*
* MessageVariantPayloadFeeder is an implementation of MessageFeeder interface.
* It provides generated payload of given size: an array of bytes.
*/
class MessageVariantPayloadFeeder : public MessageFeeder
{
public:
GVariant *next(const testlib::TestSpec &)
{
if (_payload_size == 0)
return g_variant_ref_sink(g_variant_new("()"));
auto array = new unsigned char[_payload_size];
FORLLZ (k, _payload_size)
array[k] = k % 10;
GVariant *farray = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
array, _payload_size, sizeof(array[0]));
GVariant *result = g_variant_new_tuple(&farray, 1);
delete [] array;
return g_variant_ref_sink(result);
}
void payload_size(uint64_t size) { _payload_size = size; }
private:
uint64_t _payload_size;
};
/*
* MessageVariantUnixFdsFeeder is an implementation of MessageFeeder interface.
* It provides constant hard-coded string as a message content
* as well as list of unix file descriptors array of given size.
*/
class MessageVariantUnixFdsFeeder : public MessageFeeder
{
public:
VariantWithFds next(const testlib::TestSpec &)
{
GUnixFDList *fds = g_unix_fd_list_new();
auto array = new int[_payload_size];
FORLLZ (k, _payload_size) {
int fd = open("/dev/zero", O_RDONLY|O_CLOEXEC);
tassert(fd != -1);
GError *error = nullptr;
tassert(g_unix_fd_list_append(fds, fd, &error) != -1);
g_assert_no_error(error);
close(fd);
array[k] = k;
}
GVariant *tuple[2];
tuple[0] = g_variant_new_string("This is a message with fds");
tuple[1] = g_variant_new_fixed_array(G_VARIANT_TYPE_HANDLE,
array, _payload_size, sizeof(array[0]));
GVariant *result = g_variant_new_tuple(tuple, 2);
delete [] array;
return VariantWithFds(g_variant_ref_sink(result), fds);
}
void payload_size(uint64_t size) { _payload_size = size; }
private:
uint64_t _payload_size;
};
/*
* **** ReplyHandlingMethod interface for ImplGdbus. ****
* **** For use by SendMethods ****
* ReplyHandlingMethod is responsible for actions that should be made upon receiving
* some reply for a method call. RequiredMethods:
* handleReply(Message message, Message reply, Error error)
* considers sent message, reply and error received after sending a method call
* and performs checks or other actions
*/
/*
* IsEqualReplyHandlingMethod is an implementation of ReplyHandlingMethod interface.
* It checks if a response's data is equal to message's data. It may be useful
* while calling echo-like service.
*/
class IsEqualReplyHandlingMethod
{
public:
void handleReply(GVariant *msg, GVariant *reply, GError *error)
{
g_assert_no_error(error);
g_assert_nonnull(reply);
g_assert_cmpstr(g_variant_get_type_string(msg), ==, g_variant_get_type_string(reply));
g_assert_true(g_bytes_equal(g_variant_get_data_as_bytes(msg), g_variant_get_data_as_bytes(reply)));
unref(reply);
}
};
/*
* CheckStringReplyHandlingMethod is an implementation of ReplyHandlingMethod interface.
* It checks if a response contains single argument of string type.
*/
class CheckStringReplyHandlingMethod
{
public:
void handleReply(GVariant *msg, GVariant *reply, GError *error)
{
g_assert_no_error(error);
g_assert_nonnull(reply);
g_assert_cmpstr(g_variant_get_type_string(reply), ==, "(s)");
unref(reply);
}
};
/*
* ExpectDBusErrorReplyHandlingMethod is an implementation of ReplyHandlingMethod interface.
* It checks if the method call failed with a specific error. It is parameterized by error.
*/
template <GDBusError err>
class ExpectDBusErrorReplyHandlingMethod
{
public:
void handleReply(GVariant *msg, GVariant *reply, GError *error)
{
tassert(error->domain == G_DBUS_ERROR);
tassert(error->code == err);
}
};
/*
* **** SendMethod interface for ImplGdbus. ****
* **** For use directly by test scenarios ****
* SendMethod is responsible for sending messages. Methods:
* void prepareForSending(setup)
* takes what it needs for further work
* void send(Message message)
* executes specified method call (sends message)
*/
/*
Gdbus sending functions are:
Generated by gdbus-codegen: names depend on input xml.
High-level - GDBusProxy
g_dbus_proxy_call (proxy, method, params, flags, timeout)
g_dbus_proxy_call_sync
g_dbus_proxy_call_with_unix_fd_list
g_dbus_proxy_call_with_unix_fd_list_sync
Low-level - GDBusConnection
- takes GVariant as a message
g_dbus_connection_call (connection, name/object/iface/method, params, reply_type, flags, timeout)
g_dbus_connection_call_sync
g_dbus_connection_call_with_unix_fd_list
g_dbus_connection_call_with_unix_fd_list_sync
g_dbus_connection_emit_signal (connection, name/object/iface/signal, params)
- takes GDBusMessage
g_dbus_connection_send_message (connection, message, flags)
g_dbus_connection_send_message_with_reply (connection, message, flags, timeout)
g_dbus_connection_send_message_with_reply_sync
*/
class SharedBusTypeProxyCreator
{
public:
GDBusProxy *create(GBusType busType, const testlib::TestSpec &spec)
{
GError *error = nullptr;
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(busType,
G_DBUS_PROXY_FLAGS_NONE, NULL,
spec.name(), spec.path(), spec.iface(), NULL, &error);
g_assert_nonnull(proxy);
g_assert_no_error(error);
return proxy;
}
};
class DefaultPoolSize {};
template <unsigned pool_size>
class SetPoolSize
{
public:
SetPoolSize() {
setenv("KDBUS_MEMORY_POOL_SIZE", std::to_string(pool_size).c_str(), 1);
}
};
template <typename PoolSize = DefaultPoolSize>
class PrivateBusTypeProxyCreator
{
public:
GDBusProxy *create(GBusType busType, const testlib::TestSpec &spec)
{
// set the pool size
PoolSize poolSize;
GError *error = nullptr;
this->_connection = g_dbus_connection_new_for_address_sync(
g_dbus_address_get_for_bus_sync(busType, NULL, &error),
static_cast<GDBusConnectionFlags>(
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
| G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT),
NULL, NULL, NULL);
tassert(this->_connection);
g_assert_no_error(error);
g_dbus_connection_set_exit_on_close(this->_connection, FALSE);
GDBusProxy *proxy = g_dbus_proxy_new_sync(this->_connection,
G_DBUS_PROXY_FLAGS_NONE, NULL,
spec.name(), spec.path(), spec.iface(), NULL, &error);
g_assert_nonnull(proxy);
g_assert_no_error(error);
return proxy;
}
~PrivateBusTypeProxyCreator()
{
unref(this->_connection);
}
private:
GDBusConnection *_connection;
};
/*
* SimpleProxyMethod is a base abstract implementation of SendMethod interface.
* It lacks actual sending method but prepares everything to be ready for sending
* with GDBusProxy.
* It is parameterized by its template argument ReplyHandlingMethod.
* ReplyHandlingMethod must be an implementation of ReplyHandlingMethod interface.
*/
template <typename ReplyHandlingMethod = CheckStringReplyHandlingMethod, typename ProxyCreator = SharedBusTypeProxyCreator>
class SimpleProxySendMethod
{
protected:
ReplyHandlingMethod replyhandlingMethod;
ProxyCreator proxyCreator;
public:
void prepareForSending(const ClientSetupData &setup, const testlib::TestSpec &spec)
{
proxy = proxyCreator.create(setup.busType, spec);
member = spec.member;
_flags = spec.no_auto_start() ? G_DBUS_CALL_FLAGS_NO_AUTO_START : G_DBUS_CALL_FLAGS_NONE;
}
~SimpleProxySendMethod()
{
unref(proxy);
}
void switchProxy(GDBusProxy *newproxy)
{
unref(proxy);
proxy = newproxy;
}
GDBusProxy *getProxy() { return proxy; }
protected:
GDBusProxy *proxy;
testlib::Member member;
GDBusCallFlags _flags;
};
/*
* SimpleProxySyncSendMethod is an implementation of SendMethod interface.
* It uses GDBusProxy to send passed messages synchronously and handle replies.
* It is parameterized by its template argument ReplyHandlingMethod.
* ReplyHandlingMethod must be an implementation of ReplyHandlingMethod interface.
*/
template <typename ReplyHandlingMethod = CheckStringReplyHandlingMethod, typename ProxyCreator = SharedBusTypeProxyCreator>
class SimpleProxySyncSendMethod : public SimpleProxySendMethod<ReplyHandlingMethod, ProxyCreator>
{
public:
void send(GVariant *msg, GUnixFDList *fds = nullptr)
{
GError *error = nullptr;
GVariant *reply = g_dbus_proxy_call_with_unix_fd_list_sync(this->proxy, this->member(),
msg, this->_flags, -1, fds, NULL, NULL, &error);
this->replyhandlingMethod.handleReply(msg, reply, error);
}
void send(const VariantWithFds &p)
{
send(p.first, p.second);
}
};
/*
* SimpleProxyAsyncSendMethod is an implementation of SendMethod interface.
* It uses GDBusProxy to send passed messages synchronously and handle replies.
* It is parameterized by its template argument ReplyHandlingMethod.
* ReplyHandlingMethod must be an implementation of ReplyHandlingMethod interface.
*/
template <typename ReplyHandlingMethod = CheckStringReplyHandlingMethod, typename ProxyCreator = SharedBusTypeProxyCreator>
class SimpleProxyAsyncSendMethod : public SimpleProxySendMethod<ReplyHandlingMethod, ProxyCreator>
{
GMainLoop *_loop;
GMainContext *_context;
GVariant *_currentMessage;
public:
void prepareForSending(const ClientSetupData &setup,
const testlib::TestSpec &spec)
{
SimpleProxySendMethod<ReplyHandlingMethod>::prepareForSending(setup, spec);
_context = g_main_context_new();
g_assert_nonnull(_context);
_loop = g_main_loop_new(_context, FALSE);
g_assert_nonnull(_loop);
}
void send(GVariant *msg, GUnixFDList *fds = nullptr)
{
auto callback = [] (GObject *src, GAsyncResult *res, gpointer user_data) {
auto this_ = THIS(user_data);
GError *error = nullptr;
auto *reply = g_dbus_proxy_call_finish(G_DBUS_PROXY(src), res, &error);
if (!reply) {
tfail("Error getting reply: ", error->message);
g_error_free(error);
return;
}
this_->replyhandlingMethod.handleReply(this_->_currentMessage, reply, error);
g_main_loop_quit(this_->_loop);
};
g_main_context_push_thread_default (this->_context);
this->_currentMessage = msg;
g_dbus_proxy_call_with_unix_fd_list(this->proxy, this->member(),
msg, this->_flags, -1, fds, NULL, callback, this);
g_main_loop_run(this->_loop);
g_main_context_pop_thread_default (this->_context);
}
void send(const VariantWithFds &p)
{
send(p.first, p.second);
}
~SimpleProxyAsyncSendMethod()
{
unref(this->_loop);
unref(this->_context);
}
};
/*
* SimpleProxySyncUniqueIdSendMethod is an implementation of SendMethod interface.
* It uses GDBusProxy to send passed messages synchronously and handle replies.
* Destination for messages is set as unique id for the owner of the passed well-known name.
* It is parameterized by its template argument ReplyHandlingMethod.
* ReplyHandlingMethod must be an implementation of ReplyHandlingMethod interface.
*/
template <typename ReplyHandlingMethod = CheckStringReplyHandlingMethod>
class SimpleProxySyncUniqueIdSendMethod
{
SimpleProxySyncSendMethod<ReplyHandlingMethod> sendMethod;
public:
void prepareForSending(const ClientSetupData &setup,
const testlib::TestSpec &spec)
{
sendMethod.prepareForSending(setup, spec);
GError *error = nullptr;
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(setup.busType,
G_DBUS_PROXY_FLAGS_NONE, NULL,
g_dbus_proxy_get_name_owner(sendMethod.getProxy()),
spec.path(), spec.iface(), NULL, &error);
g_assert_nonnull(proxy);
g_assert_no_error(error);
sendMethod.switchProxy(proxy);
}
void send(GVariant *msg, GUnixFDList *fds = nullptr)
{
sendMethod.send(msg, fds);
}
};
/*
* SignalSendMethod is an implementation of SendMethod interface.
* It sends signals.
*/
class SignalSendMethod
{
public:
void prepareForSending(const ClientSetupData &setup,
const testlib::TestSpec &spec)
{
_conn = g_bus_get_sync(setup.busType, NULL, NULL);
tassert(_conn);
_spec = &spec;
}
void send(GVariant *msg)
{
GError *error = nullptr;
tassert(g_dbus_connection_emit_signal(_conn, NULL, _spec->path(), _spec->iface(),
_spec->member(), msg, &error));
g_assert_no_error(error);
tassert(g_dbus_connection_flush_sync(_conn, NULL, NULL));
}
~SignalSendMethod()
{
unref(_conn);
}
private:
GDBusConnection *_conn;
const testlib::TestSpec *_spec;
};
/******************************** SERVER-SIDE ***************************/
/*
* ServiceSetupData is a wrapper for data needed to pass between Service and Dispatcher
* interfaces (see below).
*/
struct ServiceSetupData
{
GMainLoop *loop;
unsigned processed;
};
/*
* **** Reaction interface for ImplGdbus. ****
* **** For use by Services ****
* Reaction is responsible for reacting on incoming messages. Methods:
* ReplyMessage react(Message message)
* acts appropriately for incoming message
* returns a reply message
*
* For gdbus, message's type is GDBusMethodInvocation, which contains
* all the message data needed to react.
* ReplyMessages type used is GVariant *.
*/
/*
* RespondOnceReaction is an implementation of Reaction interface.
* It accepts a message and provides constant hard-coded string content as a reply.
* It also counts incoming messages.
*/
class RespondOnceReaction : public ImplCommon::Counter
{
public:
GVariant *react(GDBusMethodInvocation *invocation, const testlib::TestSpec &spec)
{
updateCounter();
return g_variant_new("(s)", "This is a reply");
}
};
/*
* JustCountReaction is an implementation of Reaction interface.
* It counts incoming messages.
*/
class JustCountReaction : public ImplCommon::Counter
{
public:
GVariant *react(GDBusMethodInvocation *invocation, const testlib::TestSpec &spec)
{
updateCounter();
return nullptr;
}
void react(GVariant *parameters, const testlib::TestSpec &spec)
{
updateCounter();
}
};
/*
* NoMessageAllowedReaction is an implementation of Reaction interface.
* A test is considered as failed if a message is received and passed to
* this reaction.
*/
class NoMessageAllowedReaction : public ImplCommon::Counter
{
public:
GVariant *react(GDBusMethodInvocation *invocation, const testlib::TestSpec &spec)
{
tassert(0 && "Unexpected message received");
return nullptr;
}
};
/*
* CheckUnixFdsAndRespondReaction is an implementation of Reaction interface.
* It accepts a message, gets unix file descriptors from the message and checks
* if they are valid.
* It does not provide response and does not count incoming messages.
* If needed, combine it with another Reaction using ReactionAnd.
*/
class CheckUnixFdsReaction
{
public:
void react(GDBusMethodInvocation *invocation, const testlib::TestSpec &spec)
{
GDBusMessage *msg = g_dbus_method_invocation_get_message(invocation);
g_assert_nonnull(msg);
GUnixFDList *fdlist = g_dbus_message_get_unix_fd_list(msg);
const gint *fds = nullptr;
gint nfds = 0;
if (spec.payload_size() > 0) {
g_assert_nonnull(fdlist);
fds = g_unix_fd_list_peek_fds(fdlist, &nfds);
} else {
g_assert_null(fdlist);
}
g_assert_cmpuint(g_dbus_message_get_num_unix_fds(msg), ==, (guint)nfds);
FORZ(k, (unsigned)nfds) {
char c;
tassert(read(fds[k], &c, 1) == 1); // read one byte from /dev/zero
tassert(c == 0); // check it is zero
}
}
};
/*
* CheckPayloadReaction is an implementation of Reaction interface.
* It accepts a message and checks if its payload is valid payload
* as generated by MessageVariantPayloadFeeder.
* It does not provide response and does not count incoming messages.
* If needed, combine it with another Reaction using ReactionAnd.
*/
class CheckPayloadReaction
{
public:
void react(GDBusMethodInvocation *invocation, const testlib::TestSpec &spec)
{
GVariant *params = g_dbus_method_invocation_get_parameters(invocation);
g_assert_nonnull(params);
react(params, spec);
}
void react(GVariant *params, const testlib::TestSpec &spec)
{
if (spec.payload_size() > 0) {
gsize data_size;
const gchar *data = reinterpret_cast<gchar*>(
g_bytes_unref_to_data(g_variant_get_data_as_bytes(params), &data_size));
g_assert_cmpint(spec.payload_size(), ==, data_size);
FORZ(k, data_size) {
g_assert_cmpint(data[k], ==, k % 10);
}
}
else
{
g_assert_cmpstr(g_variant_get_type_string(params), ==, "()");
}
}
};
/*
* ReactionAnd is an implementation of Reaction interface.
* It is template class which makes composite reaction from two reactions.
* It is parameterized by two reactions T1 and T2 (implementations of Reaction interface).
* The return value is taken from T2.
* Note: it may be extended to accept any number of reactions, if needed.
*/
template <typename T1, typename T2>
class ReactionAnd : public ImplCommon::Counter
{
T1 t1;
T2 t2;
public:
GVariant *react(GDBusMethodInvocation *invocation, testlib::TestSpec &spec)
{
updateCounter();
t1.react(invocation, spec);
return t2.react(invocation, spec);
}
void react(GVariant *parameters, const testlib::TestSpec &spec)
{
updateCounter();
t1.react(parameters, spec);
t2.react(parameters, spec);
}
};
/*
* **** InterfaceCreator interface for ImplGdbus. ****
* **** For use by SingleMethodService ****
* InterfaceCreator is responsible for creating interface data (GDBusNodeInfo).
* InterfaceData create()
* creates interface data
* returns the data
*/
/*
* SingleMethodIfaceCreator is a base class for common operations of InterfaceCreators.
*/
class SingleMethodIfaceCreator
{
public:
template<class ...Args>
auto create(const testlib::TestSpec &method, Args ...args)
{
std::string xml = "<node>"
" <interface name='" + std::string(method.iface()) + "'>"
" <method name='" + std::string(method.member()) + "'>";
std::initializer_list<int>{((void)(
xml += "<arg type='" + std::string(args) + "' name ='"
+ std::string(args) + "_in"
"' direction='in'/>"), 0)...};
xml += " <arg type='s' name='string_out' direction='out'/>"
" </method>"
" </interface>"
"</node>";
GDBusNodeInfo *introspection_data = g_dbus_node_info_new_for_xml(xml.c_str(), NULL);
g_assert_nonnull(introspection_data);
return introspection_data;
}
};
/*
* GeneratedPayloadIfaceCreator is an implementation of InterfaceCreator.
* It provides interface data for a method accepting array of bytes - or nothing - depending
* on given payload size.
*/
class GeneratedPayloadIfaceCreator : SingleMethodIfaceCreator
{
public:
auto create(const testlib::TestSpec &method)
{
if (method.payload_size() > 0)
return SingleMethodIfaceCreator::create(method, "ay");
else
return SingleMethodIfaceCreator::create(method);
}
};
/*
* SingleStringMethodIfaceCreator is an implementation of InterfaceCreator.
* It provides interface data for a method accepting a single string.
*/
class SingleStringMethodIfaceCreator : SingleMethodIfaceCreator
{
public:
auto create(const testlib::TestSpec &method)
{
return SingleMethodIfaceCreator::create(method, "s");
}
};
/*
* SingleStringMethodIfaceCreator is an implementation of InterfaceCreator.
* It provides interface data for a method accepting two arguments: a single string,
* and an array of handles (for passing unix file descriptors).
*/
class StringUnixFdsMethodIfaceCreator : SingleMethodIfaceCreator
{
public:
auto create(const testlib::TestSpec &method)
{
return SingleMethodIfaceCreator::create(method, "s", "ah");
}
};
/*
* **** Service interface for ImplGdbus. ****
* **** For use directly by test scenarios ****
* Service is responsible for receiving messages and reacting on them. Methods:
* ServiceSetupData setup()
* prepares service for receiving messages
* returns ServiceSetupData filled with prepared values
* void tearDown()
* closes service, cleans up.
*/
/*
* SingleMethodService is an implementation of Service interface.
* It provides a service with given well-known name and object at given location,
* and registers handlers for incoming messages.
* It is parameterized by InterfaceCreator, Reaction and BusTypeGetter template arguments.
* InterfaceCreator must be an implementation of InterfaceCreator interface.
* Reaction must be an implementation of Reaction interface.
* BusTypeGetter must be an implementation of BusTypeGetter interface.
*/
template <typename InterfaceCreator = SingleStringMethodIfaceCreator,
typename Reaction = RespondOnceReaction,
typename BusTypeGetter = BusTypeGetterFromEnv>
class SingleMethodService
{
BusTypeGetter busTypeGetter;
Reaction reaction;
InterfaceCreator interfaceCreator;
public:
ServiceSetupData &setup(testlib::TestSpec &spec, unsigned served_messages = 1)
{
reaction.counter(served_messages);
method = &spec;
registerNameAndObject();
return _setup;
}
void tearDown()
{
g_bus_unown_name(name_owning_id);
unref(_setup.loop);
}
private:
void registerNameAndObject()
{
_setup.loop = g_main_loop_new(NULL, FALSE);
g_assert_nonnull(_setup.loop);
auto bus_acquired_handler = [] (GDBusConnection *connection,
const gchar *name,
gpointer user_data) {
THIS(user_data)->registerMethod(connection);
};
auto name_acquired_handler = [] (GDBusConnection *connection,
const gchar *name,
gpointer user_data) {
g_main_loop_quit(THIS(user_data)->_setup.loop);
};
auto name_lost_handler = [] (GDBusConnection *connection,
const gchar *name,
gpointer user_data) {
tfail("unexpected name loss:", name);
};
name_owning_id = g_bus_own_name(busTypeGetter.getBusType(),
method->name(), G_BUS_NAME_OWNER_FLAGS_NONE,
bus_acquired_handler,
name_acquired_handler,
name_lost_handler, this, NULL);
g_main_loop_run(_setup.loop);
}
void registerMethod(GDBusConnection *connection)
{
auto introspection_data = interfaceCreator.create(*method);
const GDBusInterfaceVTable vtable = {
[] (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data) {
auto this_ = THIS(user_data);
if (g_strcmp0(method_name, this_->method->member()) == 0)
{
this_->_setup.processed++;
GVariant *reply = this_->reaction.react(invocation, *this_->method);
// may return floating ref, will be consumed below
if (reply != nullptr)
g_dbus_method_invocation_return_value(invocation, reply); // reply consumed
}
if (this_->reaction.finish())
g_main_loop_quit(this_->_setup.loop);
}
};
registration_id = g_dbus_connection_register_object(connection, method->path(),
introspection_data->interfaces[0], &vtable, this, NULL, NULL);
g_assert_cmpuint(registration_id, !=, 0);
unref(introspection_data);
}
ServiceSetupData _setup;
guint name_owning_id;
guint registration_id;
testlib::TestSpec *method;
};
/*
* ListenToSignalsService is an implementation of Service interface.
* It registers for receiving given signals.
* It also provides a bus service with given well-known name for means of synchronization with Senders.
* It is parameterized by Reaction and BusTypeGetter template arguments.
* Reaction must be an implementation of Reaction interface.
* BusTypeGetter must be an implementation of BusTypeGetter interface.
*/
// FIXME: This is not nice - it "shares" too much with previous class
template <typename Reaction = JustCountReaction,
typename BusTypeGetter = BusTypeGetterFromEnv>
class ListenToSignalService
{
Reaction reaction;
BusTypeGetter busTypeGetter;
public:
ServiceSetupData &setup(const testlib::TestSpec &spec, unsigned served_messages = 1)
{
reaction.counter(served_messages);
_spec = &spec;