def setUp(self):
    # We are inheriting from custom class.
    unittest_toolbox.Modified_TestCase.setUp(self)

    self.repository_name = 'test_repository1'

    # Copy the original repository files provided in the test folder so that
    # any modifications made to repository files are restricted to the copies.
    # The 'repository_data' directory is expected to exist in 'tuf.tests/'.
    original_repository_files = os.path.join(os.getcwd(), 'repository_data')
    temporary_repository_root = \
      self.make_temp_directory(directory=self.temporary_directory)

    # The original repository, keystore, and client directories will be copied
    # for each test case.
    original_repository = os.path.join(original_repository_files, 'repository')
    original_keystore = os.path.join(original_repository_files, 'keystore')
    original_client = os.path.join(original_repository_files, 'client')

    # Save references to the often-needed client repository directories.
    # Test cases need these references to access metadata and target files.
    self.repository_directory = \
      os.path.join(temporary_repository_root, 'repository')
    self.keystore_directory = \
      os.path.join(temporary_repository_root, 'keystore')
    self.client_directory = os.path.join(temporary_repository_root, 'client')
    self.client_metadata = os.path.join(self.client_directory,
        self.repository_name, 'metadata')
    self.client_metadata_current = os.path.join(self.client_metadata, 'current')
    self.client_metadata_previous = os.path.join(self.client_metadata, 'previous')

    # Copy the original 'repository', 'client', and 'keystore' directories
    # to the temporary repository the test cases can use.
    shutil.copytree(original_repository, self.repository_directory)
    shutil.copytree(original_client, self.client_directory)
    shutil.copytree(original_keystore, self.keystore_directory)

    # 'path/to/tmp/repository' -> 'localhost:8001/tmp/repository'.
    repository_basepath = self.repository_directory[len(os.getcwd()):]
    url_prefix = \
      'http://localhost:' + str(self.SERVER_PORT) + repository_basepath

    # Setting 'tuf.settings.repository_directory' with the temporary client
    # directory copied from the original repository files.
    tuf.settings.repositories_directory = self.client_directory

    self.repository_mirrors = {'mirror1': {'url_prefix': url_prefix,
                                           'metadata_path': 'metadata',
                                           'targets_path': 'targets',
                                           'confined_target_dirs': ['']}}

    # Creating a repository instance.  The test cases will use this client
    # updater to refresh metadata, fetch target files, etc.
    self.repository_updater = updater.Updater(self.repository_name,
                                              self.repository_mirrors)

    # Metadata role keys are needed by the test cases to make changes to the
    # repository (e.g., adding a new target file to 'targets.json' and then
    # requesting a refresh()).
    self.role_keys = _load_role_keys(self.keystore_directory)
  def setUp(self):
    # We are inheriting from custom class.
    unittest_toolbox.Modified_TestCase.setUp(self)
  
    # Copy the original repository files provided in the test folder so that
    # any modifications made to repository files are restricted to the copies.
    # The 'repository_data' directory is expected to exist in 'tuf/tests/'.
    original_repository_files = os.path.join(os.getcwd(), 'repository_data') 
    temporary_repository_root = \
      self.make_temp_directory(directory=self.temporary_directory)
  
    # The original repository, keystore, and client directories will be copied
    # for each test case. 
    original_repository = os.path.join(original_repository_files, 'repository')
    original_client = os.path.join(original_repository_files, 'client')
    original_keystore = os.path.join(original_repository_files, 'keystore')
    
    # Save references to the often-needed client repository directories.
    # Test cases need these references to access metadata and target files. 
    self.repository_directory = \
      os.path.join(temporary_repository_root, 'repository')
    self.client_directory = os.path.join(temporary_repository_root, 'client')
    self.keystore_directory = os.path.join(temporary_repository_root, 'keystore')
    
    # Copy the original 'repository', 'client', and 'keystore' directories
    # to the temporary repository the test cases can use.
    shutil.copytree(original_repository, self.repository_directory)
    shutil.copytree(original_client, self.client_directory)
    shutil.copytree(original_keystore, self.keystore_directory)
    
    # Set the url prefix required by the 'tuf/client/updater.py' updater.
    # 'path/to/tmp/repository' -> 'localhost:8001/tmp/repository'. 
    repository_basepath = self.repository_directory[len(os.getcwd()):]
    url_prefix = \
      'http://localhost:' + str(self.SERVER_PORT) + repository_basepath 
    
    # Setting 'tuf.conf.repository_directory' with the temporary client
    # directory copied from the original repository files.
    tuf.conf.repository_directory = self.client_directory 

    # Creating a repository instance.  The test cases will use this client
    # updater to refresh metadata, fetch target files, etc.
    self.repository_updater = updater.Updater('testupdater')


    # Need to override pinned.json mirrors for testing. /:
    # Point it to the right URL with the randomly selected port generated in
    # this test setup.
    mirrors = self.repository_updater.pinned_metadata['repositories'][
        'defaultrepo']['mirrors']

    for i in range(0, len(mirrors)):
      if '<DETERMINED_IN_TEST_SETUP>' in mirrors[i]:
        mirrors[i] = mirrors[i].replace(
            '<DETERMINED_IN_TEST_SETUP>', str(url_prefix))

    self.repository_updater.pinned_metadata['repositories']['defaultrepo'][
        'mirrors'] = mirrors
Beispiel #3
0
    def setUp(self):
        # We are inheriting from custom class.
        unittest_toolbox.Modified_TestCase.setUp(self)

        self.repository_name = 'test_repository1'

        # Copy the original repository files provided in the test folder so that
        # any modifications made to repository files are restricted to the copies.
        # The 'repository_data' directory is expected to exist in 'tuf/tests/'.
        original_repository_files = os.path.join(os.getcwd(),
                                                 'repository_data')
        temporary_repository_root = \
          self.make_temp_directory(directory=self.temporary_directory)

        # The original repository, keystore, and client directories will be copied
        # for each test case.
        original_repository = os.path.join(original_repository_files,
                                           'repository')
        original_client = os.path.join(original_repository_files, 'client')
        original_keystore = os.path.join(original_repository_files, 'keystore')

        # Save references to the often-needed client repository directories.
        # Test cases need these references to access metadata and target files.
        self.repository_directory = \
          os.path.join(temporary_repository_root, 'repository')
        self.client_directory = os.path.join(temporary_repository_root,
                                             'client')
        self.keystore_directory = os.path.join(temporary_repository_root,
                                               'keystore')

        # Copy the original 'repository', 'client', and 'keystore' directories
        # to the temporary repository the test cases can use.
        shutil.copytree(original_repository, self.repository_directory)
        shutil.copytree(original_client, self.client_directory)
        shutil.copytree(original_keystore, self.keystore_directory)

        # Set the url prefix required by the 'tuf/client/updater.py' updater.
        # 'path/to/tmp/repository' -> 'localhost:8001/tmp/repository'.
        repository_basepath = self.repository_directory[len(os.getcwd()):]
        url_prefix = 'http://localhost:' \
          + str(self.server_process_handler.port) + repository_basepath

        # Setting 'tuf.settings.repository_directory' with the temporary client
        # directory copied from the original repository files.
        tuf.settings.repositories_directory = self.client_directory
        self.repository_mirrors = {
            'mirror1': {
                'url_prefix': url_prefix,
                'metadata_path': 'metadata',
                'targets_path': 'targets'
            }
        }

        # Create the repository instance.  The test cases will use this client
        # updater to refresh metadata, fetch target files, etc.
        self.repository_updater = updater.Updater(self.repository_name,
                                                  self.repository_mirrors)
Beispiel #4
0
    def test__init__exceptions(self):
        # Setup:
        # Create an empty repository structure for client.
        repo_dir = self.make_temp_directory()

        # Config patch.  The repository directory must be configured in 'tuf.conf'.
        tuf.conf.repository_directory = repo_dir

        # Test: empty repository.
        self.assertRaises(tuf.RepositoryError, updater.Updater, 'Repo_Name',
                          self.mirrors)

        # Test: empty repository with {repository_dir}/metadata directory.
        meta_dir = os.path.join(repo_dir, 'metadata')
        os.mkdir(meta_dir)
        self.assertRaises(tuf.RepositoryError, updater.Updater, 'Repo_Name',
                          self.mirrors)

        # Test: empty repository with {repository_dir}/metadata/current directory.
        current_dir = os.path.join(meta_dir, 'current')
        os.mkdir(current_dir)
        self.assertRaises(tuf.RepositoryError, updater.Updater, 'Repo_Name',
                          self.mirrors)

        # Test: normal case.
        repositories = setup.create_repositories()
        client_repo_dir = repositories['client_repository']
        tuf.conf.repository_directory = client_repo_dir
        updater.Updater('Repo_Name', self.mirrors)

        # Test: case w/ only root metadata file present in the current dir.
        client_current_dir = os.path.join(client_repo_dir, 'metadata',
                                          'current')
        for directory, junk, role_list in os.walk(client_current_dir):
            for role_filepath in role_list:
                role_filepath = os.path.join(directory, role_filepath)
                if role_filepath.endswith('root.txt'):
                    continue
                os.remove(role_filepath)
        updater.Updater('Repo_Name', self.mirrors)

        #  Remove all created repositories and roles.
        setup.remove_all_repositories(repositories['main_repository'])
        tuf.roledb.clear_roledb()
Beispiel #5
0
    def setUp(self):
        #  We are inheriting from custom class.
        unittest_toolbox.Modified_TestCase.setUp(self)

        #  Patching 'tuf.conf.repository_directory' with the one we set up.
        tuf.conf.repository_directory = self.client_repo_dir

        #  Creating Repository instance.
        self.Repository = updater.Updater('Client_Repository', self.mirrors)

        #  List of all role paths, (in order they are updated).  This list will be
        #  used as an optional argument to 'download_url_to_tempfileobj' patch
        #  function.
        self.all_role_paths = [
            self.timestamp_filepath, self.release_filepath, self.root_filepath,
            self.targets_filepath, self.delegated_filepath1,
            self.delegated_filepath2
        ]

        #  Making sure that server and client's metadata files are the same.
        shutil.rmtree(self.client_current_dir)
        shutil.copytree(self.server_meta_dir, self.client_current_dir)
Beispiel #6
0
    def setUp(self):
        # We are inheriting from custom class.
        unittest_toolbox.Modified_TestCase.setUp(self)

        self.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())

        # Copy the original repository files provided in the test folder so that
        # any modifications made to repository files are restricted to the copies.
        # The 'repository_data' directory is expected to exist in 'tuf/tests/'.
        original_repository_files = os.path.join(os.getcwd(),
                                                 'repository_data')

        self.temporary_repository_root = self.make_temp_directory(
            directory=self.temporary_directory)

        # The original repository, keystore, and client directories will be copied
        # for each test case.
        original_repository = os.path.join(original_repository_files,
                                           'repository')
        original_client = os.path.join(original_repository_files, 'client',
                                       'test_repository1')
        original_keystore = os.path.join(original_repository_files, 'keystore')
        original_map_file = os.path.join(original_repository_files, 'map.json')

        # Save references to the often-needed client repository directories.
        # Test cases need these references to access metadata and target files.
        self.repository_directory = os.path.join(
            self.temporary_repository_root, 'repository_server1')
        self.repository_directory2 = os.path.join(
            self.temporary_repository_root, 'repository_server2')

        # Setting 'tuf.settings.repositories_directory' with the temporary client
        # directory copied from the original repository files.
        tuf.settings.repositories_directory = self.temporary_repository_root

        self.repository_name = 'test_repository1'
        self.repository_name2 = 'test_repository2'

        self.client_directory = os.path.join(self.temporary_repository_root,
                                             self.repository_name)
        self.client_directory2 = os.path.join(self.temporary_repository_root,
                                              self.repository_name2)

        self.keystore_directory = os.path.join(self.temporary_repository_root,
                                               'keystore')
        self.map_file = os.path.join(self.client_directory, 'map.json')
        self.map_file2 = os.path.join(self.client_directory2, 'map.json')

        # Copy the original 'repository', 'client', and 'keystore' directories
        # to the temporary repository the test cases can use.
        shutil.copytree(original_repository, self.repository_directory)
        shutil.copytree(original_repository, self.repository_directory2)
        shutil.copytree(original_client, self.client_directory)
        shutil.copytree(original_client, self.client_directory2)
        shutil.copyfile(original_map_file, self.map_file)
        shutil.copyfile(original_map_file, self.map_file2)
        shutil.copytree(original_keystore, self.keystore_directory)

        # Launch a SimpleHTTPServer (serves files in the current directory).
        # Test cases will request metadata and target files that have been
        # pre-generated in 'tuf/tests/repository_data', which will be served by the
        # SimpleHTTPServer launched here.  The test cases of this unit test assume
        # the pre-generated metadata files have a specific structure, such
        # as a delegated role 'targets/role1', three target files, five key files,
        # etc.
        self.SERVER_PORT = random.SystemRandom().randint(30000, 45000)
        self.SERVER_PORT2 = random.SystemRandom().randint(30000, 45000)

        # Avoid duplicate port numbers, to prevent multiple localhosts from
        # listening on the same port.
        while self.SERVER_PORT == self.SERVER_PORT2:
            self.SERVER_PORT2 = random.SystemRandom().randint(30000, 45000)

        # Needed because in some tests simple_server.py cannot be found.
        # The reason is that the current working directory
        # has been changed when executing a subprocess.
        SIMPLE_SERVER_PATH = os.path.join(os.getcwd(), 'simple_server.py')

        command = ['python', SIMPLE_SERVER_PATH, str(self.SERVER_PORT)]
        command2 = ['python', SIMPLE_SERVER_PATH, str(self.SERVER_PORT2)]

        self.server_process = subprocess.Popen(command,
                                               cwd=self.repository_directory)

        logger.debug('Server process started.')
        logger.debug('Server process id: ' + str(self.server_process.pid))
        logger.debug('Serving on port: ' + str(self.SERVER_PORT))

        self.server_process2 = subprocess.Popen(command2,
                                                cwd=self.repository_directory2)

        logger.debug('Server process 2 started.')
        logger.debug('Server 2 process id: ' + str(self.server_process2.pid))
        logger.debug('Serving 2 on port: ' + str(self.SERVER_PORT2))
        self.url = 'http://localhost:' + str(self.SERVER_PORT) + os.path.sep
        self.url2 = 'http://localhost:' + str(self.SERVER_PORT2) + os.path.sep

        # NOTE: Following error is raised if a delay is not long enough:
        # <urlopen error [Errno 111] Connection refused>
        # or, on Windows:
        # Failed to establish a new connection: [Errno 111] Connection refused'
        # 0.3s led to occasional failures in automated builds, primarily on
        # AppVeyor, so increasing this to 2s, sadly.
        time.sleep(2)

        url_prefix = 'http://localhost:' + str(self.SERVER_PORT)
        url_prefix2 = 'http://localhost:' + str(self.SERVER_PORT2)

        self.repository_mirrors = {
            'mirror1': {
                'url_prefix': url_prefix,
                'metadata_path': 'metadata',
                'targets_path': 'targets',
                'confined_target_dirs': ['']
            }
        }

        self.repository_mirrors2 = {
            'mirror1': {
                'url_prefix': url_prefix2,
                'metadata_path': 'metadata',
                'targets_path': 'targets',
                'confined_target_dirs': ['']
            }
        }

        # Create the repository instances.  The test cases will use these client
        # updaters to refresh metadata, fetch target files, etc.
        self.repository_updater = updater.Updater(self.repository_name,
                                                  self.repository_mirrors)
        self.repository_updater2 = updater.Updater(self.repository_name2,
                                                   self.repository_mirrors2)
Beispiel #7
0
  def setUp(self):
    # We are inheriting from custom class.
    unittest_toolbox.Modified_TestCase.setUp(self)

    self.repository_name = 'test_repository1'

    # Copy the original repository files provided in the test folder so that
    # any modifications made to repository files are restricted to the copies.
    # The 'repository_data' directory is expected to exist in 'tuf/tests/'.
    original_repository_files = os.path.join(os.getcwd(), 'repository_data')
    temporary_repository_root = \
      self.make_temp_directory(directory=self.temporary_directory)

    # The original repository, keystore, and client directories will be copied
    # for each test case.
    original_repository = os.path.join(original_repository_files, 'repository')
    original_client = os.path.join(original_repository_files, 'client')
    original_keystore = os.path.join(original_repository_files, 'keystore')

    # Save references to the often-needed client repository directories.
    # Test cases need these references to access metadata and target files.
    self.repository_directory = \
      os.path.join(temporary_repository_root, 'repository')
    self.client_directory = os.path.join(temporary_repository_root, 'client')
    self.keystore_directory = os.path.join(temporary_repository_root, 'keystore')

    # Copy the original 'repository', 'client', and 'keystore' directories
    # to the temporary repository the test cases can use.
    shutil.copytree(original_repository, self.repository_directory)
    shutil.copytree(original_client, self.client_directory)
    shutil.copytree(original_keystore, self.keystore_directory)


    # Produce a longer target file than exists in the other test repository
    # data, to provide for a long-duration slow attack. Then we'll write new
    # top-level metadata that includes a hash over that file, and provide that
    # metadata to the client as well.

    # The slow retrieval server, in mode 2 (1 byte per second), will only
    # sleep for a  total of (target file size) seconds.  Add a target file
    # that contains sufficient number of bytes to trigger a slow retrieval
    # error. A transfer should not be permitted to take 1 second per byte
    # transferred. Because this test is currently expected to fail, I'm
    # limiting the size to 10 bytes (10 seconds) to avoid expected testing
    # delays.... Consider increasing again after fix, to, e.g. 400.
    total_bytes = 10

    repository = repo_tool.load_repository(self.repository_directory)
    file1_filepath = os.path.join(self.repository_directory, 'targets',
                                  'file1.txt')
    with open(file1_filepath, 'wb') as file_object:
      data = 'a' * int(round(total_bytes))
      file_object.write(data.encode('utf-8'))

    key_file = os.path.join(self.keystore_directory, 'timestamp_key')
    timestamp_private = repo_tool.import_ed25519_privatekey_from_file(key_file,
                                                                  'password')
    key_file = os.path.join(self.keystore_directory, 'snapshot_key')
    snapshot_private = repo_tool.import_ed25519_privatekey_from_file(key_file,
                                                                  'password')
    key_file = os.path.join(self.keystore_directory, 'targets_key')
    targets_private = repo_tool.import_ed25519_privatekey_from_file(key_file,
                                                                  'password')

    repository.targets.load_signing_key(targets_private)
    repository.snapshot.load_signing_key(snapshot_private)
    repository.timestamp.load_signing_key(timestamp_private)

    repository.writeall()

    # Move the staged metadata to the "live" metadata.
    shutil.rmtree(os.path.join(self.repository_directory, 'metadata'))
    shutil.copytree(os.path.join(self.repository_directory, 'metadata.staged'),
                    os.path.join(self.repository_directory, 'metadata'))

    # Since we've changed the repository metadata in this setup (by lengthening
    # a target file and then writing new metadata), we also have to update the
    # client metadata to get to the expected initial state, where the client
    # knows the right target info (and so expects the right, longer target
    # length.
    # We'll skip using updater.refresh since we don't have a server running,
    # and we'll update the metadata locally, manually.
    shutil.rmtree(os.path.join(
        self.client_directory, self.repository_name, 'metadata', 'current'))
    shutil.copytree(os.path.join(self.repository_directory, 'metadata'),
        os.path.join(self.client_directory, self.repository_name, 'metadata',
        'current'))

    # Set the url prefix required by the 'tuf/client/updater.py' updater.
    # 'path/to/tmp/repository' -> 'localhost:8001/tmp/repository'.
    repository_basepath = self.repository_directory[len(os.getcwd()):]
    url_prefix = \
      'http://localhost:' + str(self.SERVER_PORT) + repository_basepath

    # Setting 'tuf.settings.repository_directory' with the temporary client
    # directory copied from the original repository files.
    tuf.settings.repositories_directory = self.client_directory
    self.repository_mirrors = {'mirror1': {'url_prefix': url_prefix,
                                           'metadata_path': 'metadata',
                                           'targets_path': 'targets',
                                           'confined_target_dirs': ['']}}

    # Create the repository instance.  The test cases will use this client
    # updater to refresh metadata, fetch target files, etc.
    self.repository_updater = updater.Updater(self.repository_name,
                                              self.repository_mirrors)
    def setUp(self):
        # We are inheriting from custom class.
        unittest_toolbox.Modified_TestCase.setUp(self)

        self.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())

        # Copy the original repository files provided in the test folder so that
        # any modifications made to repository files are restricted to the copies.
        # The 'repository_data' directory is expected to exist in 'tuf/tests/'.
        original_repository_files = os.path.join(os.getcwd(),
                                                 'repository_data')

        self.temporary_repository_root = self.make_temp_directory(
            directory=self.temporary_directory)

        # The original repository, keystore, and client directories will be copied
        # for each test case.
        original_repository = os.path.join(original_repository_files,
                                           'repository')
        original_client = os.path.join(original_repository_files, 'client',
                                       'test_repository1')
        original_keystore = os.path.join(original_repository_files, 'keystore')
        original_map_file = os.path.join(original_repository_files, 'map.json')

        # Save references to the often-needed client repository directories.
        # Test cases need these references to access metadata and target files.
        self.repository_directory = os.path.join(
            self.temporary_repository_root, 'repository_server1')
        self.repository_directory2 = os.path.join(
            self.temporary_repository_root, 'repository_server2')

        # Setting 'tuf.settings.repositories_directory' with the temporary client
        # directory copied from the original repository files.
        tuf.settings.repositories_directory = self.temporary_repository_root

        self.repository_name = 'test_repository1'
        self.repository_name2 = 'test_repository2'

        self.client_directory = os.path.join(self.temporary_repository_root,
                                             self.repository_name)
        self.client_directory2 = os.path.join(self.temporary_repository_root,
                                              self.repository_name2)

        self.keystore_directory = os.path.join(self.temporary_repository_root,
                                               'keystore')
        self.map_file = os.path.join(self.client_directory, 'map.json')
        self.map_file2 = os.path.join(self.client_directory2, 'map.json')

        # Copy the original 'repository', 'client', and 'keystore' directories
        # to the temporary repository the test cases can use.
        shutil.copytree(original_repository, self.repository_directory)
        shutil.copytree(original_repository, self.repository_directory2)
        shutil.copytree(original_client, self.client_directory)
        shutil.copytree(original_client, self.client_directory2)
        shutil.copyfile(original_map_file, self.map_file)
        shutil.copyfile(original_map_file, self.map_file2)
        shutil.copytree(original_keystore, self.keystore_directory)

        # Launch a SimpleHTTPServer (serves files in the current directory).
        # Test cases will request metadata and target files that have been
        # pre-generated in 'tuf/tests/repository_data', which will be served by the
        # SimpleHTTPServer launched here.  The test cases of this unit test assume
        # the pre-generated metadata files have a specific structure, such
        # as a delegated role 'targets/role1', three target files, five key files,
        # etc.
        self.SERVER_PORT = random.SystemRandom().randint(30000, 45000)
        self.SERVER_PORT2 = random.SystemRandom().randint(30000, 45000)

        # Avoid duplicate port numbers, to prevent multiple localhosts from
        # listening on the same port.
        while self.SERVER_PORT == self.SERVER_PORT2:
            self.SERVER_PORT2 = random.SystemRandom().randint(30000, 45000)

        # Needed because in some tests simple_server.py cannot be found.
        # The reason is that the current working directory
        # has been changed when executing a subprocess.
        SIMPLE_SERVER_PATH = os.path.join(os.getcwd(), 'simple_server.py')

        # Creates a subprocess running server and uses temp file for logging.
        self.server_process_handler = utils.TestServerProcess(
            log=logger,
            port=self.SERVER_PORT,
            server=SIMPLE_SERVER_PATH,
            popen_cwd=self.repository_directory)

        logger.debug('Server process started.')

        # Creates a subprocess running server and uses temp file for logging.
        self.server_process_handler2 = utils.TestServerProcess(
            log=logger,
            port=self.SERVER_PORT2,
            server=SIMPLE_SERVER_PATH,
            popen_cwd=self.repository_directory2)

        logger.debug('Server process 2 started.')

        url_prefix = 'http://localhost:' + str(self.SERVER_PORT)
        url_prefix2 = 'http://localhost:' + str(self.SERVER_PORT2)

        self.repository_mirrors = {
            'mirror1': {
                'url_prefix': url_prefix,
                'metadata_path': 'metadata',
                'targets_path': 'targets'
            }
        }

        self.repository_mirrors2 = {
            'mirror1': {
                'url_prefix': url_prefix2,
                'metadata_path': 'metadata',
                'targets_path': 'targets'
            }
        }

        # Create the repository instances.  The test cases will use these client
        # updaters to refresh metadata, fetch target files, etc.
        self.repository_updater = updater.Updater(self.repository_name,
                                                  self.repository_mirrors)
        self.repository_updater2 = updater.Updater(self.repository_name2,
                                                   self.repository_mirrors2)
    def setUp(self):
        # We are inheriting from custom class.
        unittest_toolbox.Modified_TestCase.setUp(self)

        # Copy the original repository files provided in the test folder so that
        # any modifications made to repository files are restricted to the copies.
        # The 'repository_data' directory is expected to exist in 'tuf/tests/'.
        original_repository_files = os.path.join(os.getcwd(),
                                                 'repository_data')
        temporary_repository_root = \
          self.make_temp_directory(directory=self.temporary_directory)

        # The original repository, keystore, and client directories will be copied
        # for each test case.
        original_repository = os.path.join(original_repository_files,
                                           'repository')
        original_client = os.path.join(original_repository_files, 'client')
        original_keystore = os.path.join(original_repository_files, 'keystore')

        # Save references to the often-needed client repository directories.
        # Test cases need these references to access metadata and target files.
        self.repository_directory = \
          os.path.join(temporary_repository_root, 'repository')
        self.client_directory = os.path.join(temporary_repository_root,
                                             'client')
        self.keystore_directory = os.path.join(temporary_repository_root,
                                               'keystore')

        # Copy the original 'repository', 'client', and 'keystore' directories
        # to the temporary repository the test cases can use.
        shutil.copytree(original_repository, self.repository_directory)
        shutil.copytree(original_client, self.client_directory)
        shutil.copytree(original_keystore, self.keystore_directory)

        # The slow retrieval server, in mode 2 (1 byte per second), will only
        # sleep for a  total of (target file size) seconds.  Add a target file
        # that contains sufficient number of bytes to trigger a slow retrieval
        # error.  "sufficient number of bytes" assumed to be
        # >> 'tuf.conf.SLOW_START_GRACE_PERIOD' bytes.
        extra_bytes = 8
        total_bytes = tuf.conf.SLOW_START_GRACE_PERIOD + extra_bytes

        repository = repo_tool.load_repository(self.repository_directory)
        file1_filepath = os.path.join(self.repository_directory, 'targets',
                                      'file1.txt')
        with open(file1_filepath, 'wb') as file_object:
            data = 'a' * total_bytes
            file_object.write(data.encode('utf-8'))

        key_file = os.path.join(self.keystore_directory, 'timestamp_key')
        timestamp_private = repo_tool.import_ed25519_privatekey_from_file(
            key_file, 'password')
        key_file = os.path.join(self.keystore_directory, 'snapshot_key')
        snapshot_private = repo_tool.import_ed25519_privatekey_from_file(
            key_file, 'password')
        key_file = os.path.join(self.keystore_directory, 'targets_key')
        targets_private = repo_tool.import_ed25519_privatekey_from_file(
            key_file, 'password')

        repository.targets.load_signing_key(targets_private)
        repository.snapshot.load_signing_key(snapshot_private)
        repository.timestamp.load_signing_key(timestamp_private)

        repository.write()

        # Move the staged metadata to the "live" metadata.
        shutil.rmtree(os.path.join(self.repository_directory, 'metadata'))
        shutil.copytree(
            os.path.join(self.repository_directory, 'metadata.staged'),
            os.path.join(self.repository_directory, 'metadata'))

        # Set the url prefix required by the 'tuf/client/updater.py' updater.
        # 'path/to/tmp/repository' -> 'localhost:8001/tmp/repository'.
        repository_basepath = self.repository_directory[len(os.getcwd()):]
        url_prefix = \
          'http://localhost:' + str(self.SERVER_PORT) + repository_basepath

        # Setting 'tuf.conf.repository_directory' with the temporary client
        # directory copied from the original repository files.
        tuf.conf.repository_directory = self.client_directory

        # Creating a repository instance.  The test cases will use this client
        # updater to refresh metadata, fetch target files, etc.
        self.repository_updater = updater.Updater('testupdater')

        # Need to override pinned.json mirrors for testing. /:
        # Point it to the right URL with the randomly selected port generated in
        # this test setup.
        mirrors = self.repository_updater.pinned_metadata['repositories'][
            'defaultrepo']['mirrors']

        for i in range(0, len(mirrors)):
            if '<DETERMINED_IN_TEST_SETUP>' in mirrors[i]:
                mirrors[i] = mirrors[i].replace('<DETERMINED_IN_TEST_SETUP>',
                                                str(url_prefix))

        self.repository_updater.pinned_metadata['repositories']['defaultrepo'][
            'mirrors'] = mirrors
from in_toto.models.metadata import Metablock
import os
import shutil
import glob

# we ensure the download always happens by erasing any downloaded stuff
if os.path.exists("in_toto_md"):
    shutil.rmtree("in_toto_md")
os.mkdir("in_toto_md")

tuf.settings.repositories_directory = '.'
client = updater.Updater('client',
                         repository_mirrors={
                             'mirror1': {
                                 'url_prefix': 'http://localhost:8000',
                                 'metadata_path': 'metadata',
                                 'targets_path': 'targets',
                                 'confined_target_dirs': ['']
                             }
                         })

tuf.log.enable_file_logging()
tuf.log.add_console_handler()
tuf.log.set_console_log_level(logging.INFO)

print("setting up TUF client...")
target = 'packages/demo-project.tar.gz'
client.refresh()

print("Downloading target package info: {}".format(target))
package_info = client.get_one_valid_targetinfo(target)