sec_SUITE.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. %% Copyright (c) 2018, 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(sec_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(ct_helper, [get_remote_pid_tcp/1]).
  20. -import(cowboy_test, [gun_open/1]).
  21. -import(cowboy_test, [raw_open/1]).
  22. -import(cowboy_test, [raw_send/2]).
  23. -import(cowboy_test, [raw_recv_head/1]).
  24. -import(cowboy_test, [raw_recv/3]).
  25. -import(cowboy_test, [raw_expect_recv/2]).
  26. %% ct.
  27. all() ->
  28. cowboy_test:common_all().
  29. groups() ->
  30. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  31. init_per_suite(Config) ->
  32. ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
  33. Config.
  34. end_per_suite(Config) ->
  35. ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
  36. init_per_group(Name, Config) ->
  37. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  38. end_per_group(Name, _) ->
  39. cowboy:stop_listener(Name).
  40. %% Routes.
  41. init_dispatch(_) ->
  42. cowboy_router:compile([{"localhost", [
  43. {"/", hello_h, []}
  44. ]}]).
  45. %% Tests.
  46. nc_rand(Config) ->
  47. doc("Throw random garbage at the server, then check if it's still up."),
  48. do_nc(Config, "/dev/urandom").
  49. nc_zero(Config) ->
  50. doc("Throw zeroes at the server, then check if it's still up."),
  51. do_nc(Config, "/dev/zero").
  52. do_nc(Config, Input) ->
  53. Cat = os:find_executable("cat"),
  54. Nc = os:find_executable("nc"),
  55. case {Cat, Nc} of
  56. {false, _} ->
  57. {skip, {not_found, cat}};
  58. {_, false} ->
  59. {skip, {not_found, nc}};
  60. _ ->
  61. StrPort = integer_to_list(config(port, Config)),
  62. _ = [
  63. os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  64. || _ <- lists:seq(1, 100)],
  65. ConnPid = gun_open(Config),
  66. Ref = gun:get(ConnPid, "/"),
  67. {response, _, 200, _} = gun:await(ConnPid, Ref),
  68. ok
  69. end.
  70. slowloris(Config) ->
  71. doc("Send request headers one byte at a time. "
  72. "Confirm that the connection gets closed."),
  73. _ = case config(protocol, Config) of
  74. http ->
  75. do_http_slowloris(Config);
  76. http2 ->
  77. %% @todo Write an equivalent test for HTTP2.
  78. ok
  79. end.
  80. do_http_slowloris(Config) ->
  81. Client = raw_open(Config),
  82. try
  83. [begin
  84. ok = raw_send(Client, [C]),
  85. timer:sleep(250)
  86. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  87. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  88. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  89. error(failure)
  90. catch error:{badmatch, _} ->
  91. ok
  92. end.
  93. slowloris_chunks(Config) ->
  94. _ = case config(protocol, Config) of
  95. http ->
  96. do_http_slowloris_chunks(Config);
  97. http2 ->
  98. %% @todo Write an equivalent test for HTTP2.
  99. ok
  100. end.
  101. do_http_slowloris_chunks(Config) ->
  102. doc("Send request headers one line at a time. "
  103. "Confirm that the connection gets closed."),
  104. Client = raw_open(Config),
  105. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  106. timer:sleep(300),
  107. ok = raw_send(Client, "Host: localhost\r\n"),
  108. timer:sleep(300),
  109. Data = raw_recv_head(Client),
  110. {'HTTP/1.1', 408, _, Rest} = cow_http:parse_status_line(Data),
  111. {Headers, _} = cow_http:parse_headers(Rest),
  112. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  113. {error, closed} = raw_recv(Client, 0, 1000).