Skip to content

Commit db2917c

Browse files
committedAug 30, 2022
fix test suite
1 parent d0ef7d9 commit db2917c

File tree

3 files changed

+50
-53
lines changed

3 files changed

+50
-53
lines changed
 

‎src/ssl.jl

-2
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ end
561561

562562
function Base.eof(ssl::SSLStream)::Bool
563563
isclosed(ssl) && return true
564-
# @show 1, isreadable(ssl), bytesavailable(ssl)
565564
Base.@lock ssl.lock begin
566565
while isreadable(ssl) && bytesavailable(ssl) <= 0
567566
# no immediate pending bytes, so let's check underlying socket
@@ -570,7 +569,6 @@ function Base.eof(ssl::SSLStream)::Bool
570569
end
571570
force_read_buffer(ssl)
572571
end
573-
# @show 2, isreadable(ssl), bytesavailable(ssl)
574572
return !isreadable(ssl) && bytesavailable(ssl) <= 0
575573
end
576574
end

‎test/http_helpers.jl

+33-21
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,47 @@ function test_server()
2222
sign_certificate(x509_certificate, evp_pkey)
2323

2424
server_socket = listen(5000)
25-
accepted_socket = accept(server_socket)
25+
try
26+
accepted_socket = accept(server_socket)
2627

27-
# Create and configure server SSLContext.
28-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSv12ServerMethod())
29-
_ = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
28+
# Create and configure server SSLContext.
29+
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSServerMethod())
30+
_ = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
3031

31-
OpenSSL.ssl_set_ciphersuites(ssl_ctx, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256")
32-
OpenSSL.ssl_use_certificate(ssl_ctx, x509_certificate)
33-
OpenSSL.ssl_use_private_key(ssl_ctx, evp_pkey)
32+
OpenSSL.ssl_set_ciphersuites(ssl_ctx, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256")
33+
OpenSSL.ssl_use_certificate(ssl_ctx, x509_certificate)
34+
OpenSSL.ssl_use_private_key(ssl_ctx, evp_pkey)
3435

35-
ssl = SSLStream(ssl_ctx, accepted_socket, accepted_socket)
36+
ssl = SSLStream(ssl_ctx, accepted_socket, accepted_socket)
3637

37-
OpenSSL.accept(ssl)
38+
OpenSSL.accept(ssl)
3839

39-
bytes_available = bytesavailable(ssl)
40-
request = read(ssl, bytes_available)
41-
reply = "reply: $(String(request))"
40+
@test !eof(ssl)
41+
request = readavailable(ssl)
42+
reply = "reply: $(String(request))"
4243

43-
# eof(ssl) will block
44+
# eof(ssl) will block
4445

45-
# Verify the are no more bytes available in the stream.
46-
@test bytesavailable(ssl) == 0
46+
# Verify the are no more bytes available in the stream.
47+
@test bytesavailable(ssl) == 0
4748

48-
write(ssl, reply)
49+
write(ssl, reply)
4950

50-
close(ssl)
51-
finalize(ssl_ctx)
51+
try
52+
close(ssl)
53+
catch
54+
end
55+
finalize(ssl_ctx)
56+
finally
57+
close(server_socket)
58+
end
5259
return nothing
5360
end
5461

5562
function test_client()
5663
tcp_stream = connect(5000)
5764

58-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSv12ClientMethod())
65+
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSClientMethod())
5966
ssl_options = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
6067

6168
# Create SSL stream.
@@ -74,13 +81,18 @@ function test_client()
7481

7582
written = unsafe_write(ssl, pointer(request_str), length(request_str))
7683

84+
sleep(1)
85+
@test !eof(ssl)
7786
@test length(request_str) == written
7887

79-
response_str = String(read(ssl))
88+
response_str = String(readavailable(ssl))
8089

8190
@test response_str == "reply: $request_str"
8291

83-
close(ssl)
92+
try
93+
close(ssl)
94+
catch
95+
end
8496
finalize(ssl_ctx)
8597
return nothing
8698
end

‎test/runtests.jl

+17-30
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using OpenSSL_jll
55
using Sockets
66
using Test
77

8-
include("http_helpers.jl")
8+
include(joinpath(dirname(pathof(OpenSSL)), "../test/http_helpers.jl"))
99

1010
macro catch_exception_object(code)
1111
quote
@@ -15,7 +15,7 @@ macro catch_exception_object(code)
1515
catch e
1616
e
1717
end
18-
if err == nothing
18+
if err === nothing
1919
error("Expected exception, got $err.")
2020
end
2121
err
@@ -177,7 +177,7 @@ end
177177
@testset "HttpsConnect" begin
178178
tcp_stream = connect("www.nghttp2.org", 443)
179179

180-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSv12ClientMethod())
180+
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSClientMethod())
181181
result = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
182182

183183
# Create SSL stream.
@@ -195,20 +195,20 @@ end
195195

196196
written = write(ssl, request_str)
197197

198+
@test !eof(ssl)
198199
io = IOBuffer()
199-
while !eof(ssl)
200-
write(io, readavailable(ssl))
201-
end
200+
sleep(2)
201+
write(io, readavailable(ssl))
202202
response = String(take!(io))
203-
203+
@test startswith(response, "HTTP/1.1 200 OK\r\n")
204204
close(ssl)
205205
finalize(ssl_ctx)
206206
end
207207

208208
@testset "ClosedStream" begin
209209
tcp_stream = connect("www.nghttp2.org", 443)
210210

211-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSv12ClientMethod())
211+
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSClientMethod())
212212
result = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
213213
OpenSSL.ssl_set_ciphersuites(ssl_ctx, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256")
214214

@@ -221,14 +221,12 @@ end
221221

222222
request_str = "GET / HTTP/1.1\r\nHost: www.nghttp2.org\r\nUser-Agent: curl\r\nAccept: */*\r\n\r\n"
223223

224-
err = @catch_exception_object unsafe_write(ssl, pointer(request_str), length(request_str))
225-
@test typeof(err) == Base.IOError
226-
224+
@test_throws ArgumentError unsafe_write(ssl, pointer(request_str), length(request_str))
227225
finalize(ssl_ctx)
228226
end
229227

230228
@testset "NoCloseStream" begin
231-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSv12ClientMethod())
229+
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSClientMethod())
232230
result = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
233231

234232
# Create SSL stream.
@@ -239,29 +237,18 @@ end
239237
request_str = "GET / HTTP/1.1\r\nHost: www.nghttp2.org\r\nUser-Agent: curl\r\nAccept: */*\r\n\r\n"
240238
unsafe_write(ssl, pointer(request_str), length(request_str))
241239

242-
response = read(ssl)
243-
@test contains(String(response), "HTTP/1.1 200 OK")
240+
@test !eof(ssl)
241+
io = IOBuffer()
242+
sleep(2)
243+
write(io, readavailable(ssl))
244+
response = String(take!(io))
245+
@test startswith(response, "HTTP/1.1 200 OK\r\n")
244246

245247
# Do not close SSLStream, leave it to the finalizer.
246248
#close(ssl)
247249
#finalize(ssl_ctx)
248250
end
249251

250-
@testset "InvalidStream" begin
251-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSv12ClientMethod())
252-
result = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
253-
254-
# Create SSL stream.
255-
tcp_stream = connect("www.nghttp2.org", 443)
256-
ssl = SSLStream(ssl_ctx, tcp_stream, tcp_stream)
257-
258-
err = @catch_exception_object read(ssl)
259-
@test typeof(err) == OpenSSL.OpenSSLError
260-
261-
close(ssl)
262-
free(ssl_ctx)
263-
end
264-
265252
@testset "Hash" begin
266253
res = digest(EvpMD5(), IOBuffer("The quick brown fox jumps over the lazy dog"))
267254
@test res == UInt8[0x9e, 0x10, 0x7d, 0x9d, 0x37, 0x2b, 0xb6, 0x82, 0x6b, 0xd8, 0x1d, 0x35, 0x42, 0xa4, 0x19, 0xd6]
@@ -389,7 +376,7 @@ end
389376
err_msg = OpenSSL.get_error()
390377
@test err_msg == ""
391378

392-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSv12ServerMethod())
379+
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSServerMethod())
393380

394381
# Make direct invalid call to OpenSSL
395382
invalid_cipher_suites = "TLS_AES_356_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256"

0 commit comments

Comments
 (0)
Please sign in to comment.