From 0b6b4b69e909b12bb87b159622514165109eb5f8 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Tue, 22 Sep 2020 08:36:39 -0400 Subject: [PATCH] Port test_immutable to Python 3 --- newsfragments/3431.minor | 0 src/allmydata/test/common.py | 6 +++--- src/allmydata/test/common_util.py | 20 +++++++++++------ src/allmydata/test/test_common_util.py | 30 ++++++++++++++++++++++++++ src/allmydata/test/test_immutable.py | 27 ++++++++++++++++------- src/allmydata/util/_python3.py | 2 ++ 6 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 newsfragments/3431.minor create mode 100644 src/allmydata/test/test_common_util.py diff --git a/newsfragments/3431.minor b/newsfragments/3431.minor new file mode 100644 index 000000000..e69de29bb diff --git a/src/allmydata/test/common.py b/src/allmydata/test/common.py index b69d58ab9..8560edd78 100644 --- a/src/allmydata/test/common.py +++ b/src/allmydata/test/common.py @@ -781,7 +781,7 @@ def create_mutable_filenode(contents, mdmf=False, all_contents=None): return filenode -TEST_DATA="\x02"*(Uploader.URI_LIT_SIZE_THRESHOLD+1) +TEST_DATA=b"\x02"*(Uploader.URI_LIT_SIZE_THRESHOLD+1) class WebErrorMixin(object): @@ -958,12 +958,12 @@ def _corrupt_offset_of_block_hashes_to_truncate_crypttext_hashes(data, debug=Fal assert sharevernum in (1, 2), "This test is designed to corrupt immutable shares of v1 or v2 in specific ways." if sharevernum == 1: curval = struct.unpack(">L", data[0x0c+0x18:0x0c+0x18+4])[0] - newval = random.randrange(0, max(1, (curval/hashutil.CRYPTO_VAL_SIZE)/2))*hashutil.CRYPTO_VAL_SIZE + newval = random.randrange(0, max(1, (curval/hashutil.CRYPTO_VAL_SIZE)//2))*hashutil.CRYPTO_VAL_SIZE newvalstr = struct.pack(">L", newval) return data[:0x0c+0x18]+newvalstr+data[0x0c+0x18+4:] else: curval = struct.unpack(">Q", data[0x0c+0x2c:0x0c+0x2c+8])[0] - newval = random.randrange(0, max(1, (curval/hashutil.CRYPTO_VAL_SIZE)/2))*hashutil.CRYPTO_VAL_SIZE + newval = random.randrange(0, max(1, (curval/hashutil.CRYPTO_VAL_SIZE)//2))*hashutil.CRYPTO_VAL_SIZE newvalstr = struct.pack(">Q", newval) return data[:0x0c+0x2c]+newvalstr+data[0x0c+0x2c+8:] diff --git a/src/allmydata/test/common_util.py b/src/allmydata/test/common_util.py index e1943e60a..33e353f7a 100644 --- a/src/allmydata/test/common_util.py +++ b/src/allmydata/test/common_util.py @@ -1,4 +1,14 @@ +from __future__ import absolute_import +from __future__ import division from __future__ import print_function +from __future__ import unicode_literals + +from future.utils import PY2, bord, bchr, binary_type +if PY2: + from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401 + + # XXX this is a hack that makes some tests pass on Python3, remove in the future + from ..scripts import runner import os from random import randrange @@ -9,10 +19,6 @@ from twisted.trial import unittest from ..util.assertutil import precondition from allmydata.util.encodingutil import get_io_encoding -from future.utils import PY2 -if PY2: # XXX this is a hack that makes some tests pass on Python3, remove - # in the future - from ..scripts import runner # Imported for backwards compatibility: from .common_py3 import ( SignalMixin, skip_if_cannot_represent_filename, ReallyEqualMixin, ShouldFailMixin @@ -57,9 +63,10 @@ class DevNullDictionary(dict): return def insecurerandstr(n): - return ''.join(map(chr, map(randrange, [0]*n, [256]*n))) + return b''.join(map(bchr, map(randrange, [0]*n, [256]*n))) def flip_bit(good, which): + # TODO Probs need to update with bchr/bord as with flip_one_bit, below. # flip the low-order bit of good[which] if which == -1: pieces = good[:which], good[-1:], "" @@ -70,10 +77,11 @@ def flip_bit(good, which): def flip_one_bit(s, offset=0, size=None): """ flip one random bit of the string s, in a byte greater than or equal to offset and less than offset+size. """ + assert isinstance(s, binary_type) if size is None: size=len(s)-offset i = randrange(offset, offset+size) - result = s[:i] + chr(ord(s[i])^(0x01<