PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/xmpp4r/test/caps/tc_helper.rb

https://github.com/edavis10/cerberus
Ruby | 156 lines | 110 code | 23 blank | 23 comment | 0 complexity | 0622bffd8309635cc950985a895b2f5d MD5 | raw file
Possible License(s): GPL-2.0
  1. $:.unshift File::dirname(__FILE__) + '/../../lib'
  2. require 'test/unit'
  3. require File::dirname(__FILE__) + '/../lib/clienttester'
  4. require 'xmpp4r/discovery'
  5. require 'xmpp4r/caps/helper/helper'
  6. require 'xmpp4r/dataforms'
  7. include Jabber
  8. class Caps::HelperTest < Test::Unit::TestCase
  9. include ClientTester
  10. ##
  11. # Walk through the client/ server conversation defined
  12. # in http://www.xmpp.org/extensions/xep-0115.html#usecases
  13. # and assert conformance.
  14. def test_caps_reply
  15. # This will be invoked by 'wait_state' below...
  16. state { |presence|
  17. assert_kind_of(Jabber::Presence, presence)
  18. c = presence.first_element('c')
  19. assert_kind_of(Jabber::Caps::C, c)
  20. # see http://www.xmpp.org/extensions/xep-0115.html#ver
  21. assert_equal('SrFo9ar2CCk2EnOH4q4QANeuxLQ=', c.ver)
  22. # version 1.5 of xep 0115 indicates that the <c /> stanzq MUST feature a 'hash' attribute
  23. assert_equal('sha-1', c.hash)
  24. assert_equal("http://home.gna.org/xmpp4r/##{Jabber::XMPP4R_VERSION}", c.node)
  25. send(iq_discovering_capabilities)
  26. }
  27. # Construct Caps::Helper which will send a <presence>
  28. # stanza (with embedded <c/> advert) to the 'server'
  29. h = Caps::Helper.new(@client, identities, features)
  30. # The 'server' will receive the <presence> stanza and
  31. # yield it to the 'state' block above, where an <iq> query
  32. # will be sent back to the 'client,' to discover its capabilities.
  33. # Wait here until the block has been executed.
  34. wait_state
  35. # The Caps::Helper will process the <iq> query from the 'server'
  36. # and reply with an <iq> result providing the details of its
  37. # identities and features.
  38. state { |iq|
  39. assert_kind_of(Jabber::Iq, iq)
  40. assert_equal(:result, iq.type)
  41. assert_equal('1867999907', iq.id)
  42. assert_equal('new-big-computer.local', iq.to.to_s)
  43. assert_kind_of(Jabber::Discovery::IqQueryDiscoInfo, iq.query)
  44. assert_equal(iq.query.identities.size, identities.size)
  45. get_category_iname_type = lambda{ |i| [i.category,i.iname,i.type] }
  46. assert_equal(iq.query.identities.map(&get_category_iname_type).sort!, identities.map(&get_category_iname_type).sort!)
  47. assert_equal(3, iq.query.features.size)
  48. get_var = lambda { |f| f.var }
  49. assert_equal(iq.query.features.sort, features.map(&get_var).sort)
  50. }
  51. # The 'server' will receive the <iq> result from the
  52. # 'client' and yield it to the block above. Wait here
  53. # until that block exits.
  54. wait_state
  55. end
  56. def test_custom_node
  57. client_id='http://new-big-computer.local/client#321'
  58. state { |presence|
  59. c = presence.first_element('c')
  60. assert_kind_of(Jabber::Caps::C, c)
  61. assert_equal(client_id, c.node)
  62. assert_equal('SrFo9ar2CCk2EnOH4q4QANeuxLQ=', c.ver)
  63. }
  64. h = Caps::Helper.new(@client, identities, features, client_id)
  65. wait_state
  66. end
  67. def identities
  68. [Jabber::Discovery::Identity.new('client', 'Exodus 0.9.1', 'pc')]
  69. end
  70. def features
  71. [Jabber::Discovery::Feature.new("http://jabber.org/protocol/disco#info"),
  72. Jabber::Discovery::Feature.new("http://jabber.org/protocol/disco#items"),
  73. Jabber::Discovery::Feature.new("http://jabber.org/protocol/muc")]
  74. end
  75. def iq_discovering_capabilities
  76. "<iq from='new-big-computer.local' type='get' to='matt@new-big-computer.local/capable_client' id='1867999907' xmlns='jabber:client'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>"
  77. end
  78. ##
  79. # http://www.xmpp.org/extensions/xep-0115.html#ver-gen-complex
  80. def test_caps_complex
  81. form = Dataforms::XData.new(:result)
  82. form.add(Dataforms::XDataField.new('FORM_TYPE', :hidden)).value = 'urn:xmpp:dataforms:softwareinfo'
  83. form.add(Dataforms::XDataField.new('ip_version')).values = ['ipv4', 'ipv6']
  84. form.add(Dataforms::XDataField.new('software')).value = 'Psi' # re-ordered
  85. form.add(Dataforms::XDataField.new('software_version')).value = '0.11'
  86. form.add(Dataforms::XDataField.new('os')).value = 'Mac'
  87. form.add(Dataforms::XDataField.new('os_version')).value = '10.5.1'
  88. ver = Caps::generate_ver([Discovery::Identity.new('client', 'Psi 0.9.1', 'pc').set_xml_lang('en'),
  89. Discovery::Identity.new('client', 'Ψ 0.9.1', 'pc').set_xml_lang('el')],
  90. [Discovery::Feature.new('http://jabber.org/protocol/muc'), # re-ordered
  91. Discovery::Feature.new('http://jabber.org/protocol/disco#info'),
  92. Discovery::Feature.new('http://jabber.org/protocol/disco#items')],
  93. [form])
  94. assert_equal('8lu+88MRxmKM7yO3MEzY7YmTsWs=', ver)
  95. end
  96. ##
  97. # http://www.xmpp.org/extensions/xep-0115.html#ver-gen-complex
  98. def test_caps_complex_imported
  99. query = IqQuery::import(REXML::Document.new(<<END).root)
  100. <query xmlns='http://jabber.org/protocol/disco#info'
  101. node='http://psi-im.org#8lu+88MRxmKM7yO3MEzY7YmTsWs='>
  102. <identity xml:lang='en' category='client' name='Psi 0.9.1' type='pc'/>
  103. <identity xml:lang='el' category='client' name='Ψ 0.9.1' type='pc'/>
  104. <feature var='http://jabber.org/protocol/disco#info'/>
  105. <feature var='http://jabber.org/protocol/disco#items'/>
  106. <feature var='http://jabber.org/protocol/muc'/>
  107. <x xmlns='jabber:x:data' type='result'>
  108. <field var='FORM_TYPE' type='hidden'>
  109. <value>urn:xmpp:dataforms:softwareinfo</value>
  110. </field>
  111. <field var='ip_version'>
  112. <value>ipv4</value>
  113. <value>ipv6</value>
  114. </field>
  115. <field var='os'>
  116. <value>Mac</value>
  117. </field>
  118. <field var='os_version'>
  119. <value>10.5.1</value>
  120. </field>
  121. <field var='software'>
  122. <value>Psi</value>
  123. </field>
  124. <field var='software_version'>
  125. <value>0.11</value>
  126. </field>
  127. </x>
  128. </query>
  129. END
  130. assert_equal('8lu+88MRxmKM7yO3MEzY7YmTsWs=',
  131. Caps::generate_ver_from_discoinfo(query))
  132. end
  133. end