| 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 | import ftplib |
|---|
| 35 | import operator |
|---|
| 36 | import os |
|---|
| 37 | import random |
|---|
| 38 | import stat |
|---|
| 39 | import time |
|---|
| 40 | import unittest |
|---|
| 41 | |
|---|
| 42 | import _mock_ftplib |
|---|
| 43 | import _test_base |
|---|
| 44 | import ftp_error |
|---|
| 45 | import ftp_file |
|---|
| 46 | import ftputil |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | def random_data(pool, size=10000): |
|---|
| 52 | """ |
|---|
| 53 | Return a sequence of characters consisting of those from |
|---|
| 54 | the pool of integer numbers. |
|---|
| 55 | """ |
|---|
| 56 | character_list = [] |
|---|
| 57 | for i in range(size): |
|---|
| 58 | ordinal = random.choice(pool) |
|---|
| 59 | character_list.append(chr(ordinal)) |
|---|
| 60 | result = ''.join(character_list) |
|---|
| 61 | return result |
|---|
| 62 | |
|---|
| 63 | def ascii_data(): |
|---|
| 64 | """Return an ASCII character string.""" |
|---|
| 65 | pool = range(32, 128) |
|---|
| 66 | pool.append(ord('\n')) |
|---|
| 67 | return random_data(pool) |
|---|
| 68 | |
|---|
| 69 | def binary_data(): |
|---|
| 70 | """Return a binary character string.""" |
|---|
| 71 | pool = range(0, 256) |
|---|
| 72 | return random_data(pool) |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | class FailOnLoginSession(_mock_ftplib.MockSession): |
|---|
| 79 | def __init__(self, host='', user='', password=''): |
|---|
| 80 | raise ftplib.error_perm |
|---|
| 81 | |
|---|
| 82 | class ReadMockSession(_mock_ftplib.MockSession): |
|---|
| 83 | mock_file_content = 'line 1\r\nanother line\r\nyet another line' |
|---|
| 84 | |
|---|
| 85 | class AsciiReadMockSession(_mock_ftplib.MockSession): |
|---|
| 86 | mock_file_content = '\r\n'.join(map(str, range(20))) |
|---|
| 87 | |
|---|
| 88 | class BinaryDownloadMockSession(_mock_ftplib.MockSession): |
|---|
| 89 | mock_file_content = binary_data() |
|---|
| 90 | |
|---|
| 91 | class TimeShiftMockSession(_mock_ftplib.MockSession): |
|---|
| 92 | def delete(self, file_name): |
|---|
| 93 | pass |
|---|
| 94 | |
|---|
| 95 | class InaccessibleDirSession(_mock_ftplib.MockSession): |
|---|
| 96 | _login_dir = '/inaccessible' |
|---|
| 97 | |
|---|
| 98 | def pwd(self): |
|---|
| 99 | return self._login_dir |
|---|
| 100 | |
|---|
| 101 | def cwd(self, dir): |
|---|
| 102 | if dir in (self._login_dir, self._login_dir + '/'): |
|---|
| 103 | raise ftplib.error_perm |
|---|
| 104 | else: |
|---|
| 105 | _mock_ftplib.MockSession.cwd(self, dir) |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | class FailingUploadAndDownloadFTPHost(ftputil.FTPHost): |
|---|
| 112 | def upload(self, source, target, mode=''): |
|---|
| 113 | assert False, "`FTPHost.upload` should not have been called" |
|---|
| 114 | |
|---|
| 115 | def download(self, source, target, mode=''): |
|---|
| 116 | assert False, "`FTPHost.download` should not have been called" |
|---|
| 117 | |
|---|
| 118 | class TimeShiftFTPHost(ftputil.FTPHost): |
|---|
| 119 | class _Path: |
|---|
| 120 | def set_mtime(self, mtime): |
|---|
| 121 | self._mtime = mtime |
|---|
| 122 | def getmtime(self, file_name): |
|---|
| 123 | return self._mtime |
|---|
| 124 | def abspath(self, path): |
|---|
| 125 | return "/home/sschwarzer/_ftputil_sync_" |
|---|
| 126 | |
|---|
| 127 | def isfile(self, path): |
|---|
| 128 | return True |
|---|
| 129 | |
|---|
| 130 | def __init__(self, *args, **kwargs): |
|---|
| 131 | ftputil.FTPHost.__init__(self, *args, **kwargs) |
|---|
| 132 | self.path = self._Path() |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | class TestOpenAndClose(unittest.TestCase): |
|---|
| 138 | """Test opening and closing of `FTPHost` objects.""" |
|---|
| 139 | def test_open_and_close(self): |
|---|
| 140 | """Test closing of `FTPHost`.""" |
|---|
| 141 | host = _test_base.ftp_host_factory() |
|---|
| 142 | host.close() |
|---|
| 143 | self.assertEqual(host.closed, 1) |
|---|
| 144 | self.assertEqual(host._children, []) |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | class TestLogin(unittest.TestCase): |
|---|
| 148 | def test_invalid_login(self): |
|---|
| 149 | """Login to invalid host must fail.""" |
|---|
| 150 | self.assertRaises(ftp_error.FTPOSError, _test_base.ftp_host_factory, |
|---|
| 151 | FailOnLoginSession) |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | class TestFileOperations(unittest.TestCase): |
|---|
| 155 | """Test operations with file-like objects.""" |
|---|
| 156 | def test_inaccessible_dir(self): |
|---|
| 157 | """Test whether opening a file at an invalid location fails.""" |
|---|
| 158 | host = _test_base.ftp_host_factory( |
|---|
| 159 | session_factory=InaccessibleDirSession) |
|---|
| 160 | self.assertRaises(ftp_error.FTPIOError, host.file, |
|---|
| 161 | '/inaccessible/new_file', 'w') |
|---|
| 162 | |
|---|
| 163 | def test_caching(self): |
|---|
| 164 | """Test whether `_FTPFile` cache of `FTPHost` object works.""" |
|---|
| 165 | host = _test_base.ftp_host_factory() |
|---|
| 166 | self.assertEqual(len(host._children), 0) |
|---|
| 167 | path1 = 'path1' |
|---|
| 168 | path2 = 'path2' |
|---|
| 169 | |
|---|
| 170 | file1 = host.file(path1, 'w') |
|---|
| 171 | child1 = host._children[0] |
|---|
| 172 | self.assertEqual(len(host._children), 1) |
|---|
| 173 | self.failIf(child1._file.closed) |
|---|
| 174 | |
|---|
| 175 | file2 = host.file(path2, 'w') |
|---|
| 176 | child2 = host._children[1] |
|---|
| 177 | self.assertEqual(len(host._children), 2) |
|---|
| 178 | self.failIf(child2._file.closed) |
|---|
| 179 | |
|---|
| 180 | file1.close() |
|---|
| 181 | self.assertEqual(len(host._children), 2) |
|---|
| 182 | self.failUnless(child1._file.closed) |
|---|
| 183 | self.failIf(child2._file.closed) |
|---|
| 184 | |
|---|
| 185 | file1 = host.file(path1, 'w') |
|---|
| 186 | child1_1 = file1._host |
|---|
| 187 | |
|---|
| 188 | self.failUnless(child1 is child1_1) |
|---|
| 189 | self.failIf(child1._file.closed) |
|---|
| 190 | self.failIf(child2._file.closed) |
|---|
| 191 | |
|---|
| 192 | file2.close() |
|---|
| 193 | self.failUnless(child2._file.closed) |
|---|
| 194 | |
|---|
| 195 | def test_write_to_directory(self): |
|---|
| 196 | """Test whether attempting to write to a directory fails.""" |
|---|
| 197 | host = _test_base.ftp_host_factory() |
|---|
| 198 | self.assertRaises(ftp_error.FTPIOError, host.file, |
|---|
| 199 | '/home/sschwarzer', 'w') |
|---|
| 200 | |
|---|
| 201 | def test_binary_write(self): |
|---|
| 202 | """Write binary data with `write`.""" |
|---|
| 203 | host = _test_base.ftp_host_factory() |
|---|
| 204 | data = '\000a\001b\r\n\002c\003\n\004\r\005' |
|---|
| 205 | output = host.file('dummy', 'wb') |
|---|
| 206 | output.write(data) |
|---|
| 207 | output.close() |
|---|
| 208 | child_data = _mock_ftplib.content_of('dummy') |
|---|
| 209 | expected_data = data |
|---|
| 210 | self.assertEqual(child_data, expected_data) |
|---|
| 211 | |
|---|
| 212 | def test_ascii_write(self): |
|---|
| 213 | """Write ASCII text with `write`.""" |
|---|
| 214 | host = _test_base.ftp_host_factory() |
|---|
| 215 | data = ' \nline 2\nline 3' |
|---|
| 216 | output = host.file('dummy', 'w') |
|---|
| 217 | output.write(data) |
|---|
| 218 | output.close() |
|---|
| 219 | child_data = _mock_ftplib.content_of('dummy') |
|---|
| 220 | expected_data = ' \r\nline 2\r\nline 3' |
|---|
| 221 | self.assertEqual(child_data, expected_data) |
|---|
| 222 | |
|---|
| 223 | def test_ascii_writelines(self): |
|---|
| 224 | """Write ASCII text with `writelines`.""" |
|---|
| 225 | host = _test_base.ftp_host_factory() |
|---|
| 226 | data = [' \n', 'line 2\n', 'line 3'] |
|---|
| 227 | backup_data = data[:] |
|---|
| 228 | output = host.file('dummy', 'w') |
|---|
| 229 | output.writelines(data) |
|---|
| 230 | output.close() |
|---|
| 231 | child_data = _mock_ftplib.content_of('dummy') |
|---|
| 232 | expected_data = ' \r\nline 2\r\nline 3' |
|---|
| 233 | self.assertEqual(child_data, expected_data) |
|---|
| 234 | |
|---|
| 235 | self.assertEqual(data, backup_data) |
|---|
| 236 | |
|---|
| 237 | def test_ascii_read(self): |
|---|
| 238 | """Read ASCII text with plain `read`.""" |
|---|
| 239 | host = _test_base.ftp_host_factory(session_factory=ReadMockSession) |
|---|
| 240 | input_ = host.file('dummy', 'r') |
|---|
| 241 | data = input_.read(0) |
|---|
| 242 | self.assertEqual(data, '') |
|---|
| 243 | data = input_.read(3) |
|---|
| 244 | self.assertEqual(data, 'lin') |
|---|
| 245 | data = input_.read(7) |
|---|
| 246 | self.assertEqual(data, 'e 1\nano') |
|---|
| 247 | data = input_.read() |
|---|
| 248 | self.assertEqual(data, 'ther line\nyet another line') |
|---|
| 249 | data = input_.read() |
|---|
| 250 | self.assertEqual(data, '') |
|---|
| 251 | input_.close() |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | host = _test_base.ftp_host_factory(session_factory=AsciiReadMockSession) |
|---|
| 255 | expected_data = AsciiReadMockSession.mock_file_content.\ |
|---|
| 256 | replace('\r\n', '\n') |
|---|
| 257 | input_ = host.file('dummy', 'r') |
|---|
| 258 | data = input_.read(len(expected_data)) |
|---|
| 259 | self.assertEqual(data, expected_data) |
|---|
| 260 | |
|---|
| 261 | def test_binary_readline(self): |
|---|
| 262 | """Read binary data with `readline`.""" |
|---|
| 263 | host = _test_base.ftp_host_factory(session_factory=ReadMockSession) |
|---|
| 264 | input_ = host.file('dummy', 'rb') |
|---|
| 265 | data = input_.readline(3) |
|---|
| 266 | self.assertEqual(data, 'lin') |
|---|
| 267 | data = input_.readline(10) |
|---|
| 268 | self.assertEqual(data, 'e 1\r\n') |
|---|
| 269 | data = input_.readline(13) |
|---|
| 270 | self.assertEqual(data, 'another line\r') |
|---|
| 271 | data = input_.readline() |
|---|
| 272 | self.assertEqual(data, '\n') |
|---|
| 273 | data = input_.readline() |
|---|
| 274 | self.assertEqual(data, 'yet another line') |
|---|
| 275 | data = input_.readline() |
|---|
| 276 | self.assertEqual(data, '') |
|---|
| 277 | input_.close() |
|---|
| 278 | |
|---|
| 279 | def test_ascii_readline(self): |
|---|
| 280 | """Read ASCII text with `readline`.""" |
|---|
| 281 | host = _test_base.ftp_host_factory(session_factory=ReadMockSession) |
|---|
| 282 | input_ = host.file('dummy', 'r') |
|---|
| 283 | data = input_.readline(3) |
|---|
| 284 | self.assertEqual(data, 'lin') |
|---|
| 285 | data = input_.readline(10) |
|---|
| 286 | self.assertEqual(data, 'e 1\n') |
|---|
| 287 | data = input_.readline(13) |
|---|
| 288 | self.assertEqual(data, 'another line\n') |
|---|
| 289 | data = input_.readline() |
|---|
| 290 | self.assertEqual(data, 'yet another line') |
|---|
| 291 | data = input_.readline() |
|---|
| 292 | self.assertEqual(data, '') |
|---|
| 293 | input_.close() |
|---|
| 294 | |
|---|
| 295 | def test_ascii_readlines(self): |
|---|
| 296 | """Read ASCII text with `readlines`.""" |
|---|
| 297 | host = _test_base.ftp_host_factory(session_factory=ReadMockSession) |
|---|
| 298 | input_ = host.file('dummy', 'r') |
|---|
| 299 | data = input_.read(3) |
|---|
| 300 | self.assertEqual(data, 'lin') |
|---|
| 301 | data = input_.readlines() |
|---|
| 302 | self.assertEqual(data, ['e 1\n', 'another line\n', |
|---|
| 303 | 'yet another line']) |
|---|
| 304 | input_.close() |
|---|
| 305 | |
|---|
| 306 | def test_ascii_xreadlines(self): |
|---|
| 307 | """Read ASCII text with `xreadlines`.""" |
|---|
| 308 | host = _test_base.ftp_host_factory(session_factory=ReadMockSession) |
|---|
| 309 | |
|---|
| 310 | input_ = host.file('dummy', 'r') |
|---|
| 311 | data = input_.read(3) |
|---|
| 312 | xrl_obj = input_.xreadlines() |
|---|
| 313 | self.failUnless(xrl_obj.__class__ is ftp_file._XReadlines) |
|---|
| 314 | self.failUnless(xrl_obj._ftp_file.__class__ is ftp_file._FTPFile) |
|---|
| 315 | data = xrl_obj[0] |
|---|
| 316 | self.assertEqual(data, 'e 1\n') |
|---|
| 317 | |
|---|
| 318 | self.assertRaises(RuntimeError, operator.__getitem__, xrl_obj, 2) |
|---|
| 319 | |
|---|
| 320 | data = xrl_obj[1] |
|---|
| 321 | self.assertEqual(data, 'another line\n') |
|---|
| 322 | data = xrl_obj[2] |
|---|
| 323 | self.assertEqual(data, 'yet another line') |
|---|
| 324 | |
|---|
| 325 | self.assertRaises(IndexError, operator.__getitem__, xrl_obj, 3) |
|---|
| 326 | |
|---|
| 327 | def test_binary_iterator(self): |
|---|
| 328 | """Test the iterator interface of `FTPFile` objects.""" |
|---|
| 329 | host = _test_base.ftp_host_factory(session_factory=ReadMockSession) |
|---|
| 330 | input_ = host.file('dummy') |
|---|
| 331 | input_iterator = iter(input_) |
|---|
| 332 | self.assertEqual(input_iterator.next(), "line 1\n") |
|---|
| 333 | self.assertEqual(input_iterator.next(), "another line\n") |
|---|
| 334 | self.assertEqual(input_iterator.next(), "yet another line") |
|---|
| 335 | self.assertRaises(StopIteration, input_iterator.next) |
|---|
| 336 | input_.close() |
|---|
| 337 | |
|---|
| 338 | def test_ascii_iterator(self): |
|---|
| 339 | """Test the iterator interface of `FTPFile` objects.""" |
|---|
| 340 | host = _test_base.ftp_host_factory(session_factory=ReadMockSession) |
|---|
| 341 | input_ = host.file('dummy', 'rb') |
|---|
| 342 | input_iterator = iter(input_) |
|---|
| 343 | self.assertEqual(input_iterator.next(), "line 1\r\n") |
|---|
| 344 | self.assertEqual(input_iterator.next(), "another line\r\n") |
|---|
| 345 | self.assertEqual(input_iterator.next(), "yet another line") |
|---|
| 346 | self.assertRaises(StopIteration, input_iterator.next) |
|---|
| 347 | input_.close() |
|---|
| 348 | |
|---|
| 349 | def test_read_unknown_file(self): |
|---|
| 350 | """Test whether reading a file which isn't there fails.""" |
|---|
| 351 | host = _test_base.ftp_host_factory() |
|---|
| 352 | self.assertRaises(ftp_error.FTPIOError, host.file, 'notthere', 'r') |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | class TestUploadAndDownload(unittest.TestCase): |
|---|
| 356 | """Test ASCII upload and binary download as examples.""" |
|---|
| 357 | def generate_ascii_file(self, data, filename): |
|---|
| 358 | """Generate an ASCII data file.""" |
|---|
| 359 | source_file = open(filename, 'w') |
|---|
| 360 | source_file.write(data) |
|---|
| 361 | source_file.close() |
|---|
| 362 | |
|---|
| 363 | def test_ascii_upload(self): |
|---|
| 364 | """Test ASCII mode upload.""" |
|---|
| 365 | local_source = '__test_source' |
|---|
| 366 | data = ascii_data() |
|---|
| 367 | self.generate_ascii_file(data, local_source) |
|---|
| 368 | |
|---|
| 369 | host = _test_base.ftp_host_factory() |
|---|
| 370 | host.upload(local_source, 'dummy') |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | data = data.replace('\n', '\r\n') |
|---|
| 375 | remote_file_content = _mock_ftplib.content_of('dummy') |
|---|
| 376 | self.assertEqual(data, remote_file_content) |
|---|
| 377 | |
|---|
| 378 | os.unlink(local_source) |
|---|
| 379 | |
|---|
| 380 | def test_binary_download(self): |
|---|
| 381 | """Test binary mode download.""" |
|---|
| 382 | local_target = '__test_target' |
|---|
| 383 | host = _test_base.ftp_host_factory( |
|---|
| 384 | session_factory=BinaryDownloadMockSession) |
|---|
| 385 | |
|---|
| 386 | host.download('dummy', local_target, 'b') |
|---|
| 387 | |
|---|
| 388 | data = open(local_target, 'rb').read() |
|---|
| 389 | remote_file_content = _mock_ftplib.content_of('dummy') |
|---|
| 390 | self.assertEqual(data, remote_file_content) |
|---|
| 391 | |
|---|
| 392 | os.unlink(local_target) |
|---|
| 393 | |
|---|
| 394 | def test_conditional_upload(self): |
|---|
| 395 | """Test conditional ASCII mode upload.""" |
|---|
| 396 | local_source = '__test_source' |
|---|
| 397 | data = ascii_data() |
|---|
| 398 | self.generate_ascii_file(data, local_source) |
|---|
| 399 | |
|---|
| 400 | host = _test_base.ftp_host_factory( |
|---|
| 401 | ftp_host_class=FailingUploadAndDownloadFTPHost) |
|---|
| 402 | flag = host.upload_if_newer(local_source, '/home/newer') |
|---|
| 403 | self.assertEqual(flag, False) |
|---|
| 404 | |
|---|
| 405 | host = _test_base.ftp_host_factory() |
|---|
| 406 | flag = host.upload_if_newer(local_source, '/home/older') |
|---|
| 407 | self.assertEqual(flag, True) |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | data = data.replace('\n', '\r\n') |
|---|
| 412 | remote_file_content = _mock_ftplib.content_of('older') |
|---|
| 413 | self.assertEqual(data, remote_file_content) |
|---|
| 414 | |
|---|
| 415 | host = _test_base.ftp_host_factory() |
|---|
| 416 | flag = host.upload_if_newer(local_source, '/home/notthere') |
|---|
| 417 | self.assertEqual(flag, True) |
|---|
| 418 | remote_file_content = _mock_ftplib.content_of('notthere') |
|---|
| 419 | self.assertEqual(data, remote_file_content) |
|---|
| 420 | |
|---|
| 421 | os.unlink(local_source) |
|---|
| 422 | |
|---|
| 423 | def compare_and_delete_downloaded_data(self, filename): |
|---|
| 424 | """Compare content of downloaded file with its source, then |
|---|
| 425 | delete the local target file.""" |
|---|
| 426 | data = open(filename, 'rb').read() |
|---|
| 427 | remote_file_content = _mock_ftplib.content_of('newer') |
|---|
| 428 | self.assertEqual(data, remote_file_content) |
|---|
| 429 | |
|---|
| 430 | os.unlink(filename) |
|---|
| 431 | |
|---|
| 432 | def test_conditional_download_without_target(self): |
|---|
| 433 | "Test conditional binary mode download when no target file exists." |
|---|
| 434 | local_target = '__test_target' |
|---|
| 435 | |
|---|
| 436 | host = _test_base.ftp_host_factory( |
|---|
| 437 | session_factory=BinaryDownloadMockSession) |
|---|
| 438 | flag = host.download_if_newer('/home/newer', local_target, 'b') |
|---|
| 439 | self.assertEqual(flag, True) |
|---|
| 440 | self.compare_and_delete_downloaded_data(local_target) |
|---|
| 441 | |
|---|
| 442 | def test_conditional_download_with_older_target(self): |
|---|
| 443 | """Test conditional binary mode download with newer source file.""" |
|---|
| 444 | local_target = '__test_target' |
|---|
| 445 | |
|---|
| 446 | open(local_target, 'w').close() |
|---|
| 447 | |
|---|
| 448 | host = _test_base.ftp_host_factory( |
|---|
| 449 | session_factory=BinaryDownloadMockSession) |
|---|
| 450 | flag = host.download_if_newer('/home/newer', local_target, 'b') |
|---|
| 451 | self.assertEqual(flag, True) |
|---|
| 452 | self.compare_and_delete_downloaded_data(local_target) |
|---|
| 453 | |
|---|
| 454 | def test_conditional_download_with_newer_target(self): |
|---|
| 455 | """Test conditional binary mode download with older source file.""" |
|---|
| 456 | local_target = '__test_target' |
|---|
| 457 | |
|---|
| 458 | open(local_target, 'w').close() |
|---|
| 459 | |
|---|
| 460 | host = _test_base.ftp_host_factory( |
|---|
| 461 | session_factory=BinaryDownloadMockSession) |
|---|
| 462 | host = _test_base.ftp_host_factory( |
|---|
| 463 | ftp_host_class=FailingUploadAndDownloadFTPHost, |
|---|
| 464 | session_factory=BinaryDownloadMockSession) |
|---|
| 465 | flag = host.download_if_newer('/home/older', local_target, 'b') |
|---|
| 466 | self.assertEqual(flag, False) |
|---|
| 467 | |
|---|
| 468 | os.unlink(local_target) |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | class TestTimeShift(unittest.TestCase): |
|---|
| 472 | def test_rounded_time_shift(self): |
|---|
| 473 | """Test if time shift is rounded correctly.""" |
|---|
| 474 | host = _test_base.ftp_host_factory(session_factory=TimeShiftMockSession) |
|---|
| 475 | |
|---|
| 476 | rounded_time_shift = host._FTPHost__rounded_time_shift |
|---|
| 477 | |
|---|
| 478 | test_data = [ |
|---|
| 479 | (0, 0), (0.1, 0), (-0.1, 0), (1500, 0), (-1500, 0), |
|---|
| 480 | (1800, 3600), (-1800, -3600), (2000, 3600), (-2000, -3600), |
|---|
| 481 | (5*3600-100, 5*3600), (-5*3600+100, -5*3600)] |
|---|
| 482 | for time_shift, expected_time_shift in test_data: |
|---|
| 483 | calculated_time_shift = rounded_time_shift(time_shift) |
|---|
| 484 | self.assertEqual(calculated_time_shift, expected_time_shift) |
|---|
| 485 | |
|---|
| 486 | def test_assert_valid_time_shift(self): |
|---|
| 487 | """Test time shift sanity checks.""" |
|---|
| 488 | host = _test_base.ftp_host_factory(session_factory=TimeShiftMockSession) |
|---|
| 489 | |
|---|
| 490 | assert_time_shift = host._FTPHost__assert_valid_time_shift |
|---|
| 491 | |
|---|
| 492 | test_data = [23*3600, -23*3600, 3600+30, -3600+30] |
|---|
| 493 | for time_shift in test_data: |
|---|
| 494 | self.failUnless(assert_time_shift(time_shift) is None) |
|---|
| 495 | |
|---|
| 496 | self.assertRaises(ftp_error.TimeShiftError, assert_time_shift, 25*3600) |
|---|
| 497 | self.assertRaises(ftp_error.TimeShiftError, assert_time_shift, -25*3600) |
|---|
| 498 | |
|---|
| 499 | self.assertRaises(ftp_error.TimeShiftError, assert_time_shift, 10*60) |
|---|
| 500 | self.assertRaises(ftp_error.TimeShiftError, assert_time_shift, |
|---|
| 501 | -3600-10*60) |
|---|
| 502 | |
|---|
| 503 | def test_synchronize_times(self): |
|---|
| 504 | """Test time synchronization with server.""" |
|---|
| 505 | host = _test_base.ftp_host_factory(ftp_host_class=TimeShiftFTPHost, |
|---|
| 506 | session_factory=TimeShiftMockSession) |
|---|
| 507 | |
|---|
| 508 | host.path.set_mtime(time.time() + 3630) |
|---|
| 509 | host.synchronize_times() |
|---|
| 510 | self.assertEqual(host.time_shift(), 3600) |
|---|
| 511 | |
|---|
| 512 | host.path.set_mtime(time.time() + 3600+10*60) |
|---|
| 513 | self.assertRaises(ftp_error.TimeShiftError, host.synchronize_times) |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | if __name__ == '__main__': |
|---|
| 517 | unittest.main() |
|---|