[stem/master] Integration tests for save_processed_files()

commit 86b90e84e26ac91e1f876ec96df4d983c5ab384f Author: Damian Johnson <atagar@torproject.org> Date: Thu Mar 8 09:04:47 2012 -0800 Integration tests for save_processed_files() Basic integ tests for the save function, checking that we can load persisted files and that it has some basic input validation. --- stem/descriptor/reader.py | 5 ++++- test/integ/descriptor/reader.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletions(-) diff --git a/stem/descriptor/reader.py b/stem/descriptor/reader.py index ec6a2b2..a3ad418 100644 --- a/stem/descriptor/reader.py +++ b/stem/descriptor/reader.py @@ -81,7 +81,10 @@ def save_processed_files(processed_files, path): with open(path, "w") as output_file: for path, timestamp in processed_files.items(): - output_file.write("%s %i" % (path, timestamp)) + if not os.path.isabs(path): + raise TypeError("Only absolute paths are acceptable: %s" % path) + + output_file.write("%s %i\n" % (path, timestamp)) class DescriptorReader(threading.Thread): """ diff --git a/test/integ/descriptor/reader.py b/test/integ/descriptor/reader.py index c8b93b0..6341153 100644 --- a/test/integ/descriptor/reader.py +++ b/test/integ/descriptor/reader.py @@ -71,4 +71,34 @@ class TestDescriptorReader(unittest.TestCase): test_listing_path = _make_processed_files_listing(BASIC_LISTING) os.chmod(test_listing_path, 0077) # remove read permissions self.assertRaises(IOError, stem.descriptor.reader.load_processed_files, test_listing_path) + + def test_save_processed_files(self): + """ + Basic sanity test for persisting files listings to disk. + """ + + initial_listing = { + "/tmp": 123, + "/bin/grep": 4567, + "/file with spaces/and \\ stuff": 890, + } + + # saves the initial_listing to a file then reloads it + test_listing_path = _get_processed_files_path() + stem.descriptor.reader.save_processed_files(initial_listing, test_listing_path) + loaded_listing = stem.descriptor.reader.load_processed_files(test_listing_path) + + self.assertEquals(initial_listing, loaded_listing) + + def test_save_processed_files_malformed(self): + """ + Tests the save_processed_files() function with malformed data. + """ + + missing_filename = {"": 123} + relative_filename = {"foobar": 123} + string_timestamp = {"/tmp": "123a"} + + for listing in (missing_filename, relative_filename, string_timestamp): + self.assertRaises(TypeError, stem.descriptor.reader.save_processed_files, listing, "/tmp/foo")
participants (1)
-
atagar@torproject.org