def testUnauthenticated(self): url, portnum = self.makeServer(False) client = UnauthenticatedTub() client.startService() self.services.append(client) d = client.getReference(url) return d
def setUp(self): self.services = [UnauthenticatedTub(), UnauthenticatedTub()] self.tubA, self.tubB = self.services for s in self.services: s.startService() l = s.listenOn("tcp:0:interface=127.0.0.1") s.setLocation("127.0.0.1:%d" % l.getPortnum())
def setUp(self): self.s = service.MultiService() self.s.startService() self.target_tub = UnauthenticatedTub() self.target_tub.setServiceParent(self.s) l = self.target_tub.listenOn("tcp:0:interface=127.0.0.1") self.target_tub.setLocation("127.0.0.1:%d" % l.getPortnum()) self.source_tub = UnauthenticatedTub() self.source_tub.setServiceParent(self.s)
def _testNoConnection_1(self, res, url): self.services.remove(self.tub) client = UnauthenticatedTub() client.startService() self.services.append(client) d = client.getReference(url) d.addCallbacks(lambda res: self.fail("this is supposed to fail"), self._testNoConnection_fail) return d
def testHalfAuthenticated1(self): if not crypto_available: raise unittest.SkipTest("crypto not available") url, portnum = self.makeServer(True) client = UnauthenticatedTub() client.startService() self.services.append(client) d = client.getReference(url) return d
def testClientTimeout(self): portnum = self.makeNullServer() # lower the connection timeout to 2 seconds client = UnauthenticatedTub(options={'connect_timeout': 1}) client.startService() self.services.append(client) url = "pbu://127.0.0.1:%d/target" % portnum d = client.getReference(url) d.addCallbacks(lambda res: self.fail("hey! this is supposed to fail"), lambda f: f.trap(tokens.NegotiationError)) return d
def testVersusHTTPServerUnauthenticated(self): portnum = self.makeHTTPServer() client = UnauthenticatedTub() client.startService() self.services.append(client) url = "pbu://127.0.0.1:%d/target" % portnum d = client.getReference(url) d.addCallbacks(lambda res: self.fail("this is supposed to fail"), lambda f: f.trap(BananaError)) d.addCallback(self.stall, 1) # same reason as above return d
def TODO_testNonweakrefable(self): # what happens when we register a non-Referenceable? We don't really # need this yet, but as registerReference() becomes more generalized # into just plain register(), we'll want to provide references to # Copyables and ordinary data structures too. Let's just test that # this doesn't cause an error. target = [] tub = UnauthenticatedTub() tub.setLocation("bogus:1234567") url = tub.registerReference(target) del url
def makeServers(self, t1opts={}, t2opts={}, lo1={}, lo2={}, tubAauthenticated=True, tubBauthenticated=True): if tubAauthenticated or tubBauthenticated: self.requireCrypto() # first we create two Tubs if tubAauthenticated: a = Tub(options=t1opts) else: a = UnauthenticatedTub(options=t1opts) if tubBauthenticated: b = Tub(options=t1opts) else: b = UnauthenticatedTub(options=t1opts) # then we figure out which one will be the master, and call it tub1 if a.tubID > b.tubID: # a is the master tub1, tub2 = a, b else: tub1, tub2 = b, a if not self.tub1IsMaster: tub1, tub2 = tub2, tub1 self.tub1 = tub1 self.tub2 = tub2 # now fix up the options and everything else self.tub1phases = [] t1opts['debug_gatherPhases'] = self.tub1phases tub1.options = t1opts self.tub2phases = [] t2opts['debug_gatherPhases'] = self.tub2phases tub2.options = t2opts # connection[0], the winning connection, will be from tub1 to tub2 tub1.startService() self.services.append(tub1) l1 = tub1.listenOn("tcp:0", lo1) tub1.setLocation("127.0.0.1:%d" % l1.getPortnum()) self.target1 = Target() self.url1 = tub1.registerReference(self.target1) # connection[1], the abandoned connection, will be from tub2 to tub1 tub2.startService() self.services.append(tub2) l2 = tub2.listenOn("tcp:0", lo2) tub2.setLocation("127.0.0.1:%d" % l2.getPortnum()) self.target2 = Target() self.url2 = tub2.registerReference(self.target2)
def test_duplicate(self): basedir = "test_registration" os.makedirs(basedir) ff = os.path.join(basedir, "duplicate.furl") t1 = HelperTarget() tub = UnauthenticatedTub() tub.setLocation("bogus:1234567") u1 = tub.registerReference(t1, "name", furlFile=ff) u2 = tub.registerReference(t1, "name", furlFile=ff) self.failUnlessEqual(u1, u2) self.failUnlessRaises(WrongNameError, tub.registerReference, t1, "newname", furlFile=ff)
def makeServer(self, authenticated, options={}, listenerOptions={}): if authenticated: self.tub = tub = Tub(options=options) else: self.tub = tub = UnauthenticatedTub(options=options) tub.startService() self.services.append(tub) l = tub.listenOn("tcp:0", listenerOptions) tub.setLocation("127.0.0.1:%d" % l.getPortnum()) self.target = Target() return tub.registerReference(self.target), l.getPortnum()
def connect(self, url, authenticated=True): self.clientPhases = [] opts = {"debug_stall_second_connection": True, "debug_gatherPhases": self.clientPhases} if authenticated: self.client = client = Tub(options=opts) else: self.client = client = UnauthenticatedTub(options=opts) client.startService() self.services.append(client) d = client.getReference(url) return d
def testConnectUnauthenticated(self): tub = UnauthenticatedTub() self.startTub(tub) target = HelperTarget("bob") target.obj = "unset" url = tub.registerReference(target) # can we connect to a reference on our own Tub? d = tub.getReference(url) def _connected(ref): return ref.callRemote("set", 12) d.addCallback(_connected) def _check(res): self.failUnlessEqual(target.obj, 12) d.addCallback(_check) def _connect_again(res): target.obj = None return tub.getReference(url) d.addCallback(_connect_again) d.addCallback(_connected) d.addCallback(_check) return d
def test_lose_and_retry(self): tubC = UnauthenticatedTub() connects = [] d1 = defer.Deferred() d2 = defer.Deferred() notifiers = [d1, d2] target = HelperTarget("bob") url = self.tubB.registerReference(target, "target") portb = self.tubB.getListeners()[0].getPortnum() self.rc = self.tubA.connectTo(url, self._connected, notifiers, connects) def _connected_first(res): # we are now connected to tubB. Shut it down to force a # disconnect. self.services.remove(self.tubB) d = self.tubB.stopService() return d d1.addCallback(_connected_first) def _wait(res): # wait a few seconds to give the Reconnector a chance to try and # fail a few times return self.stall(2) d1.addCallback(_wait) def _start_tubC(res): # now start tubC listening on the same port that tubB used to, # which should allow the connection to complete (since they're # both UnauthenticatedTubs) self.services.append(tubC) tubC.startService() tubC.listenOn("tcp:%d:interface=127.0.0.1" % portb) tubC.setLocation("127.0.0.1:%d" % portb) url2 = tubC.registerReference(target, "target") assert url2 == url # this will fire when the second connection has been made return d2 d1.addCallback(_start_tubC) def _connected(res): self.failUnlessEqual(len(connects), 2) self.rc.stopConnecting() d1.addCallback(_connected) return d1
def setup_tubs(self): self.infotub = UnauthenticatedTub() self.infotub.listenOn('tcp:%d' % self.config.core.info_port) self.infotub.setLocation("localhost:%d" % self.config.core.info_port) # self.infotub.setLocationAutomatically() info_server = InfoTub() info_url = self.infotub.registerReference(info_server, 'info') logging.getLogger(__name__).debug('Info url: %s', info_url) # self.infotub.startService() core_server = CoreTub() self.coretub = Tub() self.coretub.listenOn('tcp:%d' % (self.config.core.core_port)) self.coretub.setLocation("localhost:%d" % (self.config.core.core_port)) # self.coretub.setLocationAutomatically() core_info_url = self.coretub.registerReference(core_server, 'core') logging.getLogger(__name__).debug('Core Info url: %s', core_info_url) eventmanager.emit(events.CoreUrlGenerated(core_info_url))
def testWeak(self): t1 = HelperTarget() tub = UnauthenticatedTub() tub.setLocation("bogus:1234567") name = tub._assignName(t1) url = tub.buildURL(name) del url results = [] w1 = weakref.ref(t1, results.append) del t1 gc.collect() # t1 should be dead self.failIf(w1()) self.failUnlessEqual(len(results), 1)
def testStrong(self): t1 = HelperTarget() tub = UnauthenticatedTub() tub.setLocation("bogus:1234567") u1 = tub.registerReference(t1) del u1 results = [] w1 = weakref.ref(t1, results.append) del t1 gc.collect() # t1 should still be alive self.failUnless(w1()) self.failUnlessEqual(results, []) tub.unregisterReference(w1()) gc.collect() # now it should be dead self.failIf(w1()) self.failUnlessEqual(len(results), 1)
def test_retry(self): tubC = UnauthenticatedTub() connects = [] target = HelperTarget("bob") url = self.tubB.registerReference(target, "target") portb = self.tubB.getListeners()[0].getPortnum() d1 = defer.Deferred() notifiers = [d1] self.services.remove(self.tubB) d = self.tubB.stopService() def _start_connecting(res): # this will fail, since tubB is not listening anymore self.rc = self.tubA.connectTo(url, self._connected, notifiers, connects) # give it a few tries, then start tubC listening on the same port # that tubB used to, which should allow the connection to # complete (since they're both UnauthenticatedTubs) return self.stall(2) d.addCallback(_start_connecting) def _start_tubC(res): self.failUnlessEqual(len(connects), 0) self.services.append(tubC) tubC.startService() tubC.listenOn("tcp:%d:interface=127.0.0.1" % portb) tubC.setLocation("127.0.0.1:%d" % portb) url2 = tubC.registerReference(target, "target") assert url2 == url return d1 d.addCallback(_start_tubC) def _connected(res): self.failUnlessEqual(len(connects), 1) self.rc.stopConnecting() d.addCallback(_connected) return d
def make_tub(ip, port, secure, cert_file): """Create a listening tub given an ip, port, and cert_file location. Parameters ---------- ip : str The ip address or hostname that the tub should listen on. Empty means all interfaces. port : int The port that the tub should listen on. A value of 0 means pick a random port secure: bool Will the connection be secure (in the Foolscap sense). cert_file: str A filename of a file to be used for theSSL certificate. Returns ------- A tub, listener tuple. """ if secure: if have_crypto: tub = Tub(certFile=cert_file) else: raise SecurityError( "OpenSSL/pyOpenSSL is not available, so we " "can't run in secure mode. Try running without " "security using 'ipcontroller -xy'.") else: tub = UnauthenticatedTub() # Set the strport based on the ip and port and start listening if ip == '': strport = "tcp:%i" % port else: strport = "tcp:%i:interface=%s" % (port, ip) log.msg("Starting listener with [secure=%r] on: %s" % (secure, strport)) listener = tub.listenOn(strport) return tub, listener
class Application(BaseApplication): def prepare_application(self): eventmanager.register_event_handler("ApplicationLoaded", self.load_sources) self.setup_tubs() self.setup_gstreamer() eventmanager.emit(events.ApplicationLoaded()) def load_config(self): self.config.load_core_config() def setup_tubs(self): self.infotub = UnauthenticatedTub() self.infotub.listenOn('tcp:%d' % self.config.core.info_port) self.infotub.setLocation("localhost:%d" % self.config.core.info_port) # self.infotub.setLocationAutomatically() info_server = InfoTub() info_url = self.infotub.registerReference(info_server, 'info') logging.getLogger(__name__).debug('Info url: %s', info_url) # self.infotub.startService() core_server = CoreTub() self.coretub = Tub() self.coretub.listenOn('tcp:%d' % (self.config.core.core_port)) self.coretub.setLocation("localhost:%d" % (self.config.core.core_port)) # self.coretub.setLocationAutomatically() core_info_url = self.coretub.registerReference(core_server, 'core') logging.getLogger(__name__).debug('Core Info url: %s', core_info_url) eventmanager.emit(events.CoreUrlGenerated(core_info_url)) # self.coretub.startService() def setup_gstreamer(self): import gobject gobject.threads_init() import pygst pygst.require("0.10") import gst # import pygtk # pygtk.require('2.0') # import gtk # GST Debugging # gst.debug_set_active(True) # gst.debug_set_default_threshold(gst.LEVEL_INFO) # gst.debug_set_colored(True) def load_sources(self): from afm.sources import Source log = logging.getLogger(__name__) available_sources = self.config.sources.keys() available_sources.sort() for source_name in available_sources: source_config = self.config.sources[source_name] if not source_config.active: log.debug("Skipping %s. Not Active.", source_config) continue log.debug("%s active. Loading...", source_config) source = Source(source_config) source.prepare_source() def start_services(self): self.infotub.startService() self.coretub.startService() def stop_services(self): self.infotub.stopService() self.coretub.stopService()
#! /usr/bin/python from twisted.internet import reactor from foolscap.api import Referenceable, UnauthenticatedTub class MathServer(Referenceable): def remote_add(self, a, b): return a+b def remote_subtract(self, a, b): return a-b myserver = MathServer() tub = UnauthenticatedTub() tub.listenOn("tcp:12345") tub.setLocation("localhost:12345") url = tub.registerReference(myserver, "math-service") print "the object is available at:", url tub.startService() reactor.run()
def connectClient(self, portnum): tub = UnauthenticatedTub() tub.startService() self.services.append(tub) d = tub.getReference("pb://127.0.0.1:%d/hello" % portnum) return d
class ReferenceCounting(ShouldFailMixin, unittest.TestCase): def setUp(self): self.s = service.MultiService() self.s.startService() self.target_tub = UnauthenticatedTub() self.target_tub.setServiceParent(self.s) l = self.target_tub.listenOn("tcp:0:interface=127.0.0.1") self.target_tub.setLocation("127.0.0.1:%d" % l.getPortnum()) self.source_tub = UnauthenticatedTub() self.source_tub.setServiceParent(self.s) def tearDown(self): return self.s.stopService() def setupTarget(self, target): furl = self.target_tub.registerReference(target) d = self.source_tub.getReference(furl) return d def test_reference_counting(self): self.source_tub.setOption("expose-remote-exception-types", True) target = HelperTarget() d = self.setupTarget(target) def _stash(rref): # to exercise bug #104, we need to trigger remote Violations, so # we tell the sending side to not use a RemoteInterface. We do # this by reaching inside the RemoteReference and making it # forget rref.tracker.interfaceName = None rref.tracker.interface = None self.rref = rref d.addCallback(_stash) # the first call causes an error, which discards all remaining # tokens, including the OPEN tokens for the arguments. The #104 bug # is that this causes the open-count to get out of sync, by -2 (one # for the arguments sequence, one for the list inside it). d.addCallback(lambda ign: self.shouldFail(Violation, "one", None, self.rref.callRemote, "bogus", ["one list"])) #d.addCallback(lambda ign: # self.rref.callRemote("set", ["one list"])) # a method call that has no arguments (specifically no REFERENCE # sequences) won't notice the loss of sync d.addCallback(lambda ign: self.rref.callRemote("set", 42)) def _check_42(ign): self.failUnlessEqual(target.obj, 42) d.addCallback(_check_42) # but when the call takes shared arguments, sync matters l = ["list", 1, 2] s = set([3,4]) t = ("tuple", 5, 6) d.addCallback(lambda ign: self.rref.callRemote("set", [t, l, s, t])) def _check_shared(ign): # the off-by-two bug would cause the second tuple shared-ref to # point at the set instead of the first tuple self.failUnlessEqual(type(target.obj), list) one, two, three, four = target.obj self.failUnlessEqual(type(one), tuple) self.failUnlessEqual(one, t) self.failUnlessEqual(type(two), list) self.failUnlessEqual(two, l) self.failUnlessEqual(type(three), set) self.failUnlessEqual(three, s) self.failUnlessEqual(type(four), tuple) # this is where it fails self.failUnlessEqual(four, t) self.failUnlessIdentical(one, four) d.addCallback(_check_shared) return d
class TubFailures(ExamineFailuresMixin, ShouldFailMixin, unittest.TestCase): def setUp(self): self.s = service.MultiService() self.s.startService() self.target_tub = UnauthenticatedTub() self.target_tub.setServiceParent(self.s) l = self.target_tub.listenOn("tcp:0:interface=127.0.0.1") self.target_tub.setLocation("127.0.0.1:%d" % l.getPortnum()) self.source_tub = UnauthenticatedTub() self.source_tub.setServiceParent(self.s) def tearDown(self): return self.s.stopService() def setupTarget(self, target): furl = self.target_tub.registerReference(target) d = self.source_tub.getReference(furl) return d def test_raise_not_exposed(self): self.source_tub.setOption("expose-remote-exception-types", False) d = self.setupTarget(TargetWithoutInterfaces()) d.addCallback(lambda rr: self.shouldFail(RemoteException, "one", None, rr.callRemote, "fail")) d.addCallback(self._examine_raise, True) return d def test_raise_yes_exposed(self): self.source_tub.setOption("expose-remote-exception-types", True) d = self.setupTarget(TargetWithoutInterfaces()) d.addCallback(lambda rr: self.shouldFail(ValueError, "one", None, rr.callRemote, "fail")) d.addCallback(self._examine_raise, False) return d def test_raise_default(self): # current default is to expose exceptions. This may change in the # future. d = self.setupTarget(TargetWithoutInterfaces()) d.addCallback(lambda rr: self.shouldFail(ValueError, "one", None, rr.callRemote, "fail")) d.addCallback(self._examine_raise, False) return d
def GoodEnoughTub(certData=None, certFile=None, options={}): if crypto_available: return Tub(certData, certFile, options) return UnauthenticatedTub(options=options)