import unittest import logging import time from mock import Mock from lib.message import Message from lib.getdata import GetData from lib.long_buffer import LongBuffer from lib.order_buffer import OrderBuffer from lib.state import State from lib.direct import Direct from lib.broadcast import Broadcast import helper class TestGetData(unittest.TestCase): def setUp(self): helper.setup() self.alice_socket = Mock() self.alice_state = State(self.alice_socket) self.alice_state.set_knob('nick', 'alice') self.setupBob() self.bob_socket = Mock() self.bob_state = State(self.bob_socket) self.setupAlice() def setupBob(self): self.alice_state.add_peer('bob') self.alice_state.add_key( 'bob', '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w==' ) self.alice_state.update_at({ 'handle': 'bob', 'address': '127.0.0.1', 'port': 8889 }) def setupAlice(self): self.bob_state.add_peer('alice') self.bob_state.add_key( 'alice', '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w==' ) self.bob_state.update_at({ 'handle': 'alice', 'address': '127.0.0.1', 'port': 8888 }) def test_send(self): long_buffer = LongBuffer(self.bob_state) m1 = Direct({ 'handle': 'alice', 'speaker': 'bob', 'body': 'm1', 'timestamp': int(time.time()), 'long_buffer': long_buffer }, self.bob_state) m2 = Direct({ 'handle': 'alice', 'speaker': 'bob', 'body': 'm2', 'timestamp': int(time.time()), 'long_buffer': long_buffer }, self.bob_state) m3 = Direct({ 'handle': 'alice', 'speaker': 'bob', 'body': 'm3', 'timestamp': int(time.time()), 'long_buffer': long_buffer }, self.bob_state) # we need to send these messages to get them into the log alice = self.bob_state.get_peer_by_handle('alice') m1.message_bytes = m1.get_message_bytes(alice) m1.send() m2.message_bytes = m2.get_message_bytes(alice) m2.send() m3.message_bytes = m3.get_message_bytes(alice) m3.send() # now let's compile the black packet so alice can # unpack it and get a message we can pass to GetData() m1_message_bytes = m1.get_message_bytes(alice) m1_black_packet = Message.pack(alice, m1.command, m1.bounces, m1_message_bytes) # we use m3 because if we used m2 there would be no break, # and if we used m1 it would be considered the first message m3_message_bytes = m3.get_message_bytes(alice) # TODO: something strange going on here with the message bytes causing the logger to barf m3_black_packet = Message.pack(alice, m3.command, m3.bounces, m3_message_bytes) # we need bob's peer object to know what key to use to decrypt bob = self.alice_state.get_peer_by_handle('bob') m1_received = Message.unpack(bob, m1_black_packet, LongBuffer(self.alice_state), OrderBuffer(self.alice_state), {}, self.alice_state) m3_received = Message.unpack(bob, m3_black_packet, LongBuffer(self.alice_state), OrderBuffer(self.alice_state), {}, self.alice_state) gd_message = GetData(m3_received, 'self_chain', self.alice_state) gd_message.send() # rebuild the black packet so we can compare with what was actually sent gd_black_packet = Message.pack(bob, gd_message.command, gd_message.bounces, gd_message.get_message_bytes(bob)) self.alice_socket.sendto.called_once_with(gd_black_packet, (bob.address, bob.port))