PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/externals/ruby1.9/openssl/test_pair.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 250 lines | 222 code | 28 blank | 0 comment | 10 complexity | 6b83eed09ca943d64de1355b5526c6f0 MD5 | raw file
Possible License(s): GPL-3.0, JSON
  1. begin
  2. require "openssl"
  3. rescue LoadError
  4. end
  5. require 'test/unit'
  6. if defined?(OpenSSL)
  7. require 'socket'
  8. require_relative '../ruby/ut_eof'
  9. module SSLPair
  10. def server
  11. host = "127.0.0.1"
  12. port = 0
  13. ctx = OpenSSL::SSL::SSLContext.new()
  14. ctx.ciphers = "ADH"
  15. tcps = TCPServer.new(host, port)
  16. ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
  17. return ssls
  18. end
  19. def client(port)
  20. host = "127.0.0.1"
  21. ctx = OpenSSL::SSL::SSLContext.new()
  22. ctx.ciphers = "ADH"
  23. s = TCPSocket.new(host, port)
  24. ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
  25. ssl.connect
  26. ssl.sync_close = true
  27. ssl
  28. end
  29. def ssl_pair
  30. ssls = server
  31. th = Thread.new {
  32. ns = ssls.accept
  33. ssls.close
  34. ns
  35. }
  36. port = ssls.to_io.addr[1]
  37. c = client(port)
  38. s = th.value
  39. if block_given?
  40. begin
  41. yield c, s
  42. ensure
  43. c.close unless c.closed?
  44. s.close unless s.closed?
  45. end
  46. else
  47. return c, s
  48. end
  49. ensure
  50. if th && th.alive?
  51. th.kill
  52. th.join
  53. end
  54. end
  55. end
  56. class OpenSSL::TestEOF1 < Test::Unit::TestCase
  57. include TestEOF
  58. include SSLPair
  59. def open_file(content)
  60. s1, s2 = ssl_pair
  61. Thread.new { s2 << content; s2.close }
  62. yield s1
  63. end
  64. end
  65. class OpenSSL::TestEOF2 < Test::Unit::TestCase
  66. include TestEOF
  67. include SSLPair
  68. def open_file(content)
  69. s1, s2 = ssl_pair
  70. Thread.new { s1 << content; s1.close }
  71. yield s2
  72. end
  73. end
  74. class OpenSSL::TestPair < Test::Unit::TestCase
  75. include SSLPair
  76. def test_getc
  77. ssl_pair {|s1, s2|
  78. s1 << "a"
  79. assert_equal(?a, s2.getc)
  80. }
  81. end
  82. def test_readpartial
  83. ssl_pair {|s1, s2|
  84. s2.write "a\nbcd"
  85. assert_equal("a\n", s1.gets)
  86. assert_equal("bcd", s1.readpartial(10))
  87. s2.write "efg"
  88. assert_equal("efg", s1.readpartial(10))
  89. s2.close
  90. assert_raise(EOFError) { s1.readpartial(10) }
  91. assert_raise(EOFError) { s1.readpartial(10) }
  92. assert_equal("", s1.readpartial(0))
  93. }
  94. end
  95. def test_readall
  96. ssl_pair {|s1, s2|
  97. s2.close
  98. assert_equal("", s1.read)
  99. }
  100. end
  101. def test_readline
  102. ssl_pair {|s1, s2|
  103. s2.close
  104. assert_raise(EOFError) { s1.readline }
  105. }
  106. end
  107. def test_puts_meta
  108. ssl_pair {|s1, s2|
  109. begin
  110. old = $/
  111. $/ = '*'
  112. s1.puts 'a'
  113. ensure
  114. $/ = old
  115. end
  116. s1.close
  117. assert_equal("a\n", s2.read)
  118. }
  119. end
  120. def test_puts_empty
  121. ssl_pair {|s1, s2|
  122. s1.puts
  123. s1.close
  124. assert_equal("\n", s2.read)
  125. }
  126. end
  127. def test_read_nonblock
  128. ssl_pair {|s1, s2|
  129. err = nil
  130. assert_raise(OpenSSL::SSL::SSLError) {
  131. begin
  132. s2.read_nonblock(10)
  133. ensure
  134. err = $!
  135. end
  136. }
  137. assert_kind_of(IO::WaitReadable, err)
  138. s1.write "abc\ndef\n"
  139. IO.select([s2])
  140. assert_equal("ab", s2.read_nonblock(2))
  141. assert_equal("c\n", s2.gets)
  142. ret = nil
  143. assert_nothing_raised("[ruby-core:20298]") { ret = s2.read_nonblock(10) }
  144. assert_equal("def\n", ret)
  145. }
  146. end
  147. def test_write_nonblock
  148. ssl_pair {|s1, s2|
  149. n = 0
  150. begin
  151. n += s1.write_nonblock("a" * 100000)
  152. n += s1.write_nonblock("b" * 100000)
  153. n += s1.write_nonblock("c" * 100000)
  154. n += s1.write_nonblock("d" * 100000)
  155. n += s1.write_nonblock("e" * 100000)
  156. n += s1.write_nonblock("f" * 100000)
  157. rescue IO::WaitWritable
  158. end
  159. s1.close
  160. assert_equal(n, s2.read.length)
  161. }
  162. end
  163. def test_write_nonblock_with_buffered_data
  164. ssl_pair {|s1, s2|
  165. s1.write "foo"
  166. s1.write_nonblock("bar")
  167. s1.write "baz"
  168. s1.close
  169. assert_equal("foobarbaz", s2.read)
  170. }
  171. end
  172. def test_connect_accept_nonblock
  173. host = "127.0.0.1"
  174. port = 0
  175. ctx = OpenSSL::SSL::SSLContext.new()
  176. ctx.ciphers = "ADH"
  177. serv = TCPServer.new(host, port)
  178. ssls = OpenSSL::SSL::SSLServer.new(serv, ctx)
  179. port = serv.connect_address.ip_port
  180. sock1 = TCPSocket.new(host, port)
  181. sock2 = serv.accept
  182. serv.close
  183. th = Thread.new {
  184. s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx)
  185. s2.sync_close = true
  186. begin
  187. sleep 0.2
  188. s2.accept_nonblock
  189. rescue IO::WaitReadable
  190. IO.select([s2])
  191. retry
  192. rescue IO::WaitWritable
  193. IO.select(nil, [s2])
  194. retry
  195. end
  196. s2
  197. }
  198. sleep 0.1
  199. ctx = OpenSSL::SSL::SSLContext.new()
  200. ctx.ciphers = "ADH"
  201. s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx)
  202. begin
  203. sleep 0.2
  204. s1.connect_nonblock
  205. rescue IO::WaitReadable
  206. IO.select([s1])
  207. retry
  208. rescue IO::WaitWritable
  209. IO.select(nil, [s1])
  210. retry
  211. end
  212. s1.sync_close = true
  213. s2 = th.value
  214. s1.print "a\ndef"
  215. assert_equal("a\n", s2.gets)
  216. ensure
  217. serv.close if serv && !serv.closed?
  218. sock1.close if sock1 && !sock1.closed?
  219. sock2.close if sock2 && !sock2.closed?
  220. end
  221. end
  222. end