def test_Object(self): # Test conditions for valid arguments. object_schema = SCHEMA.Object(a=SCHEMA.AnyString(), bc=SCHEMA.Struct( [SCHEMA.Integer(), SCHEMA.Integer()])) self.assertTrue(object_schema.matches({'a': 'ZYYY', 'bc': [5, 9]})) self.assertTrue( object_schema.matches({ 'a': 'ZYYY', 'bc': [5, 9], 'xx': 5 })) # Test conditions for invalid arguments. self.assertFalse(object_schema.matches({'a': 'ZYYY', 'bc': [5, 9, 3]})) self.assertFalse(object_schema.matches({'a': 'ZYYY'})) # Test conditions for invalid arguments in a schema definition. self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.Object, a='a') self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.Object, a=[1]) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.Object, a=SCHEMA.AnyString(), b=1) # Test condition for invalid non-dict arguments. self.assertFalse(object_schema.matches([{'a': 'XYZ'}])) self.assertFalse(object_schema.matches(8))
def test_Integer(self): # Test conditions for valid arguments. integer_schema = SCHEMA.Integer() self.assertTrue(integer_schema.matches(99)) self.assertTrue(SCHEMA.Integer(lo=10, hi=30).matches(25)) # Test conditions for invalid arguments. self.assertFalse(integer_schema.matches(False)) self.assertFalse(integer_schema.matches('a string')) self.assertFalse(SCHEMA.Integer(lo=10, hi=30).matches(5))
def test_ListOf(self): # Test conditions for valid arguments. listof_schema = SCHEMA.ListOf(SCHEMA.RegularExpression('(?:..)*')) listof2_schema = SCHEMA.ListOf(SCHEMA.Integer(), min_count=3, max_count=10) self.assertTrue(listof_schema.matches([])) self.assertTrue( listof_schema.matches( ['Hi', 'this', 'list', 'is', 'full', 'of', 'even', 'strs'])) self.assertTrue(listof2_schema.matches([3] * 3)) self.assertTrue(listof2_schema.matches([3] * 10)) # Test conditions for invalid arguments. self.assertFalse(listof_schema.matches('hi')) self.assertFalse(listof_schema.matches({})) self.assertFalse(listof_schema.matches(['This', 'one', 'is not'])) self.assertFalse(listof2_schema.matches([3] * 2)) self.assertFalse(listof2_schema.matches(([3] * 11))) # Test conditions for invalid arguments in a schema definition. self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.ListOf, 1) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.ListOf, [1]) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.ListOf, {'a': 1})
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}) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.OneOf, [SCHEMA.AnyString(), 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'])) self.assertFalse(struct_schema.matches([[3], 'Q', 'X'])) self.assertFalse(struct_schema.matches([[], 'Q', 'X', 'Y'])) self.assertFalse(struct2_schema.matches([])) self.assertFalse(struct2_schema.matches([['X']])) self.assertFalse(struct3_schema.matches([])) self.assertFalse(struct3_schema.matches({})) self.assertFalse(struct3_schema.matches(['X'])) self.assertFalse(struct3_schema.matches(['X', 3, 9, 11])) self.assertFalse(struct3_schema.matches(['X', 3, 'A'])) # Test conditions for invalid arguments in a schema definition. self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.Struct, 1) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.Struct, [1]) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.Struct, {'a': 1}) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.Struct, [SCHEMA.AnyString(), 1])
ANY_STRING_SCHEMA = SCHEMA.AnyString() LIST_OF_ANY_STRING_SCHEMA = SCHEMA.ListOf(ANY_STRING_SCHEMA) # A datetime in 'YYYY-MM-DDTHH:MM:SSZ' ISO 8601 format. The "Z" zone designator # for the zero UTC offset is always used (i.e., a numerical offset is not # supported.) Example: '2015-10-21T13:20:00Z'. Note: This is a simple format # check, and an ISO8601 string should be fully verified when it is parsed. ISO8601_DATETIME_SCHEMA = SCHEMA.RegularExpression( r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z') # A Unix/POSIX time format. An integer representing the number of seconds # since the epoch (January 1, 1970.) Metadata uses this format for the # 'expires' field. Set 'hi' to the upper timestamp limit (year 2038), the max # value of an int. UNIX_TIMESTAMP_SCHEMA = SCHEMA.Integer(lo=0, hi=2147483647) # A hexadecimal value in '23432df87ab..' format. HEX_SCHEMA = SCHEMA.RegularExpression(r'[a-fA-F0-9]+') HASH_SCHEMA = HEX_SCHEMA # A dict in {'sha256': '23432df87ab..', 'sha512': '34324abc34df..', ...} format. HASHDICT_SCHEMA = SCHEMA.DictOf(key_schema=SCHEMA.AnyString(), value_schema=HASH_SCHEMA) # Uniform Resource Locator identifier (e.g., 'https://www.updateframework.com/'). # TODO: Some level of restriction here would be good.... Note that I pulled # this from securesystemslib, since it's neither sophisticated nor used # by anyone else. URL_SCHEMA = SCHEMA.AnyString()
SPECIFICATION_VERSION_SCHEMA = SCHEMA.OneOf([ # However, temporarily allow "1.0" for backwards-compatibility in tuf-0.12.PATCH. SCHEMA.String("1.0"), SEMVER_2_0_0_SCHEMA ]) # A datetime in 'YYYY-MM-DDTHH:MM:SSZ' ISO 8601 format. The "Z" zone designator # for the zero UTC offset is always used (i.e., a numerical offset is not # supported.) Example: '2015-10-21T13:20:00Z'. Note: This is a simple format # check, and an ISO8601 string should be fully verified when it is parsed. ISO8601_DATETIME_SCHEMA = SCHEMA.RegularExpression( r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z') # An integer representing the numbered version of a metadata file. # Must be 1, or greater. METADATAVERSION_SCHEMA = SCHEMA.Integer(lo=0) # A relative file path (e.g., 'metadata/root/'). RELPATH_SCHEMA = SCHEMA.AnyString() RELPATHS_SCHEMA = SCHEMA.ListOf(RELPATH_SCHEMA) VERSIONINFO_SCHEMA = SCHEMA.Object(object_name='VERSIONINFO_SCHEMA', version=METADATAVERSION_SCHEMA) # A string representing a role's name. ROLENAME_SCHEMA = SCHEMA.AnyString() # A role's threshold value (i.e., the minimum number # of signatures required to sign a metadata file). # Must be 1 and greater. THRESHOLD_SCHEMA = SCHEMA.Integer(lo=1)
# Note that in the schema definitions below, the 'SCHEMA.Object' types allow # additional keys which are not defined. Thus, any additions to them will be # easily backwards compatible with clients that are already deployed. # A datetime in 'YYYY-MM-DDTHH:MM:SSZ' ISO 8601 format. The "Z" zone designator # for the zero UTC offset is always used (i.e., a numerical offset is not # supported.) Example: '2015-10-21T13:20:00Z'. Note: This is a simple format # check, and an ISO8601 string should be fully verified when it is parsed. ISO8601_DATETIME_SCHEMA = SCHEMA.RegularExpression( r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z') # A Unix/POSIX time format. An integer representing the number of seconds # since the epoch (January 1, 1970.) Metadata uses this format for the # 'expires' field. Set 'hi' to the upper timestamp limit (year 2038), the max # value of an int. UNIX_TIMESTAMP_SCHEMA = SCHEMA.Integer(lo=0, hi=2147483647) # A hexadecimal value in '23432df87ab..' format. HASH_SCHEMA = SCHEMA.RegularExpression(r'[a-fA-F0-9]+') # A dict in {'sha256': '23432df87ab..', 'sha512': '34324abc34df..', ...} format. HASHDICT_SCHEMA = SCHEMA.DictOf(key_schema=SCHEMA.AnyString(), value_schema=HASH_SCHEMA) # A hexadecimal value in '23432df87ab..' format. HEX_SCHEMA = SCHEMA.RegularExpression(r'[a-fA-F0-9]+') # A key identifier (e.g., a hexadecimal value identifying an RSA key). KEYID_SCHEMA = HASH_SCHEMA # A list of KEYID_SCHEMA.