From f5359c310f74b28bf2af9fc73becf55bec6b0c55 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Wed, 26 Feb 2014 13:38:53 -0500 Subject: [PATCH] Add negative tests for VaultLib --- test/units/TestVault.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/units/TestVault.py b/test/units/TestVault.py index 019eb8c8418..cc43112b1fe 100644 --- a/test/units/TestVault.py +++ b/test/units/TestVault.py @@ -11,6 +11,7 @@ from binascii import unhexlify from binascii import hexlify from nose.plugins.skip import SkipTest +from ansible import errors from ansible.utils.vault import VaultLib # AES IMPORTS try: @@ -73,3 +74,37 @@ class TestVaultLib(TestCase): assert enc_data != "foobar", "encryption failed" assert dec_data == "foobar", "decryption failed" + @unittest.skipIf(not HAS_AES, "aes not installed") + def test_encrypt_encrypted(self): + v = VaultLib('ansible') + v.cipher_name = 'AES' + data = "$ANSIBLE_VAULT;9.9;TEST\n%s" % hexlify("ansible") + error_hit = False + try: + enc_data = v.encrypt(data) + except errors.AnsibleError, e: + error_hit = True + assert error_hit, "No error was thrown when trying to encrypt data with a header" + + def test_decrypt_decrypted(self): + v = VaultLib('ansible') + data = "ansible" + error_hit = False + try: + dec_data = v.decrypt(data) + except errors.AnsibleError, e: + error_hit = True + assert error_hit, "No error was thrown when trying to decrypt data without a header" + + @unittest.skipIf(not HAS_AES, "aes not installed") + def test_cipher_not_set(self): + v = VaultLib('ansible') + data = "ansible" + error_hit = False + try: + enc_data = v.encrypt(data) + except errors.AnsibleError, e: + error_hit = True + assert error_hit, "No error was thrown when trying to encrypt data without the cipher set" + +