create cisco type 5 filters (#39901)

This commit is contained in:
Ken Celenza 2018-05-22 08:35:54 -04:00 committed by John R Barker
parent c29b03e77b
commit dd02a4e943
2 changed files with 143 additions and 3 deletions

View file

@ -20,8 +20,11 @@ __metaclass__ = type
import os
import sys
import pytest
from ansible.compat.tests import unittest
from ansible.plugins.filter.network import parse_xml
from ansible.plugins.filter.network import parse_xml, type5_pw, hash_salt, comp_type5
from ansible.errors import AnsibleFilterError
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'network')
@ -78,3 +81,87 @@ class TestNetworkParseFilter(unittest.TestCase):
with self.assertRaises(Exception) as e:
parse_xml(output, spec_file_path)
self.assertEqual("parse_xml works on string input, but given input of : %s" % type(output), str(e.exception))
class TestNetworkType5(unittest.TestCase):
def test_defined_salt_success(self):
password = 'cisco'
salt = 'nTc1'
expected = '$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.'
parsed = type5_pw(password, salt)
self.assertEqual(parsed, expected)
def test_undefined_salt_success(self):
password = 'cisco'
parsed = type5_pw(password)
self.assertEqual(len(parsed), 30)
def test_wrong_data_type(self):
with self.assertRaises(Exception) as e:
type5_pw([])
self.assertEqual("type5_pw password input should be a string, but was given a input of list", str(e.exception))
with self.assertRaises(Exception) as e:
type5_pw({})
self.assertEqual("type5_pw password input should be a string, but was given a input of dict", str(e.exception))
with self.assertRaises(Exception) as e:
type5_pw('pass', [])
self.assertEqual("type5_pw salt input should be a string, but was given a input of list", str(e.exception))
with self.assertRaises(Exception) as e:
type5_pw('pass', {})
self.assertEqual("type5_pw salt input should be a string, but was given a input of dict", str(e.exception))
def test_bad_salt_char(self):
with self.assertRaises(Exception) as e:
type5_pw('password', '*()')
self.assertEqual("type5_pw salt used inproper characters, must be one of "
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./", str(e.exception))
with self.assertRaises(Exception) as e:
type5_pw('password', 'asd$')
self.assertEqual("type5_pw salt used inproper characters, must be one of "
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./", str(e.exception))
class TestHashSalt(unittest.TestCase):
def test_retrieve_salt(self):
password = '$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.'
parsed = hash_salt(password)
self.assertEqual(parsed, 'nTc1')
password = '$2y$14$wHhBmAgOMZEld9iJtV.'
parsed = hash_salt(password)
self.assertEqual(parsed, '14')
def test_unparseable_salt(self):
password = '$nTc1$Z28sUTcWfXlvVe2x.3XAa.'
with self.assertRaises(Exception) as e:
parsed = hash_salt(password)
self.assertEqual("Could not parse salt out password correctly from $nTc1$Z28sUTcWfXlvVe2x.3XAa.", str(e.exception))
class TestCompareType5(unittest.TestCase):
def test_compare_type5_boolean(self):
unencrypted_password = 'cisco'
encrypted_password = '$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.'
parsed = comp_type5(unencrypted_password, encrypted_password)
self.assertEqual(parsed, True)
def test_compare_type5_string(self):
unencrypted_password = 'cisco'
encrypted_password = '$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.'
parsed = comp_type5(unencrypted_password, encrypted_password, True)
self.assertEqual(parsed, '$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.')
def test_compate_type5_fail(self):
unencrypted_password = 'invalid_password'
encrypted_password = '$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.'
parsed = comp_type5(unencrypted_password, encrypted_password)
self.assertEqual(parsed, False)