def test_auth_tcp(self): # Test that authentication works over TCP server = DbusServer(echo_app) addr = 'tcp:host=127.0.0.1,port=0' server.listen(addr) client = DbusClient() client.connect(server.addresses[0]) cproto = client.connection[1] cauth = cproto._authenticator sproto = list(server.connections)[0][1] sauth = sproto._authenticator self.assertTrue(cauth.authenticationSucceeded()) self.assertTrue(sauth.authenticationSucceeded()) self.assertIsInstance(cproto.server_guid, six.text_type) self.assertTrue(cproto.server_guid.isalnum()) self.assertEqual(cproto.server_guid, cauth.getGUID()) self.assertEqual(cproto.server_guid, sproto.server_guid) self.assertEqual(sproto.server_guid, sauth.getGUID()) self.assertEqual(cauth.getMechanismName(), sauth.getMechanismName()) if hasattr(os, 'fork'): self.assertEqual(cauth.getMechanismName(), 'DBUS_COOKIE_SHA1') else: self.assertEqual(cauth.getMechanismName(), 'ANONYMOUS') client.close() server.close()
def test_auth_pipe(self): # Test that authentication works over a Pipe. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) cproto = client.connection[1] cauth = cproto._authenticator sproto = list(server.connections)[0][1] sauth = sproto._authenticator self.assertTrue(cauth.authenticationSucceeded()) self.assertTrue(sauth.authenticationSucceeded()) self.assertIsInstance(cproto.server_guid, six.text_type) self.assertTrue(cproto.server_guid.isalnum()) self.assertEqual(cproto.server_guid, cauth.getGUID()) self.assertEqual(cproto.server_guid, sproto.server_guid) self.assertEqual(sproto.server_guid, sauth.getGUID()) self.assertEqual(cauth.getMechanismName(), sauth.getMechanismName()) if hasattr(socket, 'SO_PEERCRED'): self.assertEqual(cauth.getMechanismName(), 'EXTERNAL') elif hasattr(os, 'fork'): self.assertEqual(cauth.getMechanismName(), 'DBUS_COOKIE_SHA1') else: self.assertEqual(cauth.getMechanismName(), 'ANONYMOUS') client.close() server.close()
def test_call_method_tcp(self): # Ensure that calling a method over TCP works. server = DbusServer(echo_app) addr = 'tcp:host=127.0.0.1,port=0' server.listen(addr) client = DbusClient() client.connect(server.addresses[0]) result = client.call_method('bus.name', '/path', 'my.iface', 'Echo') self.assertEqual(result, ()) server.close() client.close()
def test_call_method(self): # Ensure that calling a method over a Unix socket works. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) result = client.call_method('bus.name', '/path', 'my.iface', 'Echo') self.assertEqual(result, ()) server.close() client.close()
def test_call_method_error(self): # Ensure that a method can return an error and that in this case a # DbusMethodCallError is raised. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) exc = self.assertRaises(DbusMethodCallError, client.call_method, 'bus.name', '/path', 'my.iface', 'Error') self.assertEqual(exc.error, 'Echo.Error') self.assertEqual(exc.args, ()) server.close() client.close()
def test_get_unique_name(self): # Ensure that get_unique_name() works client and server side server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) unique_name = client.get_unique_name() self.assertIsInstance(unique_name, six.text_type) self.assertTrue(unique_name.startswith(':')) sproto = list(server.connections)[0][1] self.assertEqual(unique_name, sproto.get_unique_name()) server.close() client.close()
def test_call_method_error_args(self): # Call a method that will return an error with arguments. The arguments # should be available from the exception. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) exc = self.assertRaises(DbusMethodCallError, client.call_method, 'bus.name', '/path', 'my.iface', 'Error', signature='ss', args=('foo', 'bar')) self.assertEqual(exc.error, 'Echo.Error') self.assertEqual(exc.args, ('foo', 'bar')) server.close() client.close()
def test_call_method_int_args(self): # Ensure that calling a method with integer arguments works. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) result = client.call_method('bus.name', '/path', 'my.iface', 'Echo', signature='i', args=[1]) self.assertEqual(result, (1,)) result = client.call_method('bus.name', '/path', 'my.iface', 'Echo', signature='ii', args=[1, 2]) self.assertEqual(result, (1, 2)) server.close() client.close()
def test_call_method_str_args(self): # Ensure that calling a method with string arguments works. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) result = client.call_method('bus.name', '/path', 'my.iface', 'Echo', signature='s', args=['foo']) self.assertEqual(result, ('foo',)) result = client.call_method('bus.name', '/path', 'my.iface', 'Echo', signature='ss', args=['foo', 'bar']) self.assertEqual(result, ('foo', 'bar')) server.close() client.close()
def perf_message_throughput_tcp(self): # Test roundtrips of a simple method call over TCP. server = DbusServer(echo_app) addr = 'tcp:host=127.0.0.1,port=0' server.listen(addr) client = DbusClient() client.connect(server.addresses[0]) nmessages = 0 t0 = t1 = time.time() while t1 - t0 < 0.2: client.call_method('bus.name', '/path', 'my.iface', 'Echo') t1 = time.time() nmessages += 1 throughput = nmessages / (t1 - t0) self.add_result(throughput) server.close() client.close()
def perf_message_throughput_pipe(self): # Test roundtrips of a simple method call over a Pipe server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) nmessages = 0 t0 = t1 = time.time() while t1 - t0 < 0.2: client.call_method('bus.name', '/path', 'my.iface', 'Echo') t1 = time.time() nmessages += 1 throughput = nmessages / (t1 - t0) self.add_result(throughput) server.close() client.close()
def test_send_garbage(self): # Send random garbage and ensure the connection gets dropped. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) client = DbusClient() client.connect(addr) exc = None try: while True: chunk = os.urandom(100) client.transport.write(chunk) gruvi.sleep(0) except Exception as e: exc = e self.assertIsInstance(exc, TransportError) server.close() client.close()
def test_connection_limit(self): # Establish more connections than the DBUS server is willing to accept. # The connections should be closed. server = DbusServer(echo_app) addr = 'unix:path=' + self.pipename() server.listen(addr) server.max_connections = 10 clients = [] exc = None try: for i in range(15): client = DbusClient() client.connect(addr) clients.append(client) except Exception as e: exc = e self.assertIsInstance(exc, TransportError) self.assertLessEqual(len(server.connections), server.max_connections) for client in clients: client.close() server.close()