1 # -*- coding: utf-8 -*-
3 # Unix SMB/CIFS implementation.
4 # Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """Tests for samba.messaging."""
22 from samba.messaging import Messaging
23 from samba.tests import TestCase
25 from samba.ndr import ndr_print
26 from samba.dcerpc import server_id
31 class MessagingTests(TestCase):
33 def get_context(self, *args, **kwargs):
34 kwargs['lp_ctx'] = samba.tests.env_loadparm()
35 return Messaging(*args, **kwargs)
37 def test_register(self):
38 x = self.get_context()
42 msg_type = x.register((callback, None))
43 self.assertTrue(isinstance(msg_type, int))
44 x.deregister(callback, msg_type)
46 def test_all_servers(self):
47 x = self.get_context()
48 self.assertTrue(isinstance(x.irpc_all_servers(), list))
50 def test_by_name(self):
51 x = self.get_context()
52 for name in x.irpc_all_servers():
53 self.assertTrue(isinstance(x.irpc_servers_byname(name.name), list))
55 def test_unknown_name(self):
56 x = self.get_context()
57 self.assertRaises(KeyError,
58 x.irpc_servers_byname, "samba.messaging test NONEXISTING")
60 def test_assign_server_id(self):
61 x = self.get_context()
62 self.assertTrue(isinstance(x.server_id, server_id.server_id))
64 def test_add_remove_name(self):
65 x = self.get_context()
66 name = "samba.messaging test-%d" % random.randint(1, 1000000)
68 name_list = x.irpc_servers_byname(name)
69 self.assertEqual(len(name_list), 1)
70 self.assertEqual(ndr_print(x.server_id),
71 ndr_print(name_list[0]))
72 x.irpc_remove_name(name)
73 self.assertRaises(KeyError,
74 x.irpc_servers_byname, name)
76 def test_ping_speed(self):
77 got_ping = {"count": 0}
78 got_pong = {"count": 0}
84 server_ctx = self.get_context((0, 1))
86 def ping_callback(got_ping, msg_type, src, data):
87 got_ping["count"] += 1
88 server_ctx.send(src, msg_pong, data)
90 msg_ping = server_ctx.register((ping_callback, got_ping))
92 def pong_callback(got_pong, msg_type, src, data):
93 got_pong["count"] += 1
95 client_ctx = self.get_context((0, 2))
96 msg_pong = client_ctx.register((pong_callback, got_pong))
98 # Try both server_id forms (structure and tuple)
99 client_ctx.send((0, 1), msg_ping, "testing")
101 client_ctx.send((0, 1), msg_ping, "testing2")
103 start_time = time.time()
105 # NOTE WELL: If debugging this with GDB, then the timeout will
106 # fire while you are trying to understand it.
108 while (got_ping["count"] < 2 or got_pong["count"] < 2) and not timeout:
109 client_ctx.loop_once(0.1)
110 server_ctx.loop_once(0.1)
111 if time.time() - start_time > 1:
114 self.assertEqual(got_ping["count"], 2)
115 self.assertEqual(got_pong["count"], 2)
117 def test_pid_defaulting(self):
118 got_ping = {"count": 0}
119 got_pong = {"count": 0}
126 server_ctx = self.get_context((pid, 1))
128 def ping_callback(got_ping, msg_type, src, data):
129 got_ping["count"] += 1
130 server_ctx.send(src, msg_pong, data)
132 msg_ping = server_ctx.register((ping_callback, got_ping))
134 def pong_callback(got_pong, msg_type, src, data):
135 got_pong["count"] += 1
137 client_ctx = self.get_context((2,))
138 msg_pong = client_ctx.register((pong_callback, got_pong))
140 # Try one and two element tuple forms
141 client_ctx.send((pid, 1), msg_ping, "testing")
143 client_ctx.send((1,), msg_ping, "testing2")
145 start_time = time.time()
147 # NOTE WELL: If debugging this with GDB, then the timeout will
148 # fire while you are trying to understand it.
150 while (got_ping["count"] < 2 or got_pong["count"] < 2) and not timeout:
151 client_ctx.loop_once(0.1)
152 server_ctx.loop_once(0.1)
153 if time.time() - start_time > 1:
156 self.assertEqual(got_ping["count"], 2)
157 self.assertEqual(got_pong["count"], 2)