acceptor_SUITE.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. %% Copyright (c) 2011-2012, 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(acceptor_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. %% ct.
  17. -export([all/0]).
  18. -export([groups/0]).
  19. -export([init_per_suite/1]).
  20. -export([end_per_suite/1]).
  21. -export([init_per_group/2]).
  22. -export([end_per_group/2]).
  23. %% ssl.
  24. -export([ssl_echo/1]).
  25. %% tcp.
  26. -export([tcp_echo/1]).
  27. %% ct.
  28. all() ->
  29. [{group, tcp}, {group, ssl}].
  30. groups() ->
  31. [{tcp, [
  32. tcp_echo
  33. ]}, {ssl, [
  34. ssl_echo
  35. ]}].
  36. init_per_suite(Config) ->
  37. application:start(ranch),
  38. Config.
  39. end_per_suite(_) ->
  40. application:stop(ranch),
  41. ok.
  42. init_per_group(ssl, Config) ->
  43. application:start(crypto),
  44. application:start(public_key),
  45. application:start(ssl),
  46. Config;
  47. init_per_group(_, Config) ->
  48. Config.
  49. end_per_group(ssl, _) ->
  50. application:stop(ssl),
  51. application:stop(public_key),
  52. application:stop(crypto),
  53. ok;
  54. end_per_group(_, _) ->
  55. ok.
  56. %% ssl.
  57. ssl_echo(Config) ->
  58. {ok, _} = ranch:start_listener(ssl_echo, 1,
  59. ranch_ssl, [{port, 0},
  60. {certfile, ?config(data_dir, Config) ++ "cert.pem"}],
  61. echo_protocol, []),
  62. Port = ranch:get_port(ssl_echo),
  63. {ok, Socket} = ssl:connect("localhost", Port,
  64. [binary, {active, false}, {packet, raw},
  65. {certfile, ?config(data_dir, Config) ++ "cert.pem"}]),
  66. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  67. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  68. ok = ranch:stop_listener(ssl_echo),
  69. {error, closed} = ssl:recv(Socket, 0, 1000),
  70. %% Make sure the listener stopped.
  71. {'EXIT', _} = begin catch ranch:get_port(ssl_echo) end,
  72. ok.
  73. %% tcp.
  74. tcp_echo(_) ->
  75. {ok, _} = ranch:start_listener(tcp_echo, 1,
  76. ranch_tcp, [{port, 0}], echo_protocol, []),
  77. Port = ranch:get_port(tcp_echo),
  78. {ok, Socket} = gen_tcp:connect("localhost", Port,
  79. [binary, {active, false}, {packet, raw}]),
  80. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  81. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  82. ok = ranch:stop_listener(tcp_echo),
  83. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  84. %% Make sure the listener stopped.
  85. {'EXIT', _} = begin catch ranch:get_port(tcp_echo) end,
  86. ok.