aboutsummaryrefslogtreecommitdiffstats
path: root/test/ws_SUITE.erl
blob: 2df43296b5bf4d6cd6532600d47d4d0b3fad4bad (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
%% Copyright (c) 2015, Loïc Hoguin <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-module(ws_SUITE).
-compile(export_all).

-import(ct_helper, [config/2]).

%% ct.

all() ->
	[{group, autobahn}].

groups() ->
	[{autobahn, [], [autobahn_fuzzingserver]}].

init_per_group(autobahn, Config) ->
	%% Some systems have it named pip2.
	Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
	case string:str(Out, "autobahntestsuite") of
		0 ->
			ct:print("Skipping the autobahn group because the "
				"Autobahn Test Suite is not installed.~nTo install it, "
				"please follow the instructions on this page:~n~n    "
				"http://autobahn.ws/testsuite/installation.html"),
			{skip, "Autobahn Test Suite not installed."};
		_ ->
			Config
	end.

end_per_group(_, _) ->
	oK.

%% Tests.

autobahn_fuzzingserver(Config) ->
	Self = self(),
	spawn_link(fun() -> start_port(Config, Self) end),
	receive autobahn_ready -> ok end,
	N = get_case_count(),
	run_cases(0, N),
	Report = config(priv_dir, Config) ++ "reports/clients/index.html",
	ct:log("<h2><a href=\"~s\">Full report</a></h2>~n", [Report]),
	ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
	log_output(),
	terminate(),
	{ok, HTML} = file:read_file(Report),
	case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
		true -> error(failed);
		false -> ok
	end.

start_port(Config, Pid) ->
	Port = open_port({spawn, "wstest -m fuzzingserver -s " ++ config(data_dir, Config) ++ "server.json"},
		[{line, 10000}, {cd, config(priv_dir, Config)}, binary]),
	receive_preamble(Port, Pid),
	receive_infinity(Port).

receive_preamble(Port, Pid) ->
	receive
		{Port, {data, {eol, Line = <<"Ok, will run", _/bits>>}}} ->
			Pid ! autobahn_ready,
			io:format(user, "~s~n", [Line]);
		{Port, {data, {eol, Line}}} ->
			io:format(user, "~s~n", [Line]),
			receive_preamble(Port, Pid)
	after 5000 ->
		terminate(),
		error(timeout)
	end.

receive_infinity(Port) ->
	receive
		{Port, {data, {eol, <<"Updating reports", _/bits>>}}} ->
			receive_infinity(Port);
		{Port, {data, {eol, Line}}} ->
			io:format(user, "~s~n", [Line]),
			receive_infinity(Port)
	end.

get_case_count() ->
	{Pid, Ref} = connect("/getCaseCount"),
	receive
		{gun_ws, Pid, {text, N}} ->
			close(Pid, Ref),
			binary_to_integer(N);
		Msg ->
			ct:print("Unexpected message ~p", [Msg]),
			terminate(),
			error(failed)
	end.

run_cases(Total, Total) ->
	ok;
run_cases(N, Total) ->
	{Pid, Ref} = connect(["/runCase?case=", integer_to_binary(N + 1), "&agent=Gun"]),
	loop(Pid, Ref),
	update_reports(),
	run_cases(N + 1, Total).

loop(Pid, Ref) ->
	receive
		{gun_ws, Pid, close} ->
			gun:ws_send(Pid, close),
			loop(Pid, Ref);
		{gun_ws, Pid, {close, Code, _}} ->
			gun:ws_send(Pid, {close, Code, <<>>}),
			loop(Pid, Ref);
		{gun_ws, Pid, Frame} ->
			gun:ws_send(Pid, Frame),
			loop(Pid, Ref);
		{gun_down, Pid, ws, _, _, _} ->
			close(Pid, Ref);
		{'DOWN', Ref, process, Pid, normal} ->
			close(Pid, Ref);
		Msg ->
			ct:print("Unexpected message ~p", [Msg]),
			close(Pid, Ref)
	end.

update_reports() ->
	{Pid, Ref} = connect("/updateReports?agent=Gun"),
	receive
		{gun_ws, Pid, close} ->
			close(Pid, Ref)
	after 5000 ->
		error(failed)
	end.

log_output() ->
	ok.

connect(Path) ->
	{ok, Pid} = gun:open("127.0.0.1", 33080, #{retry=>0}),
	receive
		{gun_up, Pid, http} ->
			ok
	after 1000 ->
		error(open_timeout)
	end,
	Ref = monitor(process, Pid),
	gun:ws_upgrade(Pid, Path, [], #{compress => true}),
	receive
		{gun_ws_upgrade, Pid, ok} ->
			ok;
		Msg ->
			ct:print("Unexpected message ~p", [Msg]),
			terminate(),
			error(failed)
	end,
	{Pid, Ref}.

close(Pid, Ref) ->
	demonitor(Ref),
	gun:close(Pid),
	gun:flush(Pid).

terminate() ->
	Res = os:cmd("killall wstest"),
	io:format(user, "~s", [Res]),
	ok.