@@ -481,6 +481,184 @@ def test_generate_snapshot_metadata(self):
481481
482482
483483
484+ def test_generate_snapshot_metadata_without_length (self ):
485+ # Test normal case.
486+ temporary_directory = tempfile .mkdtemp (dir = self .temporary_directory )
487+ original_repository_path = os .path .join ('repository_data' ,
488+ 'repository' )
489+ repository_directory = os .path .join (temporary_directory , 'repository' )
490+ shutil .copytree (original_repository_path , repository_directory )
491+ metadata_directory = os .path .join (repository_directory ,
492+ repo_lib .METADATA_STAGED_DIRECTORY_NAME )
493+ targets_directory = os .path .join (repository_directory , repo_lib .TARGETS_DIRECTORY_NAME )
494+ targets_filename = os .path .join (metadata_directory ,
495+ repo_lib .TARGETS_FILENAME )
496+ version = 1
497+ expiration_date = '1985-10-21T13:20:00Z'
498+
499+ # Load a valid repository so that top-level roles exist in roledb and
500+ # generate_snapshot_metadata() has roles to specify in snapshot metadata.
501+ repository = repo_tool .Repository (repository_directory , metadata_directory ,
502+ targets_directory )
503+
504+ repository_junk = repo_tool .load_repository (repository_directory )
505+
506+ # For testing purposes, store an invalid metadata file in the metadata directory
507+ # to verify that it isn't loaded by generate_snapshot_metadata(). Unknown
508+ # metadata file extensions should be ignored.
509+ invalid_metadata_file = os .path .join (metadata_directory , 'role_file.xml' )
510+ with open (invalid_metadata_file , 'w' ) as file_object :
511+ file_object .write ('bad extension on metadata file' )
512+
513+ targets_filename = 'targets'
514+
515+ snapshot_metadata = \
516+ repo_lib .generate_snapshot_metadata (metadata_directory , version ,
517+ expiration_date ,
518+ targets_filename ,
519+ consistent_snapshot = False ,
520+ use_length = False )
521+ self .assertTrue (tuf .formats .SNAPSHOT_SCHEMA .matches (snapshot_metadata ))
522+
523+ metadata_files_info_dict = snapshot_metadata ['meta' ]
524+ for metadata_filename in sorted (os .listdir (metadata_directory ), reverse = True ):
525+
526+ # In the metadata_directory, there are files with format:
527+ # 1.root.json. The prefix number should be removed.
528+ if metadata_filename [0 ].isdigit ():
529+ version_number , metadata_filename = metadata_filename .split ('.' , 1 )
530+
531+ if metadata_filename .endswith ('.json' ):
532+ if metadata_filename not in \
533+ ['root.json' , 'targets.json' , 'timestamp.json' , 'snapshot.json' ]:
534+
535+ # Check that length is not calculated but hashes is
536+ self .assertTrue (
537+ metadata_files_info_dict [metadata_filename ].get ('length' ) is None )
538+ self .assertTrue (
539+ metadata_files_info_dict [metadata_filename ].get ('hashes' ) is not None )
540+
541+
542+
543+ def test_generate_snapshot_metadata_without_hashes (self ):
544+ # Test normal case.
545+ temporary_directory = tempfile .mkdtemp (dir = self .temporary_directory )
546+ original_repository_path = os .path .join ('repository_data' ,
547+ 'repository' )
548+ repository_directory = os .path .join (temporary_directory , 'repository' )
549+ shutil .copytree (original_repository_path , repository_directory )
550+ metadata_directory = os .path .join (repository_directory ,
551+ repo_lib .METADATA_STAGED_DIRECTORY_NAME )
552+ targets_directory = os .path .join (repository_directory , repo_lib .TARGETS_DIRECTORY_NAME )
553+ targets_filename = os .path .join (metadata_directory ,
554+ repo_lib .TARGETS_FILENAME )
555+ version = 1
556+ expiration_date = '1985-10-21T13:20:00Z'
557+
558+ # Load a valid repository so that top-level roles exist in roledb and
559+ # generate_snapshot_metadata() has roles to specify in snapshot metadata.
560+ repository = repo_tool .Repository (repository_directory , metadata_directory ,
561+ targets_directory )
562+
563+ repository_junk = repo_tool .load_repository (repository_directory )
564+
565+ # For testing purposes, store an invalid metadata file in the metadata directory
566+ # to verify that it isn't loaded by generate_snapshot_metadata(). Unknown
567+ # metadata file extensions should be ignored.
568+ invalid_metadata_file = os .path .join (metadata_directory , 'role_file.xml' )
569+ with open (invalid_metadata_file , 'w' ) as file_object :
570+ file_object .write ('bad extension on metadata file' )
571+
572+ targets_filename = 'targets'
573+
574+ snapshot_metadata = \
575+ repo_lib .generate_snapshot_metadata (metadata_directory , version ,
576+ expiration_date ,
577+ targets_filename ,
578+ consistent_snapshot = False ,
579+ use_hashes = False )
580+ self .assertTrue (tuf .formats .SNAPSHOT_SCHEMA .matches (snapshot_metadata ))
581+
582+ metadata_files_info_dict = snapshot_metadata ['meta' ]
583+ for metadata_filename in sorted (os .listdir (metadata_directory ), reverse = True ):
584+
585+ # In the metadata_directory, there are files with format:
586+ # 1.root.json. The prefix number should be removed.
587+ if metadata_filename [0 ].isdigit ():
588+ version_number , metadata_filename = metadata_filename .split ('.' , 1 )
589+
590+ if metadata_filename .endswith ('.json' ):
591+ if metadata_filename not in \
592+ ['root.json' , 'targets.json' , 'timestamp.json' , 'snapshot.json' ]:
593+
594+ # Check that hashes is not calculated but length is
595+ self .assertTrue (
596+ metadata_files_info_dict [metadata_filename ].get ('length' ) is not None )
597+ self .assertTrue (
598+ metadata_files_info_dict [metadata_filename ].get ('hashes' ) is None )
599+
600+
601+
602+ def test_generate_snapshot_metadata_without_hashes_and_length (self ):
603+ # Test normal case.
604+ temporary_directory = tempfile .mkdtemp (dir = self .temporary_directory )
605+ original_repository_path = os .path .join ('repository_data' ,
606+ 'repository' )
607+ repository_directory = os .path .join (temporary_directory , 'repository' )
608+ shutil .copytree (original_repository_path , repository_directory )
609+ metadata_directory = os .path .join (repository_directory ,
610+ repo_lib .METADATA_STAGED_DIRECTORY_NAME )
611+ targets_directory = os .path .join (repository_directory , repo_lib .TARGETS_DIRECTORY_NAME )
612+ targets_filename = os .path .join (metadata_directory ,
613+ repo_lib .TARGETS_FILENAME )
614+ version = 1
615+ expiration_date = '1985-10-21T13:20:00Z'
616+
617+ # Load a valid repository so that top-level roles exist in roledb and
618+ # generate_snapshot_metadata() has roles to specify in snapshot metadata.
619+ repository = repo_tool .Repository (repository_directory , metadata_directory ,
620+ targets_directory )
621+
622+ repository_junk = repo_tool .load_repository (repository_directory )
623+
624+ # For testing purposes, store an invalid metadata file in the metadata directory
625+ # to verify that it isn't loaded by generate_snapshot_metadata(). Unknown
626+ # metadata file extensions should be ignored.
627+ invalid_metadata_file = os .path .join (metadata_directory , 'role_file.xml' )
628+ with open (invalid_metadata_file , 'w' ) as file_object :
629+ file_object .write ('bad extension on metadata file' )
630+
631+ targets_filename = 'targets'
632+
633+ snapshot_metadata = \
634+ repo_lib .generate_snapshot_metadata (metadata_directory , version ,
635+ expiration_date ,
636+ targets_filename ,
637+ consistent_snapshot = False ,
638+ use_length = False ,
639+ use_hashes = False )
640+ self .assertTrue (tuf .formats .SNAPSHOT_SCHEMA .matches (snapshot_metadata ))
641+
642+ metadata_files_info_dict = snapshot_metadata ['meta' ]
643+ for metadata_filename in sorted (os .listdir (metadata_directory ), reverse = True ):
644+
645+ # In the metadata_directory, there are files with format:
646+ # 1.root.json. The prefix number should be removed.
647+ if metadata_filename [0 ].isdigit ():
648+ version_number , metadata_filename = metadata_filename .split ('.' , 1 )
649+
650+ if metadata_filename .endswith ('.json' ):
651+ if metadata_filename not in \
652+ ['root.json' , 'targets.json' , 'timestamp.json' , 'snapshot.json' ]:
653+
654+ # Check that both length and hashes are not is not calculated
655+ self .assertTrue (
656+ metadata_files_info_dict [metadata_filename ].get ('length' ) is None )
657+ self .assertTrue (
658+ metadata_files_info_dict [metadata_filename ].get ('hashes' ) is None )
659+
660+
661+
484662 def test_generate_timestamp_metadata (self ):
485663 # Test normal case.
486664 repository_name = 'test_repository'
0 commit comments