|
10 | 10 | import os
|
11 | 11 | import sys
|
12 | 12 | import tempfile
|
13 |
| -from typing import Optional |
| 13 | +from typing import Dict, NamedTuple, Optional, Tuple |
14 | 14 | from tuf.exceptions import UnsignedMetadataError
|
15 | 15 | import unittest
|
16 | 16 |
|
|
19 | 19 | from tests import utils
|
20 | 20 | from tests.repository_simulator import RepositorySimulator
|
21 | 21 |
|
| 22 | +class TargetData(NamedTuple): |
| 23 | + targetpath: str |
| 24 | + content: str |
| 25 | + encoded_path: str |
| 26 | + |
| 27 | +DataSet = Dict[str, TargetData] |
| 28 | + |
22 | 29 | class TestUpdater(unittest.TestCase):
|
23 | 30 | # set dump_dir to trigger repository state dumps
|
24 | 31 | dump_dir:Optional[str] = None
|
@@ -79,43 +86,45 @@ def test_refresh(self):
|
79 | 86 |
|
80 | 87 | self._run_refresh()
|
81 | 88 |
|
82 |
| - def test_targets(self): |
83 |
| - targets = { |
84 |
| - "targetpath": b"content", |
85 |
| - "åäö": b"more content", |
86 |
| - "dir/targetpath": b"dir target content" |
87 |
| - } |
| 89 | + targets: DataSet = { |
| 90 | + "standard case": ("targetpath", b"content", "targetpath"), |
| 91 | + "non-asci case": ("åäö", b"more content", "%C3%A5%C3%A4%C3%B6"), |
| 92 | + "subdirectory case": ("a/b/c/targetpath", b"dir target content", "a%2Fb%2Fc%2Ftargetpath"), |
| 93 | + } |
| 94 | + |
| 95 | + @utils.run_sub_tests_with_dataset(targets) |
| 96 | + def test_targets(self, test_case_data: TargetData): |
| 97 | + targetpath, content, encoded_path = test_case_data |
| 98 | + # target does not exist yet |
| 99 | + updater = self._run_refresh() |
| 100 | + self.assertIsNone(updater.get_one_valid_targetinfo(targetpath)) |
88 | 101 |
|
89 | 102 | # Add targets to repository
|
90 | 103 | self.sim.targets.version += 1
|
91 |
| - for targetpath, content in targets.items(): |
92 |
| - self.sim.add_target("targets", content, targetpath) |
| 104 | + self.sim.add_target("targets", content, targetpath) |
93 | 105 | self.sim.update_snapshot()
|
94 | 106 |
|
95 | 107 | updater = self._run_refresh()
|
96 |
| - for targetpath, content in targets.items(): |
97 |
| - # target now exists, is not in cache yet |
98 |
| - file_info = updater.get_one_valid_targetinfo(targetpath) |
99 |
| - self.assertIsNotNone(file_info) |
100 |
| - self.assertEqual( |
101 |
| - updater.updated_targets([file_info], self.targets_dir), |
102 |
| - [file_info] |
103 |
| - ) |
104 |
| - |
105 |
| - # download target, assert it is in cache and content is correct |
106 |
| - local_path = updater.download_target(file_info, self.targets_dir) |
107 |
| - self.assertEqual( |
108 |
| - updater.updated_targets([file_info], self.targets_dir), [] |
109 |
| - ) |
110 |
| - self.assertTrue(local_path.startswith(self.targets_dir)) |
111 |
| - with open(local_path, "rb") as f: |
112 |
| - self.assertEqual(f.read(), content) |
113 |
| - |
114 |
| - if "/" in targetpath: |
115 |
| - # assert local_path != targetpath because of the URL encoding |
116 |
| - # make target_path absolute as local_path |
117 |
| - target_path = os.path.join(self.targets_dir, targetpath) |
118 |
| - self.assertNotEqual(target_path, local_path) |
| 108 | + # target now exists, is not in cache yet |
| 109 | + file_info = updater.get_one_valid_targetinfo(targetpath) |
| 110 | + self.assertIsNotNone(file_info) |
| 111 | + self.assertEqual( |
| 112 | + updater.updated_targets([file_info], self.targets_dir), |
| 113 | + [file_info] |
| 114 | + ) |
| 115 | + |
| 116 | + # download target, assert it is in cache and content is correct |
| 117 | + local_path = updater.download_target(file_info, self.targets_dir) |
| 118 | + self.assertEqual( |
| 119 | + updater.updated_targets([file_info], self.targets_dir), [] |
| 120 | + ) |
| 121 | + self.assertTrue(local_path.startswith(self.targets_dir)) |
| 122 | + with open(local_path, "rb") as f: |
| 123 | + self.assertEqual(f.read(), content) |
| 124 | + |
| 125 | + # Assert that the targetpath was URL encoded as expected. |
| 126 | + encoded_absolute_path = os.path.join(self.targets_dir, encoded_path) |
| 127 | + self.assertEqual(local_path, encoded_absolute_path) |
119 | 128 |
|
120 | 129 |
|
121 | 130 |
|
|
0 commit comments