tests/test-wireproto.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 07 Mar 2018 16:18:52 -0800
changeset 36801 66de4555cefd
parent 36555 c28de190e20a
child 36944 65615c29e74f
permissions -rw-r--r--
wireproto: formalize permissions checking as part of protocol interface Per the inline comment desiring to formalize permissions checking in the protocol interface, we do that. I'm not convinced this is the best way to go about things. I would love for there to e.g. be a better exception for denoting permissions problems. But it does feel strictly better than snipping attributes on the proto instance. Differential Revision: https://phab.mercurial-scm.org/D2719

from __future__ import absolute_import, print_function

from mercurial import (
    error,
    pycompat,
    util,
    wireproto,
    wireprototypes,
)
stringio = util.stringio

class proto(object):
    def __init__(self, args):
        self.args = args
    def getargs(self, spec):
        args = self.args
        args.setdefault(b'*', {})
        names = spec.split()
        return [args[n] for n in names]

    def checkperm(self, perm):
        pass

class clientpeer(wireproto.wirepeer):
    def __init__(self, serverrepo):
        self.serverrepo = serverrepo

    @property
    def ui(self):
        return self.serverrepo.ui

    def url(self):
        return b'test'

    def local(self):
        return None

    def peer(self):
        return self

    def canpush(self):
        return True

    def close(self):
        pass

    def capabilities(self):
        return [b'batch']

    def _call(self, cmd, **args):
        args = pycompat.byteskwargs(args)
        res = wireproto.dispatch(self.serverrepo, proto(args), cmd)
        if isinstance(res, wireprototypes.bytesresponse):
            return res.data
        elif isinstance(res, bytes):
            return res
        else:
            raise error.Abort('dummy client does not support response type')

    def _callstream(self, cmd, **args):
        return stringio(self._call(cmd, **args))

    @wireproto.batchable
    def greet(self, name):
        f = wireproto.future()
        yield {b'name': mangle(name)}, f
        yield unmangle(f.value)

class serverrepo(object):
    def greet(self, name):
        return b"Hello, " + name

    def filtered(self, name):
        return self

def mangle(s):
    return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
def unmangle(s):
    return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))

def greet(repo, proto, name):
    return mangle(repo.greet(unmangle(name)))

wireproto.commands[b'greet'] = (greet, b'name',)

srv = serverrepo()
clt = clientpeer(srv)

print(clt.greet(b"Foobar"))
b = clt.iterbatch()
list(map(b.greet, (b'Fo, =;:<o', b'Bar')))
b.submit()
print([r for r in b.results()])