<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import unicode_literals <EOL> import os <EOL> import time <EOL> import datetime <EOL> import logging <EOL> import tempfile <EOL> import json <EOL> import shutil <EOL> import stat <EOL> import sys <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:7> ) : <EOL> import unittest <EOL> else : <EOL> import unittest2 as unittest <EOL> import tuf <EOL> import tuf . log <EOL> import tuf . formats <EOL> import tuf . roledb <EOL> import tuf . keydb <EOL> import tuf . hash <EOL> import tuf . repository_lib as repo_lib <EOL> import tuf . repository_tool as repo_tool <EOL> import six <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> repo_lib . disable_console_log_messages ( ) <EOL> class TestRepositoryToolFunctions ( unittest . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . temporary_directory = tempfile . mkdtemp ( dir = os . getcwd ( ) ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> shutil . rmtree ( cls . temporary_directory ) <EOL> def setUp ( self ) : <EOL> pass <EOL> def tearDown ( self ) : <EOL> pass <EOL> def test_generate_and_write_rsa_keypair ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> test_keypath = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> repo_lib . generate_and_write_rsa_keypair ( test_keypath , password = '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( test_keypath ) ) <EOL> self . assertTrue ( os . path . exists ( test_keypath + '<STR_LIT>' ) ) <EOL> imported_pubkey = repo_lib . import_rsa_publickey_from_file ( test_keypath + '<STR_LIT>' ) <EOL> self . assertTrue ( tuf . formats . RSAKEY_SCHEMA . matches ( imported_pubkey ) ) <EOL> imported_privkey = repo_lib . import_rsa_privatekey_from_file ( test_keypath , '<STR_LIT>' ) <EOL> self . assertTrue ( tuf . formats . RSAKEY_SCHEMA . matches ( imported_privkey ) ) <EOL> os . remove ( test_keypath ) <EOL> os . remove ( test_keypath + '<STR_LIT>' ) <EOL> repo_lib . generate_and_write_rsa_keypair ( test_keypath , bits = <NUM_LIT> , <EOL> password = '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( test_keypath ) ) <EOL> self . assertTrue ( os . path . exists ( test_keypath + '<STR_LIT>' ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_and_write_rsa_keypair , <EOL> <NUM_LIT:3> , bits = <NUM_LIT> , password = '<STR_LIT>' ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_and_write_rsa_keypair , <EOL> test_keypath , bits = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_and_write_rsa_keypair , <EOL> test_keypath , bits = <NUM_LIT> , password = <NUM_LIT:3> ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_and_write_rsa_keypair , <EOL> test_keypath , bits = <NUM_LIT> , password = '<STR_LIT>' ) <EOL> def test_import_rsa_privatekey_from_file ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> key_filepath = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( key_filepath ) ) <EOL> imported_rsa_key = repo_lib . import_rsa_privatekey_from_file ( key_filepath , <EOL> '<STR_LIT:password>' ) <EOL> self . assertTrue ( tuf . formats . RSAKEY_SCHEMA . matches ( imported_rsa_key ) ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> repo_lib . import_rsa_privatekey_from_file , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> nonexistent_keypath = os . path . join ( temporary_directory , <EOL> '<STR_LIT>' ) <EOL> self . assertRaises ( IOError , repo_lib . import_rsa_privatekey_from_file , <EOL> nonexistent_keypath , '<STR_LIT>' ) <EOL> invalid_keyfile = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> with open ( invalid_keyfile , '<STR_LIT:wb>' ) as file_object : <EOL> file_object . write ( b'<STR_LIT>' ) <EOL> self . assertRaises ( tuf . CryptoError , repo_lib . import_rsa_privatekey_from_file , <EOL> invalid_keyfile , '<STR_LIT>' ) <EOL> def test_import_rsa_publickey_from_file ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> key_filepath = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( key_filepath ) ) <EOL> imported_rsa_key = repo_lib . import_rsa_publickey_from_file ( key_filepath ) <EOL> self . assertTrue ( tuf . formats . RSAKEY_SCHEMA . matches ( imported_rsa_key ) ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> repo_lib . import_rsa_privatekey_from_file , <NUM_LIT:3> ) <EOL> nonexistent_keypath = os . path . join ( temporary_directory , <EOL> '<STR_LIT>' ) <EOL> self . assertRaises ( IOError , repo_lib . import_rsa_publickey_from_file , <EOL> nonexistent_keypath ) <EOL> invalid_keyfile = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> with open ( invalid_keyfile , '<STR_LIT:wb>' ) as file_object : <EOL> file_object . write ( b'<STR_LIT>' ) <EOL> self . assertRaises ( tuf . Error , repo_lib . import_rsa_publickey_from_file , <EOL> invalid_keyfile ) <EOL> def test_generate_and_write_ed25519_keypair ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> test_keypath = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> repo_lib . generate_and_write_ed25519_keypair ( test_keypath , password = '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( test_keypath ) ) <EOL> self . assertTrue ( os . path . exists ( test_keypath + '<STR_LIT>' ) ) <EOL> imported_pubkey = repo_lib . import_ed25519_publickey_from_file ( test_keypath + '<STR_LIT>' ) <EOL> self . assertTrue ( tuf . formats . ED25519KEY_SCHEMA . matches ( imported_pubkey ) ) <EOL> imported_privkey = repo_lib . import_ed25519_privatekey_from_file ( test_keypath , '<STR_LIT>' ) <EOL> self . assertTrue ( tuf . formats . ED25519KEY_SCHEMA . matches ( imported_privkey ) ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> repo_lib . generate_and_write_ed25519_keypair , <EOL> <NUM_LIT:3> , password = '<STR_LIT>' ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_and_write_rsa_keypair , <EOL> test_keypath , password = <NUM_LIT:3> ) <EOL> def test_import_ed25519_publickey_from_file ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> ed25519_keypath = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> repo_lib . generate_and_write_ed25519_keypair ( ed25519_keypath , password = '<STR_LIT>' ) <EOL> imported_ed25519_key = repo_lib . import_ed25519_publickey_from_file ( ed25519_keypath + '<STR_LIT>' ) <EOL> self . assertTrue ( tuf . formats . ED25519KEY_SCHEMA . matches ( imported_ed25519_key ) ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> repo_lib . import_ed25519_publickey_from_file , <NUM_LIT:3> ) <EOL> nonexistent_keypath = os . path . join ( temporary_directory , <EOL> '<STR_LIT>' ) <EOL> self . assertRaises ( IOError , repo_lib . import_ed25519_publickey_from_file , <EOL> nonexistent_keypath ) <EOL> invalid_keyfile = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> with open ( invalid_keyfile , '<STR_LIT:wb>' ) as file_object : <EOL> file_object . write ( b'<STR_LIT>' ) <EOL> self . assertRaises ( tuf . Error , repo_lib . import_ed25519_publickey_from_file , <EOL> invalid_keyfile ) <EOL> keytype = imported_ed25519_key [ '<STR_LIT>' ] <EOL> keyval = imported_ed25519_key [ '<STR_LIT>' ] <EOL> ed25519key_metadata_format = tuf . keys . format_keyval_to_metadata ( keytype , keyval , private = False ) <EOL> ed25519key_metadata_format [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> with open ( ed25519_keypath + '<STR_LIT>' , '<STR_LIT:wb>' ) as file_object : <EOL> file_object . write ( json . dumps ( ed25519key_metadata_format ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> repo_lib . import_ed25519_publickey_from_file , <EOL> ed25519_keypath + '<STR_LIT>' ) <EOL> def test_import_ed25519_privatekey_from_file ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> ed25519_keypath = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> repo_lib . generate_and_write_ed25519_keypair ( ed25519_keypath , password = '<STR_LIT>' ) <EOL> imported_ed25519_key = repo_lib . import_ed25519_privatekey_from_file ( ed25519_keypath , '<STR_LIT>' ) <EOL> self . assertTrue ( tuf . formats . ED25519KEY_SCHEMA . matches ( imported_ed25519_key ) ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> repo_lib . import_ed25519_privatekey_from_file , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> nonexistent_keypath = os . path . join ( temporary_directory , <EOL> '<STR_LIT>' ) <EOL> self . assertRaises ( IOError , repo_lib . import_ed25519_privatekey_from_file , <EOL> nonexistent_keypath , '<STR_LIT>' ) <EOL> invalid_keyfile = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> with open ( invalid_keyfile , '<STR_LIT:wb>' ) as file_object : <EOL> file_object . write ( b'<STR_LIT>' ) <EOL> self . assertRaises ( tuf . Error , repo_lib . import_ed25519_privatekey_from_file , <EOL> invalid_keyfile , '<STR_LIT>' ) <EOL> imported_ed25519_key [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> salt , iterations , derived_key = tuf . pycrypto_keys . _generate_derived_key ( '<STR_LIT>' ) <EOL> derived_key_information = { '<STR_LIT>' : salt , '<STR_LIT>' : iterations , <EOL> '<STR_LIT>' : derived_key } <EOL> encrypted_key = tuf . pycrypto_keys . _encrypt ( json . dumps ( imported_ed25519_key ) , <EOL> derived_key_information ) <EOL> with open ( ed25519_keypath , '<STR_LIT:wb>' ) as file_object : <EOL> file_object . write ( encrypted_key . encode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertRaises ( tuf . FormatError , <EOL> repo_lib . import_ed25519_privatekey_from_file , <EOL> ed25519_keypath , '<STR_LIT>' ) <EOL> def test_get_metadata_filenames ( self ) : <EOL> metadata_directory = os . path . join ( '<STR_LIT>' ) <EOL> filenames = { '<STR_LIT>' : metadata_directory + '<STR_LIT>' , <EOL> '<STR_LIT>' : metadata_directory + '<STR_LIT>' , <EOL> '<STR_LIT>' : metadata_directory + '<STR_LIT>' , <EOL> '<STR_LIT>' : metadata_directory + '<STR_LIT>' } <EOL> self . assertEqual ( filenames , repo_lib . get_metadata_filenames ( '<STR_LIT>' ) ) <EOL> metadata_directory = os . getcwd ( ) <EOL> filenames = { '<STR_LIT>' : os . path . join ( metadata_directory , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : os . path . join ( metadata_directory , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : os . path . join ( metadata_directory , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : os . path . join ( metadata_directory , '<STR_LIT>' ) } <EOL> self . assertEqual ( filenames , repo_lib . get_metadata_filenames ( ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . get_metadata_filenames , <NUM_LIT:3> ) <EOL> def test_get_metadata_fileinfo ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> test_filepath = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> with open ( test_filepath , '<STR_LIT>' ) as file_object : <EOL> file_object . write ( '<STR_LIT>' ) <EOL> file_length = os . path . getsize ( test_filepath ) <EOL> digest_object = tuf . hash . digest_filename ( test_filepath ) <EOL> file_hashes = { '<STR_LIT>' : digest_object . hexdigest ( ) } <EOL> fileinfo = { '<STR_LIT>' : file_length , '<STR_LIT>' : file_hashes } <EOL> self . assertTrue ( tuf . formats . FILEINFO_SCHEMA . matches ( fileinfo ) ) <EOL> self . assertEqual ( fileinfo , repo_lib . get_metadata_fileinfo ( test_filepath ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . get_metadata_fileinfo , <NUM_LIT:3> ) <EOL> nonexistent_filepath = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> self . assertRaises ( tuf . Error , repo_lib . get_metadata_fileinfo , <EOL> nonexistent_filepath ) <EOL> def test_get_target_hash ( self ) : <EOL> expected_target_hashes = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for filepath , target_hash in six . iteritems ( expected_target_hashes ) : <EOL> self . assertTrue ( tuf . formats . RELPATH_SCHEMA . matches ( filepath ) ) <EOL> self . assertTrue ( tuf . formats . HASH_SCHEMA . matches ( target_hash ) ) <EOL> self . assertEqual ( repo_lib . get_target_hash ( filepath ) , target_hash ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . get_target_hash , <NUM_LIT:8> ) <EOL> def test_generate_root_metadata ( self ) : <EOL> root_filepath = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> root_signable = tuf . util . load_json_file ( root_filepath ) <EOL> tuf . roledb . create_roledb_from_root_metadata ( root_signable [ '<STR_LIT>' ] ) <EOL> tuf . keydb . create_keydb_from_root_metadata ( root_signable [ '<STR_LIT>' ] ) <EOL> expires = '<STR_LIT>' <EOL> root_metadata = repo_lib . generate_root_metadata ( <NUM_LIT:1> , expires , <EOL> consistent_snapshot = False ) <EOL> self . assertTrue ( tuf . formats . ROOT_SCHEMA . matches ( root_metadata ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_root_metadata , <EOL> '<STR_LIT:3>' , expires , False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_root_metadata , <EOL> <NUM_LIT:1> , '<STR_LIT:3>' , False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_root_metadata , <EOL> <NUM_LIT:1> , expires , <NUM_LIT:3> ) <EOL> tuf . roledb . clear_roledb ( ) <EOL> tuf . keydb . clear_keydb ( ) <EOL> self . assertRaises ( tuf . Error , repo_lib . generate_root_metadata , <EOL> <NUM_LIT:1> , expires , False ) <EOL> def test_generate_targets_metadata ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> targets_directory = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> file1_path = os . path . join ( targets_directory , '<STR_LIT>' ) <EOL> tuf . util . ensure_parent_dir ( file1_path ) <EOL> with open ( file1_path , '<STR_LIT>' ) as file_object : <EOL> file_object . write ( '<STR_LIT>' ) <EOL> version = <NUM_LIT:1> <EOL> datetime_object = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:0> ) <EOL> expiration_date = datetime_object . isoformat ( ) + '<STR_LIT>' <EOL> file_permissions = oct ( os . stat ( file1_path ) . st_mode ) [ <NUM_LIT:4> : ] <EOL> target_files = { '<STR_LIT>' : { '<STR_LIT>' : file_permissions } } <EOL> delegations = { "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> } , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' <EOL> ] , <EOL> "<STR_LIT>" : <NUM_LIT:1> <EOL> } <EOL> ] <EOL> } <EOL> targets_metadata = repo_lib . generate_targets_metadata ( targets_directory , target_files , <EOL> version , expiration_date , delegations , <EOL> False ) <EOL> self . assertTrue ( tuf . formats . TARGETS_SCHEMA . matches ( targets_metadata ) ) <EOL> targets_metadata = repo_lib . generate_targets_metadata ( targets_directory , target_files , <EOL> version , expiration_date , None , <EOL> False ) <EOL> self . assertTrue ( tuf . formats . TARGETS_SCHEMA . matches ( targets_metadata ) ) <EOL> list_targets_directory = os . listdir ( targets_directory ) <EOL> targets_metadata = repo_lib . generate_targets_metadata ( targets_directory , target_files , <EOL> version , expiration_date , delegations , <EOL> write_consistent_targets = True ) <EOL> new_list_targets_directory = os . listdir ( targets_directory ) <EOL> self . assertTrue ( len ( list_targets_directory ) + <NUM_LIT:1> , <EOL> len ( new_list_targets_directory ) ) <EOL> self . assertTrue ( '<STR_LIT>' in targets_metadata [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_targets_metadata , <EOL> <NUM_LIT:3> , target_files , version , expiration_date ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_targets_metadata , <EOL> targets_directory , <NUM_LIT:3> , version , expiration_date ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_targets_metadata , <EOL> targets_directory , target_files , '<STR_LIT:3>' , expiration_date ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_targets_metadata , <EOL> targets_directory , target_files , version , '<STR_LIT:3>' ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_targets_metadata , <EOL> targets_directory , target_files , version , expiration_date , <EOL> <NUM_LIT:3> , False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_targets_metadata , <EOL> targets_directory , target_files , version , expiration_date , <EOL> delegations , <NUM_LIT:3> ) <EOL> bad_target_file = { '<STR_LIT>' : { '<STR_LIT>' : file_permissions } } <EOL> self . assertRaises ( tuf . Error , repo_lib . generate_targets_metadata , <EOL> targets_directory , bad_target_file , version , <EOL> expiration_date ) <EOL> def test_generate_snapshot_metadata ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> original_repository_path = os . path . join ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> repository_directory = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> shutil . copytree ( original_repository_path , repository_directory ) <EOL> metadata_directory = os . path . join ( repository_directory , <EOL> repo_lib . METADATA_STAGED_DIRECTORY_NAME ) <EOL> targets_directory = os . path . join ( repository_directory , repo_lib . TARGETS_DIRECTORY_NAME ) <EOL> root_filename = os . path . join ( metadata_directory , repo_lib . ROOT_FILENAME ) <EOL> targets_filename = os . path . join ( metadata_directory , <EOL> repo_lib . TARGETS_FILENAME ) <EOL> version = <NUM_LIT:1> <EOL> expiration_date = '<STR_LIT>' <EOL> repository = repo_tool . Repository ( repository_directory , metadata_directory , <EOL> targets_directory ) <EOL> repository_junk = repo_tool . load_repository ( repository_directory ) <EOL> root_filename = '<STR_LIT:root>' <EOL> targets_filename = '<STR_LIT>' <EOL> snapshot_metadata = repo_lib . generate_snapshot_metadata ( metadata_directory , version , <EOL> expiration_date , root_filename , <EOL> targets_filename , <EOL> consistent_snapshot = False ) <EOL> self . assertTrue ( tuf . formats . SNAPSHOT_SCHEMA . matches ( snapshot_metadata ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_snapshot_metadata , <EOL> <NUM_LIT:3> , version , expiration_date , <EOL> root_filename , targets_filename , consistent_snapshot = False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_snapshot_metadata , <EOL> metadata_directory , '<STR_LIT:3>' , expiration_date , <EOL> root_filename , targets_filename , consistent_snapshot = False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_snapshot_metadata , <EOL> metadata_directory , version , '<STR_LIT:3>' , <EOL> root_filename , targets_filename , consistent_snapshot = False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_snapshot_metadata , <EOL> metadata_directory , version , expiration_date , <EOL> <NUM_LIT:3> , targets_filename , consistent_snapshot = False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_snapshot_metadata , <EOL> metadata_directory , version , expiration_date , <EOL> root_filename , <NUM_LIT:3> , consistent_snapshot = False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_snapshot_metadata , <EOL> metadata_directory , version , expiration_date , <EOL> root_filename , targets_filename , <NUM_LIT:3> ) <EOL> def test_generate_timestamp_metadata ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> original_repository_path = os . path . join ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> repository_directory = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> shutil . copytree ( original_repository_path , repository_directory ) <EOL> metadata_directory = os . path . join ( repository_directory , <EOL> repo_lib . METADATA_STAGED_DIRECTORY_NAME ) <EOL> snapshot_filename = os . path . join ( metadata_directory , <EOL> repo_lib . SNAPSHOT_FILENAME ) <EOL> version = <NUM_LIT:1> <EOL> expiration_date = '<STR_LIT>' <EOL> snapshot_metadata = repo_lib . generate_timestamp_metadata ( snapshot_filename , version , <EOL> expiration_date ) <EOL> self . assertTrue ( tuf . formats . TIMESTAMP_SCHEMA . matches ( snapshot_metadata ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_timestamp_metadata , <EOL> <NUM_LIT:3> , version , expiration_date ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_timestamp_metadata , <EOL> snapshot_filename , '<STR_LIT:3>' , expiration_date ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . generate_timestamp_metadata , <EOL> snapshot_filename , version , '<STR_LIT:3>' ) <EOL> def test_sign_metadata ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> metadata_path = os . path . join ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> keystore_path = os . path . join ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> root_filename = os . path . join ( metadata_path , '<STR_LIT>' ) <EOL> root_metadata = tuf . util . load_json_file ( root_filename ) [ '<STR_LIT>' ] <EOL> tuf . keydb . create_keydb_from_root_metadata ( root_metadata ) <EOL> tuf . roledb . create_roledb_from_root_metadata ( root_metadata ) <EOL> root_keyids = tuf . roledb . get_role_keyids ( '<STR_LIT:root>' ) <EOL> root_private_keypath = os . path . join ( keystore_path , '<STR_LIT>' ) <EOL> root_private_key = repo_lib . import_rsa_privatekey_from_file ( root_private_keypath , '<STR_LIT:password>' ) <EOL> targets_private_keypath = os . path . join ( keystore_path , '<STR_LIT>' ) <EOL> targets_private_key = repo_lib . import_rsa_privatekey_from_file ( targets_private_keypath , <EOL> '<STR_LIT:password>' ) <EOL> tuf . keydb . remove_key ( root_private_key [ '<STR_LIT>' ] ) <EOL> tuf . keydb . add_key ( root_private_key ) <EOL> tuf . keydb . remove_key ( targets_private_key [ '<STR_LIT>' ] ) <EOL> tuf . keydb . add_key ( targets_private_key ) <EOL> root_keyids . extend ( tuf . roledb . get_role_keyids ( '<STR_LIT>' ) ) <EOL> root_keyids . extend ( tuf . roledb . get_role_keyids ( '<STR_LIT>' ) ) <EOL> root_signable = repo_lib . sign_metadata ( root_metadata , root_keyids , <EOL> root_filename ) <EOL> self . assertTrue ( tuf . formats . SIGNABLE_SCHEMA . matches ( root_signable ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . sign_metadata , <NUM_LIT:3> , root_keyids , <EOL> '<STR_LIT>' ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . sign_metadata , root_metadata , <EOL> <NUM_LIT:3> , '<STR_LIT>' ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . sign_metadata , root_metadata , <EOL> root_keyids , <NUM_LIT:3> ) <EOL> def test_write_metadata_file ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> metadata_directory = os . path . join ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> root_filename = os . path . join ( metadata_directory , '<STR_LIT>' ) <EOL> root_signable = tuf . util . load_json_file ( root_filename ) <EOL> output_filename = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> compression_algorithms = [ '<STR_LIT>' ] <EOL> version_number = root_signable [ '<STR_LIT>' ] [ '<STR_LIT:version>' ] + <NUM_LIT:1> <EOL> self . assertFalse ( os . path . exists ( output_filename ) ) <EOL> repo_lib . write_metadata_file ( root_signable , output_filename , <EOL> version_number , <EOL> compression_algorithms , <EOL> consistent_snapshot = False ) <EOL> self . assertTrue ( os . path . exists ( output_filename ) ) <EOL> self . assertTrue ( os . path . exists ( output_filename + '<STR_LIT>' ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . write_metadata_file , <EOL> <NUM_LIT:3> , output_filename , version_number , <EOL> compression_algorithms , False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . write_metadata_file , <EOL> root_signable , <NUM_LIT:3> , version_number , compression_algorithms , <EOL> False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . write_metadata_file , <EOL> root_signable , output_filename , '<STR_LIT:3>' , <EOL> compression_algorithms , False ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . write_metadata_file , <EOL> root_signable , output_filename , version_number , <EOL> compression_algorithms , <NUM_LIT:3> ) <EOL> def test_create_tuf_client_directory ( self ) : <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> repository_directory = os . path . join ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> client_directory = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> repo_lib . create_tuf_client_directory ( repository_directory , client_directory ) <EOL> self . assertTrue ( os . path . exists ( client_directory ) ) <EOL> metadata_directory = os . path . join ( client_directory , '<STR_LIT>' ) <EOL> current_directory = os . path . join ( metadata_directory , '<STR_LIT>' ) <EOL> previous_directory = os . path . join ( metadata_directory , '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( client_directory ) ) <EOL> self . assertTrue ( os . path . exists ( metadata_directory ) ) <EOL> self . assertTrue ( os . path . exists ( current_directory ) ) <EOL> self . assertTrue ( os . path . exists ( previous_directory ) ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . create_tuf_client_directory , <EOL> <NUM_LIT:3> , client_directory ) <EOL> self . assertRaises ( tuf . FormatError , repo_lib . create_tuf_client_directory , <EOL> repository_directory , <NUM_LIT:3> ) <EOL> self . assertRaises ( tuf . RepositoryError , repo_lib . create_tuf_client_directory , <EOL> repository_directory , client_directory ) <EOL> shutil . rmtree ( metadata_directory ) <EOL> current_client_directory_mode = os . stat ( client_directory ) [ stat . ST_MODE ] <EOL> os . chmod ( client_directory , current_client_directory_mode & ~ stat . S_IWUSR ) <EOL> self . assertRaises ( OSError , repo_lib . create_tuf_client_directory , <EOL> repository_directory , client_directory ) <EOL> os . chmod ( client_directory , current_client_directory_mode ) <EOL> def test__check_directory ( self ) : <EOL> self . assertRaises ( tuf . Error , repo_lib . _check_directory , '<STR_LIT>' ) <EOL> def test__generate_and_write_metadata ( self ) : <EOL> root_filepath = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> root_signable = tuf . util . load_json_file ( root_filepath ) <EOL> tuf . roledb . create_roledb_from_root_metadata ( root_signable [ '<STR_LIT>' ] ) <EOL> temporary_directory = tempfile . mkdtemp ( dir = self . temporary_directory ) <EOL> targets_directory = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> os . mkdir ( targets_directory ) <EOL> repository_directory = os . path . join ( temporary_directory , '<STR_LIT>' ) <EOL> metadata_directory = os . path . join ( repository_directory , <EOL> repo_lib . METADATA_STAGED_DIRECTORY_NAME ) <EOL> targets_metadata = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> obsolete_metadata = os . path . join ( metadata_directory , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> tuf . util . ensure_parent_dir ( obsolete_metadata ) <EOL> shutil . copyfile ( targets_metadata , obsolete_metadata ) <EOL> roleinfo = { '<STR_LIT>' : [ '<STR_LIT>' ] , '<STR_LIT>' : <NUM_LIT:1> } <EOL> tuf . roledb . add_role ( '<STR_LIT>' , roleinfo ) <EOL> self . assertRaises ( tuf . Error , <EOL> tuf . repository_lib . _generate_and_write_metadata , <EOL> '<STR_LIT>' , '<STR_LIT>' , False , <EOL> targets_directory , metadata_directory ) <EOL> targets_roleinfo = tuf . roledb . get_roleinfo ( '<STR_LIT>' ) <EOL> targets_roleinfo [ '<STR_LIT:version>' ] = <NUM_LIT:1> <EOL> expiration = tuf . formats . unix_timestamp_to_datetime ( int ( time . time ( ) + <NUM_LIT> ) ) <EOL> expiration = expiration . isoformat ( ) + '<STR_LIT>' <EOL> targets_roleinfo [ '<STR_LIT>' ] = expiration <EOL> tuf . roledb . add_role ( '<STR_LIT>' , targets_roleinfo ) <EOL> snapshot_filepath = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> snapshot_signable = tuf . util . load_json_file ( snapshot_filepath ) <EOL> tuf . roledb . remove_role ( '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( os . path . join ( metadata_directory , <EOL> '<STR_LIT>' ) ) ) <EOL> tuf . repository_lib . _delete_obsolete_metadata ( metadata_directory , <EOL> snapshot_signable [ '<STR_LIT>' ] , <EOL> False ) <EOL> self . assertFalse ( os . path . exists ( metadata_directory + '<STR_LIT>' ) ) <EOL> def test__remove_invalid_and_duplicate_signatures ( self ) : <EOL> root_filepath = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> root_signable = tuf . util . load_json_file ( root_filepath ) <EOL> key_filepath = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> root_rsa_key = repo_lib . import_rsa_privatekey_from_file ( key_filepath , <EOL> '<STR_LIT:password>' ) <EOL> new_pss_signature = tuf . keys . create_signature ( root_rsa_key , <EOL> root_signable [ '<STR_LIT>' ] ) <EOL> root_signable [ '<STR_LIT>' ] . append ( new_pss_signature ) <EOL> expected_number_of_signatures = len ( root_signable [ '<STR_LIT>' ] ) <EOL> tuf . repository_lib . _remove_invalid_and_duplicate_signatures ( root_signable ) <EOL> self . assertEqual ( len ( root_signable ) , expected_number_of_signatures ) <EOL> root_signable [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> tuf . repository_lib . _remove_invalid_and_duplicate_signatures ( root_signable ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
