| 1 | import errno |
| 2 | import importlib.util |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import tempfile |
| 6 | import sys |
| 7 | import unittest |
| 8 | from unittest import mock |
| 9 | |
| 10 | sys.dont_write_bytecode = True |
| 11 | |
| 12 | spec = importlib.util.spec_from_file_location('save_download', Path(__file__).with_name('save-download.py')) |
| 13 | helper = importlib.util.module_from_spec(spec) |
| 14 | spec.loader.exec_module(helper) |
| 15 | |
| 16 | |
| 17 | class DownloadSaveTests(unittest.TestCase): |
| 18 | def setUp(self): |
| 19 | self.temp = tempfile.TemporaryDirectory(prefix='cage-download-test-') |
| 20 | self.addCleanup(self.temp.cleanup) |
| 21 | self.root = Path(self.temp.name) |
| 22 | self.source = self.root / 'source' |
| 23 | self.source.write_bytes(b'complete download\x00data') |
| 24 | self.workspace = self.root / 'workspace' |
| 25 | self.workspace.mkdir() |
| 26 | |
| 27 | def copy(self, name='report.txt'): |
| 28 | fd = os.open(self.source, os.O_RDONLY | os.O_NOFOLLOW) |
| 29 | try: |
| 30 | return helper.copy_to_workspace(fd, self.source.stat().st_size, str(self.workspace), name) |
| 31 | finally: |
| 32 | os.close(fd) |
| 33 | |
| 34 | def test_atomic_publish_does_not_overwrite_existing_file_or_symlink(self): |
| 35 | destination = self.workspace / 'Downloads' |
| 36 | destination.mkdir() |
| 37 | (destination / 'report.txt').write_text('original') |
| 38 | (destination / 'report (1).txt').symlink_to(self.source) |
| 39 | result = self.copy() |
| 40 | self.assertEqual(Path(result['path']).name, 'report (2).txt') |
| 41 | self.assertEqual(Path(result['path']).read_bytes(), self.source.read_bytes()) |
| 42 | self.assertEqual((destination / 'report.txt').read_text(), 'original') |
| 43 | self.assertTrue((destination / 'report (1).txt').is_symlink()) |
| 44 | self.assertFalse(list(destination.glob('.cage-download-*'))) |
| 45 | |
| 46 | def test_write_failure_keeps_source_and_publishes_no_partial_file(self): |
| 47 | with mock.patch.object(helper.os, 'fsync', side_effect=OSError(errno.ENOSPC, 'full')): |
| 48 | with self.assertRaises(OSError): |
| 49 | self.copy() |
| 50 | self.assertTrue(self.source.exists()) |
| 51 | self.assertEqual(list((self.workspace / 'Downloads').iterdir()), []) |
| 52 | result = self.copy() |
| 53 | self.assertEqual(Path(result['path']).read_bytes(), self.source.read_bytes()) |
| 54 | |
| 55 | def test_symlink_download_directory_is_refused(self): |
| 56 | outside = self.root / 'outside' |
| 57 | outside.mkdir() |
| 58 | (self.workspace / 'Downloads').symlink_to(outside, target_is_directory=True) |
| 59 | with self.assertRaises(OSError): |
| 60 | self.copy() |
| 61 | self.assertEqual(list(outside.iterdir()), []) |
| 62 | |
| 63 | def test_cleanup_does_not_delete_replaced_source(self): |
| 64 | directory = os.open(self.root, os.O_RDONLY | os.O_DIRECTORY) |
| 65 | self.addCleanup(os.close, directory) |
| 66 | original = self.source.stat() |
| 67 | self.source.rename(self.root / 'original') |
| 68 | self.source.write_text('replacement') |
| 69 | self.assertFalse(helper.remove_source_if_unchanged(directory, 'source', original)) |
| 70 | self.assertEqual(self.source.read_text(), 'replacement') |
| 71 | current = self.source.stat() |
| 72 | self.assertTrue(helper.remove_source_if_unchanged(directory, 'source', current)) |
| 73 | self.assertFalse(self.source.exists()) |
| 74 | |
| 75 | def test_only_fixed_guid_and_leaf_names_are_accepted(self): |
| 76 | valid = '01234567-89ab-cdef-0123-456789abcdef' |
| 77 | helper.checked_download(valid, '中文文件.txt') |
| 78 | for name in ('../outside', '/absolute', '.', '..', 'bad\0name'): |
| 79 | with self.assertRaises(ValueError): |
| 80 | helper.checked_download(valid, name) |
| 81 | with self.assertRaises(ValueError): |
| 82 | helper.checked_download('../outside', 'report.txt') |
| 83 | |
| 84 | def test_cleanup_keeps_source_modified_after_copy(self): |
| 85 | directory = os.open(self.root, os.O_RDONLY | os.O_DIRECTORY) |
| 86 | self.addCleanup(os.close, directory) |
| 87 | original = self.source.stat() |
| 88 | with self.source.open('ab') as changed: |
| 89 | changed.write(b'new bytes') |
| 90 | self.assertFalse(helper.remove_source_if_unchanged(directory, 'source', original)) |
| 91 | self.assertTrue(self.source.read_bytes().endswith(b'new bytes')) |
| 92 | |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | unittest.main() |