Skip to content

Commit 297a003

Browse files
committed
ssl: move tests for IO-like methods to test_pair.rb
r8081 originally intended test_pair.rb for testing methods that behave like IO. Move tests for #{get,read}byte, #sys{read,write}, #close_write, and IO.copy_stream from test_ssl.rb to test_pair.rb. Similarly, move tests for methods that are specific to SSLSocket and not for IO compatibility to test_ssl.rb.
1 parent 80959f8 commit 297a003

2 files changed

Lines changed: 160 additions & 191 deletions

File tree

test/openssl/test_pair.rb

Lines changed: 49 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,36 @@ module OpenSSL::TestPairM
9797
def test_getc
9898
ssl_pair {|s1, s2|
9999
s1 << "a"
100+
s1.close
100101
assert_equal(?a, s2.getc)
102+
assert_nil(s2.getc)
101103
}
102104
end
103105

104106
def test_getbyte
105107
ssl_pair {|s1, s2|
106108
s1 << "a"
109+
s1.close
107110
assert_equal(97, s2.getbyte)
111+
assert_nil(s2.getbyte)
108112
}
109113
end
110114

111-
def test_readbyte
115+
def test_readchar
112116
ssl_pair {|s1, s2|
113117
s1 << "b"
114-
assert_equal(98, s2.readbyte)
118+
s1.close
119+
assert_equal("b", s2.readchar)
120+
assert_raise(EOFError) { s2.readchar }
115121
}
116122
end
117123

118-
def test_readbyte_eof
124+
def test_readbyte
119125
ssl_pair {|s1, s2|
120-
s2.close
121-
assert_raise(EOFError) { s1.readbyte }
126+
s1 << "b"
127+
s1.close
128+
assert_equal(98, s2.readbyte)
129+
assert_raise(EOFError) { s2.readbyte }
122130
}
123131
end
124132

@@ -216,6 +224,25 @@ def test_multibyte_read_write
216224
}
217225
end
218226

227+
def test_sysread_and_syswrite
228+
ssl_pair {|s1, s2|
229+
str = "x" * 100 + "\n"
230+
s1.syswrite(str)
231+
newstr = s2.sysread(str.bytesize)
232+
assert_equal(str, newstr)
233+
234+
buf = String.new
235+
s1.syswrite(str)
236+
assert_same(buf, s2.sysread(str.size, buf))
237+
assert_equal(str, buf)
238+
239+
obj = Object.new
240+
obj.define_singleton_method(:to_str) { str }
241+
s1.syswrite(obj)
242+
assert_equal(str, s2.sysread(str.bytesize))
243+
}
244+
end
245+
219246
def test_read_nonblock
220247
ssl_pair {|s1, s2|
221248
err = nil
@@ -393,116 +420,28 @@ def test_write_multiple_arguments
393420
}
394421
end
395422

396-
def test_partial_tls_record_read_nonblock
423+
def test_copy_stream
397424
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) }
425+
IO.pipe do |r, w|
426+
str = "hello world\n"
427+
w.write(str)
428+
IO.copy_stream(r, s1, str.bytesize)
429+
IO.copy_stream(s2, w, str.bytesize)
430+
assert_equal(str, r.read(str.bytesize))
431+
end
403432
}
404433
end
405434

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
435+
def test_close_write
436+
ssl_pair { |s1, s2|
437+
message = "abc"*1024
438+
s1.write(message)
439+
s1.close_write
440+
assert_equal(message, s2.read)
441+
s2.write(message)
442+
s2.close_write
443+
assert_equal(message, s1.read)
496444
}
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
506445
end
507446
end
508447

0 commit comments

Comments
 (0)