File: 73-starttls-buffering.lua

package info (click to toggle)
lua-cqueues 20161214-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,672 kB
  • ctags: 3,228
  • sloc: ansic: 20,232; sh: 2,959; makefile: 24
file content (94 lines) | stat: -rwxr-xr-x 2,169 bytes parent folder | download | duplicates (4)
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
#!/bin/sh
_=[[
	. "${0%%/*}/regress.sh"
	exec runlua "$0" "$@"
]]
require"regress".export".*"

local context = require"openssl.ssl.context"

local function starttls(autoflush, pushback)
	local cq = cqueues.new()
	local A, B = check(fileresult(socket.pair()))
	local cv = condition.new()
	local key, crt = genkey()
	local ctx = context.new("SSLv23", true)
	local text_unsecure = "unsecure"
	local text_secure = "secure"

	A:settimeout(2)
	B:settimeout(2)

	if autoflush then
		A:setmode(nil, "fba")
	else
		A:setmode(nil, "fbA")
	end

	if pushback then
		B:setmode("fbp", nil)
	else
		B:setmode("fbP", nil)
	end

	ctx:setCertificate(crt)
	ctx:setPrivateKey(key)

	info("test autoflush:%s pushback:%s", tostring(autoflush), tostring(pushback))

	cq:wrap(function()
		info("(A) sending %d bytes", #text_unsecure)
		check(fileresult(A:write(text_unsecure)))

		cv:signal()

		info"(A) initiating TLS handshake"
		local ok, why, error = fileresult(A:starttls())
		info("(A) starttls error: %d", error or 0)
		if pushback and autoflush then
			check(ok, "(A) pushback/autoflush test failed (%s)", why)
		else
			check(not ok, "(A) pushback/autoflush control test failed")
			return
		end

		info"(A) handshake complete"
		check(fileresult(A:write(text_secure)))
		check(fileresult(A:flush()))
	end)

	cq:wrap(function()
		check(fileresult(cv:wait()))

		info("(B) reading %d bytes", #text_unsecure)
		local text, why, error = fileresult(B:read(#text_unsecure))
		info("(B) read error: %d", error or 0)
		if autoflush then
			check(text == text_unsecure, "(B) autoflush test failed (%s)", text or why)
		else
			check(text ~= text_unsecure, "(B) autoflush control test failed")
			return
		end

		info"(B) initiating TLS handshake"
		local ok, why, error = fileresult(B:starttls(ctx))
		info("(B) starttls error: %d", error or 0)
		if pushback then
			check(ok, "(B) pushback test failed (%s)", why)
		else
			check(not ok, "(B) pushback control test failed")
			return
		end

		info"(B) handshake complete"
		check(check(fileresult(B:read(#text_secure))) == text_secure)
	end)

	check(cq:loop())
end

starttls(true, false)
starttls(false, true)
starttls(true, true)

say"OK"