%%% @doc %%% Gajumaru Java Lib Tester: gmt %%% @end -module(gmt). -vsn("0.1.0"). -author("Craig Everett "). -copyright("Craig Everett "). -license("LGPL-3.0-or-later"). -export([start/1]). mods() -> #{"base64" => fun base64/0, "base58" => fun base58/0, "rlp" => fun rlp/0}. -spec start(ArgV) -> ok when ArgV :: [string()]. start([]) -> Tests = mods(), ok = run(Tests), zx:silent_stop(); start(["list"]) -> ok = io:format("Available tests:~n"), ok = lists:foreach(fun display/1, maps:keys(mods())), zx:silent_stop(); start(Mods) -> Tests = maps:with(Mods, mods()), ok = case maps:size(Tests) =:= length(Mods) of true -> run(Tests); false -> NotMods = lists:subtract(Mods, maps:keys(Tests)), ok = io:format("The following arguments are not testable module names:~n"), lists:foreach(fun display/1, NotMods) end, zx:silent_stop(). display(Name) -> io:format(" ~ts~n", [Name]). run(Tests) -> BaseDir = filename:dirname(zx:get_home()), ok = file:set_cwd(BaseDir), ok = clean(), ok = build(), Results = maps:map(fun run/2, Tests), io:format("Results:~n ~tp~n", [Results]). run(Name, Test) -> ok = io:format("~nRunning: ~ts...~n", [Name]), Test(). clean() -> Temp = temp_dir(), lists:foreach(fun(D) -> ok = clean(D) end, [Temp]). clean(Dir) -> case file:del_dir_r(Dir) of ok -> ok; {error, enoent} -> ok; Error -> Error end. build() -> Out = os:cmd("bin/compile"), io:format("Compile: ~ts", [Out]). temp_dir() -> "test/temp". base64() -> TestFile = filename:join(temp_dir(), "base64.test"), ConvFile = filename:join(temp_dir(), "base64.erlang.txt"), ok = filelib:ensure_dir(TestFile), ok = file:write_file(TestFile, rand:bytes(rand:uniform(5000))), {ok, Bytes} = file:read_file(TestFile), Base64 = base64:encode(Bytes), ok = file:write_file(ConvFile, Base64), Run = unicode:characters_to_list(["bin/run base64 ", temp_dir()]), [JavaEncPath, JavaDecPath] = string:split(os:cmd(Run), " "), {ok, E_Enc_Bytes} = file:read_file(ConvFile), {ok, J_Enc_Bytes} = file:read_file(JavaEncPath), E_Hash = crypto:hash(sha512, E_Enc_Bytes), J_Hash = crypto:hash(sha512, J_Enc_Bytes), {ok, E_Dec_Bytes} = file:read_file(TestFile), {ok, J_Dec_Bytes} = file:read_file(JavaDecPath), E_BinHash = crypto:hash(sha512, E_Dec_Bytes), J_BinHash = crypto:hash(sha512, J_Dec_Bytes), E_Hash =:= J_Hash andalso E_BinHash =:= J_BinHash. base58() -> TestFile = filename:join(temp_dir(), "base58.test"), ConvFile = filename:join(temp_dir(), "base58.erlang.txt"), ok = filelib:ensure_dir(TestFile), ok = file:write_file(TestFile, rand:bytes(rand:uniform(5000))), {ok, Bytes} = file:read_file(TestFile), Base58 = base58:binary_to_base58(Bytes), ok = file:write_file(ConvFile, Base58), Run = unicode:characters_to_list(["bin/run base58 ", temp_dir()]), [JavaEncPath, JavaDecPath] = string:split(os:cmd(Run), " "), {ok, E_Enc_Bytes} = file:read_file(ConvFile), {ok, J_Enc_Bytes} = file:read_file(JavaEncPath), E_Hash = crypto:hash(sha512, E_Enc_Bytes), J_Hash = crypto:hash(sha512, J_Enc_Bytes), {ok, E_Dec_Bytes} = file:read_file(TestFile), {ok, J_Dec_Bytes} = file:read_file(JavaDecPath), E_BinHash = crypto:hash(sha512, E_Dec_Bytes), J_BinHash = crypto:hash(sha512, J_Dec_Bytes), E_Hash =:= J_Hash andalso E_BinHash =:= J_BinHash. rlp() -> Anchor = <<"I thought what I'd do was, I'd pretend I was one of those deaf-mutes.">>, Data = [rand:bytes(rand:uniform(20)), [rand:bytes(rand:uniform(20)), Anchor, rand:bytes(rand:uniform(20)), rand:bytes(rand:uniform(5000))], rand:bytes(rand:uniform(2000))], RLP = gmser_rlp:encode(Data), RLP_File = filename:join(temp_dir(), "rlp.test"), ok = file:write_file(RLP_File, RLP), Run = unicode:characters_to_list(["bin/run rlp ", temp_dir()]), [Found, JavaEncPath] = string:split(os:cmd(Run), " "), {ok, E_Enc_Bytes} = file:read_file(RLP_File), {ok, J_Enc_Bytes} = file:read_file(JavaEncPath), E_Hash = crypto:hash(sha512, E_Enc_Bytes), J_Hash = crypto:hash(sha512, J_Enc_Bytes), E_Hash =:= J_Hash andalso Found =:= Anchor.