-
+ B48397DE933C7423E2E5C881F18C0E30FF858F31D3DE6EF4C6074EA512AFA85069C9FD0CAF537C2CC41CC4CA8D69FDFE1AE1FD6F7AF9211CCDE47C8311A70776blatta/tests/test_getdata.py(0 . 0)(1 . 118)
3240 import unittest
3241 import logging
3242
3243 import time
3244 from mock import Mock
3245 from lib.message import Message
3246 from lib.getdata import GetData
3247 from lib.long_buffer import LongBuffer
3248 from lib.order_buffer import OrderBuffer
3249 from lib.state import State
3250 from lib.direct import Direct
3251 from lib.broadcast import Broadcast
3252 import helper
3253
3254 class TestGetData(unittest.TestCase):
3255 def setUp(self):
3256 helper.setup()
3257 self.alice_socket = Mock()
3258 self.alice_state = State(self.alice_socket)
3259 self.alice_state.set_knob('nick', 'alice')
3260 self.setupBob()
3261 self.bob_socket = Mock()
3262 self.bob_state = State(self.bob_socket)
3263 self.setupAlice()
3264
3265 def setupBob(self):
3266 self.alice_state.add_peer('bob')
3267 self.alice_state.add_key(
3268 'bob',
3269 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
3270 )
3271 self.alice_state.update_at({
3272 'handle': 'bob',
3273 'address': '127.0.0.1',
3274 'port': 8889
3275 })
3276
3277 def setupAlice(self):
3278 self.bob_state.add_peer('alice')
3279 self.bob_state.add_key(
3280 'alice',
3281 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
3282 )
3283 self.bob_state.update_at({
3284 'handle': 'alice',
3285 'address': '127.0.0.1',
3286 'port': 8888
3287 })
3288
3289 def test_send(self):
3290 long_buffer = LongBuffer(self.bob_state)
3291 m1 = Direct({
3292 'handle': 'alice',
3293 'speaker': 'bob',
3294 'body': 'm1',
3295 'timestamp': int(time.time()),
3296 'long_buffer': long_buffer
3297 }, self.bob_state)
3298 m2 = Direct({
3299 'handle': 'alice',
3300 'speaker': 'bob',
3301 'body': 'm2',
3302 'timestamp': int(time.time()),
3303 'long_buffer': long_buffer
3304 }, self.bob_state)
3305 m3 = Direct({
3306 'handle': 'alice',
3307 'speaker': 'bob',
3308 'body': 'm3',
3309 'timestamp': int(time.time()),
3310 'long_buffer': long_buffer
3311 }, self.bob_state)
3312 # we need to send these messages to get them into the log
3313 alice = self.bob_state.get_peer_by_handle('alice')
3314 m1.message_bytes = m1.get_message_bytes(alice)
3315 m1.send()
3316 m2.message_bytes = m2.get_message_bytes(alice)
3317 m2.send()
3318 m3.message_bytes = m3.get_message_bytes(alice)
3319 m3.send()
3320
3321 # now let's compile the black packet so alice can
3322 # unpack it and get a message we can pass to GetData()
3323 m1_message_bytes = m1.get_message_bytes(alice)
3324 m1_black_packet = Message.pack(alice, m1.command, m1.bounces, m1_message_bytes)
3325
3326 # we use m3 because if we used m2 there would be no break,
3327 # and if we used m1 it would be considered the first message
3328 m3_message_bytes = m3.get_message_bytes(alice)
3329 # TODO: something strange going on here with the message bytes causing the logger to barf
3330 m3_black_packet = Message.pack(alice, m3.command, m3.bounces, m3_message_bytes)
3331
3332
3333 # we need bob's peer object to know what key to use to decrypt
3334 bob = self.alice_state.get_peer_by_handle('bob')
3335 m1_received = Message.unpack(bob,
3336 m1_black_packet,
3337 LongBuffer(self.alice_state),
3338 OrderBuffer(self.alice_state),
3339 {},
3340 self.alice_state)
3341
3342 m3_received = Message.unpack(bob,
3343 m3_black_packet,
3344 LongBuffer(self.alice_state),
3345 OrderBuffer(self.alice_state),
3346 {},
3347 self.alice_state)
3348
3349 gd_message = GetData(m3_received, 'self_chain', self.alice_state)
3350 gd_message.send()
3351
3352 # rebuild the black packet so we can compare with what was actually sent
3353 gd_black_packet = Message.pack(bob,
3354 gd_message.command,
3355 gd_message.bounces,
3356 gd_message.get_message_bytes(bob))
3357 self.alice_socket.sendto.called_once_with(gd_black_packet, (bob.address, bob.port))