sensu-em 2.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +12 -0
  4. data/.yardopts +7 -0
  5. data/CHANGELOG.md +33 -0
  6. data/GNU +281 -0
  7. data/Gemfile +2 -0
  8. data/LICENSE +60 -0
  9. data/README.md +109 -0
  10. data/Rakefile +20 -0
  11. data/docs/DocumentationGuidesIndex.md +27 -0
  12. data/docs/GettingStarted.md +521 -0
  13. data/docs/old/ChangeLog +211 -0
  14. data/docs/old/DEFERRABLES +246 -0
  15. data/docs/old/EPOLL +141 -0
  16. data/docs/old/INSTALL +13 -0
  17. data/docs/old/KEYBOARD +42 -0
  18. data/docs/old/LEGAL +25 -0
  19. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  20. data/docs/old/PURE_RUBY +75 -0
  21. data/docs/old/RELEASE_NOTES +94 -0
  22. data/docs/old/SMTP +4 -0
  23. data/docs/old/SPAWNED_PROCESSES +148 -0
  24. data/docs/old/TODO +8 -0
  25. data/eventmachine.gemspec +38 -0
  26. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  27. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  28. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  29. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  30. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  31. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  32. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  33. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  34. data/examples/old/ex_channel.rb +43 -0
  35. data/examples/old/ex_queue.rb +2 -0
  36. data/examples/old/ex_tick_loop_array.rb +15 -0
  37. data/examples/old/ex_tick_loop_counter.rb +32 -0
  38. data/examples/old/helper.rb +2 -0
  39. data/ext/binder.cpp +124 -0
  40. data/ext/binder.h +46 -0
  41. data/ext/cmain.cpp +887 -0
  42. data/ext/ed.cpp +1988 -0
  43. data/ext/ed.h +422 -0
  44. data/ext/em.cpp +2352 -0
  45. data/ext/em.h +253 -0
  46. data/ext/eventmachine.h +128 -0
  47. data/ext/extconf.rb +179 -0
  48. data/ext/fastfilereader/extconf.rb +103 -0
  49. data/ext/fastfilereader/mapper.cpp +214 -0
  50. data/ext/fastfilereader/mapper.h +59 -0
  51. data/ext/fastfilereader/rubymain.cpp +127 -0
  52. data/ext/kb.cpp +79 -0
  53. data/ext/page.cpp +107 -0
  54. data/ext/page.h +51 -0
  55. data/ext/pipe.cpp +347 -0
  56. data/ext/project.h +161 -0
  57. data/ext/rubymain.cpp +1318 -0
  58. data/ext/ssl.cpp +468 -0
  59. data/ext/ssl.h +94 -0
  60. data/java/.classpath +6 -0
  61. data/java/.gitignore +1 -0
  62. data/java/.project +17 -0
  63. data/java/src/com/rubyeventmachine/DatagramPacket.java +13 -0
  64. data/java/src/com/rubyeventmachine/EmReactor.java +529 -0
  65. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  66. data/java/src/com/rubyeventmachine/EventCallback.java +7 -0
  67. data/java/src/com/rubyeventmachine/EventCode.java +26 -0
  68. data/java/src/com/rubyeventmachine/EventableChannel.java +130 -0
  69. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +180 -0
  70. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +405 -0
  71. data/java/src/com/rubyeventmachine/SslBox.java +310 -0
  72. data/lib/em/buftok.rb +110 -0
  73. data/lib/em/callback.rb +58 -0
  74. data/lib/em/channel.rb +64 -0
  75. data/lib/em/completion.rb +304 -0
  76. data/lib/em/connection.rb +712 -0
  77. data/lib/em/deferrable/pool.rb +2 -0
  78. data/lib/em/deferrable.rb +210 -0
  79. data/lib/em/file_watch.rb +73 -0
  80. data/lib/em/future.rb +61 -0
  81. data/lib/em/iterator.rb +231 -0
  82. data/lib/em/messages.rb +66 -0
  83. data/lib/em/pool.rb +151 -0
  84. data/lib/em/process_watch.rb +45 -0
  85. data/lib/em/processes.rb +123 -0
  86. data/lib/em/protocols/header_and_content.rb +138 -0
  87. data/lib/em/protocols/httpclient.rb +279 -0
  88. data/lib/em/protocols/httpclient2.rb +600 -0
  89. data/lib/em/protocols/line_and_text.rb +125 -0
  90. data/lib/em/protocols/line_protocol.rb +29 -0
  91. data/lib/em/protocols/linetext2.rb +161 -0
  92. data/lib/em/protocols/memcache.rb +331 -0
  93. data/lib/em/protocols/object_protocol.rb +46 -0
  94. data/lib/em/protocols/postgres3.rb +246 -0
  95. data/lib/em/protocols/saslauth.rb +175 -0
  96. data/lib/em/protocols/smtpclient.rb +365 -0
  97. data/lib/em/protocols/smtpserver.rb +643 -0
  98. data/lib/em/protocols/socks4.rb +66 -0
  99. data/lib/em/protocols/stomp.rb +205 -0
  100. data/lib/em/protocols/tcptest.rb +54 -0
  101. data/lib/em/protocols.rb +37 -0
  102. data/lib/em/pure_ruby.rb +1017 -0
  103. data/lib/em/queue.rb +71 -0
  104. data/lib/em/resolver.rb +209 -0
  105. data/lib/em/spawnable.rb +84 -0
  106. data/lib/em/streamer.rb +118 -0
  107. data/lib/em/threaded_resource.rb +90 -0
  108. data/lib/em/tick_loop.rb +85 -0
  109. data/lib/em/timers.rb +61 -0
  110. data/lib/em/version.rb +3 -0
  111. data/lib/eventmachine.rb +1553 -0
  112. data/lib/jeventmachine.rb +321 -0
  113. data/lib/rubyeventmachine.jar +0 -0
  114. data/rakelib/cpp.rake_example +77 -0
  115. data/rakelib/package.rake +98 -0
  116. data/rakelib/test.rake +8 -0
  117. data/tests/client.crt +31 -0
  118. data/tests/client.key +51 -0
  119. data/tests/em_test_helper.rb +64 -0
  120. data/tests/server.crt +36 -0
  121. data/tests/server.key +51 -0
  122. data/tests/test_attach.rb +150 -0
  123. data/tests/test_basic.rb +294 -0
  124. data/tests/test_channel.rb +62 -0
  125. data/tests/test_completion.rb +177 -0
  126. data/tests/test_connection_count.rb +53 -0
  127. data/tests/test_defer.rb +18 -0
  128. data/tests/test_deferrable.rb +35 -0
  129. data/tests/test_epoll.rb +145 -0
  130. data/tests/test_error_handler.rb +38 -0
  131. data/tests/test_exc.rb +28 -0
  132. data/tests/test_file_watch.rb +65 -0
  133. data/tests/test_futures.rb +170 -0
  134. data/tests/test_get_sock_opt.rb +37 -0
  135. data/tests/test_handler_check.rb +35 -0
  136. data/tests/test_hc.rb +155 -0
  137. data/tests/test_httpclient.rb +190 -0
  138. data/tests/test_httpclient2.rb +133 -0
  139. data/tests/test_idle_connection.rb +25 -0
  140. data/tests/test_inactivity_timeout.rb +54 -0
  141. data/tests/test_iterator.rb +97 -0
  142. data/tests/test_kb.rb +34 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +288 -0
  146. data/tests/test_next_tick.rb +104 -0
  147. data/tests/test_object_protocol.rb +36 -0
  148. data/tests/test_pause.rb +102 -0
  149. data/tests/test_pending_connect_timeout.rb +52 -0
  150. data/tests/test_pool.rb +194 -0
  151. data/tests/test_process_watch.rb +48 -0
  152. data/tests/test_processes.rb +128 -0
  153. data/tests/test_proxy_connection.rb +180 -0
  154. data/tests/test_pure.rb +88 -0
  155. data/tests/test_queue.rb +50 -0
  156. data/tests/test_resolver.rb +55 -0
  157. data/tests/test_running.rb +14 -0
  158. data/tests/test_sasl.rb +47 -0
  159. data/tests/test_send_file.rb +217 -0
  160. data/tests/test_servers.rb +33 -0
  161. data/tests/test_set_sock_opt.rb +37 -0
  162. data/tests/test_shutdown_hooks.rb +23 -0
  163. data/tests/test_smtpclient.rb +55 -0
  164. data/tests/test_smtpserver.rb +57 -0
  165. data/tests/test_spawn.rb +293 -0
  166. data/tests/test_ssl_args.rb +78 -0
  167. data/tests/test_ssl_echo_data.rb +60 -0
  168. data/tests/test_ssl_methods.rb +56 -0
  169. data/tests/test_ssl_verify.rb +82 -0
  170. data/tests/test_stomp.rb +37 -0
  171. data/tests/test_system.rb +42 -0
  172. data/tests/test_threaded_resource.rb +53 -0
  173. data/tests/test_tick_loop.rb +59 -0
  174. data/tests/test_timers.rb +123 -0
  175. data/tests/test_ud.rb +8 -0
  176. data/tests/test_unbind_reason.rb +48 -0
  177. metadata +297 -0
@@ -0,0 +1,37 @@
1
+ require 'em_test_helper'
2
+ require 'socket'
3
+
4
+ class TestSetSockOpt < Test::Unit::TestCase
5
+
6
+ if EM.respond_to? :set_sock_opt
7
+ def setup
8
+ assert(!EM.reactor_running?)
9
+ end
10
+
11
+ def teardown
12
+ assert(!EM.reactor_running?)
13
+ end
14
+
15
+ #-------------------------------------
16
+
17
+ def test_set_sock_opt
18
+ test = self
19
+ EM.run do
20
+ EM.connect 'google.com', 80, Module.new {
21
+ define_method :post_init do
22
+ val = set_sock_opt Socket::SOL_SOCKET, Socket::SO_BROADCAST, true
23
+ test.assert_equal 0, val
24
+ EM.stop
25
+ end
26
+ }
27
+ end
28
+ end
29
+ else
30
+ warn "EM.set_sock_opt not implemented, skipping tests in #{__FILE__}"
31
+
32
+ # Because some rubies will complain if a TestCase class has no tests
33
+ def test_em_set_sock_opt_unsupported
34
+ assert true
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestShutdownHooks < Test::Unit::TestCase
4
+ def test_shutdown_hooks
5
+ r = false
6
+ EM.run {
7
+ EM.add_shutdown_hook { r = true }
8
+ EM.stop
9
+ }
10
+ assert_equal( true, r )
11
+ end
12
+
13
+ def test_hook_order
14
+ r = []
15
+ EM.run {
16
+ EM.add_shutdown_hook { r << 2 }
17
+ EM.add_shutdown_hook { r << 1 }
18
+ EM.stop
19
+ }
20
+ assert_equal( [1, 2], r )
21
+ end
22
+ end
23
+
@@ -0,0 +1,55 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestSmtpClient < Test::Unit::TestCase
4
+
5
+ Localhost = "127.0.0.1"
6
+ Localport = 9801
7
+
8
+ def setup
9
+ end
10
+
11
+ def teardown
12
+ end
13
+
14
+ def test_a
15
+ # No real tests until we have a server implementation to test against.
16
+ # This is what the call looks like, though:
17
+ err = nil
18
+ EM.run {
19
+ d = EM::Protocols::SmtpClient.send :domain=>"example.com",
20
+ :host=>Localhost,
21
+ :port=>Localport, # optional, defaults 25
22
+ :starttls=>true,
23
+ :from=>"sender@example.com",
24
+ :to=> ["to_1@example.com", "to_2@example.com"],
25
+ :header=> {"Subject" => "This is a subject line"},
26
+ :body=> "This is the body of the email",
27
+ :verbose=>true
28
+ d.errback {|e|
29
+ err = e
30
+ EM.stop
31
+ }
32
+ }
33
+ assert(err)
34
+ end
35
+
36
+ def test_content
37
+ err = nil
38
+ EM.run {
39
+ d = EM::Protocols::SmtpClient.send :domain=>"example.com",
40
+ :host=>Localhost,
41
+ :port=>Localport, # optional, defaults 25
42
+ :starttls=>true,
43
+ :from=>"sender@example.com",
44
+ :to=> ["to_1@example.com", "to_2@example.com"],
45
+ :content => ["Subject: xxx\r\n\r\ndata\r\n.\r\n"],
46
+ :verbose=>true
47
+ d.errback {|e|
48
+ err = e
49
+ EM.stop
50
+ }
51
+ }
52
+ assert(err)
53
+ end
54
+
55
+ end
@@ -0,0 +1,57 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestSmtpServer < Test::Unit::TestCase
4
+
5
+ # Don't test on port 25. It requires superuser and there's probably
6
+ # a mail server already running there anyway.
7
+ Localhost = "127.0.0.1"
8
+ Localport = 25001
9
+
10
+ # This class is an example of what you need to write in order
11
+ # to implement a mail server. You override the methods you are
12
+ # interested in. Some, but not all, of these are illustrated here.
13
+ #
14
+ class Mailserver < EM::Protocols::SmtpServer
15
+
16
+ attr_reader :my_msg_body, :my_sender, :my_recipients
17
+
18
+ def initialize *args
19
+ super
20
+ end
21
+ def receive_sender sender
22
+ @my_sender = sender
23
+ #p sender
24
+ true
25
+ end
26
+ def receive_recipient rcpt
27
+ @my_recipients ||= []
28
+ @my_recipients << rcpt
29
+ true
30
+ end
31
+ def receive_data_chunk c
32
+ @my_msg_body = c.last
33
+ end
34
+ def connection_ended
35
+ EM.stop
36
+ end
37
+ end
38
+
39
+ def test_mail
40
+ c = nil
41
+ EM.run {
42
+ EM.start_server( Localhost, Localport, Mailserver ) {|conn| c = conn}
43
+ EM::Timer.new(2) {EM.stop} # prevent hanging the test suite in case of error
44
+ EM::Protocols::SmtpClient.send :host=>Localhost,
45
+ :port=>Localport,
46
+ :domain=>"bogus",
47
+ :from=>"me@example.com",
48
+ :to=>"you@example.com",
49
+ :header=> {"Subject"=>"Email subject line", "Reply-to"=>"me@example.com"},
50
+ :body=>"Not much of interest here."
51
+
52
+ }
53
+ assert_equal( "Not much of interest here.", c.my_msg_body )
54
+ assert_equal( "<me@example.com>", c.my_sender )
55
+ assert_equal( ["<you@example.com>"], c.my_recipients )
56
+ end
57
+ end
@@ -0,0 +1,293 @@
1
+
2
+ require 'em_test_helper'
3
+
4
+
5
+
6
+ class TestSpawn < Test::Unit::TestCase
7
+
8
+ # Spawn a process that simply stops the reactor.
9
+ # Assert that the notification runs after the block that calls it.
10
+ #
11
+ def test_stop
12
+ x = nil
13
+ EM.run {
14
+ s = EM.spawn {EM.stop}
15
+ s.notify
16
+ x = true
17
+ }
18
+ assert x
19
+ end
20
+
21
+
22
+ # Pass a parameter to a spawned process.
23
+ #
24
+ def test_parms
25
+ val = 5
26
+ EM.run {
27
+ s = EM.spawn {|v| val *= v; EM.stop}
28
+ s.notify 3
29
+ }
30
+ assert_equal( 15, val )
31
+ end
32
+
33
+ # Pass multiple parameters to a spawned process.
34
+ #
35
+ def test_multiparms
36
+ val = 5
37
+ EM.run {
38
+ s = EM.spawn {|v1,v2| val *= (v1 + v2); EM.stop}
39
+ s.notify 3,4
40
+ }
41
+ assert_equal( 35, val )
42
+ end
43
+
44
+
45
+ # This test demonstrates that a notification does not happen immediately,
46
+ # but rather is scheduled sometime after the current code path completes.
47
+ #
48
+ def test_race
49
+ x = 0
50
+ EM.run {
51
+ s = EM.spawn {x *= 2; EM.stop}
52
+ s.notify
53
+ x = 2
54
+ }
55
+ assert_equal( 4, x)
56
+ end
57
+
58
+
59
+ # Spawn a process and notify it 25 times to run fibonacci
60
+ # on a pair of global variables.
61
+ #
62
+ def test_fibonacci
63
+ x = 1
64
+ y = 1
65
+ EM.run {
66
+ s = EM.spawn {x,y = y,x+y}
67
+ 25.times {s.notify}
68
+
69
+ t = EM.spawn {EM.stop}
70
+ t.notify
71
+ }
72
+ assert_equal( 121393, x)
73
+ assert_equal( 196418, y)
74
+ end
75
+
76
+ # This one spawns 25 distinct processes, and notifies each one once,
77
+ # rather than notifying a single process 25 times.
78
+ #
79
+ def test_another_fibonacci
80
+ x = 1
81
+ y = 1
82
+ EM.run {
83
+ 25.times {
84
+ s = EM.spawn {x,y = y,x+y}
85
+ s.notify
86
+ }
87
+
88
+ t = EM.spawn {EM.stop}
89
+ t.notify
90
+ }
91
+ assert_equal( 121393, x)
92
+ assert_equal( 196418, y)
93
+ end
94
+
95
+
96
+ # Make a chain of processes that notify each other in turn
97
+ # with intermediate fibonacci results. The final process in
98
+ # the chain stops the loop and returns the result.
99
+ #
100
+ def test_fibonacci_chain
101
+ a,b = nil
102
+
103
+ EM.run {
104
+ nextpid = EM.spawn {|x,y|
105
+ a,b = x,y
106
+ EM.stop
107
+ }
108
+
109
+ 25.times {
110
+ n = nextpid
111
+ nextpid = EM.spawn {|x,y| n.notify( y, x+y )}
112
+ }
113
+
114
+ nextpid.notify( 1, 1 )
115
+ }
116
+
117
+ assert_equal( 121393, a)
118
+ assert_equal( 196418, b)
119
+ end
120
+
121
+
122
+ # EM#yield gives a spawed process to yield control to other processes
123
+ # (in other words, to stop running), and to specify a different code block
124
+ # that will run on its next notification.
125
+ #
126
+ def test_yield
127
+ a = 0
128
+ EM.run {
129
+ n = EM.spawn {
130
+ a += 10
131
+ EM.yield {
132
+ a += 20
133
+ EM.yield {
134
+ a += 30
135
+ EM.stop
136
+ }
137
+ }
138
+ }
139
+ n.notify
140
+ n.notify
141
+ n.notify
142
+ }
143
+ assert_equal( 60, a )
144
+ end
145
+
146
+ # EM#yield_and_notify behaves like EM#yield, except that it also notifies the
147
+ # yielding process. This may sound trivial, since the yield block will run very
148
+ # shortly after with no action by the program, but this actually can be very useful,
149
+ # because it causes the reactor core to execute once before the yielding process
150
+ # gets control back. So it can be used to allow heavily-used network connections
151
+ # to clear buffers, or allow other processes to process their notifications.
152
+ #
153
+ # Notice in this test code that only a simple notify is needed at the bottom
154
+ # of the initial block. Even so, all of the yielded blocks will execute.
155
+ #
156
+ def test_yield_and_notify
157
+ a = 0
158
+ EM.run {
159
+ n = EM.spawn {
160
+ a += 10
161
+ EM.yield_and_notify {
162
+ a += 20
163
+ EM.yield_and_notify {
164
+ a += 30
165
+ EM.stop
166
+ }
167
+ }
168
+ }
169
+ n.notify
170
+ }
171
+ assert_equal( 60, a )
172
+ end
173
+
174
+ # resume is an alias for notify.
175
+ #
176
+ def test_resume
177
+ EM.run {
178
+ n = EM.spawn {EM.stop}
179
+ n.resume
180
+ }
181
+ assert true
182
+ end
183
+
184
+ # run is an idiomatic alias for notify.
185
+ #
186
+ def test_run
187
+ EM.run {
188
+ (EM.spawn {EM.stop}).run
189
+ }
190
+ assert true
191
+ end
192
+
193
+
194
+ # Clones the ping-pong example from the Erlang tutorial, in much less code.
195
+ # Illustrates that a spawned block executes in the context of a SpawnableObject.
196
+ # (Meaning, we can pass self as a parameter to another process that can then
197
+ # notify us.)
198
+ #
199
+ def test_ping_pong
200
+ n_pongs = 0
201
+ EM.run {
202
+ pong = EM.spawn {|x, ping|
203
+ n_pongs += 1
204
+ ping.notify( x-1 )
205
+ }
206
+ ping = EM.spawn {|x|
207
+ if x > 0
208
+ pong.notify x, self
209
+ else
210
+ EM.stop
211
+ end
212
+ }
213
+ ping.notify 3
214
+ }
215
+ assert_equal( 3, n_pongs )
216
+ end
217
+
218
+ # Illustrates that you can call notify inside a notification, and it will cause
219
+ # the currently-executing process to be re-notified. Of course, the new notification
220
+ # won't run until sometime after the current one completes.
221
+ #
222
+ def test_self_notify
223
+ n = 0
224
+ EM.run {
225
+ pid = EM.spawn {|x|
226
+ if x > 0
227
+ n += x
228
+ notify( x-1 )
229
+ else
230
+ EM.stop
231
+ end
232
+ }
233
+ pid.notify 3
234
+ }
235
+ assert_equal( 6, n )
236
+ end
237
+
238
+
239
+ # Illustrates that the block passed to #spawn executes in the context of a
240
+ # SpawnedProcess object, NOT in the local context. This can often be deceptive.
241
+ #
242
+ class BlockScopeTest
243
+ attr_reader :var
244
+ def run
245
+ # The following line correctly raises a NameError.
246
+ # The problem is that the programmer expected the spawned block to
247
+ # execute in the local context, but it doesn't.
248
+ #
249
+ # (EM.spawn { do_something }).notify ### NO! BAD!
250
+
251
+
252
+
253
+ # The following line correctly passes self as a parameter to the
254
+ # notified process.
255
+ #
256
+ (EM.spawn {|obj| obj.do_something }).notify(self)
257
+
258
+
259
+
260
+ # Here's another way to do it. This works because "myself" is bound
261
+ # in the local scope, unlike "self," so the spawned block sees it.
262
+ #
263
+ myself = self
264
+ (EM.spawn { myself.do_something }).notify
265
+
266
+
267
+
268
+ # And we end the loop.
269
+ # This is a tangential point, but observe that #notify never blocks.
270
+ # It merely appends a message to the internal queue of a spawned process
271
+ # and returns. As it turns out, the reactor processes notifications for ALL
272
+ # spawned processes in the order that #notify is called. So there is a
273
+ # reasonable expectation that the process which stops the reactor will
274
+ # execute after the previous ones in this method. HOWEVER, this is NOT
275
+ # a documented behavior and is subject to change.
276
+ #
277
+ (EM.spawn {EM.stop}).notify
278
+ end
279
+ def do_something
280
+ @var ||= 0
281
+ @var += 100
282
+ end
283
+ end
284
+
285
+ def test_block_scope
286
+ bs = BlockScopeTest.new
287
+ EM.run {
288
+ bs.run
289
+ }
290
+ assert_equal( 200, bs.var )
291
+ end
292
+
293
+ end
@@ -0,0 +1,78 @@
1
+ require "test/unit"
2
+ require 'tempfile'
3
+
4
+ require 'em_test_helper'
5
+
6
+ module EM
7
+ def self._set_mocks
8
+ class <<self
9
+ alias set_tls_parms_old set_tls_parms
10
+ alias start_tls_old start_tls
11
+ begin
12
+ old, $VERBOSE = $VERBOSE, nil
13
+ def set_tls_parms *args; end
14
+ def start_tls *args; end
15
+ ensure
16
+ $VERBOSE = old
17
+ end
18
+ end
19
+ end
20
+
21
+ def self._clear_mocks
22
+ class <<self
23
+ begin
24
+ old, $VERBOSE = $VERBOSE, nil
25
+ alias set_tls_parms set_tls_parms_old
26
+ alias start_tls start_tls_old
27
+ ensure
28
+ $VERBOSE = old
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+
36
+ class TestSslArgs < Test::Unit::TestCase
37
+ def setup
38
+ EM._set_mocks
39
+ end
40
+
41
+ def teardown
42
+ EM._clear_mocks
43
+ end
44
+
45
+ def test_tls_params_file_doesnt_exist
46
+ priv_file, cert_file = 'foo_priv_key', 'bar_cert_file'
47
+ [priv_file, cert_file].all? do |f|
48
+ assert(!File.exists?(f), "Cert file #{f} seems to exist, and should not for the tests")
49
+ end
50
+
51
+ # associate_callback_target is a pain! (build!)
52
+ conn = EM::Connection.new('foo')
53
+
54
+ assert_raises(EM::FileNotFoundException) do
55
+ conn.start_tls(:private_key_file => priv_file)
56
+ end
57
+ assert_raises(EM::FileNotFoundException) do
58
+ conn.start_tls(:cert_chain_file => cert_file)
59
+ end
60
+ assert_raises(EM::FileNotFoundException) do
61
+ conn.start_tls(:private_key_file => priv_file, :cert_chain_file => cert_file)
62
+ end
63
+ end
64
+
65
+ def test_tls_params_file_does_exist
66
+ priv_file = Tempfile.new('em_test')
67
+ cert_file = Tempfile.new('em_test')
68
+ priv_file_path = priv_file.path
69
+ cert_file_path = cert_file.path
70
+ conn = EM::Connection.new('foo')
71
+ params = {:private_key_file => priv_file_path, :cert_chain_file => cert_file_path}
72
+ begin
73
+ conn.start_tls params
74
+ rescue Object
75
+ assert(false, 'should not have raised an exception')
76
+ end
77
+ end
78
+ end if EM.ssl?
@@ -0,0 +1,60 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestSSLEchoData < Test::Unit::TestCase
4
+ def setup
5
+ $dir = File.dirname(File.expand_path(__FILE__)) + '/'
6
+ end
7
+
8
+ module SslEchoServer
9
+ def post_init
10
+ start_tls(:private_key_file => $dir+'server.key', :cert_chain_file => $dir+'server.crt')
11
+ end
12
+
13
+ def receive_data data
14
+ send_data data
15
+ end
16
+
17
+ end
18
+
19
+ module SslEchoClient
20
+ def connection_completed
21
+ start_tls
22
+ end
23
+
24
+ def ssl_handshake_completed
25
+ send_data $expected_return_data[$index]
26
+ $index += 1
27
+ end
28
+
29
+ def receive_data data
30
+ $actual_return_data ||= []
31
+ $actual_return_data << data
32
+ if $index < 10
33
+ send_data $expected_return_data[$index]
34
+ $index += 1
35
+ else
36
+ @stopping_on_purpose = true
37
+ EM.stop
38
+ end
39
+ end
40
+
41
+ def unbind
42
+ fail "unexpected socket close" unless @stopping_on_purpose
43
+ end
44
+
45
+ end
46
+
47
+ def test_ssl_echo_data
48
+ $expected_return_data = (1..10).map {|n| "Hello, world! (#{n})"}
49
+ $index = 0
50
+
51
+ EM.run {
52
+ EM.add_timer(12) { fail "TIMEOUT" }
53
+ EM.start_server("127.0.0.1", 9999, SslEchoServer)
54
+ EM.connect("127.0.0.1", 9999, SslEchoClient)
55
+ }
56
+
57
+ assert_equal($expected_return_data, $actual_return_data)
58
+ end
59
+
60
+ end if EM.ssl?
@@ -0,0 +1,56 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestSSLMethods < Test::Unit::TestCase
4
+ def setup
5
+ $dir = File.dirname(File.expand_path(__FILE__)) + '/'
6
+ $client_cert_from_file = File.read($dir+'client.crt')
7
+ $server_cert_from_file = File.read($dir+'server.crt')
8
+ end
9
+
10
+ module ServerHandler
11
+
12
+ def post_init
13
+ start_tls(:private_key_file => $dir+'server.key', :cert_chain_file => $dir+'server.crt', :verify_peer => true)
14
+ end
15
+
16
+ def ssl_handshake_completed
17
+ $server_called_back = true
18
+ $client_cert_value = get_peer_cert
19
+ end
20
+
21
+ def ssl_verify_peer cert
22
+ true
23
+ end
24
+ end
25
+
26
+ module ClientHandler
27
+
28
+ def post_init
29
+ start_tls(:private_key_file => $dir+'client.key', :cert_chain_file => $dir+'client.crt')
30
+ end
31
+
32
+ def ssl_handshake_completed
33
+ $client_called_back = true
34
+ $server_cert_value = get_peer_cert
35
+ EM.stop_event_loop
36
+ end
37
+
38
+ end
39
+
40
+ def test_ssl_methods
41
+ $server_called_back, $client_called_back = false, false
42
+ $server_cert_value, $client_cert_value = nil, nil
43
+
44
+ EM.run {
45
+ EM.start_server("127.0.0.1", 9999, ServerHandler)
46
+ EM.connect("127.0.0.1", 9999, ClientHandler)
47
+ }
48
+
49
+ assert($server_called_back)
50
+ assert($client_called_back)
51
+
52
+ assert_equal($server_cert_from_file, $server_cert_value.gsub("\r", ""))
53
+ assert_equal($client_cert_from_file, $client_cert_value.gsub("\r", ""))
54
+ end
55
+
56
+ end if EM.ssl?