[Telepathy-commits] [telepathy-idle/master] Add regression test for leading-space messages

Jonathon Jongsma jonathon.jongsma at collabora.co.uk
Fri Feb 13 17:43:16 PST 2009


There was a long-standing bug in telepathy-idle where a received message that
had a leading space was not parsed correctly and was dropped silently.  This bug
has recently been fixed, and this is simply a regression test for it.
---
 tests/twisted/Makefile.am               |    1 +
 tests/twisted/idletest.py               |   56 +++++++++++++++++++++----------
 tests/twisted/messages/leading-space.py |   55 ++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 18 deletions(-)
 create mode 100644 tests/twisted/messages/leading-space.py

diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am
index 0572a71..7b1d3b0 100644
--- a/tests/twisted/Makefile.am
+++ b/tests/twisted/Makefile.am
@@ -5,6 +5,7 @@ TWISTED_TESTS = \
 		connect/connect-fail-ssl.py \
 		channels/join-muc-channel.py \
 		messages/message-order.py \
+		messages/leading-space.py \
 		$(NULL)
 
 TESTS =
diff --git a/tests/twisted/idletest.py b/tests/twisted/idletest.py
index f5c2d1a..72b1a3b 100644
--- a/tests/twisted/idletest.py
+++ b/tests/twisted/idletest.py
@@ -48,30 +48,50 @@ class BaseIRCServer(irc.IRC):
         print ("connection Lost  %s" % reason)
         self.event_func(make_disconnected_event())
 
-    def dataReceived(self, data):
-        print ("data received: %s" % (data,))
-        (prefix, command, args) = irc.parsemsg(data)
-        if command == 'PRIVMSG':
+    def handlePRIVMSG(self, args, prefix):
             self.event_func(make_privmsg_event(args[0], ' '.join(args[1:]).rstrip('\r\n')))
+
         #handle 'login' handshake
-        elif command == 'PASS':
-            self.passwd = args[0]
-        elif command == 'NICK':
-            self.nick = args[0]
-        elif command == 'USER':
-            self.user = args[0]
-            if ((not self.require_pass) or (self.passwd is not None)) \
-                and (self.nick is not None and self.user is not None):
-                    self.sendWelcome()
-        elif command == 'JOIN':
-            print ("Joined channel %s" % args[0])
-            self.sendMessage('JOIN', args[0], prefix=self.nick)
-        elif command == 'QUIT':
-            self.transport.loseConnection()
+    def handlePASS(self, args, prefix):
+        self.passwd = args[0]
+    def handleNICK(self, args, prefix):
+        self.nick = args[0]
+    def handleUSER(self, args, prefix):
+        self.user = args[0]
+        if ((not self.require_pass) or (self.passwd is not None)) \
+            and (self.nick is not None and self.user is not None):
+                self.sendWelcome()
+
+    def handleJOIN(self, args, prefix):
+        room = args[0]
+        self.sendMessage('JOIN', room, prefix=self.nick)
+        self._sendNameReply(room, [self.nick])
+
+    def _sendNameReply(self, room, members):
+        #namereply
+        self.sendMessage('353', '%s = %s' % (self.nick, room), ":%s" % ' '.join(members),
+                prefix='idle.test.server')
+        #namereply end
+        self.sendMessage('366', self.nick, room, ':End if /NAMES list', prefix='idle.test.server')
+
+    def handleQUIT(self, args, prefix):
+        self.transport.loseConnection()
 
     def sendWelcome(self):
         self.sendMessage('001', self.nick, ':Welcome to the test IRC Network', prefix='idle.test.server')
 
+    def dataReceived(self, data):
+        print ("data received: %s" % (data,))
+        (_prefix, _command, _args) = irc.parsemsg(data)
+        try:
+            f = getattr(self, 'handle%s' % _command)
+            try:
+                f(_args, _prefix)
+            except Exception, e:
+                print 'handler failed: %s' % e
+        except Exception, e:
+            print 'No handler for command %s: %s' % (_command, e)
+
 class SSLIRCServer(BaseIRCServer):
     def listen(self, port, factory):
         print ("SSLIRCServer listening...")
diff --git a/tests/twisted/messages/leading-space.py b/tests/twisted/messages/leading-space.py
new file mode 100644
index 0000000..a621993
--- /dev/null
+++ b/tests/twisted/messages/leading-space.py
@@ -0,0 +1,55 @@
+
+"""
+Test that messages that are sent with a leading space are parsed correctly by
+telepathy-idle.  This is a regression test for a long-standing bug that was
+recently fixed
+"""
+
+from idletest import exec_test, BaseIRCServer
+from servicetest import EventPattern, call_async
+import dbus
+
+MESSAGE_WITH_LEADING_SPACE=' This is a message with a leading space'
+class LeadingSpaceIRCServer(BaseIRCServer):
+    remoteuser = 'remoteuser'
+
+    def handlePRIVMSG(self, args, prefix):
+        #chain up to the base class implementation which simply signals a privmsg event
+        BaseIRCServer.handlePRIVMSG(self, args, prefix)
+        sender = prefix
+        recipient = args[0]
+        self.sendMessage('PRIVMSG', recipient, ':%s' % MESSAGE_WITH_LEADING_SPACE, prefix=self.remoteuser)
+
+    def handleJOIN(self, args, prefix):
+        room = args[0]
+        self.sendMessage('JOIN', room, prefix=self.nick)
+        self._sendNameReply(room, [self.nick, self.remoteuser])
+
+
+def test(q, bus, conn, stream):
+    conn.Connect()
+    q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])
+    CHANNEL_NAME = '#idletest'
+    room_handles = conn.RequestHandles(2, [CHANNEL_NAME])
+    call_async(q, conn, 'RequestChannel', 'org.freedesktop.Telepathy.Channel.Type.Text', 2, room_handles[0], True)
+
+    ret = q.expect('dbus-return', method='RequestChannel')
+    q.expect('dbus-signal', signal='MembersChanged')
+    chan = bus.get_object(conn.bus_name, ret.value[0])
+
+    text_chan = dbus.Interface(chan,
+        u'org.freedesktop.Telepathy.Channel.Type.Text')
+    # send a message
+    call_async(q, text_chan, 'Send', 0, 'foo')
+    q.expect('irc-privmsg')
+    # the test server above is rigged to send a reply message with a leading
+    # space in response to our PRIVMSG.  If telepathy-idle parses this message
+    # correctly, we should emit a 'Received' signal
+    q.expect('dbus-signal', signal='Received', predicate=lambda x: x.args[5]==MESSAGE_WITH_LEADING_SPACE)
+
+    call_async(q, conn, 'Disconnect')
+    return True
+
+if __name__ == '__main__':
+    exec_test(test, timeout=10, protocol=LeadingSpaceIRCServer)
+
-- 
1.5.6.5




More information about the telepathy-commits mailing list