xref: /openbmc/phosphor-misc/http-redirect/test.awk (revision 756eaf798dc804fc7936ff1121c6f3e441d29fac)
1BEGIN {
2	if (!resultfile)
3		resultfile="testfile"
4	if (!script)
5		script = "http-redirect.awk"
6	invoke="awk -f " script
7	cmd = invoke " > " resultfile
8	if (tracefile)
9		invoke = invoke " -v tracefile=" tracefile
10}
11
12function reportfail(resultfile, expect, request, headers)
13{
14	print("Testcase FAIL")
15	print("Expected to find :" expect)
16	print("actual:")
17	system("cat " resultfile)
18	print("expect:" expect)
19	print("input:")
20	print("reuest: " request)
21	for (h in headers)
22		print("headers:" headers[h])
23	print("")
24	print("FAIL")
25	exit(1)
26}
27
28function test(code, expect, request, headers)
29{
30	runtest(cmd, request, headers)
31	result = check(resultfile, code, expect, request, headers)
32	results[result] = results[result] + 1
33	if (result == "FAIL")
34		reportfail(resultfile, expect, request, headers)
35}
36
37function check(resultfile, code, expect, request, headers)
38{
39	line = ""
40	getline result < resultfile
41
42	while ((getline line < resultfile) > 0)
43		if (line ~ /^Location/)
44			found = line
45
46	close(resultfile)
47
48	if (code in locations)
49	if ((line == expect) && (location ~ " " code " "))
50		rc = "PASS"
51	else
52		rc = fail
53	return rc
54}
55
56function runtest(cmd, request, headers,	h, OORS)
57{
58	if (!cmd)
59		return
60	OORS = ORS
61	ORS = "\r\n"
62
63	print(request) | cmd
64	for (h in headers)
65		print(headers[h]) | cmd
66	print("") | cmd
67	close(cmd)
68
69	ORS = OORS
70}
71
72function add(array, value) {
73	counter = counter + 1
74	array[counter] = value
75}
76
77function mkrequest(method, path, version)
78{
79	return method " " path " " version
80}
81
82BEGIN {
83	failingtests()
84	passingtests()
85	exit(0)
86}
87
88function failingtests(headers) {
89	host1 = "somewhere.example.com"
90
91	headers[0] = "Host: " host1 ports[port]
92	version="HTTP/1.1"
93	path="/"
94	method="GET"
95
96	othermethods(path, version, headers)
97	test(505, "", mkrequest(method, path, "HTTP/1"), headers)
98	test(505, "", mkrequest(method, path, "HTTP/2.0"), headers)
99	test(505, "", mkrequest(method, path, "http/1.1"), headers)
100	junk[0] = "Host: abc_def.example.com"
101	test(400, "", mkrequest(method, path, "HTTP/1.1"), junk)
102	test(400, "", mkrequest(method, path, "HTTP/1.1"), headers)
103	test(404, "", mkrequest(method, "/abc", "HTTP/1.1"), junk)
104}
105
106function othermethods(path, version, headers, methods, m)
107{
108	add(methods, "PUT")
109	add(methods, "POST")
110	add(methods, "TRACE")
111	add(methods, "CONNECT")
112	add(methods, "get")
113	add(methods, "head")
114
115	for (m in methods)
116		test(501, "", mkrequest(methods[m], path, version), headers)
117}
118
119function passingtests(headers) {
120	method="GET"
121	path="/"
122	version="HTTP/1.1"
123
124	host1 = "somewhere.example.com"
125	host2 = "elsewhere.example.com"
126	mkports(ports)
127
128	expect="Location: https://" host1 "/\r"
129
130	request = mkrequest(method, path, version)
131	for (port in ports) {
132		headers[0] = "Host: " host1 ports[port]
133		test(308, expect, request, headers)
134	}
135	test(308, expect, mkrequest("HEAD", path, version), headers)
136	headers[0] = "Host: " host2
137	testabsuris(expect, headers, host1, ports)
138}
139
140function testabsuris(expect, headers, host, ports)
141{
142	mkabsuris(uris, host, ports)
143
144	# test with and without path
145	for (uri in uris) {
146		u = uris[uri]
147		test(308, expect, mkrequest(method, u, version), headers)
148		test(308, expect, mkrequest(method, u "/", version), headers)
149	}
150}
151
152function mkports(ports) {
153	add(ports, "")
154	add(ports, ":")
155	add(ports, ":8080")
156}
157
158function mkabsuris(uris, host, ports,	schemes, scheme, port)
159{
160	mkschemes(schemes)
161
162	for (scheme in schemes)
163		for (port in ports)
164			add(uris, schemes[scheme] "://" host ports[port] )
165}
166
167function mkschemes(schemes,	h, t1, t2, p, hs, ts, ps) {
168	mkletters(hs, 4, 8)
169	mkletters(ts, 5, 4)
170	mkletters(ps, 5, 0)
171
172	for (h in hs)
173		for (t1 in ts)
174			for (t2 in ts)
175				for (p in ps)
176					add(schemes, hs[h] ts[t1] ts[t2] ps[p])
177}
178
179function mkletters(ns, h1, h2)
180{
181	add(ns, "%" h1 h2)
182	add(ns, "%" (h1 + 2) h2)
183	add(ns, sprintf("%c", h1 * 16 + h2))
184	add(ns, sprintf("%c", (h1 + 2) * 16 + h2))
185
186}
187
188