From a11cf8f53dba5620b7c725bae1d02dcba9f20472 Mon Sep 17 00:00:00 2001 From: Andreas Schultz Date: Wed, 17 Apr 2024 12:55:21 +0200 Subject: [PATCH] [ct] prepare for IPv6 support Fully prepare client suite for full IPv6 support and add some minor tweaks to the others to move them closer to IPv6 support. --- test/eradius_client_SUITE.erl | 202 +++++++++++++++++++++------------ test/eradius_config_SUITE.erl | 14 +-- test/eradius_logtest.erl | 24 ++-- test/eradius_metrics_SUITE.erl | 36 +++--- test/eradius_proxy_SUITE.erl | 30 ++--- test/eradius_test_handler.erl | 61 +++++----- test/eradius_test_lib.erl | 53 +++++++++ 7 files changed, 268 insertions(+), 152 deletions(-) create mode 100644 test/eradius_test_lib.erl diff --git a/test/eradius_client_SUITE.erl b/test/eradius_client_SUITE.erl index b30d4e5..a7a3438 100644 --- a/test/eradius_client_SUITE.erl +++ b/test/eradius_client_SUITE.erl @@ -1,65 +1,81 @@ -%% Copyright (c) 2010-2017 by Travelping GmbH - -%% Permission is hereby granted, free of charge, to any person obtaining a -%% copy of this software and associated documentation files (the "Software"), -%% to deal in the Software without restriction, including without limitation -%% the rights to use, copy, modify, merge, publish, distribute, sublicense, -%% and/or sell copies of the Software, and to permit persons to whom the -%% Software is furnished to do so, subject to the following conditions: - -%% The above copyright notice and this permission notice shall be included in -%% all copies or substantial portions of the Software. - -%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -%% DEALINGS IN THE SOFTWARE. - +%% Copyright (c) 2010-2017, Travelping GmbH +%% +%% SPDX-License-Identifier: MIT +%% -module(eradius_client_SUITE). --compile(export_all). + +-compile([export_all, nowarn_export_all]). + +-behaviour(ct_suite). -include("test/eradius_test.hrl"). +%%%=================================================================== +%%% Defines +%%%=================================================================== + -define(HUT_SOCKET, eradius_client_socket). --define(BAD_SERVER_IP, {eradius_test_handler:localhost(ip), 1820, "secret"}). +-define(BAD_SERVER_IP(Family), + {eradius_test_lib:localhost(Family, ip), 1820, "secret"}). -define(BAD_SERVER_INITIAL_RETRIES, 3). --define(BAD_SERVER_TUPLE_INITIAL, {{eradius_test_handler:localhost(tuple), 1820}, - ?BAD_SERVER_INITIAL_RETRIES, - ?BAD_SERVER_INITIAL_RETRIES}). --define(BAD_SERVER_TUPLE, {{eradius_test_handler:localhost(tuple), 1820}, - ?BAD_SERVER_INITIAL_RETRIES - 1, - ?BAD_SERVER_INITIAL_RETRIES}). --define(BAD_SERVER_IP_ETS_KEY, {eradius_test_handler:localhost(tuple), 1820}). +-define(BAD_SERVER_TUPLE_INITIAL(Family), + {{eradius_test_lib:localhost(Family, tuple), 1820}, + ?BAD_SERVER_INITIAL_RETRIES, + ?BAD_SERVER_INITIAL_RETRIES}). +-define(BAD_SERVER_TUPLE(Family), + {{eradius_test_lib:localhost(Family, tuple), 1820}, + ?BAD_SERVER_INITIAL_RETRIES - 1, + ?BAD_SERVER_INITIAL_RETRIES}). +-define(BAD_SERVER_IP_ETS_KEY(Family), + {eradius_test_lib:localhost(Family, tuple), 1820}). -define(GOOD_SERVER_INITIAL_RETRIES, 3). --define(GOOD_SERVER_TUPLE, {{eradius_test_handler:localhost(tuple), 1812}, - ?GOOD_SERVER_INITIAL_RETRIES, - ?GOOD_SERVER_INITIAL_RETRIES}). --define(GOOD_SERVER_2_TUPLE, {{{127, 0, 0, 2}, 1813}, - ?GOOD_SERVER_INITIAL_RETRIES, - ?GOOD_SERVER_INITIAL_RETRIES}). - --define(RADIUS_SERVERS, [?GOOD_SERVER_TUPLE, - ?BAD_SERVER_TUPLE_INITIAL, - ?GOOD_SERVER_2_TUPLE]). +-define(GOOD_SERVER_TUPLE(Family), + {{eradius_test_lib:localhost(Family, tuple), 1812}, + ?GOOD_SERVER_INITIAL_RETRIES, + ?GOOD_SERVER_INITIAL_RETRIES}). +-define(GOOD_SERVER_2_TUPLE(Family), + {{eradius_test_lib:badhost(Family), 1813}, + ?GOOD_SERVER_INITIAL_RETRIES, + ?GOOD_SERVER_INITIAL_RETRIES}). + +-define(RADIUS_SERVERS(Family), + [?GOOD_SERVER_TUPLE(Family), + ?BAD_SERVER_TUPLE_INITIAL(Family), + ?GOOD_SERVER_2_TUPLE(Family)]). + +%%%=================================================================== +%%% Setup +%%%=================================================================== -spec all() -> [ct_suite:ct_test_def(), ...]. -all() -> [ - send_request, - wanna_send, - reconf_address, - wanna_send, - reconf_ports_30, - wanna_send, - reconf_ports_10, - wanna_send, - send_request_failover, - check_upstream_servers - ]. +all() -> + [{group, ipv4}, + {group, ipv4_mapped_ipv6}, + {group, ipv6}]. + +common() -> + [send_request, + wanna_send, + reconf_address, + wanna_send, + reconf_ports_30, + wanna_send, + reconf_ports_10, + wanna_send, + send_request_failover, + check_upstream_servers + ]. + +-spec groups() -> [ct_suite:ct_group_def(), ...]. +groups() -> + SocketGroups = [{group, inet}, {group, socket}], + [{inet, [], common()}, + {socket, [], common()}, + {ipv4, [], SocketGroups}, + {ipv4_mapped_ipv6, [], SocketGroups}, + {ipv6, [], SocketGroups}]. init_per_suite(Config) -> {ok, _} = application:ensure_all_started(eradius), @@ -70,17 +86,48 @@ end_per_suite(_Config) -> application:stop(eradius), ok. +init_per_group(inet, Config) -> + [{inet_backend, inet} | Config]; +init_per_group(socket, Config) -> + [{inet_backend, socket} | Config]; +init_per_group(ipv6 = Group, Config) -> + {skip, "no IPv6 server support (yet)"}; + %% case eradius_test_lib:has_ipv6_test_config() of + %% true -> + %% [{family, Group} | Config]; + %% _ -> + %% {skip, "IPv6 test IPs not configured"} + %% end; +init_per_group(ipv4_mapped_ipv6 = Group, Config) -> + case eradius_test_lib:has_ipv6_test_config() of + true -> + [{family, Group} | Config]; + _ -> + {skip, "IPv6 test IPs not configured"} + end; +init_per_group(ipv4 = Group, Config) -> + [{family, Group} | Config]. + +end_per_group(_Group, _Config) -> + application:stop(eradius), + ok. + +start_handler(Config) -> + Backend = proplists:get_value(inet_backend, Config, inet), + Family = proplists:get_value(family, Config), + eradius_test_handler:start(Backend, Family). + init_per_testcase(send_request, Config) -> application:stop(eradius), - eradius_test_handler:start(), + start_handler(Config), Config; init_per_testcase(send_request_failover, Config) -> application:stop(eradius), - eradius_test_handler:start(), + start_handler(Config), Config; init_per_testcase(check_upstream_servers, Config) -> application:stop(eradius), - eradius_test_handler:start(), + start_handler(Config), Config; init_per_testcase(_Test, Config) -> Config. @@ -157,6 +204,7 @@ check(#{sockets := OS, no_ports := _OP, idcounters := _OC, socket_id := {_, OA}} test(PA == NA, "Adress not configured") and case NA of OA -> + ct:pal("NP: ~p, NC: ~p", [NP, NC]), {_, Rest} = split(NP, array:to_list(OS)), test(P == NP,"Ports not configured") and test(maps:fold( fun(_Peer, {NextPortIdx, _NextReqId}, Akk) -> @@ -173,11 +221,16 @@ check(#{sockets := OS, no_ports := _OP, idcounters := _OC, socket_id := {_, OA}} %% TESTS -send_request(_Config) -> - ?equal(accept, eradius_test_handler:send_request(eradius_test_handler:localhost(tuple))), - ?equal(accept, eradius_test_handler:send_request(eradius_test_handler:localhost(ip))), - ?equal(accept, eradius_test_handler:send_request(eradius_test_handler:localhost(string))), - ?equal(accept, eradius_test_handler:send_request(eradius_test_handler:localhost(binary))), +send_request(Config) -> + Family = proplists:get_value(family, Config), + ?equal(accept, + eradius_test_handler:send_request(eradius_test_lib:localhost(Family, tuple))), + ?equal(accept, + eradius_test_handler:send_request(eradius_test_lib:localhost(Family, ip))), + ?equal(accept, + eradius_test_handler:send_request(eradius_test_lib:localhost(Family, string))), + ?equal(accept, + eradius_test_handler:send_request(eradius_test_lib:localhost(Family, binary))), ok. send(FUN, Ports, Address) -> @@ -198,27 +251,36 @@ wanna_send(_Config) -> %% socket shutdown is done asynchronous, the tests need to wait a bit for it to finish. reconf_address(_Config) -> - FUN = fun() -> eradius_client_mngr:reconfigure(), timer:sleep(100) end, - application:set_env(eradius, client_ip, "7.13.23.42"), + FUN = fun() -> + eradius_client_mngr:reconfigure(#{ip => "7.13.23.42"}), + timer:sleep(100) + end, send(FUN, null, "7.13.23.42"). reconf_ports_30(_Config) -> - FUN = fun() -> eradius_client_mngr:reconfigure(), timer:sleep(100) end, - application:set_env(eradius, client_ports, 30), + FUN = fun() -> + eradius_client_mngr:reconfigure(#{no_ports => 30}), + timer:sleep(100) + end, send(FUN, 30, null). reconf_ports_10(_Config) -> - FUN = fun() -> eradius_client_mngr:reconfigure(), timer:sleep(100) end, - application:set_env(eradius, client_ports, 10), + FUN = fun() -> + eradius_client_mngr:reconfigure(#{no_ports => 10}), + timer:sleep(100) + end, send(FUN, 10, null). -send_request_failover(_Config) -> - ?equal(accept, eradius_test_handler:send_request_failover(?BAD_SERVER_IP)), +send_request_failover(Config) -> + Family = proplists:get_value(family, Config), + ?equal(accept, eradius_test_handler:send_request_failover(?BAD_SERVER_IP(Family))), {ok, Timeout} = application:get_env(eradius, unreachable_timeout), timer:sleep(Timeout * 1000), - ?equal([?BAD_SERVER_TUPLE], eradius_client_mngr:servers(?BAD_SERVER_IP_ETS_KEY)), + ?equal([?BAD_SERVER_TUPLE(Family)], + eradius_client_mngr:servers(?BAD_SERVER_IP_ETS_KEY(Family))), ok. -check_upstream_servers(_Config) -> - ?equal(?RADIUS_SERVERS, eradius_client_mngr:servers()), +check_upstream_servers(Config) -> + Family = proplists:get_value(family, Config), + ?equal(lists:keysort(1, ?RADIUS_SERVERS(Family)), eradius_client_mngr:servers()), ok. diff --git a/test/eradius_config_SUITE.erl b/test/eradius_config_SUITE.erl index 2f35e1e..07dd2cb 100644 --- a/test/eradius_config_SUITE.erl +++ b/test/eradius_config_SUITE.erl @@ -40,7 +40,7 @@ config_1(_Config) -> Conf = [{session_nodes, ['node1@host1', 'node2@host2']}, {radius_callback, ?MODULE}, {servers, [ - {root, {eradius_test_handler:localhost(ip), [1812, 1813]}} + {root, {eradius_test_lib:localhost(ip), [1812, 1813]}} ]}, {root, [ { {"NAS1", [arg1, arg2]}, @@ -49,7 +49,7 @@ config_1(_Config) -> [{{10, 18, 14, 3}, <<"secret2">>, [{nas_id, <<"name">>}]}]} ]}], ok = apply_conf(Conf), - LocalHost = eradius_test_handler:localhost(tuple), + LocalHost = eradius_test_lib:localhost(tuple), ?match({ok, {?MODULE,[arg1,arg2]}, #nas_prop{ server_ip = LocalHost, @@ -83,7 +83,7 @@ config_2(_Config) -> ] }, {servers, [ - {root, {eradius_test_handler:localhost(ip), [1812, 1813]}} + {root, {eradius_test_lib:localhost(ip), [1812, 1813]}} ]}, {root, [ { {handler1, "NAS1", [arg1, arg2]}, @@ -93,7 +93,7 @@ config_2(_Config) -> [ {"10.18.14.2", <<"secret2">>, [{group, "NodeGroup2"}]} ] } ]}], ok = apply_conf(Conf), - LocalHost = eradius_test_handler:localhost(tuple), + LocalHost = eradius_test_lib:localhost(tuple), ?match({ok, {handler1,[arg1,arg2]}, #nas_prop{ server_ip = LocalHost, @@ -146,7 +146,7 @@ test_validate_server(_Config) -> ok. config_nas_removing(_Config) -> - Conf = [{servers, [ {root, {eradius_test_handler:localhost(ip), [1812, 1813]}} ]}, + Conf = [{servers, [ {root, {eradius_test_lib:localhost(ip), [1812, 1813]}} ]}, {root, [ ]}], ok = apply_conf(Conf), ?match([], ets:tab2list(eradius_nas_tab)), @@ -159,14 +159,14 @@ config_with_ranges(_Config) -> ] }, {servers, [ - {root, {eradius_test_handler:localhost(ip), [1812, 1813]}} + {root, {eradius_test_lib:localhost(ip), [1812, 1813]}} ]}, {root, [ { {handler, "NAS", []}, [ {"10.18.14.2/30", <<"secret2">>, [{group, "NodeGroup"}]} ] } ]}], ok = apply_conf(Conf), - LocalHost = eradius_test_handler:localhost(tuple), + LocalHost = eradius_test_lib:localhost(tuple), ?match({ok, {handler,[]}, #nas_prop{ server_ip = LocalHost, diff --git a/test/eradius_logtest.erl b/test/eradius_logtest.erl index aa410aa..125422a 100644 --- a/test/eradius_logtest.erl +++ b/test/eradius_logtest.erl @@ -41,25 +41,25 @@ start() -> application:load(eradius), - ProxyConfig = [{default_route, {eradius_test_handler:localhost(tuple), 1813, ?SECRET}}, + ProxyConfig = [{default_route, {eradius_test_lib:localhost(tuple), 1813, ?SECRET}}, {options, [{type, realm}, {strip, true}, {separator, "@"}]}, - {routes, [{"test", {eradius_test_handler:localhost(tuple), 1815, ?SECRET3}} + {routes, [{"test", {eradius_test_lib:localhost(tuple), 1815, ?SECRET3}} ]} ], Config = [{radius_callback, eradius_logtest}, - {servers, [{root, {eradius_test_handler:localhost(ip), [1812, 1813]}}, - {test, {eradius_test_handler:localhost(ip), [1815]}}, - {proxy, {eradius_test_handler:localhost(ip), [11812, 11813]}} + {servers, [{root, {eradius_test_lib:localhost(ip), [1812, 1813]}}, + {test, {eradius_test_lib:localhost(ip), [1815]}}, + {proxy, {eradius_test_lib:localhost(ip), [11812, 11813]}} ]}, {session_nodes, [node()]}, {root, [ { {eradius_logtest, "root", [] }, [{"127.0.0.1/24", ?SECRET, [{nas_id, <<"Test_Nas_Id">>}]}] } ]}, {test, [ - { {eradius_logtest, "test", [] }, [{eradius_test_handler:localhost(ip), ?SECRET3, [{nas_id, <<"Test_Nas_Id_test">>}]}] } + { {eradius_logtest, "test", [] }, [{eradius_test_lib:localhost(ip), ?SECRET3, [{nas_id, <<"Test_Nas_Id_test">>}]}] } ]}, {proxy, [ - { {eradius_proxy, "proxy", ProxyConfig }, [{eradius_test_handler:localhost(ip), ?SECRET2, [{nas_id, <<"Test_Nas_proxy">>}]}] } + { {eradius_proxy, "proxy", ProxyConfig }, [{eradius_test_lib:localhost(ip), ?SECRET2, [{nas_id, <<"Test_Nas_proxy">>}]}] } ]} ], [application:set_env(eradius, Key, Value) || {Key, Value} <- Config], @@ -97,20 +97,20 @@ test_client() -> test_client(Command) -> eradius_dict:load_tables([dictionary, dictionary_3gpp]), Request = eradius_lib:set_attributes(#radius_request{cmd = Command, msg_hmac = true}, attrs("user")), - send_request(eradius_test_handler:localhost(tuple), 1813, ?SECRET, Request). + send_request(eradius_test_lib:localhost(tuple), 1813, ?SECRET, Request). test_proxy() -> test_proxy(request). test_proxy(Command) -> eradius_dict:load_tables([dictionary, dictionary_3gpp]), - send_request(eradius_test_handler:localhost(tuple), 11813, ?SECRET2, #radius_request{cmd = Command}), + send_request(eradius_test_lib:localhost(tuple), 11813, ?SECRET2, #radius_request{cmd = Command}), Request = eradius_lib:set_attributes(#radius_request{cmd = Command, msg_hmac = true}, attrs("proxy_test")), - send_request(eradius_test_handler:localhost(tuple), 11813, ?SECRET2, Request), + send_request(eradius_test_lib:localhost(tuple), 11813, ?SECRET2, Request), Request2 = eradius_lib:set_attributes(#radius_request{cmd = Command, msg_hmac = true}, attrs("user@test")), - send_request(eradius_test_handler:localhost(tuple), 11813, ?SECRET2, Request2), + send_request(eradius_test_lib:localhost(tuple), 11813, ?SECRET2, Request2), Request3 = eradius_lib:set_attributes(#radius_request{cmd = Command, msg_hmac = true}, attrs("user@domain@test")), - send_request(eradius_test_handler:localhost(tuple), 11813, ?SECRET2, Request3). + send_request(eradius_test_lib:localhost(tuple), 11813, ?SECRET2, Request3). send_request(Ip, Port, Secret, Request) -> case eradius_client:send_request({Ip, Port, Secret}, Request) of diff --git a/test/eradius_metrics_SUITE.erl b/test/eradius_metrics_SUITE.erl index d788d84..a94dff6 100644 --- a/test/eradius_metrics_SUITE.erl +++ b/test/eradius_metrics_SUITE.erl @@ -31,7 +31,7 @@ -define(ATTRS_BAD, [{?NAS_Identifier, "bad"}]). -define(ATTRS_ERROR, [{?NAS_Identifier, "error"}]). -define(ATTRS_AS_RECORD, [{#attribute{id = ?RStatus_Type}, ?RStatus_Type_Start}]). --define(LOCALHOST, eradius_test_handler:localhost(atom)). +-define(LOCALHOST, eradius_test_lib:localhost(atom)). %% test callbacks all() -> [good_requests, bad_requests, error_requests, request_with_attrs_as_record]. @@ -39,15 +39,15 @@ all() -> [good_requests, bad_requests, error_requests, request_with_attrs_as_rec init_per_suite(Config) -> application:load(eradius), EradiusConfig = [{radius_callback, ?MODULE}, - {servers, [{good, {eradius_test_handler:localhost(ip), [1812]}}, %% for 'positive' responses, e.g. access accepts - {bad, {eradius_test_handler:localhost(ip), [1813]}}, %% for 'negative' responses, e.g. coa naks - {error, {eradius_test_handler:localhost(ip), [1814]}} %% here things go wrong, e.g. duplicate requests + {servers, [{good, {eradius_test_lib:localhost(ip), [1812]}}, %% for 'positive' responses, e.g. access accepts + {bad, {eradius_test_lib:localhost(ip), [1813]}}, %% for 'negative' responses, e.g. coa naks + {error, {eradius_test_lib:localhost(ip), [1814]}} %% here things go wrong, e.g. duplicate requests ]}, {session_nodes, [node()]}, {servers_pool, - [{test_pool, [{eradius_test_handler:localhost(tuple), 1814, ?SECRET}, - {eradius_test_handler:localhost(tuple), 1813, ?SECRET}, - {eradius_test_handler:localhost(tuple), 1812, ?SECRET}]}] + [{test_pool, [{eradius_test_lib:localhost(tuple), 1814, ?SECRET}, + {eradius_test_lib:localhost(tuple), 1813, ?SECRET}, + {eradius_test_lib:localhost(tuple), 1812, ?SECRET}]}] }, {good, [ { {"good", [] }, [{"127.0.0.2", ?SECRET, [{nas_id, <<"good_nas">>}]}] } @@ -107,12 +107,12 @@ error_requests(_Config) -> check_single_request(error, request, access, access_accept). request_with_attrs_as_record(_Config) -> - ok = send_request(accreq, eradius_test_handler:localhost(tuple), 1812, ?ATTRS_AS_RECORD, [{server_name, good}, {client_name, test_records}]), + ok = send_request(accreq, eradius_test_lib:localhost(tuple), 1812, ?ATTRS_AS_RECORD, [{server_name, good}, {client_name, test_records}]), ok = check_metric(accreq, client_accounting_requests_total, [{server_name, good}, {client_name, test_records}, {acct_type, start}], 1). %% helpers check_single_request(good, EradiusRequestType, _RequestType, _ResponseType) -> - ok = send_request(EradiusRequestType, eradius_test_handler:localhost(tuple), 1812, ?ATTRS_GOOD, [{server_name, good}, {client_name, test}]), + ok = send_request(EradiusRequestType, eradius_test_lib:localhost(tuple), 1812, ?ATTRS_GOOD, [{server_name, good}, {client_name, test}]), ok = check_metric(client_access_requests_total, [{server_name, good}], 1), ok = check_metric_multi(EradiusRequestType, client_accounting_requests_total, [{server_name, good}], 1), ok = check_metric_multi({bad_type, EradiusRequestType}, client_accounting_requests_total, [{server_name, good}, {acct_type, bad_type}], 0), @@ -122,18 +122,18 @@ check_single_request(good, EradiusRequestType, _RequestType, _ResponseType) -> ok = check_metric(client_accept_responses_total, [{server_name, good}], 1), ok = check_metric(accept_responses_total, [{server_name, good}], 1), ok = check_metric(access_requests_total, [{server_name, good}], 1), - ok = check_metric(server_status, true, [eradius_test_handler:localhost(tuple), 1812]); + ok = check_metric(server_status, true, [eradius_test_lib:localhost(tuple), 1812]); check_single_request(bad, EradiusRequestType, _RequestType, _ResponseType) -> - ok = send_request(EradiusRequestType, eradius_test_handler:localhost(tuple), 1813, ?ATTRS_BAD, [{server_name, bad}, {client_name, test}]), + ok = send_request(EradiusRequestType, eradius_test_lib:localhost(tuple), 1813, ?ATTRS_BAD, [{server_name, bad}, {client_name, test}]), ok = check_metric(client_access_requests_total, [{server_name, bad}], 1), ok = check_metric(client_reject_responses_total, [{server_name, bad}], 1), ok = check_metric(access_requests_total, [{server_name, bad}], 1), ok = check_metric(reject_responses_total, [{server_name, bad}], 1), - ok = check_metric(server_status, true, [eradius_test_handler:localhost(tuple), 1813]); + ok = check_metric(server_status, true, [eradius_test_lib:localhost(tuple), 1813]); check_single_request(error, EradiusRequestType, _RequestType, _ResponseType) -> - ok = send_request(EradiusRequestType, eradius_test_handler:localhost(tuple), 1814, ?ATTRS_ERROR, + ok = send_request(EradiusRequestType, eradius_test_lib:localhost(tuple), 1814, ?ATTRS_ERROR, [{server_name, error}, {client_name, test}, {timeout, 1000}, - {failover, [{eradius_test_handler:localhost(tuple), 1812, ?SECRET}]}]), + {failover, [{eradius_test_lib:localhost(tuple), 1812, ?SECRET}]}]), ok = check_metric(client_access_requests_total, [{server_name, error}], 1), ok = check_metric(client_retransmissions_total, [{server_name, error}], 1), ok = check_metric(access_requests_total, [{server_name, error}], 1), @@ -141,10 +141,10 @@ check_single_request(error, EradiusRequestType, _RequestType, _ResponseType) -> ok = check_metric(duplicated_requests_total, [{server_name, error}], 1), ok = check_metric(client_requests_total, [{server_name, error}], 1), ok = check_metric(requests_total, [{server_name, error}], 2), - ok = check_metric(server_status, false, [eradius_test_handler:localhost(tuple), 1812]), - ok = check_metric(server_status, false, [eradius_test_handler:localhost(tuple), 1813]), - ok = check_metric(server_status, true, [eradius_test_handler:localhost(tuple), 1814]), - ok = check_metric(server_status, undefined, [eradius_test_handler:localhost(tuple), 1815]). + ok = check_metric(server_status, false, [eradius_test_lib:localhost(tuple), 1812]), + ok = check_metric(server_status, false, [eradius_test_lib:localhost(tuple), 1813]), + ok = check_metric(server_status, true, [eradius_test_lib:localhost(tuple), 1814]), + ok = check_metric(server_status, undefined, [eradius_test_lib:localhost(tuple), 1815]). check_total_requests(good, N) -> diff --git a/test/eradius_proxy_SUITE.erl b/test/eradius_proxy_SUITE.erl index b76ee2f..85c6a2e 100644 --- a/test/eradius_proxy_SUITE.erl +++ b/test/eradius_proxy_SUITE.erl @@ -34,10 +34,10 @@ all() -> [ ]. resolve_routes_test(_) -> - DefaultRoute = {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}, - Prod = {eradius_test_handler:localhost(tuple), 1812, <<"prod">>}, - Test = {eradius_test_handler:localhost(tuple), 11813, <<"test">>}, - Dev = {eradius_test_handler:localhost(tuple), 11814, <<"dev">>}, + DefaultRoute = {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}, + Prod = {eradius_test_lib:localhost(tuple), 1812, <<"prod">>}, + Test = {eradius_test_lib:localhost(tuple), 11813, <<"test">>}, + Dev = {eradius_test_lib:localhost(tuple), 11814, <<"dev">>}, {ok, R1} = re:compile("prod"), {ok, R2} = re:compile("test"), {ok, R3} = re:compile("^dev_.*"), @@ -66,41 +66,41 @@ resolve_routes_test(_) -> ok. validate_arguments_test(_) -> - GoodConfig = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}}, + GoodConfig = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}}, {options, [{type, realm}, {strip, true}, {separator, "@"}]}, - {routes, [{"test_1", {eradius_test_handler:localhost(tuple), 1815, <<"secret1">>}, [{pool, test_pool}]}, + {routes, [{"test_1", {eradius_test_lib:localhost(tuple), 1815, <<"secret1">>}, [{pool, test_pool}]}, {"test_2", {<<"localhost">>, 1816, <<"secret2">>}} ]} ], - GoodOldConfig = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}, test_pool}, + GoodOldConfig = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}, test_pool}, {options, [{type, realm}, {strip, true}, {separator, "@"}]}, - {routes, [{"test_1", {eradius_test_handler:localhost(tuple), 1815, <<"secret1">>}, [{pool, test_pool}]}, + {routes, [{"test_1", {eradius_test_lib:localhost(tuple), 1815, <<"secret1">>}, [{pool, test_pool}]}, {"test_2", {<<"localhost">>, 1816, <<"secret2">>}} ]} ], - BadConfig = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}}, + BadConfig = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}}, {options, [{type, abc}]} ], - BadConfig1 = [{default_route, {eradius_test_handler:localhost(tuple), 0, <<"secret">>}}], + BadConfig1 = [{default_route, {eradius_test_lib:localhost(tuple), 0, <<"secret">>}}], BadConfig2 = [{default_route, {abc, 123, <<"secret">>}}], - BadConfig3 = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}}, + BadConfig3 = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}}, {options, [{type, realm}, {strip, true}, {separator, "@"}]}, {routes, [{"test_1", {wrong_ip, 1815, <<"secret1">>}}, {"test_2", {<<"localhost">>, 1816, <<"secret2">>}} ]}], - BadConfig4 = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}}, + BadConfig4 = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}}, {options, [{type, realm}, {strip, true}, {separator, "@"}, {timeout, "wrong"}]}, {routes, [{"test", {wrong_ip, 1815, <<"secret1">>}}, {"test_2", {<<"localhost">>, 1816, <<"secret2">>}} ]}], - BadConfig5 = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}}, + BadConfig5 = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}}, {options, [{type, realm}, {strip, true}, {separator, "@"}, {retries, "wrong"}]}, {routes, [{"test", {wrong_ip, 1815, <<"secret1">>}}, {"test_2", {"localhost", 1816, <<"secret2">>}} ]}], - BadConfig6 = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>, [{pool, "wrong_pool"}]}}], - BadConfig7 = [{default_route, {eradius_test_handler:localhost(tuple), 1813, <<"secret">>}}, + BadConfig6 = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>, [{pool, "wrong_pool"}]}}], + BadConfig7 = [{default_route, {eradius_test_lib:localhost(tuple), 1813, <<"secret">>}}, {routes, [{"test", {wrong_ip, 1815, <<"secret1">>}, [{pool, "wrong_pool"}]}]}], {Result, ConfigData} = eradius_proxy:validate_arguments(GoodConfig), ?equal(true, Result), diff --git a/test/eradius_test_handler.erl b/test/eradius_test_handler.erl index 20a8c3d..7427ac8 100644 --- a/test/eradius_test_handler.erl +++ b/test/eradius_test_handler.erl @@ -2,26 +2,46 @@ -behaviour(eradius_server). --export([start/0, stop/0, send_request/1, send_request_failover/1, radius_request/3]). --export([localhost/1]). +-export([start/0, start/2, stop/0, send_request/1, send_request_failover/1, radius_request/3]). -include("include/eradius_lib.hrl"). start() -> + start(inet, ipv4). + +start(Backend, Family) -> application:load(eradius), application:set_env(eradius, radius_callback, ?MODULE), - application:set_env(eradius, client_ip, localhost(tuple)), + %% application:set_env(eradius, client_ip, eradius_test_lib:localhost(tuple)), application:set_env(eradius, session_nodes, local), - application:set_env(eradius, one, [{{"ONE", []}, [{localhost(ip), "secret"}]}]), - application:set_env(eradius, two, [{{"TWO", [{default_route, {{127, 0, 0, 2}, 1813, <<"secret">>}}]}, - [{localhost(ip), "secret"}]}]), - application:set_env(eradius, servers, [{one, {localhost(ip), [1812]}}, - {two, {localhost(ip), [1813]}}]), + application:set_env(eradius, one, + [{{"ONE", []}, [{eradius_test_lib:localhost(ip), "secret"}]}]), + application:set_env(eradius, two, + [{{"TWO", [{default_route, {{127, 0, 0, 2}, 1813, <<"secret">>}}]}, + [{eradius_test_lib:localhost(ip), "secret"}]}]), + application:set_env(eradius, servers, + [{one, {eradius_test_lib:localhost(ip), [1812]}}, + {two, {eradius_test_lib:localhost(ip), [1813]}}]), application:set_env(eradius, unreachable_timeout, 2), - application:set_env(eradius, servers_pool, [{test_pool, [{localhost(tuple), 1812, "secret"}, - %% fake upstream server for fail-over - {localhost(string), 1820, "secret"}]}]), + application:set_env(eradius, servers_pool, + [{test_pool, + [{eradius_test_lib:localhost(tuple), 1812, "secret"}, + %% fake upstream server for fail-over + {eradius_test_lib:localhost(string), 1820, "secret"}]}]), application:ensure_all_started(eradius), + + ClientConfig = + #{inet_backend => Backend, + family => eradius_test_lib:inet_family(Family), + ip => eradius_test_lib:localhost(Family, tuple), + servers => [{one, {eradius_test_lib:localhost(Family, ip), [1812]}}, + {two, {eradius_test_lib:localhost(Family, ip), [1813]}}], + servers_pool => + [{test_pool, [{eradius_test_lib:localhost(Family, tuple), 1812, "secret"}, + %% fake upstream server for fail-over + {eradius_test_lib:localhost(Family, string), 1820, "secret"}]}] + }, + eradius_client_mngr:reconfigure(ClientConfig), eradius:modules_ready([?MODULE]). stop() -> @@ -45,22 +65,3 @@ send_request_failover(Server) -> radius_request(#radius_request{cmd = request}, _Nasprop, _Args) -> {reply, #radius_request{cmd = accept}}. - -%% travis is stupid, it includes localhost twice with -%% different IPs in /etc/hosts. This will cause a list -%% of IP to be returned from inet:gethostbyname and that -%% triggers the load balancing in eradius_client. -localhost(string) -> - case os:getenv("TRAVIS") of - false -> "localhost"; - _ -> "ip4-loopback" - end; -localhost(binary) -> - list_to_binary(localhost(string)); -localhost(tuple) -> - {ok, IP} = inet:getaddr(localhost(string), inet), - IP; -localhost(ip) -> - inet:ntoa(localhost(tuple)); -localhost(atom) -> - list_to_atom(localhost(ip)). diff --git a/test/eradius_test_lib.erl b/test/eradius_test_lib.erl new file mode 100644 index 0000000..bde6347 --- /dev/null +++ b/test/eradius_test_lib.erl @@ -0,0 +1,53 @@ +%% Copyright (c) 2010-2017, Travelping GmbH +%% +%% SPDX-License-Identifier: MIT +%% +-module(eradius_test_lib). + +-compile([export_all, nowarn_export_all]). + +%%%=================================================================== +%%% Helper functions +%%%=================================================================== + +has_ipv6_test_config() -> + try + {ok, IfList} = inet:getifaddrs(), + Lo = proplists:get_value("lo", IfList), + V6 = [X || {addr, X = {0,0,0,0,0,0,0,1}} <- Lo], + ct:pal("V6: ~p", [V6]), + length(V6) > 0 + catch + _:_ -> + false + end. + +inet_family(ipv4) -> inet; +inet_family(ipv6) -> inet6; +inet_family(ipv4_mapped_ipv6) -> inet6. + +badhost(Family) + when Family =:= ipv4; Family =:= ipv4_mapped_ipv6 -> + {127, 0, 0, 2}; +badhost(ipv6) -> + {0, 0, 0, 0, 0, 0, 0, 2}. + +localhost(Type) -> + localhost(ipv4, Type). + +localhost(Family, string) when Family =:= ipv4; Family =:= ipv4_mapped_ipv6 -> + "ip4-loopback"; +localhost(ipv6, string) -> + "ip6-loopback"; +localhost(Family, binary) -> + list_to_binary(localhost(Family, string)); +localhost(Family, tuple) when Family =:= ipv4; Family =:= ipv4_mapped_ipv6 -> + {ok, IP} = inet:getaddr(localhost(ipv4, string), inet), + IP; +localhost(ipv6, tuple) -> + {ok, IP} = inet:getaddr(localhost(ipv6, string), inet6), + IP; +localhost(Family, ip) -> + inet:ntoa(localhost(Family, tuple)); +localhost(Family, atom) -> + list_to_atom(localhost(Family, ip)).