Skip to content

Commit 7063d04

Browse files
committed
ssl: simplify test_pair.rb
OpenSSL::SSL::SSLSocket only depends on T_FILE and a small number of methods defined on IO, so the difference between TCPSocket and Socket is not significant for these tests. Test only one of them to reduce the test run time by half. Add a simple client using Socket to test_ssl.rb to keep basic coverage. Also simplify ut_eof.rb to test only one direction, since the direction does not matter after the handshake.
1 parent 297a003 commit 7063d04

3 files changed

Lines changed: 43 additions & 100 deletions

File tree

test/openssl/test_pair.rb

Lines changed: 19 additions & 100 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,57 +38,7 @@ 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

@@ -445,38 +394,8 @@ def test_close_write
445394
end
446395
end
447396

448-
class OpenSSL::TestEOF1 < OpenSSL::TestCase
449-
include OpenSSL::TestEOF
450-
include OpenSSL::SSLPair
451-
include OpenSSL::TestEOF1M
452-
end
453-
454-
class OpenSSL::TestEOF1LowlevelSocket < OpenSSL::TestCase
455-
include OpenSSL::TestEOF
456-
include OpenSSL::SSLPairLowlevelSocket
457-
include OpenSSL::TestEOF1M
458-
end
459-
460-
class OpenSSL::TestEOF2 < OpenSSL::TestCase
461-
include OpenSSL::TestEOF
462-
include OpenSSL::SSLPair
463-
include OpenSSL::TestEOF2M
464-
end
465-
466-
class OpenSSL::TestEOF2LowlevelSocket < OpenSSL::TestCase
467-
include OpenSSL::TestEOF
468-
include OpenSSL::SSLPairLowlevelSocket
469-
include OpenSSL::TestEOF2M
470-
end
471-
472-
class OpenSSL::TestPair < OpenSSL::TestCase
397+
class OpenSSL::TestSSLPair < OpenSSL::TestCase
473398
include OpenSSL::SSLPair
474399
include OpenSSL::TestPairM
475-
end
476-
477-
class OpenSSL::TestPairLowlevelSocket < OpenSSL::TestCase
478-
include OpenSSL::SSLPairLowlevelSocket
479-
include OpenSSL::TestPairM
480-
end
481-
400+
include OpenSSL::TestEOF
482401
end

test/openssl/test_ssl.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ def test_connect_accept_nonblock
198198
th&.join
199199
end
200200

201+
def test_low_level_socket
202+
start_server do |port|
203+
sock = Socket.tcp("127.0.0.1", port)
204+
ssl = OpenSSL::SSL::SSLSocket.new(sock)
205+
ssl.connect
206+
ssl.puts("abc")
207+
assert_equal("abc\n", ssl.gets)
208+
ensure
209+
ssl&.close
210+
sock&.close
211+
end
212+
end
213+
201214
def test_socket_open
202215
start_server { |port|
203216
begin

test/openssl/ut_eof.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
if defined?(OpenSSL)
55

66
module OpenSSL::TestEOF
7+
def open_file(content)
8+
ssl_pair { |s1, s2|
9+
begin
10+
th = Thread.new { s2 << content; s2.close }
11+
yield s1
12+
ensure
13+
th&.join
14+
end
15+
}
16+
end
17+
718
def test_getbyte_eof
819
open_file("") {|f| assert_nil f.getbyte }
920
end

0 commit comments

Comments
 (0)