def test_client_server_transaction(self): """Tests the whole, client driven transaction protocol and logic""" client_db = ContentDB() server_db = ContentDB() server_db.add_messages( [Message('fubar'), Message('foo'), Message('bar')]) self.assertEqual(client_db.message_count, 0) self.assertEqual(server_db.message_count, 3) with TestServerHelper() as server_helper, TestClientHelper( ) as client_helper: client_transaction = ClientTransaction(client_helper.sock, client_db) server_transaction = ServerTransaction(server_helper.sock, server_db) """Run the client transactions asynchronously""" server_thread = threading.Thread(target=server_transaction.process) client_thread = threading.Thread(target=client_transaction.process) server_thread.start() client_thread.start() """Wait for client to hang up""" client_thread.join(1) # One sec should be plenty server_thread.join(2 * TIMEOUT) """Make sure the client has updated the db""" self.assertEqual(client_db.message_count, 3) self.assertEqual(server_db.message_count, 3) self.assertEqual( len([ srvmsg for srvmsg in server_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)
def __init__(self, config_file=None): if config_file is None: self._cfg_file_name = 'dandelion.conf' else: self._cfg_file_name = config_file self._server_config = ServerConfig() self._synchronizer_config = SynchronizerConfig() self._id_manager_config = IdentityConfig() self._ui_config = UiConfig() self.read_file() self._content_db = ContentDB()
def test_client_transaction_protocol_violation(self): """Tests the client transaction protocol and logic""" client_db = ContentDB() 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(b'NON PROTOCOL MESSAGE\n') self.assertRaises(socket.timeout, srv_sock._read) """Wait for client to hang up""" thread.join(2 * TIMEOUT)
def test_server_transaction_protocol_violation(self): """Tests the servers response to an invalid request""" db = ContentDB() 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(b'NON PROTOCOL MESSAGE\n') self.assertRaises(socket.timeout, test_client._read) """Wait for server (will time out if no requests)""" thread.join(2 * TIMEOUT) server_helper, client_helper = None, None
def __init__(self, config_file=None): self._cfg = self.CONFIG_DEFAULTS.copy() if not config_file: self._config_file = self.DEFAULT_CONFIG_FILE else: self._config_file = config_file if not self._config_file_exists(): self.write_defaults_to_config_file() self.load_config() self._content_db = ContentDB() # store all dict values as attributes so we can access the like this # self.server_time (the same as self_cfg['synchronization']['server_time'] # one downfall is that we cant have settings with the same name even though they are # in different sections. for key in self._cfg: setattr(self, key, self._cfg[key]) for k, v in self._cfg[key].items(): setattr(self.__class__, k, v)
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)