python/samba/tests: port samba.tests.py_credentials for py2/py3 compat
authorNoel Power <noel.power@suse.com>
Wed, 8 Aug 2018 11:51:20 +0000 (12:51 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 5 Sep 2018 21:27:12 +0000 (23:27 +0200)
+ 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 <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/py_credentials.py

index bcf5d3a0a296678ebf390e9f58b723ef1a603710..b16c726a023a890fdcf51710fa40cc85fcbf573b 100644 (file)
@@ -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()