1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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()
|