Exemple #1
0
 def test_encrypt(self, xor_new_mock, encode_mock):
     xor_cipher_mock = xor_new_mock.return_value
     cipher = xor_cipher.XORCipher("fishsticks")
     self.assertEqual(cipher.encrypt("meow"), encode_mock.return_value)
     xor_new_mock.assert_called_with("fishsticks")
     xor_cipher_mock.encrypt.assert_called_with("meow")
     encode_mock.assert_called_with(xor_cipher_mock.encrypt.return_value)
Exemple #2
0
 def test_decrypt(self, xor_new_mock, decode_mock):
     xor_cipher_mock = xor_new_mock.return_value
     cipher = xor_cipher.XORCipher("puppychow")
     self.assertEqual(cipher.decrypt("wruff"),
                      xor_cipher_mock.decrypt.return_value)
     xor_new_mock.assert_called_with("puppychow")
     decode_mock.assert_called_with("wruff")
     xor_cipher_mock.decrypt.assert_called_with(decode_mock.return_value)
Exemple #3
0
    def test_generate_key(self):
        # Test random byte array.
        cipher = xor_cipher.XORCipher()
        self.assertEqual(len(cipher.generate_key()), 16)  # test default.
        self.assertEqual(len(cipher.generate_key(16)), 16)
        self.assertEqual(len(cipher.generate_key(32)), 32)
        self.assertRaises(AttributeError, cipher.generate_key, 48)

        # Test ASCII only.
        self.assertEqual(len(cipher.generate_key(ascii_only=True)), 16)
        for char in cipher.generate_key(32, ascii_only=True):
            self.assertTrue(char in string.ascii_letters)
Exemple #4
0
    def test_encryption(self):
        """This will actually execute encrypting/decrypting data."""
        cipher = xor_cipher.XORCipher("fishsticks")

        def run(cipher, plaintext):
            ciphertext = cipher.encrypt(plaintext)
            self.assertTrue(ciphertext is not None)
            self.assertNotEqual(plaintext, ciphertext)
            self.assertEqual(plaintext, cipher.decrypt(ciphertext))

        # Test string/byte array.
        util.test_cipher_encryption(self, cipher, "meow" * 500)
        util.test_cipher_encryption(self, cipher,
                                    random.Random.new().read(2000))

        # Test encoders.
        cipher.set_encoding(base_encoders.NullEncoder)
        util.test_cipher_encryption(self, cipher, "wruff" * 400)
        util.test_cipher_encryption(self, cipher,
                                    random.Random.new().read(2000))

        cipher.set_encoding(binary_encoders.Base64Encoder)
        util.test_cipher_encryption(self, cipher, "meow" * 500)
        util.test_cipher_encryption(self, cipher,
                                    random.Random.new().read(2000))

        cipher.set_encoding(binary_encoders.URLSafeBase64Encoder)
        util.test_cipher_encryption(self, cipher, "wruff" * 400)
        util.test_cipher_encryption(self, cipher,
                                    random.Random.new().read(2000))

        # Test keys and instances.
        cipher1 = xor_cipher.XORCipher("fishsticks")
        cipher2 = xor_cipher.XORCipher("fishsticks")
        cipher3 = xor_cipher.XORCipher("puppychow")

        self.assertEqual(cipher1.encrypt("meow"), cipher2.encrypt("meow"))
        self.assertNotEqual(cipher2.encrypt("wruff"), cipher3.encrypt("wruff"))
Exemple #5
0
 def test_set_encoding(self):
     cipher = xor_cipher.XORCipher()
     self._test_set_encoding(cipher)
Exemple #6
0
 def test_key_property(self):
     cipher = xor_cipher.XORCipher(key="fishsticks")
     self._test_init_key(cipher, "fishsticks")
     self._test_key_setter(cipher, "puppychow", invalid_key="puppy" * 12)
Exemple #7
0
 def test_init(self):
     cipher = xor_cipher.XORCipher()
     self._test_init_no_args(cipher)