20
20
21
21
from tests import utils
22
22
23
- import tuf . exceptions
23
+ from tuf import exceptions
24
24
from tuf .api .metadata import (
25
25
Metadata ,
26
26
Root ,
@@ -178,7 +178,7 @@ def test_sign_verify(self):
178
178
self .assertTrue (len (metadata_obj .signatures ) == 1 )
179
179
# ... which is valid for the correct key.
180
180
targets_key .verify_signature (metadata_obj )
181
- with self .assertRaises (tuf . exceptions .UnsignedMetadataError ):
181
+ with self .assertRaises (exceptions .UnsignedMetadataError ):
182
182
snapshot_key .verify_signature (metadata_obj )
183
183
184
184
sslib_signer = SSlibSigner (self .keystore ['snapshot' ])
@@ -197,7 +197,7 @@ def test_sign_verify(self):
197
197
self .assertTrue (len (metadata_obj .signatures ) == 1 )
198
198
# ... valid for that key.
199
199
timestamp_key .verify_signature (metadata_obj )
200
- with self .assertRaises (tuf . exceptions .UnsignedMetadataError ):
200
+ with self .assertRaises (exceptions .UnsignedMetadataError ):
201
201
targets_key .verify_signature (metadata_obj )
202
202
203
203
@@ -280,7 +280,6 @@ def test_targetfile_class(self):
280
280
targetfile_obj = TargetFile .from_dict (copy .copy (data ))
281
281
self .assertEqual (targetfile_obj .to_dict (), data )
282
282
283
-
284
283
def test_metadata_snapshot (self ):
285
284
snapshot_path = os .path .join (
286
285
self .repo_dir , 'metadata' , 'snapshot.json' )
@@ -352,6 +351,7 @@ def test_metadata_timestamp(self):
352
351
timestamp_test = Timestamp .from_dict (test_dict )
353
352
self .assertEqual (timestamp_dict ['signed' ], timestamp_test .to_dict ())
354
353
354
+
355
355
def test_key_class (self ):
356
356
keys = {
357
357
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d" :{
@@ -638,6 +638,66 @@ def test_support_for_unrecognized_fields(self):
638
638
metadata_obj .signed .to_dict (), metadata_obj2 .signed .to_dict ()
639
639
)
640
640
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
+
641
701
642
702
# Run unit test.
643
703
if __name__ == '__main__' :
0 commit comments