xmpp4r 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/COPYING +340 -0
  2. data/ChangeLog +28 -0
  3. data/LICENSE +59 -0
  4. data/README +20 -0
  5. data/Rakefile +103 -0
  6. data/UPDATING +40 -0
  7. data/data/doc/xmpp4r/examples/advanced/adventure/README +57 -0
  8. data/data/doc/xmpp4r/examples/advanced/adventure/adventure.rb +23 -0
  9. data/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb +136 -0
  10. data/data/doc/xmpp4r/examples/advanced/adventure/cube.xml +15 -0
  11. data/data/doc/xmpp4r/examples/advanced/adventure/tower.xml +69 -0
  12. data/data/doc/xmpp4r/examples/advanced/adventure/world.rb +425 -0
  13. data/data/doc/xmpp4r/examples/advanced/fileserve.conf +11 -0
  14. data/data/doc/xmpp4r/examples/advanced/fileserve.rb +344 -0
  15. data/data/doc/xmpp4r/examples/advanced/getonline.rb +56 -0
  16. data/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb +315 -0
  17. data/data/doc/xmpp4r/examples/advanced/migrate.rb +89 -0
  18. data/data/doc/xmpp4r/examples/advanced/minimuc.rb +266 -0
  19. data/data/doc/xmpp4r/examples/advanced/recvfile.rb +83 -0
  20. data/data/doc/xmpp4r/examples/advanced/rosterdiscovery.rb +130 -0
  21. data/data/doc/xmpp4r/examples/advanced/sendfile.conf +10 -0
  22. data/data/doc/xmpp4r/examples/advanced/sendfile.rb +72 -0
  23. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb +51 -0
  24. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_jabber.rb +43 -0
  25. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_test.rb +10 -0
  26. data/data/doc/xmpp4r/examples/advanced/versionpoll.rb +90 -0
  27. data/data/doc/xmpp4r/examples/advanced/xmpping.rb +134 -0
  28. data/data/doc/xmpp4r/examples/advanced/xmppingrc.sample +9 -0
  29. data/data/doc/xmpp4r/examples/basic/change_password.rb +41 -0
  30. data/data/doc/xmpp4r/examples/basic/client.rb +68 -0
  31. data/data/doc/xmpp4r/examples/basic/component.rb +11 -0
  32. data/data/doc/xmpp4r/examples/basic/echo_nonthreaded.rb +32 -0
  33. data/data/doc/xmpp4r/examples/basic/echo_threaded.rb +32 -0
  34. data/data/doc/xmpp4r/examples/basic/jabbersend.rb +41 -0
  35. data/data/doc/xmpp4r/examples/basic/mass_sender.rb +67 -0
  36. data/data/doc/xmpp4r/examples/basic/mucinfo.rb +39 -0
  37. data/data/doc/xmpp4r/examples/basic/mucsimplebot.rb +83 -0
  38. data/data/doc/xmpp4r/examples/basic/register.rb +25 -0
  39. data/data/doc/xmpp4r/examples/basic/remove_registration.rb +18 -0
  40. data/data/doc/xmpp4r/examples/basic/roster.rb +42 -0
  41. data/data/doc/xmpp4r/examples/basic/rosterprint.rb +50 -0
  42. data/data/doc/xmpp4r/examples/basic/rosterrename.rb +34 -0
  43. data/data/doc/xmpp4r/examples/basic/rosterwatch.rb +172 -0
  44. data/data/doc/xmpp4r/examples/basic/send_vcard.rb +68 -0
  45. data/data/doc/xmpp4r/examples/basic/versionbot.rb +75 -0
  46. data/data/doc/xmpp4r/examples/buggy/jabber2jabber/jabber2jabber.rb +18 -0
  47. data/data/doc/xmpp4r/examples/buggy/miniedgarr_cgi.rb +192 -0
  48. data/data/doc/xmpp4r/examples/buggy/miniedgarr_watch.rb +82 -0
  49. data/lib/callbacks.rb +122 -0
  50. data/lib/xmpp4r/authenticationfailure.rb +13 -0
  51. data/lib/xmpp4r/bytestreams/helper/filetransfer.rb +315 -0
  52. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +256 -0
  53. data/lib/xmpp4r/bytestreams/helper/ibb/initiator.rb +30 -0
  54. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +46 -0
  55. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +151 -0
  56. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb +85 -0
  57. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb +178 -0
  58. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/socks5.rb +56 -0
  59. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb +61 -0
  60. data/lib/xmpp4r/bytestreams/iq/bytestreams.rb +177 -0
  61. data/lib/xmpp4r/bytestreams/iq/si.rb +221 -0
  62. data/lib/xmpp4r/bytestreams.rb +11 -0
  63. data/lib/xmpp4r/client.rb +249 -0
  64. data/lib/xmpp4r/component.rb +103 -0
  65. data/lib/xmpp4r/connection.rb +166 -0
  66. data/lib/xmpp4r/dataforms/x/data.rb +248 -0
  67. data/lib/xmpp4r/dataforms.rb +1 -0
  68. data/lib/xmpp4r/debuglog.rb +34 -0
  69. data/lib/xmpp4r/delay/x/delay.rb +100 -0
  70. data/lib/xmpp4r/delay.rb +1 -0
  71. data/lib/xmpp4r/discovery/iq/discoinfo.rb +225 -0
  72. data/lib/xmpp4r/discovery/iq/discoitems.rb +162 -0
  73. data/lib/xmpp4r/discovery.rb +2 -0
  74. data/lib/xmpp4r/error.rb +230 -0
  75. data/lib/xmpp4r/errorexception.rb +32 -0
  76. data/lib/xmpp4r/feature_negotiation/iq/feature.rb +42 -0
  77. data/lib/xmpp4r/feature_negotiation.rb +1 -0
  78. data/lib/xmpp4r/idgenerator.rb +37 -0
  79. data/lib/xmpp4r/iq.rb +229 -0
  80. data/lib/xmpp4r/jid.rb +167 -0
  81. data/lib/xmpp4r/message.rb +171 -0
  82. data/lib/xmpp4r/muc/helper/mucbrowser.rb +107 -0
  83. data/lib/xmpp4r/muc/helper/mucclient.rb +382 -0
  84. data/lib/xmpp4r/muc/helper/simplemucclient.rb +222 -0
  85. data/lib/xmpp4r/muc/x/muc.rb +98 -0
  86. data/lib/xmpp4r/muc/x/mucuserinvite.rb +58 -0
  87. data/lib/xmpp4r/muc/x/mucuseritem.rb +148 -0
  88. data/lib/xmpp4r/muc.rb +6 -0
  89. data/lib/xmpp4r/presence.rb +255 -0
  90. data/lib/xmpp4r/query.rb +43 -0
  91. data/lib/xmpp4r/rexmladdons.rb +826 -0
  92. data/lib/xmpp4r/roster/helper/roster.rb +514 -0
  93. data/lib/xmpp4r/roster/iq/roster.rb +244 -0
  94. data/lib/xmpp4r/roster/x/roster.rb +155 -0
  95. data/lib/xmpp4r/roster.rb +4 -0
  96. data/lib/xmpp4r/sasl.rb +167 -0
  97. data/lib/xmpp4r/stream.rb +543 -0
  98. data/lib/xmpp4r/streamparser.rb +77 -0
  99. data/lib/xmpp4r/vcard/helper/vcard.rb +86 -0
  100. data/lib/xmpp4r/vcard/iq/vcard.rb +102 -0
  101. data/lib/xmpp4r/vcard.rb +3 -0
  102. data/lib/xmpp4r/version/helper/responder.rb +71 -0
  103. data/lib/xmpp4r/version/helper/simpleresponder.rb +44 -0
  104. data/lib/xmpp4r/version/iq/version.rb +118 -0
  105. data/lib/xmpp4r/version.rb +3 -0
  106. data/lib/xmpp4r/x.rb +43 -0
  107. data/lib/xmpp4r/xmlstanza.rb +174 -0
  108. data/lib/xmpp4r/xmpp4r.rb +16 -0
  109. data/lib/xmpp4r.rb +122 -0
  110. data/setup.rb +1360 -0
  111. data/test/bytestreams/tc_ibb.rb +186 -0
  112. data/test/bytestreams/tc_socks5bytestreams.rb +57 -0
  113. data/test/delay/tc_xdelay.rb +51 -0
  114. data/test/lib/clienttester.rb +110 -0
  115. data/test/muc/tc_muc_mucclient.rb +569 -0
  116. data/test/muc/tc_muc_simplemucclient.rb +72 -0
  117. data/test/roster/.tc_helper.rb.swp +0 -0
  118. data/test/roster/tc_helper.rb +389 -0
  119. data/test/roster/tc_iqqueryroster.rb +140 -0
  120. data/test/roster/tc_xroster.rb +70 -0
  121. data/test/tc_callbacks.rb +128 -0
  122. data/test/tc_class_names.rb +129 -0
  123. data/test/tc_client.rb +30 -0
  124. data/test/tc_error.rb +103 -0
  125. data/test/tc_idgenerator.rb +30 -0
  126. data/test/tc_iq.rb +109 -0
  127. data/test/tc_iqquery.rb +31 -0
  128. data/test/tc_jid.rb +202 -0
  129. data/test/tc_message.rb +114 -0
  130. data/test/tc_presence.rb +148 -0
  131. data/test/tc_stream.rb +182 -0
  132. data/test/tc_streamError.rb +87 -0
  133. data/test/tc_streamSend.rb +59 -0
  134. data/test/tc_streamThreaded.rb +168 -0
  135. data/test/tc_xmlstanza.rb +76 -0
  136. data/test/ts_xmpp4r.rb +34 -0
  137. data/test/vcard/tc_iqvcard.rb +52 -0
  138. data/test/version/tc_helper.rb +46 -0
  139. data/test/version/tc_iqqueryversion.rb +96 -0
  140. data/tools/doctoweb.bash +30 -0
  141. data/tools/gen_requires.bash +10 -0
  142. metadata +232 -0
data/test/tc_jid.rb ADDED
@@ -0,0 +1,202 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift '../lib'
4
+
5
+ require 'test/unit'
6
+ require 'xmpp4r/jid'
7
+ include Jabber
8
+
9
+ class JIDTest < Test::Unit::TestCase
10
+ def test_create1
11
+ j = JID::new('a', 'b', 'c')
12
+ assert_equal('a', j.node)
13
+ assert_equal('b', j.domain)
14
+ assert_equal('c', j.resource)
15
+ end
16
+
17
+ def test_create2
18
+ j = JID::new('a', 'b', 'c')
19
+ j2 = JID::new(j)
20
+ assert_equal('a', j2.node)
21
+ assert_equal('b', j2.domain)
22
+ assert_equal('c', j2.resource)
23
+ assert_equal('a@b/c', j.to_s)
24
+ end
25
+
26
+ def test_create3
27
+ j = JID::new('a@b/c')
28
+ assert_equal('a', j.node)
29
+ assert_equal('b', j.domain)
30
+ assert_equal('c', j.resource)
31
+ assert_equal('a@b/c', j.to_s)
32
+ end
33
+
34
+ def test_create4
35
+ j = JID::new('a@b')
36
+ assert_equal('a', j.node)
37
+ assert_equal('b', j.domain)
38
+ assert_equal(nil, j.resource)
39
+ assert_equal('a@b', j.to_s)
40
+ end
41
+
42
+ def test_create5
43
+ j = JID::new
44
+ assert_equal(nil, j.node)
45
+ assert_equal(nil, j.domain)
46
+ assert_equal(nil, j.resource)
47
+ assert_equal('', j.to_s)
48
+ end
49
+
50
+ def test_create6
51
+ j = JID::new('dom')
52
+ assert_equal(nil, j.node)
53
+ assert_equal('dom', j.domain)
54
+ assert_equal(nil, j.resource)
55
+ assert_equal('dom', j.to_s)
56
+ end
57
+
58
+ def test_create7
59
+ j = JID::new('dom/res')
60
+ assert_equal(nil, j.node)
61
+ assert_equal('dom', j.domain)
62
+ assert_equal('res', j.resource)
63
+ assert_equal('dom/res', j.to_s)
64
+ end
65
+
66
+ def test_create8
67
+ j = JID::new('dom/a@b')
68
+ assert_equal(nil, j.node)
69
+ assert_equal('dom', j.domain)
70
+ assert_equal('a@b', j.resource)
71
+ assert_equal('dom/a@b', j.to_s)
72
+ end
73
+
74
+ def test_create9
75
+ assert_nothing_raised { JID::new("#{'n'*1023}@#{'d'*1023}/#{'r'*1023}") }
76
+ assert_raises(ArgumentError) { JID::new("#{'n'*1024}@#{'d'*1023}/#{'r'*1023}") }
77
+ assert_raises(ArgumentError) { JID::new("#{'n'*1023}@#{'d'*1024}/#{'r'*1023}") }
78
+ assert_raises(ArgumentError) { JID::new("#{'n'*1023}@#{'d'*1023}/#{'r'*1024}") }
79
+ end
80
+
81
+ def test_create10
82
+ j = JID::new('@b/c')
83
+ assert_equal('', j.node)
84
+ assert_equal('b', j.domain)
85
+ assert_equal('c', j.resource)
86
+ assert_equal('@b/c', j.to_s)
87
+ end
88
+
89
+ def test_create11
90
+ j = JID::new('@b')
91
+ assert_equal('', j.node)
92
+ assert_equal('b', j.domain)
93
+ assert_equal(nil, j.resource)
94
+ assert_equal('@b', j.to_s)
95
+ end
96
+
97
+ def test_create12
98
+ j = JID::new('@b/')
99
+ assert_equal('', j.node)
100
+ assert_equal('b', j.domain)
101
+ assert_equal('', j.resource)
102
+ assert_equal('@b/', j.to_s)
103
+ end
104
+
105
+ def test_create13
106
+ j = JID::new('a@b/')
107
+ assert_equal('a', j.node)
108
+ assert_equal('b', j.domain)
109
+ assert_equal('', j.resource)
110
+ assert_equal('a@b/', j.to_s)
111
+ end
112
+
113
+ def test_create14
114
+ j = JID::new('nOdE@dOmAiN/rEsOuRcE')
115
+ assert_equal('node', j.node)
116
+ assert_equal('domain', j.domain)
117
+ assert_equal('rEsOuRcE', j.resource)
118
+ assert_equal('node@domain/rEsOuRcE', j.to_s)
119
+ end
120
+
121
+ def test_tos
122
+ assert_equal('', JID::new.to_s)
123
+ assert_equal('domain.fr', JID::new('domain.fr').to_s)
124
+ assert_equal('l@domain.fr', JID::new('l','domain.fr').to_s)
125
+ assert_equal('l@domain.fr/res', JID::new('l','domain.fr','res').to_s)
126
+ assert_equal('domain.fr/res', JID::new(nil,'domain.fr','res').to_s)
127
+ end
128
+
129
+ def test_equal
130
+ assert_equal(JID::new('domain.fr'), JID::new('domain.fr'))
131
+ assert_equal(JID::new('domain.fr'), JID::new(nil, 'domain.fr'))
132
+ assert_equal(JID::new('l@domain.fr'), JID::new('l@domain.fr'))
133
+ assert_equal(JID::new('l@domain.fr'), JID::new('l', 'domain.fr'))
134
+ assert_equal(JID::new('l@domain.fr/res'), JID::new('l@domain.fr/res'))
135
+ assert_equal(JID::new('l@domain.fr/res'), JID::new('l', 'domain.fr', 'res'))
136
+ end
137
+
138
+ def test_hash
139
+ h = {}
140
+ j = JID::new('l@domain.fr/res')
141
+ h[j] = 'a'
142
+ assert_equal(h[j], h[JID::new('l@domain.fr/res')])
143
+ end
144
+
145
+ def test_strip
146
+ assert_equal(JID::new('l@domain.fr'), JID::new('l@domain.fr/res').strip)
147
+ assert_equal(JID::new('l@domain.fr'), JID::new('l@domain.fr').strip)
148
+ assert_equal(JID::new('l@domain.fr'), JID::new('l@domain.fr/res').bare)
149
+ jid = JID::new('l@domain.fr/res')
150
+ jid.strip!
151
+ assert_equal(JID::new('l@domain.fr'), jid)
152
+
153
+ jid = JID::new('l@domain.fr/res')
154
+ jid.bare!
155
+ assert_equal(JID::new('l@domain.fr'), jid)
156
+ end
157
+
158
+ def test_change1
159
+ j = JID::new('a@b/c')
160
+ j.node = 'd'
161
+ assert_equal('d@b/c', j.to_s)
162
+ j.domain = 'e'
163
+ assert_equal('d@e/c', j.to_s)
164
+ j.resource = 'f'
165
+ assert_equal('d@e/f', j.to_s)
166
+ end
167
+
168
+ def test_escaping
169
+ j = JID::new('user1@server1')
170
+ j2 = JID::new(JID::escape(j), 'server2', 'res2')
171
+ assert_equal('user1%server1@server2/res2', j2.to_s)
172
+ end
173
+
174
+ if defined?(libidnbug) # this crashes the interpreter
175
+ def test_invalidnode
176
+ # assert_raises(IDN::Stringprep::StringprepError) { JID::new('toto@a/a', 'server', 'res') }
177
+ assert_raises(IDN::Stringprep::StringprepError) { IDN::Stringprep.nodeprep('toto@a/a') }
178
+ end
179
+ end
180
+
181
+ def test_empty
182
+ assert(JID.new.empty?)
183
+ assert(!JID.new("test").empty?)
184
+ end
185
+
186
+ def test_stripped
187
+ assert(JID.new("node@domain").stripped?)
188
+ assert(!JID.new("node@domain/res").stripped?)
189
+ assert(JID.new("node@domain").bared?)
190
+ assert(!JID.new("node@domain/res").bared?)
191
+ end
192
+
193
+ def test_sort
194
+ assert_equal(-1, JID.new('a@b') <=> JID.new('b@b'))
195
+ assert_equal(0, JID.new('a@b') <=> JID.new('a@b'))
196
+ assert_equal(1, JID.new('a@b/r') <=> JID.new('a@b'))
197
+
198
+ jids = [JID.new('b@b'), JID.new('a@b/r'), JID.new('a@b')]
199
+ jids.sort!
200
+ assert_equal([JID.new('a@b'), JID.new('a@b/r'), JID.new('b@b')], jids)
201
+ end
202
+ end
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift '../lib'
4
+
5
+ require 'test/unit'
6
+ require 'socket'
7
+ require 'xmpp4r/rexmladdons'
8
+ require 'xmpp4r/message'
9
+ include Jabber
10
+
11
+ class MessageTest < Test::Unit::TestCase
12
+ def test_create
13
+ x = Message::new()
14
+ assert_equal("message", x.name)
15
+ assert_equal(nil, x.to)
16
+ assert_equal(nil, x.body)
17
+
18
+ x = Message::new("lucas@linux.ensimag.fr", "coucou")
19
+ assert_equal("message", x.name)
20
+ assert_equal("lucas@linux.ensimag.fr", x.to.to_s)
21
+ assert_equal("coucou", x.body)
22
+ end
23
+
24
+ def test_import
25
+ x = Message::new
26
+ assert_kind_of(REXML::Element, x.typed_add(REXML::Element.new('thread')))
27
+ assert_kind_of(X, x.typed_add(REXML::Element.new('x')))
28
+ assert_kind_of(X, x.x)
29
+ end
30
+
31
+ def test_type
32
+ x = Message.new
33
+ assert_equal(nil, x.type)
34
+ x.type = :chat
35
+ assert_equal(:chat, x.type)
36
+ assert_equal(x, x.set_type(:error))
37
+ assert_equal(:error, x.type)
38
+ x.type = :groupchat
39
+ assert_equal(:groupchat, x.type)
40
+ x.type = :headline
41
+ assert_equal(:headline, x.type)
42
+ x.type = :normal
43
+ assert_equal(:normal, x.type)
44
+ x.type = :invalid
45
+ assert_equal(nil, x.type)
46
+ end
47
+
48
+ def test_body
49
+ x = Message::new()
50
+ assert_equal(nil, x.body)
51
+ assert_equal(x, x.set_body("trezrze ezfrezr ezr zer ezr ezrezrez ezr z"))
52
+ assert_equal("trezrze ezfrezr ezr zer ezr ezrezrez ezr z", x.body)
53
+ x.body = "2"
54
+ assert_equal("2", x.body)
55
+ end
56
+
57
+ def test_subject
58
+ x = Message::new
59
+ assert_equal(nil, x.subject)
60
+ subject = REXML::Element.new('subject')
61
+ subject.text = 'A'
62
+ x.add(subject)
63
+ assert_equal('A', x.subject)
64
+ x.subject = 'Test message'
65
+ assert_equal('Test message', x.subject)
66
+ x.each_element('subject') { |s| assert_equal('Test message', s.text) }
67
+ assert_equal(x, x.set_subject('Breaking news'))
68
+ assert_equal('Breaking news', x.subject)
69
+ end
70
+
71
+ def test_thread
72
+ x = Message::new
73
+ assert_equal(nil, x.thread)
74
+ thread = REXML::Element.new('thread')
75
+ thread.text = '123'
76
+ x.add(thread)
77
+ assert_equal('123', x.thread)
78
+ x.thread = '321'
79
+ assert_equal('321', x.thread)
80
+ x.each_element('thread') { |s| assert_equal('321', s.text) }
81
+ assert_equal(x, x.set_thread('abc'))
82
+ assert_equal('abc', x.thread)
83
+ end
84
+
85
+ def test_error
86
+ x = Message::new()
87
+ assert_equal(nil, x.error)
88
+ e = REXML::Element::new('error')
89
+ x.add(e)
90
+ # test if, after an import, the error element is successfully changed
91
+ # into an Error object.
92
+ x2 = Message::new.import(x)
93
+ assert_equal(Error, x2.first_element('error').class)
94
+ end
95
+
96
+ def test_answer
97
+ orig = Message::new
98
+ orig.from = 'a@b'
99
+ orig.to = 'b@a'
100
+ orig.id = '123'
101
+ orig.type = :chat
102
+ orig.add(REXML::Element.new('x'))
103
+
104
+ answer = orig.answer
105
+ assert_equal(JID.new('b@a'), answer.from)
106
+ assert_equal(JID.new('a@b'), answer.to)
107
+ assert_equal('123', answer.id)
108
+ assert_equal(:chat, answer.type)
109
+ answer.each_element { |e|
110
+ assert_equal('x', e.name)
111
+ assert_kind_of(X, e)
112
+ }
113
+ end
114
+ end
@@ -0,0 +1,148 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift '../lib'
4
+
5
+ require 'test/unit'
6
+ require 'socket'
7
+ require 'xmpp4r/rexmladdons'
8
+ require 'xmpp4r/presence'
9
+ include Jabber
10
+
11
+ class PresenceTest < Test::Unit::TestCase
12
+ def test_create
13
+ x = Presence::new()
14
+ assert_equal("presence", x.name)
15
+ assert_equal(nil, x.to)
16
+ assert_equal(nil, x.show)
17
+ assert_equal(nil, x.status)
18
+ assert_equal(nil, x.priority)
19
+
20
+ x = Presence::new(:away, "I am away", 23)
21
+ assert_equal("presence", x.name)
22
+ assert_equal(:away, x.show)
23
+ assert_equal("away", x.show.to_s)
24
+ assert_equal("I am away", x.status)
25
+ assert_equal(23, x.priority)
26
+ end
27
+
28
+ def test_show
29
+ x = Presence::new()
30
+ assert_equal(nil, x.show)
31
+ assert_raise(RuntimeError) { x.show = "a" }
32
+ assert_equal(nil, x.show)
33
+ assert_raise(RuntimeError) { x.show = 'away' }
34
+ assert_equal(nil, x.show)
35
+ x.show = :away
36
+ assert_equal(:away, x.show)
37
+ x.each_element('show') { |e| assert(e.class == REXML::Element, "<show/> is not REXML::Element") }
38
+ x.show = nil
39
+ assert_equal(nil, x.show)
40
+ x.each_element('show') { |e| assert(true, "<show/> exists after 'show=nil'") }
41
+ x.show = nil
42
+ assert_equal(nil, x.show)
43
+
44
+ showelement = REXML::Element.new('show')
45
+ showelement.text = 'chat'
46
+ x.add(showelement)
47
+ assert_equal(:chat, x.show)
48
+ end
49
+
50
+ def test_status
51
+ x = Presence::new()
52
+ assert_equal(nil, x.status)
53
+ x.status = "b"
54
+ assert_equal("b", x.status)
55
+ x.each_element('status') { |e| assert(e.class == REXML::Element, "<status/> is not REXML::Element") }
56
+ x.status = nil
57
+ assert_equal(nil, x.status)
58
+ x.each_element('status') { |e| assert(true, "<status/> exists after 'status=nil'") }
59
+ x.status = nil
60
+ assert_equal(nil, x.status)
61
+ end
62
+
63
+ def test_priority
64
+ x = Presence::new()
65
+ assert_equal(nil, x.priority)
66
+ x.priority = 5
67
+ assert_equal(5, x.priority)
68
+ x.each_element('priority') { |e| assert(e.class == REXML::Element, "<priority/> is not REXML::Element") }
69
+ x.priority = "5"
70
+ assert_equal(5, x.priority)
71
+ x.priority = nil
72
+ assert_equal(nil, x.priority)
73
+ x.each_element('priority') { |e| assert(true, "<priority/> exists after 'priority=nil'") }
74
+ end
75
+
76
+ def test_type
77
+ x = Presence::new()
78
+ assert_equal(nil, x.type)
79
+ x.type = :delete
80
+ assert_equal(nil, x.type)
81
+ x.type = nil
82
+ assert_equal(nil, x.type)
83
+ x.type = nil
84
+ assert_equal(nil, x.type)
85
+ [:error, :probe, :subscribe, :subscribed, :unavailable, :unsubscribe, :unsubscribed, nil].each { |type|
86
+ x.type = type
87
+ assert_equal(type, x.type)
88
+ }
89
+ end
90
+
91
+ def test_chaining
92
+ x = Presence::new()
93
+ x.set_show(:xa).set_status("Plundering the fridge.").set_priority(0)
94
+ assert_equal(:xa, x.show)
95
+ assert_equal("Plundering the fridge.", x.status)
96
+ assert_equal(0, x.priority)
97
+ end
98
+
99
+ def test_error
100
+ x = Presence::new()
101
+ e = REXML::Element::new('error')
102
+ x.add(e)
103
+ x2 = Presence::new.import(x)
104
+ # test if, after an import, the error element is successfully changed
105
+ # into an Error object.
106
+ assert_equal(Error, x2.first_element('error').class)
107
+ end
108
+
109
+ def test_sample
110
+ x = Presence::new
111
+ require 'rexml/document'
112
+ d = REXML::Document.new("<presence from='astro@spaceboyz.net/versionbot' to='astro@spaceboyz.net/edgarr' xmlns='jabber:client'>\n <x from='astro@spaceboyz.net/versionbot' stamp='20050823T02:18:42' xmlns='jabber:x:delay'/><show>xa</show>\n <status>I am the evil fingerprinting robot</status>\n </presence>")
113
+ x.import(d.root)
114
+ num = 0
115
+ x.each_element('show') { num += 1 }
116
+ assert_equal(1, num)
117
+ assert_equal(:xa, x.show)
118
+ assert_equal('I am the evil fingerprinting robot', x.status)
119
+ end
120
+
121
+ def test_xpathbug
122
+ require 'rexml/document'
123
+ d = REXML::Document.new("<tag1 xmlns='ns1'><tag2 xmlns='ns2'/><tada>xa</tada></tag1>")
124
+ x = d.root
125
+ num = 0
126
+ x.each_element('tada') { num += 1 }
127
+ assert_equal(1, num)
128
+ end
129
+
130
+ def test_compare_prio
131
+ assert_equal(0, Presence::new(:chat, '', 5) <=> Presence::new(:chat, '', 5))
132
+ assert_equal(-1, Presence::new(:chat, '', 4) <=> Presence::new(:chat, '', 5))
133
+ assert_equal(1, Presence::new(:chat, '', 4) <=> Presence::new(:chat, '', 3))
134
+ assert_equal(-1, Presence::new(:chat, '', nil) <=> Presence::new(:chat, '', 3))
135
+ assert_equal(1, Presence::new(:chat, '', 10) <=> Presence::new(:chat, '', nil))
136
+ assert_equal(0, Presence::new(:chat, '', nil) <=> Presence::new(:chat, '', nil))
137
+ end
138
+
139
+ def test_compare_interest
140
+ unav = Presence::new.set_type(:unavailable)
141
+ assert_equal(0, unav.cmp_interest(unav))
142
+ assert_equal(1, unav.cmp_interest(Presence::new))
143
+ assert_equal(-1, Presence::new.cmp_interest(unav))
144
+ assert_equal(1, Presence::new(:chat).cmp_interest(Presence::new))
145
+ assert_equal(-1, Presence::new(:away).cmp_interest(Presence::new(:dnd)))
146
+ end
147
+
148
+ end
data/test/tc_stream.rb ADDED
@@ -0,0 +1,182 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift '../lib'
4
+
5
+ require 'tempfile'
6
+ require 'test/unit'
7
+ require 'socket'
8
+ require 'xmpp4r/stream'
9
+ include Jabber
10
+
11
+
12
+ class StreamTest < Test::Unit::TestCase
13
+ def setup
14
+ @tmpfile = Tempfile::new("StreamSendTest")
15
+ @tmpfilepath = @tmpfile.path()
16
+ @tmpfile.unlink
17
+ @servlisten = UNIXServer::new(@tmpfilepath)
18
+ @server = nil
19
+ thServer = Thread.new { @server = @servlisten.accept }
20
+ @iostream = UNIXSocket::new(@tmpfilepath)
21
+ n = 0
22
+ while @server.nil? and n < 10
23
+ sleep 0.1
24
+ n += 1
25
+ end
26
+ @stream = Stream::new(false)
27
+ @stream.start(@iostream)
28
+ end
29
+
30
+ def teardown
31
+ @stream.close
32
+ @server.close
33
+ end
34
+
35
+ ##
36
+ # tests that stream really waits the call to process() to dispatch
37
+ # stanzas to filters
38
+ def test_process
39
+ =begin
40
+ Disabled, because non-threaded mode is broken
41
+
42
+ called = false
43
+ @stream.add_xml_callback { called = true }
44
+ assert(!called)
45
+ @server.puts('<stream:stream>')
46
+ @server.flush
47
+ sleep 0.1
48
+ assert(!called)
49
+ @stream.process
50
+ assert(called)
51
+ =end
52
+ end
53
+
54
+ ##
55
+ # tests that you can select how many messages you want to get with process
56
+ def test_process_multi
57
+ =begin
58
+ Disabled, because non-threaded mode is broken
59
+
60
+ nbcalls = 0
61
+ called = false
62
+ @stream.add_xml_callback { |element|
63
+ nbcalls += 1
64
+ if element.name == "message"
65
+ called = true
66
+ end
67
+ }
68
+ assert(!called)
69
+ @server.puts('<stream:stream/>')
70
+ @server.flush
71
+ assert(!called)
72
+ @stream.process
73
+ assert(!called)
74
+ assert_equal(1, nbcalls)
75
+ for i in 1..10
76
+ @server.puts('<presence/>')
77
+ @server.flush
78
+ end
79
+ @server.puts('<message/>')
80
+ @server.flush
81
+ assert(!called)
82
+ assert_equal(1, nbcalls)
83
+ @stream.process(8)
84
+ assert_equal(9, nbcalls)
85
+ assert(!called)
86
+ @stream.process(2)
87
+ assert_equal(11, nbcalls)
88
+ assert(!called)
89
+ @stream.process(1)
90
+ assert_equal(12, nbcalls)
91
+ assert(called)
92
+ =end
93
+ end
94
+
95
+ # tests that you can get all waiting messages if you don't use a parameter
96
+ def test_process_multi2
97
+ =begin
98
+ Disabled, because non-threaded mode is broken
99
+
100
+ @called = false
101
+ @nbcalls = 0
102
+ @stream.add_xml_callback { |element|
103
+ @nbcalls += 1
104
+ if element.name == "message"
105
+ @called = true
106
+ end
107
+ }
108
+ assert(!@called)
109
+ @server.puts('<stream:stream>')
110
+ @server.flush
111
+ assert(!@called)
112
+ @stream.process
113
+ assert(!@called)
114
+ assert_equal(1, @nbcalls)
115
+ for i in 1..20
116
+ @server.puts('<iq/>')
117
+ @server.flush
118
+ end
119
+ @server.puts('<message/>')
120
+ @server.flush
121
+ assert(!@called)
122
+ assert_equal(1, @nbcalls)
123
+ @stream.process
124
+ assert_equal(22, @nbcalls)
125
+ assert(@called)
126
+ =end
127
+ end
128
+
129
+ # Check that <message><message/></message> is recognized as one Message
130
+ def test_similar_children
131
+ n = 0
132
+ @stream.add_message_callback { n += 1 }
133
+ assert_equal(0, n)
134
+ @server.puts('<stream:stream><message/>')
135
+ @server.flush
136
+ @stream.process
137
+ assert_equal(1, n)
138
+ @server.puts('<message>')
139
+ @server.flush
140
+ @stream.process
141
+ assert_equal(1, n)
142
+ @server.puts('<message/>')
143
+ @server.flush
144
+ @stream.process
145
+ assert_equal(1, n)
146
+ @server.puts('</message>')
147
+ @server.flush
148
+ @stream.process
149
+ assert_equal(2, n)
150
+ @server.puts('<message><stream:stream><message/></stream:stream>')
151
+ @server.flush
152
+ @stream.process
153
+ assert_equal(2, n)
154
+ @server.puts('</message>')
155
+ @server.flush
156
+ @stream.process
157
+ assert_equal(3, n)
158
+ end
159
+
160
+ def test_send
161
+ @server.puts('<stream:stream>')
162
+ @server.flush
163
+ @stream.process
164
+
165
+ Thread.new {
166
+ assert_equal(Iq.new(:get).to_s, @server.gets('>'))
167
+ @stream.receive(Iq.new(:result))
168
+ }
169
+
170
+ called = 0
171
+ @stream.send(Iq.new(:get)) { |reply|
172
+ called += 1
173
+ if reply.kind_of? Iq and reply.type == :result
174
+ true
175
+ else
176
+ false
177
+ end
178
+ }
179
+
180
+ assert_equal(1, called)
181
+ end
182
+ end