Skip to content

Commit 7e86e13

Browse files
committed
Add tests for Updater input validation
This test covers `targetinfo`, `target_path`, `target_base_url`, `metadata_dir` and `filepath` input validation of the `Updater` methods Signed-off-by: Ivana Atanasova <[email protected]>
1 parent cb7bd6a commit 7e86e13

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

tests/test_updater_ng.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,27 @@ def test_persist_metadata_fails(
314314
for filename in os.listdir(self.updater._dir):
315315
self.assertFalse(filename.startswith("tmp"))
316316

317+
def test_invalid_target_base_url(self) -> None:
318+
info = TargetFile(1, {"sha256": ""}, "targetpath")
319+
with self.assertRaises(exceptions.DownloadError):
320+
self.updater.download_target(info, target_base_url="invalid_url")
321+
322+
def test_non_existing_target_file(self) -> None:
323+
info = TargetFile(1, {"sha256": ""}, "/non_existing_file.txt")
324+
# When non-existing target file is given, download fails with
325+
# "404 Client Error: File not found for url"
326+
with self.assertRaises(exceptions.FetcherHTTPError):
327+
self.updater.download_target(info)
328+
329+
def test_empty_target_file_path(self) -> None:
330+
# When target file path is empty, a listing of targets directory is fetched.
331+
# This results in mismatch of expected length or hash. The test is written
332+
# to fail on length mistmatch, as proper length can easily be changed in future
333+
# by adding target files to tests.
334+
info = TargetFile(1, {"sha256": ""}, "")
335+
with self.assertRaises(exceptions.DownloadLengthMismatchError):
336+
self.updater.download_target(info)
337+
317338

318339
if __name__ == "__main__":
319340
utils.configure_test_logging(sys.argv)

tests/test_updater_validation.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022, New York University and the TUF contributors
4+
# SPDX-License-Identifier: MIT OR Apache-2.0
5+
6+
"""Test ngclient Updater validations.
7+
"""
8+
9+
import os
10+
import sys
11+
import tempfile
12+
import unittest
13+
14+
from tests import utils
15+
from tests.repository_simulator import RepositorySimulator
16+
from tuf import unittest_toolbox
17+
from tuf.ngclient import Updater
18+
19+
20+
class TestUpdater(unittest_toolbox.Modified_TestCase):
21+
"""Test ngclient Updater input validation."""
22+
23+
def setUp(self) -> None:
24+
# pylint: disable-next=consider-using-with
25+
self.temp_dir = tempfile.TemporaryDirectory()
26+
self.metadata_dir = os.path.join(self.temp_dir.name, "metadata")
27+
self.targets_dir = os.path.join(self.temp_dir.name, "targets")
28+
os.mkdir(self.metadata_dir)
29+
os.mkdir(self.targets_dir)
30+
31+
# Setup the repository, bootstrap client root.json
32+
self.sim = RepositorySimulator()
33+
with open(os.path.join(self.metadata_dir, "root.json"), "bw") as f:
34+
f.write(self.sim.signed_roots[0])
35+
36+
def tearDown(self) -> None:
37+
self.temp_dir.cleanup()
38+
39+
def _new_updater(self) -> Updater:
40+
return Updater(
41+
self.metadata_dir,
42+
"https://example.com/metadata/",
43+
self.targets_dir,
44+
"https://example.com/targets/",
45+
fetcher=self.sim,
46+
)
47+
48+
def test_empty_target_path_validation(self) -> None:
49+
updater = self._new_updater()
50+
target_file = updater.get_targetinfo("")
51+
self.assertIsNone(target_file)
52+
53+
def test_local_target_storage_fail(self) -> None:
54+
self.sim.add_target("targets", b"content", "targetpath")
55+
self.sim.targets.version += 1
56+
self.sim.update_snapshot()
57+
58+
updater = self._new_updater()
59+
target_info = updater.get_targetinfo("targetpath")
60+
assert target_info is not None
61+
with self.assertRaises(FileNotFoundError):
62+
updater.download_target(target_info, filepath="")
63+
64+
def test_non_existing_metadata_dir(self) -> None:
65+
with self.assertRaises(FileNotFoundError):
66+
# Initialize Updater with non-existing metadata_dir
67+
Updater(
68+
"non_existing_metadata_dir",
69+
"https://example.com/metadata/",
70+
fetcher=self.sim,
71+
)
72+
73+
74+
if __name__ == "__main__":
75+
utils.configure_test_logging(sys.argv)
76+
unittest.main()

0 commit comments

Comments
 (0)