-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_client.py
129 lines (104 loc) · 3.75 KB
/
test_client.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
import unittest
# import messages
import client
class MockSocket(object):
"""
Mocks a socket connection
"""
def __init__(self):
"""
Initializes MockSocket and defines some configuration attributes:
connect_fail: bool, if true mocksocket will raise an error on connect
invalid_response: bool, if true MockSocket will return invalid data on recv
"""
self.connect_fail = False
self.invalid_response = False
def connect(self, *args, **kwargs):
"""
Mocks Socket.connect method
Raises:
socket.error: raised if self.connect_fail is True
"""
if self.connect_fail:
raise client.socket.error
def sendall(self, *args, **kwargs):
"""
Mocks Socket.sendall method
"""
pass
def recv(self, *args, **kwargs):
"""
Mocks Socket.recv method
Returns:
str, if self.invalid_response is True
messages.fib_response, if self.invalid_response if False
"""
if self.invalid_response:
return "shoes"
else:
return client.messages.fib_response.pack(200, 255)
def close(self):
"""
Mocks Socket.close method
"""
pass
class MockClient(client.Client):
"""
Client using a MockSocket interface
"""
def __init__(self, address, port):
"""
Initializes MockClient
Args:
address: str, server's IP address or Hostname
port: int, server's TCP port number
"""
client.Client.__init__(self, address, port)
self.sock = MockSocket()
class TestClient(unittest.TestCase):
"""
Client unittests
"""
def setUp(self):
"""
Set up test environment and initialize client connection through MockSocket
"""
address = "127.0.0.1"
port = 9090
client.logging.basicConfig(level=client.logging.DEBUG)
self.client = MockClient(address, port)
def testSendRequest(self):
"""
Test client is able to process a valid request
This test passes when each sent request returns a status code of 200 and no errors are raised
"""
self.assertEqual(self.client.send_request(9), (200, 255))
self.assertEqual(self.client.send_request("3"), (200, 255))
def testSendRequestInvalidInput(self):
"""
Test client is able to handle a request containing invalid input
This test passes when each sent request returns a value of None
"""
self.assertEqual(self.client.send_request("nine"), None)
self.assertEqual(self.client.send_request(-3), None)
def testSendRequestConnectionError(self):
"""
Test client is able to handle a connection error to server
For this test MockSocket is configured to raise socket.error on the connect method being called.
This will simulate a connection issue with a server
This test passes if send_request returns a status code of 400,1
"""
self.client.sock.connect_fail = True
self.assertEqual(self.client.send_request(9), (400, 2))
def testSendRequestRecieveInvalidResponse(self):
"""
Test client is able to handle recieving an invalid response from the server
For this test MockSocket is configured to return an incorrectly packed response on recv.
This will simulate the server returning unexpected data.
This test passes if send_request returns a status code of 400,2
"""
self.client.sock.invalid_response = True
self.assertEqual(self.client.send_request(9), (400, 1))
if __name__ == "__main__":
unittest.main(verbosity=2)