コード例 #1
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    def test_NNTPArticle_UU_encode_02(self):
        """
        Test the encoding of fresh new data
        """

        # Our private Key Location
        tmp_file = join(
            self.tmp_dir,
            'test_NNTPArticle_UU_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 = CodecUU(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())
コード例 #2
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    def test_decoding_uuenc_single_part(self):
        """
        Decodes a single UUEncoded message
        """
        # Input File
        encoded_filepath = join(self.var_dir, 'uuencoded.tax.jpg.msg')
        assert isfile(encoded_filepath)

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

        # Initialize Codec
        ud_py = CodecUU(work_dir=self.test_dir)

        # Read data and decode it
        with open(encoded_filepath, 'r') as fd_in:
            article = ud_py.decode(fd_in)

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

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

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

        # Compare our processed content with the expected results
        assert decoded == article.getvalue()
コード例 #3
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    def test_NNTPArticle_UU_encode_01(self):
        """
        Test the encoding of data; this is nessisary prior to a post
        """

        # First we take a binary file
        binary_filepath = join(self.var_dir, 'joystick.jpg')
        assert isfile(binary_filepath)

        # Initialize Codec
        encoder = CodecUU(work_dir=self.test_dir)

        # Create an NNTPArticle Object
        article = NNTPArticle()
        # Add our file
        article.add(binary_filepath)

        # Encode our article by object
        new_article_a = article.encode(encoder)

        # We should have gotten an NNTPArticle Object
        assert isinstance(new_article_a, NNTPArticle) is True

        # We should actually have article associated with out data
        assert len(new_article_a) > 0

        # Encode our article by type
        new_article_b = article.encode(CodecUU)

        # We should have gotten an NNTPArticle Object
        assert isinstance(new_article_b, NNTPArticle) is True

        # We should actually have article associated with out data
        assert len(new_article_b) > 0

        # Our article should be the same when it was generated by both
        # methods
        assert new_article_a[0].md5() == new_article_b[0].md5()

        # Chain our encodings
        new_article = article.encode(
            [CodecUU, CodecUU(work_dir=self.test_dir)],
        )

        # We should have gotten an ASCII Content Object
        assert isinstance(new_article, NNTPArticle) is True

        # We should actually have article associated with out data
        assert len(new_article) > 0
コード例 #4
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    def test_NNTPContent_encode(self):
        """
        Test the encoding of data; this is nessisary prior to a post
        """

        # First we take a binary file
        binary_filepath = join(self.var_dir, 'joystick.jpg')
        assert isfile(binary_filepath)

        # Initialize Codec
        encoder = CodecUU(work_dir=self.test_dir)

        # Create an NNTPContent Object
        content = NNTPBinaryContent(binary_filepath)

        # Encode our content by object
        new_content_a = content.encode(encoder)

        # We should have gotten an ASCII Content Object
        assert isinstance(new_content_a, NNTPAsciiContent) is True

        # We should actually have content associated with out data
        assert len(new_content_a) > 0

        # Encode our content by type
        new_content_b = content.encode(CodecUU)

        # We should have gotten an ASCII Content Object
        assert isinstance(new_content_b, NNTPAsciiContent) is True

        # We should actually have content associated with out data
        assert len(new_content_b) > 0

        # Our content should be the same when it was generated by both
        # methods
        assert new_content_a.md5() == new_content_b.md5()

        # Chain our encodings
        new_content = content.encode(
            [CodecUU, CodecUU(work_dir=self.test_dir)],
        )

        # We should have gotten an ASCII Content Object
        assert isinstance(new_content, NNTPAsciiContent) is True

        # We should actually have content associated with out data
        assert len(new_content) > 0
コード例 #5
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    def test_uu_bad_headers(self):
        """
        Make sure we fail on bad headers
        """
        # Initialize Codec
        ud = CodecUU(work_dir=self.tmp_dir, out_dir=self.out_dir)

        # Make sure we don't pick up on yenc content
        assert ud.detect(
            "=ybegin line=1024 size=12345",
        ) is None

        yenc_meta = ud.detect(
            "begin BDP FILENAME",
        )
        # The BDP (Bad Permission) assumes to be part of the filename
        assert len(yenc_meta) == 2
        assert yenc_meta['key'] == 'begin'
        assert yenc_meta['name'] == 'BDP FILENAME'
コード例 #6
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    def test_uu_encoding(self):
        """
        Test the encoding of data; this is nessisary prior to a post
        """

        # First we take a binary file
        binary_filepath = join(self.var_dir, 'joystick.jpg')
        assert isfile(binary_filepath)

        # Initialize Codec
        encoder = CodecUU(work_dir=self.test_dir)

        content = encoder.encode(binary_filepath)

        # We should have gotten an ASCII Content Object
        assert isinstance(content, NNTPAsciiContent) is True

        # We should actually have content associated with out data
        assert len(content) > 0
コード例 #7
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    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.
        """

        # Input File
        encoded_filepath = join(self.var_dir, 'uuencoded.tax.jpg.msg')
        assert isfile(encoded_filepath)

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

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

        # Read data and decode it
        with open(encoded_filepath, 'r') as fd_in:
            article = ud_py.decode(fd_in)

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

        # Our article should not be considered valid on an
        # early exit
        assert article.is_valid() is False

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

        # Compare our processed content with the expected results
        length = len(article.getvalue())

        # Even though we have't decoded all of our content, we're
        # still the same as the expected result up to what has been
        # processed.
        assert decoded[0:length] == article.getvalue()
コード例 #8
0
ファイル: CodecUU_Test.py プロジェクト: sebastian404/newsreap
    def test_uu_headers(self):
        """
        Test that we can pick up the uu headers correctly
        """
        # Initialize Codec
        ud = CodecUU(work_dir=self.tmp_dir, out_dir=self.out_dir)
        uu_meta = ud.detect("begin 775 mybinary.dat")
        assert uu_meta is not None
        assert len(uu_meta) == 3
        assert uu_meta['key'] == 'begin'
        assert uu_meta['perm'] == 0775
        assert uu_meta['name'] == 'mybinary.dat'

        # Whitespace has no bearing
        uu_meta = ud.detect("  begin    775   mybinary.dat   ")
        assert uu_meta is not None
        assert len(uu_meta) == 3
        assert uu_meta['key'] == 'begin'
        assert uu_meta['perm'] == 0775
        assert uu_meta['name'] == 'mybinary.dat'

        # End fails because we're processing content relative
        # to the decoder (which is expecing a begin at this time)
        uu_meta = ud.detect("  end ")
        assert uu_meta is None

        # end also doesn't care about whitespace
        uu_meta = ud.detect("  end ", relative=False)
        assert uu_meta is not None
        assert len(uu_meta) == 1
        assert uu_meta['key'] == 'end'

        # The '`' tilda character is used on the line
        # prior to the 'end' keyword.  we ignore this
        # entry in most cases, but treat it as a key
        # none the less.
        uu_meta = ud.detect("`", relative=False)
        assert uu_meta is not None
        assert len(uu_meta) == 1
        assert uu_meta['key'] == '`'
コード例 #9
0
    def test_encrytion(self):
        """
        Test te encryption and decryption of data

        """

        # Create our Cryptography Object
        obj = NNTPCryptography()

        # We can't save if we haven't created keys yet
        assert (obj.save() is False)

        # Generate our keys
        (prv, pub) = obj.genkeys()

        # Check that they're stored
        assert (prv, pub) == obj.keys()

        # Test small content first
        content = 'newsreap'

        # Let's encrypt our content
        encrypted = obj.encrypt(content)

        # Decrypt it now:
        decrypted = obj.decrypt(encrypted)

        # Test it out
        assert (str(content) == str(decrypted))

        # Note that the Hash value is important as encryption
        # and decryption will fail otherwise
        encrypted = obj.encrypt(
            content,
            alg=HashType.SHA512,
            mgf1=HashType.SHA512,
        )
        # Returns None in all cases below because either the alg
        assert (obj.decrypt(
            encrypted, alg=HashType.SHA256, mgf1=HashType.SHA512) is None)
        assert (obj.decrypt(
            encrypted, alg=HashType.SHA512, mgf1=HashType.SHA256) is None)
        assert (obj.decrypt(encrypted, alg=HashType.SHA384, mgf1=HashType.SHA1)
                is None)

        # However if we use the right hash
        decrypted = obj.decrypt(
            encrypted,
            alg=HashType.SHA512,
            mgf1=HashType.SHA512,
        )

        # It will succeed again
        assert (str(content) == str(decrypted))

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

        # Let's create a slightly larger file; one we'll need to process
        # in chunks
        assert (self.touch(tmp_file, size='128KB', random=True))

        # We'll yEnc the file since we can't deal with binary
        # Create an NNTPContent Object
        content = NNTPBinaryContent(tmp_file)

        # We need to iterate over all of our possible compression types
        # so that we can test that the chunk sizes are valid in all cases
        # This big O(n2) will test all of our supported operations
        for alg in CRYPTOGRAPHY_HASH_MAP.keys():
            for mgf1 in CRYPTOGRAPHY_HASH_MAP.keys():

                # Create our Cryptography Object
                obj = NNTPCryptography(alg=alg, mgf1=mgf1)

                # We can't save if we haven't created keys yet
                assert (obj.save() is False)

                # Generate our keys
                (prv, pub) = obj.genkeys()

                encoder = CodecUU(work_dir=self.test_dir)
                response = encoder.encode(content)

                # We should have gotten an ASCII Content Object
                assert (len(response) > 0)

                with open(response.filepath, 'rb') as f:
                    # Any chunk size higher then 190 doesn't seem to work
                    for chunk in iter(lambda: f.read(obj.chunk_size()), b''):
                        # Let's encrypt our content
                        encrypted = obj.encrypt(chunk)
                        assert (encrypted is not None)

                        # Decrypt it now:
                        decrypted = obj.decrypt(encrypted)

                        # Test it out
                        assert (str(chunk) == str(decrypted))