File: test_updater_validation.py

package info (click to toggle)
python-tuf 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,300 kB
  • sloc: python: 7,738; makefile: 8
file content (66 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright 2022, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0

"""Test ngclient Updater validations."""

import os
import sys
import tempfile
import unittest

from tests import utils
from tests.repository_simulator import RepositorySimulator
from tuf.ngclient import Updater


class TestUpdater(unittest.TestCase):
    """Test ngclient Updater input validation."""

    def setUp(self) -> None:
        self.temp_dir = tempfile.TemporaryDirectory()
        self.metadata_dir = os.path.join(self.temp_dir.name, "metadata")
        self.targets_dir = os.path.join(self.temp_dir.name, "targets")
        os.mkdir(self.metadata_dir)
        os.mkdir(self.targets_dir)

        # Setup the repository, bootstrap client root.json
        self.sim = RepositorySimulator()
        with open(os.path.join(self.metadata_dir, "root.json"), "bw") as f:
            f.write(self.sim.signed_roots[0])

    def tearDown(self) -> None:
        self.temp_dir.cleanup()

    def _new_updater(self) -> Updater:
        return Updater(
            self.metadata_dir,
            "https://example.com/metadata/",
            self.targets_dir,
            "https://example.com/targets/",
            fetcher=self.sim,
        )

    def test_local_target_storage_fail(self) -> None:
        self.sim.add_target("targets", b"content", "targetpath")
        self.sim.targets.version += 1
        self.sim.update_snapshot()

        updater = self._new_updater()
        target_info = updater.get_targetinfo("targetpath")
        assert target_info is not None
        with self.assertRaises(FileNotFoundError):
            updater.download_target(target_info, filepath="")

    def test_non_existing_metadata_dir(self) -> None:
        with self.assertRaises(FileNotFoundError):
            # Initialize Updater with non-existing metadata_dir
            Updater(
                "non_existing_metadata_dir",
                "https://example.com/metadata/",
                fetcher=self.sim,
            )


if __name__ == "__main__":
    utils.configure_test_logging(sys.argv)
    unittest.main()