File size: 4,158 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
describe("http.client module", function()
	local client = require "http.client"
	local http_connection_common = require "http.connection_common"
	local http_h1_connection = require "http.h1_connection"
	local http_h2_connection = require "http.h2_connection"
	local http_headers = require "http.headers"
	local http_tls = require "http.tls"
	local cqueues = require "cqueues"
	local ca = require "cqueues.auxlib"
	local cs = require "cqueues.socket"
	local openssl_pkey = require "openssl.pkey"
	local openssl_ctx = require "openssl.ssl.context"
	local openssl_x509 = require "openssl.x509"
	it("invalid network parameters return nil, err, errno", function()
		-- Invalid network parameters will return nil, err, errno
		local ok, err, errno = client.connect{host="127.0.0.1", port="invalid"}
		assert.same(nil, ok)
		assert.same("string", type(err))
		assert.same("number", type(errno))
	end)
	local function test_pair(client_options, server_func)
		local s, c = ca.assert(cs.pair())
		local cq = cqueues.new();
		cq:wrap(function()
			local conn = assert(client.negotiate(c, client_options))
			local stream = conn:new_stream()
			local req_headers = http_headers.new()
			req_headers:append(":authority", "myauthority")
			req_headers:append(":method", "GET")
			req_headers:append(":path", "/")
			req_headers:append(":scheme", client_options.tls and "https" or "http")
			assert(stream:write_headers(req_headers, true))
			local res_headers = assert(stream:get_headers())
			assert.same("200", res_headers:get(":status"))
		end)
		cq:wrap(function()
			s = server_func(s)
			if not s then return end
			if client_options.tls then
				local ssl = s:checktls()
				assert.same(client_options.sendname, ssl:getHostName())
			end
			local stream = assert(s:get_next_incoming_stream())
			assert(stream:get_headers())
			local res_headers = http_headers.new()
			res_headers:append(":status", "200")
			assert(stream:write_headers(res_headers, true))
		end)
		assert_loop(cq, TEST_TIMEOUT)
		assert.truthy(cq:empty())
		c:close()
		s:close()
	end
	local function new_server_ctx()
		local key = openssl_pkey.new()
		local crt = openssl_x509.new()
		crt:setPublicKey(key)
		crt:sign(key)
		local ctx = http_tls.new_server_context()
		assert(ctx:setPrivateKey(key))
		assert(ctx:setCertificate(crt))
		return ctx
	end
	it("works with an http/1.1 server", function()
		test_pair({}, function(s)
			return http_h1_connection.new(s, "server", 1.1)
		end)
	end)
	it("works with an http/2 server", function()
		test_pair({
			version = 2;
		}, function(s)
			return http_h2_connection.new(s, "server", {})
		end)
	end)
	it("fails with unknown http version", function()
		assert.has.error(function()
			test_pair({
				version = 5;
			}, function() end)
		end)
	end)
	it("works with an https/1.1 server", function()
		local client_ctx = http_tls.new_client_context()
		client_ctx:setVerify(openssl_ctx.VERIFY_NONE)
		test_pair({
			tls = true;
			ctx = client_ctx;
			sendname = "mysendname";
		}, function(s)
			assert(s:starttls(new_server_ctx()))
			return http_h1_connection.new(s, "server", 1.1)
		end)
	end)
	-- pending as older openssl (used by e.g. travis-ci) doesn't have any non-disallowed ciphers
	pending("works with an https/2 server", function()
		local client_ctx = http_tls.new_client_context()
		client_ctx:setVerify(openssl_ctx.VERIFY_NONE)
		test_pair({
			tls = true;
			ctx = client_ctx;
			sendname = "mysendname";
			version = 2;
		}, function(s)
			assert(s:starttls(new_server_ctx()))
			return http_h2_connection.new(s, "server", {})
		end)
	end)
	it("reports errors from :starttls", function()
		-- default settings should fail as it should't allow self-signed
		local s, c = ca.assert(cs.pair())
		local cq = cqueues.new();
		cq:wrap(function()
			local ok, err = client.negotiate(c, {
				tls = true;
			})
			assert.falsy(ok)
			assert.truthy(err:match("starttls: "))
		end)
		cq:wrap(function()
			s:onerror(http_connection_common.onerror)
			local ok, err = s:starttls()
			assert.falsy(ok)
			assert.truthy(err:match("starttls: "))
		end)
		assert_loop(cq, TEST_TIMEOUT)
		assert.truthy(cq:empty())
		c:close()
		s:close()
	end)
end)