Skip to content

Add tests for Updater input validation #1758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/test_updater_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ def test_persist_metadata_fails(
for filename in os.listdir(self.updater._dir):
self.assertFalse(filename.startswith("tmp"))

def test_invalid_target_base_url(self) -> None:
info = TargetFile(1, {"sha256": ""}, "targetpath")
with self.assertRaises(exceptions.DownloadError):
self.updater.download_target(info, target_base_url="invalid_url")

def test_non_existing_target_file(self) -> None:
info = TargetFile(1, {"sha256": ""}, "/non_existing_file.txt")
# When non-existing target file is given, download fails with
# "404 Client Error: File not found for url"
with self.assertRaises(exceptions.DownloadHTTPError):
self.updater.download_target(info)


if __name__ == "__main__":
utils.configure_test_logging(sys.argv)
Expand Down
70 changes: 70 additions & 0 deletions tests/test_updater_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python

# 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:
# pylint: disable-next=consider-using-with
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()