Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

securesystemslib: Add AnyNonemptyString, use for PATH_SCHEMA #244

Merged
2 changes: 1 addition & 1 deletion securesystemslib/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
SCHEME_SCHEMA = SCHEMA.AnyString()

# A path string, whether relative or absolute, e.g. 'metadata/root/'
PATH_SCHEMA = SCHEMA.AnyString()
PATH_SCHEMA = SCHEMA.AnyNonemptyString()
PATHS_SCHEMA = SCHEMA.ListOf(PATH_SCHEMA)

# An integer representing logger levels, such as logging.CRITICAL (=50).
Expand Down
40 changes: 40 additions & 0 deletions securesystemslib/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,46 @@ def check_match(self, object):



class AnyNonemptyString(AnyString):
"""
<Purpose>
Matches any string with one or more characters.
This schema can be viewed as the Any() schema applied to Strings, but an
additional check is performed to ensure only strings are considered and
that said strings have at least one character.

Supported methods include:
matches(): returns a Boolean result.
check_match(): raises 'exceptions.FormatError' on a mismatch.

<Example Use>

>>> schema = AnyNonemptyString()
>>> schema.matches('')
False
>>> schema.matches('a string')
True
>>> schema.matches(['a'])
False
>>> schema.matches(3)
False
>>> schema.matches(u'a unicode string')
True
>>> schema.matches({})
False
"""

def check_match(self, object):
AnyString.check_match(self, object)

if object == "":
raise securesystemslib.exceptions.FormatError('Expected a string'
' with at least one character but got ' + repr(object))





class AnyBytes(Schema):
"""
<Purpose>
Expand Down
2 changes: 1 addition & 1 deletion securesystemslib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def file_in_confined_directories(filepath, confined_directories):
# Do the arguments have the correct format?
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)
securesystemslib.formats.PATHS_SCHEMA.check_match(confined_directories)
securesystemslib.formats.NAMES_SCHEMA.check_match(confined_directories)

for confined_directory in confined_directories:
# The empty string (arbitrarily chosen) signifies the client is confined
Expand Down
13 changes: 13 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ def test_AnyString(self):



def test_AnyNonemptyString(self):
anynonemptystring_schema = SCHEMA.AnyNonemptyString()

self.assertTrue(anynonemptystring_schema.matches("foo"))

# Test conditions for invalid arguments.
self.assertFalse(anynonemptystring_schema.matches(''))
self.assertFalse(anynonemptystring_schema.matches(['a']))
self.assertFalse(anynonemptystring_schema.matches(3))
self.assertFalse(anynonemptystring_schema.matches({'a': 'string'}))



def test_OneOf(self):
# Test conditions for valid arguments.
oneof_schema = SCHEMA.OneOf([SCHEMA.ListOf(SCHEMA.Integer()),
Expand Down