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_DictOf(self): # Test conditions for valid arguments. dictof_schema = SCHEMA.DictOf( SCHEMA.RegularExpression(r'[aeiou]+'), SCHEMA.Struct([SCHEMA.AnyString(), SCHEMA.AnyString()])) self.assertTrue(dictof_schema.matches({})) self.assertTrue(dictof_schema.matches({ 'a': ['x', 'y'], 'e': ['', ''] })) # Test conditions for invalid arguments. self.assertFalse(dictof_schema.matches('')) self.assertFalse(dictof_schema.matches({'a': ['x', 3], 'e': ['', '']})) self.assertFalse( dictof_schema.matches({ 'a': ['x', 'y'], 'e': ['', ''], 'd': ['a', 'b'] })) # Test conditions for invalid arguments in a schema definition. self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.DictOf, 1, 1) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.DictOf, [1], [1]) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.DictOf, {'a': 1}, 1) self.assertRaises(securesystemslib.exceptions.FormatError, SCHEMA.DictOf, 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])