cowboy_test.erl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. %% Copyright (c) 2014-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(cowboy_test).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. %% Listeners initialization.
  19. init_http(Ref, ProtoOpts, Config) ->
  20. {ok, _} = cowboy:start_clear(Ref, [{port, 0}], ProtoOpts),
  21. Port = ranch:get_port(Ref),
  22. [{ref, Ref}, {type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config].
  23. init_https(Ref, ProtoOpts, Config) ->
  24. Opts = ct_helper:get_certs_from_ets(),
  25. {ok, _} = cowboy:start_tls(Ref, Opts ++ [{port, 0}], ProtoOpts),
  26. Port = ranch:get_port(Ref),
  27. [{ref, Ref}, {type, ssl}, {protocol, http}, {port, Port}, {opts, Opts}|Config].
  28. init_http2(Ref, ProtoOpts, Config) ->
  29. Opts = ct_helper:get_certs_from_ets(),
  30. {ok, _} = cowboy:start_tls(Ref, Opts ++ [{port, 0}], ProtoOpts),
  31. Port = ranch:get_port(Ref),
  32. [{ref, Ref}, {type, ssl}, {protocol, http2}, {port, Port}, {opts, Opts}|Config].
  33. %% @todo This will probably require TransOpts as argument.
  34. init_http3(Ref, ProtoOpts, Config) ->
  35. Port = 4567,
  36. %% @todo Quicer does not currently support non-file cert/key,
  37. %% so we use quicer test certificates for now.
  38. %% @todo Quicer also does not support cacerts which means
  39. %% we currently have no authentication based security.
  40. DataDir = filename:dirname(filename:dirname(config(data_dir, Config)))
  41. ++ "/rfc9114_SUITE_data",
  42. TransOpts = #{
  43. socket_opts => [
  44. {cert, DataDir ++ "/server.pem"},
  45. {key, DataDir ++ "/server.key"}
  46. ]
  47. },
  48. {ok, _} = cowboy:start_quic(TransOpts, ProtoOpts), %% @todo Ref argument.
  49. [{ref, Ref}, {type, quic}, {protocol, http3}, {port, Port}, {opts, TransOpts}|Config].
  50. %% Common group of listeners used by most suites.
  51. common_all() ->
  52. [
  53. {group, http},
  54. {group, https},
  55. {group, h2},
  56. {group, h2c},
  57. {group, h3},
  58. {group, http_compress},
  59. {group, https_compress},
  60. {group, h2_compress},
  61. {group, h2c_compress}
  62. ].
  63. common_groups(Tests) ->
  64. Opts = case os:getenv("NO_PARALLEL") of
  65. false -> [parallel];
  66. _ -> []
  67. end,
  68. [
  69. {http, Opts, Tests},
  70. {https, Opts, Tests},
  71. {h2, Opts, Tests},
  72. {h2c, Opts, Tests},
  73. {h3, [], Tests}, %% @todo Enable parallel when issues get fixed.
  74. {http_compress, Opts, Tests},
  75. {https_compress, Opts, Tests},
  76. {h2_compress, Opts, Tests},
  77. {h2c_compress, Opts, Tests}
  78. ].
  79. init_common_groups(Name = http, Config, Mod) ->
  80. init_http(Name, #{
  81. env => #{dispatch => Mod:init_dispatch(Config)}
  82. }, [{flavor, vanilla}|Config]);
  83. init_common_groups(Name = https, Config, Mod) ->
  84. init_https(Name, #{
  85. env => #{dispatch => Mod:init_dispatch(Config)}
  86. }, [{flavor, vanilla}|Config]);
  87. init_common_groups(Name = h2, Config, Mod) ->
  88. init_http2(Name, #{
  89. env => #{dispatch => Mod:init_dispatch(Config)}
  90. }, [{flavor, vanilla}|Config]);
  91. init_common_groups(Name = h2c, Config, Mod) ->
  92. Config1 = init_http(Name, #{
  93. env => #{dispatch => Mod:init_dispatch(Config)}
  94. }, [{flavor, vanilla}|Config]),
  95. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  96. init_common_groups(Name = h3, Config, Mod) ->
  97. init_http3(Name, #{
  98. env => #{dispatch => Mod:init_dispatch(Config)}
  99. }, [{flavor, vanilla}|Config]);
  100. init_common_groups(Name = http_compress, Config, Mod) ->
  101. init_http(Name, #{
  102. env => #{dispatch => Mod:init_dispatch(Config)},
  103. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  104. }, [{flavor, compress}|Config]);
  105. init_common_groups(Name = https_compress, Config, Mod) ->
  106. init_https(Name, #{
  107. env => #{dispatch => Mod:init_dispatch(Config)},
  108. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  109. }, [{flavor, compress}|Config]);
  110. init_common_groups(Name = h2_compress, Config, Mod) ->
  111. init_http2(Name, #{
  112. env => #{dispatch => Mod:init_dispatch(Config)},
  113. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  114. }, [{flavor, compress}|Config]);
  115. init_common_groups(Name = h2c_compress, Config, Mod) ->
  116. Config1 = init_http(Name, #{
  117. env => #{dispatch => Mod:init_dispatch(Config)},
  118. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  119. }, [{flavor, compress}|Config]),
  120. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  121. %% Support functions for testing using Gun.
  122. gun_open(Config) ->
  123. gun_open(Config, #{}).
  124. gun_open(Config, Opts) ->
  125. TlsOpts = case proplists:get_value(no_cert, Config, false) of
  126. true -> [{verify, verify_none}];
  127. false -> ct_helper:get_certs_from_ets()
  128. end,
  129. {ok, ConnPid} = gun:open("localhost", config(port, Config), Opts#{
  130. retry => 0,
  131. transport => config(type, Config),
  132. tls_opts => TlsOpts,
  133. protocols => [config(protocol, Config)]
  134. }),
  135. ConnPid.
  136. gun_down(ConnPid) ->
  137. receive {gun_down, ConnPid, _, _, _} -> ok
  138. after 500 -> error(timeout) end.
  139. %% Support functions for testing using a raw socket.
  140. raw_open(Config) ->
  141. Transport = case config(type, Config) of
  142. tcp -> gen_tcp;
  143. ssl -> ssl
  144. end,
  145. {_, Opts} = lists:keyfind(opts, 1, Config),
  146. {ok, Socket} = Transport:connect("localhost", config(port, Config),
  147. [binary, {active, false}, {packet, raw},
  148. {reuseaddr, true}, {nodelay, true}|Opts]),
  149. {raw_client, Socket, Transport}.
  150. raw_send({raw_client, Socket, Transport}, Data) ->
  151. Transport:send(Socket, Data).
  152. raw_recv_head({raw_client, Socket, Transport}) ->
  153. {ok, Data} = Transport:recv(Socket, 0, 10000),
  154. raw_recv_head(Socket, Transport, Data).
  155. raw_recv_head(Socket, Transport, Buffer) ->
  156. case binary:match(Buffer, <<"\r\n\r\n">>) of
  157. nomatch ->
  158. {ok, Data} = Transport:recv(Socket, 0, 10000),
  159. raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
  160. {_, _} ->
  161. Buffer
  162. end.
  163. raw_recv({raw_client, Socket, Transport}, Length, Timeout) ->
  164. Transport:recv(Socket, Length, Timeout).
  165. raw_expect_recv({raw_client, _, _}, <<>>) ->
  166. ok;
  167. raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
  168. {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 10000),
  169. ok.