1: %%% eturnal STUN/TURN server.
    2: %%%
    3: %%% Copyright (c) 2020-2023 Holger Weiss <holger@zedat.fu-berlin.de>.
    4: %%% Copyright (c) 2020-2023 ProcessOne, SARL.
    5: %%% All rights reserved.
    6: %%%
    7: %%% Licensed under the Apache License, Version 2.0 (the "License");
    8: %%% you may not use this file except in compliance with the License.
    9: %%% You may obtain a copy of the License at
   10: %%%
   11: %%%     http://www.apache.org/licenses/LICENSE-2.0
   12: %%%
   13: %%% Unless required by applicable law or agreed to in writing, software
   14: %%% distributed under the License is distributed on an "AS IS" BASIS,
   15: %%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16: %%% See the License for the specific language governing permissions and
   17: %%% limitations under the License.
   18: 
   19: -module(eturnal_SUITE).
   20: -compile(export_all).
   21: -include_lib("common_test/include/ct.hrl").
   22: 
   23: -type info() :: ct_suite:ct_info().
   24: -type config() :: ct_suite:ct_config().
   25: -type test_def() :: ct_suite:ct_test_def().
   26: -type test_name() :: ct_suite:ct_testname().
   27: -type group_def() :: ct_suite:ct_group_def().
   28: -type group_name() :: ct_suite:ct_groupname().
   29: 
   30: %% API.
   31: 
   32: -spec suite() -> [info()].
   33: suite() ->
   34:     [{require, {server, [address, udp_port, tcp_port, tls_port, auto_port]}},
   35:      {timetrap, {seconds, 120}}].
   36: 
   37: -spec init_per_suite(config()) -> config().
   38: init_per_suite(Config) ->
   39:     Server = ct:get_config(server),
   40:     Address = proplists:get_value(address, Server),
   41:     UdpPort = proplists:get_value(udp_port, Server),
   42:     TcpPort = proplists:get_value(tcp_port, Server),
   43:     TlsPort = proplists:get_value(tls_port, Server),
   44:     AutoPort = proplists:get_value(auto_port, Server),
   45:     [{address, Address},
   46:      {udp_port, UdpPort},
   47:      {tcp_port, TcpPort},
   48:      {tls_port, TlsPort},
   49:      {auto_port, AutoPort} | Config].
   50: 
   51: -spec end_per_suite(config()) -> ok.
   52: end_per_suite(_Config) ->
   53:     ok.
   54: 
   55: -spec init_per_group(group_name(), config()) -> config().
   56: init_per_group(_GroupName, Config) ->
   57:     Config.
   58: 
   59: -spec end_per_group(group_name(), config()) -> ok.
   60: end_per_group(_GroupName, _Config) ->
   61:     ok.
   62: 
   63: -spec init_per_testcase(test_name(), config()) -> config().
   64: -ifdef(old_inet_backend).
   65: init_per_testcase(start_eturnal, Config) ->
   66:     set_eturnal_env("eturnal-old-otp.yml", Config);
   67: init_per_testcase(stun_tcp_auto, _Config) ->
   68:     {skip, otp_version_unsupported};
   69: init_per_testcase(stun_tls_auto, _Config) ->
   70:     {skip, otp_version_unsupported};
   71: init_per_testcase(_TestCase, Config) ->
   72:     Config.
   73: -else.
   74: init_per_testcase(start_eturnal, Config) ->
   75:     set_eturnal_env("eturnal-new-otp.yml", Config);
   76: init_per_testcase(_TestCase, Config) ->
   77:     Config.
   78: -endif.
   79: 
   80: -spec end_per_testcase(test_name(), config()) -> ok.
   81: end_per_testcase(_TestCase, _Config) ->
   82:     ok.
   83: 
   84: -spec groups() -> [group_def()].
   85: groups() ->
   86:     [].
   87: 
   88: -spec all() -> [test_def()] | {skip, term()}.
   89: all() ->
   90:     [start_eturnal,
   91:      check_info,
   92:      check_all_sessions,
   93:      check_user_sessions,
   94:      check_disconnect,
   95:      check_credentials,
   96:      check_loglevel,
   97:      check_version,
   98:      reload,
   99:      connect_tcp,
  100:      connect_tls,
  101:      turn_udp,
  102:      stun_udp,
  103:      stun_tcp,
  104:      stun_tls,
  105:      stun_tcp_auto,
  106:      stun_tls_auto,
  107:      stop_eturnal].
  108: 
  109: -spec start_eturnal(config()) -> any().
  110: start_eturnal(_Config) ->
  111:     ct:pal("Starting up eturnal"),
  112:     ok = eturnal:start().
  113: 
  114: -spec check_info(config()) -> any().
  115: check_info(_Config) ->
  116:     ct:pal("Checking eturnal statistics"),
  117:     {ok, Info} = eturnal_ctl:get_info(),
  118:     ct:pal("Got eturnal statistics: ~p", [Info]),
  119:     true = is_list(Info).
  120: 
  121: -spec check_all_sessions(config()) -> any().
  122: check_all_sessions(_Config) ->
  123:     ct:pal("Checking active TURN sessions"),
  124:     {ok, Sessions} = eturnal_ctl:get_sessions(),
  125:     ct:pal("Got active TURN sessions: ~p", [Sessions]),
  126:     true = is_list(Sessions).
  127: 
  128: -spec check_user_sessions(config()) -> any().
  129: check_user_sessions(_Config) ->
  130:     ct:pal("Checking active TURN sessions of user"),
  131:     {ok, Sessions} = eturnal_ctl:get_sessions("alice"),
  132:     ct:pal("Got active TURN sessions of user: ~p", [Sessions]),
  133:     true = is_list(Sessions),
  134:     ct:pal("Checking active TURN sessions of invalid user"),
  135:     {error, Reason} = eturnal_ctl:disconnect(alice),
  136:     ct:pal("Got an error, as expected: ~p", [Reason]).
  137: 
  138: -spec check_disconnect(config()) -> any().
  139: check_disconnect(_Config) ->
  140:     ct:pal("Checking disconnection of TURN user"),
  141:     {ok, Msg} = eturnal_ctl:disconnect("alice"),
  142:     ct:pal("Got result for disconnecting TURN user: ~p", [Msg]),
  143:     true = is_list(Msg),
  144:     ct:pal("Checking disconnection of invalid TURN user"),
  145:     {error, Reason} = eturnal_ctl:disconnect(alice),
  146:     ct:pal("Got an error, as expected: ~p", [Reason]).
  147: 
  148: -spec check_credentials(config()) -> any().
  149: check_credentials(_Config) ->
  150:     Timestamp = "2009-10-30 11:00:00Z",
  151:     Username = 1256900400,
  152:     Password = "uEKlpcME7MNMMVRV8rUFPCTIFEs=",
  153:     ct:pal("Checking credentials valid until ~s", [Timestamp]),
  154:     {ok, Credentials} = eturnal_ctl:get_credentials(Timestamp, []),
  155:     {ok, [Username, Password], []} =
  156:         io_lib:fread("Username: ~u~~nPassword: ~s", Credentials),
  157:     lists:foreach(
  158:       fun(Lifetime) ->
  159:               ct:pal("Checking credentials valid for ~s", [Lifetime]),
  160:               {ok, Creds} = eturnal_ctl:get_credentials(Lifetime, "alice"),
  161:               {ok, [Time, Pass], []} =
  162:                   io_lib:fread("Username: ~u:alice~~nPassword: ~s", Creds),
  163:               {ok, Pass} =
  164:                   eturnal_ctl:get_password(integer_to_list(Time) ++ ":alice"),
  165:               true = erlang:system_time(second) + 86400 - Time < 5
  166:       end, ["86400", "86400s", "1440m", "24h", "1d"]),
  167:     ct:pal("Checking invalid expiry"),
  168:     lists:foreach(
  169:       fun(Invalid) ->
  170:               {error, _Reason1} = eturnal_ctl:get_password(Invalid),
  171:               {error, _Reason2} = eturnal_ctl:get_credentials(Invalid, [])
  172:       end, ["Invalid", invalid, [invalid]]),
  173:     ct:pal("Checking invalid suffix"),
  174:     {error, _Reason} = eturnal_ctl:get_credentials(Username, invalid),
  175:     ct:pal("Checking static credentials"),
  176:     {ok, "l0vesBob"} = eturnal_ctl:get_password("alice").
  177: 
  178: -spec check_loglevel(config()) -> any().
  179: check_loglevel(_Config) ->
  180:     Level = "debug",
  181:     ct:pal("Setting log level to ~s", [Level]),
  182:     ok = eturnal_ctl:set_loglevel(list_to_atom(Level)),
  183:     ct:pal("Checking whether log level is set to ~s", [Level]),
  184:     {ok, Level} = eturnal_ctl:get_loglevel(),
  185:     ct:pal("Setting invalid log level"),
  186:     {error, _Reason} = eturnal_ctl:set_loglevel(invalid),
  187:     ct:pal("Checking whether log level is still set to ~s", [Level]),
  188:     {ok, Level} = eturnal_ctl:get_loglevel().
  189: 
  190: -spec check_version(config()) -> any().
  191: check_version(_Config) ->
  192:     ct:pal("Checking eturnal version"),
  193:     {ok, Version} = eturnal_ctl:get_version(),
  194:     ct:pal("Got eturnal version: ~p", [Version]),
  195:     match = re:run(Version, "^[0-9]+\\.[0-9]+\\.[0-9](\\+[0-9]+)?",
  196:                    [{capture, none}]).
  197: 
  198: -spec reload(config()) -> any().
  199: reload(_Config) ->
  200:     CertFile = <<"run/cert.pem">>,
  201:     ct:pal("Deleting TLS certificate"),
  202:     ok = file:delete(CertFile),
  203:     ct:pal("Reloading eturnal"),
  204:     ok = eturnal_ctl:reload(),
  205:     ct:pal("Checking whether new TLS certificate was created"),
  206:     {ok, _} = file:read_file_info(CertFile),
  207:     ct:pal("Reloading eturnal without changes"),
  208:     ok = eturnal_ctl:reload().
  209: 
  210: -spec connect_tcp(config()) -> any().
  211: connect_tcp(Config) ->
  212:     Addr = ?config(address, Config),
  213:     Port = ?config(tcp_port, Config),
  214:     ct:pal("Connecting to 127.0.0.1:~B (TCP)", [Port]),
  215:     {ok, Sock} = gen_tcp:connect(Addr, Port, []),
  216:     ok = gen_tcp:close(Sock).
  217: 
  218: -spec connect_tls(config()) -> any().
  219: connect_tls(Config) ->
  220:     Addr = ?config(address, Config),
  221:     Port = ?config(tls_port, Config),
  222:     ct:pal("Connecting to 127.0.0.1:~B (TLS)", [Port]),
  223:     {ok, TCPSock} = gen_tcp:connect(Addr, Port, []),
  224:     {ok, TLSSock} = fast_tls:tcp_to_tls(TCPSock, [connect]),
  225:     ok = fast_tls:close(TLSSock).
  226: 
  227: -spec turn_udp(config()) -> any().
  228: turn_udp(Config) ->
  229:     Addr = ?config(address, Config),
  230:     Port = ?config(udp_port, Config),
  231:     Username1 = <<"alice">>,
  232:     Password1 = <<"l0vesBob">>,
  233:     Username2 = <<"2145913200">>,
  234:     Password2 = <<"cLwpKS2/9bWHf+agUImD47PIXNE=">>,
  235:     Realm = <<"eturnal.net">>,
  236:     ct:pal("Allocating TURN relay on 127.0.0.1:~B (UDP)", [Port]),
  237:     ok = stun_test:allocate_udp(Addr, Port, Username1, Realm, Password1),
  238:     ok = stun_test:allocate_udp(Addr, Port, Username2, Realm, Password2).
  239: 
  240: -spec stun_udp(config()) -> any().
  241: stun_udp(Config) ->
  242:     Addr = ?config(address, Config),
  243:     Port = ?config(udp_port, Config),
  244:     ct:pal("Performing STUN query against 127.0.0.1:~B (UDP)", [Port]),
  245:     Result = stun_test:bind_udp(Addr, Port),
  246:     ct:pal("Got query result: ~p", [Result]),
  247:     true = is_tuple(Result),
  248:     true = element(1, Result) =:= stun.
  249: 
  250: -spec stun_tcp(config()) -> any().
  251: stun_tcp(Config) ->
  252:     Addr = ?config(address, Config),
  253:     Port = ?config(tcp_port, Config),
  254:     ct:pal("Performing STUN query against 127.0.0.1:~B (TCP)", [Port]),
  255:     Result = stun_test:bind_tcp(Addr, Port),
  256:     ct:pal("Got query result: ~p", [Result]),
  257:     true = is_tuple(Result),
  258:     true = element(1, Result) =:= stun.
  259: 
  260: -spec stun_tls(config()) -> any().
  261: stun_tls(Config) ->
  262:     Addr = ?config(address, Config),
  263:     Port = ?config(tls_port, Config),
  264:     ct:pal("Performing STUN query against 127.0.0.1:~B (TLS)", [Port]),
  265:     Result = stun_test:bind_tls(Addr, Port),
  266:     ct:pal("Got query result: ~p", [Result]),
  267:     true = is_tuple(Result),
  268:     true = element(1, Result) =:= stun.
  269: 
  270: -spec stun_tcp_auto(config()) -> any().
  271: stun_tcp_auto(Config) ->
  272:     Addr = ?config(address, Config),
  273:     Port = ?config(auto_port, Config),
  274:     ct:pal("Performing STUN query against 127.0.0.1:~B (TCP)", [Port]),
  275:     Result = stun_test:bind_tcp(Addr, Port),
  276:     ct:pal("Got query result: ~p", [Result]),
  277:     true = is_tuple(Result),
  278:     true = element(1, Result) =:= stun.
  279: 
  280: -spec stun_tls_auto(config()) -> any().
  281: stun_tls_auto(Config) ->
  282:     Addr = ?config(address, Config),
  283:     Port = ?config(auto_port, Config),
  284:     ct:pal("Performing STUN query against 127.0.0.1:~B (TLS)", [Port]),
  285:     Result = stun_test:bind_tls(Addr, Port),
  286:     ct:pal("Got query result: ~p", [Result]),
  287:     true = is_tuple(Result),
  288:     true = element(1, Result) =:= stun.
  289: 
  290: -spec stop_eturnal(config()) -> any().
  291: stop_eturnal(_Config) ->
  292:     ct:pal("Stopping eturnal"),
  293:     ok = eturnal:stop().
  294: 
  295: %% Internal functions.
  296: 
  297: -spec set_eturnal_env(file:filename_all(), config()) -> config().
  298: set_eturnal_env(ConfName, Config) ->
  299:     DataDir = ?config(data_dir, Config),
  300:     ConfFile = filename:join(DataDir, ConfName),
  301:     ok = application:set_env(conf, file, ConfFile),
  302:     ok = application:set_env(conf, on_fail, stop),
  303:     ok = application:set_env(eturnal, on_fail, exit),
  304:     Config.