Beispiel #1
0
 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())
Beispiel #2
0
 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)
Beispiel #3
0
    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)
Beispiel #4
0
 def testUnauthenticated(self):
     url, portnum = self.makeServer(False)
     client = UnauthenticatedTub()
     client.startService()
     self.services.append(client)
     d = client.getReference(url)
     return d
Beispiel #5
0
 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
Beispiel #6
0
 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
Beispiel #7
0
 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
Beispiel #8
0
 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()
Beispiel #9
0
 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
Beispiel #10
0
 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
Beispiel #11
0
 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
Beispiel #12
0
 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)
Beispiel #13
0
 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)
Beispiel #14
0
    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
Beispiel #15
0
 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)
Beispiel #16
0
    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
Beispiel #17
0
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
Beispiel #18
0
    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
Beispiel #19
0
def GoodEnoughTub(certData=None, certFile=None, options={}):
    if crypto_available:
        return Tub(certData, certFile, options)
    return UnauthenticatedTub(options=options)
Beispiel #20
0
#! /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()
Beispiel #21
0
 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