Skip to content

Commit 614831d

Browse files
authored
Merge pull request #1097 from rhenium/ky/ssl-test-pair-refactor
Refactor SSL tests
2 parents 15094bd + c30c576 commit 614831d

5 files changed

Lines changed: 697 additions & 832 deletions

File tree

‎test/openssl/test_pair.rb‎

Lines changed: 68 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,34 @@
22
require_relative 'utils'
33
require_relative 'ut_eof'
44

5-
if defined?(OpenSSL::SSL)
5+
return unless defined?(OpenSSL::SSL)
66

7-
module OpenSSL::SSLPairM
8-
def setup
7+
module OpenSSL::SSLPair
8+
def ssl_pair
99
svr_dn = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=localhost")
1010
ee_exts = [
1111
["keyUsage", "keyEncipherment,digitalSignature", true],
1212
]
13-
@svr_key = OpenSSL::TestUtils::Fixtures.pkey("rsa-1")
14-
@svr_cert = issue_cert(svr_dn, @svr_key, 1, ee_exts, nil, nil)
15-
end
13+
svr_key = OpenSSL::TestUtils::Fixtures.pkey("rsa-1")
14+
svr_cert = issue_cert(svr_dn, svr_key, 1, ee_exts, nil, nil)
1615

17-
def ssl_pair
1816
host = "127.0.0.1"
19-
tcps = create_tcp_server(host, 0)
20-
port = tcps.connect_address.ip_port
17+
svr = TCPServer.new(host, 0)
18+
svr.setsockopt(:TCP, :NODELAY, 1)
19+
port = svr.connect_address.ip_port
2120

21+
tcps = nil
2222
th = Thread.new {
23+
tcps = svr.accept
2324
sctx = OpenSSL::SSL::SSLContext.new
24-
sctx.cert = @svr_cert
25-
sctx.key = @svr_key
26-
sctx.options |= OpenSSL::SSL::OP_NO_COMPRESSION
27-
ssls = OpenSSL::SSL::SSLServer.new(tcps, sctx)
28-
ns = ssls.accept
29-
ssls.close
30-
ns
25+
sctx.add_certificate(svr_cert, svr_key)
26+
ssl = OpenSSL::SSL::SSLSocket.new(tcps, sctx)
27+
ssl.accept
28+
ssl
3129
}
3230

33-
tcpc = create_tcp_client(host, port)
31+
tcpc = TCPSocket.new(host, port)
32+
tcpc.setsockopt(:TCP, :NODELAY, 1)
3433
c = OpenSSL::SSL::SSLSocket.new(tcpc)
3534
c.connect
3635
s = th.value
@@ -39,86 +38,44 @@ def ssl_pair
3938
ensure
4039
tcpc&.close
4140
tcps&.close
42-
s&.close
43-
end
44-
end
45-
46-
module OpenSSL::SSLPair
47-
include OpenSSL::SSLPairM
48-
49-
def create_tcp_server(host, port)
50-
TCPServer.new(host, port)
51-
end
52-
53-
def create_tcp_client(host, port)
54-
TCPSocket.new(host, port)
55-
end
56-
end
57-
58-
module OpenSSL::SSLPairLowlevelSocket
59-
include OpenSSL::SSLPairM
60-
61-
def create_tcp_server(host, port)
62-
Addrinfo.tcp(host, port).listen
63-
end
64-
65-
def create_tcp_client(host, port)
66-
Addrinfo.tcp(host, port).connect
67-
end
68-
end
69-
70-
module OpenSSL::TestEOF1M
71-
def open_file(content)
72-
ssl_pair { |s1, s2|
73-
begin
74-
th = Thread.new { s2 << content; s2.close }
75-
yield s1
76-
ensure
77-
th&.join
78-
end
79-
}
80-
end
81-
end
82-
83-
module OpenSSL::TestEOF2M
84-
def open_file(content)
85-
ssl_pair { |s1, s2|
86-
begin
87-
th = Thread.new { s1 << content; s1.close }
88-
yield s2
89-
ensure
90-
th&.join
91-
end
92-
}
41+
svr&.close
9342
end
9443
end
9544

9645
module OpenSSL::TestPairM
9746
def test_getc
9847
ssl_pair {|s1, s2|
9948
s1 << "a"
49+
s1.close
10050
assert_equal(?a, s2.getc)
51+
assert_nil(s2.getc)
10152
}
10253
end
10354

10455
def test_getbyte
10556
ssl_pair {|s1, s2|
10657
s1 << "a"
58+
s1.close
10759
assert_equal(97, s2.getbyte)
60+
assert_nil(s2.getbyte)
10861
}
10962
end
11063

111-
def test_readbyte
64+
def test_readchar
11265
ssl_pair {|s1, s2|
11366
s1 << "b"
114-
assert_equal(98, s2.readbyte)
67+
s1.close
68+
assert_equal("b", s2.readchar)
69+
assert_raise(EOFError) { s2.readchar }
11570
}
11671
end
11772

118-
def test_readbyte_eof
73+
def test_readbyte
11974
ssl_pair {|s1, s2|
120-
s2.close
121-
assert_raise(EOFError) { s1.readbyte }
75+
s1 << "b"
76+
s1.close
77+
assert_equal(98, s2.readbyte)
78+
assert_raise(EOFError) { s2.readbyte }
12279
}
12380
end
12481

@@ -216,6 +173,25 @@ def test_multibyte_read_write
216173
}
217174
end
218175

176+
def test_sysread_and_syswrite
177+
ssl_pair {|s1, s2|
178+
str = "x" * 100 + "\n"
179+
s1.syswrite(str)
180+
newstr = s2.sysread(str.bytesize)
181+
assert_equal(str, newstr)
182+
183+
buf = String.new
184+
s1.syswrite(str)
185+
assert_same(buf, s2.sysread(str.size, buf))
186+
assert_equal(str, buf)
187+
188+
obj = Object.new
189+
obj.define_singleton_method(:to_str) { str }
190+
s1.syswrite(obj)
191+
assert_equal(str, s2.sysread(str.bytesize))
192+
}
193+
end
194+
219195
def test_read_nonblock
220196
ssl_pair {|s1, s2|
221197
err = nil
@@ -393,151 +369,33 @@ def test_write_multiple_arguments
393369
}
394370
end
395371

396-
def test_partial_tls_record_read_nonblock
372+
def test_copy_stream
397373
ssl_pair { |s1, s2|
398-
# the beginning of a TLS record
399-
s1.io.write("\x17")
400-
# should raise a IO::WaitReadable since a full TLS record is not available
401-
# for reading
402-
assert_raise(IO::WaitReadable) { s2.read_nonblock(1) }
374+
IO.pipe do |r, w|
375+
str = "hello world\n"
376+
w.write(str)
377+
IO.copy_stream(r, s1, str.bytesize)
378+
IO.copy_stream(s2, w, str.bytesize)
379+
assert_equal(str, r.read(str.bytesize))
380+
end
403381
}
404382
end
405383

406-
def tcp_pair
407-
host = "127.0.0.1"
408-
serv = TCPServer.new(host, 0)
409-
port = serv.connect_address.ip_port
410-
sock1 = TCPSocket.new(host, port)
411-
sock2 = serv.accept
412-
serv.close
413-
[sock1, sock2]
414-
ensure
415-
serv.close if serv && !serv.closed?
416-
end
417-
418-
def test_connect_accept_nonblock_no_exception
419-
ctx2 = OpenSSL::SSL::SSLContext.new
420-
ctx2.cert = @svr_cert
421-
ctx2.key = @svr_key
422-
423-
sock1, sock2 = tcp_pair
424-
425-
s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2)
426-
accepted = s2.accept_nonblock(exception: false)
427-
assert_equal :wait_readable, accepted
428-
429-
ctx1 = OpenSSL::SSL::SSLContext.new
430-
s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx1)
431-
th = Thread.new do
432-
rets = []
433-
begin
434-
rv = s1.connect_nonblock(exception: false)
435-
rets << rv
436-
case rv
437-
when :wait_writable
438-
IO.select(nil, [s1], nil, 5)
439-
when :wait_readable
440-
IO.select([s1], nil, nil, 5)
441-
end
442-
end until rv == s1
443-
rets
444-
end
445-
446-
until th.join(0.01)
447-
accepted = s2.accept_nonblock(exception: false)
448-
assert_include([s2, :wait_readable, :wait_writable ], accepted)
449-
end
450-
451-
rets = th.value
452-
assert_instance_of Array, rets
453-
rets.each do |rv|
454-
assert_include([s1, :wait_readable, :wait_writable ], rv)
455-
end
456-
ensure
457-
th.join if th
458-
s1.close if s1
459-
s2.close if s2
460-
sock1.close if sock1
461-
sock2.close if sock2
462-
accepted.close if accepted.respond_to?(:close)
463-
end
464-
465-
def test_connect_accept_nonblock
466-
ctx = OpenSSL::SSL::SSLContext.new
467-
ctx.cert = @svr_cert
468-
ctx.key = @svr_key
469-
470-
sock1, sock2 = tcp_pair
471-
472-
th = Thread.new {
473-
s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx)
474-
5.times {
475-
begin
476-
break s2.accept_nonblock
477-
rescue IO::WaitReadable
478-
IO.select([s2], nil, nil, 1)
479-
rescue IO::WaitWritable
480-
IO.select(nil, [s2], nil, 1)
481-
end
482-
sleep 0.2
483-
}
484-
}
485-
486-
s1 = OpenSSL::SSL::SSLSocket.new(sock1)
487-
5.times {
488-
begin
489-
break s1.connect_nonblock
490-
rescue IO::WaitReadable
491-
IO.select([s1], nil, nil, 1)
492-
rescue IO::WaitWritable
493-
IO.select(nil, [s1], nil, 1)
494-
end
495-
sleep 0.2
384+
def test_close_write
385+
ssl_pair { |s1, s2|
386+
message = "abc"*1024
387+
s1.write(message)
388+
s1.close_write
389+
assert_equal(message, s2.read)
390+
s2.write(message)
391+
s2.close_write
392+
assert_equal(message, s1.read)
496393
}
497-
498-
s2 = th.value
499-
500-
s1.print "a\ndef"
501-
assert_equal("a\n", s2.gets)
502-
ensure
503-
sock1&.close
504-
sock2&.close
505-
th&.join
506394
end
507395
end
508396

509-
class OpenSSL::TestEOF1 < OpenSSL::TestCase
510-
include OpenSSL::TestEOF
397+
class OpenSSL::TestSSLPair < OpenSSL::TestCase
511398
include OpenSSL::SSLPair
512-
include OpenSSL::TestEOF1M
513-
end
514-
515-
class OpenSSL::TestEOF1LowlevelSocket < OpenSSL::TestCase
516-
include OpenSSL::TestEOF
517-
include OpenSSL::SSLPairLowlevelSocket
518-
include OpenSSL::TestEOF1M
519-
end
520-
521-
class OpenSSL::TestEOF2 < OpenSSL::TestCase
522-
include OpenSSL::TestEOF
523-
include OpenSSL::SSLPair
524-
include OpenSSL::TestEOF2M
525-
end
526-
527-
class OpenSSL::TestEOF2LowlevelSocket < OpenSSL::TestCase
528-
include OpenSSL::TestEOF
529-
include OpenSSL::SSLPairLowlevelSocket
530-
include OpenSSL::TestEOF2M
531-
end
532-
533-
class OpenSSL::TestPair < OpenSSL::TestCase
534-
include OpenSSL::SSLPair
535-
include OpenSSL::TestPairM
536-
end
537-
538-
class OpenSSL::TestPairLowlevelSocket < OpenSSL::TestCase
539-
include OpenSSL::SSLPairLowlevelSocket
540399
include OpenSSL::TestPairM
541-
end
542-
400+
include OpenSSL::TestEOF
543401
end

0 commit comments

Comments
 (0)