diff options
Diffstat (limited to 'distbuild/initiator_connection_tests.py')
-rw-r--r-- | distbuild/initiator_connection_tests.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/distbuild/initiator_connection_tests.py b/distbuild/initiator_connection_tests.py new file mode 100644 index 00000000..9d5145cf --- /dev/null +++ b/distbuild/initiator_connection_tests.py @@ -0,0 +1,68 @@ +# coding=utf8 +# mainloop/initiator_connection_tests.py -- tests talking to clients +# +# Copyright (C) 2015 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.. + + +import contextlib +import json +import logging +import socket +import unittest + +import distbuild + + +class InitiatorConnectionTests(unittest.TestCase): + def setUp(self): + self.loop = distbuild.MainLoop() + + @contextlib.contextmanager + def json_initiator_connection(self, port): + '''Return a JsonMachine connected to an InitiatorConnection.''' + listener = distbuild.ListenServer( + 'localhost', port, distbuild.InitiatorConnection, [None, None]) + self.loop.add_state_machine(listener) + + listen_socket = socket.socket() + json_machine = None + try: + listen_socket.connect(('localhost', port)) + self.loop._run_once() + + json_machine = distbuild.JsonMachine(listen_socket) + self.loop.add_state_machine(json_machine) + + yield json_machine + finally: + logging.info('Shutting down test InitiatorConnection') + if json_machine: + json_machine.close() + listen_socket.shutdown(socket.SHUT_RDWR) + listener.close() + self.loop.run() + + def test_invalid_message_from_initiator(self): + '''Malformed messages from clients should not break anything.''' + + invalid_build_request_message = { + 'typeo': 'build-request', + } + + with self.json_initiator_connection(7879) as jm: + jm.send(invalid_build_request_message) + self.loop._run_once() + self.loop._run_once() |