diff options
-rw-r--r-- | combox/file.py | 29 | ||||
-rw-r--r-- | tests/crypto_test.py | 30 | ||||
-rw-r--r-- | tests/file_test.py | 35 |
3 files changed, 60 insertions, 34 deletions
diff --git a/combox/file.py b/combox/file.py index ec0372e..775f1fd 100644 --- a/combox/file.py +++ b/combox/file.py @@ -94,34 +94,39 @@ def write_file(filename, filecontent): print "ERROR: creating and writing content to %s" % (filename) exit(1) -def write_shards(shards, directory, shard_basename): +def write_shards(shards, directories, shard_basename): """Write shards to respective files respective files. shard: list of strings (ciphers or data). - directory: absolute path of directory to which it shards must be written to. + directories: absolute path of directories to which it shards must be written to. shard_basename: base name of the shard. """ - # partial filename of the shard - p_filename = path.join(directory, shard_basename) shard_no = 0 - for shard in shards: + for directory in directories: + # partial filename of the shard + p_filename = path.join(directory, shard_basename) shard_name = "%s.shard%s" % (p_filename, shard_no) - write_file(shard_name, shard) + write_file(shard_name, shards[shard_no]) shard_no += 1 -def read_shards(directory, shard_basename): +def read_shards(directories, shard_basename): """Read the shards from directory and return it as a list. - directory: absolute path of directory from which to read the shards. + directories: absolute path of directories from which to read the shards. shard_basename: base name of the shard. """ + # get the names of the file shards + file_shards = [] + for directory in directories: + filename_glob = "%s.shard*" % path.join(directory, shard_basename) + file_shard = glob(filename_glob)[0] + file_shards.append(file_shard) + shards = [] - # partial filename of the shard - p_filename = "%s.shard*" % path.join(directory, shard_basename) - for shard_file in sorted(glob(p_filename)): - shard_content = read_file(shard_file) + for file_shard in sorted(file_shards): + shard_content = read_file(file_shard) shards.append(shard_content) return shards diff --git a/tests/crypto_test.py b/tests/crypto_test.py index aaa3bd2..8de58e7 100644 --- a/tests/crypto_test.py +++ b/tests/crypto_test.py @@ -16,6 +16,8 @@ # along with Combox (see COPYING). If not, see # <http://www.gnu.org/licenses/>. +import yaml + from glob import glob from nose.tools import * from os import path, remove @@ -25,10 +27,19 @@ from combox.file import (split_data, glue_data, write_file, from combox.crypto import encrypt, decrypt, encrypt_shards, decrypt_shards -FILES_DIR = path.join('tests','files') +CONFIG_DIR = path.join('tests', 'test-config') + +config_file = path.join(CONFIG_DIR, 'config.yaml') +try: + config = yaml.load(file(config_file, 'r')) +except yaml.YAMLError, exc: + raise AssertionError("Error in configuration file:", exc) + +FILES_DIR = config['combox_dir'] TEST_FILE = path.join(FILES_DIR,'the-red-star.jpg') PASS = 'topsecret' + def test_encryption(): """ Read file, encrypt it to a cipher, write cipher to file, read encrypted file, decrypt it, write decrypted data to file. @@ -51,20 +62,24 @@ def test_split_encryption(): glue the shards together, write glued data to file. """ + # no. of shards = no. of nodes + SHARDS = len(config['nodes_info'].keys()) + f = path.abspath(TEST_FILE) f_content = read_file(f) - f_shards = split_data(f_content, 5) + f_shards = split_data(f_content, SHARDS) # encrypt shards ciphered_shards = encrypt_shards(f_shards, PASS) # write ciphered shards to disk - f_path = FILES_DIR f_basename = "%s.ciphered" % path.basename(f) - write_shards(ciphered_shards, f_path, f_basename) + nodes = [path.abspath(node['path']) for node in config['nodes_info'].itervalues()] + + write_shards(ciphered_shards, nodes, f_basename) # read ciphered shards from disk - ciphered_shards = read_shards(f_path, f_basename) + ciphered_shards = read_shards(nodes, f_basename) # decrypt shards f_parts = decrypt_shards(ciphered_shards, PASS) @@ -72,8 +87,3 @@ def test_split_encryption(): f_content_glued = glue_data(f_parts) assert f_content == f_content_glued - - # remove ciphered shards from disk - c_shards = glob("%s.ciphered*" % TEST_FILE) - for shard in c_shards: - remove(shard) diff --git a/tests/file_test.py b/tests/file_test.py index e418dfc..321533b 100644 --- a/tests/file_test.py +++ b/tests/file_test.py @@ -16,6 +16,8 @@ # along with Combox (see COPYING). If not, see # <http://www.gnu.org/licenses/>. +import yaml + from glob import glob from nose.tools import * from os import path, remove @@ -23,7 +25,16 @@ from os import path, remove from combox.file import (split_data, glue_data, write_file, read_file, write_shards, read_shards) -FILES_DIR = path.join('tests','files') +CONFIG_DIR = path.join('tests', 'test-config') + +config_file = path.join(CONFIG_DIR, 'config.yaml') +try: + config = yaml.load(file(config_file, 'r')) +except yaml.YAMLError, exc: + raise AssertionError("Error in configuration file:", exc) + + +FILES_DIR = config['combox_dir'] TEST_FILE = path.join(FILES_DIR,'the-red-star.jpg') def test_split(): @@ -47,26 +58,26 @@ def test_shards(): and check if they're the same as the orginal file. """ - SHARDS = 5 + # no. of shards = no. of nodes + SHARDS = len(config['nodes_info'].keys()) + f = path.abspath(TEST_FILE) f_content = read_file(f) f_shards = split_data(f_content, SHARDS) - f_path = FILES_DIR + f_basename = path.basename(f) - write_shards(f_shards, f_path, f_basename) + nodes = [path.abspath(node['path']) for node in config['nodes_info'].itervalues()] + write_shards(f_shards, nodes, f_basename) # check if the shards have been created. - for i in range(0,SHARDS): - shard = "%s.shard%s" % (TEST_FILE, i) + i = 0 + for node in nodes: + shard = "%s.shard%s" % (path.join(node, f_basename), i) + i += 1 assert path.isfile(shard) - f_shards = read_shards(f_path, f_basename) + f_shards = read_shards(nodes, f_basename) f_content_glued = glue_data(f_shards) assert f_content == f_content_glued - - # remove shards from disk. - shards = glob("%s.shard*" % TEST_FILE) - for shard in shards: - remove(shard) |