-
Notifications
You must be signed in to change notification settings - Fork 229
/
server_interfaces_test.go
486 lines (432 loc) · 10.6 KB
/
server_interfaces_test.go
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
package dbus
import (
"errors"
"sync"
"sync/atomic"
"testing"
"time"
)
type tester struct {
conn *Conn
sigs chan *Signal
subSigsMu sync.Mutex
subSigs map[string]map[string]struct{}
serial uint32
}
type intro struct {
path ObjectPath
}
func (i *intro) introspectPath(path ObjectPath) string {
switch path {
case "/":
return `<node><node name="com"></node></node>`
case "/com":
return `<node><node name="github"></node></node>`
case "/com/github":
return `<node><node name="godbus"></node></node>`
case "/com/github/godbus":
return `<node><node name="tester"></node></node>`
}
return ""
}
func (i *intro) LookupInterface(name string) (Interface, bool) {
if name == "org.freedesktop.DBus.Introspectable" {
return i, true
}
return nil, false
}
func (i *intro) LookupMethod(name string) (Method, bool) {
if name == "Introspect" {
return intro_fn(func() string {
return i.introspectPath(i.path)
}), true
}
return nil, false
}
func newIntro(path ObjectPath) *intro {
return &intro{path}
}
// Handler
func (t *tester) LookupObject(path ObjectPath) (ServerObject, bool) {
if path == "/com/github/godbus/tester" {
return t, true
}
return newIntro(path), true
}
// ServerObject
func (t *tester) LookupInterface(name string) (Interface, bool) {
switch name {
case "com.github.godbus.dbus.Tester":
return t, true
case "org.freedesktop.DBus.Introspectable":
return t, true
}
return nil, false
}
// Interface
func (t *tester) LookupMethod(name string) (Method, bool) {
switch name {
case "Test":
return t, true
case "Error":
return terrfn(func(in string) error {
return errors.New(in)
}), true
case "Introspect":
return intro_fn(func() string {
return `<node>
<interface name="org.freedesktop.DBus.Introspectable.Introspect">
<method name="Introspect">
<arg name="out" type="i" direction="out">
</method>
</interface>
<interface name="com.github.godbus.dbus.Tester">
<method name="Test">
<arg name="in" type="i" direction="in">
<arg name="out" type="i" direction="out">
</method>
<signal name="sig1">
<arg name="out" type="i" direction="out">
</signal>
</interface>
</node>`
}), true
}
return nil, false
}
// Method
func (t *tester) Call(args ...interface{}) ([]interface{}, error) {
return args, nil
}
func (t *tester) NumArguments() int {
return 1
}
func (t *tester) NumReturns() int {
return 1
}
func (t *tester) ArgumentValue(position int) interface{} {
return ""
}
func (t *tester) ReturnValue(position int) interface{} {
return ""
}
type terrfn func(in string) error
func (t terrfn) Call(args ...interface{}) ([]interface{}, error) {
return nil, t(*args[0].(*string))
}
func (t terrfn) NumArguments() int {
return 1
}
func (t terrfn) NumReturns() int {
return 0
}
func (t terrfn) ArgumentValue(position int) interface{} {
return ""
}
func (t terrfn) ReturnValue(position int) interface{} {
return ""
}
// SignalHandler
func (t *tester) DeliverSignal(iface, name string, signal *Signal) {
t.subSigsMu.Lock()
intf, ok := t.subSigs[iface]
t.subSigsMu.Unlock()
if !ok {
return
}
if _, ok := intf[name]; !ok {
return
}
t.sigs <- signal
}
func (t *tester) AddSignal(iface, name string) error {
t.subSigsMu.Lock()
if i, ok := t.subSigs[iface]; ok {
i[name] = struct{}{}
} else {
t.subSigs[iface] = make(map[string]struct{})
t.subSigs[iface][name] = struct{}{}
}
t.subSigsMu.Unlock()
return t.conn.AddMatchSignal(WithMatchInterface(iface), WithMatchMember(name))
}
func (t *tester) Close() {
t.conn.Close()
close(t.sigs)
}
func (t *tester) Name() string {
return t.conn.Names()[0]
}
func (t *tester) GetSerial() uint32 {
return atomic.AddUint32(&t.serial, 1)
}
func (t *tester) RetireSerial(serial uint32) {}
type intro_fn func() string
func (intro intro_fn) Call(args ...interface{}) ([]interface{}, error) {
return []interface{}{intro()}, nil
}
func (_ intro_fn) NumArguments() int {
return 0
}
func (_ intro_fn) NumReturns() int {
return 1
}
func (_ intro_fn) ArgumentValue(position int) interface{} {
return nil
}
func (_ intro_fn) ReturnValue(position int) interface{} {
return ""
}
func newTester() (*tester, error) {
tester := &tester{
sigs: make(chan *Signal),
subSigs: make(map[string]map[string]struct{}),
}
conn, err := ConnectSessionBus(
WithHandler(tester),
WithSignalHandler(tester),
WithSerialGenerator(tester),
)
if err != nil {
return nil, err
}
tester.conn = conn
return tester, nil
}
func TestHandlerCall(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus/tester")
var out string
in := "foo"
err = obj.Call("com.github.godbus.dbus.Tester.Test", 0, in).Store(&out)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if out != in {
t.Errorf("Unexpected error: got %s, expected %s", out, in)
}
tester.Close()
}
func TestHandlerCallGenericError(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus/tester")
var out string
in := "foo"
err = obj.Call("com.github.godbus.dbus.Tester.Error", 0, in).Store(&out)
if err != nil && err.(Error).Body[0].(string) != "foo" {
t.Errorf("Unexpected error: %s", err)
}
tester.Close()
}
func TestHandlerCallNonExistent(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus/tester/nonexist")
var out string
in := "foo"
err = obj.Call("com.github.godbus.dbus.Tester.Test", 0, in).Store(&out)
if err != nil {
if err.Error() != "Object does not implement the interface 'com.github.godbus.dbus.Tester'" {
t.Errorf("Unexpected error: %s", err)
}
}
tester.Close()
}
func TestHandlerInvalidFunc(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus/tester")
var out string
in := "foo"
err = obj.Call("com.github.godbus.dbus.Tester.Notexist", 0, in).Store(&out)
if err == nil {
t.Errorf("didn't get expected error")
}
tester.Close()
}
func TestHandlerInvalidNumArg(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus/tester")
var out string
err = obj.Call("com.github.godbus.dbus.Tester.Test", 0).Store(&out)
if err == nil {
t.Errorf("didn't get expected error")
}
tester.Close()
}
func TestHandlerInvalidArgType(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus/tester")
var out string
err = obj.Call("com.github.godbus.dbus.Tester.Test", 0, 2.10).Store(&out)
if err == nil {
t.Errorf("didn't get expected error")
}
tester.Close()
}
func TestHandlerIntrospect(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus/tester")
var out string
err = obj.Call("org.freedesktop.DBus.Introspectable.Introspect", 0).Store(&out)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
expected := `<node>
<interface name="org.freedesktop.DBus.Introspectable.Introspect">
<method name="Introspect">
<arg name="out" type="i" direction="out">
</method>
</interface>
<interface name="com.github.godbus.dbus.Tester">
<method name="Test">
<arg name="in" type="i" direction="in">
<arg name="out" type="i" direction="out">
</method>
<signal name="sig1">
<arg name="out" type="i" direction="out">
</signal>
</interface>
</node>`
if out != expected {
t.Errorf("didn't get expected return value, expected %s got %s", expected, out)
}
tester.Close()
}
func TestHandlerIntrospectPath(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
obj := conn.Object(tester.Name(), "/com/github/godbus")
var out string
err = obj.Call("org.freedesktop.DBus.Introspectable.Introspect", 0).Store(&out)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
expected := `<node><node name="tester"></node></node>`
if out != expected {
t.Errorf("didn't get expected return value, expected %s got %s", expected, out)
}
tester.Close()
}
func TestHandlerSignal(t *testing.T) {
tester, err := newTester()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
conn, err := ConnectSessionBus()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
defer conn.Close()
if err = tester.AddSignal("com.github.godbus.dbus.Tester", "sig1"); err != nil {
t.Fatal(err)
}
if err = conn.Emit(
"/com/github/godbus/tester",
"com.github.godbus.dbus.Tester.sig1",
"foo",
); err != nil {
t.Fatal(err)
}
select {
case sig := <-tester.sigs:
if sig.Body[0] != "foo" {
t.Errorf("Unexpected signal got %s, expected %s", sig.Body[0], "foo")
}
case <-time.After(time.Second * 10): // overly generous timeout
t.Errorf("Didn't receive a signal after 10 seconds")
}
tester.Close()
}
type X struct{}
func (x *X) Method1() *Error {
return nil
}
func TestRaceInExport(t *testing.T) {
const (
dbusPath = "/org/example/godbus/test1"
dbusInterface = "org.example.godbus.test1"
)
bus, err := ConnectSessionBus()
if err != nil {
t.Fatal(err)
}
defer bus.Close()
var x X
var wg sync.WaitGroup
wg.Add(2)
go func() {
err = bus.Export(&x, dbusPath, dbusInterface)
if err != nil {
t.Error(err)
}
wg.Done()
}()
go func() {
obj := bus.Object(bus.Names()[0], dbusPath)
obj.Call(dbusInterface+".Method1", 0)
wg.Done()
}()
wg.Wait()
}