Skip to content

Commit

Permalink
Update affected module following review of util.py and test_util.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-v-diaz committed Feb 13, 2013
1 parent 5ab806f commit d2a799f
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 111 deletions.
2 changes: 1 addition & 1 deletion basic_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def update_client(repository_mirror):
repository_mirrors = {'mirror': {'url_prefix': repository_mirror,
'metadata_path': 'metadata',
'targets_path': 'targets',
'confined_target_paths': ['']}}
'confined_target_dirs': ['']}}

# Create the repository object using the repository name 'repository'
# and the repository mirrors defined above.
Expand Down
2 changes: 1 addition & 1 deletion example_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
repository_mirrors = {'mirror1': {'url_prefix': 'http://localhost:8001',
'metadata_path': 'metadata',
'targets_path': 'targets',
'confined_target_paths': ['']}}
'confined_target_dirs': ['']}}

# Create the Upater object using the updater name 'tuf-example'
# and the repository mirrors defined above.
Expand Down
8 changes: 4 additions & 4 deletions tuf/client/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@
# mirror is located at 'http://localhost:8001', and all of the metadata
# and targets files can be found in the 'metadata' and 'targets' directory,
# respectively. If the client wishes to only download target files from
# specific directories on the mirror, the 'confined_target_paths' field
# specific directories on the mirror, the 'confined_target_dirs' field
# should be set. In the example, the client has chosen '', which is
# interpreted as no confinement. In other words, the client can download
# targets from any directory or subdirectories. If the client had chosen
# 'targets1', they would have been confined to the '/targets/targets1/'
# 'targets1/', they would have been confined to the '/targets/targets1/'
# directory on the 'http://localhost:8001' mirror.
repository_mirrors = {'mirror1': {'url_prefix': 'http://localhost:8001',
'metadata_path': 'metadata',
'targets_path': 'targets',
'confined_target_paths': ['']}}
'confined_target_dirs': ['']}}
# The updater may now be instantiated. The Updater class of 'updater.py'
# is called with two arguments. The first argument assigns a name to this
Expand Down Expand Up @@ -232,7 +232,7 @@ def __init__(self, updater_name, repository_mirrors):
repository_mirrors = {'mirror1': {'url_prefix': 'http://localhost:8001',
'metadata_path': 'metadata',
'targets_path': 'targets',
'confined_target_paths': ['']}}
'confined_target_dirs': ['']}}
<Exceptions>
tuf.FormatError:
Expand Down
2 changes: 1 addition & 1 deletion tuf/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
url_prefix=URL_SCHEMA,
metadata_path=RELPATH_SCHEMA,
targets_path=RELPATH_SCHEMA,
confined_target_paths=SCHEMA.ListOf(PATH_SCHEMA),
confined_target_dirs=SCHEMA.ListOf(RELPATH_SCHEMA),
custom=SCHEMA.Optional(SCHEMA.Object()))

# A dictionary of mirrors where the dict keys hold the mirror's name and
Expand Down
11 changes: 6 additions & 5 deletions tuf/mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_list_of_mirrors(file_type, file_path, mirrors_dict):
{'url_prefix': 'http://localhost:8001'
'metadata_path': 'metadata/'
'targets_path': 'targets/'
'confined_target_paths': ['targets/release1', ...]
'confined_target_dirs': ['targets/release1/', ...]
'custom': {...}}
The 'custom' field is optional.
Expand All @@ -75,12 +75,12 @@ def get_list_of_mirrors(file_type, file_path, mirrors_dict):
'\'target\'.')
raise tuf.FormatError(msg)

# Reference to 'tuf.util.path_in_confined_paths()' (improve readability).
# This function checks whether a mirror serves the required file.
# Reference to 'tuf.util.file_in_confined_directories()' (improve readability).
# This function checks whether a mirror should serve a file to the client.
# A client may be confined to certain paths on a repository mirror
# when fetching target files. This field may be set by the client when
# the repository mirror is added to the 'tuf.client.updater.Updater' object.
in_confined = tuf.util.path_in_confined_paths
in_confined_directory = tuf.util.file_in_confined_directories

list_of_mirrors = []
for mirror_name, mirror_info in mirrors_dict.items():
Expand All @@ -90,7 +90,8 @@ def get_list_of_mirrors(file_type, file_path, mirrors_dict):
else:
targets_path = mirror_info['targets_path']
full_filepath = os.path.join(targets_path, file_path)
if not in_confined(full_filepath, mirror_info['confined_target_paths']):
if not in_confined_directory(full_filepath,
mirror_info['confined_target_dirs']):
continue
base = mirror_info['url_prefix']+'/'+mirror_info['targets_path']

Expand Down
6 changes: 3 additions & 3 deletions tuf/tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ def test_schemas(self):
{'url_prefix': 'http://localhost:8001',
'metadata_path': 'metadata/',
'targets_path': 'targets/',
'confined_target_paths': ['path1/', 'path2/'],
'confined_target_dirs': ['path1/', 'path2/'],
'custom': {'type': 'mirror'}}),

'MIRRORDICT_SCHEMA': (tuf.formats.MIRRORDICT_SCHEMA,
{'mirror1': {'url_prefix': 'http://localhost:8001',
'metadata_path': 'metadata/',
'targets_path': 'targets/',
'confined_target_paths': ['path1/', 'path2/'],
'confined_target_dirs': ['path1/', 'path2/'],
'custom': {'type': 'mirror'}}}),

'MIRRORLIST_SCHEMA': (tuf.formats.MIRRORLIST_SCHEMA,
Expand All @@ -240,7 +240,7 @@ def test_schemas(self):
'mirrors': [{'url_prefix': 'http://localhost:8001',
'metadata_path': 'metadata/',
'targets_path': 'targets/',
'confined_target_paths': ['path1/', 'path2/'],
'confined_target_dirs': ['path1/', 'path2/'],
'custom': {'type': 'mirror'}}]})}

# Iterate through 'valid_schemas', ensuring each 'valid_schema' correctly
Expand Down
19 changes: 9 additions & 10 deletions tuf/tests/test_mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,31 @@ def setUp(self):
{'mirror1': {'url_prefix' : 'http://mirror1.com',
'metadata_path' : 'metadata',
'targets_path' : 'targets',
'confined_target_paths' : ['']},
'confined_target_dirs' : ['']},
'mirror2': {'url_prefix' : 'http://mirror2.com',
'metadata_path' : 'metadata',
'targets_path' : 'targets',
'confined_target_paths' : ['targets/target3.py',
'targets/target4.py']},
'confined_target_dirs' : ['targets/release/',
'targets/release/']},
'mirror3': {'url_prefix' : 'http://mirror3.com',
'metadata_path' : 'metadata',
'targets_path' : 'targets',
'confined_target_paths' : ['targets/target1.py',
'targets/target2.py']}}
'confined_target_dirs' : ['targets/release/',
'targets/release/']}}



def test_get_list_of_mirrors(self):
# Test: Normal case.
mirror_list = \
mirrors.get_list_of_mirrors('meta', 'release.txt', self.mirrors)
mirror_list = mirrors.get_list_of_mirrors('meta', 'release.txt', self.mirrors)
self.assertEquals(len(mirror_list), 3)
for mirror, mirror_info in self.mirrors.items():
url = mirror_info['url_prefix']+'/metadata/release.txt'
self.assertTrue(url in mirror_list)

mirror_list = mirrors.get_list_of_mirrors('target', 'a', self.mirrors)
self.assertEquals(len(mirror_list), 3)
self.assertTrue(self.mirrors['mirror1']['url_prefix']+'/targets/a' in \
mirror_list = mirrors.get_list_of_mirrors('target', 'a.txt', self.mirrors)
self.assertEquals(len(mirror_list), 1)
self.assertTrue(self.mirrors['mirror1']['url_prefix']+'/targets/a.txt' in \
mirror_list)

mirror_list = mirrors.get_list_of_mirrors('target', 'a/b', self.mirrors)
Expand Down
8 changes: 4 additions & 4 deletions tuf/tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,12 @@ def test_6_download_target(self):
# Test:
# Attempt a file download of a valid target, however, a download exception
# occurs because the target is not within the mirror's confined
# target paths.
# Adjust mirrors dictionary, so that 'confined_target_paths' field
# target directories.
# Adjust mirrors dictionary, so that 'confined_target_dirs' field
# contains at least one confined target and excludes needed target file.
mirrors = self.Repository.mirrors
for mirror_name, mirror_info in mirrors.items():
mirrors[mirror_name]['confined_target_paths'] = [self.random_path()]
mirrors[mirror_name]['confined_target_dirs'] = [self.random_path()]

# Get the target file info.
file_path = target_rel_paths_src[0]
Expand All @@ -971,7 +971,7 @@ def test_6_download_target(self):
dest_dir)

for mirror_name, mirror_info in mirrors.items():
mirrors[mirror_name]['confined_target_paths'] = ['']
mirrors[mirror_name]['confined_target_dirs'] = ['']



Expand Down
51 changes: 30 additions & 21 deletions tuf/tests/test_util.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
import gzip
import shutil
import logging
import tuf.hash
import tempfile
import unittest
import unittest_toolbox

import tuf
import tuf.log
import tuf.hash
import tuf.util as util
import tuf.tests.unittest_toolbox as unittest_toolbox

# Disable/Enable logging. Uncomment to Disable.
logging.getLogger('tuf')
# Disable all logging calls of level CRITICAL and below.
# Comment the line below to enable logging.
logging.disable(logging.CRITICAL)


Expand Down Expand Up @@ -244,27 +245,35 @@ def test_B2_ensure_parent_dir(self):



def test_B3_path_in_confined_paths(self):
# Goal: Provide invalid input for 'test_path' and 'confined_paths'.
def test_B3_file_in_confined_directories(self):
# Goal: Provide invalid input for 'filepath' and 'confined_directories'.
# Include inputs like: '[1, 2, "a"]' and such...
Errors = (tuf.FormatError, TypeError)
list_of_confined_paths = ['a', 12, {'a':'a'}, [1]]
list_of_paths = [12, ['a'], {'a':'a'}, 'a']
for bogus_confined_paths in list_of_confined_paths:
for bogus_path in list_of_paths:
self.assertRaises(tuf.FormatError, util.path_in_confined_paths,
bogus_path, bogus_confined_paths)
# Reference to 'file_in_confined_directories()' to improve readability.
in_confined_directory = tuf.util.file_in_confined_directories
list_of_confined_directories = ['a', 12, {'a':'a'}, [1]]
list_of_filepaths = [12, ['a'], {'a':'a'}, 'a']
for bogus_confined_directory in list_of_confined_directories:
for filepath in list_of_filepaths:
self.assertRaises(tuf.FormatError, in_confined_directory,
filepath, bogus_confined_directory)

# Test: Inputs that evaluate to False.
for confined_paths in [['/a/b/c.txt', 'a/b/c'], ['/a/b/c/d/e/']]:
for path in ['/a/b/d.txt', 'a', 'a/b/c/d/']:
self.assertFalse(util.path_in_confined_paths(path, confined_paths))

confined_directories = ['a/b/', 'a/b/c/d/']
self.assertFalse(in_confined_directory('a/b/c/1.txt', confined_directories))

confined_directories = ['a/b/c/d/e/']
self.assertFalse(in_confined_directory('a', confined_directories))
self.assertFalse(in_confined_directory('a/b', confined_directories))
self.assertFalse(in_confined_directory('a/b/c', confined_directories))
self.assertFalse(in_confined_directory('a/b/c/d', confined_directories))
# Below, 'e' is a file in the 'a/b/c/d/' directory.
self.assertFalse(in_confined_directory('a/b/c/d/e', confined_directories))

# Test: Inputs that evaluate to True.
for confined_paths in [[''], ['/a/b/c.txt', '/a/', '/a/b/c/d/']]:
for path in ['a/b/d.txt', 'a/b/x', 'a/b', 'a/b/c/d/g']:
self.assertTrue(util.path_in_confined_paths(path, confined_paths))

self.assertTrue(in_confined_directory('a/b/c.txt', ['']))
self.assertTrue(in_confined_directory('a/b/c.txt', ['a/b/']))
self.assertTrue(in_confined_directory('a/b/c.txt', ['x', '']))
self.assertTrue(in_confined_directory('a/b/c/..', ['a/']))


def test_B4_import_json(self):
Expand Down
6 changes: 3 additions & 3 deletions tuf/tests/unittest_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ def setUp():
mirrors = {'mirror1': {'url_prefix' : 'http://mirror1.com',
'metadata_path' : 'metadata',
'targets_path' : 'targets',
'confined_target_paths' : ['']},
'confined_target_dirs' : ['']},
'mirror2': {'url_prefix' : 'http://mirror2.com',
'metadata_path' : 'metadata',
'targets_path' : 'targets',
'confined_target_paths' : ['']},
'confined_target_dirs' : ['']},
'mirror3': {'url_prefix' : 'http://mirror3.com',
'metadata_path' : 'metadata',
'targets_path' : 'targets',
'confined_target_paths' : ['']}}
'confined_target_dirs' : ['']}}



Expand Down

0 comments on commit d2a799f

Please sign in to comment.