-
Notifications
You must be signed in to change notification settings - Fork 41
/
hikvision_tftpd_test.py
executable file
·228 lines (188 loc) · 8.01 KB
/
hikvision_tftpd_test.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python2
__author__ = 'Scott Lamb'
__license__ = 'MIT'
__email__ = 'slamb@slamb.org'
import errno
import hikvision_tftpd
import socket
import string
import unittest
import platform
import sys
class TftpdTest(unittest.TestCase):
_BLOCK_SIZE = 1468
# From a packet capture.
_TEST_RRQ = ('\x00\x01digicap.dav\x00' # request file digicap.dav
'octet\x00' # mode octet
'timeout\x005\x00' # RFC 2349 timeout = 5 seconds
'blksize\x00' + str(_BLOCK_SIZE) + '\x00') # RFC 2348 block size = 1458
_TEST_RRQ_DEFAULT_BLKSIZE = ('\x00\x01digicap.dav\x00' # request file digicap.dav
'octet\x00') # mode octet
_LARGE_BUFFER_SIZE = 65536
_BLKSIZE_OPTION = 'blksize\x00' + str(_BLOCK_SIZE) + '\x00'
def setUp(self):
self._server = None
def tearDown(self):
if self._server is not None:
self._server.close()
self._handshake_client.close()
self._tftp_client.close()
def _setup(self, data):
self._server = hikvision_tftpd.Server(
('127.0.0.1', 0), ('127.0.0.1', 0), 'digicap.dav', data)
self._handshake_client = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM)
self._handshake_client.connect(
self._server._handshake_sock.getsockname())
self._tftp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._tftp_client.connect(self._server._tftp_sock.getsockname())
# These tests run self._server._iterate() and then expect data to
# be available immediately or not at all. But at least on OS X,
# non-blocking operations don't already receive data that's already
# been sent to this socket, so give some fudge time.
self._tftp_client.settimeout(0.1)
def _assert_no_data(self):
try:
d = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
except socket.timeout:
pass
else:
self.fail('expected nothing, got: %r' % d)
def test_eaddrinuse(self):
self._setup('')
try:
hikvision_tftpd.Server(self._server._handshake_sock.getsockname(),
self._server._tftp_sock.getsockname(),
'digicap.dav', '')
except hikvision_tftpd.Error, e:
self.assertTrue('in use' in e.message, 'Unexpected: %r' % e)
else:
self.fail('expected an error')
def test_eaddrnotavail(self):
try:
# 192.0.2.0/24 is "TEST-NET", according to RFC 5737.
# The local machine shouldn't have such an IP address.
# (Okay, according to the RFCs, it shouldn't be using 192.0.0.128
# either, but we do what we must.)
hikvision_tftpd.Server(('192.0.2.1', 0), ('192.0.2.1', 0),
'digicap.dav', '')
except hikvision_tftpd.Error, e:
self.assertTrue('not available' in e.message, 'Unexpected: %r' % e)
else:
self.fail('expected an error')
# Skip check for user root not relevant on Windows Platform
@unittest.skipIf(sys.platform.startswith('win'),
'Skip check for root permissions on Windows')
def test_eaccess(self):
try:
hikvision_tftpd.Server(('127.0.0.1', 1), ('127.0.0.1', 3),
'digicap.dav', '')
except hikvision_tftpd.Error, e:
self.assertTrue('permission' in e.message, 'Unexpected: %r' % e)
else:
self.fail('expected an error. '
'(did you run the tests as root? don\'t.)')
def test_proper_handshake(self):
self._setup('')
self._handshake_client.send(hikvision_tftpd.HANDSHAKE_BYTES)
self._server._iterate()
pkt = self._handshake_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual(hikvision_tftpd.HANDSHAKE_BYTES, pkt)
def test_bogus_handshake(self):
self._setup('')
self._handshake_client.send('asdf')
self._server._iterate()
self._assert_no_data()
def test_one_block(self):
data = string.letters
self._setup(data)
self._tftp_client.send(self._TEST_RRQ)
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x06' + self._BLKSIZE_OPTION, pkt)
# OACK ACK
self._tftp_client.send('\x00\x04\x00\x00')
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x03\x00\x01' + data, pkt)
self._tftp_client.send('\x00\x03\x00\x01')
self._server._iterate()
self._assert_no_data()
def test_two_block(self):
blocksize = self._BLOCK_SIZE
repetitions = 1 + blocksize // len(string.letters)
data = string.letters * repetitions
self._setup(data)
# First packet.
self._tftp_client.send(self._TEST_RRQ)
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x06' + self._BLKSIZE_OPTION, pkt)
# OACK ACK
self._tftp_client.send('\x00\x04\x00\x00')
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x03\x00\x01' + data[:blocksize], pkt)
# Second packet.
self._tftp_client.send('\x00\x04\x00\x01')
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x03\x00\x02' + data[blocksize:], pkt)
# No more packets.
self._tftp_client.send('\x00\x04\x00\x02')
self._server._iterate()
self._assert_no_data()
def test_full_block(self):
blocksize = self._BLOCK_SIZE
data = 'x' * blocksize
self._setup(data)
self._tftp_client.send(self._TEST_RRQ)
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x06' + self._BLKSIZE_OPTION, pkt)
# OACK ACK
self._tftp_client.send('\x00\x04\x00\x00')
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x03\x00\x01' + data, pkt)
# Second packet (empty).
self._tftp_client.send('\x00\x04\x00\x01')
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x03\x00\x02', pkt)
# No more packets.
self._tftp_client.send('\x00\x04\x00\x02')
self._server._iterate()
self._assert_no_data()
def test_full_block_default_blksize(self):
blocksize = 512
data = 'x' * blocksize
self._setup(data)
self._tftp_client.send(self._TEST_RRQ_DEFAULT_BLKSIZE)
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x03\x00\x01' + data, pkt)
# Second packet (empty).
self._tftp_client.send('\x00\x04\x00\x01')
self._server._iterate()
pkt = self._tftp_client.recv(self._LARGE_BUFFER_SIZE)
self.assertEqual('\x00\x03\x00\x02', pkt)
# No more packets.
self._tftp_client.send('\x00\x04\x00\x02')
self._server._iterate()
self._assert_no_data()
def test_max_file_size(self):
# The number of blocks in the file must fit within 16 bits.
# The final block can't be full.
max_blocks = 2**16 - 1
max_size = self._BLOCK_SIZE * max_blocks - 1
self._setup('x' * max_size)
self._tftp_client.send(self._TEST_RRQ)
self._server._iterate()
self._server.close()
self._setup('x' * (max_size + 1))
self._tftp_client.send(self._TEST_RRQ)
self.assertRaises(hikvision_tftpd.Error,
self._server._iterate)
if __name__ == '__main__':
unittest.main()