raw
9982-getdata            1 import unittest
9982-getdata 2 import logging
9982-getdata 3
9982-getdata 4 import time
9982-getdata 5 from mock import Mock
9982-getdata 6 from lib.message import Message
9982-getdata 7 from lib.getdata import GetData
9982-getdata 8 from lib.long_buffer import LongBuffer
9982-getdata 9 from lib.order_buffer import OrderBuffer
9982-getdata 10 from lib.state import State
9982-getdata 11 from lib.direct import Direct
9982-getdata 12 from lib.broadcast import Broadcast
9982-getdata 13 import helper
9982-getdata 14
9982-getdata 15 class TestGetData(unittest.TestCase):
9982-getdata 16 def setUp(self):
9982-getdata 17 helper.setup()
9982-getdata 18 self.alice_socket = Mock()
9982-getdata 19 self.alice_state = State(self.alice_socket)
9982-getdata 20 self.alice_state.set_knob('nick', 'alice')
9982-getdata 21 self.setupBob()
9982-getdata 22 self.bob_socket = Mock()
9982-getdata 23 self.bob_state = State(self.bob_socket)
9982-getdata 24 self.setupAlice()
9982-getdata 25
9982-getdata 26 def setupBob(self):
9982-getdata 27 self.alice_state.add_peer('bob')
9982-getdata 28 self.alice_state.add_key(
9982-getdata 29 'bob',
9982-getdata 30 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 31 )
9982-getdata 32 self.alice_state.update_at({
9982-getdata 33 'handle': 'bob',
9982-getdata 34 'address': '127.0.0.1',
9982-getdata 35 'port': 8889
9982-getdata 36 })
9982-getdata 37
9982-getdata 38 def setupAlice(self):
9982-getdata 39 self.bob_state.add_peer('alice')
9982-getdata 40 self.bob_state.add_key(
9982-getdata 41 'alice',
9982-getdata 42 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 43 )
9982-getdata 44 self.bob_state.update_at({
9982-getdata 45 'handle': 'alice',
9982-getdata 46 'address': '127.0.0.1',
9982-getdata 47 'port': 8888
9982-getdata 48 })
9982-getdata 49
9982-getdata 50 def test_send(self):
9982-getdata 51 long_buffer = LongBuffer(self.bob_state)
9982-getdata 52 m1 = Direct({
9982-getdata 53 'handle': 'alice',
9982-getdata 54 'speaker': 'bob',
9982-getdata 55 'body': 'm1',
9982-getdata 56 'timestamp': int(time.time()),
9982-getdata 57 'long_buffer': long_buffer
9982-getdata 58 }, self.bob_state)
9982-getdata 59 m2 = Direct({
9982-getdata 60 'handle': 'alice',
9982-getdata 61 'speaker': 'bob',
9982-getdata 62 'body': 'm2',
9982-getdata 63 'timestamp': int(time.time()),
9982-getdata 64 'long_buffer': long_buffer
9982-getdata 65 }, self.bob_state)
9982-getdata 66 m3 = Direct({
9982-getdata 67 'handle': 'alice',
9982-getdata 68 'speaker': 'bob',
9982-getdata 69 'body': 'm3',
9982-getdata 70 'timestamp': int(time.time()),
9982-getdata 71 'long_buffer': long_buffer
9982-getdata 72 }, self.bob_state)
9982-getdata 73 # we need to send these messages to get them into the log
9982-getdata 74 alice = self.bob_state.get_peer_by_handle('alice')
9982-getdata 75 m1.message_bytes = m1.get_message_bytes(alice)
9982-getdata 76 m1.send()
9982-getdata 77 m2.message_bytes = m2.get_message_bytes(alice)
9982-getdata 78 m2.send()
9982-getdata 79 m3.message_bytes = m3.get_message_bytes(alice)
9982-getdata 80 m3.send()
9982-getdata 81
9982-getdata 82 # now let's compile the black packet so alice can
9982-getdata 83 # unpack it and get a message we can pass to GetData()
9982-getdata 84 m1_message_bytes = m1.get_message_bytes(alice)
9982-getdata 85 m1_black_packet = Message.pack(alice, m1.command, m1.bounces, m1_message_bytes)
9982-getdata 86
9982-getdata 87 # we use m3 because if we used m2 there would be no break,
9982-getdata 88 # and if we used m1 it would be considered the first message
9982-getdata 89 m3_message_bytes = m3.get_message_bytes(alice)
9982-getdata 90 # TODO: something strange going on here with the message bytes causing the logger to barf
9982-getdata 91 m3_black_packet = Message.pack(alice, m3.command, m3.bounces, m3_message_bytes)
9982-getdata 92
9982-getdata 93
9982-getdata 94 # we need bob's peer object to know what key to use to decrypt
9982-getdata 95 bob = self.alice_state.get_peer_by_handle('bob')
9982-getdata 96 m1_received = Message.unpack(bob,
9982-getdata 97 m1_black_packet,
9982-getdata 98 LongBuffer(self.alice_state),
9982-getdata 99 OrderBuffer(self.alice_state),
9982-getdata 100 {},
9982-getdata 101 self.alice_state)
9982-getdata 102
9982-getdata 103 m3_received = Message.unpack(bob,
9982-getdata 104 m3_black_packet,
9982-getdata 105 LongBuffer(self.alice_state),
9982-getdata 106 OrderBuffer(self.alice_state),
9982-getdata 107 {},
9982-getdata 108 self.alice_state)
9982-getdata 109
9982-getdata 110 gd_message = GetData(m3_received, 'self_chain', self.alice_state)
9982-getdata 111 gd_message.send()
9982-getdata 112
9982-getdata 113 # rebuild the black packet so we can compare with what was actually sent
9982-getdata 114 gd_black_packet = Message.pack(bob,
9982-getdata 115 gd_message.command,
9982-getdata 116 gd_message.bounces,
9982-getdata 117 gd_message.get_message_bytes(bob))
9982-getdata 118 self.alice_socket.sendto.called_once_with(gd_black_packet, (bob.address, bob.port))