Merge pull request #823 from tahoe-lafs/3431.port-test_immutable

Port test_immutable to Python 3
This commit is contained in:
Chad Whitacre 2020-09-28 08:34:03 -04:00 committed by GitHub
commit afd28f3402
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 17 deletions

0
newsfragments/3431.minor Normal file
View File

View File

@ -781,7 +781,7 @@ def create_mutable_filenode(contents, mdmf=False, all_contents=None):
return filenode return filenode
TEST_DATA="\x02"*(Uploader.URI_LIT_SIZE_THRESHOLD+1) TEST_DATA=b"\x02"*(Uploader.URI_LIT_SIZE_THRESHOLD+1)
class WebErrorMixin(object): 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." assert sharevernum in (1, 2), "This test is designed to corrupt immutable shares of v1 or v2 in specific ways."
if sharevernum == 1: if sharevernum == 1:
curval = struct.unpack(">L", data[0x0c+0x18:0x0c+0x18+4])[0] 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) newvalstr = struct.pack(">L", newval)
return data[:0x0c+0x18]+newvalstr+data[0x0c+0x18+4:] return data[:0x0c+0x18]+newvalstr+data[0x0c+0x18+4:]
else: else:
curval = struct.unpack(">Q", data[0x0c+0x2c:0x0c+0x2c+8])[0] 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) newvalstr = struct.pack(">Q", newval)
return data[:0x0c+0x2c]+newvalstr+data[0x0c+0x2c+8:] return data[:0x0c+0x2c]+newvalstr+data[0x0c+0x2c+8:]

View File

@ -8,12 +8,10 @@ from twisted.internet import reactor, defer
from twisted.trial import unittest from twisted.trial import unittest
from ..util.assertutil import precondition 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 from ..scripts import runner
from allmydata.util.encodingutil import get_io_encoding
# Imported for backwards compatibility: # Imported for backwards compatibility:
from future.utils import bord, bchr, binary_type
from .common_py3 import ( from .common_py3 import (
SignalMixin, skip_if_cannot_represent_filename, ReallyEqualMixin, ShouldFailMixin SignalMixin, skip_if_cannot_represent_filename, ReallyEqualMixin, ShouldFailMixin
) )
@ -57,9 +55,10 @@ class DevNullDictionary(dict):
return return
def insecurerandstr(n): 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): 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] # flip the low-order bit of good[which]
if which == -1: if which == -1:
pieces = good[:which], good[-1:], "" pieces = good[:which], good[-1:], ""
@ -70,10 +69,11 @@ def flip_bit(good, which):
def flip_one_bit(s, offset=0, size=None): 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 """ flip one random bit of the string s, in a byte greater than or equal to offset and less
than offset+size. """ than offset+size. """
precondition(isinstance(s, binary_type))
if size is None: if size is None:
size=len(s)-offset size=len(s)-offset
i = randrange(offset, offset+size) i = randrange(offset, offset+size)
result = s[:i] + chr(ord(s[i])^(0x01<<randrange(0, 8))) + s[i+1:] result = s[:i] + bchr(bord(s[i])^(0x01<<randrange(0, 8))) + s[i+1:]
assert result != s, "Internal error -- flip_one_bit() produced the same string as its input: %s == %s" % (result, s) assert result != s, "Internal error -- flip_one_bit() produced the same string as its input: %s == %s" % (result, s)
return result return result

View File

@ -0,0 +1,29 @@
"""
This module has been ported to Python 3.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2
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
import random
import unittest
from allmydata.test.common_util import flip_one_bit
class TestFlipOneBit(unittest.TestCase):
def setUp(self):
random.seed(42) # I tried using version=1 on PY3 to avoid the if below, to no avail.
def test_accepts_byte_string(self):
actual = flip_one_bit(b'foo')
self.assertEqual(actual, b'fno' if PY2 else b'fom')
def test_rejects_unicode_string(self):
self.assertRaises(AssertionError, flip_one_bit, u'foo')

View File

@ -1,3 +1,14 @@
"""
This module has been ported to Python 3.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2
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
import random import random
@ -79,7 +90,7 @@ class TestShareFinder(unittest.TestCase):
# ever", and then immediately tell them "oh, and here's # ever", and then immediately tell them "oh, and here's
# another share", then you lose. # another share", then you lose.
rcap = uri.CHKFileURI('a'*32, 'a'*32, 3, 99, 100) rcap = uri.CHKFileURI(b'a'*32, b'a'*32, 3, 99, 100)
vcap = rcap.get_verify_cap() vcap = rcap.get_verify_cap()
class MockBuckets(object): class MockBuckets(object):
@ -88,8 +99,8 @@ class TestShareFinder(unittest.TestCase):
class MockServer(object): class MockServer(object):
def __init__(self, buckets): def __init__(self, buckets):
self.version = { self.version = {
'http://allmydata.org/tahoe/protocols/storage/v1': { b'http://allmydata.org/tahoe/protocols/storage/v1': {
"tolerates-immutable-read-overrun": True b"tolerates-immutable-read-overrun": True
} }
} }
self.buckets = buckets self.buckets = buckets
@ -126,9 +137,9 @@ class TestShareFinder(unittest.TestCase):
mockserver1 = MockServer({1: MockBuckets(), 2: MockBuckets()}) mockserver1 = MockServer({1: MockBuckets(), 2: MockBuckets()})
mockserver2 = MockServer({}) mockserver2 = MockServer({})
mockserver3 = MockServer({3: MockBuckets()}) mockserver3 = MockServer({3: MockBuckets()})
servers = [ NoNetworkServer("ms1", mockserver1), servers = [ NoNetworkServer(b"ms1", mockserver1),
NoNetworkServer("ms2", mockserver2), NoNetworkServer(b"ms2", mockserver2),
NoNetworkServer("ms3", mockserver3), ] NoNetworkServer(b"ms3", mockserver3), ]
mockstoragebroker = MockStorageBroker(servers) mockstoragebroker = MockStorageBroker(servers)
mockdownloadstatus = MockDownloadStatus() mockdownloadstatus = MockDownloadStatus()
mocknode = MockNode(check_reneging=True, check_fetch_failed=True) mocknode = MockNode(check_reneging=True, check_fetch_failed=True)
@ -155,7 +166,7 @@ class Test(GridTestMixin, unittest.TestCase, common.ShouldFailMixin):
# Tests that need to test servers of happiness using this should # Tests that need to test servers of happiness using this should
# set their own value for happy -- the default (7) breaks stuff. # set their own value for happy -- the default (7) breaks stuff.
c1.encoding_params['happy'] = 1 c1.encoding_params['happy'] = 1
d = c1.upload(Data(TEST_DATA, convergence="")) d = c1.upload(Data(TEST_DATA, convergence=b""))
def _after_upload(ur): def _after_upload(ur):
self.uri = ur.get_uri() self.uri = ur.get_uri()
self.filenode = self.g.clients[0].create_node_from_uri(ur.get_uri()) self.filenode = self.g.clients[0].create_node_from_uri(ur.get_uri())
@ -176,7 +187,7 @@ class Test(GridTestMixin, unittest.TestCase, common.ShouldFailMixin):
return d return d
def _shuffled(self, num_shnums): def _shuffled(self, num_shnums):
shnums = range(10) shnums = list(range(10))
random.shuffle(shnums) random.shuffle(shnums)
return shnums[:num_shnums] return shnums[:num_shnums]

View File

@ -79,6 +79,7 @@ PORTED_TEST_MODULES = [
"allmydata.test.test_base32", "allmydata.test.test_base32",
"allmydata.test.test_base62", "allmydata.test.test_base62",
"allmydata.test.test_codec", "allmydata.test.test_codec",
"allmydata.test.test_common_util",
"allmydata.test.test_configutil", "allmydata.test.test_configutil",
"allmydata.test.test_connection_status", "allmydata.test.test_connection_status",
"allmydata.test.test_crawler", "allmydata.test.test_crawler",
@ -92,6 +93,7 @@ PORTED_TEST_MODULES = [
"allmydata.test.test_hashtree", "allmydata.test.test_hashtree",
"allmydata.test.test_hashutil", "allmydata.test.test_hashutil",
"allmydata.test.test_humanreadable", "allmydata.test.test_humanreadable",
"allmydata.test.test_immutable",
"allmydata.test.test_iputil", "allmydata.test.test_iputil",
"allmydata.test.test_log", "allmydata.test.test_log",
"allmydata.test.test_monitor", "allmydata.test.test_monitor",