ws_autobahn_SUITE.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. %% Copyright (c) 2011-2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(ws_autobahn_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. %% ct.
  20. all() ->
  21. [{group, autobahn}].
  22. groups() ->
  23. [{autobahn, [], [autobahn_fuzzingclient]}].
  24. init_per_group(Name, Config) ->
  25. %% Some systems have it named pip2.
  26. Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
  27. case string:str(Out, "autobahntestsuite") of
  28. 0 ->
  29. ct:print("Skipping the autobahn group because the "
  30. "Autobahn Test Suite is not installed.~nTo install it, "
  31. "please follow the instructions on this page:~n~n "
  32. "http://autobahn.ws/testsuite/installation.html"),
  33. {skip, "Autobahn Test Suite not installed."};
  34. _ ->
  35. {ok, _} = cowboy:start_clear(Name, [{port, 33080}], #{
  36. env => #{dispatch => init_dispatch()}
  37. }),
  38. Config
  39. end.
  40. end_per_group(Listener, _Config) ->
  41. cowboy:stop_listener(Listener).
  42. %% Dispatch configuration.
  43. init_dispatch() ->
  44. cowboy_router:compile([
  45. {"localhost", [
  46. {"/ws_echo", ws_echo, []}
  47. ]}
  48. ]).
  49. %% Tests.
  50. autobahn_fuzzingclient(Config) ->
  51. doc("Autobahn test suite for the Websocket protocol."),
  52. Self = self(),
  53. spawn_link(fun() -> do_start_port(Config, Self) end),
  54. receive autobahn_exit -> ok end,
  55. ct:log("<h2><a href=\"log_private/reports/servers/index.html\">Full report</a></h2>~n"),
  56. Report = config(priv_dir, Config) ++ "reports/servers/index.html",
  57. ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
  58. {ok, HTML} = file:read_file(Report),
  59. case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
  60. true -> error(failed);
  61. false -> ok
  62. end.
  63. do_start_port(Config, Pid) ->
  64. Port = open_port({spawn, "wstest -m fuzzingclient -s " ++ config(data_dir, Config) ++ "client.json"},
  65. [{line, 10000}, {cd, config(priv_dir, Config)}, binary, eof]),
  66. do_receive_infinity(Port, Pid).
  67. do_receive_infinity(Port, Pid) ->
  68. receive
  69. {Port, {data, {eol, Line}}} ->
  70. io:format(user, "~s~n", [Line]),
  71. do_receive_infinity(Port, Pid);
  72. {Port, eof} ->
  73. Pid ! autobahn_exit
  74. end.