3 # Copyright (C) 2004 Red Hat Inc. <http://www.redhat.com/>
4 # Copyright (C) 2005, 2006 Collabora Ltd. <http://www.collabora.co.uk/>
6 # Licensed under the Academic Free License version 2.1
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 builddir
= os
.path
.normpath(os
.environ
["DBUS_TOP_BUILDDIR"])
31 pydir
= os
.path
.normpath(os
.environ
["DBUS_TOP_SRCDIR"])
44 if not pkg
.startswith(pydir
):
45 raise Exception("DBus modules (%s) are not being picked up from the package"%pkg
)
47 if not _dbus_bindings
.__file
__.startswith(builddir
):
48 raise Exception("DBus modules (%s) are not being picked up from the package"%_dbus_bindings
.__file
__)
50 test_types_vals
= [1, 12323231, 3.14159265, 99999999.99,
51 "dude", "123", "What is all the fuss about?", "gob@gob.com",
52 u
'\\u310c\\u310e\\u3114', u
'\\u0413\\u0414\\u0415',
53 u
'\\u2200software \\u2203crack', u
'\\xf4\\xe5\\xe8',
54 [1,2,3], ["how", "are", "you"], [1.23,2.3], [1], ["Hello"],
55 (1,2,3), (1,), (1,"2",3), ("2", "what"), ("you", 1.2),
56 {1:"a", 2:"b"}, {"a":1, "b":2}, #{"a":(1,"B")},
57 {1:1.1, 2:2.2}, [[1,2,3],[2,3,4]], [["a","b"],["c","d"]],
59 dbus
.Int16(-10), dbus
.UInt16(10), 'SENTINEL',
60 #([1,2,3],"c", 1.2, ["a","b","c"], {"a": (1,"v"), "b": (2,"d")})
63 NAME
= "org.freedesktop.DBus.TestSuitePythonService"
64 IFACE
= "org.freedesktop.DBus.TestSuiteInterface"
65 OBJECT
= "/org/freedesktop/DBus/TestSuitePythonObject"
67 class TestDBusBindings(unittest
.TestCase
):
69 self
.bus
= dbus
.SessionBus()
70 self
.remote_object
= self
.bus
.get_object(NAME
, OBJECT
)
71 self
.remote_object_follow
= self
.bus
.get_object(NAME
, OBJECT
,
72 follow_name_owner_changes
=True)
73 self
.iface
= dbus
.Interface(self
.remote_object
, IFACE
)
75 def testGObject(self
):
76 print "Testing ExportedGObject... ",
77 remote_gobject
= self
.bus
.get_object(NAME
, OBJECT
+ '/GObject')
78 iface
= dbus
.Interface(remote_gobject
, IFACE
)
79 print "introspection, ",
80 remote_gobject
.Introspect(dbus_interface
=dbus
.INTROSPECTABLE_IFACE
)
81 print "method call, ",
82 self
.assertEquals(iface
.Echo('123'), '123')
85 def testWeakRefs(self
):
86 # regression test for Sugar crash caused by smcv getting weak refs
87 # wrong - pre-bugfix, this would segfault
88 bus
= dbus
.SessionBus(private
=True)
89 ref
= weakref
.ref(bus
)
90 self
.assert_(ref() is bus
)
92 self
.assert_(ref() is None)
94 def testInterfaceKeyword(self
):
95 #test dbus_interface parameter
96 print self
.remote_object
.Echo("dbus_interface on Proxy test Passed", dbus_interface
= IFACE
)
97 print self
.iface
.Echo("dbus_interface on Interface test Passed", dbus_interface
= IFACE
)
100 def testGetDBusMethod(self
):
101 self
.assertEquals(self
.iface
.get_dbus_method('AcceptListOfByte')('\1\2\3'), [1,2,3])
102 self
.assertEquals(self
.remote_object
.get_dbus_method('AcceptListOfByte', dbus_interface
=IFACE
)('\1\2\3'), [1,2,3])
104 def testCallingConventionOptions(self
):
105 self
.assertEquals(self
.iface
.AcceptListOfByte('\1\2\3'), [1,2,3])
106 self
.assertEquals(self
.iface
.AcceptListOfByte('\1\2\3', byte_arrays
=True), '\1\2\3')
107 self
.assertEquals(self
.iface
.AcceptByteArray('\1\2\3'), [1,2,3])
108 self
.assertEquals(self
.iface
.AcceptByteArray('\1\2\3', byte_arrays
=True), '\1\2\3')
109 self
.assert_(isinstance(self
.iface
.AcceptUTF8String('abc'), unicode))
110 self
.assert_(isinstance(self
.iface
.AcceptUTF8String('abc', utf8_strings
=True), str))
111 self
.assert_(isinstance(self
.iface
.AcceptUnicodeString('abc'), unicode))
112 self
.assert_(isinstance(self
.iface
.AcceptUnicodeString('abc', utf8_strings
=True), str))
114 def testIntrospection(self
):
116 print "\n********* Introspection Test ************"
117 print self
.remote_object
.Introspect(dbus_interface
="org.freedesktop.DBus.Introspectable")
118 print "Introspection test passed"
121 def testMultiPathIntrospection(self
):
122 # test introspection on an object exported in multiple places
123 # https://bugs.freedesktop.org/show_bug.cgi?id=11794
124 remote_object
= self
.bus
.get_object(NAME
, OBJECT
+ '/Multi1')
125 remote_object
.Introspect(dbus_interface
="org.freedesktop.DBus.Introspectable")
126 remote_object
= self
.bus
.get_object(NAME
, OBJECT
+ '/Multi2')
127 remote_object
.Introspect(dbus_interface
="org.freedesktop.DBus.Introspectable")
128 remote_object
= self
.bus
.get_object(NAME
, OBJECT
+ '/Multi2/3')
129 remote_object
.Introspect(dbus_interface
="org.freedesktop.DBus.Introspectable")
132 def testPythonTypes(self
):
133 #test sending python types and getting them back
134 print "\n********* Testing Python Types ***********"
136 for send_val
in test_types_vals
:
137 print "Testing %s"% str(send_val
)
138 recv_val
= self
.iface
.Echo(send_val
)
139 self
.assertEquals(send_val
, recv_val
)
140 self
.assertEquals(recv_val
.variant_level
, 1)
142 def testMethodExtraInfoKeywords(self
):
143 print "Testing MethodExtraInfoKeywords..."
144 sender
, path
, destination
, message_cls
= self
.iface
.MethodExtraInfoKeywords()
145 self
.assert_(sender
.startswith(':'))
146 self
.assertEquals(path
, '/org/freedesktop/DBus/TestSuitePythonObject')
147 # we're using the "early binding" form of get_object (without
148 # follow_name_owner_changes), so the destination we actually sent it
149 # to will be the unique name
150 self
.assert_(destination
.startswith(':'))
151 self
.assertEquals(message_cls
, 'dbus.lowlevel.MethodCallMessage')
153 def testUtf8StringsSync(self
):
155 recv_val
= self
.iface
.Echo(send_val
, utf8_strings
=True)
156 self
.assert_(isinstance(recv_val
, str))
157 self
.assert_(isinstance(recv_val
, dbus
.UTF8String
))
158 recv_val
= self
.iface
.Echo(send_val
, utf8_strings
=False)
159 self
.assert_(isinstance(recv_val
, unicode))
160 self
.assert_(isinstance(recv_val
, dbus
.String
))
162 def testBenchmarkIntrospect(self
):
163 print "\n********* Benchmark Introspect ************"
166 print self
.iface
.GetComplexArray()
169 print "Delta: %f" % (b
- a
)
172 def testAsyncCalls(self
):
173 #test sending python types and getting them back async
174 print "\n********* Testing Async Calls ***********"
177 main_loop
= gobject
.MainLoop()
180 def __init__(self
, test_controler
, expected_result
, do_exit
, utf8
):
181 self
.expected_result
= expected_result
182 self
.do_exit
= do_exit
184 self
.test_controler
= test_controler
186 def callback(self
, val
):
191 self
.test_controler
.assertEquals(val
, self
.expected_result
)
192 self
.test_controler
.assertEquals(val
.variant_level
, 1)
193 if self
.utf8
and not isinstance(val
, dbus
.UTF8String
):
194 failures
.append('%r should have been utf8 but was not' % val
)
196 elif not self
.utf8
and isinstance(val
, dbus
.UTF8String
):
197 failures
.append('%r should not have been utf8' % val
)
200 failures
.append("%s:\n%s" % (e
.__class
__, e
))
202 def error_handler(self
, error
):
207 failures
.append('%s: %s' % (error
.__class
__, error
))
209 last_type
= test_types_vals
[-1]
210 for send_val
in test_types_vals
:
211 print "Testing %s" % str(send_val
)
212 utf8
= (send_val
== 'gob@gob.com')
213 check
= async_check(self
, send_val
, last_type
== send_val
,
215 recv_val
= self
.iface
.Echo(send_val
,
216 reply_handler
=check
.callback
,
217 error_handler
=check
.error_handler
,
221 self
.assert_(False, failures
)
223 def testStrictMarshalling(self
):
224 print "\n********* Testing strict return & signal marshalling ***********"
226 # these values are the same as in the server, and the
227 # methods should only succeed when they are called with
228 # the right value number, because they have out_signature
229 # decorations, and return an unmatching type when called
230 # with a different number
231 values
= ["", ("",""), ("","",""), [], {}, ["",""], ["","",""]]
233 (self
.iface
.ReturnOneString
, 'SignalOneString', set([0]), set([0])),
234 (self
.iface
.ReturnTwoStrings
, 'SignalTwoStrings', set([1, 5]), set([1])),
235 (self
.iface
.ReturnStruct
, 'SignalStruct', set([1, 5]), set([1])),
236 # all of our test values are sequences so will marshall correctly into an array :P
237 (self
.iface
.ReturnArray
, 'SignalArray', set(range(len(values
))), set([3, 5, 6])),
238 (self
.iface
.ReturnDict
, 'SignalDict', set([0, 3, 4]), set([4]))
241 for (method
, signal
, success_values
, return_values
) in methods
:
242 print "\nTrying correct behaviour of", method
._method
_name
243 for value
in range(len(values
)):
247 print "%s(%r) raised %s: %s" % (method
._method
_name
, values
[value
], e
.__class
__, e
)
249 # should fail if it tried to marshal the wrong type
250 self
.assert_(value
not in success_values
, "%s should succeed when we ask it to return %r\n%s\n%s" % (method
._method
_name
, values
[value
], e
.__class
__, e
))
252 print "%s(%r) returned %r" % (method
._method
_name
, values
[value
], ret
)
254 # should only succeed if it's the right return type
255 self
.assert_(value
in success_values
, "%s should fail when we ask it to return %r" % (method
._method
_name
, values
[value
]))
257 # check the value is right too :D
258 returns
= map(lambda n
: values
[n
], return_values
)
259 self
.assert_(ret
in returns
, "%s should return one of %r but it returned %r instead" % (method
._method
_name
, returns
, ret
))
261 print "\nTrying correct emission of", signal
262 for value
in range(len(values
)):
264 self
.iface
.EmitSignal(signal
, value
)
266 print "EmitSignal(%s, %r) raised %s" % (signal
, values
[value
], e
.__class
__)
268 # should fail if it tried to marshal the wrong type
269 self
.assert_(value
not in success_values
, "EmitSignal(%s) should succeed when we ask it to return %r\n%s\n%s" % (signal
, values
[value
], e
.__class
__, e
))
271 print "EmitSignal(%s, %r) appeared to succeed" % (signal
, values
[value
])
273 # should only succeed if it's the right return type
274 self
.assert_(value
in success_values
, "EmitSignal(%s) should fail when we ask it to return %r" % (signal
, values
[value
]))
276 # FIXME: wait for the signal here
280 def testInheritance(self
):
281 print "\n********* Testing inheritance from dbus.method.Interface ***********"
282 ret
= self
.iface
.CheckInheritance()
283 print "CheckInheritance returned %s" % ret
284 self
.assert_(ret
, "overriding CheckInheritance from TestInterface failed")
286 def testAsyncMethods(self
):
287 print "\n********* Testing asynchronous method implementation *******"
288 for async in (True, False):
289 for fail
in (True, False):
291 val
= ('a', 1, False, [1,2], {1:2})
292 print "calling AsynchronousMethod with %s %s %s" % (async, fail
, val
)
293 ret
= self
.iface
.AsynchronousMethod(async, fail
, val
)
295 self
.assert_(fail
, '%s: %s' % (e
.__class
__, e
))
296 print "Expected failure: %s: %s" % (e
.__class
__, e
)
298 self
.assert_(not fail
, 'Expected failure but succeeded?!')
299 self
.assertEquals(val
, ret
)
300 self
.assertEquals(1, ret
.variant_level
)
302 def testBusInstanceCaching(self
):
303 print "\n********* Testing dbus.Bus instance sharing *********"
305 # unfortunately we can't test the system bus here
306 # but the codepaths are the same
307 for (cls
, type, func
) in ((dbus
.SessionBus
, dbus
.Bus
.TYPE_SESSION
, dbus
.Bus
.get_session
), (dbus
.StarterBus
, dbus
.Bus
.TYPE_STARTER
, dbus
.Bus
.get_starter
)):
308 print "\nTesting %s:" % cls
.__name
__
311 share_type
= dbus
.Bus(bus_type
=type)
314 private_cls
= cls(private
=True)
315 private_type
= dbus
.Bus(bus_type
=type, private
=True)
316 private_func
= func(private
=True)
318 print " - checking shared instances are the same..."
319 self
.assert_(share_cls
== share_type
, '%s should equal %s' % (share_cls
, share_type
))
320 self
.assert_(share_type
== share_func
, '%s should equal %s' % (share_type
, share_func
))
322 print " - checking private instances are distinct from the shared instance..."
323 self
.assert_(share_cls
!= private_cls
, '%s should not equal %s' % (share_cls
, private_cls
))
324 self
.assert_(share_type
!= private_type
, '%s should not equal %s' % (share_type
, private_type
))
325 self
.assert_(share_func
!= private_func
, '%s should not equal %s' % (share_func
, private_func
))
327 print " - checking private instances are distinct from each other..."
328 self
.assert_(private_cls
!= private_type
, '%s should not equal %s' % (private_cls
, private_type
))
329 self
.assert_(private_type
!= private_func
, '%s should not equal %s' % (private_type
, private_func
))
330 self
.assert_(private_func
!= private_cls
, '%s should not equal %s' % (private_func
, private_cls
))
332 def testSenderName(self
):
333 print '\n******** Testing sender name keyword ********'
334 myself
= self
.iface
.WhoAmI()
337 def testBusGetNameOwner(self
):
338 ret
= self
.bus
.get_name_owner(NAME
)
339 self
.assert_(ret
.startswith(':'), ret
)
341 def testBusListNames(self
):
342 ret
= self
.bus
.list_names()
343 self
.assert_(NAME
in ret
, ret
)
345 def testBusListActivatableNames(self
):
346 ret
= self
.bus
.list_activatable_names()
347 self
.assert_(NAME
in ret
, ret
)
349 def testBusNameHasOwner(self
):
350 self
.assert_(self
.bus
.name_has_owner(NAME
))
351 self
.assert_(not self
.bus
.name_has_owner('badger.mushroom.snake'))
353 def testBusNameCreation(self
):
354 print '\n******** Testing BusName creation ********'
355 test
= [('org.freedesktop.DBus.Python.TestName', True),
356 ('org.freedesktop.DBus.Python.TestName', True),
357 ('org.freedesktop.DBus.Python.InvalidName&^*%$', False)]
358 # Do some more intelligent handling/testing of queueing vs success?
359 # ('org.freedesktop.DBus.TestSuitePythonService', False)]
360 # For some reason this actually succeeds
361 # ('org.freedesktop.DBus', False)]
363 # make a method call to ensure the test service is active
364 self
.iface
.Echo("foo")
367 for (name
, succeed
) in test
:
369 print "requesting %s" % name
370 busname
= dbus
.service
.BusName(name
, dbus
.SessionBus())
372 print "%s:\n%s" % (e
.__class
__, e
)
373 self
.assert_(not succeed
, 'did not expect registering bus name %s to fail' % name
)
376 self
.assert_(succeed
, 'expected registering bus name %s to fail'% name
)
378 self
.assert_(names
[name
] == busname
, 'got a new instance for same name %s' % name
)
379 print "instance of %s re-used, good!" % name
381 names
[name
] = busname
390 ret
= bus
.name_has_owner('org.freedesktop.DBus.Python.TestName')
391 self
.assert_(not ret
, 'deleting reference failed to release BusName org.freedesktop.DBus.Python.TestName')
393 def testMultipleReturnWithoutSignature(self
):
394 # https://bugs.freedesktop.org/show_bug.cgi?id=10174
395 ret
= self
.iface
.MultipleReturnWithoutSignature()
396 self
.assert_(not isinstance(ret
, dbus
.Struct
), repr(ret
))
397 self
.assertEquals(ret
, ('abc', 123))
399 def testListExportedChildObjects(self
):
400 self
.assert_(self
.iface
.TestListExportedChildObjects())
402 def testRemoveFromConnection(self
):
403 # https://bugs.freedesktop.org/show_bug.cgi?id=10457
404 self
.assert_(not self
.iface
.HasRemovableObject())
405 self
.assert_(self
.iface
.AddRemovableObject())
406 self
.assert_(self
.iface
.HasRemovableObject())
408 removable
= self
.bus
.get_object(NAME
, OBJECT
+ '/RemovableObject')
409 iface
= dbus
.Interface(removable
, IFACE
)
410 self
.assert_(iface
.IsThere())
411 self
.assert_(iface
.RemoveSelf())
413 self
.assert_(not self
.iface
.HasRemovableObject())
416 self
.assert_(self
.iface
.AddRemovableObject())
417 self
.assert_(self
.iface
.HasRemovableObject())
418 self
.assert_(iface
.IsThere())
419 self
.assert_(iface
.RemoveSelf())
420 self
.assert_(not self
.iface
.HasRemovableObject())
422 def testFallbackObjectTrivial(self
):
423 obj
= self
.bus
.get_object(NAME
, OBJECT
+ '/Fallback')
424 iface
= dbus
.Interface(obj
, IFACE
)
425 path
, rel
, unique_name
= iface
.TestPathAndConnKeywords()
426 self
.assertEquals(path
, OBJECT
+ '/Fallback')
427 self
.assertEquals(rel
, '/')
428 self
.assertEquals(unique_name
, obj
.bus_name
)
430 def testFallbackObjectNested(self
):
431 obj
= self
.bus
.get_object(NAME
, OBJECT
+ '/Fallback/Nested')
432 iface
= dbus
.Interface(obj
, IFACE
)
433 path
, rel
, unique_name
= iface
.TestPathAndConnKeywords()
434 self
.assertEquals(path
, OBJECT
+ '/Fallback/Nested')
435 self
.assertEquals(rel
, '/')
436 self
.assertEquals(unique_name
, obj
.bus_name
)
438 obj
= self
.bus
.get_object(NAME
, OBJECT
+ '/Fallback/Nested/Badger/Mushroom')
439 iface
= dbus
.Interface(obj
, IFACE
)
440 path
, rel
, unique_name
= iface
.TestPathAndConnKeywords()
441 self
.assertEquals(path
, OBJECT
+ '/Fallback/Nested/Badger/Mushroom')
442 self
.assertEquals(rel
, '/Badger/Mushroom')
443 self
.assertEquals(unique_name
, obj
.bus_name
)
445 def testFallbackObject(self
):
446 obj
= self
.bus
.get_object(NAME
, OBJECT
+ '/Fallback/Badger/Mushroom')
447 iface
= dbus
.Interface(obj
, IFACE
)
448 path
, rel
, unique_name
= iface
.TestPathAndConnKeywords()
449 self
.assertEquals(path
, OBJECT
+ '/Fallback/Badger/Mushroom')
450 self
.assertEquals(rel
, '/Badger/Mushroom')
451 self
.assertEquals(unique_name
, obj
.bus_name
)
453 def testTimeoutSync(self
):
454 self
.assert_(self
.iface
.BlockFor500ms(timeout
=1.0) is None)
455 self
.assertRaises(dbus
.DBusException
,
456 lambda: self
.iface
.BlockFor500ms(timeout
=0.25))
458 def testTimeoutAsyncClient(self
):
459 loop
= gobject
.MainLoop()
462 def correctly_returned():
463 passes
.append('1000')
464 if len(passes
) + len(fails
) >= 2:
466 def correctly_failed(exc
):
468 if len(passes
) + len(fails
) >= 2:
470 def incorrectly_returned():
472 if len(passes
) + len(fails
) >= 2:
474 def incorrectly_failed(exc
):
476 if len(passes
) + len(fails
) >= 2:
478 self
.iface
.BlockFor500ms(timeout
=1.0,
479 reply_handler
=correctly_returned
,
480 error_handler
=incorrectly_failed
)
481 self
.iface
.BlockFor500ms(timeout
=0.25,
482 reply_handler
=incorrectly_returned
,
483 error_handler
=correctly_failed
)
485 self
.assertEquals(passes
, ['250', '1000'])
486 self
.assertEquals(fails
, [])
488 def testTimeoutAsyncService(self
):
489 self
.assert_(self
.iface
.AsyncWait500ms(timeout
=1.0) is None)
490 self
.assertRaises(dbus
.DBusException
,
491 lambda: self
.iface
.AsyncWait500ms(timeout
=0.25))
493 """ Remove this for now
494 class TestDBusPythonToGLibBindings(unittest.TestCase):
496 self.bus = dbus.SessionBus()
497 self.remote_object = self.bus.get_object("org.freedesktop.DBus.TestSuiteGLibService", "/org/freedesktop/DBus/Tests/MyTestObject")
498 self.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.Tests.MyObject")
500 def testIntrospection(self):
502 print "\n********* Introspection Test ************"
503 print self.remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
504 print "Introspection test passed"
508 print "\n********* Call Test ************"
509 result = self.iface.ManyArgs(1000, 'Hello GLib', 2)
511 self.assert_(result == [2002.0, 'HELLO GLIB'])
513 arg0 = {"Dude": 1, "john": "palmieri", "python": 2.4}
514 result = self.iface.ManyStringify(arg0)
517 print "Call test passed"
520 def testPythonTypes(self):
521 print "\n********* Testing Python Types ***********"
523 for send_val in test_types_vals:
524 print "Testing %s"% str(send_val)
525 recv_val = self.iface.EchoVariant(send_val)
526 self.assertEquals(send_val, recv_val.object)
528 if __name__
== '__main__':
529 gobject
.threads_init()
530 dbus
.glib
.init_threads()