Replace os.sep with '/' in _check_path()

Use a hard-coded unix separator ('/') so that an
exception is also raised for paths starting with '/'
when executing on Windows systems.

Update test_check_path to explicitly test invalid paths
starting with Windows style separator.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
This commit is contained in:
Teodora Sechkova 2020-04-08 15:26:23 +03:00
parent a71b3c2b67
commit f035cf4e34
No known key found for this signature in database
GPG key ID: 65F78F613EA1914E
2 changed files with 8 additions and 7 deletions

View file

@ -1677,10 +1677,6 @@ def test_add_paths(self):
# paths, which it previously did.
self.targets_object.add_paths(['non-existent'], 'tuf')
# add_paths() should not raise an exception for paths that
# are not located in the repository's targets directory
repository_directory = os.path.join('repository_data', 'repository')
self.targets_object.add_paths([repository_directory], 'tuf')
@ -1724,11 +1720,16 @@ def test_check_path(self):
self.assertRaises(securesystemslib.exceptions.FormatError,
self.targets_object._check_path, 3)
# Test invalid pathname - starting with os separator
# Test invalid pathname
# Starting with os separator
self.assertRaises(tuf.exceptions.InvalidNameError,
self.targets_object._check_path, '/file1.txt')
# Test invalid pathname - using '\' as os separator
# Starting with Windows-style separator
self.assertRaises(tuf.exceptions.InvalidNameError,
self.targets_object._check_path, '\\file1.txt')
# Using Windows-style separator ('\')
self.assertRaises(tuf.exceptions.InvalidNameError,
self.targets_object._check_path, 'subdir\\non-existent')

View file

@ -2784,7 +2784,7 @@ def _check_path(self, pathname):
raise tuf.exceptions.InvalidNameError('Path ' + repr(pathname)
+ ' does not use the forward slash (/) as directory separator.')
if pathname.startswith(os.sep):
if pathname.startswith('/'):
raise tuf.exceptions.InvalidNameError('Path ' + repr(pathname)
+ ' starts with a directory separator. All paths should be relative'
' to targets directory.')