diff --git a/azurelinuxagent/common/utils/textutil.py b/azurelinuxagent/common/utils/textutil.py index 59b8fe7..c683a13 100644 --- a/azurelinuxagent/common/utils/textutil.py +++ b/azurelinuxagent/common/utils/textutil.py @@ -267,6 +267,9 @@ def gen_password_hash(password, crypt_id, salt_len): collection = string.ascii_letters + string.digits salt = ''.join(random.choice(collection) for _ in range(salt_len)) salt = "${0}${1}".format(crypt_id, salt) + if sys.version_info[0] == 2: + # if python 2.*, encode to type 'str' to prevent Unicode Encode Error from crypt.crypt + password = password.encode('utf-8') return crypt.crypt(password, salt) diff --git a/tests/utils/test_passwords.txt b/tests/utils/test_passwords.txt new file mode 100644 index 0000000..0d995ef --- /dev/null +++ b/tests/utils/test_passwords.txt @@ -0,0 +1,4 @@ +김치 +करी +hamburger +café \ No newline at end of file diff --git a/tests/utils/test_text_util.py b/tests/utils/test_text_util.py index dc3de85..6f204c7 100644 --- a/tests/utils/test_text_util.py +++ b/tests/utils/test_text_util.py @@ -23,12 +23,16 @@ from azurelinuxagent.common.future import ustr import azurelinuxagent.common.utils.textutil as textutil from azurelinuxagent.common.utils.textutil import Version + class TestTextUtil(AgentTestCase): def test_get_password_hash(self): - password_hash = textutil.gen_password_hash("asdf", 6, 10) - self.assertNotEquals(None, password_hash) - password_hash = textutil.gen_password_hash("asdf", 6, 0) - self.assertNotEquals(None, password_hash) + with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_passwords.txt'), 'rb') as in_file: + for data in in_file: + # Remove bom on bytes data before it is converted into string. + data = textutil.remove_bom(data) + data = ustr(data, encoding='utf-8') + password_hash = textutil.gen_password_hash(data, 6, 10) + self.assertNotEquals(None, password_hash) def test_remove_bom(self): #Test bom could be removed