mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-27 10:40:22 -07:00
Updated parsing/vault/test_vault.py to use the fake byte literals in six when using hexlify.
This was to fix the `TypeError: 'str' does not support the buffer interface` errors.
This commit is contained in:
parent
f8fe1357b0
commit
28443cf0a9
1 changed files with 14 additions and 12 deletions
|
@ -24,6 +24,8 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import six
|
||||||
|
|
||||||
from binascii import unhexlify
|
from binascii import unhexlify
|
||||||
from binascii import hexlify
|
from binascii import hexlify
|
||||||
from nose.plugins.skip import SkipTest
|
from nose.plugins.skip import SkipTest
|
||||||
|
@ -63,13 +65,13 @@ class TestVaultLib(unittest.TestCase):
|
||||||
'decrypt',
|
'decrypt',
|
||||||
'_add_header',
|
'_add_header',
|
||||||
'_split_header',]
|
'_split_header',]
|
||||||
for slot in slots:
|
for slot in slots:
|
||||||
assert hasattr(v, slot), "VaultLib is missing the %s method" % slot
|
assert hasattr(v, slot), "VaultLib is missing the %s method" % slot
|
||||||
|
|
||||||
def test_is_encrypted(self):
|
def test_is_encrypted(self):
|
||||||
v = VaultLib(None)
|
v = VaultLib(None)
|
||||||
assert not v.is_encrypted("foobar"), "encryption check on plaintext failed"
|
assert not v.is_encrypted("foobar"), "encryption check on plaintext failed"
|
||||||
data = "$ANSIBLE_VAULT;9.9;TEST\n%s" % hexlify("ansible")
|
data = "$ANSIBLE_VAULT;9.9;TEST\n%s" % hexlify(six.b("ansible"))
|
||||||
assert v.is_encrypted(data), "encryption check on headered text failed"
|
assert v.is_encrypted(data), "encryption check on headered text failed"
|
||||||
|
|
||||||
def test_add_header(self):
|
def test_add_header(self):
|
||||||
|
@ -82,15 +84,15 @@ class TestVaultLib(unittest.TestCase):
|
||||||
header = lines[0]
|
header = lines[0]
|
||||||
assert header.endswith(';TEST'), "header does end with cipher name"
|
assert header.endswith(';TEST'), "header does end with cipher name"
|
||||||
header_parts = header.split(';')
|
header_parts = header.split(';')
|
||||||
assert len(header_parts) == 3, "header has the wrong number of parts"
|
assert len(header_parts) == 3, "header has the wrong number of parts"
|
||||||
assert header_parts[0] == '$ANSIBLE_VAULT', "header does not start with $ANSIBLE_VAULT"
|
assert header_parts[0] == '$ANSIBLE_VAULT', "header does not start with $ANSIBLE_VAULT"
|
||||||
assert header_parts[1] == v.version, "header version is incorrect"
|
assert header_parts[1] == v.version, "header version is incorrect"
|
||||||
assert header_parts[2] == 'TEST', "header does end with cipher name"
|
assert header_parts[2] == 'TEST', "header does end with cipher name"
|
||||||
|
|
||||||
def test_split_header(self):
|
def test_split_header(self):
|
||||||
v = VaultLib('ansible')
|
v = VaultLib('ansible')
|
||||||
data = "$ANSIBLE_VAULT;9.9;TEST\nansible"
|
data = "$ANSIBLE_VAULT;9.9;TEST\nansible"
|
||||||
rdata = v._split_header(data)
|
rdata = v._split_header(data)
|
||||||
lines = rdata.split('\n')
|
lines = rdata.split('\n')
|
||||||
assert lines[0] == "ansible"
|
assert lines[0] == "ansible"
|
||||||
assert v.cipher_name == 'TEST', "cipher name was not set"
|
assert v.cipher_name == 'TEST', "cipher name was not set"
|
||||||
|
@ -104,7 +106,7 @@ class TestVaultLib(unittest.TestCase):
|
||||||
enc_data = v.encrypt("foobar")
|
enc_data = v.encrypt("foobar")
|
||||||
dec_data = v.decrypt(enc_data)
|
dec_data = v.decrypt(enc_data)
|
||||||
assert enc_data != "foobar", "encryption failed"
|
assert enc_data != "foobar", "encryption failed"
|
||||||
assert dec_data == "foobar", "decryption failed"
|
assert dec_data == "foobar", "decryption failed"
|
||||||
|
|
||||||
def test_encrypt_decrypt_aes256(self):
|
def test_encrypt_decrypt_aes256(self):
|
||||||
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
|
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
|
||||||
|
@ -114,20 +116,20 @@ class TestVaultLib(unittest.TestCase):
|
||||||
enc_data = v.encrypt("foobar")
|
enc_data = v.encrypt("foobar")
|
||||||
dec_data = v.decrypt(enc_data)
|
dec_data = v.decrypt(enc_data)
|
||||||
assert enc_data != "foobar", "encryption failed"
|
assert enc_data != "foobar", "encryption failed"
|
||||||
assert dec_data == "foobar", "decryption failed"
|
assert dec_data == "foobar", "decryption failed"
|
||||||
|
|
||||||
def test_encrypt_encrypted(self):
|
def test_encrypt_encrypted(self):
|
||||||
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
|
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
|
||||||
raise SkipTest
|
raise SkipTest
|
||||||
v = VaultLib('ansible')
|
v = VaultLib('ansible')
|
||||||
v.cipher_name = 'AES'
|
v.cipher_name = 'AES'
|
||||||
data = "$ANSIBLE_VAULT;9.9;TEST\n%s" % hexlify("ansible")
|
data = "$ANSIBLE_VAULT;9.9;TEST\n%s" % hexlify(six.b("ansible"))
|
||||||
error_hit = False
|
error_hit = False
|
||||||
try:
|
try:
|
||||||
enc_data = v.encrypt(data)
|
enc_data = v.encrypt(data)
|
||||||
except errors.AnsibleError as e:
|
except errors.AnsibleError as e:
|
||||||
error_hit = True
|
error_hit = True
|
||||||
assert error_hit, "No error was thrown when trying to encrypt data with a header"
|
assert error_hit, "No error was thrown when trying to encrypt data with a header"
|
||||||
|
|
||||||
def test_decrypt_decrypted(self):
|
def test_decrypt_decrypted(self):
|
||||||
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
|
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
|
||||||
|
@ -139,7 +141,7 @@ class TestVaultLib(unittest.TestCase):
|
||||||
dec_data = v.decrypt(data)
|
dec_data = v.decrypt(data)
|
||||||
except errors.AnsibleError as e:
|
except errors.AnsibleError as e:
|
||||||
error_hit = True
|
error_hit = True
|
||||||
assert error_hit, "No error was thrown when trying to decrypt data without a header"
|
assert error_hit, "No error was thrown when trying to decrypt data without a header"
|
||||||
|
|
||||||
def test_cipher_not_set(self):
|
def test_cipher_not_set(self):
|
||||||
# not setting the cipher should default to AES256
|
# not setting the cipher should default to AES256
|
||||||
|
@ -152,5 +154,5 @@ class TestVaultLib(unittest.TestCase):
|
||||||
enc_data = v.encrypt(data)
|
enc_data = v.encrypt(data)
|
||||||
except errors.AnsibleError as e:
|
except errors.AnsibleError as e:
|
||||||
error_hit = True
|
error_hit = True
|
||||||
assert not error_hit, "An error was thrown when trying to encrypt data without the cipher set"
|
assert not error_hit, "An error was thrown when trying to encrypt data without the cipher set"
|
||||||
assert v.cipher_name == "AES256", "cipher name is not set to AES256: %s" % v.cipher_name
|
assert v.cipher_name == "AES256", "cipher name is not set to AES256: %s" % v.cipher_name
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue