3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 21:19:31 +01:00

Fix fakeirc and tests for relay (#56)

This commit is contained in:
James Lu 2015-07-17 22:52:55 -07:00
parent 4553eda6ec
commit a51cfcb7b2
5 changed files with 40 additions and 17 deletions

View File

@ -1,4 +1,6 @@
from collections import defaultdict from collections import defaultdict
import threading
from random import randint
from log import log from log import log
import main import main
@ -90,6 +92,10 @@ class FakeIRC(main.Irc):
self.hookargs = [] self.hookargs = []
self.hookmsgs = [] self.hookmsgs = []
self.socket = None self.socket = None
self.initVars()
self.spawnMain()
self.connected = threading.Event()
self.connected.set()
def run(self, data): def run(self, data):
"""Queues a message to the fake IRC server.""" """Queues a message to the fake IRC server."""
@ -140,3 +146,15 @@ class FakeProto():
@staticmethod @staticmethod
def connect(irc): def connect(irc):
pass pass
@staticmethod
def spawnClient(irc, nick, *args, **kwargs):
uid = randint(1, 10000000000)
ts = int(time.time())
irc.users[uid] = user = IrcUser(nick, ts, uid)
return user
@staticmethod
def joinClient(irc, client, channel):
irc.channels[channel].users.add(client)
irc.users[client].channels.add(channel)

13
main.py
View File

@ -19,7 +19,7 @@ class Irc():
def initVars(self): def initVars(self):
# Server, channel, and user indexes to be populated by our protocol module # Server, channel, and user indexes to be populated by our protocol module
self.servers = {} self.servers = {self.sid: classes.IrcServer(None, self.serverdata['hostname'], internal=True)}
self.users = {} self.users = {}
self.channels = defaultdict(classes.IrcChannel) self.channels = defaultdict(classes.IrcChannel)
# Sets flags such as whether to use halfops, etc. The default RFC1459 # Sets flags such as whether to use halfops, etc. The default RFC1459
@ -36,6 +36,7 @@ class Irc():
self.umodes = {'invisible': 'i', 'snomask': 's', 'wallops': 'w', self.umodes = {'invisible': 'i', 'snomask': 's', 'wallops': 'w',
'oper': 'o', 'oper': 'o',
'*A': '', '*B': '', '*C': 's', '*D': 'iow'} '*A': '', '*B': '', '*C': 's', '*D': 'iow'}
# This nicklen value is only a default, and SHOULD be set by the # This nicklen value is only a default, and SHOULD be set by the
# protocol module as soon as the relevant capability information is # protocol module as soon as the relevant capability information is
# received from the uplink. Plugins that depend on maxnicklen being # received from the uplink. Plugins that depend on maxnicklen being
@ -44,17 +45,19 @@ class Irc():
# is also dependent on the protocol module. # is also dependent on the protocol module.
self.maxnicklen = 30 self.maxnicklen = 30
self.prefixmodes = 'ov' self.prefixmodes = 'ov'
# Uplink SID (filled in by protocol module) # Uplink SID (filled in by protocol module)
self.uplink = None self.uplink = None
self.start_ts = int(time.time())
# UID generators, for servers that need it
self.uidgen = {}
def __init__(self, netname, proto, conf): def __init__(self, netname, proto, conf):
# Initialize some variables # Initialize some variables
self.connected = threading.Event() self.connected = threading.Event()
self.name = netname.lower() self.name = netname.lower()
self.conf = conf self.conf = conf
self.initVars()
self.serverdata = conf['servers'][netname] self.serverdata = conf['servers'][netname]
self.sid = self.serverdata["sid"] self.sid = self.serverdata["sid"]
self.botdata = conf['bot'] self.botdata = conf['bot']
@ -62,6 +65,8 @@ class Irc():
self.pingfreq = self.serverdata.get('pingfreq') or 10 self.pingfreq = self.serverdata.get('pingfreq') or 10
self.pingtimeout = self.pingfreq * 2 self.pingtimeout = self.pingfreq * 2
self.initVars()
self.connection_thread = threading.Thread(target = self.connect) self.connection_thread = threading.Thread(target = self.connect)
self.connection_thread.start() self.connection_thread.start()
self.pingTimer = None self.pingTimer = None

View File

@ -291,16 +291,14 @@ def pingServer(irc, source=None, target=None):
_send(irc, source, 'PING %s %s' % (source, target)) _send(irc, source, 'PING %s %s' % (source, target))
def connect(irc): def connect(irc):
irc.start_ts = ts = int(time.time()) ts = irc.start_ts
irc.uidgen = {} irc.uidgen = {}
host = irc.serverdata["hostname"]
irc.servers[irc.sid] = IrcServer(None, host, internal=True)
f = irc.send f = irc.send
f('CAPAB START 1202') f('CAPAB START 1202')
f('CAPAB CAPABILITIES :PROTOCOL=1202') f('CAPAB CAPABILITIES :PROTOCOL=1202')
f('CAPAB END') f('CAPAB END')
f('SERVER {host} {Pass} 0 {sid} :PyLink Service'.format(host=host, f('SERVER {host} {Pass} 0 {sid} :PyLink Service'.format(host=irc.serverdata["hostname"],
Pass=irc.serverdata["sendpass"], sid=irc.sid)) Pass=irc.serverdata["sendpass"], sid=irc.sid))
f(':%s BURST %s' % (irc.sid, ts)) f(':%s BURST %s' % (irc.sid, ts))
f(':%s ENDBURST' % (irc.sid)) f(':%s ENDBURST' % (irc.sid))

View File

@ -8,6 +8,7 @@ from collections import defaultdict
import inspircd import inspircd
import classes import classes
import utils import utils
import coreplugin
class TestProtoInspIRCd(unittest.TestCase): class TestProtoInspIRCd(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -19,21 +19,22 @@ class TestRelay(unittest.TestCase):
self.f = relay.normalizeNick self.f = relay.normalizeNick
def testNormalizeNick(self): def testNormalizeNick(self):
self.assertEqual(self.f(self.irc, 'helloworld'), 'helloworld/unittest') # Second argument simply states the suffix.
self.assertEqual(self.f(self.irc, 'ObnoxiouslyLongNick'), 'Obnoxiously/unittest') self.assertEqual(self.f(self.irc, 'unittest', 'helloworld'), 'helloworld/unittest')
self.assertEqual(self.f(self.irc, '10XAAAAAA'), '_10XAAAAAA/unittest') self.assertEqual(self.f(self.irc, 'unittest', 'ObnoxiouslyLongNick'), 'Obnoxiously/unittest')
self.assertEqual(self.f(self.irc, 'unittest', '10XAAAAAA'), '_10XAAAAAA/unittest')
def testNormalizeNickConflict(self): def testNormalizeNickConflict(self):
self.assertEqual(self.f(self.irc, 'helloworld'), 'helloworld/unittest') self.assertEqual(self.f(self.irc, 'unittest', 'helloworld'), 'helloworld/unittest')
self.irc.users['10XAAAAAA'] = classes.IrcUser('helloworld/unittest', 1234, '10XAAAAAA') self.irc.users['10XAAAAAA'] = classes.IrcUser('helloworld/unittest', 1234, '10XAAAAAA')
# Increase amount of /'s by one # Increase amount of /'s by one
self.assertEqual(self.f(self.irc, 'helloworld'), 'helloworld//unittest') self.assertEqual(self.f(self.irc, 'unittest', 'helloworld'), 'helloworld//unittest')
self.irc.users['10XAAAAAB'] = classes.IrcUser('helloworld//unittest', 1234, '10XAAAAAB') self.irc.users['10XAAAAAB'] = classes.IrcUser('helloworld//unittest', 1234, '10XAAAAAB')
# Cut off the nick, not the suffix if the result is too long. # Cut off the nick, not the suffix if the result is too long.
self.assertEqual(self.f(self.irc, 'helloworld'), 'helloworl///unittest') self.assertEqual(self.f(self.irc, 'unittest', 'helloworld'), 'helloworl///unittest')
def testNormalizeNickRemovesSlashes(self): def testNormalizeNickRemovesSlashes(self):
self.irc.proto.__name__ = "charybdis" self.irc.proto.__name__ = "charybdis"
self.assertEqual(self.f(self.irc, 'helloworld'), 'helloworld|unittest') self.assertEqual(self.f(self.irc, 'unittest', 'helloworld'), 'helloworld|unittest')
self.assertEqual(self.f(self.irc, 'abcde/eJanus'), 'abcde|eJanu|unittest') self.assertEqual(self.f(self.irc, 'unittest', 'abcde/eJanus'), 'abcde|eJanu|unittest')
self.assertEqual(self.f(self.irc, 'ObnoxiouslyLongNick'), 'Obnoxiously|unittest') self.assertEqual(self.f(self.irc, 'unittest', 'ObnoxiouslyLongNick'), 'Obnoxiously|unittest')