def test_tcpnoserv(self): """Abort a TCP persistent circuit trying to connect to nothing """ client=StubClient() client.tst=self name1=padString('test1') user=padString('hello') host=padString('world') target=('127.0.0.1', 65534) self.cfact=CACircuitFactory(client) conf=Config(Config.empty) conf.nameservs=[target] resolv=Resolver(conf=conf, tcpfactory=self.cfact) d=resolv.lookup('test1') @d.addCallback def result(srv): self.assertTrue(srv is None) d2=deferLater(reactor, 0.5, resolv.close) return gatherResults([d,d2])
def test_tcplookup(self): client=StubClient() client.tst=self name1=padString('test1') name2=padString('anotherpv') user=padString('hello') host=padString('world') sfact=self.sfact=CAExpectFactory() sfact.tst=self self.serv=reactor.listenTCP(0, sfact, interface='127.0.0.1') target=('127.0.0.1', self.serv.getHost().port) sfact.program= \ [('send',CAmessage(dtype=0, count=12)), ('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=20, size=len(user), body=user)), ('recv',CAmessage(cmd=21, size=len(host), body=host)), ('recv',CAmessage(cmd=6, size=len(name1), dtype=5, count=CA_VERSION, p1=0, p2=0, body=name1)), ('send',CAmessage(cmd=6, size=8, dtype=target[1], p1=0xffffffff, p2=0, body=searchbody.pack(12))), ('recv',CAmessage(cmd=6, size=len(name2), dtype=5, count=CA_VERSION, p1=1, p2=1, body=name2)), ('send',CAmessage(cmd=6, size=8, dtype=target[1], p1=0xffffffff, p2=1, body=searchbody.pack(12))), ] self.cfact=CACircuitFactory(client) conf=Config(Config.empty) conf.nameservs=[target] resolv=self.resolv=Resolver(conf=conf, tcpfactory=self.cfact) d=resolv.lookup('test1') @d.addCallback def result(srv): self.assertEqual(srv, target) self.assertEqual(len(self.sfact.program),2) return resolv.lookup('anotherpv') @d.addCallback def result(srv): self.assertEqual(srv, target) self.assertEqual(len(self.sfact.program),0) return d
def connectionMade(self): self.peer = self.transport.getPeer() log.debug('Open %s', self) user = padString(self.client.user) host = padString(self.client.host) msg = CAmessage(cmd=0, dtype=self.prio, count=defs.CA_VERSION).pack() msg += CAmessage(cmd=20, size=len(user), body=user).pack() msg += CAmessage(cmd=21, size=len(host), body=host).pack() self.transport.write(msg) self.readyWait = reactor.callLater(0.1, self.circuitReady)
def readnotify(self, pkt, peer, circuit): """Client get """ log.debug('Read %s from %s', self.pv.name, peer) try: if not self.rights & RIGHT.READ: raise CAError('Operation not permitted', error.ECA_NORDACCESS) if pkt.count == 0 and self.circuit.version >= 13: pkt.count = min(self.pv.count, self.pv.maxcount) else: pkt.count = min(pkt.count, self.pv.maxcount) data, count = self.pv.get(self, pkt.dtype, pkt.count) if count < pkt.count: # Zero pad data dbf, _ = dbr_to_dbf(pkt.dtype) pad = dbf_element_size(dbf) * (pkt.count - count) data = padString(data + '\0' * pad) count = pkt.count pkt.cmd = 15 pkt.size = len(data) pkt.count = count pkt.p1 = error.ECA_NORMAL pkt.body = data except CAError, e: log.exception('Read failed: ' + e.msg) pkt.size = 0 pkt.count = 0 pkt.p1 = e.code pkt.body = ''
def test_tcpabort(self): """Abort a TCP persistent circuit """ client=StubClient() client.tst=self name1=padString('test1') user=padString('hello') host=padString('world') sfact=self.sfact=CAExpectFactory() sfact.tst=self self.serv=reactor.listenTCP(0, sfact, interface='127.0.0.1') target=('127.0.0.1', self.serv.getHost().port) sfact.program= \ [('send',CAmessage(dtype=0, count=12)), ('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=20, size=len(user), body=user)), ('recv',CAmessage(cmd=21, size=len(host), body=host)), ]+[('recv',CAmessage(cmd=6, size=len(name1), dtype=5, count=CA_VERSION, p1=0, p2=0, body=name1))]*6 self.cfact=CACircuitFactory(client) conf=Config(Config.empty) conf.nameservs=[target] resolv=Resolver(conf=conf, tcpfactory=self.cfact) d=resolv.lookup('test1') @d.addCallback def result(srv): self.assertTrue(srv is None) d2=deferLater(reactor, 0.5, resolv.close) return gatherResults([d,d2])
def setUp(self): client = StubClient() user = padString('hello') host = padString('world') self.program = \ [('send',CAmessage(dtype=0, count=13)), ('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=20, size=len(user), body=user)), ('recv',CAmessage(cmd=21, size=len(host), body=host)), ] sfact = CAExpectFactory() sfact.tst = self sfact.program = self.program + self.program self.serv = reactor.listenTCP(0, sfact, interface='127.0.0.1') self.target = ('127.0.0.1', self.serv.getHost().port) self.cfact = CACircuitFactory(client)
def test_handshakeV12(self): """Handshake with a v11 server. Server sends version on connection to facilitate name server on TCP """ user = padString('hello') host = padString('world') self.sfact.program= \ [('send',CAmessage(dtype=0, count=12)), ('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=20, size=len(user), body=user)), ('recv',CAmessage(cmd=21, size=len(host), body=host)), ] # since client gets notification before program # completes have server do shutdown self.sfact.halt = True d = self.cfact.requestCircuit(self.target) @d.addCallback def postCondition(circ): self.assertTrue(circ is not None) # we get notification when the first packet is processed # the next three may have been received self.assertTrue(len(self.sfact.program) <= 3) self.assertEqual(circ.version, 12) return circ.transport.connector.whenDis @d.addCallback def done(circ): self.assertEqual(self.sfact.program, []) return d
def addchan(self, channel): """Add a channel to this circuit """ assert channel not in self.channels channel.cid = self.channels.add(channel) name = padString(channel.name) msg = CAmessage(cmd=18, size=len(name), p1=channel.cid, p2=defs.CA_VERSION, body=name).pack() self.transport.write(msg)
def test_handshakeV11(self): """Handshake with a v11 server. Server sends version after authentication """ user = padString('hello') host = padString('world') self.sfact.program= \ [('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=20, size=len(user), body=user)), ('recv',CAmessage(cmd=21, size=len(host), body=host)), ] d = self.cfact.requestCircuit(self.target) @d.addCallback def postCondition(circ): self.assertTrue(circ is not None) self.assertEqual(self.sfact.program, []) self.assertEqual(circ.version, 11) return d
def test_udplookup(self): name=padString('test1') serv=CAExpectDatagramProtocol(self, [], halt=False) up=self.up=reactor.listenUDP(0, serv, interface='127.0.0.1') addr=up.getHost() addr=addr.host, addr.port conf=Config(Config.empty) conf.addrs=[addr] resolv=self.resolv=Resolver(conf=conf) serv.dest='127.0.0.1', resolv._udp.getHost().port # name search # respond after second request serv.program= \ [('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=6, size=len(name), dtype=5, count=CA_VERSION, p1=0, p2=0, body=name)), ('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=6, size=len(name), dtype=5, count=CA_VERSION, p1=0, p2=0, body=name)), ('send',CAmessage(cmd=6, size=8, dtype=addr[1], p1=0xffffffff, p2=0, body=searchbody.pack(11))), ] d=resolv.lookup('test1') @d.addCallback def result(srv): self.assertEqual(srv, addr) self.assertEqual(len(serv.program),0) return d
def post(self, mask): """Send monitor update if mask matches Data is read from the PV with the type meta-data requested by the client """ if (self.mask & mask) == 0: return try: count = self.count if count == 0 and self.channel.circuit.version >= 13: # when a client requests a dcount it must get the exact # count. Additional space is zeros. # with version 13 a request for zero data get the # current native size count = self.channel.pv.count count = min(count, self.channel.pv.maxcount) data, count = self.channel.pv.get(self.channel, self.dbr, count) if self.count != 0 and count < self.count: # Zero pad data dbf, _ = dbr_to_dbf(self.dbr) pad = dbf_element_size(dbf) * (self.count - count) data = padString(data + '\0' * pad) count = self.count pkt = CAmessage(cmd=1, size=len(data), dtype=self.dbr, count=count, p1=ECA_NORMAL, p2=self.ioid, body=data) log.debug('post to %s', self.channel.circuit.peer) except CAError, e: log.exception('Post failed') pkt = CAmessage(cmd=1, size=0, dtype=self.dbr, count=0, p1=e.code, p2=self.ioid)
def __init__(self, name, id, manager): self.name, self.id, self.manager = name, id, manager self.d = Deferred() self.wait = 0.04 self.T = None self.Skip = set() nbody = padString(name) self.udp = CAmessage(cmd=0, count=CA_VERSION).pack() self.tcp = CAmessage(cmd=6, size=len(nbody), dtype=5, count=CA_VERSION, p1=id, p2=id, body=nbody).pack() self.udp = self.udp + self.tcp self.lookup()
def test_opencircuit(self): name = padString('testpv') user = padString('testuser') host = padString('testhost') namelookup = CAExpectDatagramProtocol(self, [], halt=False) udp = self.udp = reactor.listenUDP(0, namelookup, interface='127.0.0.1') sfact = self.sfact = CAExpectFactory() sfact.tst = self tcp = self.tcp = reactor.listenTCP(0, sfact, interface='127.0.0.1') udptarget = '127.0.0.1', udp.getHost().port tcptarget = '127.0.0.1', tcp.getHost().port # name search # respond after second request namelookup.program= \ [('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=6, size=len(name), dtype=5, count=CA_VERSION, p1=0, p2=0, body=name)), ('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=6, size=len(name), dtype=5, count=CA_VERSION, p1=0, p2=0, body=name)), ('send',CAmessage(cmd=6, size=8, dtype=tcptarget[1], p1=0xffffffff, p2=0, body=searchbody.pack(11))), ] #namelookup.debug=True sfact.program= \ [('send',CAmessage(dtype=0, count=12)), ('recv',CAmessage(dtype=0, count=CA_VERSION)), ('recv',CAmessage(cmd=20, size=len(user), body=user)), ('recv',CAmessage(cmd=21, size=len(host), body=host)), ('recv',CAmessage(cmd=100)), ] conf = Config(Config.empty) conf.addrs = [udptarget] cli = self.cli = CAClient(conf, user='******', host='testhost') namelookup.dest = '127.0.0.1', cli.resolv._udp.getHost().port d = cli.lookup('testpv') @d.addCallback @inlineCallbacks def findAndConnect(srv): self.assertEqual(srv, tcptarget) circ = yield cli.openCircuit(srv) self.assertTrue(circ is not None) self.assertEqual(circ.version, 12) return d