From 0e9ad99c042648ee4fecd9291bc6cb1bf2d76009 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 23 Jan 2019 23:40:32 +0000 Subject: [PATCH] testcrypt / cryptsuite: another set of Python 3 fixes. One of these days I'll manage not to mess this up in every new test I add ... perhaps. --- test/cryptsuite.py | 8 ++++---- test/testcrypt.py | 24 ++++++++---------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/test/cryptsuite.py b/test/cryptsuite.py index 0ee774eb..d34dbfd0 100755 --- a/test/cryptsuite.py +++ b/test/cryptsuite.py @@ -1136,7 +1136,7 @@ class crypt(MyTestBase): hashalg = 'sha256' seed = b"hello, world" entropy = b'1234567890' * 100 - rev = lambda s: b''.join(reversed(s)) + rev = lambda s: valbytes(reversed(bytevals(s))) # Replicate the generation of some random numbers. to ensure # they really are the hashes of what they're supposed to be. @@ -1150,21 +1150,21 @@ class crypt(MyTestBase): data3 = prng_read(pr, 128) key1 = hash_str(hashalg, b'R' + seed) - expected_data1 = ''.join( + expected_data1 = b''.join( rev(hash_str(hashalg, key1 + b'G' + ssh2_mpint(counter))) for counter in range(4)) # After prng_read finishes, we expect the PRNG to have # automatically reseeded itself, so that if its internal state # is revealed then the previous output can't be reconstructed. key2 = hash_str(hashalg, key1 + b'R') - expected_data2 = ''.join( + expected_data2 = b''.join( rev(hash_str(hashalg, key2 + b'G' + ssh2_mpint(counter))) for counter in range(4,8)) # There will have been another reseed after the second # prng_read, and then another due to the entropy. key3 = hash_str(hashalg, key2 + b'R') key4 = hash_str(hashalg, key3 + b'R' + hash_str(hashalg, entropy)) - expected_data3 = ''.join( + expected_data3 = b''.join( rev(hash_str(hashalg, key4 + b'G' + ssh2_mpint(counter))) for counter in range(8,12)) diff --git a/test/testcrypt.py b/test/testcrypt.py index 20cb6582..95d311d9 100644 --- a/test/testcrypt.py +++ b/test/testcrypt.py @@ -3,6 +3,7 @@ import os import numbers import subprocess import re +import struct from binascii import hexlify # Expect to be run from the 'test' subdirectory, one level down from @@ -15,20 +16,11 @@ def unicode_to_bytes(arg): arg = arg.encode("UTF-8") return arg -# Another pair of P2/P3 compatibility shims, to give a stream of -# integers corresponding to the byte values in a bytes object, and to -# take an integer and return a bytes object containing a byte with -# that value. -if b'A'[0] != b'A': - def bytevals(arg): - return arg # in P3 this is a no-op - def byte2str(arg): - return bytes([arg]) -else: - def bytevals(arg): - return map(ord, arg) # in P2 you have to use ord() - def byte2str(arg): - return chr(arg) +def bytevals(b): + return struct.unpack("{:d}B".format(len(b)), b) +def valbytes(b): + b = list(b) + return struct.pack("{:d}B".format(len(b)), *b) class ChildProcess(object): def __init__(self): @@ -140,14 +132,14 @@ def make_argword(arg, argtype, fnname, argindex, to_preserve): def make_retval(rettype, word, unpack_strings): if rettype.startswith("opt_"): - if word == "NULL": + if word == b"NULL": return None rettype = rettype[4:] if rettype == "val_string" and unpack_strings: retwords = childprocess.funcall("getstring", [word]) childprocess.funcall("free", [word]) return re.sub(b"%[0-9A-F][0-9A-F]", - lambda m: byte2str(int(m.group(0)[1:], 16)), + lambda m: valbytes([int(m.group(0)[1:], 16)]), retwords[0]) if rettype.startswith("val_"): return Value(rettype, word)