From bde424c7f985494b35b65e38869f651794b109f3 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Sat, 13 Mar 2021 09:00:01 -0500 Subject: [PATCH] Finish porting testing & test_testing --- src/allmydata/test/test_testing.py | 21 ++++++++++++++++----- src/allmydata/testing/web.py | 26 ++++++++++++++++---------- src/allmydata/util/_python3.py | 2 ++ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/allmydata/test/test_testing.py b/src/allmydata/test/test_testing.py index ec6935914..527b235bd 100644 --- a/src/allmydata/test/test_testing.py +++ b/src/allmydata/test/test_testing.py @@ -9,7 +9,18 @@ """ Tests for the allmydata.testing helpers + +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 from twisted.internet.defer import ( inlineCallbacks, @@ -79,7 +90,7 @@ class FakeWebTest(TestCase): self.assertThat(cap, IsInstance(CHKFileURI)) resp = yield http_client.get( - "http://example.com/uri?uri={}".format(cap.to_string()) + b"http://example.com/uri?uri=" + cap.to_string() ) self.assertThat(resp.code, Equals(200)) @@ -88,7 +99,7 @@ class FakeWebTest(TestCase): # using the form "/uri/" is also valid resp = yield http_client.get( - "http://example.com/uri/{}".format(cap.to_string()) + b"http://example.com/uri?uri=" + cap.to_string() ) self.assertEqual(resp.code, 200) @@ -136,9 +147,9 @@ class FakeWebTest(TestCase): """ http_client = create_tahoe_treq_client() - cap_gen = capability_generator("URI:CHK:") - - uri = DecodedURL.from_text(u"http://example.com/uri?uri={}".format(next(cap_gen))) + cap_gen = capability_generator(b"URI:CHK:") + cap = next(cap_gen).decode('ascii') + uri = DecodedURL.from_text(u"http://example.com/uri?uri={}".format(cap)) resp = http_client.get(uri.to_uri().to_text()) self.assertThat( diff --git a/src/allmydata/testing/web.py b/src/allmydata/testing/web.py index 0c3389956..34f5087e8 100644 --- a/src/allmydata/testing/web.py +++ b/src/allmydata/testing/web.py @@ -94,16 +94,19 @@ def capability_generator(kind): given kind. The N, K and size values aren't related to anything real. - :param str kind: the kind of capability, like `URI:CHK` + :param bytes kind: the kind of capability, like `URI:CHK` :returns: a generator that yields new capablities of a particular kind. """ + if not isinstance(kind, bytes): + raise TypeError("'kind' must be bytes") + if kind not in KNOWN_CAPABILITIES: raise ValueError( "Unknown capability kind '{} (valid are {})'".format( kind, - ", ".join(KNOWN_CAPABILITIES), + b", ".join(KNOWN_CAPABILITIES), ) ) # what we do here is to start with empty hashers for the key and @@ -118,16 +121,16 @@ def capability_generator(kind): # capabilities are "prefix:<128-bits-base32>:<256-bits-base32>:N:K:size" while True: number += 1 - key_hasher.update("\x00") - ueb_hasher.update("\x00") + key_hasher.update(b"\x00") + ueb_hasher.update(b"\x00") key = base32.b2a(key_hasher.digest()[:16]) # key is 16 bytes ueb_hash = base32.b2a(ueb_hasher.digest()) # ueb hash is 32 bytes cap = u"{kind}{key}:{ueb_hash}:{n}:{k}:{size}".format( - kind=kind, - key=key, - ueb_hash=ueb_hash, + kind=kind.decode('ascii'), + key=key.decode('ascii'), + ueb_hash=ueb_hash.decode('ascii'), n=1, k=1, size=number * 1000, @@ -164,6 +167,8 @@ class _FakeTahoeUriHandler(Resource, object): :returns: a two-tuple: a bool (True if the data is freshly added) and a capability-string """ + if not isinstance(kind, bytes): + raise TypeError("'kind' must be bytes") if not isinstance(data, bytes): raise TypeError("'data' must be bytes") @@ -181,7 +186,7 @@ class _FakeTahoeUriHandler(Resource, object): def render_PUT(self, request): data = request.content.read() - fresh, cap = self.add_data("URI:CHK:", data) + fresh, cap = self.add_data(b"URI:CHK:", data) if fresh: request.setResponseCode(http.CREATED) # real code does this for brand-new files else: @@ -193,7 +198,7 @@ class _FakeTahoeUriHandler(Resource, object): data = request.content.read() type_to_kind = { - "mkdir-immutable": "URI:DIR2-CHK:" + "mkdir-immutable": b"URI:DIR2-CHK:" } kind = type_to_kind[t] fresh, cap = self.add_data(kind, data) @@ -216,9 +221,10 @@ class _FakeTahoeUriHandler(Resource, object): # the user gave us a capability; if our Grid doesn't have any # data for it, that's an error. + capability = capability.encode('ascii') if capability not in self.data: request.setResponseCode(http.BAD_REQUEST) - return u"No data for '{}'".format(capability).decode("ascii") + return u"No data for '{}'".format(capability.decode('ascii')) return self.data[capability] diff --git a/src/allmydata/util/_python3.py b/src/allmydata/util/_python3.py index e5e616674..358990d94 100644 --- a/src/allmydata/util/_python3.py +++ b/src/allmydata/util/_python3.py @@ -87,6 +87,7 @@ PORTED_MODULES = [ "allmydata.storage.shares", "allmydata.test.no_network", "allmydata.test.mutable.util", + "allmydata.testing", "allmydata.unknown", "allmydata.uri", "allmydata.util._python3", @@ -205,6 +206,7 @@ PORTED_TEST_MODULES = [ # should be done once CLI is ported. "allmydata.test.test_system", + "allmydata.test.test_testing", "allmydata.test.test_time_format", "allmydata.test.test_upload", "allmydata.test.test_uri",