gmcmillan-xmpp4r 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +91 -0
  3. data/COPYING +340 -0
  4. data/LICENSE +59 -0
  5. data/README.rdoc +122 -0
  6. data/README_ruby19.txt +43 -0
  7. data/Rakefile +258 -0
  8. data/data/doc/xmpp4r/examples/advanced/adventure/README +56 -0
  9. data/data/doc/xmpp4r/examples/advanced/adventure/adventure.rb +23 -0
  10. data/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb +136 -0
  11. data/data/doc/xmpp4r/examples/advanced/adventure/cube.xml +15 -0
  12. data/data/doc/xmpp4r/examples/advanced/adventure/tower.xml +69 -0
  13. data/data/doc/xmpp4r/examples/advanced/adventure/world.rb +424 -0
  14. data/data/doc/xmpp4r/examples/advanced/fileserve.conf +11 -0
  15. data/data/doc/xmpp4r/examples/advanced/fileserve.rb +346 -0
  16. data/data/doc/xmpp4r/examples/advanced/getonline.rb +56 -0
  17. data/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb +315 -0
  18. data/data/doc/xmpp4r/examples/advanced/migrate.rb +88 -0
  19. data/data/doc/xmpp4r/examples/advanced/minimuc.rb +266 -0
  20. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/index.xsl +235 -0
  21. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/pep-aggregator.rb +147 -0
  22. data/data/doc/xmpp4r/examples/advanced/recvfile.rb +85 -0
  23. data/data/doc/xmpp4r/examples/advanced/rosterdiscovery.rb +129 -0
  24. data/data/doc/xmpp4r/examples/advanced/sendfile.conf +10 -0
  25. data/data/doc/xmpp4r/examples/advanced/sendfile.rb +72 -0
  26. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb +51 -0
  27. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_jabber.rb +43 -0
  28. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_test.rb +10 -0
  29. data/data/doc/xmpp4r/examples/advanced/versionpoll.rb +109 -0
  30. data/data/doc/xmpp4r/examples/advanced/xmpping.rb +146 -0
  31. data/data/doc/xmpp4r/examples/advanced/xmppingrc.sample +14 -0
  32. data/data/doc/xmpp4r/examples/basic/change_password.rb +41 -0
  33. data/data/doc/xmpp4r/examples/basic/client.rb +70 -0
  34. data/data/doc/xmpp4r/examples/basic/component.rb +11 -0
  35. data/data/doc/xmpp4r/examples/basic/echo.rb +37 -0
  36. data/data/doc/xmpp4r/examples/basic/jabbersend.rb +41 -0
  37. data/data/doc/xmpp4r/examples/basic/mass_sender.rb +68 -0
  38. data/data/doc/xmpp4r/examples/basic/muc_owner_config.rb +12 -0
  39. data/data/doc/xmpp4r/examples/basic/mucinfo.rb +41 -0
  40. data/data/doc/xmpp4r/examples/basic/mucsimplebot.rb +82 -0
  41. data/data/doc/xmpp4r/examples/basic/register.rb +42 -0
  42. data/data/doc/xmpp4r/examples/basic/remove_registration.rb +18 -0
  43. data/data/doc/xmpp4r/examples/basic/roster.rb +44 -0
  44. data/data/doc/xmpp4r/examples/basic/rosterprint.rb +50 -0
  45. data/data/doc/xmpp4r/examples/basic/rosterrename.rb +34 -0
  46. data/data/doc/xmpp4r/examples/basic/rosterwatch.rb +171 -0
  47. data/data/doc/xmpp4r/examples/basic/send_vcard.rb +67 -0
  48. data/data/doc/xmpp4r/examples/basic/tune_client.rb +56 -0
  49. data/data/doc/xmpp4r/examples/basic/tune_server.rb +58 -0
  50. data/data/doc/xmpp4r/examples/basic/versionbot.rb +75 -0
  51. data/lib/xmpp4r.rb +118 -0
  52. data/lib/xmpp4r/base64.rb +32 -0
  53. data/lib/xmpp4r/bytestreams.rb +15 -0
  54. data/lib/xmpp4r/bytestreams/helper/filetransfer.rb +321 -0
  55. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +257 -0
  56. data/lib/xmpp4r/bytestreams/helper/ibb/initiator.rb +31 -0
  57. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +54 -0
  58. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +153 -0
  59. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb +86 -0
  60. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb +198 -0
  61. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/socks5.rb +65 -0
  62. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb +82 -0
  63. data/lib/xmpp4r/bytestreams/iq/bytestreams.rb +170 -0
  64. data/lib/xmpp4r/bytestreams/iq/si.rb +206 -0
  65. data/lib/xmpp4r/callbacks.rb +133 -0
  66. data/lib/xmpp4r/caps.rb +1 -0
  67. data/lib/xmpp4r/caps/c.rb +67 -0
  68. data/lib/xmpp4r/caps/helper/generator.rb +160 -0
  69. data/lib/xmpp4r/caps/helper/helper.rb +84 -0
  70. data/lib/xmpp4r/client.rb +345 -0
  71. data/lib/xmpp4r/command/helper/responder.rb +53 -0
  72. data/lib/xmpp4r/command/iq/command.rb +154 -0
  73. data/lib/xmpp4r/component.rb +103 -0
  74. data/lib/xmpp4r/connection.rb +231 -0
  75. data/lib/xmpp4r/dataforms.rb +5 -0
  76. data/lib/xmpp4r/dataforms/x/data.rb +297 -0
  77. data/lib/xmpp4r/debuglog.rb +63 -0
  78. data/lib/xmpp4r/delay.rb +5 -0
  79. data/lib/xmpp4r/delay/x/delay.rb +99 -0
  80. data/lib/xmpp4r/discovery.rb +8 -0
  81. data/lib/xmpp4r/discovery/helper/helper.rb +58 -0
  82. data/lib/xmpp4r/discovery/helper/responder.rb +165 -0
  83. data/lib/xmpp4r/discovery/iq/discoinfo.rb +211 -0
  84. data/lib/xmpp4r/discovery/iq/discoitems.rb +147 -0
  85. data/lib/xmpp4r/entity_time.rb +6 -0
  86. data/lib/xmpp4r/entity_time/iq.rb +45 -0
  87. data/lib/xmpp4r/entity_time/responder.rb +57 -0
  88. data/lib/xmpp4r/errors.rb +284 -0
  89. data/lib/xmpp4r/feature_negotiation.rb +5 -0
  90. data/lib/xmpp4r/feature_negotiation/iq/feature.rb +28 -0
  91. data/lib/xmpp4r/framework/base.rb +55 -0
  92. data/lib/xmpp4r/framework/bot.rb +148 -0
  93. data/lib/xmpp4r/httpbinding.rb +5 -0
  94. data/lib/xmpp4r/httpbinding/client.rb +275 -0
  95. data/lib/xmpp4r/idgenerator.rb +37 -0
  96. data/lib/xmpp4r/iq.rb +221 -0
  97. data/lib/xmpp4r/jid.rb +167 -0
  98. data/lib/xmpp4r/last.rb +2 -0
  99. data/lib/xmpp4r/last/helper/helper.rb +37 -0
  100. data/lib/xmpp4r/last/iq/last.rb +67 -0
  101. data/lib/xmpp4r/location.rb +2 -0
  102. data/lib/xmpp4r/location/helper/helper.rb +56 -0
  103. data/lib/xmpp4r/location/location.rb +179 -0
  104. data/lib/xmpp4r/message.rb +226 -0
  105. data/lib/xmpp4r/muc.rb +14 -0
  106. data/lib/xmpp4r/muc/helper/mucbrowser.rb +92 -0
  107. data/lib/xmpp4r/muc/helper/mucclient.rb +469 -0
  108. data/lib/xmpp4r/muc/helper/simplemucclient.rb +332 -0
  109. data/lib/xmpp4r/muc/iq/mucadmin.rb +23 -0
  110. data/lib/xmpp4r/muc/iq/mucadminitem.rb +20 -0
  111. data/lib/xmpp4r/muc/iq/mucowner.rb +15 -0
  112. data/lib/xmpp4r/muc/item.rb +143 -0
  113. data/lib/xmpp4r/muc/x/muc.rb +70 -0
  114. data/lib/xmpp4r/muc/x/mucuserinvite.rb +60 -0
  115. data/lib/xmpp4r/muc/x/mucuseritem.rb +36 -0
  116. data/lib/xmpp4r/presence.rb +232 -0
  117. data/lib/xmpp4r/pubsub.rb +8 -0
  118. data/lib/xmpp4r/pubsub/children/configuration.rb +86 -0
  119. data/lib/xmpp4r/pubsub/children/event.rb +49 -0
  120. data/lib/xmpp4r/pubsub/children/item.rb +35 -0
  121. data/lib/xmpp4r/pubsub/children/items.rb +53 -0
  122. data/lib/xmpp4r/pubsub/children/node_config.rb +48 -0
  123. data/lib/xmpp4r/pubsub/children/publish.rb +38 -0
  124. data/lib/xmpp4r/pubsub/children/retract.rb +41 -0
  125. data/lib/xmpp4r/pubsub/children/subscription.rb +62 -0
  126. data/lib/xmpp4r/pubsub/children/subscription_config.rb +67 -0
  127. data/lib/xmpp4r/pubsub/children/unsubscribe.rb +63 -0
  128. data/lib/xmpp4r/pubsub/helper/nodebrowser.rb +129 -0
  129. data/lib/xmpp4r/pubsub/helper/nodehelper.rb +156 -0
  130. data/lib/xmpp4r/pubsub/helper/oauth_service_helper.rb +90 -0
  131. data/lib/xmpp4r/pubsub/helper/servicehelper.rb +490 -0
  132. data/lib/xmpp4r/pubsub/iq/pubsub.rb +19 -0
  133. data/lib/xmpp4r/query.rb +15 -0
  134. data/lib/xmpp4r/reliable.rb +168 -0
  135. data/lib/xmpp4r/rexmladdons.rb +197 -0
  136. data/lib/xmpp4r/roster.rb +7 -0
  137. data/lib/xmpp4r/roster/helper/roster.rb +532 -0
  138. data/lib/xmpp4r/roster/iq/roster.rb +215 -0
  139. data/lib/xmpp4r/roster/x/roster.rb +138 -0
  140. data/lib/xmpp4r/rpc.rb +2 -0
  141. data/lib/xmpp4r/rpc/helper/client.rb +123 -0
  142. data/lib/xmpp4r/rpc/helper/server.rb +74 -0
  143. data/lib/xmpp4r/rpc/helper/xmlrpcaddons.rb +67 -0
  144. data/lib/xmpp4r/rpc/iq/rpc.rb +23 -0
  145. data/lib/xmpp4r/sasl.rb +259 -0
  146. data/lib/xmpp4r/semaphore.rb +38 -0
  147. data/lib/xmpp4r/stream.rb +611 -0
  148. data/lib/xmpp4r/streamparser.rb +66 -0
  149. data/lib/xmpp4r/test/listener_mocker.rb +118 -0
  150. data/lib/xmpp4r/tune.rb +2 -0
  151. data/lib/xmpp4r/tune/helper/helper.rb +58 -0
  152. data/lib/xmpp4r/tune/tune.rb +113 -0
  153. data/lib/xmpp4r/vcard.rb +6 -0
  154. data/lib/xmpp4r/vcard/helper/vcard.rb +84 -0
  155. data/lib/xmpp4r/vcard/iq/vcard.rb +109 -0
  156. data/lib/xmpp4r/version.rb +7 -0
  157. data/lib/xmpp4r/version/helper/responder.rb +72 -0
  158. data/lib/xmpp4r/version/helper/simpleresponder.rb +44 -0
  159. data/lib/xmpp4r/version/iq/version.rb +105 -0
  160. data/lib/xmpp4r/x.rb +37 -0
  161. data/lib/xmpp4r/xhtml.rb +1 -0
  162. data/lib/xmpp4r/xhtml/html.rb +115 -0
  163. data/lib/xmpp4r/xmpp4r.rb +20 -0
  164. data/lib/xmpp4r/xmppelement.rb +168 -0
  165. data/lib/xmpp4r/xmppstanza.rb +162 -0
  166. data/setup.rb +1586 -0
  167. data/test/bytestreams/tc_ibb.rb +188 -0
  168. data/test/bytestreams/tc_socks5bytestreams.rb +114 -0
  169. data/test/caps/tc_helper.rb +158 -0
  170. data/test/dataforms/tc_data.rb +81 -0
  171. data/test/delay/tc_xdelay.rb +51 -0
  172. data/test/discovery/tc_responder.rb +91 -0
  173. data/test/entity_time/tc_responder.rb +65 -0
  174. data/test/last/tc_helper.rb +75 -0
  175. data/test/lib/assert_equal_xml.rb +14 -0
  176. data/test/lib/clienttester.rb +149 -0
  177. data/test/muc/tc_muc_mucclient.rb +834 -0
  178. data/test/muc/tc_muc_simplemucclient.rb +114 -0
  179. data/test/muc/tc_mucowner.rb +50 -0
  180. data/test/pubsub/tc_helper.rb +785 -0
  181. data/test/pubsub/tc_nodeconfig.rb +61 -0
  182. data/test/pubsub/tc_subscriptionconfig.rb +41 -0
  183. data/test/roster/tc_helper.rb +523 -0
  184. data/test/roster/tc_iqqueryroster.rb +173 -0
  185. data/test/roster/tc_xroster.rb +73 -0
  186. data/test/rpc/tc_helper.rb +96 -0
  187. data/test/tc_callbacks.rb +129 -0
  188. data/test/tc_class_names.rb +146 -0
  189. data/test/tc_client.rb +30 -0
  190. data/test/tc_errors.rb +146 -0
  191. data/test/tc_idgenerator.rb +30 -0
  192. data/test/tc_iq.rb +113 -0
  193. data/test/tc_iqquery.rb +31 -0
  194. data/test/tc_jid.rb +204 -0
  195. data/test/tc_presence.rb +150 -0
  196. data/test/tc_rexml.rb +139 -0
  197. data/test/tc_stream.rb +193 -0
  198. data/test/tc_streamComponent.rb +105 -0
  199. data/test/tc_streamError.rb +129 -0
  200. data/test/tc_streamSend.rb +59 -0
  201. data/test/tc_streamparser.rb +137 -0
  202. data/test/tc_xmppstanza.rb +135 -0
  203. data/test/ts_xmpp4r.rb +44 -0
  204. data/test/tune/tc_helper_recv.rb +82 -0
  205. data/test/tune/tc_helper_send.rb +74 -0
  206. data/test/tune/tc_tune.rb +79 -0
  207. data/test/vcard/tc_helper.rb +49 -0
  208. data/test/vcard/tc_iqvcard.rb +62 -0
  209. data/test/version/tc_helper.rb +60 -0
  210. data/test/version/tc_iqqueryversion.rb +97 -0
  211. data/test/xhtml/tc_html.rb +41 -0
  212. data/tools/gen_requires.bash +31 -0
  213. data/tools/xmpp4r-gemspec-test.rb +11 -0
  214. data/xmpp4r.gemspec +249 -0
  215. metadata +299 -0
@@ -0,0 +1,193 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ $:.unshift '../lib'
5
+ $:.unshift './lib/'
6
+
7
+ require 'tempfile'
8
+ require 'test/unit'
9
+ require 'socket'
10
+ require 'xmpp4r/stream'
11
+ require 'xmpp4r/semaphore'
12
+ require 'clienttester'
13
+ include Jabber
14
+
15
+ # Jabber::debug = true
16
+
17
+ class StreamTest < Test::Unit::TestCase
18
+ include ClientTester
19
+
20
+ def busywait(&block)
21
+ n = 0
22
+ while not block.call and n < 10000
23
+ Thread::pass
24
+ n += 1
25
+ end
26
+ end
27
+
28
+ ##
29
+ # tests that connection really waits the call to process() to dispatch
30
+ # stanzas to filters
31
+ def test_process
32
+ called = false
33
+ @client.add_xml_callback { called = true }
34
+ assert(!called)
35
+ @server.send('<iq/>')
36
+ busywait { called }
37
+ assert(called)
38
+ end
39
+
40
+ def test_process20
41
+ done = Semaphore.new
42
+ n = 0
43
+ @client.add_message_callback {
44
+ n += 1
45
+ done.run if n % 20 == 0
46
+ }
47
+
48
+ 20.times {
49
+ @server.send('<message/>')
50
+ }
51
+
52
+ done.wait
53
+ assert_equal(20, n)
54
+
55
+ @server.send('<message/>' * 20)
56
+
57
+ done.wait
58
+ assert_equal(40, n)
59
+ end
60
+
61
+ def test_send
62
+ sem = Semaphore::new
63
+ @server.add_xml_callback { |e|
64
+ @server.send(Iq.new(:result))
65
+ sem.run
66
+ }
67
+
68
+ called = 0
69
+ @client.send(Iq.new(:get)) { |reply|
70
+ called += 1
71
+ if reply.kind_of? Iq and reply.type == :result
72
+ true
73
+ else
74
+ false
75
+ end
76
+ }
77
+ sem.wait
78
+ busywait { called }
79
+ assert_equal(1, called)
80
+ end
81
+
82
+ def test_send_with_callback_timeout_from_block
83
+ sem = Semaphore::new
84
+ @server.add_xml_callback { |e|
85
+ @server.send(Iq.new(:result))
86
+ sem.run
87
+ }
88
+
89
+ assert_raise Timeout::Error do
90
+ @client.send(Iq.new(:get), 1) { |reply|
91
+ sleep(2)
92
+ true
93
+ }
94
+ end
95
+
96
+ sem.wait
97
+ end
98
+
99
+ def test_send_with_callback_timeout_from_no_stanza
100
+ assert_raise Timeout::Error do
101
+ @client.send(Iq.new(:get), 1) { |reply|
102
+ true
103
+ }
104
+ end
105
+ end
106
+
107
+ def test_send_nested
108
+ finished = Semaphore.new
109
+
110
+ id = 0
111
+ @server.add_xml_callback do |e|
112
+ id += 1
113
+ if id == 1
114
+ @server.send(Iq.new(:result).set_id('1').delete_namespace)
115
+ elsif id == 2
116
+ @server.send(Iq.new(:result).set_id('2').delete_namespace)
117
+ elsif id == 3
118
+ @server.send(Iq.new(:result).set_id('3').delete_namespace)
119
+ else
120
+ p e
121
+ end
122
+ end
123
+
124
+ called_outer = 0
125
+ called_inner = 0
126
+
127
+ @client.send(Iq.new(:get)) do |reply|
128
+ called_outer += 1
129
+ assert_kind_of(Iq, reply)
130
+ assert_equal(:result, reply.type)
131
+
132
+ if reply.id == '1'
133
+ @client.send(Iq.new(:set)) do |reply2|
134
+ called_inner += 1
135
+ assert_kind_of(Iq, reply2)
136
+ assert_equal(:result, reply2.type)
137
+ assert_equal('2', reply2.id)
138
+
139
+ @client.send(Iq.new(:get))
140
+
141
+ true
142
+ end
143
+ false
144
+ elsif reply.id == '3'
145
+ true
146
+ else
147
+ false
148
+ end
149
+ end
150
+
151
+ assert_equal(2, called_outer)
152
+ assert_equal(1, called_inner)
153
+ end
154
+
155
+ def test_send_in_callback
156
+ finished = Semaphore.new
157
+
158
+ @client.add_message_callback {
159
+ @client.send_with_id(Iq.new(:get)) { |reply|
160
+ assert_equal(:result, reply.type)
161
+ finished.run
162
+ }
163
+ }
164
+
165
+ @server.add_iq_callback { |iq|
166
+ @server.send(Iq.new(:result).set_id(iq.id))
167
+ }
168
+
169
+ @server.send(Message.new)
170
+ finished.wait
171
+ end
172
+
173
+ def test_similar_children
174
+ n = 0
175
+ @client.add_message_callback { n += 1 }
176
+ assert_equal(0, n)
177
+ @server.send("<message/>")
178
+ busywait { n == 1 }
179
+ assert_equal(1, n)
180
+ @server.send('<message>')
181
+ assert_equal(1, n)
182
+ @server.send('<message/>')
183
+ assert_equal(1, n)
184
+ @server.send('</message>')
185
+ busywait { n == 2 }
186
+ assert_equal(2, n)
187
+ @server.send("<message><stream:stream><message/></stream:stream>")
188
+ assert_equal(2, n)
189
+ @server.send('</message>')
190
+ busywait { n == 3 }
191
+ assert_equal(3, n)
192
+ end
193
+ end
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift '../lib'
4
+
5
+ require 'tempfile'
6
+ require 'test/unit'
7
+ require 'socket'
8
+ require 'xmpp4r/component'
9
+ require 'xmpp4r/bytestreams'
10
+ require 'xmpp4r/semaphore'
11
+ require 'xmpp4r'
12
+ include Jabber
13
+
14
+ class StreamComponentTest < Test::Unit::TestCase
15
+ @@SOCKET_PORT = 65224
16
+ STREAM = 'stream:stream xmlns:stream="http://etherx.jabber.org/streams"'
17
+
18
+ def setup
19
+ servlisten = TCPServer.new(@@SOCKET_PORT)
20
+ serverwait = Semaphore.new
21
+ Thread.new do
22
+ Thread.current.abort_on_exception = true
23
+ serversock = servlisten.accept
24
+ servlisten.close
25
+ serversock.sync = true
26
+ @server = Stream.new
27
+ @server.add_xml_callback do |xml|
28
+ if xml.prefix == 'stream' and xml.name == 'stream'
29
+ @server.send("<#{STREAM} xmlns='jabber:component:accept'>")
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+ @server.start(serversock)
36
+
37
+ serverwait.run
38
+ end
39
+
40
+ @stream = Component.new('test')
41
+ @stream.connect('localhost', @@SOCKET_PORT)
42
+
43
+ serverwait.wait
44
+ end
45
+
46
+ def teardown
47
+ n = 0
48
+ while @stream.processing > 0 and n < 1000
49
+ Thread::pass
50
+ n += 1
51
+ end
52
+ n = 0
53
+ while @server.processing > 0 and n < 1000
54
+ Thread::pass
55
+ n += 1
56
+ end
57
+ #@stream.close
58
+ @server.close
59
+ end
60
+
61
+ def test_process
62
+ stanzas = 0
63
+ message_lock = Semaphore.new
64
+ iq_lock = Semaphore.new
65
+ presence_lock = Semaphore.new
66
+
67
+ @stream.add_message_callback { |msg|
68
+ assert_kind_of(Message, msg)
69
+ stanzas += 1
70
+ message_lock.run
71
+ }
72
+ @stream.add_iq_callback { |iq|
73
+ assert_kind_of(Iq, iq)
74
+ stanzas += 1
75
+ iq_lock.run
76
+ }
77
+ @stream.add_presence_callback { |pres|
78
+ assert_kind_of(Presence, pres)
79
+ stanzas += 1
80
+ presence_lock.run
81
+ }
82
+
83
+ @server.send('<message/>')
84
+ @server.send('<iq/>')
85
+ @server.send('<presence/>')
86
+
87
+ message_lock.wait
88
+ iq_lock.wait
89
+ presence_lock.wait
90
+
91
+ assert_equal(3, stanzas)
92
+ end
93
+
94
+ def test_outgoing
95
+ received_wait = Semaphore.new
96
+
97
+ @server.add_message_callback { |msg|
98
+ assert_kind_of(Message, msg)
99
+ received_wait.run
100
+ }
101
+
102
+ @stream.send(Message.new)
103
+ received_wait.wait
104
+ end
105
+ end
@@ -0,0 +1,129 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
4
+
5
+ require 'test/unit'
6
+ require 'socket'
7
+ require 'xmpp4r/client'
8
+ include Jabber
9
+
10
+ class ConnectionErrorTest < Test::Unit::TestCase
11
+ @@SOCKET_PORT = 65225
12
+ STREAM = '<stream:stream xmlns:stream="http://etherx.jabber.org/streams">'
13
+
14
+ def setup
15
+ servlisten = TCPServer.new(@@SOCKET_PORT)
16
+ serverwait = Semaphore.new
17
+ @server = nil
18
+ Thread.new do
19
+ Thread.current.abort_on_exception = true
20
+ @server = servlisten.accept
21
+ servlisten.close
22
+ @server.sync = true
23
+
24
+ serverwait.run
25
+ end
26
+
27
+ @conn = TCPSocket.new('localhost', @@SOCKET_PORT)
28
+
29
+ serverwait.wait
30
+ end
31
+
32
+ def teardown
33
+ @conn.close if not @conn.closed?
34
+ @server.close if not @conn.closed?
35
+ end
36
+
37
+ def test_connectionError_start_withexcblock
38
+ @stream = Stream.new
39
+ error = false
40
+ @stream.on_exception do |e, o, w|
41
+ # strange exception, it's caused by REXML, actually
42
+ assert_kind_of(StandardError, e)
43
+ assert_equal(Jabber::Stream, o.class)
44
+ assert_equal(:start, w)
45
+ error = true
46
+ end
47
+ assert(!error)
48
+ begin
49
+ # wrong port on purpose
50
+ conn = TCPSocket.new('localhost', 1)
51
+ rescue
52
+ end
53
+ @stream.start(conn)
54
+ sleep 0.2
55
+ assert(error)
56
+ @server.close
57
+ @stream.close
58
+ end
59
+
60
+ def test_connectionError_parse_withexcblock
61
+ @stream = Stream.new
62
+ error = false
63
+ @stream.start(@conn)
64
+ @stream.on_exception do |e, o, w|
65
+ if w == :disconnected
66
+ assert_equal(nil, e)
67
+ assert_equal(Jabber::Stream, o.class)
68
+ else
69
+ assert_equal(REXML::ParseException, e.class)
70
+ assert_equal(Jabber::Stream, o.class)
71
+ assert_equal(:parser, w)
72
+ error = true
73
+ end
74
+ end
75
+ @server.puts(STREAM)
76
+ @server.flush
77
+ assert(!error)
78
+ @server.puts('</blop>')
79
+ @server.flush
80
+ sleep 0.2
81
+ assert(error)
82
+ @server.close
83
+ @stream.close
84
+ end
85
+
86
+ def test_connectionError_send_withexcblock
87
+ @stream = Stream.new
88
+ error = 0
89
+ @stream.start(@conn)
90
+ @stream.on_exception do |exc, o, w|
91
+ case w
92
+ when :sending
93
+ assert_equal(IOError, exc.class)
94
+ assert_equal(Jabber::Stream, o.class)
95
+ when :disconnected
96
+ assert_equal(nil, exc)
97
+ assert_equal(Jabber::Stream, o.class)
98
+ end
99
+ error += 1
100
+ end
101
+ @server.puts(STREAM)
102
+ @server.flush
103
+ assert_equal(0, error)
104
+ @server.close
105
+ sleep 0.1
106
+ assert_equal(1, error)
107
+ @stream.send('</test>')
108
+ sleep 0.1
109
+ @stream.send('</test>')
110
+ sleep 0.1
111
+ assert_equal(3, error)
112
+ @stream.close
113
+ end
114
+
115
+ def test_connectionError_send_withoutexcblock
116
+ @stream = Stream.new
117
+ @stream.start(@conn)
118
+ @server.puts(STREAM)
119
+ @server.flush
120
+ assert_raise(IOError) do
121
+ @server.close
122
+ sleep 0.1
123
+ @stream.send('</test>')
124
+ sleep 0.1
125
+ @stream.send('</test>')
126
+ sleep 0.1
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift '../lib'
4
+
5
+ require 'test/unit'
6
+ require 'socket'
7
+ require 'tempfile'
8
+ require 'io/wait'
9
+ require 'xmpp4r'
10
+ include Jabber
11
+
12
+ class StreamSendTest < Test::Unit::TestCase
13
+ def setup
14
+ @tmpfile = Tempfile.new("StreamSendTest")
15
+ @tmpfilepath = @tmpfile.path()
16
+ @tmpfile.unlink
17
+ @servlisten = UNIXServer.new(@tmpfilepath)
18
+ thServer = Thread.new { @server = @servlisten.accept }
19
+ @iostream = UNIXSocket.new(@tmpfilepath)
20
+ @stream = Stream.new
21
+ @stream.start(@iostream)
22
+
23
+ thServer.join
24
+ end
25
+
26
+ def teardown
27
+ @stream.close
28
+ @server.close
29
+ @servlisten.close
30
+ end
31
+
32
+ def mysend(s)
33
+ @stream.send(s)
34
+ @stream.send("\n") #needed for easy test writing
35
+ end
36
+
37
+ ##
38
+ # Tries to send a basic message
39
+ def test_sendbasic
40
+ mysend(Message.new)
41
+ assert_equal("<message/>\n", @server.gets)
42
+ end
43
+
44
+ def test_sendmessage
45
+ mysend(Message.new('lucas@linux.ensimag.fr', 'coucou'))
46
+ assert_equal("<message to='lucas@linux.ensimag.fr'><body>coucou</body></message>\n", @server.gets)
47
+ end
48
+
49
+ def test_sendpresence
50
+ mysend(Presence.new)
51
+ assert_equal("<presence/>\n", @server.gets)
52
+ end
53
+
54
+ def test_sendiq
55
+ mysend(Iq.new)
56
+ assert_equal("<iq/>\n", @server.gets)
57
+ end
58
+
59
+ end