示例#1
0
    def test_with(self):
        """
        Test the use of the with clause
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.with', '1MB.rar')
        # File should not already exist
        assert (isfile(tmp_file) is False)
        # Create our file
        assert (self.touch(tmp_file, size='1MB', random=True) is True)
        # File should exist now
        assert (isfile(tmp_file) is True)

        # Now we want to load it into a NNTPContent object
        content_a = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir)
        content_a.attach()

        # Create a second object
        content_b = NNTPContent(work_dir=self.tmp_dir)

        with content_a as fp:
            while 1:
                # Read our data
                buf = fp.read(1024)
                if not buf:
                    break

                # Write our content to disk
                content_b.write(buf)

        # At this point we should have a duplicate of our original object
        assert (len(content_a) == len(content_b))
        assert (content_a.md5() == content_b.md5())
示例#2
0
    def test_writes(self):
        """
        More overhead then a normal write() but none the less, using the
        write() in this class keeps things simple since the file is
        automatically opened if it was otherwise closed
        """

        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.write', 'tmp.file')
        # File should not already exist
        assert (isfile(tmp_file) is False)

        # Now we want to create our NNTPContent() object surrouding this
        # file that does not exist.
        content = NNTPContent(filepath=tmp_file, work_dir=dirname(tmp_file))
        # It's worth noting that this file will 'still' not exist
        assert (isfile(tmp_file) is False)
        # we'll write data

        data = 'hello\r\n'
        content.write(data)

        # It's worth noting that this file will ''STILL'' not exist
        assert (isfile(tmp_file) is False)

        # Save content
        assert (content.save() is True)

        # Now the file 'will' exist
        assert (isfile(tmp_file) is True)

        # Open our file and verify it is the data we saved.
        with open(tmp_file) as f:
            data_read = f.read()
        assert (data == data_read)
示例#3
0
    def test_with(self):
        """
        Test the use of the with clause
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.with', '1MB.rar')
        # File should not already exist
        assert(isfile(tmp_file) is False)
        # Create our file
        assert(self.touch(tmp_file, size='1MB', random=True) is True)
        # File should exist now
        assert(isfile(tmp_file) is True)

        # Now we want to load it into a NNTPContent object
        content_a = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir)
        content_a.attach()

        # Create a second object
        content_b = NNTPContent(work_dir=self.tmp_dir)

        with content_a as fp:
            while 1:
                # Read our data
                buf = fp.read(1024)
                if not buf:
                    break

                # Write our content to disk
                content_b.write(buf)

        # At this point we should have a duplicate of our original object
        assert(len(content_a) == len(content_b))
        assert(content_a.md5() == content_b.md5())
示例#4
0
    def test_writes(self):
        """
        More overhead then a normal write() but none the less, using the
        write() in this class keeps things simple since the file is
        automatically opened if it was otherwise closed
        """

        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.write', 'tmp.file')
        # File should not already exist
        assert(isfile(tmp_file) is False)

        # Now we want to create our NNTPContent() object surrouding this
        # file that does not exist.
        content = NNTPContent(filepath=tmp_file, work_dir=dirname(tmp_file))
        # It's worth noting that this file will 'still' not exist
        assert(isfile(tmp_file) is False)
        # we'll write data

        data = 'hello\r\n'
        content.write(data)

        # It's worth noting that this file will ''STILL'' not exist
        assert(isfile(tmp_file) is False)

        # Save content
        assert(content.save() is True)

        # Now the file 'will' exist
        assert(isfile(tmp_file) is True)

        # Open our file and verify it is the data we saved.
        with open(tmp_file) as f:
            data_read = f.read()
        assert(data == data_read)
示例#5
0
    def test_get_paths(self):
        """
        Test that we fail under certain conditions

        """
        # Generate temporary folder to work with
        work_dir = join(self.tmp_dir, 'CodecFile_Test', 'work')

        # Initialize Codec (without volume_size disables it)
        cr = CodecFile(work_dir=work_dir)

        # create some dummy file entries
        tmp_files = set()
        for i in range(0, 10):
            # Create some temporary files to work with in our source
            # directory
            tmp_file = join(work_dir, 'DSC_IMG%.3d.jpeg' % i)
            self.touch(tmp_file, size='120K', random=True)

            # Add a file to our tmp_files list
            tmp_files.add(tmp_file)

        # Non-existant file reference
        invalid_file = join(self.tmp_dir, 'non-existant-file')
        assert isfile(invalid_file) is False

        content = NNTPContent(
            join(work_dir, 'testfile'),
            work_dir=self.tmp_dir,
        )
        content.write('test data')

        # Empty NNTPArticle() can not be added
        article = NNTPArticle(work_dir=self.tmp_dir)

        # New Empty NNTPContent() can not be added
        article_content = NNTPContent(
            join(work_dir, 'testfile2'),
            work_dir=self.tmp_dir,
        )
        # Store some new data
        article_content.write('some more test data')

        # We'll add our new content to our article
        assert article.add(content) is True

        # save path
        sub_dir = join(work_dir, 'subdir')
        assert mkdir(sub_dir) is True
        assert isdir(sub_dir) is True

        # string work
        assert len(cr.get_paths(self.tmp_dir)) == 1
        assert cr.get_paths(self.tmp_dir).pop() == self.tmp_dir

        # Sub-directories that exist within a root directory also included are
        # removed
        assert len(cr.get_paths([self.tmp_dir, sub_dir])) == 1
        assert cr.get_paths([self.tmp_dir, sub_dir]).pop() == self.tmp_dir

        # Invalid files/dirs are not found
        assert len(cr.get_paths(invalid_file)) == 0

        # Create a list of many assorted type of items
        __set = set([
            work_dir,
            sub_dir,
            article_content,
            content,
            invalid_file,
        ]) | set(tmp_files)

        # At the end of the day, the work_dir includes all of the sub-content
        # and the invalid_file is simply just tossed. However because our
        # NNTPContent() and NNTPArticle() files are stored outside of our
        # work_dir, they will also be included in the results
        results = cr.get_paths(__set)
        assert len(results) == 3
        assert work_dir in results
        assert content.filepath in results
        assert article_content.filepath in results

        # Now if we did the same test but without the work_dir directory, then
        # we'd have a much larger list; we'll work with lists this time to
        # show that we support them too
        __list = [
            sub_dir,
            article_content,
            content,
            invalid_file,
        ]
        __list.extend(tmp_files)

        results = cr.get_paths(__list)
        # +1 for content
        # +1 for sub_dir
        assert len(results) == (len(tmp_files) + len(article) + 2)
        for f in tmp_files:
            # Each file in our tmp_files will be in our results
            assert f in results
        assert content.filepath in results
        assert article_content.filepath in results
        assert sub_dir in results
示例#6
0
    def test_rar_errors(self):
        """
        Test that we fail under certain conditions

        """
        # Generate temporary folder to work with
        work_dir = join(self.tmp_dir, 'CodecRar_Test.rar.fail', 'work')

        # Now we want to prepare a folder filled with temporary content
        # Note: this directory is horrible because it's 'within' our work_dir
        # as a result, adding content should not succeed
        source_dir = join(work_dir, 'test')

        # Initialize Codec (without volume_size disables it)
        cr = CodecRar(work_dir=work_dir)

        # No files
        assert len(cr) == 0

        tmp_file = join(source_dir, 'temp_file_non-existant')
        assert isfile(tmp_file) is False
        # We can't add content that does not exist
        assert cr.add(tmp_file) is False

        # Still No files
        assert len(cr) == 0

        # However directories can not cross into our work directory
        tmp_dir = dirname(work_dir)

        # We intentionally pick a directory that has the work_dir
        # as a child within it
        assert isdir(tmp_dir)

        # Denied adding the file because it would include the work_dir
        # if we did
        assert cr.add(tmp_file) is False

        # Temporary file (within directory denied in previous check)
        tmp_file = join(tmp_dir, 'temp_file')
        assert isfile(tmp_file) is False

        # Create our temporary file now
        self.touch(tmp_file, size='120K', random=True)
        assert isfile(tmp_file) is True

        # This file is within our work_dir but we're still okay because
        # it's accessing the file explicitly and the fact it's a file
        # and not a directory
        assert cr.add(tmp_file) is True

        # Now we'll have 1 entry in our list
        assert len(cr) == 1

        # You can't add duplicates
        assert cr.add(tmp_file) is False

        # We still have 1 entry
        assert len(cr) == 1

        # Empty NNTPContent() can not be added
        content = NNTPContent(unique=True, work_dir=self.tmp_dir)

        # Can't do it
        assert cr.add(content) is False

        # Store some data
        content.write('some data\r\n')

        # Now we can add it because it has data in it
        assert cr.add(content) is True

        # We now have 2 entries
        assert len(cr) == 2

        # We can't add duplicates
        assert cr.add(content) is False

        # We still have 2 entries
        assert len(cr) == 2

        # Empty NNTPArticle() can not be added
        article = NNTPArticle(work_dir=self.tmp_dir)

        # Can't do it
        assert cr.add(article) is False

        # If we add content that's already been added, nothing
        # new will happen either
        assert article.add(content) is True

        # Still can't do (only because it was already added)
        assert cr.add(article) is False

        # We still have 2 entries
        assert len(cr) == 2

        # New Empty NNTPContent() can not be added
        content = NNTPContent(unique=True, work_dir=self.tmp_dir)

        # We'll add our new content to our article
        assert article.add(content) is True

        # Our new content has no data associated with it, so this should
        # still fail
        assert cr.add(article) is False

        # We still have 2 entries
        assert len(cr) == 2

        # Store some new data
        content.write('some new data\r\n')

        # Our new content within our article now has data so this will work
        assert cr.add(article) is True

        # We now have 3 entries
        assert len(cr) == 3
示例#7
0
    def test_7z_errors(self):
        """
        Test that we fail under certain conditions

        """
        # Generate temporary folder to work with
        work_dir = join(self.tmp_dir, 'Codec7Zip_Test.7z.fail', 'work')

        # Now we want to prepare a folder filled with temporary content
        # Note: this directory is horrible because it's 'within' our work_dir
        # as a result, adding content should not succeed
        source_dir = join(work_dir, 'test')

        # Initialize Codec (without volume_size disables it)
        cr = Codec7Zip(work_dir=work_dir)

        # No files
        assert len(cr) == 0

        tmp_file = join(source_dir, 'temp_file_non-existant')
        assert isfile(tmp_file) is False
        # We can't add content that does not exist
        assert cr.add(tmp_file) is False

        # Still No files
        assert len(cr) == 0

        # However directories can not cross into our work directory
        tmp_dir = dirname(work_dir)

        # We intentionally pick a directory that has the work_dir
        # as a child within it
        assert isdir(tmp_dir)

        # Denied adding the file because it would include the work_dir
        # if we did
        assert cr.add(tmp_file) is False

        # Temporary file (within directory denied in previous check)
        tmp_file = join(tmp_dir, 'temp_file')
        assert isfile(tmp_file) is False

        # Create our temporary file now
        self.touch(tmp_file, size='120K', random=True)
        assert isfile(tmp_file) is True

        # This file is within our work_dir but we're still okay because
        # it's accessing the file explicitly and the fact it's a file
        # and not a directory
        assert cr.add(tmp_file) is True

        # Now we'll have 1 entry in our list
        assert len(cr) == 1

        # You can't add duplicates
        assert cr.add(tmp_file) is False

        # We still have 1 entry
        assert len(cr) == 1

        # Empty NNTPContent() can not be added
        content = NNTPContent(unique=True, work_dir=self.tmp_dir)

        # Can't do it
        assert cr.add(content) is False

        # Store some data
        content.write('some data\r\n')

        # Now we can add it because it has data in it
        assert cr.add(content) is True

        # We now have 2 entries
        assert len(cr) == 2

        # We can't add duplicates
        assert cr.add(content) is False

        # We still have 2 entries
        assert len(cr) == 2

        # Empty NNTPArticle() can not be added
        article = NNTPArticle(work_dir=self.tmp_dir)

        # Can't do it
        assert cr.add(article) is False

        # If we add content that's already been added, nothing
        # new will happen either
        assert article.add(content) is True

        # Still can't do (only because it was already added)
        assert cr.add(article) is False

        # We still have 2 entries
        assert len(cr) == 2

        # New Empty NNTPContent() can not be added
        content = NNTPContent(unique=True, work_dir=self.tmp_dir)

        # We'll add our new content to our article
        assert article.add(content) is True

        # Our new content has no data associated with it, so this should
        # still fail
        assert cr.add(article) is False

        # We still have 2 entries
        assert len(cr) == 2

        # Store some new data
        content.write('some new data\r\n')

        # Our new content within our article now has data so this will work
        assert cr.add(article) is True

        # We now have 3 entries
        assert len(cr) == 3