-
+ 29EA5E81951450A33616BE0D9609CA1391F9A92A45378ABA6E6DA21058D4AD588FEF569CFF2B4AEE6B98BE416AEC7B6CE01BB09716A19975189BDB2A1281C04F
blatta/tests/test_broadcast.py
(0 . 0)(1 . 69)
3089 import unittest
3090 import helper
3091 from mock import Mock
3092 from lib.state import State
3093 from lib.message import Message
3094 from lib.broadcast import Broadcast
3095 from lib.long_buffer import LongBuffer
3096 from lib.order_buffer import OrderBuffer
3097
3098 class TestMessage(unittest.TestCase):
3099 def setUp(self):
3100 helper.setup()
3101 self.alice_socket = Mock()
3102 self.alice_state = State(self.alice_socket)
3103 self.bob_socket = Mock()
3104 self.bob_state = State(self.bob_socket)
3105 self.setupAlice()
3106 self.setupBob()
3107
3108 def setupAlice(self):
3109 self.bob_state.add_peer('alice')
3110 self.bob_state.add_key(
3111 'alice',
3112 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
3113 )
3114 self.bob_state.update_at({
3115 'handle': 'alice',
3116 'address': '127.0.0.1',
3117 'port': 8888
3118 })
3119
3120 def setupBob(self):
3121 self.alice_state.add_peer('bob')
3122 self.alice_state.add_key(
3123 'bob',
3124 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
3125 )
3126 self.alice_state.update_at({
3127 'handle': 'bob',
3128 'address': '127.0.0.1',
3129 'port': 8889
3130 })
3131
3132 def tearDown(self):
3133 self.alice_state.remove_peer('bob')
3134 self.bob_state.remove_peer('alice')
3135
3136 def test_broadcast_message(self):
3137 message = Broadcast({
3138 'handle': 'bob',
3139 'speaker': 'alice',
3140 'body': 'm1',
3141 'long_buffer': LongBuffer(self.alice_state)
3142 }, self.alice_state)
3143 message.send()
3144 bob = self.alice_state.get_peer_by_handle('bob')
3145 message_bytes = message.get_message_bytes(bob)
3146 black_packet = Message.pack(bob, message.command, message.bounces, message_bytes)
3147 self.bob_socket.sendto.called_once_with(black_packet, (bob.address, bob.port))
3148
3149 # now bob must unpack the black packet
3150 alice = self.bob_state.get_peer_by_handle('alice')
3151 received_message = Message.unpack(alice,
3152 black_packet,
3153 LongBuffer(self.bob_state),
3154 OrderBuffer(self.bob_state),
3155 {})
3156 self.assertEqual(message.body, received_message['body'])
3157