spdy_SUITE.erl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. %% Copyright (c) 2013, 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(spdy_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. -include("../src/cowboy_spdy.hrl").
  17. %% ct.
  18. -export([all/0]).
  19. -export([groups/0]).
  20. -export([init_per_suite/1]).
  21. -export([end_per_suite/1]).
  22. -export([init_per_group/2]).
  23. -export([end_per_group/2]).
  24. %% Tests.
  25. -export([check_status/1]).
  26. %% ct.
  27. all() ->
  28. [{group, spdy}].
  29. groups() ->
  30. [{spdy, [], [
  31. check_status
  32. ]}].
  33. init_per_suite(Config) ->
  34. application:start(crypto),
  35. application:start(ranch),
  36. application:start(cowboy),
  37. application:start(asn1),
  38. application:start(public_key),
  39. application:start(ssl),
  40. Dir = ?config(priv_dir, Config) ++ "/static",
  41. ct_helper:create_static_dir(Dir),
  42. [{static_dir, Dir}|Config].
  43. end_per_suite(Config) ->
  44. Dir = ?config(static_dir, Config),
  45. ct_helper:delete_static_dir(Dir),
  46. application:stop(ssl),
  47. application:stop(public_key),
  48. application:stop(asn1),
  49. application:stop(cowboy),
  50. application:stop(ranch),
  51. application:stop(crypto),
  52. ok.
  53. init_per_group(Name, Config) ->
  54. {_, Cert, Key} = ct_helper:make_certs(),
  55. Opts = [{cert, Cert}, {key, Key}],
  56. {ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
  57. {env, [{dispatch, init_dispatch(Config)}]}
  58. ]),
  59. Port = ranch:get_port(Name),
  60. [{port, Port}|Config].
  61. end_per_group(Name, _) ->
  62. cowboy:stop_listener(Name),
  63. ok.
  64. %% Dispatch configuration.
  65. init_dispatch(Config) ->
  66. cowboy_router:compile([
  67. {"localhost", [
  68. {"/static/[...]", cowboy_static,
  69. [{directory, ?config(static_dir, Config)},
  70. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  71. {"/chunked", http_chunked, []},
  72. {"/", http_handler, []}
  73. ]}
  74. ]).
  75. %% Convenience functions.
  76. quick_get(Host, Path, ExpectedFlags, Config) ->
  77. {_, Port} = lists:keyfind(port, 1, Config),
  78. {ok, Socket} = ssl:connect("localhost", Port, [
  79. binary, {active, false},
  80. {client_preferred_next_protocols, client, [<<"spdy/3">>]}
  81. ]),
  82. {Zdef, Zinf} = zlib_init(),
  83. ReqHeaders = headers_encode(Zdef, [
  84. {<<":method">>, <<"GET">>},
  85. {<<":path">>, list_to_binary(Path)},
  86. {<<":version">>, <<"HTTP/1.1">>},
  87. {<<":host">>, list_to_binary(Host)},
  88. {<<":scheme">>, <<"https">>}
  89. ]),
  90. ReqLength = 10 + byte_size(ReqHeaders),
  91. StreamID = 1,
  92. ok = ssl:send(Socket, << 1:1, 3:15, 1:16, 0:8, ReqLength:24,
  93. 0:1, StreamID:31, 0:1, 0:31, 0:3, 0:5, 0:8, ReqHeaders/binary >>),
  94. {ok, Packet} = ssl:recv(Socket, 0, 1000),
  95. << 1:1, 3:15, 2:16, Flags:8, RespLength:24,
  96. _:1, StreamID:31, RespHeaders/bits >> = Packet,
  97. Flags = ExpectedFlags,
  98. RespLength = 4 + byte_size(RespHeaders),
  99. [<< NbHeaders:32, Rest/bits >>] = try
  100. zlib:inflate(Zinf, RespHeaders)
  101. catch _:_ ->
  102. ok = zlib:inflateSetDictionary(Zinf, ?ZDICT),
  103. zlib:inflate(Zinf, <<>>)
  104. end,
  105. RespHeaders2 = headers_decode(Zinf, Rest, []),
  106. NbHeaders = length(RespHeaders2),
  107. {_, << Status:3/binary, _/bits >>}
  108. = lists:keyfind(<<":status">>, 1, RespHeaders2),
  109. StatusCode = list_to_integer(binary_to_list(Status)),
  110. ok = ssl:close(Socket),
  111. zlib_terminate(Zdef, Zinf),
  112. {StatusCode, RespHeaders2}.
  113. zlib_init() ->
  114. Zdef = zlib:open(),
  115. ok = zlib:deflateInit(Zdef),
  116. _ = zlib:deflateSetDictionary(Zdef, ?ZDICT),
  117. Zinf = zlib:open(),
  118. ok = zlib:inflateInit(Zinf),
  119. {Zdef, Zinf}.
  120. zlib_terminate(Zdef, Zinf) ->
  121. zlib:close(Zdef),
  122. zlib:close(Zinf).
  123. headers_encode(Zdef, Headers) ->
  124. NbHeaders = length(Headers),
  125. Headers2 = << << (begin
  126. SizeN = byte_size(N),
  127. SizeV = byte_size(V),
  128. << SizeN:32, N/binary, SizeV:32, V/binary >>
  129. end)/binary >> || {N, V} <- Headers >>,
  130. Headers3 = << NbHeaders:32, Headers2/binary >>,
  131. iolist_to_binary(zlib:deflate(Zdef, Headers3, full)).
  132. headers_decode(_, <<>>, Acc) ->
  133. lists:reverse(Acc);
  134. headers_decode(Zinf, << SizeN:32, Rest/bits >>, Acc) ->
  135. << Name:SizeN/binary, SizeV:32, Rest2/bits >> = Rest,
  136. << Value:SizeV/binary, Rest3/bits >> = Rest2,
  137. headers_decode(Zinf, Rest3, [{Name, Value}|Acc]).
  138. %% Tests.
  139. check_status(Config) ->
  140. Tests = [
  141. {200, nofin, "localhost", "/"},
  142. {200, nofin, "localhost", "/chunked"},
  143. {200, nofin, "localhost", "/static/style.css"},
  144. {400, fin, "bad-host", "/"},
  145. {400, fin, "localhost", "bad-path"},
  146. {404, fin, "localhost", "/this/path/does/not/exist"}
  147. ],
  148. _ = [{Status, Fin, Host, Path} = begin
  149. RespFlags = case Fin of fin -> 1; nofin -> 0 end,
  150. {Ret, _} = quick_get(Host, Path, RespFlags, Config),
  151. {Ret, Fin, Host, Path}
  152. end || {Status, Fin, Host, Path} <- Tests].