valid_anytype didn't work as it should (caught by Lorenzo), this should fix it
This commit is contained in:
@@ -11,7 +11,9 @@ class NotValid(Exception):
|
|||||||
|
|
||||||
class OutsideCardinality(Exception):
|
class OutsideCardinality(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# --------------------- validators -------------------------------------
|
||||||
|
#
|
||||||
def valid_ncname(name):
|
def valid_ncname(name):
|
||||||
exp = re.compile("(?P<NCName>[a-zA-Z_](\w|[_.-])*)")
|
exp = re.compile("(?P<NCName>[a-zA-Z_](\w|[_.-])*)")
|
||||||
match = exp.match(name)
|
match = exp.match(name)
|
||||||
@@ -163,6 +165,9 @@ def valid_unsigned_short(val):
|
|||||||
struct.pack("H", int(val))
|
struct.pack("H", int(val))
|
||||||
except struct.error:
|
except struct.error:
|
||||||
raise NotValid("unsigned short")
|
raise NotValid("unsigned short")
|
||||||
|
except ValueError:
|
||||||
|
raise NotValid("unsigned short")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def valid_non_negative_integer(val):
|
def valid_non_negative_integer(val):
|
||||||
@@ -190,7 +195,7 @@ def valid_base64(val):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def valid_qname(val):
|
def valid_qname(val):
|
||||||
""" either
|
""" A qname is either
|
||||||
NCName or
|
NCName or
|
||||||
NCName ':' NCName
|
NCName ':' NCName
|
||||||
"""
|
"""
|
||||||
@@ -202,10 +207,17 @@ def valid_qname(val):
|
|||||||
return valid_ncname(val)
|
return valid_ncname(val)
|
||||||
|
|
||||||
def valid_anytype(val):
|
def valid_anytype(val):
|
||||||
# Should I go through and check all known types ???
|
""" Goes through all known type validators
|
||||||
|
|
||||||
|
:param val: The value to validate
|
||||||
|
:return: True is value is valid otherwise an exception is raised
|
||||||
|
"""
|
||||||
for validator in VALIDATOR.values():
|
for validator in VALIDATOR.values():
|
||||||
if validator(val):
|
try:
|
||||||
return True
|
if validator(val):
|
||||||
|
return True
|
||||||
|
except NotValid:
|
||||||
|
pass
|
||||||
|
|
||||||
if isinstance(val, type):
|
if isinstance(val, type):
|
||||||
return True
|
return True
|
||||||
@@ -219,7 +231,6 @@ VALIDATOR = {
|
|||||||
"NCName": valid_ncname,
|
"NCName": valid_ncname,
|
||||||
"dateTime": valid_date_time,
|
"dateTime": valid_date_time,
|
||||||
"anyURI": valid_any_uri,
|
"anyURI": valid_any_uri,
|
||||||
"string": valid_string,
|
|
||||||
"nonNegativeInteger": valid_non_negative_integer,
|
"nonNegativeInteger": valid_non_negative_integer,
|
||||||
"boolean": valid_boolean,
|
"boolean": valid_boolean,
|
||||||
"unsignedShort": valid_unsigned_short,
|
"unsignedShort": valid_unsigned_short,
|
||||||
@@ -228,6 +239,7 @@ VALIDATOR = {
|
|||||||
"integer": valid_integer,
|
"integer": valid_integer,
|
||||||
"QName": valid_qname,
|
"QName": valid_qname,
|
||||||
"anyType": valid_anytype,
|
"anyType": valid_anytype,
|
||||||
|
"string": valid_string,
|
||||||
}
|
}
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from saml2.validate import valid_string
|
|||||||
from saml2.validate import valid_instance
|
from saml2.validate import valid_instance
|
||||||
from saml2.validate import valid_any_uri
|
from saml2.validate import valid_any_uri
|
||||||
from saml2.validate import NotValid
|
from saml2.validate import NotValid
|
||||||
|
from saml2.validate import valid_anytype
|
||||||
|
|
||||||
from py.test import raises
|
from py.test import raises
|
||||||
|
|
||||||
@@ -94,4 +95,13 @@ def test_valid_instance():
|
|||||||
response.assertion.append(saml.Assertion())
|
response.assertion.append(saml.Assertion())
|
||||||
|
|
||||||
raises( NotValid, 'valid_instance(response)')
|
raises( NotValid, 'valid_instance(response)')
|
||||||
|
|
||||||
|
def test_valid_anytype():
|
||||||
|
assert valid_anytype("130.239.16.3")
|
||||||
|
assert valid_anytype("textstring")
|
||||||
|
assert valid_anytype("12345678")
|
||||||
|
assert valid_anytype("-1234")
|
||||||
|
assert valid_anytype("P1Y2M3DT10H30M")
|
||||||
|
assert valid_anytype("urn:oasis:names:tc:SAML:2.0:attrname-format:uri")
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user