Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub-PR of 846: Modification of Updater and tests as part of #660 #868

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
logger = logging.getLogger('tuf.test_updater')
repo_tool.disable_console_log_messages()

# Load the current specification version at import time in order to reset it
# after each test. At least one test modifies tuf.SPECIFICATION_VERSION, and
# even if it fails, we want to make sure that the spec version is correct at
# the start of the next test.
UNMODIFIED_SPEC_VERSION = tuf.SPECIFICATION_VERSION

class TestUpdater(unittest_toolbox.Modified_TestCase):

Expand Down Expand Up @@ -137,6 +142,11 @@ def tearDownClass(cls):
def setUp(self):
# We are inheriting from custom class.
unittest_toolbox.Modified_TestCase.setUp(self)

# Make sure that the tuf.SPECIFICATION_VERSION is correct, in case a prior
# test has modified it.
tuf.SPECIFICATION_VERSION = UNMODIFIED_SPEC_VERSION

tuf.roledb.clear_roledb(clear_all=True)
tuf.keydb.clear_keydb(clear_all=True)

Expand Down Expand Up @@ -346,7 +356,7 @@ def test_1__rebuild_key_and_role_db(self):
root_threshold = root_metadata['roles']['root']['threshold']
number_of_root_keys = len(root_metadata['keys'])

self.assertEqual(root_roleinfo['threshold'], root_threshold)
self.assertEqual(root_roleinfo['roles']['root']['threshold'], root_threshold)

# Ensure we add 2 to the number of root keys (actually, the number of root
# keys multiplied by the number of keyid hash algorithms), to include the
Expand All @@ -360,7 +370,7 @@ def test_1__rebuild_key_and_role_db(self):
self.repository_updater._rebuild_key_and_role_db()

root_roleinfo = tuf.roledb.get_roleinfo('root', self.repository_name)
self.assertEqual(root_roleinfo['threshold'], root_threshold)
self.assertEqual(root_roleinfo['roles']['root']['threshold'], root_threshold)

# _rebuild_key_and_role_db() will only rebuild the keys and roles specified
# in the 'root.json' file, unlike __init__(). Instantiating an updater
Expand All @@ -375,7 +385,7 @@ def test_1__rebuild_key_and_role_db(self):
self.repository_updater._rebuild_key_and_role_db()

root_roleinfo = tuf.roledb.get_roleinfo('root', self.repository_name)
self.assertEqual(root_roleinfo['threshold'], 8)
self.assertEqual(root_roleinfo['roles']['root']['threshold'], 8)
self.assertEqual(number_of_root_keys * 2 - 2, len(tuf.keydb._keydb_dict[self.repository_name]))


Expand All @@ -392,7 +402,7 @@ def test_1__update_versioninfo(self):
# populates the 'self.versioninfo' dictionary.
self.repository_updater._update_versioninfo('targets.json')
self.assertEqual(len(versioninfo_dict), 1)
self.assertTrue(tuf.formats.FILEINFODICT_SCHEMA.matches(versioninfo_dict))
self.assertTrue(tuf.formats.FILEINFO_DICT_SCHEMA.matches(versioninfo_dict))

# The Snapshot role stores the version numbers of all the roles available
# on the repository. Load Snapshot to extract Root's version number
Expand Down Expand Up @@ -437,7 +447,7 @@ def test_1__update_fileinfo(self):
# 'self.fileinfo' dictionary.
self.repository_updater._update_fileinfo('root.json')
self.assertEqual(len(fileinfo_dict), 1)
self.assertTrue(tuf.formats.FILEDICT_SCHEMA.matches(fileinfo_dict))
self.assertTrue(tuf.formats.FILEINFO_DICT_SCHEMA.matches(fileinfo_dict))
root_filepath = os.path.join(self.client_metadata_current, 'root.json')
length, hashes = securesystemslib.util.get_file_details(root_filepath)
root_fileinfo = tuf.formats.make_fileinfo(length, hashes)
Expand Down Expand Up @@ -509,7 +519,8 @@ def test_2__import_delegations(self):

self.repository_updater._rebuild_key_and_role_db()

self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 4)
# self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 4)
self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 1)

# Take into account the number of keyids algorithms supported by default,
# which this test condition expects to be two (sha256 and sha512).
Expand All @@ -520,15 +531,17 @@ def test_2__import_delegations(self):

# Verify that there was no change to the roledb and keydb dictionaries by
# checking the number of elements in the dictionaries.
self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 4)
# self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 4)
self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 1)
# Take into account the number of keyid hash algorithms, which this
# test condition expects to be two (for sha256 and sha512).
self.assertEqual(len(tuf.keydb._keydb_dict[repository_name]), 4 * 2)

# Test: normal case, first level delegation.
self.repository_updater._import_delegations('targets')

self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 5)
# self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 5)
self.assertEqual(len(tuf.roledb._roledb_dict[repository_name]), 2)
# The number of root keys (times the number of key hash algorithms) +
# delegation's key (+1 for its sha512 keyid).
self.assertEqual(len(tuf.keydb._keydb_dict[repository_name]), 4 * 2 + 2)
Expand Down Expand Up @@ -575,7 +588,8 @@ def test_2__import_delegations(self):
# delegated roles is malformed.
self.repository_updater.metadata['current']['targets']\
['delegations']['roles'][0]['name'] = 1
self.assertRaises(securesystemslib.exceptions.FormatError, self.repository_updater._import_delegations, 'targets')
#TODO this should not be raising a TypeError and must be handled separately?
self.assertRaises(TypeError, self.repository_updater._import_delegations, 'targets')



Expand Down Expand Up @@ -686,6 +700,7 @@ def test_3__update_metadata(self):

# Test: normal case.
# Verify 'timestamp.json' is properly installed.
# TODO fix assertion -> should check current
self.assertFalse('timestamp' in self.repository_updater.metadata)

logger.info('\nroleinfo: ' + repr(tuf.roledb.get_rolenames(self.repository_name)))
Expand Down Expand Up @@ -745,17 +760,15 @@ def test_3__update_metadata(self):



@unittest.expectedFailure
def test_3__get_metadata_file(self):

'''
This test focuses on making sure that the updater rejects unknown or
badly-formatted TUF specification version numbers....
'''

# Make note of the correct supported TUF specification version.
correct_specification_version = tuf.SPECIFICATION_VERSION

# Change it long enough to write new metadata.
# Change the TUF specification version long enough to write new metadata.
tuf.SPECIFICATION_VERSION = '9.0'

repository = repo_tool.load_repository(self.repository_directory)
Expand All @@ -771,7 +784,7 @@ def test_3__get_metadata_file(self):
# Change the supported TUF specification version back to what it should be
# so that we can parse the metadata and see that the spec version in the
# metadata does not match the code's expected spec version.
tuf.SPECIFICATION_VERSION = correct_specification_version
tuf.SPECIFICATION_VERSION = UNMODIFIED_SPEC_VERSION

upperbound_filelength = tuf.settings.DEFAULT_TIMESTAMP_REQUIRED_LENGTH
try:
Expand Down Expand Up @@ -807,7 +820,7 @@ def test_3__get_metadata_file(self):
# Change the supported TUF specification version back to what it should be,
# so that code expects the correct specification version, and gets nonsense
# instead.
tuf.SPECIFICATION_VERSION = correct_specification_version
tuf.SPECIFICATION_VERSION = UNMODIFIED_SPEC_VERSION

try:
self.repository_updater._get_metadata_file('timestamp', 'timestamp.json',
Expand All @@ -823,11 +836,6 @@ def test_3__get_metadata_file(self):
'specification version number that was not in the correct format. '
'No error was raised.')

# REDUNDANTLY reset the specification version the code thinks it supports
# as the last step in this test, in case future changes to the tests above
# neglect to reset it above....
tuf.SPECIFICATION_VERSION = correct_specification_version




Expand Down Expand Up @@ -899,7 +907,7 @@ def test_3__targets_of_role(self):

# Verify that the list of targets was returned, and that it contains valid
# target files.
self.assertTrue(tuf.formats.TARGETINFOS_SCHEMA.matches(targetinfos_list))
self.assertTrue(tuf.formats.LABELED_FILEINFO_SCHEMA.matches(targetinfos_list))
for targetinfo in targetinfos_list:
self.assertTrue((targetinfo['filepath'], targetinfo['fileinfo']) in six.iteritems(targets_in_metadata))

Expand Down Expand Up @@ -1010,8 +1018,8 @@ def test_5_all_targets(self):
all_targets = self.repository_updater.all_targets()

# Verify format of 'all_targets', it should correspond to
# 'TARGETINFOS_SCHEMA'.
self.assertTrue(tuf.formats.TARGETINFOS_SCHEMA.matches(all_targets))
# 'LABELED_FILEINFO_SCHEMA'.
self.assertTrue(tuf.formats.LABELED_FILEINFO_SCHEMA.matches(all_targets))

# Verify that there is a correct number of records in 'all_targets' list,
# and the expected filepaths specified in the metadata. On the targets
Expand Down Expand Up @@ -1062,7 +1070,7 @@ def test_5_targets_of_role(self):

# Verify that list of targets was returned and that it contains valid
# target files.
self.assertTrue(tuf.formats.TARGETINFOS_SCHEMA.matches(targetinfos))
self.assertTrue(tuf.formats.LABELED_FILEINFO_SCHEMA.matches(targetinfos))
for targetinfo in targetinfos:
self.assertTrue((targetinfo['filepath'], targetinfo['fileinfo']) in six.iteritems(expected_targets))

Expand Down Expand Up @@ -1120,7 +1128,22 @@ def test_6_get_one_valid_targetinfo(self):
target_files[filepath] = fileinfo

target_targetinfo = self.repository_updater.get_one_valid_targetinfo(filepath)
self.assertTrue(tuf.formats.TARGETINFO_SCHEMA.matches(target_targetinfo))
self.assertTrue(tuf.formats.LABELED_FILEINFO_SCHEMA.matches(target_targetinfo))
self.assertEqual(target_targetinfo['filepath'], filepath)
self.assertEqual(target_targetinfo['fileinfo'], fileinfo)

# NOTE: this part only exists to verify that get_one_valid_targetinfo works
# fine for non top-level metadata
filepath = "file3.txt"
fileinfo = {
'hashes': {
'sha256': '141f740f53781d1ca54b8a50af22cbf74e44c21a998fa2a8a05aaac2c002886b',
'sha512': 'ef5beafa16041bcdd2937140afebd485296cd54f7348ecd5a4d035c09759608de467a7ac0eb58753d0242df873c305e8bffad2454aa48f44480f15efae1cacd0'
},
'length': 28
}
target_targetinfo = self.repository_updater.get_one_valid_targetinfo(filepath)
self.assertTrue(tuf.formats.LABELED_FILEINFO_SCHEMA.matches(target_targetinfo))
self.assertEqual(target_targetinfo['filepath'], filepath)
self.assertEqual(target_targetinfo['fileinfo'], fileinfo)

Expand Down
Loading