Exemplo n.º 1
0
    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())
Exemplo n.º 2
0
    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
    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))