Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_OneOf(self):
# Test conditions for valid arguments.
oneof_schema = SCHEMA.OneOf([SCHEMA.ListOf(SCHEMA.Integer()),
SCHEMA.String('Hello'), SCHEMA.String('bye')])
self.assertTrue(oneof_schema.matches([]))
self.assertTrue(oneof_schema.matches('bye'))
self.assertTrue(oneof_schema.matches([1,2]))
# Test conditions for invalid arguments.
self.assertFalse(oneof_schema.matches(3))
self.assertFalse(oneof_schema.matches(['Hi']))
# Test conditions for invalid arguments in a schema definition.
self.assertRaises(securesystemslib.exceptions.FormatError,
SCHEMA.OneOf, 1)
self.assertRaises(securesystemslib.exceptions.FormatError,
SCHEMA.OneOf, [1])
self.assertRaises(securesystemslib.exceptions.FormatError,
SCHEMA.OneOf, {'a': 1})
def test_Struct(self):
# Test conditions for valid arguments.
struct_schema = SCHEMA.Struct([SCHEMA.ListOf(SCHEMA.AnyString()),
SCHEMA.AnyString(), SCHEMA.String('X')])
struct2_schema = SCHEMA.Struct([SCHEMA.String('X')], allow_more=True)
struct3_schema = SCHEMA.Struct([SCHEMA.String('X'),
SCHEMA.Integer()], [SCHEMA.Integer()])
self.assertTrue(struct_schema.matches([[], 'Q', 'X']))
self.assertTrue(struct2_schema.matches(['X']))
self.assertTrue(struct2_schema.matches(['X', 'Y']))
self.assertTrue(struct2_schema.matches(['X', ['Y', 'Z']]))
self.assertTrue(struct3_schema.matches(['X', 3]))
self.assertTrue(struct3_schema.matches(['X', 3, 9]))
# Test conditions for invalid arguments.
self.assertFalse(struct_schema.matches(False))
self.assertFalse(struct_schema.matches('Foo'))
self.assertFalse(struct_schema.matches([[], 'Q', 'D']))
def test_AllOf(self):
# Test conditions for valid arguments.
allof_schema = SCHEMA.AllOf([SCHEMA.Any(),
SCHEMA.AnyString(), SCHEMA.String('a')])
self.assertTrue(allof_schema.matches('a'))
# Test conditions for invalid arguments.
self.assertFalse(allof_schema.matches('b'))
# Test conditions for invalid arguments in a schema definition.
self.assertRaises(securesystemslib.exceptions.FormatError,
SCHEMA.AllOf, 1)
self.assertRaises(securesystemslib.exceptions.FormatError,
SCHEMA.AllOf, [1])
self.assertRaises(securesystemslib.exceptions.FormatError,
SCHEMA.AllOf, {'a': 1})
self.assertRaises(securesystemslib.exceptions.FormatError,
SCHEMA.AllOf, [SCHEMA.AnyString(), 1])
)
GPG_RSA_PUBKEY_SCHEMA = _create_gpg_pubkey_with_subkey_schema(
_GPG_RSA_PUBKEY_SCHEMA)
GPG_DSA_PUBKEYVAL_SCHEMA = SCHEMA.Object(
object_name = "GPG_DSA_PUBKEYVAL_SCHEMA",
y = HEX_SCHEMA,
p = HEX_SCHEMA,
q = HEX_SCHEMA,
g = HEX_SCHEMA
)
# C.f. comment above _GPG_RSA_PUBKEY_SCHEMA definition
_GPG_DSA_PUBKEY_SCHEMA = SCHEMA.Object(
object_name = "GPG_DSA_PUBKEY_SCHEMA",
type = SCHEMA.String("dsa"),
method = SCHEMA.String(GPG_DSA_PUBKEY_METHOD_STRING),
hashes = SCHEMA.ListOf(SCHEMA.String(GPG_HASH_ALGORITHM_STRING)),
creation_time = SCHEMA.Optional(UNIX_TIMESTAMP_SCHEMA),
validity_period = SCHEMA.Optional(SCHEMA.Integer(lo=0)),
keyid = KEYID_SCHEMA,
keyval = SCHEMA.Object(
public = GPG_DSA_PUBKEYVAL_SCHEMA,
private = SCHEMA.String("")
)
)
GPG_DSA_PUBKEY_SCHEMA = _create_gpg_pubkey_with_subkey_schema(
_GPG_DSA_PUBKEY_SCHEMA)
GPG_ED25519_PUBKEYVAL_SCHEMA = SCHEMA.Object(
object_name = "GPG_ED25519_PUBKEYVAL_SCHEMA",
)
# We have to define GPG_RSA_PUBKEY_SCHEMA in two steps, because it is
# self-referential. Here we define a shallow _GPG_RSA_PUBKEY_SCHEMA, which we
# use below to create the self-referential GPG_RSA_PUBKEY_SCHEMA.
_GPG_RSA_PUBKEY_SCHEMA = SCHEMA.Object(
object_name = "GPG_RSA_PUBKEY_SCHEMA",
type = SCHEMA.String("rsa"),
method = SCHEMA.String(GPG_RSA_PUBKEY_METHOD_STRING),
hashes = SCHEMA.ListOf(SCHEMA.String(GPG_HASH_ALGORITHM_STRING)),
creation_time = SCHEMA.Optional(UNIX_TIMESTAMP_SCHEMA),
validity_period = SCHEMA.Optional(SCHEMA.Integer(lo=0)),
keyid = KEYID_SCHEMA,
keyval = SCHEMA.Object(
public = GPG_RSA_PUBKEYVAL_SCHEMA,
private = SCHEMA.String("")
)
)
GPG_RSA_PUBKEY_SCHEMA = _create_gpg_pubkey_with_subkey_schema(
_GPG_RSA_PUBKEY_SCHEMA)
GPG_DSA_PUBKEYVAL_SCHEMA = SCHEMA.Object(
object_name = "GPG_DSA_PUBKEYVAL_SCHEMA",
y = HEX_SCHEMA,
p = HEX_SCHEMA,
q = HEX_SCHEMA,
g = HEX_SCHEMA
)
# C.f. comment above _GPG_RSA_PUBKEY_SCHEMA definition
_GPG_DSA_PUBKEY_SCHEMA = SCHEMA.Object(
object_name = "GPG_DSA_PUBKEY_SCHEMA",
# We have to define RSA_PUBKEY_SCHEMA in two steps, because it is
# self-referential. Here we define a shallow _RSA_PUBKEY_SCHEMA, which we use
# below to create the self-referential RSA_PUBKEY_SCHEMA.
_RSA_PUBKEY_SCHEMA = ssl_schema.Object(
object_name = "RSA_PUBKEY_SCHEMA",
type = ssl_schema.String("rsa"),
method = ssl_schema.String(PGP_RSA_PUBKEY_METHOD_STRING),
hashes = ssl_schema.ListOf(ssl_schema.String(GPG_HASH_ALGORITHM_STRING)),
creation_time = ssl_schema.Optional(ssl_formats.UNIX_TIMESTAMP_SCHEMA),
validity_period = ssl_schema.Optional(ssl_schema.Integer(lo=0)),
keyid = ssl_formats.KEYID_SCHEMA,
keyval = ssl_schema.Object(
public = RSA_PUBKEYVAL_SCHEMA,
private = ssl_schema.String("")
)
)
RSA_PUBKEY_SCHEMA = _create_pubkey_with_subkey_schema(
_RSA_PUBKEY_SCHEMA)
DSA_PUBKEYVAL_SCHEMA = ssl_schema.Object(
object_name = "DSA_PUBKEYVAL_SCHEMA",
y = ssl_formats.HEX_SCHEMA,
p = ssl_formats.HEX_SCHEMA,
q = ssl_formats.HEX_SCHEMA,
g = ssl_formats.HEX_SCHEMA
)
# We have to define DSA_PUBKEY_SCHEMA in two steps, because it is
# Must be between 0 and 50.
LOGLEVEL_SCHEMA = SCHEMA.Integer(lo=0, hi=50)
# A string representing a named object.
NAME_SCHEMA = SCHEMA.AnyString()
NAMES_SCHEMA = SCHEMA.ListOf(NAME_SCHEMA)
# A byte string representing data.
DATA_SCHEMA = SCHEMA.AnyBytes()
# A text string. For instance, a string entered by the user.
TEXT_SCHEMA = SCHEMA.AnyString()
# Supported hash algorithms.
HASHALGORITHMS_SCHEMA = SCHEMA.ListOf(SCHEMA.OneOf(
[SCHEMA.String('md5'), SCHEMA.String('sha1'),
SCHEMA.String('sha224'), SCHEMA.String('sha256'),
SCHEMA.String('sha384'), SCHEMA.String('sha512'),
SCHEMA.String('blake2s'), SCHEMA.String('blake2b')]))
# The contents of an encrypted key. Encrypted keys are saved to files
# in this format.
ENCRYPTEDKEY_SCHEMA = SCHEMA.AnyString()
# A value that is either True or False, on or off, etc.
BOOLEAN_SCHEMA = SCHEMA.Boolean()
# The minimum number of bits for an RSA key. Must be 2048 bits, or greater
# (recommended by TUF). Recommended RSA key sizes:
# http://www.emc.com/emc-plus/rsa-labs/historical/twirl-and-rsa-key-size.htm#table1
RSAKEYBITS_SCHEMA = SCHEMA.Integer(lo=2048)
PGP_RSA_PUBKEY_METHOD_STRING = "pgp+rsa-pkcsv1.5"
PGP_DSA_PUBKEY_METHOD_STRING = "pgp+dsa-fips-180-2"
RSA_PUBKEYVAL_SCHEMA = ssl_schema.Object(
object_name = "RSA_PUBKEYVAL_SCHEMA",
e = ssl_schema.AnyString(),
n = ssl_formats.HEX_SCHEMA
)
# We have to define RSA_PUBKEY_SCHEMA in two steps, because it is
# self-referential. Here we define a shallow _RSA_PUBKEY_SCHEMA, which we use
# below to create the self-referential RSA_PUBKEY_SCHEMA.
_RSA_PUBKEY_SCHEMA = ssl_schema.Object(
object_name = "RSA_PUBKEY_SCHEMA",
type = ssl_schema.String("rsa"),
method = ssl_schema.String(PGP_RSA_PUBKEY_METHOD_STRING),
hashes = ssl_schema.ListOf(ssl_schema.String(GPG_HASH_ALGORITHM_STRING)),
creation_time = ssl_schema.Optional(ssl_formats.UNIX_TIMESTAMP_SCHEMA),
validity_period = ssl_schema.Optional(ssl_schema.Integer(lo=0)),
keyid = ssl_formats.KEYID_SCHEMA,
keyval = ssl_schema.Object(
public = RSA_PUBKEYVAL_SCHEMA,
private = ssl_schema.String("")
)
)
RSA_PUBKEY_SCHEMA = _create_pubkey_with_subkey_schema(
_RSA_PUBKEY_SCHEMA)
DSA_PUBKEYVAL_SCHEMA = ssl_schema.Object(
object_name = "DSA_PUBKEYVAL_SCHEMA",
DSA_PUBKEYVAL_SCHEMA = ssl_schema.Object(
object_name = "DSA_PUBKEYVAL_SCHEMA",
y = ssl_formats.HEX_SCHEMA,
p = ssl_formats.HEX_SCHEMA,
q = ssl_formats.HEX_SCHEMA,
g = ssl_formats.HEX_SCHEMA
)
# We have to define DSA_PUBKEY_SCHEMA in two steps, because it is
# self-referential. Here we define a shallow _DSA_PUBKEY_SCHEMA, which we use
# below to create the self-referential DSA_PUBKEY_SCHEMA.
_DSA_PUBKEY_SCHEMA = ssl_schema.Object(
object_name = "DSA_PUBKEY_SCHEMA",
type = ssl_schema.String("dsa"),
method = ssl_schema.String(PGP_DSA_PUBKEY_METHOD_STRING),
hashes = ssl_schema.ListOf(ssl_schema.String(GPG_HASH_ALGORITHM_STRING)),
keyid = ssl_formats.KEYID_SCHEMA,
keyval = ssl_schema.Object(
public = DSA_PUBKEYVAL_SCHEMA,
private = ssl_schema.String("")
)
)
DSA_PUBKEY_SCHEMA = _create_pubkey_with_subkey_schema(
_DSA_PUBKEY_SCHEMA)
PUBKEY_SCHEMA = ssl_schema.OneOf([RSA_PUBKEY_SCHEMA,
DSA_PUBKEY_SCHEMA])
SIGNATURE_SCHEMA = ssl_schema.Object(