def testRegister(self): s = self.services[0] l = s.listenOn("tcp:0:interface=127.0.0.1") s.setLocation("127.0.0.1:%d" % l.getPortnum()) t1 = Target() public_url = s.registerReference(t1, "target") if crypto_available: self.failUnless(public_url.startswith("pb://")) self.failUnless(public_url.endswith("@127.0.0.1:%d/target" % l.getPortnum())) else: self.failUnlessEqual(public_url, "pbu://127.0.0.1:%d/target" % l.getPortnum()) self.failUnlessEqual(s.registerReference(t1, "target"), public_url) self.failUnlessIdentical(s.getReferenceForURL(public_url), t1) t2 = Target() private_url = s.registerReference(t2) self.failUnlessEqual(s.registerReference(t2), private_url) self.failUnlessIdentical(s.getReferenceForURL(private_url), t2) s.unregisterURL(public_url) self.failUnlessRaises(KeyError, s.getReferenceForURL, public_url) s.unregisterReference(t2) self.failUnlessRaises(KeyError, s.getReferenceForURL, private_url)
def testRef5(self): # those RemoteReferences can be used to invoke methods on the sender. # 'r' lives on side A. The anonymous target lives on side B. From # side A we invoke B.set(r), and we get the matching RemoteReference # 'rr' which lives on side B. Then we use 'rr' to invoke r.getName # from side A. r = Target() r.name = "ernie" d = self.send(r) d.addCallback(lambda rr: rr.callRemote("getName")) d.addCallback(self.assertEqual, "ernie") return d
def testRef5(self): # those RemoteReferences can be used to invoke methods on the sender. # 'r' lives on side A. The anonymous target lives on side B. From # side A we invoke B.set(r), and we get the matching RemoteReference # 'rr' which lives on side B. Then we use 'rr' to invoke r.getName # from side A. r = Target() r.name = "ernie" d = self.send(r) d.addCallback(lambda rr: rr.callRemote("getName")) d.addCallback(self.failUnlessEqual, "ernie") return d
def testFailWrongMethodLocal(self): # the caller knows that this method does not really exist rr, target = self.setupTarget(Target(), True) d = rr.callRemote("bogus") # RIMyTarget doesn't implement .bogus() d.addCallbacks(lambda res: self.fail("should have failed"), self._testFailWrongMethodLocal_1) return d
def testCall2(self): # server end uses an interface this time, but not the client end rr, target = self.setupTarget(Target(), True) d = rr.callRemote("add", a=3, b=4, _useSchema=False) # the schema is enforced upon receipt d.addCallback(lambda res: self.failUnlessEqual(res, 7)) return d
def test_local_return_violation_default(self): rr, target = self.setupTarget(Target(), True) d = self.shouldFail(Violation, "one", None, rr.callRemote, "add", a=1, b=2, _resultConstraint=str) d.addCallback(self._examine_local_return_violation) return d
def testRef2(self): # sending a Referenceable over the wire multiple times should result # in equivalent RemoteReferences r = Target() d = self.send(r) d.addCallback(self._testRef2_1, r) return d
def testStack(self): # when you violate your outbound schema, the Failure you get should # have a stack trace that includes the actual callRemote invocation. # Sometimes the stack trace doesn't include source code (either we # have .pyc files but not .py files, or because the code is coming # from an .egg). So this test merely asserts that test_interfaces.py # is present in the trace, followed by either a source code line that # mentions callRemote, or the filename/linenumber/functionname line # that mentions callRemote. self.setupBrokers() rr, target = self.setupTarget(Target(), True) d = rr.callRemote('add', "not a number", "oops") def _check_failure(f): s = f.getTraceback().split("\n") for i in range(len(s)): line = s[i] if ("test_interfaces.py" in line and i + 2 < len(s) and ("rr.callRemote" in s[i + 1] or "in callRemote" in s[i + 2])): return # all good print("failure looked like this:") print(f) self.fail("didn't see invocation of callRemote in stacktrace") d.addCallbacks(lambda res: self.fail("hey, this was supposed to fail"), _check_failure) return d
def test_nonqualified_port(self): tubA, tubB = self.makeTubs() portnum = util.allocate_tcp_port() tubA.listenOn("%d" % portnum) # this is deprecated tubA.setLocation("tcp:127.0.0.1:%d" % portnum) furl = tubA.registerReference(Target()) yield tubB.getReference(furl)
def testRef3(self): # sending the same Referenceable in multiple arguments should result # in equivalent RRs r = Target() d = self.send2(r, r) d.addCallback(self._testRef3_1) return d
def test_string(self): tubA, tubB = self.makeTubs() portnum = util.allocate_tcp_port() tubA.listenOn("tcp:%d:interface=127.0.0.1" % portnum) tubA.setLocation("tcp:127.0.0.1:%d" % portnum) furl = tubA.registerReference(Target()) yield tubB.getReference(furl)
def test_remote_violation_yes_exposed(self): self._set_expose(True) rr, target = self.setupTarget(Target(), True) d = self.shouldFail(Violation, "one", None, rr.callRemote, "add", a=1,b="foo", _useSchema=False) d.addCallback(self._examine_remote_violation, False) return d
def testUnconstrainedMethod(self): rr, target = self.setupTarget(Target(), True) d = rr.callRemote('free', 3, 4, x="boo") def _check(res): self.failUnlessEqual(res, "bird") self.failUnlessEqual(target.calls, [((3,4), {"x": "boo"})]) d.addCallback(_check) return d
def testFailWrongArgsLocal1(self): # we violate the interface (extra arg), and the sender should catch it rr, target = self.setupTarget(Target(), True) d = rr.callRemote("add", a=1, b=2, c=3) d.addCallbacks(lambda res: self.fail("should have failed"), self._testFailWrongArgsLocal1_1) d.addCallback(lambda res: self.assertFalse(target.calls)) return d
def test_local_return_violation_yes_exposed(self): self._set_expose(True) rr, target = self.setupTarget(Target(), True) d = self.shouldFail(Violation, "one", None, rr.callRemote, "add", a=1, b=2, _resultConstraint=bytes) d.addCallback(self._examine_local_return_violation) return d
def testRef4(self): # sending the same Referenceable in multiple calls will result in # equivalent RRs r = Target() rr, target = self.setupTarget(HelperTarget()) d = rr.callRemote("set", obj=r) d.addCallback(self._testRef4_1, rr, r, target) return d
def testFailWrongArgsLocal2(self): # we violate the interface (bad arg), and the sender should catch it rr, target = self.setupTarget(Target(), True) d = rr.callRemote("add", a=1, b="two") d.addCallbacks(lambda res: self.fail("should have failed"), self._testFailWrongArgsLocal2_1) d.addCallback(lambda res: self.failIf(target.calls)) return d
def test_endpoint(self): tubA, tubB = self.makeTubs() portnum = util.allocate_tcp_port() ep = endpoints.TCP4ServerEndpoint(reactor, portnum, interface="127.0.0.1") tubA.listenOn(ep) tubA.setLocation("tcp:127.0.0.1:%d" % portnum) furl = tubA.registerReference(Target()) yield tubB.getReference(furl)
def test_parsed_endpoint(self): tubA, tubB = self.makeTubs() portnum = util.allocate_tcp_port() ep = endpoints.serverFromString(reactor, "tcp:%d:interface=127.0.0.1" % portnum) tubA.listenOn(ep) tubA.setLocation("tcp:127.0.0.1:%d" % portnum) furl = tubA.registerReference(Target()) yield tubB.getReference(furl)
def testFailWrongMethodRemote(self): # if the target doesn't specify any remote interfaces, then the # calling side shouldn't try to do any checking. The problem is # caught on the target side. rr, target = self.setupTarget(Target(), False) d = rr.callRemote("bogus") # RIMyTarget doesn't implement .bogus() d.addCallbacks(lambda res: self.fail("should have failed"), self._testFailWrongMethodRemote_1) return d
def test_remote_violation_not_exposed(self): self._set_expose(False) # the sender thinks they're ok, but the recipient catches the # violation. rr, target = self.setupTarget(Target(), True) d = self.shouldFail(RemoteException, "one", None, rr.callRemote, "add", a=1,b="foo", _useSchema=False) d.addCallback(self._examine_remote_violation, True) return d
def test_local_violation_not_exposed(self): self._set_expose(False) # the caller knows that this method does not really exist, so we # should get a local Violation. Local exceptions are never reported # as RemoteExceptions, so the expose option doesn't affect behavior. rr, target = self.setupTarget(Target(), True) d = self.shouldFail(Violation, "one", None, rr.callRemote, "bogus") d.addCallback(self._examine_local_violation) return d
def testFailWrongArgsRemote1(self): # the sender thinks they're ok but the recipient catches the # violation rr, target = self.setupTarget(Target(), True) d = rr.callRemote("add", a=1, b="foo", _useSchema=False) d.addCallbacks(lambda res: self.fail("should have failed"), self._testFailWrongArgsRemote1_1) d.addCallbacks(lambda res: self.failIf(target.calls)) return d
def makeTub(self, hint_type): tubA = Tub(certData=certData_low) tubA.setServiceParent(self.s) tubB = Tub(certData=certData_high) tubB.setServiceParent(self.s) portnum = util.allocate_tcp_port() tubA.listenOn("tcp:%d:interface=127.0.0.1" % portnum) tubA.setLocation("%s:127.0.0.1:%d" % (hint_type, portnum)) furl = tubA.registerReference(Target()) return furl, tubB
def test_nonqualified_port(self): tubA, tubB = self.makeTubs() portnum = util.allocate_tcp_port() import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") tubA.listenOn("%d" % portnum) # this is deprecated tubA.setLocation("tcp:127.0.0.1:%d" % portnum) furl = tubA.registerReference(Target()) yield tubB.getReference(furl)
def testFailWrongMethodRemote2(self): # call a method which doesn't actually exist. The sender thinks # they're ok but the recipient catches the violation rr, target = self.setupTarget(Target(), True) d = rr.callRemote("bogus", _useSchema=False) # RIMyTarget2 has a 'sub' method, but RIMyTarget (the real interface) # does not d.addCallbacks(lambda res: self.fail("should have failed"), self._testFailWrongMethodRemote2_1) d.addCallback(lambda res: self.failIf(target.calls)) return d
def testWrongSwiss(self): target = Target() url = self.tubB.registerReference(target) badurl = url + "_wrong" swiss = url[url.rindex("/")+1:] d = self.tubA.getReference(badurl) def _check(f): self.failIf(swiss in str(f), "swissnum revealed") self.failUnless(swiss[:2] in str(f), "swissnum hint not given") d.addErrback(_check) return d
def test_local_return_violation_not_exposed(self): self._set_expose(False) # the target returns a value which violations our _resultConstraint # Local exceptions are never reported as RemoteExceptions, so the # expose option doesn't affect behavior. rr, target = self.setupTarget(Target(), True) d = self.shouldFail(Violation, "one", None, rr.callRemote, "add", a=1, b=2, _resultConstraint=str) d.addCallback(self._examine_local_return_violation) return d
def testListener(self): furl, tubB, hint = self.makeTub("tcp") rref1 = yield tubB.getReference(furl) yield rref1.callRemote("free", Target()) rref2 = self._target.calls[0][0][0] ci = rref2.getConnectionInfo() self.assertEqual(ci.connectorStatuses, {}) (listener, status) = ci.listenerStatus self.assertEqual(status, "successful") self.assertEqual( listener, "Listener on IPv4Address(TCP, '127.0.0.1', %d)" % self._portnum)
def testRegister(self): s = self.services[0] portnum = allocate_tcp_port() s.listenOn("tcp:%d:interface=127.0.0.1" % portnum) s.setLocation("127.0.0.1:%d" % portnum) t1 = Target() public_url = s.registerReference(t1, "target") self.assertTrue(public_url.startswith("pb://")) self.assertTrue(public_url.endswith("@127.0.0.1:%d/target" % portnum)) self.assertEqual(s.registerReference(t1, "target"), public_url) self.assertIs(s.getReferenceForURL(public_url), t1) t2 = Target() private_url = s.registerReference(t2) self.assertEqual(s.registerReference(t2), private_url) self.assertIs(s.getReferenceForURL(private_url), t2) s.unregisterURL(public_url) self.assertRaises(KeyError, s.getReferenceForURL, public_url) s.unregisterReference(t2) self.assertRaises(KeyError, s.getReferenceForURL, private_url)
def testFailWrongReturnLocal(self): # the target returns a value which violates our _resultConstraint rr, target = self.setupTarget(Target(), True) d = rr.callRemote("add", a=1, b=2, _resultConstraint=str) # The target returns an int, which matches the schema they're using, # so they think they're ok. We've overridden our expectations to # require a string. d.addCallbacks(lambda res: self.fail("should have failed"), self._testFailWrongReturnLocal_1) # the method should have been run d.addCallback(lambda res: self.failUnless(target.calls)) return d