Skip to content

Commit

Permalink
test_file_download.py: Don't make > 40 client calls in a test
Browse files Browse the repository at this point in the history
test_multiple_changes_to_target starts the client 43 times, this is
excessive and not useful.

* Keep making multiple versions of the repository metadata
* Only use the client every 5 targets versions
* Avoid unneeded refresh() calls in all target download tests

Signed-off-by: Jussi Kukkonen <[email protected]>
  • Loading branch information
jku committed Aug 21, 2024
1 parent a2559be commit a08851f
Showing 1 changed file with 32 additions and 47 deletions.
79 changes: 32 additions & 47 deletions tuf_conformance/test_file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def test_client_downloads_expected_file(
assert client.get_downloaded_target_bytes() == []

assert client.download_target(init_data, target_path) == 0

# assert that the downloaded file contents are correct
assert client.get_downloaded_target_bytes() == [target_content]


Expand All @@ -46,13 +44,7 @@ def test_client_downloads_expected_file_in_sub_dir(
target_content = b"target file contents"
repo.add_artifact(Targets.type, target_content, target_path)

# Client updates, sanity check that nothing was downloaded
assert client.refresh(init_data) == 0
assert client.get_downloaded_target_bytes() == []

assert client.download_target(init_data, target_path) == 0

# assert that the downloaded file contents are correct
assert client.get_downloaded_target_bytes() == [target_content]


Expand All @@ -74,9 +66,6 @@ def test_repository_substitutes_target_file(
repo.add_artifact(Targets.type, target_content_1, target_path_1)
repo.add_artifact(Targets.type, target_content_2, target_path_2)

# Client updates
assert client.refresh(init_data) == 0

# Download one of the artifacts
assert client.download_target(init_data, target_path_1) == 0
assert client.get_downloaded_target_bytes() == [target_content_1]
Expand Down Expand Up @@ -125,9 +114,6 @@ def test_multiple_changes_to_target(
target_content = b"target file contents"
repo.add_artifact(Targets.type, target_content, target_path)

# Client updates
assert client.refresh(init_data) == 0

# Client downloads the file
assert client.download_target(init_data, target_path) == 0
# check file contents
Expand All @@ -139,7 +125,7 @@ def test_multiple_changes_to_target(
# It modifies the targets file in the targets metadata including
# the hashes and length, and it also makes the corresponding
# content changes in the file itself.
for i in range(10):
for i in range(11):
# Modify the existing artifact legitimately:
modified_contents = f"modified file contents {i}".encode()
repo.add_artifact(Targets.type, modified_contents, target_path)
Expand All @@ -152,35 +138,34 @@ def test_multiple_changes_to_target(
# Bump repo snapshot
repo.update_snapshot()

# Client updates
assert client.refresh(init_data) == 0

# Client downloads the modified artifact
assert client.download_target(init_data, target_path) == 0
# the previous content is no longer there, modified content is there
expected_downloads.remove(previous_content)
expected_downloads.append(modified_contents)
previous_content = modified_contents
assert client.get_downloaded_target_bytes() == expected_downloads

# Client downloads the new artifact
assert client.download_target(init_data, new_target_path) == 0
expected_downloads.append(new_file_contents)

# check downloaded contents
assert client.get_downloaded_target_bytes() == expected_downloads

# Modify artifact content without updating the hashes/length in metadata.
malicious_file_contents = f"malicious contents {i}".encode()
repo.artifacts[target_path].data = malicious_file_contents
repo.targets.version += 1

# Bump repo snapshot
repo.update_snapshot()

# ask client to download (this may fail or succeed, see
# test_repository_substitutes_target_file)
client.download_target(init_data, target_path)

# Check that the client did not download the malicious file
assert client.get_downloaded_target_bytes() == expected_downloads
# Client only sees every fifth targets version
if i % 5 == 0:
# Client downloads the modified artifact
assert client.download_target(init_data, target_path) == 0
# the previous content is no longer there, modified content is there
expected_downloads.remove(previous_content)
expected_downloads.append(modified_contents)
previous_content = modified_contents
assert client.get_downloaded_target_bytes() == expected_downloads

# Client downloads the new artifact
assert client.download_target(init_data, new_target_path) == 0
expected_downloads.append(new_file_contents)

# check downloaded contents
assert client.get_downloaded_target_bytes() == expected_downloads

# Modify artifact content without updating the hashes/length in metadata.
malicious_file_contents = f"malicious contents {i}".encode()
repo.artifacts[target_path].data = malicious_file_contents
repo.targets.version += 1

# Bump repo snapshot
repo.update_snapshot()

# ask client to download (this call may fail or succeed, see
# test_repository_substitutes_target_file)
client.download_target(init_data, target_path)

# Check that the client did not download the malicious file
assert client.get_downloaded_target_bytes() == expected_downloads

0 comments on commit a08851f

Please sign in to comment.