def testPersistence(self): temp_dir = getTempDirectory() directory = join(temp_dir, 'read_write') db_file = join(directory, 'db') mkdir(directory) try: db = MasterDB(db_file) self.assertTrue(exists(db_file), db_file) address = ('example.com', 1024) db.add(address) address2 = ('example.org', 1024) db.add(address2) # Values are visible to a new db instance db2 = MasterDB(db_file) self.assertTrue(address in db2, [x for x in db2]) self.assertTrue(address2 in db2, [x for x in db2]) db.discard(address) # Create yet another instance (file is not supposed to be shared) db3 = MasterDB(db_file) self.assertFalse(address in db3, [x for x in db3]) self.assertTrue(address2 in db3, [x for x in db3]) finally: if exists(db_file): unlink(db_file) rmdir(directory)
def testLaterAccessRights(self): """ Verify MasterDB does not raise when modifying database. """ temp_dir = getTempDirectory() directory = join(temp_dir, 'read_write') db_file = join(directory, 'db') mkdir(directory) try: db = MasterDB(db_file) self.assertTrue(exists(db_file), db_file) chmod(db_file, 0400) address = ('example.com', 1024) # Must not raise db.add(address) # Value is stored self.assertTrue(address in db, [x for x in db]) # But not visible to a new db instance (write access restored so # it can be created) chmod(db_file, 0600) db2 = MasterDB(db_file) self.assertFalse(address in db2, [x for x in db2]) finally: if exists(db_file): unlink(db_file) rmdir(directory)