spdy_SUITE.erl 4.6 KB

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