From 3b79e21684aa31b568aeb3969d034e772ac9e610 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Fri, 14 Oct 2011 12:00:50 +0100 Subject: [PATCH 1/2] Test grabbing from MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a contact is not on your roster, you typically have no idea what their nickname is: no roster, no PEP, no vCard (assuming the server doesn't let random people fetch your vCard). In this situation, contacts who message you out of the blue can include in the message itself. This is implemented in a kind of dodgy way in Gabble at the moment: the IM channel forcibly retains an entry for the contact in the presence cache, and then the presence cache stashes the nickname as if it came from presence… It was also previously untested, so I thought it worth adding a test before I even thought about fixing how it's implemented. --- src/presence-cache.c | 10 +++ tests/twisted/Makefile.am | 1 + tests/twisted/vcard/test-alias-message.py | 105 +++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 0 deletions(-) create mode 100644 tests/twisted/vcard/test-alias-message.py diff --git a/src/presence-cache.c b/src/presence-cache.c index 89a6534..5e939a8 100644 --- a/src/presence-cache.c +++ b/src/presence-cache.c @@ -1848,6 +1848,16 @@ gabble_presence_parse_presence_message (GabblePresenceCache *cache, return ret; } +/* FIXME: this scrapes nicknames out of , and relies on im-channel.c + * setting keep_unavailable back to FALSE to make nicknames random peers send + * us disappear once we close the accompanying messages. As a side effect, it + * makes specifying in MUC messages work, which is questionable + * behaviour. See vcard/test-alias-message.py. + * + * It would be cleaner to make the IM channel stash the nickname if we want it + * to go away when the channel closes, rather than relying on this + * spooky-action-at-a-distance. + */ static LmHandlerResult _parse_message_message (GabblePresenceCache *cache, TpHandle handle, diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am index 392a128..7418157 100644 --- a/tests/twisted/Makefile.am +++ b/tests/twisted/Makefile.am @@ -133,6 +133,7 @@ TWISTED_VCARD_TESTS = \ vcard/set-set-disconnect.py \ vcard/supported-fields.py \ vcard/test-alias-empty-vcard.py \ + vcard/test-alias-message.py \ vcard/test-alias-pep.py \ vcard/test-alias.py \ vcard/test-avatar-async.py \ diff --git a/tests/twisted/vcard/test-alias-message.py b/tests/twisted/vcard/test-alias-message.py new file mode 100644 index 0000000..362036a --- /dev/null +++ b/tests/twisted/vcard/test-alias-message.py @@ -0,0 +1,105 @@ +# vim: set fileencoding=utf-8 : +""" +Tests grabbing aliases from incoming messages, as described by +. + +This test has nothing to do with vcards, just like a lot of other tests in the +vcard/ directory. +""" + +from servicetest import EventPattern, assertEquals, wrap_channel +from gabbletest import exec_test, elem, expect_and_handle_get_vcard +from mucutil import join_muc, make_muc_presence + +import constants as cs +import ns + +def test(q, bus, conn, stream): + expect_and_handle_get_vcard(q, stream) + + jid = u'bora.horza.gobuchul@culture.lit' + alias = u'Horza' + handle = conn.RequestHandles(cs.HT_CONTACT, [jid])[0] + + # We don't have an interesting alias for Horza + assertEquals({handle: jid}, conn.Aliasing.GetAliases([handle])) + + # Horza sends us a message containing his preferred nickname. + stream.send( + elem('message', from_=jid, type='chat')( + elem('body')(u"It's a long story."), + elem(ns.NICK, 'nick')(alias) + )) + _, mr = q.expect_many( + EventPattern('dbus-signal', signal='AliasesChanged', + args=[[(handle, alias)]]), + EventPattern('dbus-signal', signal='MessageReceived'), + ) + + channel = wrap_channel(bus.get_object(conn.bus_name, mr.path), 'Text') + + # So now we know his alias. + assertEquals({handle: alias}, conn.Aliasing.GetAliases([handle])) + + # Presumably to avoid non-contacts being able to make Gabble's memory + # footprint grow forever, Gabble throws the alias away when we close the + # channel. + header = mr.args[0][0] + channel.Text.AcknowledgePendingMessages([header['pending-message-id']]) + channel.Close() + + # FIXME: Gabble forgets the alias, but it doesn't signal that it has done + # so; it probably should. + # q.expect('dbus-signal', signal='AliasesChanged', args=[[(handle, jid)]]) + assertEquals({handle: jid}, conn.Aliasing.GetAliases([handle])) + + + # Basically the same test, but in a MUC. + # + # It's a bit questionable whether this ought to work. + # doesn't have anything to + # say about including in messages; it does talk about including + # in your MUC presence, which is actually equally sketchy! If I join + # a muc with the resource '/wjt', and you join with resource '/ohai' but + # say that your nickname is 'wjt', what on earth is Alice's UI supposed to + # show when you send a message? + # + # But anyway, at the time of writing this "works", so I'm adding a test to + # make it explicit. Perhaps in future we might change this test to verify + # that it doesn't "work". + room_jid = 'clear-air-turbulence@culture.lit' + _, muc, _, _ = join_muc(q, bus, conn, stream, room_jid) + + bob_jid = room_jid + '/bob' + bob_handle = conn.RequestHandles(cs.HT_CONTACT, [bob_jid])[0] + + assertEquals({bob_handle: 'bob'}, conn.Aliasing.GetAliases([bob_handle])) + + stream.send( + elem('message', from_=bob_jid, type='groupchat')( + elem('body')(u'My religion dies with me.'), + elem(ns.NICK, 'nick')(alias), + )) + + q.expect_many( + EventPattern('dbus-signal', signal='AliasesChanged', + args=[[(bob_handle, alias)]]), + EventPattern('dbus-signal', signal='MessageReceived'),) + + assertEquals({bob_handle: alias}, conn.Aliasing.GetAliases([bob_handle])) + + muc.Close() + q.expect('stream-presence', to=room_jid + '/test') + echo = make_muc_presence('member', 'none', room_jid, 'test') + echo['type'] = 'unavailable' + stream.send(echo) + q.expect('dbus-signal', signal='ChannelClosed') + + # FIXME: Gabble forgets the alias, but it doesn't signal that it has done + # so; it probably should. + # q.expect('dbus-signal', signal='AliasesChanged', + # args=[[(bob_handle, 'bob')]]) + assertEquals({bob_handle: 'bob'}, conn.Aliasing.GetAliases([bob_handle])) + +if __name__ == '__main__': + exec_test(test) -- 1.7.7