From cd21f70da5dbe3b7e2adf2d538556938e7aeed39 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Wed, 22 May 2019 17:15:41 +0300 Subject: [PATCH] Add router --- apps/epp_proxy/src/certs.erl | 1 + apps/epp_proxy/src/epp_xml.erl | 10 ++++- apps/epp_proxy/src/router.erl | 57 +++++++++++++++++++++++++++ apps/epp_proxy/test/epp_xml_SUITE.erl | 12 ++++++ apps/epp_proxy/test/epp_xml_tests.erl | 26 ++++++++++-- apps/epp_proxy/test/router_SUITE.erl | 12 ++++++ apps/epp_proxy/test/router_tests.erl | 56 ++++++++++++++++++++++++++ config/sys.config | 5 ++- config/test.config | 6 +++ 9 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 apps/epp_proxy/src/certs.erl create mode 100644 apps/epp_proxy/src/router.erl create mode 100644 apps/epp_proxy/test/epp_xml_SUITE.erl create mode 100644 apps/epp_proxy/test/router_SUITE.erl create mode 100644 apps/epp_proxy/test/router_tests.erl create mode 100644 config/test.config diff --git a/apps/epp_proxy/src/certs.erl b/apps/epp_proxy/src/certs.erl new file mode 100644 index 0000000..fcfcc3e --- /dev/null +++ b/apps/epp_proxy/src/certs.erl @@ -0,0 +1 @@ +-module(certs). diff --git a/apps/epp_proxy/src/epp_xml.erl b/apps/epp_proxy/src/epp_xml.erl index 3907861..1728390 100644 --- a/apps/epp_proxy/src/epp_xml.erl +++ b/apps/epp_proxy/src/epp_xml.erl @@ -1,9 +1,17 @@ -module(epp_xml). --export([find_cltrid/1, parse/1]). +-export([find_cltrid/1, get_command/1, parse/1]). -include_lib("xmerl/include/xmerl.hrl"). +%% Get command from an xmlElement. Otherwise return undefined. +get_command(Record) when is_record(Record, xmlElement) -> + case xmerl_xpath:string("name(/epp/command/*[1])", Record) of + {xmlObj, string, []} -> undefined; + {xmlObj, string, Command} -> Command + end; +get_command(_) -> undefined. + %% xml_erl expects a list of characters, so let's give it to it. %% Otherwise return an error tuple. parse(Text) when is_list(Text) -> parse_list(Text); diff --git a/apps/epp_proxy/src/router.erl b/apps/epp_proxy/src/router.erl new file mode 100644 index 0000000..12ba360 --- /dev/null +++ b/apps/epp_proxy/src/router.erl @@ -0,0 +1,57 @@ +-module(router). + +-export([route_request/1, is_valid_epp_command/1]). + +-define(validCommands, ["hello", "login", "logout", "check", "info", "poll", + "create", "delete", "renew", "update", "transfer"]). + +%% By default, return test values. This might cause issues in production. +-define(baseSessionUrl, + case application:get_env(epp_proxy, epp_session_url) of + undefined -> "https://registry.test/epp/session/"; + {ok, Value} -> Value + end). + +-define(baseCommandUrl, + case application:get_env(epp_proxy, epp_command_url) of + undefined -> "https://registry.test/epp/command/"; + {ok, Value} -> Value + end). + +%% Save yourself some checking beforehand. +is_valid_epp_command(Command) -> + lists:member(Command, ?validCommands). + +%% Base router +route_request(Command) when is_binary(Command) -> + List = binary_to_list(Command), + url_map(List); +route_request(Command) when is_list(Command) -> url_map(Command). + +%% Actually route to places +url_map(Command) when is_list(Command) -> + case Command of + %% Session commands + "hello" -> string:concat(base_session_url(), Command); + "login" -> string:concat(base_session_url(), Command); + "logout" -> string:concat(base_session_url(), Command); + %% Poll commands + "check" -> string:concat(base_command_url(), Command); + "info" -> string:concat(base_command_url(), Command); + "poll" -> string:concat(base_command_url(), Command); + %% Transform commands + "create" -> string:concat(base_command_url(), Command); + "delete" -> string:concat(base_command_url(), Command); + "renew" -> string:concat(base_command_url(), Command); + "update" -> string:concat(base_command_url(), Command); + % Transfer is both poll and query + "transfer" -> string:concat(base_command_url(), Command) + % Anything else should fail. + end. + +%% Just return the macros as defined +base_session_url() -> + ?baseSessionUrl. + +base_command_url() -> + ?baseCommandUrl. diff --git a/apps/epp_proxy/test/epp_xml_SUITE.erl b/apps/epp_proxy/test/epp_xml_SUITE.erl new file mode 100644 index 0000000..cabc12c --- /dev/null +++ b/apps/epp_proxy/test/epp_xml_SUITE.erl @@ -0,0 +1,12 @@ +-module(epp_xml_SUITE). + +-include_lib("common_test/include/ct.hrl"). + +-export([all/0]). +-export([run_eunit/1]). + +all() -> [run_eunit]. + +%% Run Unit tests +run_eunit(_Config) -> + ok = eunit:test(epp_xml_tests). diff --git a/apps/epp_proxy/test/epp_xml_tests.erl b/apps/epp_proxy/test/epp_xml_tests.erl index 67ab337..17bd465 100644 --- a/apps/epp_proxy/test/epp_xml_tests.erl +++ b/apps/epp_proxy/test/epp_xml_tests.erl @@ -15,6 +15,12 @@ "). +-define(validXMLNotEPPList, + " + test + test@test.com + "). + %% parse parse_not_a_list_or_binary_test() -> Input = 1234, @@ -33,11 +39,11 @@ parse_sample_valid_xml_binary_test() -> parse_sample_invalid_xml_list_test() -> Input = "Some text", - {error, Error} = epp_xml:parse(Input). + {error, {fatal, _Details}} = epp_xml:parse(Input). parse_sample_invalid_xml_binary_test() -> Input = list_to_binary("Some text"), - {error, Error} = epp_xml:parse(Input). + {error, {fatal, _Details}} = epp_xml:parse(Input). %% find_cltrid find_cltrid_empty_list_test() -> @@ -56,5 +62,19 @@ find_cltrid_binary_test() -> Input = ?sampleCommandList, ?assertEqual(<<"sample1trid">>, epp_xml:find_cltrid(Input)). - %% get_command +get_command_success_test() -> + %% We require an existing xlmElement record to pass around. + {ok, XMLElement} = epp_xml:parse(?sampleCommandList), + Command = epp_xml:get_command(XMLElement), + ?assertEqual("login", Command). + +get_command_xml_not_epp_failure_test() -> + {ok, XMLElement} = epp_xml:parse(?validXMLNotEPPList), + Command = epp_xml:get_command(XMLElement), + ?assertEqual(undefined, Command). + +get_command_failure_test() -> + %% Can pass any garbage, should get back undefined., + Command = epp_xml:get_command("Some random string"), + ?assertEqual(undefined, Command). diff --git a/apps/epp_proxy/test/router_SUITE.erl b/apps/epp_proxy/test/router_SUITE.erl new file mode 100644 index 0000000..cbaedc5 --- /dev/null +++ b/apps/epp_proxy/test/router_SUITE.erl @@ -0,0 +1,12 @@ +-module(router_SUITE). + +-include_lib("common_test/include/ct.hrl"). + +-export([all/0]). +-export([run_eunit/1]). + +all() -> [run_eunit]. + +%% Run Unit tests +run_eunit(_Config) -> + ok = eunit:test(router_tests). diff --git a/apps/epp_proxy/test/router_tests.erl b/apps/epp_proxy/test/router_tests.erl new file mode 100644 index 0000000..230fc29 --- /dev/null +++ b/apps/epp_proxy/test/router_tests.erl @@ -0,0 +1,56 @@ +-module(router_tests). + +-include_lib("eunit/include/eunit.hrl"). + +is_valid_epp_command_test() -> + Commands = ["hello", "login", "logout", "check", "info", "poll", + "create", "delete", "renew", "update", "transfer"], + lists:foreach(fun (N) -> + ?assert(router:is_valid_epp_command(N)) + end, + Commands). + +%% TODO: Make less verbose and repetitive +hello_url_test() -> + ?assertEqual("https://registry.test/epp/session/hello", router:route_request("hello")), + ?assertEqual("https://registry.test/epp/session/hello", router:route_request(<<"hello">>)). + +login_url_test() -> + ?assertEqual("https://registry.test/epp/session/login", router:route_request("login")), + ?assertEqual("https://registry.test/epp/session/login", router:route_request(<<"login">>)). + +logout_url_test() -> + ?assertEqual("https://registry.test/epp/session/logout", router:route_request("logout")), + ?assertEqual("https://registry.test/epp/session/logout", router:route_request(<<"logout">>)). + +check_url_test() -> + ?assertEqual("https://registry.test/epp/command/check", router:route_request("check")), + ?assertEqual("https://registry.test/epp/command/check", router:route_request(<<"check">>)). + +info_url_test() -> + ?assertEqual("https://registry.test/epp/command/info", router:route_request("info")), + ?assertEqual("https://registry.test/epp/command/info", router:route_request(<<"info">>)). + +poll_url_test() -> + ?assertEqual("https://registry.test/epp/command/poll", router:route_request("poll")), + ?assertEqual("https://registry.test/epp/command/poll", router:route_request(<<"poll">>)). + +create_url_test() -> + ?assertEqual("https://registry.test/epp/command/create", router:route_request("create")), + ?assertEqual("https://registry.test/epp/command/create", router:route_request(<<"create">>)). + +delete_url_test() -> + ?assertEqual("https://registry.test/epp/command/delete", router:route_request("delete")), + ?assertEqual("https://registry.test/epp/command/delete", router:route_request(<<"delete">>)). + +renew_url_test() -> + ?assertEqual("https://registry.test/epp/command/renew", router:route_request("renew")), + ?assertEqual("https://registry.test/epp/command/renew", router:route_request(<<"renew">>)). + +update_url_test() -> + ?assertEqual("https://registry.test/epp/command/update", router:route_request("update")), + ?assertEqual("https://registry.test/epp/command/update", router:route_request(<<"update">>)). + +transfer_url_test() -> + ?assertEqual("https://registry.test/epp/command/transfer", router:route_request("transfer")), + ?assertEqual("https://registry.test/epp/command/transfer", router:route_request(<<"transfer">>)). diff --git a/config/sys.config b/config/sys.config index 2a45bab..cd748b1 100644 --- a/config/sys.config +++ b/config/sys.config @@ -1,3 +1,6 @@ [ - {epp_proxy, []} + {epp_proxy, [{tcp_port, 3333}, + {tls_port, 4444}, + {epp_session_url, "https://registry.test/epp/session/"}, + {epp_command_url, "https://registry.test/epp/command/"}]} ]. diff --git a/config/test.config b/config/test.config new file mode 100644 index 0000000..06b167d --- /dev/null +++ b/config/test.config @@ -0,0 +1,6 @@ +[ + {epp_proxy, [{tcp_port, 1133}, + {tls_port, 4444}, + {epp_session_url, "https://registry.test/epp/session/"}, + {epp_command_url, "https://registry.test/epp/command/"}]} +].