Skip to content

Commit e3a3050

Browse files
committed
test/test_pair, test/test_ssl: fix for TLS 1.3
Fix test cases failing with TLS 1.3-enabled OpenSSL master.
1 parent 315b650 commit e3a3050

File tree

2 files changed

+135
-176
lines changed

2 files changed

+135
-176
lines changed

test/test_pair.rb

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,41 @@
55
if defined?(OpenSSL::TestUtils)
66

77
module OpenSSL::SSLPairM
8-
def server
9-
host = "127.0.0.1"
10-
port = 0
11-
ctx = OpenSSL::SSL::SSLContext.new()
12-
ctx.ciphers = "ADH"
13-
ctx.security_level = 0
14-
ctx.tmp_dh_callback = proc { OpenSSL::TestUtils::Fixtures.pkey_dh("dh1024") }
15-
tcps = create_tcp_server(host, port)
16-
ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
17-
return ssls
8+
def setup
9+
svr_dn = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=localhost")
10+
ee_exts = [
11+
["keyUsage", "keyEncipherment,digitalSignature", true],
12+
]
13+
@svr_key = OpenSSL::TestUtils::Fixtures.pkey("rsa1024")
14+
@svr_cert = issue_cert(svr_dn, @svr_key, 1, ee_exts, nil, nil)
1815
end
1916

20-
def client(port)
17+
def ssl_pair
2118
host = "127.0.0.1"
22-
ctx = OpenSSL::SSL::SSLContext.new()
23-
ctx.ciphers = "ADH"
24-
ctx.security_level = 0
25-
s = create_tcp_client(host, port)
26-
ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
27-
ssl.connect
28-
ssl.sync_close = true
29-
ssl
30-
end
19+
tcps = create_tcp_server(host, 0)
20+
port = tcps.connect_address.ip_port
3121

32-
def ssl_pair
33-
ssls = server
3422
th = Thread.new {
23+
sctx = OpenSSL::SSL::SSLContext.new
24+
sctx.cert = @svr_cert
25+
sctx.key = @svr_key
26+
sctx.tmp_dh_callback = proc { OpenSSL::TestUtils::Fixtures.pkey_dh("dh1024") }
27+
ssls = OpenSSL::SSL::SSLServer.new(tcps, sctx)
3528
ns = ssls.accept
3629
ssls.close
3730
ns
3831
}
39-
port = ssls.to_io.local_address.ip_port
40-
c = client(port)
32+
33+
tcpc = create_tcp_client(host, port)
34+
c = OpenSSL::SSL::SSLSocket.new(tcpc)
35+
c.connect
4136
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
37+
38+
yield c, s
5239
ensure
53-
if th&.alive?
54-
th.kill
55-
th.join
56-
end
40+
tcpc&.close
41+
tcps&.close
42+
s&.close
5743
end
5844
end
5945

@@ -83,23 +69,27 @@ def create_tcp_client(host, port)
8369

8470
module OpenSSL::TestEOF1M
8571
def open_file(content)
86-
s1, s2 = ssl_pair
87-
th = Thread.new { s2 << content; s2.close }
88-
yield s1
89-
ensure
90-
th.join if th
91-
s1.close
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+
}
9280
end
9381
end
9482

9583
module OpenSSL::TestEOF2M
9684
def open_file(content)
97-
s1, s2 = ssl_pair
98-
th = Thread.new { s1 << content; s1.close }
99-
yield s2
100-
ensure
101-
th.join if th
102-
s2.close
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+
}
10393
end
10494
end
10595

@@ -373,8 +363,8 @@ def tcp_pair
373363

374364
def test_connect_accept_nonblock_no_exception
375365
ctx2 = OpenSSL::SSL::SSLContext.new
376-
ctx2.ciphers = "ADH"
377-
ctx2.security_level = 0
366+
ctx2.cert = @svr_cert
367+
ctx2.key = @svr_key
378368
ctx2.tmp_dh_callback = proc { OpenSSL::TestUtils::Fixtures.pkey_dh("dh1024") }
379369

380370
sock1, sock2 = tcp_pair
@@ -384,8 +374,6 @@ def test_connect_accept_nonblock_no_exception
384374
assert_equal :wait_readable, accepted
385375

386376
ctx1 = OpenSSL::SSL::SSLContext.new
387-
ctx1.ciphers = "ADH"
388-
ctx1.security_level = 0
389377
s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx1)
390378
th = Thread.new do
391379
rets = []
@@ -423,8 +411,8 @@ def test_connect_accept_nonblock_no_exception
423411

424412
def test_connect_accept_nonblock
425413
ctx = OpenSSL::SSL::SSLContext.new()
426-
ctx.ciphers = "ADH"
427-
ctx.security_level = 0
414+
ctx.cert = @svr_cert
415+
ctx.key = @svr_key
428416
ctx.tmp_dh_callback = proc { OpenSSL::TestUtils::Fixtures.pkey_dh("dh1024") }
429417

430418
sock1, sock2 = tcp_pair
@@ -447,8 +435,6 @@ def test_connect_accept_nonblock
447435

448436
sleep 0.1
449437
ctx = OpenSSL::SSL::SSLContext.new()
450-
ctx.ciphers = "ADH"
451-
ctx.security_level = 0
452438
s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx)
453439
begin
454440
sleep 0.2

0 commit comments

Comments
 (0)