Skip to content

Commit 22facb7

Browse files
committed
Update test_api
Add tests for hash and length verification. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 8710f8d commit 22facb7

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

tests/test_api.py

+64-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from tests import utils
2222

23-
import tuf.exceptions
23+
from tuf import exceptions
2424
from tuf.api.metadata import (
2525
Metadata,
2626
Root,
@@ -178,7 +178,7 @@ def test_sign_verify(self):
178178
self.assertTrue(len(metadata_obj.signatures) == 1)
179179
# ... which is valid for the correct key.
180180
targets_key.verify_signature(metadata_obj)
181-
with self.assertRaises(tuf.exceptions.UnsignedMetadataError):
181+
with self.assertRaises(exceptions.UnsignedMetadataError):
182182
snapshot_key.verify_signature(metadata_obj)
183183

184184
sslib_signer = SSlibSigner(self.keystore['snapshot'])
@@ -197,7 +197,7 @@ def test_sign_verify(self):
197197
self.assertTrue(len(metadata_obj.signatures) == 1)
198198
# ... valid for that key.
199199
timestamp_key.verify_signature(metadata_obj)
200-
with self.assertRaises(tuf.exceptions.UnsignedMetadataError):
200+
with self.assertRaises(exceptions.UnsignedMetadataError):
201201
targets_key.verify_signature(metadata_obj)
202202

203203

@@ -280,7 +280,6 @@ def test_targetfile_class(self):
280280
targetfile_obj = TargetFile.from_dict(copy.copy(data))
281281
self.assertEqual(targetfile_obj.to_dict(), data)
282282

283-
284283
def test_metadata_snapshot(self):
285284
snapshot_path = os.path.join(
286285
self.repo_dir, 'metadata', 'snapshot.json')
@@ -352,6 +351,7 @@ def test_metadata_timestamp(self):
352351
timestamp_test = Timestamp.from_dict(test_dict)
353352
self.assertEqual(timestamp_dict['signed'], timestamp_test.to_dict())
354353

354+
355355
def test_key_class(self):
356356
keys = {
357357
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{
@@ -638,6 +638,66 @@ def test_support_for_unrecognized_fields(self):
638638
metadata_obj.signed.to_dict(), metadata_obj2.signed.to_dict()
639639
)
640640

641+
def test_length_and_hash_validation(self):
642+
643+
# Test metadata files' hash and length verification.
644+
# Use timestamp to get a MetaFile object and snapshot
645+
# for untrusted metadata file to verify.
646+
timestamp_path = os.path.join(
647+
self.repo_dir, 'metadata', 'timestamp.json')
648+
timestamp = Metadata.from_file(timestamp_path)
649+
snapshot_metafile = timestamp.signed.meta["snapshot.json"]
650+
651+
snapshot_path = os.path.join(
652+
self.repo_dir, 'metadata', 'snapshot.json')
653+
654+
with open(snapshot_path, "rb") as file:
655+
# test with data as a file object
656+
snapshot_metafile.verify_length_and_hashes(file)
657+
file.seek(0)
658+
data = file.read()
659+
# test with data as bytes
660+
snapshot_metafile.verify_length_and_hashes(data)
661+
662+
# test exceptions
663+
expected_length = snapshot_metafile.length
664+
snapshot_metafile.length = 2345
665+
self.assertRaises(exceptions.LengthOrHashMismatchError,
666+
snapshot_metafile.verify_length_and_hashes, data)
667+
668+
snapshot_metafile.length = expected_length
669+
snapshot_metafile.hashes = {'sha256': 'incorrecthash'}
670+
self.assertRaises(exceptions.LengthOrHashMismatchError,
671+
snapshot_metafile.verify_length_and_hashes, data)
672+
673+
# test optional length and hashes
674+
snapshot_metafile.length = None
675+
snapshot_metafile.hashes = None
676+
snapshot_metafile.verify_length_and_hashes(data)
677+
678+
679+
# Test target files' hash and length verification
680+
targets_path = os.path.join(
681+
self.repo_dir, 'metadata', 'targets.json')
682+
targets = Metadata.from_file(targets_path)
683+
file1_targetfile = targets.signed.targets['file1.txt']
684+
filepath = os.path.join(
685+
self.repo_dir, 'targets', 'file1.txt')
686+
687+
with open(filepath, "rb") as file1:
688+
file1_targetfile.verify_length_and_hashes(file1)
689+
690+
# test exceptions
691+
expected_length = file1_targetfile.length
692+
file1_targetfile.length = 2345
693+
self.assertRaises(exceptions.LengthOrHashMismatchError,
694+
file1_targetfile.verify_length_and_hashes, file1)
695+
696+
file1_targetfile.length = expected_length
697+
file1_targetfile.hashes = {'sha256': 'incorrecthash'}
698+
self.assertRaises(exceptions.LengthOrHashMismatchError,
699+
file1_targetfile.verify_length_and_hashes, file1)
700+
641701

642702
# Run unit test.
643703
if __name__ == '__main__':

0 commit comments

Comments
 (0)