Ejemplo n.º 1
0
class TestFileSystem(unittest.TestCase):
    def setUp(self):
        self.file_system = FileSystem()
        drive = self.file_system.get_root_node()
        self.test_folder = self.file_system.create(2, 'test_folder', drive.path)
        self.test_folder2 = self.file_system.create(2, 'test_folder2', drive.path)
        self.text_file1 = self.file_system.create(3, 'text_file1', self.test_folder.path)
        self.text_file2 = self.file_system.create(3, 'text_file2', self.test_folder.path)
        self.text_file3 = self.file_system.create(3, 'text_file3', self.test_folder2.path)
        self.zip_file1 = self.file_system.create(4, 'zip_file1', self.test_folder2.path)
        self.text_file4 = self.file_system.create(3, 'text_file4', self.zip_file1.path)

    def test_create_should_create_folder(self):
        type = EntityTypes.FOLDER.value
        name = 'testfolder'
        parent = self.test_folder
        created = self.file_system.create(type, name, parent.path)
        self.assertIn(created.name, parent.children)

    def test_create_should_create_file(self):
        type = EntityTypes.TEXT_FILE.value
        name = 'testfile'
        parent = self.test_folder
        created = self.file_system.create(type, name, parent.path)
        self.assertIn(created.name, parent.children)

    def test_create_should_not_allow_duplicate(self):
        type = EntityTypes.FOLDER.value
        name = 'test_folder'
        parent = self.file_system.get_root_node()
        with self.assertRaises(FileExistsError):
            self.file_system.create(type, name, parent.path)

    def test_create_should_not_allow_if_no_parent(self):
        type = EntityTypes.ZIP_FILE.value
        name = 'test'
        with self.assertRaises(FileNotFoundError):
            self.file_system.create(type, name, 'C:\\cant_find_me')

    def test_delete_should_delete_file(self):
        type = EntityTypes.TEXT_FILE.value
        name = 'testfile1'
        parent = self.test_folder
        created = self.file_system.create(type, name, parent.path)
        self.file_system.delete(created.path)
        self.assertNotIn(created.name, parent.children)

    def test_delete_should_error_if_not_found(self):
        with self.assertRaises(FileNotFoundError):
            self.file_system.delete('C:\\i_dont_exist')

    def test_move_should_move_file(self):
        type = EntityTypes.TEXT_FILE.value
        name = 'test_pre_move_file'
        moved_name = 'test_moved_file'
        parent = self.test_folder
        moved_parent = self.test_folder2
        created = self.file_system.create(type, name, parent.path)
        moved = self.file_system.move(created.path, f'{moved_parent.path}\\{moved_name}')
        self.assertNotIn(created.name, parent.children)
        self.assertIn(moved.name, moved_parent.children)

    def test_write_to_file_should_change_content(self):
        content = 'Test Content'
        self.file_system.write_to_file(self.text_file1.path, content)
        self.assertEqual(content, self.text_file1.content)

    def test_write_to_file_should_change_size(self):
        content = 'More Test Content'
        expected_size = len(content)
        self.file_system.write_to_file(self.text_file2.path, content)
        self.assertEqual(expected_size, self.text_file2.size)

    def test_write_to_file_should_change_parent_size(self):
        content = 'More Test Content'
        expected_size = len(content)
        self.file_system.write_to_file(self.text_file3.path, content)
        self.assertEqual(expected_size, self.test_folder2.size)
        
    def test_write_to_file_in_zip_file_should_change_parent_size(self):
        content = 'File inside zip file content'
        # Zip file should expect to be half the size of the inner contents
        expected_size = len(content) / 2
        self.file_system.write_to_file(self.text_file4.path, content)
        self.assertEqual(expected_size, self.zip_file1.size)
Ejemplo n.º 2
0
class Peer(Base):
    def __init__(self, addr, port, network=None):
        Base.__init__(self, DFS(addr, port))
        self.fileSystem_ = FileSystem(self.dfs_)

        if not network:
            network = Network(self.dfs_, self.fileSystem_)
        self.network_ = network

# TODO not loading state for now
#        self.loadState()

    ##
    # Public API
    ##
    def open(self, fileName, op):
        exists = self.fileSystem_.exists(fileName)

        if exists and self.fileSystem_.isDeleted(fileName):
            self.log_.i('WARNING: Trying to open non-existant file ' + fileName + ' in "r". Aborting open...')
            return err.CannotOpenFile

        status = self.updateFile(fileName)
        self.log_.i('Opening ' + fileName + ' in "' + op + '"...')

        if op is "r":
            if exists and not self.fileSystem_.physical_.exists(fileName):
                self.log_.i('WARNING: Unable to reteived ' + fileName + '. Aborting open...')
                return err.CannotOpenFile

            if exists:
                if self.fileSystem_.canRead(fileName):
                    self.fileSystem_.logical_.fileList_[fileName].readCounter += 1
                    self.fileSystem_.logical_.fileList_[fileName].state = "r"
                else:
                    self.log_.i('WARNING: Trying to open ' + fileName + ' in "r", but it is already open in "w". Aborting open...')
                    return err.CannotOpenFile
            else:
                self.log_.i('WARNING: Trying to open non-existant file ' + fileName + ' in "r". Aborting open...')
                self.log_.w('tried to open ' + fileName + ' in read mode, but it doesnt exist')
                return err.FileNotFound
        elif op is "w":
            if not exists:
                buf = [' ']
                self.fileSystem_.physical_.fillEmptyFile(fileName, 1)
                self.fileSystem_.add(fileName)
                self.log_.v('creating ' + fileName + ' b/c opened it in write mode for the first time')
            if self.fileSystem_.canWrite(fileName):
                if not self.fileSystem_.physical_.exists(fileName):
                    self.fileSystem_.physical_.fillEmptyFile(fileName, 1)
                self.fileSystem_.logical_.fileList_[fileName].state = "w"
            else:
                self.log_.i('WARNING: Trying to open ' + fileName + ' in "w", but it is already open. Aborting open...')
                return err.CannotOpenFile
        else:
            self.log_.i('WARNING: Trying to open ' + fileName + ' in unknown mode "' + op + '". Aborting open...')
            return err.InvalidOp

        self.log_.v('-- opened ' + fileName + ' in ' + op)
        return status

    def close(self, fileName, noLogging=False):
        if not self.fileSystem_.exists(fileName) or self.fileSystem_.isDeleted(fileName):
            self.log_.i('WARNING: Trying to close non-existant file ' + fileName + ' in "r". Aborting close...')
            self.log_.w('tried to close ' + fileName + ', which doesnt exist: ' + str(self.fileSystem_.exists(fileName)))
            return err.FileNotFound

        file = self.fileSystem_.logical_.fileList_[fileName]
        if file.state is "":
            self.log_.i('WARNING: Trying to close ' + fileName + ', but it is already closed. Aborting close...')
            return err.FileNotOpen

        if not noLogging:
            self.log_.i('Closing ' + fileName + '...')

        if file.state is "r":
            if file.readCounter > 0:
                file.readCounter -= 1
                if file.readCounter == 0:
                    file.state = ''
            else:
                file.state = ""
        else: # state is 'w'
            if file.hasLocalChanges and self.dfs_.online:
                file.latestVersion = file.localVersion.copy()
                self.network_.fileEdited()
                self.network_.waitForPropagation()
            file.state = ""

        self.log_.v('-- closed ' + fileName + ', state is now: ' + file.state)
        return err.OK

    def read(self, fileName, buf=None, offset=0, bufsize=-1):
        if not self.fileSystem_.exists(fileName):
            self.log_.i('WARNING: Unable to retreive ' + fileName + '. Aborting read...')
            self.log_.w('tried to read from ' + fileName + ', which doesnt exist')
            return err.FileNotFound

        if self.fileSystem_.logical_.fileList_[fileName].state is "r":
            if not self.fileSystem_.physical_.exists(fileName):
                self.log_.i('WARNING: Unable to retrieve ' + fileName + '. Aborting read...')
                self.log_.e('tried to read from ' + fileName + ', which doesnt exist physically')
                return err.FileNotFound

            if self.fileSystem_.isMissingChunks(fileName):
                self.log_.i('WARNING: ' + fileName + ' was not fully retreived. You are reading a partially downloaded file.')
            elif self.fileSystem_.logical_.getFile(fileName).isOutOfDate():
                self.log_.i('WARNING: ' + fileName + ' could not be retreived from the system. You are reading an out of date file.')

            if not buf:
                buf = [0] * self.fileSystem_.physical_.getFileSize(fileName)
            if bufsize < 0:
                bufsize = len(buf)
            status = self.fileSystem_.readIntoBuffer(fileName, buf, offset, bufsize)
        else:
            self.log_.i('WARNING: Cannot read from ' + fileName + ' because it is not in read mode. Aborting read....')
            self.log_.w('tried to read from ' + fileName + ' while not in read mode')
            return err.FileNotOpen

        try:
            text = ''.join(buf)
        except Exception:
            text = str(buf)
        textToPrint = text
        if len(textToPrint) > 500:
            textToPrint = textToPrint[:500]
        self.log_.i('Read ' + fileName + ':\n"""\n' + textToPrint + '\n"""')
        return text

    def getSize(self, fileName):
        if self.fileSystem_.physical_.exists(fileName):
            return self.fileSystem_.physical_.getFileSize(fileName)
        return 0

    def hasLocalCopy(self, fileName):
        if not self.fileSystem_.exists(fileName):
            return False
        return self.fileSystem_.isMissingChunks(fileName) or not self.fileSystem_.isUpToDate(fileName)

    def write(self, fileName, buf, offset=0, bufsize=-1, deleteOriginalContent=False):
        if bufsize < 0:
            bufsize = len(buf)

        self.log_.i('Writing buffer of size ' + str(bufsize) + ' to ' + fileName + '...')
        if not self.fileSystem_.exists(fileName):
            self.log_.i('WARNING: Cannot write to ' + fileName + ' because it is not in write mode. Aborting write....')
            return err.FileNotFound

        if self.fileSystem_.logical_.fileList_[fileName].state is "w":
            if deleteOriginalContent:
                self.fileSystem_.physical_.trim(fileName, 1)
            self.fileSystem_.write(fileName, buf, offset, bufsize)
        else:
            self.log_.i('WARNING: Cannot write to ' + fileName + ' because it is not in write mode. Aborting write....')
            self.log_.w('tried to write to ' + fileName + ' while not in write mode')
            return err.FileNotOpenForWrite
        return err.OK

    def delete(self, fileName):
        if not self.fileSystem_.exists(fileName) or self.fileSystem_.isDeleted(fileName):
            self.log_.w(fileName + ' doest exist or is already deleted')
            return 1
        file = self.fileSystem_.logical_.getFile(fileName)
        if file.state != '':
            self.close(fileName, noLogging=True)

        self.log_.i('Deleting ' + fileName + '...')
        self.fileSystem_.delete(fileName)
        self.network_.fileEdited()
        self.network_.waitForPropagation()
        return err.OK

    def listFiles(self, files=[]):
        while len(files) > 0:
            files.pop()
        for f in self.fileSystem_.list():
            files.append(f)
        self.printInfo(files)
        return err.OK

    # mark the file as stable
    def markStable(self, fileName):
        if not self.fileSystem_.exists(fileName):
            self.log_.w('tried to mark ' + fileName + ' as stable, but doesnt exist')
            return 1
        self.log_.i('Marking ' + fileName + ' as stable...')
        newFileName = fileName + ".stable";
        while self.fileSystem_.exists(newFileName):
            newFileName = newFileName + ".stable"
        self.fileSystem_.copyFile(fileName, newFileName)
        if self.dfs_.online:
            self.network_.fileEdited()
            self.network_.waitForPropagation()

    # save the most recent version of the file locally
    def pin(self, fileName):
        if not self.fileSystem_.exists(fileName):
            return err.FileNotFound
        # TODO check write / read access
        self.log_.i('Pinning ' + fileName + '...')
        status = self.updateFile(fileName, 3)
        return status

    # delete the local copy of the file
    def unpin(self, fileName):
        if not self.fileSystem_.exists(fileName):
            return err.FileNotFound
        # TODO check write / read access
        self.log_.i('Unpinning ' + fileName + '...')
        self.fileSystem_.deleteLocalCopy(fileName)
        return err.OK

    # join DFS, connecting to the peer at the given addr and port if given
    def join(self, addr, port=None):
        if not port:
            peer = addr
            addr = peer.dfs_.addr
            port = peer.dfs_.port

        self.log_.i('Connecting to DFS from starting point ' + addr + ':' + str(port) + '...')
        status = self.network_.connectTo(DFS(addr, port))
        self.network_.waitForPropagation()
        return status

    # retire from the system
    def retire(self):
        self.log_.i('Retiring...')
        self.goOffline()
        return err.OK

    # connect to the internet
    def goOnline(self):
        if not self.dfs_.online:
            self.log_.i('Going online...')
            self.network_.connect()
            self.dfs_.online = True
            self.network_.waitForPropagation()
            return err.OK
        else:
            return err.AlreadyOnline

    # disconnect from the internet
    def goOffline(self):
        if self.dfs_.online:
            self.log_.i('Going offline...')
            self.network_.disconnect()
            self.dfs_.online = False
            self.network_.waitForPropagation()
            return err.OK
        else:
            return err.AlreadyOffline

    # exits the program
    def exit(self):
        self.log_.i('exiting...')
        self.goOffline()
        fs = self.fileSystem_.getState()
        ns = self.network_.getState()
        self.log_.v('writing ' + str(len(fs)) + ' files and ' + str(len(ns)) + ' peers to disk')
        state = (fs, ns)
        s = serializer.serialize(state)
        self.fileSystem_.writeState(s)

    ##
    # Private functions
    ##
    def loadState(self):
        state = self.fileSystem_.readState()
        if state:
            try:
                fs, ns = serializer.deserialize(state)
            except Exception, ex:
                self.log_.e('found state, but failed to deserialize: ' + str(ex))
                return

            if len(fs) + len(ns) > 0:
                self.log_.i('Loaded ' + str(len(fs)) + ' files and ' + str(len(ns)) + ' device addresses from state')
                self.fileSystem_.loadFromState(fs)
                self.network_.loadFromState(ns)
class FileSystemTests(IceCommon.TestCase):
    
    def removeDirectory(self, dir):
        if os.path.exists(dir):
            files = os.listdir(dir)
            for file in files:
                    file = dir + "/" + file
                    if os.path.isdir(file):
                            self.removeDirectory(file)
                    else:
                            os.remove(file)
            os.rmdir(dir)
        
    
    def setUp(self):
        self.__testDir = "tempTestDir"
        self.fs = FileSystem(self.__testDir)
        self.removeDirectory(self.__testDir)
        assert(os.path.exists(self.__testDir)==False)
        try: os.mkdir(self.__testDir)
        except: pass
    
    def tearDown(self):
        self.removeDirectory(self.__testDir)
        pass
    
    def testCurrentWorkingDirectory(self):
        testDir = os.path.abspath(self.__testDir).replace("\\", "/")
        self.assertEqual(self.fs.currentWorkingDirectory, testDir)

    #    absolutePath(path)
    def testAbsolutePath(self):
        # on windows root will equal "c:/"
        root = os.path.abspath("/").replace("\\", "/")
        #print "root='%s'" % root
        self.assertEqual(self.fs.absolutePath("."), self.fs.currentWorkingDirectory)
        self.assertEqual(self.fs.absolutePath(".."), os.path.abspath(".").replace("\\", "/"))
        self.assertEqual(self.fs.absolutePath("/"), root)
        self.assertEqual(self.fs.absolutePath("/test/one"), root + "test/one")
        self.assertEqual(self.fs.absolutePath("/test\\one"), root + "test/one")
    
    #    join(*args)
    def testJoin(self):
        self.assertEqual(self.fs.join("one", "two", "3"), "one/two/3")
        self.assertEqual(self.fs.join("one/", "two/"), "one/two/")
        self.assertEqual(self.fs.join("one", "/two"), "/two")
        self.assertEqual(self.fs.join(".", ".."), "./..")
        
    #    split(path)
    def testSplit(self):
        self.assertEqual(self.fs.split("one/two/three"), ("one/two", "three"))
        self.assertEqual(self.fs.split("one"), ("", "one"))
        self.assertEqual(self.fs.split("../."), ("..", "."))
        self.assertEqual(self.fs.split(""), ("", ""))
    
    #    splitExt(path)
    def testSplitExt(self):
        self.assertEqual(self.fs.splitExt("one/two"), ("one/two", ""))
        self.assertEqual(self.fs.splitExt("one.two.ext"), ("one.two", ".ext"))
        self.assertEqual(self.fs.splitExt("one/two.ext/three"), ("one/two.ext/three", ""))
        self.assertEqual(self.fs.splitExt(".ext"), ("", ".ext"))
        self.assertEqual(self.fs.splitExt(""), ("", ""))
    
    #    splitPathFileExt(path)\
    def testSplitPathFileExt(self):
        self.assertEqual(self.fs.splitPathFileExt("/one/two"), ("/one", "two", ""))
        self.assertEqual(self.fs.splitPathFileExt("one.two.ext"), ("", "one.two", ".ext"))
        self.assertEqual(self.fs.splitPathFileExt("one/two.ext/three"), ("one/two.ext", "three", ""))
        self.assertEqual(self.fs.splitPathFileExt(".ext"), ("", "", ".ext"))
        self.assertEqual(self.fs.splitPathFileExt(""), ("", "", ""))
    
    #    exists(path)
    def testExists(self):
        testFile = "testExistsFile"
        testDir = "testExistsDir"
        
        self.assertFalse(self.fs.exists(testFile))
        self.assertFalse(self.fs.exists(testDir))
        f = open(os.path.join(self.__testDir, testFile), "wb")
        f.write("Testing")
        f.close()
        os.mkdir(os.path.join(self.__testDir, testDir))
        self.assertTrue(self.fs.exists(testFile))
        self.assertTrue(self.fs.exists(testDir))
        
    
    #    isFile(path)
    def testIsFile(self):
        testFile = "testIsFile"
        testDir = "testIsFileDir"
        
        self.assertFalse(self.fs.isFile(testFile))
        self.assertFalse(self.fs.isFile(testDir))
        f = open(os.path.join(self.__testDir, testFile), "wb")
        f.write("Testing")
        f.close()
        os.mkdir(os.path.join(self.__testDir, testDir))
        self.assertTrue(self.fs.isFile(testFile))
        self.assertFalse(self.fs.isFile(testDir))
    
    #    isDirectory(path)
    def testIsDirectory(self):
        testFile = "testIsDirFile"
        testDir = "testIsDir"
        
        self.assertFalse(self.fs.isDirectory(testFile))
        self.assertFalse(self.fs.isDirectory(testDir))
        f = open(os.path.join(self.__testDir, testFile), "wb")
        f.write("Testing")
        f.close()
        os.mkdir(os.path.join(self.__testDir, testDir))
        self.assertFalse(self.fs.isDirectory(testFile))
        self.assertTrue(self.fs.isDirectory(testDir))
    
    #    makeDirectory(path)
    def testMakeDirectory(self):
        dir1 = "makeDir"
        dir2 = "makeDir2/subDir/subSubDir"
        self.assertFalse(self.fs.isDirectory(dir1))
        self.assertFalse(self.fs.isDirectory(dir2))
        self.fs.makeDirectory(dir1)
        self.fs.makeDirectory(dir2)
        self.assertTrue(self.fs.isDirectory(dir1))
        self.assertTrue(self.fs.isDirectory(dir2))
    
    #    delete(path)
    def testDelete(self):
        file = "deleteFile"
        dir = "dir/subDir/subSubDir"
        self.fs.writeFile(file, "testing")
        self.fs.writeFile(self.fs.join(dir, "file"), "test")
        self.assertTrue(self.fs.isDirectory(dir))
        self.assertTrue(self.fs.isFile(file))
        self.fs.delete(file)
        self.fs.delete(dir)
        self.assertFalse(self.fs.isDirectory(dir))
        self.assertFalse(self.fs.isFile(file))
    
    #    copy(fromPath, toPath)
    def testCopy(self):
        # Simple file copy test
        fromFile = "copyFile"
        toFile = "copyToFile"
        toDirFile = "dir/subDir/copyToFile"
        self.fs.writeFile(fromFile, "test data")
        self.assertFalse(self.fs.exists(toFile))
        self.fs.copy(fromFile, toFile)
        self.fs.copy(fromFile, toDirFile)
        self.assertTrue(self.fs.exists(toFile))
        self.assertEqual(self.fs.readFile(toFile), self.fs.readFile(fromFile))
        self.assertEqual(self.fs.readFile(toDirFile), self.fs.readFile(fromFile))
        
        # Directory, subDirectory and files copy test
        dir = "copyDir"
        toDir = "copyToDir"
        self.fs.writeFile("copyDir/subDir/file", "testing")
        self.assertFalse(self.fs.exists(toDir))
        self.fs.copy(dir, toDir)
        self.assertEqual(self.fs.readFile("copyDir/subDir/file"), self.fs.readFile("copyToDir/subDir/file"))
    
    #    move(fromPath, toPath)
    def testMove(self):
        # move file test
        file = "file"
        toFile = "toFile"
        toDirFile = "dir/subDir/toFile"
        self.fs.writeFile(file, "testing")
        self.fs.move(file, toFile)
        self.assertFalse(self.fs.isFile(file))
        self.assertTrue(self.fs.isFile(toFile))
        self.fs.move(toFile, toDirFile)
        self.assertFalse(self.fs.isFile(toFile))
        self.assertTrue(self.fs.isFile(toDirFile))
        
        # move dir test
        dir = "dirToMove"
        toDir = "movedDir"
        self.fs.writeFile(self.fs.join(dir, "file"), "testing")
        self.fs.move(dir, toDir)
        self.assertFalse(self.fs.isDirectory(dir))
        self.assertTrue(self.fs.isDirectory(toDir))
        self.assertEqual(self.fs.readFile(self.fs.join(toDir, "file")), "testing")
    
    
    #  walk(path)
    def testWalk(self):
        tree = ["./one.txt", "./test/two.txt", "./test/subDir/three.txt"]
        tree.sort()
        for f in tree:
            self.fs.writeFile(f, "testData")
        rFiles = []
        for path, dirs, files in self.fs.walk("."):
            self.assertTrue(path.endswith("/"))
            for file in files:
                rFiles.append(path + file)
        rFiles.sort()
        self.assertEqual(tree, rFiles)
    
    
    #  walker(path, func)
    def testWalker(self):
        tree = ["./one.txt", "./test/two.txt", "./test/subDir/three.txt"]
        tree.sort()
        for f in tree:
            self.fs.writeFile(f, "testData")
            
        def callback(path, dirs, files):
            self.assertTrue(path.endswith("/"))
            for file in files:
                rFiles.append(path + file)
            
        rFiles = []
        self.fs.walker(".", callback)
        rFiles.sort()
        self.assertEqual(tree, rFiles)
    
    
    #  createTempDirectory
    def testCreateTempDirectory(self):
        tempDir = self.fs.createTempDirectory()
        self.assertNotEqual(tempDir, None)
        self.assertTrue(len(str(tempDir))>1)
        self.assertTrue(tempDir.isTempDirectory)
        self.assertTrue(os.path.isdir(str(tempDir)))
        
        tempDir.delete()
        #print str(tempDir)
    
    #  writeFile(path, data)
    def testWriteFile(self):
        file = "writeFile"
        data = "Testing\nThe End.\n"
        self.assertFalse(self.fs.isFile(file))
        self.fs.writeFile(file, data)
        self.assertTrue(self.fs.isFile(file))
        self.assertEqual(self.fs.readFile(file), data)
    
    #  readFile(path)
    def testReadFile(self):
        file = "writeFile"
        data = "Testing\nThe End.\n"
        self.assertFalse(self.fs.isFile(file))
        self.fs.writeFile(file, data)
        self.assertTrue(self.fs.isFile(file))
        self.assertEqual(self.fs.readFile(file), data)
    
    #  zip(toZipFile, path="./")
    def testZip(self, data="TestData\n", files=None):
        pathToZip = "zipDir"
        if files is None:
            files = ["one.txt", "two.txt", "dir/three.txt", "dir2/subDir/four.txt"]
        zipFile = "test.zip"
        for file in files:
            path = self.fs.join(pathToZip, file)
            self.fs.writeFile(path, data)
        self.fs.zip(zipFile, pathToZip)
        self.assertTrue(self.fs.isFile(zipFile))
        
    
    #  unzipToTempDirectory(zipFile)
    def testUnzipToTempDirectory(self):
        self.testZip()
        tempDir = self.fs.unzipToTempDirectory("test.zip")
        self.assertTrue(tempDir.isFile("one.txt"))
        tempDir.delete()
        
    
    #  unzipToDirectory(zipFile, path)
    def testUnzipToDirectory(self):
        data="Testing\n123\n"
        self.testZip(data=data)
        self.fs.unzipToDirectory("test.zip", "temp")
        self.assertTrue(self.fs.isDirectory("temp"))
        self.assertEqual(self.fs.readFile("temp/dir2/subDir/four.txt"), data)
    
    
    def testZipList(self):
        files = ["one.txt", "two.txt", "dir/three.txt", "dir2/subDir/four.txt"]
        self.testZip(files=files)
        list = self.fs.zipList("test.zip")
        files.sort()
        list.sort()
        self.assertEqual(list, files)
    
    #  readFromZipFile(zipFile, filename)
    def testReadFromZipFile(self):
        data="Testing\n123\n"
        self.testZip(data=data)
        result = self.fs.readFromZipFile("test.zip", "one.txt")
        self.assertEqual(data, result)
        result = self.fs.readFromZipFile("test.zip", "dir2/subDir/four.txt")
        self.assertEqual(data, result)
    
    
    #  addToZipFile(zipFile, filename, data)
    def testAddToZipFile(self):
        files = ["one.txt", "two.txt", "dir/three.txt", "dir2/subDir/four.txt"]
        self.testZip(files=files)
        # Test adding a new File
        self.fs.addToZipFile("test.zip", "newFile.txt", "testing")
        files.append("newFile.txt")
        files.sort()
        rList = self.fs.zipList("test.zip")
        rList.sort()
        self.assertEqual(rList, files)
        # Test replacing a file
        self.fs.addToZipFile("test.zip", "one.txt", "replaceData")
        rList = self.fs.zipList("test.zip")
        rList.sort()
        self.assertEqual(rList, files)
        self.assertEqual(self.fs.readFromZipFile("test.zip", "one.txt"), "replaceData")
    
    
    #  list(path=".")
    def testList(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        list = self.fs.list(dir)
        list.sort()
        all = files + dirs
        all.sort()
        self.assertEqual(list, all)
    
    #  listFiles(path=".")
    def testListFiles(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        files.sort()
        dirs.sort()
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        list = self.fs.listFiles(dir)
        list.sort()
        self.assertEqual(list, files)
    
    #  listDirectories(path=".")
    def testDirectories(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        list = self.fs.listDirectories(dir)
        list.sort()
        dirs.sort()
        self.assertEqual(list, dirs)

    #  listDirsFiles(path=".")
    def testListDirsFiles(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        files.sort()
        dirs.sort()
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        rdirs, rfiles = self.fs.listDirsFiles(dir)
        rdirs.sort()
        rfiles.sort()
        self.assertEqual(rdirs, dirs)
        self.assertEqual(rfiles, files)