cowboy_test.erl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. %% @todo Quicer does not currently support non-file cert/key,
  36. %% so we use quicer test certificates for now.
  37. %% @todo Quicer also does not support cacerts which means
  38. %% we currently have no authentication based security.
  39. DataDir = filename:dirname(filename:dirname(config(data_dir, Config)))
  40. ++ "/rfc9114_SUITE_data",
  41. TransOpts = #{
  42. socket_opts => [
  43. {certfile, DataDir ++ "/server.pem"},
  44. {keyfile, DataDir ++ "/server.key"}
  45. ]
  46. },
  47. {ok, Listener} = cowboy:start_quic(TransOpts, ProtoOpts), %% @todo Ref argument.
  48. {ok, {_, Port}} = quicer:sockname(Listener),
  49. %% @todo Keep listener information around in a better place.
  50. persistent_term:put({cowboy_test_quic, Ref}, Listener),
  51. [{ref, Ref}, {type, quic}, {protocol, http3}, {port, Port}, {opts, TransOpts}|Config].
  52. stop_group(Ref) ->
  53. case persistent_term:get({cowboy_test_quic, Ref}, undefined) of
  54. undefined ->
  55. cowboy:stop_listener(Ref);
  56. Listener ->
  57. quicer:close_listener(Listener)
  58. end.
  59. %% Common group of listeners used by most suites.
  60. common_all() ->
  61. [
  62. {group, http},
  63. {group, https},
  64. {group, h2},
  65. {group, h2c},
  66. {group, h3},
  67. {group, http_compress},
  68. {group, https_compress},
  69. {group, h2_compress},
  70. {group, h2c_compress},
  71. {group, h3_compress}
  72. ].
  73. common_groups(Tests) ->
  74. Opts = case os:getenv("NO_PARALLEL") of
  75. false -> [parallel];
  76. _ -> []
  77. end,
  78. [
  79. {http, Opts, Tests},
  80. {https, Opts, Tests},
  81. {h2, Opts, Tests},
  82. {h2c, Opts, Tests},
  83. {h3, Opts, Tests},
  84. {http_compress, Opts, Tests},
  85. {https_compress, Opts, Tests},
  86. {h2_compress, Opts, Tests},
  87. {h2c_compress, Opts, Tests},
  88. {h3_compress, Opts, Tests}
  89. ].
  90. init_common_groups(Name = http, Config, Mod) ->
  91. init_http(Name, #{
  92. env => #{dispatch => Mod:init_dispatch(Config)}
  93. }, [{flavor, vanilla}|Config]);
  94. init_common_groups(Name = https, Config, Mod) ->
  95. init_https(Name, #{
  96. env => #{dispatch => Mod:init_dispatch(Config)}
  97. }, [{flavor, vanilla}|Config]);
  98. init_common_groups(Name = h2, Config, Mod) ->
  99. init_http2(Name, #{
  100. env => #{dispatch => Mod:init_dispatch(Config)}
  101. }, [{flavor, vanilla}|Config]);
  102. init_common_groups(Name = h2c, Config, Mod) ->
  103. Config1 = init_http(Name, #{
  104. env => #{dispatch => Mod:init_dispatch(Config)}
  105. }, [{flavor, vanilla}|Config]),
  106. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  107. init_common_groups(Name = h3, Config, Mod) ->
  108. init_http3(Name, #{
  109. env => #{dispatch => Mod:init_dispatch(Config)}
  110. }, [{flavor, vanilla}|Config]);
  111. init_common_groups(Name = http_compress, Config, Mod) ->
  112. init_http(Name, #{
  113. env => #{dispatch => Mod:init_dispatch(Config)},
  114. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  115. }, [{flavor, compress}|Config]);
  116. init_common_groups(Name = https_compress, Config, Mod) ->
  117. init_https(Name, #{
  118. env => #{dispatch => Mod:init_dispatch(Config)},
  119. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  120. }, [{flavor, compress}|Config]);
  121. init_common_groups(Name = h2_compress, Config, Mod) ->
  122. init_http2(Name, #{
  123. env => #{dispatch => Mod:init_dispatch(Config)},
  124. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  125. }, [{flavor, compress}|Config]);
  126. init_common_groups(Name = h2c_compress, Config, Mod) ->
  127. Config1 = init_http(Name, #{
  128. env => #{dispatch => Mod:init_dispatch(Config)},
  129. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  130. }, [{flavor, compress}|Config]),
  131. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  132. init_common_groups(Name = h3_compress, Config, Mod) ->
  133. init_http3(Name, #{
  134. env => #{dispatch => Mod:init_dispatch(Config)},
  135. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  136. }, [{flavor, compress}|Config]).
  137. %% Support functions for testing using Gun.
  138. gun_open(Config) ->
  139. gun_open(Config, #{}).
  140. gun_open(Config, Opts) ->
  141. TlsOpts = case proplists:get_value(no_cert, Config, false) of
  142. true -> [{verify, verify_none}];
  143. false -> ct_helper:get_certs_from_ets() %% @todo Wrong in current quicer.
  144. end,
  145. {ok, ConnPid} = gun:open("localhost", config(port, Config), Opts#{
  146. retry => 0,
  147. transport => config(type, Config),
  148. tls_opts => TlsOpts,
  149. protocols => [config(protocol, Config)]
  150. }),
  151. ConnPid.
  152. gun_down(ConnPid) ->
  153. receive {gun_down, ConnPid, _, _, _} -> ok
  154. after 500 -> error(timeout) end.
  155. %% Support functions for testing using a raw socket.
  156. raw_open(Config) ->
  157. Transport = case config(type, Config) of
  158. tcp -> gen_tcp;
  159. ssl -> ssl
  160. end,
  161. {_, Opts} = lists:keyfind(opts, 1, Config),
  162. {ok, Socket} = Transport:connect("localhost", config(port, Config),
  163. [binary, {active, false}, {packet, raw},
  164. {reuseaddr, true}, {nodelay, true}|Opts]),
  165. {raw_client, Socket, Transport}.
  166. raw_send({raw_client, Socket, Transport}, Data) ->
  167. Transport:send(Socket, Data).
  168. raw_recv_head({raw_client, Socket, Transport}) ->
  169. {ok, Data} = Transport:recv(Socket, 0, 10000),
  170. raw_recv_head(Socket, Transport, Data).
  171. raw_recv_head(Socket, Transport, Buffer) ->
  172. case binary:match(Buffer, <<"\r\n\r\n">>) of
  173. nomatch ->
  174. {ok, Data} = Transport:recv(Socket, 0, 10000),
  175. raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
  176. {_, _} ->
  177. Buffer
  178. end.
  179. raw_recv({raw_client, Socket, Transport}, Length, Timeout) ->
  180. Transport:recv(Socket, Length, Timeout).
  181. raw_expect_recv({raw_client, _, _}, <<>>) ->
  182. ok;
  183. raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
  184. {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 10000),
  185. ok.