Beispiel #1
0
    def test_yenc_v1_3_NNTPArticle_encode_02(self):
        """
        Test the encoding of fresh new data
        """

        # A simple test for ensuring that the yEnc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Our private Key Location
        tmp_file = join(
            self.tmp_dir,
            'test_yenc_v1_3_NNTPArticle_encode_02.tmp',
        )

        # Create a larger file
        assert(self.touch(tmp_file, size='1M', random=True))

        # Create an NNTPContent Object pointing to our new data
        content = NNTPBinaryContent(tmp_file)

        # Create a Yenc Codec instance
        encoder = CodecYenc(work_dir=self.test_dir)

        # This should produce our yEnc object now
        encoded = encoder.encode(content)
        assert isinstance(encoded, NNTPAsciiContent) is True

        # Now we want to decode the content we just encoded
        decoded = encoder.decode(encoded)

        # We should get a Binary Object in return
        assert isinstance(decoded, NNTPBinaryContent) is True

        # Our original content should be the same as our decoded
        # content
        assert(decoded.crc32() == content.crc32())
        assert(decoded.md5() == content.md5())
Beispiel #2
0
    def test_yenc_v1_3_NNTPArticle_encode_02(self):
        """
        Test the encoding of fresh new data
        """

        # A simple test for ensuring that the yEnc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Our private Key Location
        tmp_file = join(
            self.tmp_dir,
            'test_yenc_v1_3_NNTPArticle_encode_02.tmp',
        )

        # Create a larger file
        assert (self.touch(tmp_file, size='1M', random=True))

        # Create an NNTPContent Object pointing to our new data
        content = NNTPBinaryContent(tmp_file)

        # Create a Yenc Codec instance
        encoder = CodecYenc(work_dir=self.test_dir)

        # This should produce our yEnc object now
        encoded = encoder.encode(content)
        assert isinstance(encoded, NNTPAsciiContent) is True

        # Now we want to decode the content we just encoded
        decoded = encoder.decode(encoded)

        # We should get a Binary Object in return
        assert isinstance(decoded, NNTPBinaryContent) is True

        # Our original content should be the same as our decoded
        # content
        assert (decoded.crc32() == content.crc32())
        assert (decoded.md5() == content.md5())
Beispiel #3
0
    def test_partial_download(self):
        """
        Test the handling of a download that is explicitly ordered to abort
        after only some content is retrieved.  A way of 'peeking' if you will.

        This test is identicle to test_decoding_yenc_single_part defined in
        this same test file with the exception of testing the early abort.
        """
        # A simple test for ensuring that the yEnc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Input File
        encoded_filepath = join(self.var_dir, '00000005.ntx')
        assert isfile(encoded_filepath)

        # Compare File
        decoded_filepath = join(self.var_dir, 'testfile.txt')
        assert isfile(decoded_filepath)

        # Python Solution
        fd_py = BytesIO()

        # C Solution
        fd_c = BytesIO()

        # Initialize Codec (restrict content to be no larger then 10 bytes)
        decoder = CodecYenc(work_dir=self.test_dir, max_bytes=10)

        # Force to operate in python (manual/slow) mode
        CodecYenc.FAST_YENC_SUPPORT = False
        with open(encoded_filepath, 'r') as fd_in:
            content_py = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_py, NNTPBinaryContent)

        # Force to operate with the C extension yEnc
        # This require the extensions to be installed
        # on the system
        CodecYenc.FAST_YENC_SUPPORT = True
        with open(encoded_filepath, 'r') as fd_in:
            content_c = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_c, NNTPBinaryContent)

        # Our content is subject to an early exit so in all cases we should
        # not have valid content
        assert content_py.is_valid() is False
        assert content_c.is_valid() is False

        # Confirm that our output from our python implimentation
        # matches that of our yEnc C version.
        assert fd_py.tell() == fd_c.tell()

        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        # Compare our processed content with the expected results
        length_py = len(content_py.getvalue())
        length_c = len(content_c.getvalue())

        # Compare our processed content with the expected results
        assert decoded[0:length_py] == content_py.getvalue()
        assert decoded[0:length_c] == content_c.getvalue()
Beispiel #4
0
    def test_decoding_yenc_multi_part(self):
        """
        Test decoding of a yEnc multi-part

        This test was generated after visiting http://www.yenc.org and finding
        the examples they provide on their site.

            Downloaded the following zip file:
                http://www.yenc.org/yenc2.zip

            Then extracting it revealed 3 files:
                - 00000020.ntx
                    This is the yEnc file as it would have been seen after
                    being downloaded from the NNTP server (part 1 of 2)

                - 00000021.ntx
                    This is the yEnc file as it would have been seen after
                    being downloaded from the NNTP server (part 2 of 2)

                - joystick.jpg
                    This is what the contents of the file should look like
                    after being decoded (and assembled). This is what we use
                    to test the file against.
        """

        # A simple test for ensuring that the yEnc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Input File
        encoded_filepath_1 = join(self.var_dir, '00000020.ntx')
        encoded_filepath_2 = join(self.var_dir, '00000021.ntx')

        assert isfile(encoded_filepath_1)
        assert isfile(encoded_filepath_2)

        # Compare File
        decoded_filepath = join(self.var_dir, 'joystick.jpg')
        assert isfile(decoded_filepath)

        # Python Solution
        fd1_py = BytesIO()
        fd2_py = BytesIO()

        # C Solution
        fd1_c = BytesIO()
        fd2_c = BytesIO()

        # Initialize Codec
        decoder = CodecYenc(work_dir=self.test_dir)

        contents_py = []
        contents_c = []

        # Force to operate in python (manual/slow) mode
        CodecYenc.FAST_YENC_SUPPORT = False
        with open(encoded_filepath_1, 'r') as fd_in:
            contents_py.append(decoder.decode(fd_in))
        with open(encoded_filepath_2, 'r') as fd_in:
            contents_py.append(decoder.decode(fd_in))

        for x in contents_py:
            # Verify our data is good
            assert x.is_valid() is True

        # Force to operate with the C extension yEnc
        # This require the extensions to be installed
        # on the system
        CodecYenc.FAST_YENC_SUPPORT = True
        with open(encoded_filepath_1, 'r') as fd_in:
            contents_c.append(decoder.decode(fd_in))
        with open(encoded_filepath_2, 'r') as fd_in:
            contents_c.append(decoder.decode(fd_in))

        for x in contents_c:
            # Verify our data is good
            assert x.is_valid() is True

        # Confirm that our output from our python implimentation
        # matches that of our yEnc C version.
        assert fd1_py.tell() == fd1_c.tell()
        assert fd2_py.tell() == fd2_c.tell()

        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        # Assemble (TODO)
        contents_py.sort()
        contents_c.sort()

        content_py = NNTPBinaryContent(
            filepath=contents_py[0].filename,
            save_dir=self.out_dir,
        )
        content_c = NNTPBinaryContent(
            filepath=contents_c[0].filename,
            save_dir=self.out_dir,
        )

        # append() takes a list or another NNTPContent
        # and appends it's content to the end of the content
        content_py.append(contents_py)
        content_c.append(contents_py)

        assert len(content_py) == len(decoded)
        assert len(content_c) == len(decoded)

        # Compare our processed content with the expected results
        assert content_py.getvalue() == decoded
        assert content_c.getvalue() == decoded
Beispiel #5
0
    def test_decoding_yenc_single_part(self):
        """
        Test decoding of a yEnc single part

        This test was generated after visiting http://www.yenc.org and finding
        the examples they provide on their site.

            Downloaded the following zip file:
                http://www.yenc.org/yenc1.zip

            Then extracting it revealed 2 files:
                - 00000005.ntx
                    This is the yenc file as it would have been seen after
                    being downloaded from the NNTP server

                - testfile.txt
                    This is what the contents of the file should look like
                    after being decoded. This is what we use to test the file
                    against.
        """

        # A simple test for ensuring that the yenc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Input File
        encoded_filepath = join(self.var_dir, '00000005.ntx')
        assert isfile(encoded_filepath)

        # Compare File
        decoded_filepath = join(self.var_dir, 'testfile.txt')
        assert isfile(decoded_filepath)

        # Python Solution
        fd_py = BytesIO()

        # C Solution
        fd_c = BytesIO()

        # Initialize Codec
        decoder = CodecYenc(work_dir=self.test_dir)

        # Force to operate in python (manual/slow) mode
        CodecYenc.FAST_YENC_SUPPORT = False
        with open(encoded_filepath, 'r') as fd_in:
            content_py = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_py, NNTPBinaryContent)

        # Force to operate with the C extension yEnc
        # This require the extensions to be installed
        # on the system
        CodecYenc.FAST_YENC_SUPPORT = True
        with open(encoded_filepath, 'r') as fd_in:
            content_c = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_c, NNTPBinaryContent)

        # Verify the actual content itself reports itself
        # as being okay (structurally)
        assert content_py.is_valid() is True
        assert content_c.is_valid() is True

        # Confirm that our output from our python implimentation
        # matches that of our yenc C version.
        assert fd_py.tell() == fd_c.tell()

        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        # Compare our processed content with the expected results
        assert decoded == content_py.getvalue()
        assert decoded == content_c.getvalue()
Beispiel #6
0
    def test_partial_download(self):
        """
        Test the handling of a download that is explicitly ordered to abort
        after only some content is retrieved.  A way of 'peeking' if you will.

        This test is identicle to test_decoding_yenc_single_part defined in
        this same test file with the exception of testing the early abort.
        """
        # A simple test for ensuring that the yEnc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Input File
        encoded_filepath = join(self.var_dir, '00000005.ntx')
        assert isfile(encoded_filepath)

        # Compare File
        decoded_filepath = join(self.var_dir, 'testfile.txt')
        assert isfile(decoded_filepath)

        # Python Solution
        fd_py = BytesIO()

        # C Solution
        fd_c = BytesIO()

        # Initialize Codec (restrict content to be no larger then 10 bytes)
        decoder = CodecYenc(work_dir=self.test_dir, max_bytes=10)

        # Force to operate in python (manual/slow) mode
        CodecYenc.FAST_YENC_SUPPORT = False
        with open(encoded_filepath, 'r') as fd_in:
            content_py = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_py, NNTPBinaryContent)

        # Force to operate with the C extension yEnc
        # This require the extensions to be installed
        # on the system
        CodecYenc.FAST_YENC_SUPPORT = True
        with open(encoded_filepath, 'r') as fd_in:
            content_c = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_c, NNTPBinaryContent)

        # Our content is subject to an early exit so in all cases we should
        # not have valid content
        assert content_py.is_valid() is False
        assert content_c.is_valid() is False

        # Confirm that our output from our python implimentation
        # matches that of our yEnc C version.
        assert fd_py.tell() == fd_c.tell()

        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        # Compare our processed content with the expected results
        length_py = len(content_py.getvalue())
        length_c = len(content_c.getvalue())

        # Compare our processed content with the expected results
        assert decoded[0:length_py] == content_py.getvalue()
        assert decoded[0:length_c] == content_c.getvalue()
Beispiel #7
0
    def test_decoding_yenc_multi_part(self):
        """
        Test decoding of a yEnc multi-part

        This test was generated after visiting http://www.yenc.org and finding
        the examples they provide on their site.

            Downloaded the following zip file:
                http://www.yenc.org/yenc2.zip

            Then extracting it revealed 3 files:
                - 00000020.ntx
                    This is the yEnc file as it would have been seen after
                    being downloaded from the NNTP server (part 1 of 2)

                - 00000021.ntx
                    This is the yEnc file as it would have been seen after
                    being downloaded from the NNTP server (part 2 of 2)

                - joystick.jpg
                    This is what the contents of the file should look like
                    after being decoded (and assembled). This is what we use
                    to test the file against.
        """

        # A simple test for ensuring that the yEnc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Input File
        encoded_filepath_1 = join(self.var_dir, '00000020.ntx')
        encoded_filepath_2 = join(self.var_dir, '00000021.ntx')

        assert isfile(encoded_filepath_1)
        assert isfile(encoded_filepath_2)

        # Compare File
        decoded_filepath = join(self.var_dir, 'joystick.jpg')
        assert isfile(decoded_filepath)

        # Python Solution
        fd1_py = BytesIO()
        fd2_py = BytesIO()

        # C Solution
        fd1_c = BytesIO()
        fd2_c = BytesIO()

        # Initialize Codec
        decoder = CodecYenc(work_dir=self.test_dir)

        contents_py = []
        contents_c = []

        # Force to operate in python (manual/slow) mode
        CodecYenc.FAST_YENC_SUPPORT = False
        with open(encoded_filepath_1, 'r') as fd_in:
            contents_py.append(decoder.decode(fd_in))
        with open(encoded_filepath_2, 'r') as fd_in:
            contents_py.append(decoder.decode(fd_in))

        for x in contents_py:
            # Verify our data is good
            assert x.is_valid() is True

        # Force to operate with the C extension yEnc
        # This require the extensions to be installed
        # on the system
        CodecYenc.FAST_YENC_SUPPORT = True
        with open(encoded_filepath_1, 'r') as fd_in:
            contents_c.append(decoder.decode(fd_in))
        with open(encoded_filepath_2, 'r') as fd_in:
            contents_c.append(decoder.decode(fd_in))

        for x in contents_c:
            # Verify our data is good
            assert x.is_valid() is True

        # Confirm that our output from our python implimentation
        # matches that of our yEnc C version.
        assert fd1_py.tell() == fd1_c.tell()
        assert fd2_py.tell() == fd2_c.tell()

        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        # Assemble (TODO)
        contents_py.sort()
        contents_c.sort()

        content_py = NNTPBinaryContent(
            filepath=contents_py[0].filename,
            save_dir=self.out_dir,
        )
        content_c = NNTPBinaryContent(
            filepath=contents_c[0].filename,
            save_dir=self.out_dir,
        )

        # append() takes a list or another NNTPContent
        # and appends it's content to the end of the content
        content_py.append(contents_py)
        content_c.append(contents_py)

        assert len(content_py) == len(decoded)
        assert len(content_c) == len(decoded)

        # Compare our processed content with the expected results
        assert content_py.getvalue() == decoded
        assert content_c.getvalue() == decoded
Beispiel #8
0
    def test_decoding_yenc_single_part(self):
        """
        Test decoding of a yEnc single part

        This test was generated after visiting http://www.yenc.org and finding
        the examples they provide on their site.

            Downloaded the following zip file:
                http://www.yenc.org/yenc1.zip

            Then extracting it revealed 2 files:
                - 00000005.ntx
                    This is the yenc file as it would have been seen after
                    being downloaded from the NNTP server

                - testfile.txt
                    This is what the contents of the file should look like
                    after being decoded. This is what we use to test the file
                    against.
        """

        # A simple test for ensuring that the yenc
        # library exists; otherwise we want this test
        # to fail; the below line will handle this for
        # us; we'll let the test fail on an import error
        import yenc

        # Input File
        encoded_filepath = join(self.var_dir, '00000005.ntx')
        assert isfile(encoded_filepath)

        # Compare File
        decoded_filepath = join(self.var_dir, 'testfile.txt')
        assert isfile(decoded_filepath)

        # Python Solution
        fd_py = BytesIO()

        # C Solution
        fd_c = BytesIO()

        # Initialize Codec
        decoder = CodecYenc(work_dir=self.test_dir)

        # Force to operate in python (manual/slow) mode
        CodecYenc.FAST_YENC_SUPPORT = False
        with open(encoded_filepath, 'r') as fd_in:
            content_py = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_py, NNTPBinaryContent)

        # Force to operate with the C extension yEnc
        # This require the extensions to be installed
        # on the system
        CodecYenc.FAST_YENC_SUPPORT = True
        with open(encoded_filepath, 'r') as fd_in:
            content_c = decoder.decode(fd_in)

        # our content should be valid
        assert isinstance(content_c, NNTPBinaryContent)

        # Verify the actual content itself reports itself
        # as being okay (structurally)
        assert content_py.is_valid() is True
        assert content_c.is_valid() is True

        # Confirm that our output from our python implimentation
        # matches that of our yenc C version.
        assert fd_py.tell() == fd_c.tell()

        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        # Compare our processed content with the expected results
        assert decoded == content_py.getvalue()
        assert decoded == content_c.getvalue()