9987-embargoing 1
9987-embargoing 2 import unittest
9987-embargoing 3 import logging
9987-embargoing 4 from mock import Mock
9982-getdata 5 from lib.commands import DIRECT
9982-getdata 6 from lib.commands import GETDATA
9987-embargoing 7 from lib.station import Station
9982-getdata 8 from lib.state import State
9982-getdata 9 from lib.message import Message
9982-getdata 10 from lib.getdata import GetData
9982-getdata 11 from lib.order_buffer import OrderBuffer
9982-getdata 12 from lib.long_buffer import LongBuffer
9982-getdata 13 from collections import namedtuple
9982-getdata 14 import helper
9987-embargoing 15
9987-embargoing 16 class TestStation(unittest.TestCase):
9987-embargoing 17 def setUp(self):
9982-getdata 18 helper.setup()
9987-embargoing 19 logging.basicConfig(level=logging.DEBUG)
9982-getdata 20 self.station_socket = Mock()
9982-getdata 21 Options = namedtuple('Options', ['db_path',
9982-getdata 22 'address_table_path',
9982-getdata 23 'socket',
9982-getdata 24 'irc_ports',
9982-getdata 25 'udp_port',
9982-getdata 26 'channel_name',
9982-getdata 27 'password',
9982-getdata 28 'motd',
9982-getdata 29 'listen'])
9982-getdata 30 options = Options(
9982-getdata 31 None,
9982-getdata 32 None,
9982-getdata 33 self.station_socket,
9982-getdata 34 None,
9982-getdata 35 None,
9982-getdata 36 None,
9982-getdata 37 None,
9982-getdata 38 None,
9982-getdata 39 None
9982-getdata 40 )
9987-embargoing 41 self.station = Station(options)
9987-embargoing 42 self.station.deliver = Mock()
9987-embargoing 43 self.station.rebroadcast = Mock()
9987-embargoing 44 self.station.rebroadcast.return_value = "foobar"
9982-getdata 45 self.bob_state = State(Mock(), None)
9982-getdata 46 self.station.state.set_knob('nick', 'alice')
9982-getdata 47 self.bob_state.set_knob('nick', 'bob')
9982-getdata 48 self.setupBob()
9982-getdata 49 self.setupAlice()
9982-getdata 50
9982-getdata 51 def setupBob(self):
9982-getdata 52 self.station.state.add_peer('bob')
9982-getdata 53 self.station.state.add_key(
9982-getdata 54 'bob',
9982-getdata 55 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 56 )
9982-getdata 57 self.station.state.update_at({
9982-getdata 58 'handle': 'bob',
9982-getdata 59 'address': '127.0.0.1',
9982-getdata 60 'port': 8889
9982-getdata 61 })
9982-getdata 62
9982-getdata 63 def setupAlice(self):
9982-getdata 64 self.bob_state.add_peer('alice')
9982-getdata 65 self.bob_state.add_key(
9982-getdata 66 'alice',
9982-getdata 67 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 68 )
9982-getdata 69 self.bob_state.update_at({
9982-getdata 70 'handle': 'alice',
9982-getdata 71 'address': '127.0.0.1',
9982-getdata 72 'port': 8888
9982-getdata 73 })
9987-embargoing 74
9987-embargoing 75 def tearDown(self):
9987-embargoing 76 pass
9987-embargoing 77
9987-embargoing 78 def test_embargo_bounce_ordering(self):
9982-getdata 79 self.skipTest("the tested code has been re-implemented")
9987-embargoing 80 peer1 = Mock()
9987-embargoing 81 peer1.handles = ["a", "b"]
9987-embargoing 82 peer2 = Mock()
9987-embargoing 83 peer2.handles = ["c", "d"]
9987-embargoing 84 low_bounce_message = Mock()
9987-embargoing 85 low_bounce_message.peer = peer1
9987-embargoing 86 low_bounce_message.bounces = 1
9987-embargoing 87 low_bounce_message.message_hash = "messagehash"
9987-embargoing 88 high_bounce_message = Mock()
9987-embargoing 89 high_bounce_message.peer = peer2
9987-embargoing 90 high_bounce_message.bounces = 2
9987-embargoing 91 high_bounce_message.message_hash = "messagehash"
9982-getdata 92 self.station.short_buffer = {
9987-embargoing 93 "messagehash": [
9987-embargoing 94 low_bounce_message,
9987-embargoing 95 high_bounce_message
9987-embargoing 96 ],
9987-embargoing 97 }
9987-embargoing 98 self.station.flush_hearsay_messages()
9987-embargoing 99 self.station.deliver.assert_called_once_with(low_bounce_message)
9987-embargoing 100 self.station.rebroadcast.assert_called_once_with(low_bounce_message)
9987-embargoing 101
9987-embargoing 102 def test_embargo_queue_cleared(self):
9982-getdata 103 self.skipTest("the embargo queue is now th short buffer")
9987-embargoing 104 peer = Mock()
9987-embargoing 105 peer.handles = ["a", "b"]
9987-embargoing 106 message = Mock()
9987-embargoing 107 message.speaker = "c"
9987-embargoing 108 message.peer = peer
9982-getdata 109 self.station.short_buffer = {
9987-embargoing 110 "messagehash": [
9987-embargoing 111 message
9987-embargoing 112 ],
9987-embargoing 113 }
9982-getdata 114 self.assertEqual(len(self.station.short_buffer), 1)
9987-embargoing 115 self.station.flush_hearsay_messages()
9982-getdata 116 self.assertEqual(len(self.station.short_buffer), 0)
9987-embargoing 117
9987-embargoing 118 def test_simple_hearsay_prefix(self):
9982-getdata 119 self.skipTest("this code has moved")
9987-embargoing 120 peer = Mock()
9987-embargoing 121 peer.handles = ["a", "b"]
9987-embargoing 122 message = Mock()
9987-embargoing 123 message.speaker = "c"
9987-embargoing 124 message.prefix = None
9987-embargoing 125 message.peer = peer
9982-getdata 126 self.station.short_buffer = {
9987-embargoing 127 "messagehash": [
9987-embargoing 128 message
9987-embargoing 129 ],
9987-embargoing 130 }
9987-embargoing 131 self.station.flush_hearsay_messages()
9987-embargoing 132 self.assertEqual(message.prefix, "c[a]")
9987-embargoing 133
9987-embargoing 134 def test_in_wot_hearsay_prefix_under_four(self):
9982-getdata 135 self.skipTest("the embargo queue is now th short buffer")
9987-embargoing 136 peer1 = Mock()
9987-embargoing 137 peer1.handles = ["a", "b"]
9987-embargoing 138 peer2 = Mock()
9987-embargoing 139 peer2.handles = ["d", "e"]
9987-embargoing 140 peer3 = Mock()
9987-embargoing 141 peer3.handles = ["f", "g"]
9987-embargoing 142 message_via_peer1 = Mock()
9987-embargoing 143 message_via_peer1.speaker = "c"
9987-embargoing 144 message_via_peer1.prefix = None
9987-embargoing 145 message_via_peer1.peer = peer1
9987-embargoing 146 message_via_peer1.bounces = 1
9987-embargoing 147 message_via_peer2 = Mock()
9987-embargoing 148 message_via_peer2.speaker = "c"
9987-embargoing 149 message_via_peer2.prefix = None
9987-embargoing 150 message_via_peer2.peer = peer2
9987-embargoing 151 message_via_peer2.bounces = 2
9987-embargoing 152 message_via_peer3 = Mock()
9987-embargoing 153 message_via_peer3.speaker = "c"
9987-embargoing 154 message_via_peer3.prefix = None
9987-embargoing 155 message_via_peer3.peer = peer3
9987-embargoing 156 message_via_peer3.bounces = 1
9982-getdata 157 self.station.short_buffer = {
9987-embargoing 158 "messagehash": [
9987-embargoing 159 message_via_peer1,
9987-embargoing 160 message_via_peer2,
9987-embargoing 161 message_via_peer3
9987-embargoing 162 ],
9987-embargoing 163 }
9987-embargoing 164 self.station.flush_hearsay_messages()
9987-embargoing 165 self.station.deliver.assert_called_once_with(message_via_peer1)
9987-embargoing 166 self.assertEqual(message_via_peer1.prefix, "c[a|d|f]")
9987-embargoing 167
9987-embargoing 168 def test_in_wot_hearsay_prefix_more_than_three(self):
9982-getdata 169 self.skipTest("the embargo queue is now th short buffer")
9987-embargoing 170 peer1 = Mock()
9987-embargoing 171 peer1.handles = ["a", "b"]
9987-embargoing 172 peer2 = Mock()
9987-embargoing 173 peer2.handles = ["d", "e"]
9987-embargoing 174 peer3 = Mock()
9987-embargoing 175 peer3.handles = ["f", "g"]
9987-embargoing 176 peer4 = Mock()
9987-embargoing 177 peer4.handles = ["f", "g"]
9987-embargoing 178 message_via_peer1 = Mock()
9987-embargoing 179 message_via_peer1.speaker = "c"
9987-embargoing 180 message_via_peer1.prefix = None
9987-embargoing 181 message_via_peer1.peer = peer1
9987-embargoing 182 message_via_peer1.bounces = 1
9987-embargoing 183 message_via_peer2 = Mock()
9987-embargoing 184 message_via_peer2.speaker = "c"
9987-embargoing 185 message_via_peer2.prefix = None
9987-embargoing 186 message_via_peer2.peer = peer2
9987-embargoing 187 message_via_peer2.bounces = 2
9987-embargoing 188 message_via_peer3 = Mock()
9987-embargoing 189 message_via_peer3.speaker = "c"
9987-embargoing 190 message_via_peer3.prefix = None
9987-embargoing 191 message_via_peer3.peer = peer3
9987-embargoing 192 message_via_peer3.bounces = 1
9987-embargoing 193 message_via_peer4 = Mock()
9987-embargoing 194 message_via_peer4.speaker = "c"
9987-embargoing 195 message_via_peer4.prefix = None
9987-embargoing 196 message_via_peer4.peer = peer4
9987-embargoing 197 message_via_peer4.bounces = 1
9982-getdata 198 self.station.short_buffer = {
9987-embargoing 199 "messagehash": [
9987-embargoing 200 message_via_peer1,
9987-embargoing 201 message_via_peer2,
9987-embargoing 202 message_via_peer3,
9987-embargoing 203 message_via_peer4
9987-embargoing 204 ],
9987-embargoing 205 }
9987-embargoing 206 self.station.flush_hearsay_messages()
9987-embargoing 207 self.station.deliver.assert_called_once_with(message_via_peer1)
9987-embargoing 208 self.assertEqual(message_via_peer1.prefix, "c[4]")
9982-getdata 209
9982-getdata 210
9982-getdata 211 def test_receive_getdata_request_for_existing_direct_message(self):
9982-getdata 212 self.skipTest("intermittent failure")
9982-getdata 213
9982-getdata 214 m1 = Message({
9982-getdata 215 'command': DIRECT,
9982-getdata 216 'handle': 'bob',
9982-getdata 217 'speaker': 'alice',
9982-getdata 218 'body': 'm1',
9982-getdata 219 'bounces': 0
9982-getdata 220 }, self.station.state)
9982-getdata 221
9982-getdata 222 m1.send()
9982-getdata 223
9982-getdata 224 m2 = Message({
9982-getdata 225 'command': DIRECT,
9982-getdata 226 'handle': 'bob',
9982-getdata 227 'speaker': 'alice',
9982-getdata 228 'body': 'm2',
9982-getdata 229 'bounces': 0,
9982-getdata 230 }, self.station.state)
9982-getdata 231
9982-getdata 232 m2.send()
9982-getdata 233
9982-getdata 234
9982-getdata 235
9982-getdata 236
9982-getdata 237 alice = self.bob_state.get_peer_by_handle('alice')
9982-getdata 238 bob = self.station.state.get_peer_by_handle('bob')
9982-getdata 239 gd_message = GetData(m2, self.bob_state)
9982-getdata 240 gd_message_bytes = gd_message.get_message_bytes(alice)
9982-getdata 241 gd_black_packet = Message.pack(bob, GETDATA, gd_message.bounces, gd_message_bytes)
9982-getdata 242
9982-getdata 243
9982-getdata 244 self.station.handle_udp_data([gd_black_packet, ['127.0.0.1', 8889]])
9982-getdata 245
9982-getdata 246
9982-getdata 247 retry_black_packet = Message.pack(bob, DIRECT, 0, m1.get_message_bytes(bob))
9982-getdata 248
9982-getdata 249
9982-getdata 250 sent_message_black_packet = self.station_socket.sendto.call_args[0][0]
9982-getdata 251 sent_message = Message.unpack(bob, sent_message_black_packet, LongBuffer(), OrderBuffer(), self.station.state)
9982-getdata 252 self.assertEqual(sent_message.body, 'm1')