Exemplo n.º 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())
Exemplo n.º 2
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())
Exemplo n.º 3
0
    def test_checksum(self):
        """Test the assorted checksums supported
        """

        # First we create a 1MB file
        tmp_file = join(
            self.tmp_dir, 'NNTPContent_Test.checksum', 'tmpa.tmp')
        # File should not already exist
        assert(isfile(tmp_file) is False)
        # Create a random 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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir)

        md5 = content.md5()
        sha1 = content.sha1()
        sha256 = content.sha256()

        assert(md5 is not None)
        assert(sha1 is not None)
        assert(sha256 is not None)

        tmp_file_2 = join(
            self.tmp_dir, 'NNTPContent_Test.checksum', 'tmp2.rar')
        # File should not already exist
        assert(isfile(tmp_file_2) is False)
        # We'll create a copy of our file
        assert(content.save(filepath=tmp_file_2, copy=True) is True)
        # Now it should
        assert(isfile(tmp_file_2) is True)
        # Now we'll open the new file we created
        content_2 = NNTPContent(filepath=tmp_file_2, work_dir=self.tmp_dir)

        md5_2 = content_2.md5()
        sha1_2 = content_2.sha1()
        sha256_2 = content_2.sha256()

        assert(md5_2 is not None)
        assert(sha1_2 is not None)
        assert(sha256_2 is not None)

        # files should be the same
        assert(md5 == md5_2)
        assert(sha1 == sha1_2)
        assert(sha256 == sha256_2)
Exemplo n.º 4
0
    def test_copy02(self):
        """
        Test the copy() function some more
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.copy', '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)

        # We want to make a copy of it
        content_b = content_a.copy()

        # Our content should be the same
        assert (len(content_a) == len(content_b))
        assert (content_a.md5() == content_b.md5())

        # Because we made a copy, we're working with a different file however
        # we share the exact same content and filename identifier.
        assert (content_a == content_b)

        # Our paths are not the same though; this is what makes us a official
        # copy
        assert (content_a.path() != content_b.path())
Exemplo n.º 5
0
    def test_copy02(self):
        """
        Test the copy() function some more
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.copy', '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)

        # We want to make a copy of it
        content_b = content_a.copy()

        # Our content should be the same
        assert(len(content_a) == len(content_b))
        assert(content_a.md5() == content_b.md5())

        # Because we made a copy, we're working with a different file however
        # we share the exact same content and filename identifier.
        assert(content_a == content_b)

        # Our paths are not the same though; this is what makes us a official
        # copy
        assert(content_a.path() != content_b.path())
Exemplo n.º 6
0
    def test_checksum(self):
        """Test the assorted checksums supported
        """

        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.checksum', 'tmpa.tmp')
        # File should not already exist
        assert (isfile(tmp_file) is False)
        # Create a random 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 = NNTPContent(filepath=tmp_file, work_dir=self.tmp_dir)

        md5 = content.md5()
        sha1 = content.sha1()
        sha256 = content.sha256()

        assert (md5 is not None)
        assert (sha1 is not None)
        assert (sha256 is not None)

        tmp_file_2 = join(self.tmp_dir, 'NNTPContent_Test.checksum',
                          'tmp2.rar')
        # File should not already exist
        assert (isfile(tmp_file_2) is False)
        # We'll create a copy of our file
        assert (content.save(filepath=tmp_file_2, copy=True) is True)
        # Now it should
        assert (isfile(tmp_file_2) is True)
        # Now we'll open the new file we created
        content_2 = NNTPContent(filepath=tmp_file_2, work_dir=self.tmp_dir)

        md5_2 = content_2.md5()
        sha1_2 = content_2.sha1()
        sha256_2 = content_2.sha256()

        assert (md5_2 is not None)
        assert (sha1_2 is not None)
        assert (sha256_2 is not None)

        # files should be the same
        assert (md5 == md5_2)
        assert (sha1 == sha1_2)
        assert (sha256 == sha256_2)
Exemplo n.º 7
0
    def test_append(self):
        """
        Test the split() and then append() function
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.append', '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)

        # Attach our content
        assert (content_a.is_attached() is False)
        content_a.attach()

        # We'll split it in 4
        results = content_a.split(strsize_to_bytes('512K'))

        # Tests that our results are expected
        assert (isinstance(results, sortedset) is True)
        assert (len(results) == 2)

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

        # Our content should be attached
        assert (content_a.is_attached() is True)
        assert (content_b.is_attached() is True)

        # For each of our disassembled items, re-assemble them
        for content in results:
            assert (content_b.append(content) is True)

        # Test that our content is the same again
        assert (len(content_a) == len(content_b))
        assert (content_a.md5() == content_b.md5())
Exemplo n.º 8
0
    def test_append(self):
        """
        Test the split() and then append() function
        """
        # First we create a 1MB file
        tmp_file = join(self.tmp_dir, 'NNTPContent_Test.append', '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)

        # Attach our content
        assert(content_a.is_attached() is False)
        content_a.attach()

        # We'll split it in 4
        results = content_a.split(strsize_to_bytes('512K'))

        # Tests that our results are expected
        assert(isinstance(results, sortedset) is True)
        assert(len(results) == 2)

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

        # Our content should be attached
        assert(content_a.is_attached() is True)
        assert(content_b.is_attached() is True)

        # For each of our disassembled items, re-assemble them
        for content in results:
            assert(content_b.append(content) is True)

        # Test that our content is the same again
        assert(len(content_a) == len(content_b))
        assert(content_a.md5() == content_b.md5())