From c73c9be14feb58fcac9bb2298ed5372d6b60a59d Mon Sep 17 00:00:00 2001 From: Noel Power Date: Wed, 8 Aug 2018 12:51:20 +0100 Subject: [PATCH] python/samba/tests: port samba.tests.py_credentials for py2/py3 compat + ord takes 'str' param + unicode doesn't exist in py3 + py3 bytes class doesn't have encode method, try to ensure py2 code runs unchanged while adapting code (by using get_string) Signed-off-by: Noel Power Reviewed-by: Andrew Bartlett --- python/samba/tests/py_credentials.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/python/samba/tests/py_credentials.py b/python/samba/tests/py_credentials.py index bcf5d3a0a29..b16c726a023 100644 --- a/python/samba/tests/py_credentials.py +++ b/python/samba/tests/py_credentials.py @@ -39,8 +39,11 @@ from samba.dsdb import ( from samba.ndr import ndr_pack from samba.samdb import SamDB from samba import NTSTATUSError, ntstatus +from samba.compat import get_string + import ctypes + """ Integration tests for pycredentials """ @@ -48,7 +51,6 @@ Integration tests for pycredentials MACHINE_NAME = "PCTM" USER_NAME = "PCTU" - class PyCredentialsTests(TestCase): def setUp(self): @@ -232,10 +234,10 @@ class PyCredentialsTests(TestCase): newpass = samba.generate_random_password(PWD_LEN, PWD_LEN) encoded = newpass.encode('utf-16-le') pwd_len = len(encoded) - filler = [ord(x) for x in os.urandom(DATA_LEN - pwd_len)] + filler = [x if isinstance(x, int) else ord(x) for x in os.urandom(DATA_LEN - pwd_len)] pwd = netlogon.netr_CryptPassword() pwd.length = pwd_len - pwd.data = filler + [ord(x) for x in encoded] + pwd.data = filler + [x if isinstance(x, int) else ord(x) for x in encoded] self.machine_creds.encrypt_netr_crypt_password(pwd) c.netr_ServerPasswordSet2(self.server, self.machine_creds.get_workstation(), @@ -265,9 +267,7 @@ class PyCredentialsTests(TestCase): # run failed delete_force(self.ldb, self.machine_dn) - utf16pw = unicode( - '"' + self.machine_pass.encode('utf-8') + '"', 'utf-8' - ).encode('utf-16-le') + utf16pw = ('"%s"' % get_string(self.machine_pass)).encode('utf-16-le') self.ldb.add({ "dn": self.machine_dn, "objectclass": "computer", @@ -295,9 +295,7 @@ class PyCredentialsTests(TestCase): # run failed delete_force(self.ldb, self.user_dn) - utf16pw = unicode( - '"' + self.user_pass.encode('utf-8') + '"', 'utf-8' - ).encode('utf-16-le') + utf16pw = ('"%s"' % get_string(self.user_pass)).encode('utf-16-le') self.ldb.add({ "dn": self.user_dn, "objectclass": "user", @@ -317,7 +315,7 @@ class PyCredentialsTests(TestCase): def get_authenticator(self, c): auth = self.machine_creds.new_client_authenticator() current = netr_Authenticator() - current.cred.data = [ord(x) for x in auth["credential"]] + current.cred.data = [x if isinstance(x, int) else ord(x) for x in auth["credential"]] current.timestamp = auth["timestamp"] subsequent = netr_Authenticator() @@ -367,10 +365,10 @@ def samlogon_logon_info(domain_name, computer_name, creds, logon = netlogon.netr_NetworkInfo() - logon.challenge = [ord(x) for x in challenge] + logon.challenge = [x if isinstance(x, int) else ord(x) for x in challenge] logon.nt = netlogon.netr_ChallengeResponse() logon.nt.length = len(response["nt_response"]) - logon.nt.data = [ord(x) for x in response["nt_response"]] + logon.nt.data = [x if isinstance(x, int) else ord(x) for x in response["nt_response"]] logon.identity_info = netlogon.netr_IdentityInfo() (username, domain) = creds.get_ntlm_username_domain() -- 2.34.1