PageRenderTime 73ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/nicksieger/jruby
Ruby | 144 lines | 126 code | 18 blank | 0 comment | 2 complexity | 9da94f8f24d2c1a38bc0cd3bd35610f4 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. dir = File.expand_path(__FILE__)
  9. 2.times {dir = File.dirname(dir)}
  10. $:.replace([File.join(dir, "ruby")] | $:)
  11. require 'ut_eof'
  12. module SSLPair
  13. def server
  14. host = "127.0.0.1"
  15. port = 0
  16. ctx = OpenSSL::SSL::SSLContext.new()
  17. ctx.ciphers = "ADH"
  18. tcps = TCPServer.new(host, port)
  19. ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
  20. return ssls
  21. end
  22. def client(port)
  23. host = "127.0.0.1"
  24. ctx = OpenSSL::SSL::SSLContext.new()
  25. ctx.ciphers = "ADH"
  26. s = TCPSocket.new(host, port)
  27. ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
  28. ssl.connect
  29. ssl.sync_close = true
  30. ssl
  31. end
  32. def ssl_pair
  33. ssls = server
  34. th = Thread.new {
  35. ns = ssls.accept
  36. ssls.close
  37. ns
  38. }
  39. port = ssls.to_io.addr[1]
  40. c = client(port)
  41. s = th.value
  42. if block_given?
  43. begin
  44. yield c, s
  45. ensure
  46. c.close unless c.closed?
  47. s.close unless s.closed?
  48. end
  49. else
  50. return c, s
  51. end
  52. end
  53. end
  54. class OpenSSL::TestEOF1 < Test::Unit::TestCase
  55. include TestEOF
  56. include SSLPair
  57. def open_file(content)
  58. s1, s2 = ssl_pair
  59. Thread.new { s2 << content; s2.close }
  60. yield s1
  61. end
  62. end
  63. class OpenSSL::TestEOF2 < Test::Unit::TestCase
  64. include TestEOF
  65. include SSLPair
  66. def open_file(content)
  67. s1, s2 = ssl_pair
  68. Thread.new { s1 << content; s1.close }
  69. yield s2
  70. end
  71. end
  72. class OpenSSL::TestPair < Test::Unit::TestCase
  73. include SSLPair
  74. def test_getc
  75. ssl_pair {|s1, s2|
  76. s1 << "a"
  77. assert_equal(?a, s2.getc)
  78. }
  79. end
  80. def test_readpartial
  81. ssl_pair {|s1, s2|
  82. s2.write "a\nbcd"
  83. assert_equal("a\n", s1.gets)
  84. assert_equal("bcd", s1.readpartial(10))
  85. s2.write "efg"
  86. assert_equal("efg", s1.readpartial(10))
  87. s2.close
  88. assert_raise(EOFError) { s1.readpartial(10) }
  89. assert_raise(EOFError) { s1.readpartial(10) }
  90. assert_equal("", s1.readpartial(0))
  91. }
  92. end
  93. def test_readall
  94. ssl_pair {|s1, s2|
  95. s2.close
  96. assert_equal("", s1.read)
  97. }
  98. end
  99. def test_readline
  100. ssl_pair {|s1, s2|
  101. s2.close
  102. assert_raise(EOFError) { s1.readline }
  103. }
  104. end
  105. def test_puts_meta
  106. ssl_pair {|s1, s2|
  107. begin
  108. old = $/
  109. $/ = '*'
  110. s1.puts 'a'
  111. ensure
  112. $/ = old
  113. end
  114. s1.close
  115. assert_equal("a\n", s2.read)
  116. }
  117. end
  118. def test_puts_empty
  119. ssl_pair {|s1, s2|
  120. s1.puts
  121. s1.close
  122. assert_equal("\n", s2.read)
  123. }
  124. end
  125. end
  126. end