|
| 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.ngclient import Updater |
| 17 | + |
| 18 | + |
| 19 | +class TestUpdater(unittest.TestCase): |
| 20 | + """Test ngclient Updater input validation.""" |
| 21 | + |
| 22 | + def setUp(self) -> None: |
| 23 | + # pylint: disable-next=consider-using-with |
| 24 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 25 | + self.metadata_dir = os.path.join(self.temp_dir.name, "metadata") |
| 26 | + self.targets_dir = os.path.join(self.temp_dir.name, "targets") |
| 27 | + os.mkdir(self.metadata_dir) |
| 28 | + os.mkdir(self.targets_dir) |
| 29 | + |
| 30 | + # Setup the repository, bootstrap client root.json |
| 31 | + self.sim = RepositorySimulator() |
| 32 | + with open(os.path.join(self.metadata_dir, "root.json"), "bw") as f: |
| 33 | + f.write(self.sim.signed_roots[0]) |
| 34 | + |
| 35 | + def tearDown(self) -> None: |
| 36 | + self.temp_dir.cleanup() |
| 37 | + |
| 38 | + def _new_updater(self) -> Updater: |
| 39 | + return Updater( |
| 40 | + self.metadata_dir, |
| 41 | + "https://example.com/metadata/", |
| 42 | + self.targets_dir, |
| 43 | + "https://example.com/targets/", |
| 44 | + fetcher=self.sim, |
| 45 | + ) |
| 46 | + |
| 47 | + def test_local_target_storage_fail(self) -> None: |
| 48 | + self.sim.add_target("targets", b"content", "targetpath") |
| 49 | + self.sim.targets.version += 1 |
| 50 | + self.sim.update_snapshot() |
| 51 | + |
| 52 | + updater = self._new_updater() |
| 53 | + target_info = updater.get_targetinfo("targetpath") |
| 54 | + assert target_info is not None |
| 55 | + with self.assertRaises(FileNotFoundError): |
| 56 | + updater.download_target(target_info, filepath="") |
| 57 | + |
| 58 | + def test_non_existing_metadata_dir(self) -> None: |
| 59 | + with self.assertRaises(FileNotFoundError): |
| 60 | + # Initialize Updater with non-existing metadata_dir |
| 61 | + Updater( |
| 62 | + "non_existing_metadata_dir", |
| 63 | + "https://example.com/metadata/", |
| 64 | + fetcher=self.sim, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + utils.configure_test_logging(sys.argv) |
| 70 | + unittest.main() |
0 commit comments