2014-06-20 23:39:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2017-11-30 18:33:11 +00:00
|
|
|
# Copyright 2014 - 2017, New York University and the TUF contributors
|
|
|
|
|
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
"""
|
|
|
|
|
<Program Name>
|
2014-06-30 18:04:01 +00:00
|
|
|
test_developer_tool.py.
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
<Authors>
|
2014-07-02 15:10:50 +00:00
|
|
|
Santiago Torres Arias <torresariass@gmail.com>
|
2014-06-20 23:39:11 +00:00
|
|
|
Zane Fisher <zanefisher@gmail.com>
|
|
|
|
|
|
2017-11-30 18:33:11 +00:00
|
|
|
<Started>
|
|
|
|
|
January 22, 2014.
|
|
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
<Copyright>
|
2018-02-05 16:31:19 +00:00
|
|
|
See LICENSE-MIT OR LICENSE for licensing information.
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
<Purpose>
|
2014-06-30 18:04:01 +00:00
|
|
|
Unit test for the 'developer_tool.py' module.
|
2014-06-20 23:39:11 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import datetime
|
|
|
|
|
import unittest
|
|
|
|
|
import logging
|
|
|
|
|
import tempfile
|
|
|
|
|
import shutil
|
2017-09-21 21:16:29 +00:00
|
|
|
import unittest
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
import tuf
|
|
|
|
|
import tuf.log
|
|
|
|
|
import tuf.roledb
|
2017-01-11 22:05:46 +00:00
|
|
|
import tuf.keydb
|
2014-06-20 23:39:11 +00:00
|
|
|
import tuf.developer_tool as developer_tool
|
2017-01-11 22:05:46 +00:00
|
|
|
import tuf.exceptions
|
|
|
|
|
|
|
|
|
|
import securesystemslib
|
2020-04-22 16:54:27 +00:00
|
|
|
import securesystemslib.exceptions
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
from tuf.developer_tool import METADATA_DIRECTORY_NAME
|
|
|
|
|
from tuf.developer_tool import TARGETS_DIRECTORY_NAME
|
|
|
|
|
|
2020-03-02 20:43:43 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
developer_tool.disable_console_log_messages()
|
|
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
class TestProject(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
tmp_dir = None
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
cls.tmp_dir = tempfile.mkdtemp(dir = os.getcwd())
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def tearDownClass(cls):
|
|
|
|
|
shutil.rmtree(cls.tmp_dir)
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
# called before every test case
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
# called after every test case
|
2016-07-15 19:24:45 +00:00
|
|
|
tuf.roledb.clear_roledb(clear_all=True)
|
2017-01-11 22:05:46 +00:00
|
|
|
tuf.keydb.clear_keydb(clear_all=True)
|
2016-07-15 19:24:45 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
def test_create_new_project(self):
|
2014-06-30 18:04:01 +00:00
|
|
|
# Test cases for the create_new_project function. In this test we will
|
2014-06-20 23:39:11 +00:00
|
|
|
# check input, correct file creation and format. We also check
|
|
|
|
|
# that a proper object is generated. We will use the normal layout for this
|
|
|
|
|
# test suite.
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Create a local subfolder for this test.
|
2014-06-20 23:39:11 +00:00
|
|
|
local_tmp = tempfile.mkdtemp(dir = self.tmp_dir)
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# These are the usual values we will be throwing to the function, however
|
2014-06-20 23:39:11 +00:00
|
|
|
# we will swap these for nulls or malformed values every now and then to
|
2014-06-30 18:04:01 +00:00
|
|
|
# test input.
|
2014-07-02 15:10:50 +00:00
|
|
|
project_name = 'test_suite'
|
2017-01-11 22:05:46 +00:00
|
|
|
metadata_directory = local_tmp
|
2014-06-20 23:39:11 +00:00
|
|
|
location_in_repository = '/prefix'
|
2017-01-11 22:05:46 +00:00
|
|
|
targets_directory = None
|
2014-06-20 23:39:11 +00:00
|
|
|
key = None
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Create a blank project.
|
2017-01-11 22:05:46 +00:00
|
|
|
project = developer_tool.create_new_project(project_name, metadata_directory,
|
2014-06-20 23:39:11 +00:00
|
|
|
location_in_repository)
|
|
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(project, developer_tool.Project))
|
|
|
|
|
self.assertTrue(project.layout_type == 'repo-like')
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertTrue(project.prefix == location_in_repository)
|
|
|
|
|
self.assertTrue(project.project_name == project_name)
|
|
|
|
|
self.assertTrue(project.metadata_directory ==
|
2014-06-20 23:39:11 +00:00
|
|
|
os.path.join(metadata_directory,METADATA_DIRECTORY_NAME))
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertTrue(project.targets_directory ==
|
2014-06-20 23:39:11 +00:00
|
|
|
os.path.join(metadata_directory,TARGETS_DIRECTORY_NAME))
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Create a blank project without a prefix.
|
2014-06-20 23:39:11 +00:00
|
|
|
project = developer_tool.create_new_project(project_name, metadata_directory)
|
|
|
|
|
self.assertTrue(isinstance(project, developer_tool.Project))
|
|
|
|
|
self.assertTrue(project.layout_type == 'repo-like')
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertTrue(project.prefix == '')
|
|
|
|
|
self.assertTrue(project.project_name == project_name)
|
|
|
|
|
self.assertTrue(project.metadata_directory ==
|
2014-06-20 23:39:11 +00:00
|
|
|
os.path.join(metadata_directory,METADATA_DIRECTORY_NAME))
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertTrue(project.targets_directory ==
|
2014-06-20 23:39:11 +00:00
|
|
|
os.path.join(metadata_directory,TARGETS_DIRECTORY_NAME))
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Create a blank project without a valid metadata directory.
|
2017-01-11 22:05:46 +00:00
|
|
|
self.assertRaises(securesystemslib.exceptions.FormatError, developer_tool.create_new_project,
|
|
|
|
|
0, metadata_directory, location_in_repository)
|
|
|
|
|
self.assertRaises(securesystemslib.exceptions.FormatError, developer_tool.create_new_project,
|
|
|
|
|
project_name, 0, location_in_repository)
|
|
|
|
|
self.assertRaises(securesystemslib.exceptions.FormatError, developer_tool.create_new_project,
|
|
|
|
|
project_name, metadata_directory, 0)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Create a new project with a flat layout.
|
2014-06-20 23:39:11 +00:00
|
|
|
targets_directory = tempfile.mkdtemp(dir = local_tmp)
|
|
|
|
|
metadata_directory = tempfile.mkdtemp(dir = local_tmp)
|
|
|
|
|
project = developer_tool.create_new_project(project_name, metadata_directory,
|
|
|
|
|
location_in_repository, targets_directory)
|
|
|
|
|
self.assertTrue(isinstance(project, developer_tool.Project))
|
|
|
|
|
self.assertTrue(project.layout_type == 'flat')
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertTrue(project.prefix == location_in_repository)
|
|
|
|
|
self.assertTrue(project.project_name == project_name)
|
|
|
|
|
self.assertTrue(project.metadata_directory == metadata_directory)
|
|
|
|
|
self.assertTrue(project.targets_directory == targets_directory)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Finally, check that if targets_directory is set, it is valid.
|
2017-01-11 22:05:46 +00:00
|
|
|
self.assertRaises(securesystemslib.exceptions.FormatError, developer_tool.create_new_project,
|
2014-06-20 23:39:11 +00:00
|
|
|
project_name, metadata_directory, location_in_repository, 0)
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Copy a key to our workspace and create a new project with it.
|
2014-06-20 23:39:11 +00:00
|
|
|
keystore_path = os.path.join('repository_data','keystore')
|
|
|
|
|
|
|
|
|
|
# I will use the same key as the one provided in the repository
|
|
|
|
|
# tool tests for the root role, but this is not a root role...
|
|
|
|
|
root_key_path = os.path.join(keystore_path,'root_key.pub')
|
|
|
|
|
project_key = developer_tool.import_rsa_publickey_from_file(root_key_path)
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Test create new project with a key added by default.
|
2014-06-20 23:39:11 +00:00
|
|
|
project = developer_tool.create_new_project(project_name, metadata_directory,
|
|
|
|
|
location_in_repository, targets_directory, project_key)
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
self.assertTrue(isinstance(project, developer_tool.Project))
|
|
|
|
|
self.assertTrue(project.layout_type == 'flat')
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertTrue(project.prefix == location_in_repository)
|
|
|
|
|
self.assertTrue(project.project_name == project_name)
|
|
|
|
|
self.assertTrue(project.metadata_directory == metadata_directory)
|
|
|
|
|
self.assertTrue(project.targets_directory == targets_directory)
|
2014-06-20 23:39:11 +00:00
|
|
|
self.assertTrue(len(project.keys) == 1)
|
|
|
|
|
self.assertTrue(project.keys[0] == project_key['keyid'])
|
|
|
|
|
|
2018-04-27 15:15:14 +00:00
|
|
|
# Try to write to an invalid location. The OSError should be re-raised by
|
|
|
|
|
# create_new_project().
|
2014-06-21 23:07:34 +00:00
|
|
|
shutil.rmtree(targets_directory)
|
|
|
|
|
tuf.roledb.clear_roledb()
|
2017-01-11 22:05:46 +00:00
|
|
|
tuf.keydb.clear_keydb()
|
2018-04-27 15:15:14 +00:00
|
|
|
|
|
|
|
|
metadata_directory = '/'
|
|
|
|
|
valid_metadata_directory_name = developer_tool.METADATA_DIRECTORY_NAME
|
|
|
|
|
developer_tool.METADATA_DIRECTORY_NAME = '/'
|
2018-04-27 17:36:16 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
developer_tool.create_new_project(project_name, metadata_directory,
|
|
|
|
|
location_in_repository, targets_directory, project_key)
|
|
|
|
|
|
2019-04-16 20:48:29 +00:00
|
|
|
except (OSError, tuf.exceptions.RepositoryError):
|
2018-04-27 17:36:16 +00:00
|
|
|
pass
|
|
|
|
|
|
2018-04-27 15:15:14 +00:00
|
|
|
developer_tool.METADATA_DIRECTORY_NAME = valid_metadata_directory_name
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_project(self):
|
2014-06-30 18:04:01 +00:00
|
|
|
# This test case will first try to load an existing project and test for
|
|
|
|
|
# verify the loaded object. It will next try to load a nonexisting project
|
|
|
|
|
# and expect a correct error handler. Finally, it will try to overwrite the
|
|
|
|
|
# existing prefix of the loaded project.
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Create a local subfolder for this test.
|
2014-06-20 23:39:11 +00:00
|
|
|
local_tmp = tempfile.mkdtemp(dir = self.tmp_dir)
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Test non-existent project filepath.
|
2014-07-02 15:10:50 +00:00
|
|
|
nonexistent_path = os.path.join(local_tmp, 'nonexistent')
|
2020-04-22 16:54:27 +00:00
|
|
|
self.assertRaises(securesystemslib.exceptions.StorageError,
|
|
|
|
|
developer_tool.load_project, nonexistent_path)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Copy the pregenerated metadata.
|
2014-06-20 23:39:11 +00:00
|
|
|
project_data_filepath = os.path.join('repository_data', 'project')
|
|
|
|
|
target_project_data_filepath = os.path.join(local_tmp, 'project')
|
2014-07-02 15:10:50 +00:00
|
|
|
shutil.copytree('repository_data/project', target_project_data_filepath)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Properly load a project.
|
2014-06-20 23:39:11 +00:00
|
|
|
repo_filepath = os.path.join(local_tmp, 'project', 'test-flat')
|
2014-06-21 23:07:34 +00:00
|
|
|
new_targets_path = os.path.join(local_tmp, 'project', 'targets')
|
|
|
|
|
project = developer_tool.load_project(repo_filepath,
|
|
|
|
|
new_targets_location = new_targets_path)
|
|
|
|
|
self.assertTrue(project._targets_directory == new_targets_path)
|
2014-06-20 23:39:11 +00:00
|
|
|
self.assertTrue(project.layout_type == 'flat')
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Load a project overwriting the prefix.
|
2014-06-20 23:39:11 +00:00
|
|
|
project = developer_tool.load_project(repo_filepath, prefix='new')
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertTrue(project.prefix == 'new')
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Load a project with a file missing.
|
2016-04-14 20:53:48 +00:00
|
|
|
file_to_corrupt = os.path.join(repo_filepath, 'test-flat.json')
|
2014-06-21 23:07:34 +00:00
|
|
|
with open(file_to_corrupt, 'wt') as fp:
|
2014-07-02 15:10:50 +00:00
|
|
|
fp.write('this is not a json file')
|
2014-06-21 23:07:34 +00:00
|
|
|
|
2017-01-11 22:05:46 +00:00
|
|
|
self.assertRaises(securesystemslib.exceptions.Error, developer_tool.load_project, repo_filepath)
|
|
|
|
|
|
|
|
|
|
|
2014-06-21 23:07:34 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
def test_add_verification_keys(self):
|
2017-01-11 22:05:46 +00:00
|
|
|
# Create a new project instance.
|
2014-07-02 15:10:50 +00:00
|
|
|
project = developer_tool.Project('test_verification_keys', 'somepath',
|
|
|
|
|
'someotherpath', 'prefix')
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add invalid verification key.
|
2017-01-11 22:05:46 +00:00
|
|
|
self.assertRaises(securesystemslib.exceptions.FormatError, project.add_verification_key, 'invalid')
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add verification key.
|
2017-01-11 22:05:46 +00:00
|
|
|
# - load it first
|
2016-04-14 20:53:48 +00:00
|
|
|
keystore_path = os.path.join('repository_data', 'keystore')
|
2014-06-20 23:39:11 +00:00
|
|
|
first_verification_key_path = os.path.join(keystore_path,'root_key.pub')
|
|
|
|
|
first_verification_key = \
|
|
|
|
|
developer_tool.import_rsa_publickey_from_file(first_verification_key_path)
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
project.add_verification_key(first_verification_key)
|
|
|
|
|
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add another verification key (should expect exception.)
|
2016-04-14 20:53:48 +00:00
|
|
|
second_verification_key_path = os.path.join(keystore_path, 'snapshot_key.pub')
|
2014-06-20 23:39:11 +00:00
|
|
|
second_verification_key = \
|
2016-07-01 16:52:04 +00:00
|
|
|
developer_tool.import_ed25519_publickey_from_file(second_verification_key_path)
|
2017-01-11 22:05:46 +00:00
|
|
|
|
|
|
|
|
self.assertRaises(securesystemslib.exceptions.Error,
|
2014-06-20 23:39:11 +00:00
|
|
|
project.add_verification_key,(second_verification_key))
|
|
|
|
|
|
|
|
|
|
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add a verification key for the delegation.
|
|
|
|
|
project.delegate('somedelegation', [], [])
|
|
|
|
|
project('somedelegation').add_verification_key(first_verification_key)
|
|
|
|
|
project('somedelegation').add_verification_key(second_verification_key)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add another delegation of the delegation.
|
|
|
|
|
project('somedelegation').delegate('somesubdelegation', [], [])
|
2016-04-06 00:23:37 +00:00
|
|
|
project('somesubdelegation').add_verification_key(first_verification_key)
|
|
|
|
|
project('somesubdelegation').add_verification_key(second_verification_key)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_write(self):
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Create tmp directory.
|
2014-06-20 23:39:11 +00:00
|
|
|
local_tmp = tempfile.mkdtemp(dir=self.tmp_dir)
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Create new project inside tmp directory.
|
2017-01-11 22:05:46 +00:00
|
|
|
project = developer_tool.create_new_project('new_project', local_tmp,
|
2014-07-02 15:10:50 +00:00
|
|
|
'prefix');
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Create some target files inside the tmp directory.
|
|
|
|
|
target_filepath = os.path.join(local_tmp, 'targets', 'test_target')
|
|
|
|
|
with open(target_filepath, 'wt') as fp:
|
|
|
|
|
fp.write('testing file')
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add the targets.
|
2018-04-06 17:18:33 +00:00
|
|
|
project.add_target(os.path.basename(target_filepath))
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add verification keys.
|
|
|
|
|
keystore_path = os.path.join('repository_data', 'keystore')
|
|
|
|
|
project_key_path = os.path.join(keystore_path, 'root_key.pub')
|
2014-06-20 23:39:11 +00:00
|
|
|
project_key = \
|
|
|
|
|
developer_tool.import_rsa_publickey_from_file(project_key_path)
|
|
|
|
|
|
|
|
|
|
|
2017-01-11 22:05:46 +00:00
|
|
|
# Call status (for the sake of doing it and to improve test coverage by
|
2014-07-02 15:10:50 +00:00
|
|
|
# executing its statements.)
|
2014-06-20 23:39:11 +00:00
|
|
|
project.status()
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
project.add_verification_key(project_key)
|
|
|
|
|
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add another verification key (should expect exception.)
|
|
|
|
|
delegation_key_path = os.path.join(keystore_path, 'snapshot_key.pub')
|
2014-06-20 23:39:11 +00:00
|
|
|
delegation_key = \
|
2016-07-01 16:52:04 +00:00
|
|
|
developer_tool.import_ed25519_publickey_from_file(delegation_key_path)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add a subdelegation.
|
|
|
|
|
subdelegation_key_path = os.path.join(keystore_path, 'timestamp_key.pub')
|
2014-06-20 23:39:11 +00:00
|
|
|
subdelegation_key = \
|
2016-07-01 16:52:04 +00:00
|
|
|
developer_tool.import_ed25519_publickey_from_file(subdelegation_key_path)
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Add a delegation.
|
|
|
|
|
project.delegate('delegation', [delegation_key], [])
|
|
|
|
|
project('delegation').delegate('subdelegation', [subdelegation_key], [])
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
# call write (except)
|
2017-01-11 22:05:46 +00:00
|
|
|
self.assertRaises(securesystemslib.exceptions.Error, project.write, ())
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Call status (for the sake of doing it and executing its statements.)
|
2014-06-20 23:39:11 +00:00
|
|
|
project.status()
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Load private keys.
|
2014-06-20 23:39:11 +00:00
|
|
|
project_private_key_path = os.path.join(keystore_path, 'root_key')
|
|
|
|
|
project_private_key = \
|
|
|
|
|
developer_tool.import_rsa_privatekey_from_file(project_private_key_path,
|
|
|
|
|
'password')
|
|
|
|
|
|
|
|
|
|
delegation_private_key_path = os.path.join(keystore_path, 'snapshot_key')
|
|
|
|
|
delegation_private_key = \
|
2016-07-01 16:52:04 +00:00
|
|
|
developer_tool.import_ed25519_privatekey_from_file(delegation_private_key_path,
|
2014-06-20 23:39:11 +00:00
|
|
|
'password')
|
|
|
|
|
|
|
|
|
|
subdelegation_private_key_path = \
|
|
|
|
|
os.path.join(keystore_path, 'timestamp_key')
|
|
|
|
|
subdelegation_private_key = \
|
2016-07-01 16:52:04 +00:00
|
|
|
developer_tool.import_ed25519_privatekey_from_file(subdelegation_private_key_path,
|
2014-06-20 23:39:11 +00:00
|
|
|
'password')
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# Test partial write.
|
2014-06-20 23:39:11 +00:00
|
|
|
# backup everything (again)
|
2014-07-02 15:10:50 +00:00
|
|
|
# + backup targets.
|
2014-06-20 23:39:11 +00:00
|
|
|
targets_backup = project.target_files
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# + backup delegations.
|
2014-06-20 23:39:11 +00:00
|
|
|
delegations_backup = \
|
2017-11-27 16:38:49 +00:00
|
|
|
tuf.roledb.get_delegated_rolenames(project.project_name)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# + backup layout type.
|
2014-06-20 23:39:11 +00:00
|
|
|
layout_type_backup = project.layout_type
|
|
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
# + backup keyids.
|
2014-06-20 23:39:11 +00:00
|
|
|
keys_backup = project.keys
|
2014-07-02 15:10:50 +00:00
|
|
|
delegation_keys_backup = project('delegation').keys
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# + backup the prefix.
|
2017-11-27 16:38:49 +00:00
|
|
|
prefix_backup = project.prefix
|
2017-01-11 22:05:46 +00:00
|
|
|
|
|
|
|
|
# + backup the name.
|
2017-11-27 16:38:49 +00:00
|
|
|
name_backup = project.project_name
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Write and reload.
|
2017-01-11 22:05:46 +00:00
|
|
|
self.assertRaises(securesystemslib.exceptions.Error, project.write)
|
2014-06-20 23:39:11 +00:00
|
|
|
project.write(write_partial=True)
|
2014-06-21 23:07:34 +00:00
|
|
|
|
2014-06-20 23:39:11 +00:00
|
|
|
project = developer_tool.load_project(local_tmp)
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Check against backup.
|
2014-07-02 15:10:50 +00:00
|
|
|
self.assertEqual(list(project.target_files.keys()), list(targets_backup.keys()))
|
2017-11-27 16:38:49 +00:00
|
|
|
new_delegations = tuf.roledb.get_delegated_rolenames(project.project_name)
|
2014-06-30 18:04:01 +00:00
|
|
|
self.assertEqual(new_delegations, delegations_backup)
|
|
|
|
|
self.assertEqual(project.layout_type, layout_type_backup)
|
|
|
|
|
self.assertEqual(project.keys, keys_backup)
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-07-02 15:10:50 +00:00
|
|
|
self.assertEqual(project('delegation').keys, delegation_keys_backup)
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertEqual(project.prefix, prefix_backup)
|
|
|
|
|
self.assertEqual(project.project_name, name_backup)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2017-11-27 16:38:49 +00:00
|
|
|
roleinfo = tuf.roledb.get_roleinfo(project.project_name)
|
2014-06-21 23:07:34 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
self.assertEqual(roleinfo['partial_loaded'], True)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Load_signing_keys.
|
2014-07-02 15:10:50 +00:00
|
|
|
project('delegation').load_signing_key(delegation_private_key)
|
2014-06-21 23:07:34 +00:00
|
|
|
|
|
|
|
|
project.status()
|
|
|
|
|
|
|
|
|
|
project.load_signing_key(project_private_key)
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Backup everything.
|
|
|
|
|
# + backup targets.
|
2014-06-20 23:39:11 +00:00
|
|
|
targets_backup = project.target_files
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# + backup delegations.
|
2014-06-20 23:39:11 +00:00
|
|
|
delegations_backup = \
|
2017-11-27 16:38:49 +00:00
|
|
|
tuf.roledb.get_delegated_rolenames(project.project_name)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# + backup layout type.
|
2014-06-20 23:39:11 +00:00
|
|
|
layout_type_backup = project.layout_type
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# + backup keyids
|
2014-06-20 23:39:11 +00:00
|
|
|
keys_backup = project.keys
|
2014-07-02 15:10:50 +00:00
|
|
|
delegation_keys_backup = project('delegation').keys
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# + backup the prefix.
|
2017-11-27 16:38:49 +00:00
|
|
|
prefix_backup = project.prefix
|
2017-01-11 22:05:46 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# + backup the name.
|
2017-11-27 16:38:49 +00:00
|
|
|
name_backup = project.project_name
|
2014-06-21 23:07:34 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Call status (for the sake of doing it.)
|
2014-06-21 23:07:34 +00:00
|
|
|
project.status()
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Call write.
|
2014-06-20 23:39:11 +00:00
|
|
|
project.write()
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Call load.
|
2014-06-20 23:39:11 +00:00
|
|
|
project = developer_tool.load_project(local_tmp)
|
|
|
|
|
|
|
|
|
|
|
2014-06-30 18:04:01 +00:00
|
|
|
# Check against backup.
|
2014-07-02 15:10:50 +00:00
|
|
|
self.assertEqual(list(project.target_files.keys()), list(targets_backup.keys()))
|
2014-06-20 23:39:11 +00:00
|
|
|
|
2017-11-27 16:38:49 +00:00
|
|
|
new_delegations = tuf.roledb.get_delegated_rolenames(project.project_name)
|
2014-06-30 18:04:01 +00:00
|
|
|
self.assertEqual(new_delegations, delegations_backup)
|
|
|
|
|
self.assertEqual(project.layout_type, layout_type_backup)
|
|
|
|
|
self.assertEqual(project.keys, keys_backup)
|
2014-07-02 15:10:50 +00:00
|
|
|
self.assertEqual(project('delegation').keys, delegation_keys_backup)
|
2017-11-27 16:38:49 +00:00
|
|
|
self.assertEqual(project.prefix, prefix_backup)
|
|
|
|
|
self.assertEqual(project.project_name, name_backup)
|
2014-06-20 23:39:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|