| 56 | | |
|---|
| | 56 | def random_data(pool, size=10000): |
|---|
| | 57 | """ |
|---|
| | 58 | Return a sequence of characters consisting of those from |
|---|
| | 59 | the pool of integer numbers. |
|---|
| | 60 | """ |
|---|
| | 61 | character_list = [] |
|---|
| | 62 | for i in range(size): |
|---|
| | 63 | ordinal = random.choice(pool) |
|---|
| | 64 | character_list.append( chr(ordinal) ) |
|---|
| | 65 | result = ''.join(character_list) |
|---|
| | 66 | return result |
|---|
| | 67 | |
|---|
| | 68 | def ascii_data(): |
|---|
| | 69 | """Return an ASCII character string.""" |
|---|
| | 70 | pool = range(32, 128) |
|---|
| | 71 | pool.append( ord('\n') ) |
|---|
| | 72 | return random_data(pool) |
|---|
| | 73 | |
|---|
| | 74 | def binary_data(): |
|---|
| | 75 | """Return an binary character string.""" |
|---|
| | 76 | pool = range(0, 256) |
|---|
| | 77 | return random_data(pool) |
|---|
| | 78 | |
|---|
| | 79 | class BinaryDownloadMockSession(_mock_ftplib.MockSession): |
|---|
| | 80 | mock_file_content = binary_data() |
|---|
| | 81 | |
|---|
| | 82 | |
|---|
| 352 | | def random_data(self, pool, size=10000): |
|---|
| 353 | | """ |
|---|
| 354 | | Return a sequence of characters consisting of those from |
|---|
| 355 | | the pool of integer numbers. |
|---|
| 356 | | """ |
|---|
| 357 | | character_list = [] |
|---|
| 358 | | for i in range(size): |
|---|
| 359 | | ordinal = random.choice(pool) |
|---|
| 360 | | character_list.append( chr(ordinal) ) |
|---|
| 361 | | result = ''.join(character_list) |
|---|
| 362 | | return result |
|---|
| 363 | | |
|---|
| 364 | | def ascii_data(self): |
|---|
| 365 | | """Return an ASCII character string.""" |
|---|
| 366 | | pool = range(32, 128) |
|---|
| 367 | | pool.append( ord('\n') ) |
|---|
| 368 | | return self.random_data(pool) |
|---|
| 369 | | |
|---|
| 370 | | def binary_data(self): |
|---|
| 371 | | """Return an binary character string.""" |
|---|
| 372 | | pool = range(0, 256) |
|---|
| 373 | | return self.random_data(pool) |
|---|
| 374 | | |
|---|
| 388 | | file_content = _mock_ftplib.content_of('dummy') |
|---|
| 389 | | self.assertEqual(data, file_content) |
|---|
| 390 | | |
|---|
| 391 | | # host.download(remote_path, local_test_path) |
|---|
| 392 | | # # compare local data |
|---|
| 393 | | # input_ = file(local_source) |
|---|
| 394 | | # original = input_.read() |
|---|
| 395 | | # input_.close() |
|---|
| 396 | | # input_ = file(local_test_path) |
|---|
| 397 | | # copy = input_.read() |
|---|
| 398 | | # input_.close() |
|---|
| 399 | | # self.assertEqual(original, copy) |
|---|
| 400 | | # # test binary up/download |
|---|
| 401 | | # host.upload(local_source, remote_path, 'b') |
|---|
| 402 | | # host.download(remote_path, local_test_path, 'b') |
|---|
| 403 | | # # compare local data |
|---|
| 404 | | # input_ = file(local_source, 'rb') |
|---|
| 405 | | # original = input_.read() |
|---|
| 406 | | # input_.close() |
|---|
| 407 | | # input_ = file(local_test_path, 'rb') |
|---|
| 408 | | # copy = input_.read() |
|---|
| 409 | | # input_.close() |
|---|
| 410 | | # self.assertEqual(original, copy) |
|---|
| 411 | | # # clean up |
|---|
| 412 | | # host.remove(remote_path) |
|---|
| 413 | | # os.remove(local_test_path) |
|---|
| | 393 | remote_file_content = _mock_ftplib.content_of('dummy') |
|---|
| | 394 | self.assertEqual(data, remote_file_content) |
|---|
| | 395 | # clean up |
|---|
| | 396 | os.unlink(local_source) |
|---|
| | 397 | |
|---|
| | 398 | def test_binary_download(self): |
|---|
| | 399 | """Test binary mode download.""" |
|---|
| | 400 | local_target = '__test_target' |
|---|
| | 401 | host = ftp_host_factory(session_factory=BinaryDownloadMockSession) |
|---|
| | 402 | # download |
|---|
| | 403 | host.download('dummy', local_target, 'b') |
|---|
| | 404 | # read file and compare |
|---|
| | 405 | data = open(local_target, 'rb').read() |
|---|
| | 406 | remote_file_content = _mock_ftplib.content_of('dummy') |
|---|
| | 407 | self.assertEqual(data, remote_file_content) |
|---|
| | 408 | # clean up |
|---|
| | 409 | os.unlink(local_target) |
|---|