def test_create_message_list_request(self):
        """Test message list request creation"""

        m1 = b'42'
        m2 = b'\x01\x23\x245'
        m3 = b'\x42\x42\x42'
        m3_str = dandelion.util.encode_bytes(m3).decode()
        m2_str = dandelion.util.encode_bytes(m2).decode()
        m1_str = dandelion.util.encode_bytes(m1).decode()
        
        self.assertTrue(Protocol.is_message_list_request('GETMESSAGES\n'))
        self.assertFalse(Protocol.is_message_list_request('GETMES_XXX_SAGES\n'))
        self.assertEqual(Protocol.create_message_list_request(), 'GETMESSAGES\n')
        self.assertEqual(Protocol.create_message_list_request([]), 'GETMESSAGES\n')
        
        str_ = Protocol.create_message_list_request([m1, m2, m3])
        self.assertTrue(Protocol.is_message_list_request(str_))
        self.assertTrue('GETMESSAGES ' in str_)
        self.assertEquals(str_.count(';'), 2)

        self.assertTrue(m1_str in str_)
        self.assertTrue(m2_str in str_)
        self.assertTrue(m3_str in str_)        
        
        """Testing bad input"""
        self.assertRaises(TypeError, Protocol.create_message_list_request, 0)
        self.assertRaises(TypeError, Protocol.create_message_list_request, -1337)
        self.assertRaises(TypeError, Protocol.create_message_list_request, "1337")
        self.assertRaises(TypeError, Protocol.create_message_list_request, "XXX")
Ejemplo n.º 2
0
    def test_create_message_list_request(self):
        """Test message list request creation"""

        m1 = b'42'
        m2 = b'\x01\x23\x245'
        m3 = b'\x42\x42\x42'
        m3_str = dandelion.util.encode_bytes(m3).decode()
        m2_str = dandelion.util.encode_bytes(m2).decode()
        m1_str = dandelion.util.encode_bytes(m1).decode()

        self.assertTrue(Protocol.is_message_list_request('GETMESSAGES\n'))
        self.assertFalse(
            Protocol.is_message_list_request('GETMES_XXX_SAGES\n'))
        self.assertEqual(Protocol.create_message_list_request(),
                         'GETMESSAGES\n')
        self.assertEqual(Protocol.create_message_list_request([]),
                         'GETMESSAGES\n')

        str_ = Protocol.create_message_list_request([m1, m2, m3])
        self.assertTrue(Protocol.is_message_list_request(str_))
        self.assertTrue('GETMESSAGES ' in str_)
        self.assertEquals(str_.count(';'), 2)

        self.assertTrue(m1_str in str_)
        self.assertTrue(m2_str in str_)
        self.assertTrue(m3_str in str_)
        """Testing bad input"""
        self.assertRaises(TypeError, Protocol.create_message_list_request, 0)
        self.assertRaises(TypeError, Protocol.create_message_list_request,
                          -1337)
        self.assertRaises(TypeError, Protocol.create_message_list_request,
                          "1337")
        self.assertRaises(TypeError, Protocol.create_message_list_request,
                          "XXX")
 def test_roundtrip_message_list_request(self):
     """Test message list request creation / parsing by a round trip"""
     msg = Protocol.create_message_list_request()
     res = Protocol.parse_message_list_request(msg)
     self.assertEqual(res, None)
     
     msg = Protocol.create_message_list_request([b'1', b'2'])
     res = Protocol.parse_message_list_request(msg)
     self.assertEqual(res, [b'1', b'2'])
Ejemplo n.º 4
0
    def test_roundtrip_message_list_request(self):
        """Test message list request creation / parsing by a round trip"""
        msg = Protocol.create_message_list_request()
        res = Protocol.parse_message_list_request(msg)
        self.assertEqual(res, None)

        msg = Protocol.create_message_list_request([b'1', b'2'])
        res = Protocol.parse_message_list_request(msg)
        self.assertEqual(res, [b'1', b'2'])
    def test_basic_server_transaction(self):
        """Tests the server transaction protocol and logic""" 
    
        db = ContentDB()
        tc = db.add_messages([Message('fubar'), Message('foo'), Message('bar')])
    
        with TestServerHelper() as server_helper, TestClientHelper() as client_helper:
            srv_transaction = ServerTransaction(server_helper.sock, db)
            test_client = SocketTransaction(client_helper.sock, b'\n')
            
            """Run the server transaction in a separate thread to allow client access"""
            thread = threading.Thread(target=srv_transaction.process)
            thread.start()

            """Check greeting from server"""
            rcv = test_client._read()
            self.assertEqual(rcv, Protocol.create_greeting_message(db.id).encode())
            
            """Check response to mdgid list req"""
            test_client._write(Protocol.create_message_id_list_request(tc).encode())
            rcv = test_client._read()
            self.assertEqual(rcv, Protocol.create_message_id_list(tc, None).encode())

            """Check response to mdg req"""
            test_client._write(Protocol.create_message_list_request([msg.id for msg in db.get_messages()]).encode())
            rcv = test_client._read()
            self.assertEqual(rcv, Protocol.create_message_list(db.get_messages()).encode())

            """Wait for server (will time out if no requests)"""
            thread.join(2*TIMEOUT)
Ejemplo n.º 6
0
    def process(self):
#        print("CLIENT TRANSACTION: starting")
        
        try:
            """Read greeting from server"""
            dbid = Protocol.parse_greeting_message(self._read().decode())

            """TODO: We should use the remote tc from the last sync..."""
            
            """Request and read message id's"""
            self._write(Protocol.create_message_id_list_request().encode())
            _, msgids = Protocol.parse_message_id_list(self._read().decode())
            
            req_msgids = [mid for mid in msgids if not self._db.contains_message(mid)]

            if len(req_msgids) == 0: # Nothing to fetch
#                print("CLIENT TRANSACTION: hanging up - 0 sync")
                return 
            
            """Request and read messages"""        
            self._write(Protocol.create_message_list_request(req_msgids).encode())
            msgs = Protocol.parse_message_list(self._read().decode())

            """Store the new messages"""
            self._db.add_messages(msgs)
            
        except (socket.timeout, ProtocolParseError, ValueError, TypeError):
            """Do nothing on error, just hang up"""
            #print("CLIENT TRANSACTION: Error processing data from server")

        print("CLIENT TRANSACTION: hanging up")
Ejemplo n.º 7
0
    def test_basic_client_transaction(self):
        """Tests the client transaction protocol and logic"""

        client_db = ContentDB()
        srv_db = ContentDB()
        tc = srv_db.add_messages(
            [Message('fubar'),
             Message('foo'), Message('bar')])

        self.assertEqual(client_db.message_count, 0)
        self.assertEqual(srv_db.message_count, 3)

        with TestServerHelper() as server_helper, TestClientHelper(
        ) as client_helper:

            client_transaction = ClientTransaction(client_helper.sock,
                                                   client_db)
            srv_sock = SocketTransaction(server_helper.sock, b'\n')
            """Run the client transaction in a separate thread"""
            thread = threading.Thread(target=client_transaction.process)
            thread.start()
            """Send a greeting (should be req. by client)"""
            srv_sock._write(
                Protocol.create_greeting_message(srv_db.id).encode())
            """Reading msg id list request"""
            rcv = srv_sock._read()
            self.assertEqual(
                rcv,
                Protocol.create_message_id_list_request().encode())
            """Sending the msg id list"""
            srv_sock._write(
                Protocol.create_message_id_list(
                    tc, srv_db.get_messages()).encode())
            """Reading msg list request"""
            rcv = srv_sock._read()
            self.assertEqual(
                rcv,
                Protocol.create_message_list_request(
                    [msg.id for msg in srv_db.get_messages()]).encode())
            """Sending the msg id list"""
            srv_sock._write(
                Protocol.create_message_list(srv_db.get_messages()).encode())
            """Wait for client to hang up"""
            thread.join(2 * TIMEOUT)
        """Make sure the client has updated the db"""
        self.assertEqual(client_db.message_count, 3)
        self.assertEqual(srv_db.message_count, 3)
        self.assertEqual(
            len([
                srvmsg for srvmsg in srv_db.get_messages()
                if srvmsg not in client_db.get_messages()
            ]), 0)
    def test_basic_client_transaction(self):
        """Tests the client transaction protocol and logic""" 
        
        client_db = ContentDB()
        srv_db = ContentDB()
        tc = srv_db.add_messages([Message('fubar'), Message('foo'), Message('bar')])
    
        self.assertEqual(client_db.message_count, 0)
        self.assertEqual(srv_db.message_count, 3)
    
        with TestServerHelper() as server_helper, TestClientHelper() as client_helper:
            
            client_transaction = ClientTransaction(client_helper.sock, client_db)
            srv_sock = SocketTransaction(server_helper.sock, b'\n')
            
            """Run the client transaction in a separate thread"""
            thread = threading.Thread(target=client_transaction.process)
            thread.start()
            
            """Send a greeting (should be req. by client)"""
            srv_sock._write(Protocol.create_greeting_message(srv_db.id).encode())
            
            """Reading msg id list request"""
            rcv = srv_sock._read()
            self.assertEqual(rcv, Protocol.create_message_id_list_request().encode())

            """Sending the msg id list"""
            srv_sock._write(Protocol.create_message_id_list(tc, srv_db.get_messages()).encode())

            """Reading msg list request"""
            rcv = srv_sock._read()
            self.assertEqual(rcv, Protocol.create_message_list_request([msg.id for msg in srv_db.get_messages()]).encode())

            """Sending the msg id list"""
            srv_sock._write(Protocol.create_message_list(srv_db.get_messages()).encode())

            """Wait for client to hang up"""
            thread.join(2*TIMEOUT)
                
        """Make sure the client has updated the db"""
        self.assertEqual(client_db.message_count, 3)
        self.assertEqual(srv_db.message_count, 3)
        self.assertEqual(len([srvmsg for srvmsg in srv_db.get_messages() if srvmsg not in client_db.get_messages()]), 0) 
Ejemplo n.º 9
0
    def test_basic_server_transaction(self):
        """Tests the server transaction protocol and logic"""

        db = ContentDB()
        tc = db.add_messages(
            [Message('fubar'),
             Message('foo'), Message('bar')])

        with TestServerHelper() as server_helper, TestClientHelper(
        ) as client_helper:
            srv_transaction = ServerTransaction(server_helper.sock, db)
            test_client = SocketTransaction(client_helper.sock, b'\n')
            """Run the server transaction in a separate thread to allow client access"""
            thread = threading.Thread(target=srv_transaction.process)
            thread.start()
            """Check greeting from server"""
            rcv = test_client._read()
            self.assertEqual(rcv,
                             Protocol.create_greeting_message(db.id).encode())
            """Check response to mdgid list req"""
            test_client._write(
                Protocol.create_message_id_list_request(tc).encode())
            rcv = test_client._read()
            self.assertEqual(
                rcv,
                Protocol.create_message_id_list(tc, None).encode())
            """Check response to mdg req"""
            test_client._write(
                Protocol.create_message_list_request(
                    [msg.id for msg in db.get_messages()]).encode())
            rcv = test_client._read()
            self.assertEqual(
                rcv,
                Protocol.create_message_list(db.get_messages()).encode())
            """Wait for server (will time out if no requests)"""
            thread.join(2 * TIMEOUT)