1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 01:48:00 +00:00

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.
This commit is contained in:
Simon Tatham 2019-01-23 23:40:32 +00:00
parent 621f8f4314
commit 0e9ad99c04
2 changed files with 12 additions and 20 deletions

View File

@ -1136,7 +1136,7 @@ class crypt(MyTestBase):
hashalg = 'sha256' hashalg = 'sha256'
seed = b"hello, world" seed = b"hello, world"
entropy = b'1234567890' * 100 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 # Replicate the generation of some random numbers. to ensure
# they really are the hashes of what they're supposed to be. # they really are the hashes of what they're supposed to be.
@ -1150,21 +1150,21 @@ class crypt(MyTestBase):
data3 = prng_read(pr, 128) data3 = prng_read(pr, 128)
key1 = hash_str(hashalg, b'R' + seed) key1 = hash_str(hashalg, b'R' + seed)
expected_data1 = ''.join( expected_data1 = b''.join(
rev(hash_str(hashalg, key1 + b'G' + ssh2_mpint(counter))) rev(hash_str(hashalg, key1 + b'G' + ssh2_mpint(counter)))
for counter in range(4)) for counter in range(4))
# After prng_read finishes, we expect the PRNG to have # After prng_read finishes, we expect the PRNG to have
# automatically reseeded itself, so that if its internal state # automatically reseeded itself, so that if its internal state
# is revealed then the previous output can't be reconstructed. # is revealed then the previous output can't be reconstructed.
key2 = hash_str(hashalg, key1 + b'R') key2 = hash_str(hashalg, key1 + b'R')
expected_data2 = ''.join( expected_data2 = b''.join(
rev(hash_str(hashalg, key2 + b'G' + ssh2_mpint(counter))) rev(hash_str(hashalg, key2 + b'G' + ssh2_mpint(counter)))
for counter in range(4,8)) for counter in range(4,8))
# There will have been another reseed after the second # There will have been another reseed after the second
# prng_read, and then another due to the entropy. # prng_read, and then another due to the entropy.
key3 = hash_str(hashalg, key2 + b'R') key3 = hash_str(hashalg, key2 + b'R')
key4 = hash_str(hashalg, key3 + b'R' + hash_str(hashalg, entropy)) 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))) rev(hash_str(hashalg, key4 + b'G' + ssh2_mpint(counter)))
for counter in range(8,12)) for counter in range(8,12))

View File

@ -3,6 +3,7 @@ import os
import numbers import numbers
import subprocess import subprocess
import re import re
import struct
from binascii import hexlify from binascii import hexlify
# Expect to be run from the 'test' subdirectory, one level down from # 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") arg = arg.encode("UTF-8")
return arg return arg
# Another pair of P2/P3 compatibility shims, to give a stream of def bytevals(b):
# integers corresponding to the byte values in a bytes object, and to return struct.unpack("{:d}B".format(len(b)), b)
# take an integer and return a bytes object containing a byte with def valbytes(b):
# that value. b = list(b)
if b'A'[0] != b'A': return struct.pack("{:d}B".format(len(b)), *b)
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)
class ChildProcess(object): class ChildProcess(object):
def __init__(self): def __init__(self):
@ -140,14 +132,14 @@ def make_argword(arg, argtype, fnname, argindex, to_preserve):
def make_retval(rettype, word, unpack_strings): def make_retval(rettype, word, unpack_strings):
if rettype.startswith("opt_"): if rettype.startswith("opt_"):
if word == "NULL": if word == b"NULL":
return None return None
rettype = rettype[4:] rettype = rettype[4:]
if rettype == "val_string" and unpack_strings: if rettype == "val_string" and unpack_strings:
retwords = childprocess.funcall("getstring", [word]) retwords = childprocess.funcall("getstring", [word])
childprocess.funcall("free", [word]) childprocess.funcall("free", [word])
return re.sub(b"%[0-9A-F][0-9A-F]", 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]) retwords[0])
if rettype.startswith("val_"): if rettype.startswith("val_"):
return Value(rettype, word) return Value(rettype, word)