From 7898595eac3e7c55c7ce1f28bfd4013f01bd24a7 Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Mon, 8 Jul 2019 11:21:28 -0400
Subject: [PATCH] Make it all really work, fixing errors pointed out by
 validation

---
 .../_storage_client.py                        |  1 +
 .../_storage_server.py                        | 27 ++++++++++++++++---
 .../tests/test_storage_protocol.py            |  9 ++++---
 3 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/src/_secureaccesstokenauthorizer/_storage_client.py b/src/_secureaccesstokenauthorizer/_storage_client.py
index ff86d26..309eaab 100644
--- a/src/_secureaccesstokenauthorizer/_storage_client.py
+++ b/src/_secureaccesstokenauthorizer/_storage_client.py
@@ -127,6 +127,7 @@ class SecureAccessTokenAuthorizerStorageClient(object):
     ):
         return self._rref.callRemote(
             "slot_testv_and_readv_and_writev",
+            self._get_tokens(),
             storage_index,
             secrets,
             tw_vectors,
diff --git a/src/_secureaccesstokenauthorizer/_storage_server.py b/src/_secureaccesstokenauthorizer/_storage_server.py
index 72ccd37..e642512 100644
--- a/src/_secureaccesstokenauthorizer/_storage_server.py
+++ b/src/_secureaccesstokenauthorizer/_storage_server.py
@@ -63,14 +63,32 @@ def add_tokens(schema):
     :return foolscap.remoteinterface.RemoteMethodSchema: A schema like
         ``schema`` but with one additional required argument.
     """
-    return add_arguments(schema, tokens=TokenList)
+    return add_arguments(schema, [(b"tokens", TokenList)])
 
 
+def add_arguments(schema, kwargs):
+    """
+    Create a new schema like ``schema`` but with the arguments given by
+    ``kwargs`` prepended to the signature.
+
+    :param foolscap.remoteinterface.RemoteMethodSchema schema: The existing
+        schema.
+
+    :param list[(bytes, foolscap.IConstraint)] kwargs: The arguments to
+        prepend to the signature of ``schema``.
 
-def add_arguments(schema, **kwargs):
-    new_kwargs = schema.argConstraints.copy()
+    :return foolscap.remoteinterface.RemoteMethodSchema: The new schema
+        object.
+    """
+    new_kwargs = dict(schema.argConstraints)
     new_kwargs.update(kwargs)
     modified_schema = RemoteMethodSchema(**new_kwargs)
+    # Initialized from **new_kwargs, RemoteMethodSchema.argumentNames is in
+    # some arbitrary, probably-incorrect order.  Fix it.
+    modified_schema.argumentNames = (
+        list(argName for (argName, _) in kwargs) +
+        schema.argumentNames
+    )
     return modified_schema
 
 
@@ -129,7 +147,8 @@ class SecureAccessTokenAuthorizerStorageServer(Referenceable):
     def remote_advise_corrupt_share(self, *a, **kw):
         return self._original.remote_advise_corrupt_share(*a, **kw)
 
-    def remote_slot_testv_and_readv_and_writev(self, *a, **kw):
+    def remote_slot_testv_and_readv_and_writev(self, tokens, *a, **kw):
+        self._validate_tokens(tokens)
         return self._original.remote_slot_testv_and_readv_and_writev(*a, **kw)
 
     def remote_slot_readv(self, *a, **kw):
diff --git a/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py b/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py
index a2fdbef..106585b 100644
--- a/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py
+++ b/src/_secureaccesstokenauthorizer/tests/test_storage_protocol.py
@@ -77,6 +77,9 @@ from ..api import (
     SecureAccessTokenAuthorizerStorageServer,
     SecureAccessTokenAuthorizerStorageClient,
 )
+from .._storage_server import (
+    TOKEN_LENGTH,
+)
 
 class AnonymousStorageServer(Fixture):
     def _setUp(self):
@@ -113,7 +116,7 @@ class ShareTests(TestCase):
         self.anonymous_storage_server = self.useFixture(AnonymousStorageServer()).storage_server
 
         def get_tokens():
-            return [u"x"]
+            return [b"x" * TOKEN_LENGTH]
 
         self.server = SecureAccessTokenAuthorizerStorageServer(
             self.anonymous_storage_server,
@@ -299,10 +302,10 @@ class ShareTests(TestCase):
 
         extract_result(
             self.client.advise_corrupt_share(
-                u"immutable",
+                b"immutable",
                 storage_index,
                 sharenum,
-                u"the bits look bad",
+                b"the bits look bad",
             ),
         )
         self.assertThat(
-- 
GitLab