def test_validate_path(self): base_dir = "/tmp/test/foo" validate_path(base_dir, "/tmp/test/foo/bar") validate_path(base_dir, "/tmp/test/foo/baz") validate_path(base_dir, "/tmp/test/foo/barf/text.dat") try: validate_path(base_dir, "/tmp/test/foo/../../../etc/passwd") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e
class SmokeTests(ArchiveTestCase): def _testLeakATempFile(self): out_file = open(self.tmps.make_temp_file(), 'wb') out_file.write("OH NOES! FILZ IZ LIIKAN!!!") out_file.close() def make_empty_archive(self, block_name): archive = WORMBlockArchive(DeltaCoder(), BlockStorage(self.tmps)) archive.create(self.test_dir, block_name) return archive def load_archive(self, block_name): archive = WORMBlockArchive(DeltaCoder(), BlockStorage(self.tmps)) archive.load(self.test_dir, block_name) return archive def test_create_archive(self): print archive = self.make_empty_archive('A') dump_archive(archive) def test_load_archive(self): print self.make_empty_archive('A') b = self.load_archive('A') dump_archive(b) def test_archive_write_read(self): a = self.make_empty_archive('A') dump_archive(a, "empty") r0 = self.write_file("OH HAI!") r1 = self.write_file("OH HAI! AGAIN") r2 = self.write_file("STILL ME") t1 = self.tmps.make_temp_file() try: a.start_update() link0 = a.write_new_delta(NULL_SHA, r0) link1 = a.write_new_delta(NULL_SHA, r1) link2 = a.write_new_delta(NULL_SHA, r2) # Write a.commit_update() dump_archive(a, "updated") # Read print print str_sha(link0[0]), a.get_data(link0[0]) print str_sha(link1[0]), a.get_data(link1[0]) print str_sha(link2[0]), a.get_data(link2[0]) a.close() b = self.load_archive('A') dump_archive(b, "[Reloaded from disk]") print # Mix up order. print str_sha(link1[0]), b.get_data(link1[0]) print str_sha(link0[0]), b.get_data(link0[0]) print str_sha(link2[0]), b.get_data(link2[0]) finally: self.tmps.remove_temp_file(t1) self.tmps.remove_temp_file(r0) self.tmps.remove_temp_file(r1) self.tmps.remove_temp_file(r2) #a.abandon_update() def test_torture_a_single_chain(self): a = self.make_empty_archive('A') dump_archive(a, "empty") text = "" prev = NULL_SHA for iteration in range(0, 5000): # Write a.start_update() text += str(time.time()) + '\n' t2 = self.write_file(text) #print "Adding to: ", str_sha(prev) link = a.write_new_delta(prev, t2) new_sha = link[0] link = None #print "Added: ", str_sha(new_sha), str_sha(new_parent) a.commit_update() self.tmps.remove_temp_file(t2) #history = a.blocks.get_history(new_sha) #history_size = sum([value[6] for value in history]) #print "History: ", len(history), history_size, len(text) #print #dump_archive(a, "updated", True) t3 = self.tmps.make_temp_file() a.get_file(new_sha, t3) self.assertTrue(text == self.read_file(t3)) prev = new_sha if iteration > 0 and iteration % 100 == 0: print "iteration: ", iteration # grrr... giving up on temp files def test_single_update(self): a = self.make_empty_archive('A') m = FileManifest() data = ( \ ('foo.txt', 'This is the foo file.\n'), ('empty.txt', ''), ('big.txt', '*' * (1024 * 128)), ) entries = entries_from_seq(self.tmps, data) m.update(a, entries) dump_archive(a) def test_multiple_updates(self): a = self.make_empty_archive('A') m = FileManifest() data0 = ( \ ('foo.txt', 'This is the foo file.\n'), ('empty.txt', ''), ('big.txt', '*' * (1 * 128)), ) print "manifest sha: ", str_sha(m.stored_sha) m.update(a, entries_from_seq(self.tmps, data0)) print "manifest sha: ", str_sha(m.stored_sha) dump_archive(a, "AFTER FIRST WRITE:") verify_manifest(a, m) data1 = ( \ ('foo.txt', 'This is the foo file.\n'), ('empty.txt', ''), ('big.txt', 'hello' + ('*' * (1 * 128))), ) m.update(a, entries_from_seq(self.tmps, data1)) print "manifest sha: ", str_sha(m.stored_sha) dump_archive(a) verify_link_map(a.blocks.link_map) verify_manifest(a, m) def test_words(self): print WORD_ITR.next() def test_lines(self): for line in lines(10): print line def test_many_updates(self): a = self.make_empty_archive('A') m = FileManifest() files = ("A.txt", "B.txt", "C.txt") updates = 100 for dummy in range(0, updates): names = list(files) random.shuffle(names) #names = names[:random.randrange(1, len(files))] data = [] for name in names: text = '' if name in m.name_map: tmp = self.tmps.make_temp_file() a.get_file(m.name_map[name][1], tmp) text = self.read_file(tmp) text += "\n".join([line for line in lines(20)]) data.append((name, text)) #print "updating:" #for value in data: # print value[0], len(value[1]) #print "manifest sha: ", str_sha(m.stored_sha) #dump_archive(a, "BEFORE UPDATE: %i" % count, True) m.update(a, entries_from_seq(self.tmps, data)) #print "manifest sha: ", str_sha(m.stored_sha) #dump_archive(a, "AFTER UPDATE: %i" % count, True) verify_manifest(a, m, True) verify_link_map(a.blocks.link_map) dump_blocks(a.blocks, None, True) a.close() def test_validate_path(self): base_dir = "/tmp/test/foo" validate_path(base_dir, "/tmp/test/foo/bar") validate_path(base_dir, "/tmp/test/foo/baz") validate_path(base_dir, "/tmp/test/foo/barf/text.dat") try: validate_path(base_dir, "/tmp/test/foo/../../../etc/passwd") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e try: validate_path(base_dir, "/tmp/test/foo/../forbidden") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e
validate_path(base_dir, "/tmp/test/foo/barf/text.dat") try: validate_path(base_dir, "/tmp/test/foo/../../../etc/passwd") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e try: validate_path(base_dir, "/tmp/test/foo/../forbidden") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e try: validate_path(base_dir, u"/tmp/test/foo/f\xc3\xb6rbjuden.txt") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e try: validate_path(base_dir, "/tmp/test/foo/f\xc3\xb6rbjuden.txt") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e def test_is_contiguous(self): self.assertTrue(is_contiguous(())) self.assertTrue(is_contiguous(((0, 0, '?'), ))) self.assertTrue(is_contiguous(((0, 0, 2), (1, 1, '?')))) self.assertTrue(is_contiguous(((0, 1, 2), (2, 3, '?'))))
validate_path(base_dir, "/tmp/test/foo/barf/text.dat") try: validate_path(base_dir, "/tmp/test/foo/../../../etc/passwd") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e try: validate_path(base_dir, "/tmp/test/foo/../forbidden") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e try: validate_path(base_dir, u"/tmp/test/foo/f\xc3\xb6rbjuden.txt") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e try: validate_path(base_dir, "/tmp/test/foo/f\xc3\xb6rbjuden.txt") self.assertTrue(False) except IOError, e: print "Got expected exception: ", e def test_is_contiguous(self): self.assertTrue(is_contiguous( () )) self.assertTrue(is_contiguous( ((0, 0, '?'), ) )) self.assertTrue(is_contiguous( ((0, 0, 2), (1, 1, '?')) ))