Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with open(invalid_metadata_file, 'w') as file_object:
file_object.write('bad extension on metadata file')
root_filename = 'root'
targets_filename = 'targets'
snapshot_metadata = \
repo_lib.generate_snapshot_metadata(metadata_directory, version,
expiration_date, root_filename,
targets_filename,
consistent_snapshot=False)
self.assertTrue(tuf.formats.SNAPSHOT_SCHEMA.matches(snapshot_metadata))
# Test improperly formatted arguments.
self.assertRaises(securesystemslib.exceptions.FormatError, repo_lib.generate_snapshot_metadata,
3, version, expiration_date,
root_filename, targets_filename, consistent_snapshot=False)
self.assertRaises(securesystemslib.exceptions.FormatError, repo_lib.generate_snapshot_metadata,
metadata_directory, '3', expiration_date,
root_filename, targets_filename, consistent_snapshot=False)
self.assertRaises(securesystemslib.exceptions.FormatError, repo_lib.generate_snapshot_metadata,
metadata_directory, version, '3',
root_filename, targets_filename, consistent_snapshot=False)
self.assertRaises(securesystemslib.exceptions.FormatError, repo_lib.generate_snapshot_metadata,
metadata_directory, version, expiration_date,
3, targets_filename, consistent_snapshot=False)
self.assertRaises(securesystemslib.exceptions.FormatError, repo_lib.generate_snapshot_metadata,
metadata_directory, version, expiration_date,
root_filename, 3, consistent_snapshot=False)
self.assertRaises(securesystemslib.exceptions.FormatError, repo_lib.generate_snapshot_metadata,
metadata_directory, version, expiration_date,
# KEYID is used as the filename. The key is saved to the current working
# directory.
default_keypath = interface.generate_and_write_rsa_keypair(password='pw')
self.assertTrue(os.path.exists(default_keypath))
self.assertTrue(os.path.exists(default_keypath + '.pub'))
written_key = interface.import_rsa_publickey_from_file(default_keypath + '.pub')
self.assertEqual(written_key['keyid'], os.path.basename(default_keypath))
os.remove(default_keypath)
os.remove(default_keypath + '.pub')
# Test improperly formatted arguments.
self.assertRaises(securesystemslib.exceptions.FormatError,
interface.generate_and_write_rsa_keypair, 3, bits=2048, password='pw')
self.assertRaises(securesystemslib.exceptions.FormatError,
interface.generate_and_write_rsa_keypair, test_keypath, bits='bad',
password='pw')
self.assertRaises(securesystemslib.exceptions.FormatError,
interface.generate_and_write_rsa_keypair, test_keypath, bits=2048,
password=3)
# Test invalid 'bits' argument.
self.assertRaises(securesystemslib.exceptions.FormatError,
interface.generate_and_write_rsa_keypair, test_keypath, bits=1024,
password='pw')
def test_set_console_log_level(self):
# Test setting a console log level without first adding one.
self.assertRaises(securesystemslib.exceptions.Error, tuf.log.set_console_log_level)
# Normal case. Default log level. Setting the console log level first
# requires adding a console logger.
tuf.log.add_console_handler()
tuf.log.set_console_log_level()
# Expected log levels.
for level in log_levels:
tuf.log.set_console_log_level(level)
# Test for improperly formatted argument.
self.assertRaises(securesystemslib.exceptions.FormatError, tuf.log.set_console_log_level, '123')
# Test for invalid argument.
self.assertRaises(securesystemslib.exceptions.FormatError, tuf.log.set_console_log_level, 51)
def test_unpack_rule_not_list(self):
"""Test rule syntax error, not a list. """
rule = "CREATE stuff"
with self.assertRaises(securesystemslib.exceptions.FormatError):
unpack_rule(rule)
def _validate_type(self):
"""Private method to check that `_type` is set to "link"."""
if self._type != "link":
raise securesystemslib.exceptions.FormatError(
"Invalid Link: field `_type` must be set to 'link', got: {}"
.format(self._type))
raise securesystemslib.exceptions.FormatError("'{0}' is not a valid "
"'type'. Rule type must be one of: {1} (case insensitive)."
.format(rule_type, ", ".join(ALL_RULES)))
if rule_type.upper() == "MATCH":
if (not securesystemslib.formats.ANY_STRING_SCHEMA.matches(dest_type) or
not (dest_type.lower() == "materials" or
dest_type.lower() == "products")):
raise securesystemslib.exceptions.FormatError("'{}' is not a valid"
" 'dest_type'. Rules of type 'MATCH' require a destination type of"
" either 'MATERIALS' or 'PRODUCTS' (case insensitive)."
.format(dest_type))
if not (securesystemslib.formats.ANY_STRING_SCHEMA.matches(dest_name) and
dest_name):
raise securesystemslib.exceptions.FormatError("'{}' is not a valid"
" 'dest_name'. Rules of type 'MATCH' require a step name as a"
" destination name.".format(dest_name))
# Construct rule
rule = ["MATCH", pattern]
if source_prefix:
securesystemslib.formats.ANY_STRING_SCHEMA.check_match(source_prefix)
rule += ["IN", source_prefix]
rule += ["WITH", dest_type.upper()]
if dest_prefix:
securesystemslib.formats.ANY_STRING_SCHEMA.check_match(dest_prefix)
rule += ["IN", dest_prefix]
A string representing the signing role (e.g., 'root', 'targets').
The role string is returned with characters all lower case.
"""
# Does 'signable' have the correct type?
# This check ensures 'signable' conforms to
# 'SIGNABLE_SCHEMA'.
SIGNABLE_SCHEMA.check_match(signable)
try:
role_type = signable['signed']['_type']
except (KeyError, TypeError):
raise securesystemslib.exceptions.FormatError('Untyped signable object.')
try:
schema = SCHEMAS_BY_TYPE[role_type]
except KeyError:
raise securesystemslib.exceptions.FormatError('Unrecognized'
' type ' + repr(role_type))
# 'securesystemslib.exceptions.FormatError' raised if 'signable' does not
# have a properly formatted role schema.
schema.check_match(signable['signed'])
return role_type.lower()
def _validate_type(self):
"""Private method to ensure that the type field is set to inspection."""
if self._type != "inspection":
raise securesystemslib.exceptions.FormatError(
"The _type field must be set to 'inspection'!")
def _validate_products(self):
"""Private method to check that `products` is a `dict` of `HASHDICTs`."""
if not isinstance(self.products, dict):
raise securesystemslib.exceptions.FormatError(
"Invalid Link: field `products` must be of type dict, got: {}"
.format(type(self.products)))
for product in self.products.values():
securesystemslib.formats.HASHDICT_SCHEMA.check_match(product)
def check_match(self, object):
# Simply return as soon as we find a match.
# Raise 'exceptions.FormatError' if no matches are found.
for alternative in self._alternatives:
if alternative.matches(object):
return
raise securesystemslib.exceptions.FormatError('Object did not match a'
' recognized alternative.')