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

/test/openssl/test_pair.rb

https://github.com/ahwuyeah/ruby
Ruby | 372 lines | 329 code | 41 blank | 2 comment | 18 complexity | 1a2928f99200222296f3af8c1f989212 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, Unlicense, GPL-2.0
  1. require_relative 'utils'
  2. if defined?(OpenSSL)
  3. require 'socket'
  4. require_relative '../ruby/ut_eof'
  5. module OpenSSL::SSLPairM
  6. def server
  7. host = "127.0.0.1"
  8. port = 0
  9. ctx = OpenSSL::SSL::SSLContext.new()
  10. ctx.ciphers = "ADH"
  11. ctx.tmp_dh_callback = proc { OpenSSL::TestUtils::TEST_KEY_DH1024 }
  12. tcps = create_tcp_server(host, port)
  13. ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
  14. return ssls
  15. end
  16. def client(port)
  17. host = "127.0.0.1"
  18. ctx = OpenSSL::SSL::SSLContext.new()
  19. ctx.ciphers = "ADH"
  20. s = create_tcp_client(host, port)
  21. ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
  22. ssl.connect
  23. ssl.sync_close = true
  24. ssl
  25. end
  26. def ssl_pair
  27. ssls = server
  28. th = Thread.new {
  29. ns = ssls.accept
  30. ssls.close
  31. ns
  32. }
  33. port = ssls.to_io.local_address.ip_port
  34. c = client(port)
  35. s = th.value
  36. if block_given?
  37. begin
  38. yield c, s
  39. ensure
  40. c.close unless c.closed?
  41. s.close unless s.closed?
  42. end
  43. else
  44. return c, s
  45. end
  46. ensure
  47. if th && th.alive?
  48. th.kill
  49. th.join
  50. end
  51. end
  52. end
  53. module OpenSSL::SSLPair
  54. include OpenSSL::SSLPairM
  55. def create_tcp_server(host, port)
  56. TCPServer.new(host, port)
  57. end
  58. def create_tcp_client(host, port)
  59. TCPSocket.new(host, port)
  60. end
  61. end
  62. module OpenSSL::SSLPairLowlevelSocket
  63. include OpenSSL::SSLPairM
  64. def create_tcp_server(host, port)
  65. Addrinfo.tcp(host, port).listen
  66. end
  67. def create_tcp_client(host, port)
  68. Addrinfo.tcp(host, port).connect
  69. end
  70. end
  71. module OpenSSL::TestEOF1M
  72. def open_file(content)
  73. s1, s2 = ssl_pair
  74. th = Thread.new { s2 << content; s2.close }
  75. yield s1
  76. ensure
  77. th.join
  78. s1.close
  79. end
  80. end
  81. module OpenSSL::TestEOF2M
  82. def open_file(content)
  83. s1, s2 = ssl_pair
  84. th = Thread.new { s1 << content; s1.close }
  85. yield s2
  86. ensure
  87. th.join
  88. s2.close
  89. end
  90. end
  91. module OpenSSL::TestPairM
  92. def test_getc
  93. ssl_pair {|s1, s2|
  94. s1 << "a"
  95. assert_equal(?a, s2.getc)
  96. }
  97. end
  98. def test_readpartial
  99. ssl_pair {|s1, s2|
  100. s2.write "a\nbcd"
  101. assert_equal("a\n", s1.gets)
  102. result = ""
  103. result << s1.readpartial(10) until result.length == 3
  104. assert_equal("bcd", result)
  105. s2.write "efg"
  106. result = ""
  107. result << s1.readpartial(10) until result.length == 3
  108. assert_equal("efg", result)
  109. s2.close
  110. assert_raise(EOFError) { s1.readpartial(10) }
  111. assert_raise(EOFError) { s1.readpartial(10) }
  112. assert_equal("", s1.readpartial(0))
  113. }
  114. end
  115. def test_readall
  116. ssl_pair {|s1, s2|
  117. s2.close
  118. assert_equal("", s1.read)
  119. }
  120. end
  121. def test_readline
  122. ssl_pair {|s1, s2|
  123. s2.close
  124. assert_raise(EOFError) { s1.readline }
  125. }
  126. end
  127. def test_puts_meta
  128. ssl_pair {|s1, s2|
  129. begin
  130. old = $/
  131. $/ = '*'
  132. s1.puts 'a'
  133. ensure
  134. $/ = old
  135. end
  136. s1.close
  137. assert_equal("a\n", s2.read)
  138. }
  139. end
  140. def test_puts_empty
  141. ssl_pair {|s1, s2|
  142. s1.puts
  143. s1.close
  144. assert_equal("\n", s2.read)
  145. }
  146. end
  147. def test_read_nonblock
  148. ssl_pair {|s1, s2|
  149. err = nil
  150. assert_raise(OpenSSL::SSL::SSLErrorWaitReadable) {
  151. begin
  152. s2.read_nonblock(10)
  153. ensure
  154. err = $!
  155. end
  156. }
  157. assert_kind_of(IO::WaitReadable, err)
  158. s1.write "abc\ndef\n"
  159. IO.select([s2])
  160. assert_equal("ab", s2.read_nonblock(2))
  161. assert_equal("c\n", s2.gets)
  162. ret = nil
  163. assert_nothing_raised("[ruby-core:20298]") { ret = s2.read_nonblock(10) }
  164. assert_equal("def\n", ret)
  165. s1.close
  166. sleep 0.1
  167. assert_raise(EOFError) { s2.read_nonblock(10) }
  168. }
  169. end
  170. def test_read_nonblock_no_exception
  171. ssl_pair {|s1, s2|
  172. assert_equal :wait_readable, s2.read_nonblock(10, exception: false)
  173. s1.write "abc\ndef\n"
  174. IO.select([s2])
  175. assert_equal("ab", s2.read_nonblock(2, exception: false))
  176. assert_equal("c\n", s2.gets)
  177. ret = nil
  178. assert_nothing_raised("[ruby-core:20298]") { ret = s2.read_nonblock(10, exception: false) }
  179. assert_equal("def\n", ret)
  180. s1.close
  181. sleep 0.1
  182. assert_equal(nil, s2.read_nonblock(10, exception: false))
  183. }
  184. end
  185. def write_nonblock(socket, meth, str)
  186. ret = socket.send(meth, str)
  187. ret.is_a?(Symbol) ? 0 : ret
  188. end
  189. def write_nonblock_no_ex(socket, str)
  190. ret = socket.write_nonblock str, exception: false
  191. ret.is_a?(Symbol) ? 0 : ret
  192. end
  193. def test_write_nonblock
  194. ssl_pair {|s1, s2|
  195. n = 0
  196. begin
  197. n += write_nonblock s1, :write_nonblock, "a" * 100000
  198. n += write_nonblock s1, :write_nonblock, "b" * 100000
  199. n += write_nonblock s1, :write_nonblock, "c" * 100000
  200. n += write_nonblock s1, :write_nonblock, "d" * 100000
  201. n += write_nonblock s1, :write_nonblock, "e" * 100000
  202. n += write_nonblock s1, :write_nonblock, "f" * 100000
  203. rescue IO::WaitWritable
  204. end
  205. s1.close
  206. assert_equal(n, s2.read.length)
  207. }
  208. end
  209. def test_write_nonblock_no_exceptions
  210. ssl_pair {|s1, s2|
  211. n = 0
  212. begin
  213. n += write_nonblock_no_ex s1, "a" * 100000
  214. n += write_nonblock_no_ex s1, "b" * 100000
  215. n += write_nonblock_no_ex s1, "c" * 100000
  216. n += write_nonblock_no_ex s1, "d" * 100000
  217. n += write_nonblock_no_ex s1, "e" * 100000
  218. n += write_nonblock_no_ex s1, "f" * 100000
  219. rescue OpenSSL::SSL::SSLError => e
  220. # on some platforms (maybe depend on OpenSSL version), writing to
  221. # SSLSocket after SSL_ERROR_WANT_WRITE causes this error.
  222. raise e if n == 0
  223. end
  224. s1.close
  225. assert_equal(n, s2.read.length)
  226. }
  227. end
  228. def test_write_nonblock_with_buffered_data
  229. ssl_pair {|s1, s2|
  230. s1.write "foo"
  231. s1.write_nonblock("bar")
  232. s1.write "baz"
  233. s1.close
  234. assert_equal("foobarbaz", s2.read)
  235. }
  236. end
  237. def test_write_nonblock_with_buffered_data_no_exceptions
  238. ssl_pair {|s1, s2|
  239. s1.write "foo"
  240. s1.write_nonblock("bar", exception: false)
  241. s1.write "baz"
  242. s1.close
  243. assert_equal("foobarbaz", s2.read)
  244. }
  245. end
  246. def tcp_pair
  247. host = "127.0.0.1"
  248. serv = TCPServer.new(host, 0)
  249. port = serv.connect_address.ip_port
  250. sock1 = TCPSocket.new(host, port)
  251. sock2 = serv.accept
  252. serv.close
  253. [sock1, sock2]
  254. ensure
  255. serv.close if serv && !serv.closed?
  256. end
  257. def test_connect_accept_nonblock
  258. ctx = OpenSSL::SSL::SSLContext.new()
  259. ctx.ciphers = "ADH"
  260. ctx.tmp_dh_callback = proc { OpenSSL::TestUtils::TEST_KEY_DH1024 }
  261. sock1, sock2 = tcp_pair
  262. th = Thread.new {
  263. s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx)
  264. s2.sync_close = true
  265. begin
  266. sleep 0.2
  267. s2.accept_nonblock
  268. rescue IO::WaitReadable
  269. IO.select([s2])
  270. retry
  271. rescue IO::WaitWritable
  272. IO.select(nil, [s2])
  273. retry
  274. end
  275. s2
  276. }
  277. sleep 0.1
  278. ctx = OpenSSL::SSL::SSLContext.new()
  279. ctx.ciphers = "ADH"
  280. s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx)
  281. begin
  282. sleep 0.2
  283. s1.connect_nonblock
  284. rescue IO::WaitReadable
  285. IO.select([s1])
  286. retry
  287. rescue IO::WaitWritable
  288. IO.select(nil, [s1])
  289. retry
  290. end
  291. s1.sync_close = true
  292. s2 = th.value
  293. s1.print "a\ndef"
  294. assert_equal("a\n", s2.gets)
  295. ensure
  296. th.join
  297. s1.close if s1 && !s1.closed?
  298. s2.close if s2 && !s2.closed?
  299. sock1.close if sock1 && !sock1.closed?
  300. sock2.close if sock2 && !sock2.closed?
  301. end
  302. end
  303. class OpenSSL::TestEOF1 < Test::Unit::TestCase
  304. include TestEOF
  305. include OpenSSL::SSLPair
  306. include OpenSSL::TestEOF1M
  307. end
  308. class OpenSSL::TestEOF1LowlevelSocket < Test::Unit::TestCase
  309. include TestEOF
  310. include OpenSSL::SSLPairLowlevelSocket
  311. include OpenSSL::TestEOF1M
  312. end
  313. class OpenSSL::TestEOF2 < Test::Unit::TestCase
  314. include TestEOF
  315. include OpenSSL::SSLPair
  316. include OpenSSL::TestEOF2M
  317. end
  318. class OpenSSL::TestEOF2LowlevelSocket < Test::Unit::TestCase
  319. include TestEOF
  320. include OpenSSL::SSLPairLowlevelSocket
  321. include OpenSSL::TestEOF2M
  322. end
  323. class OpenSSL::TestPair < Test::Unit::TestCase
  324. include OpenSSL::SSLPair
  325. include OpenSSL::TestPairM
  326. end
  327. class OpenSSL::TestPairLowlevelSocket < Test::Unit::TestCase
  328. include OpenSSL::SSLPairLowlevelSocket
  329. include OpenSSL::TestPairM
  330. end
  331. end