This is an automated email from the ASF dual-hosted git repository. jaydoane pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git
commit afa766fd48fd5d5b34d684600e7a1d749a6e9dea Author: Nick Vatamaniuc <vatam...@apache.org> AuthorDate: Mon Oct 2 13:49:54 2017 -0400 Replace deprecated random module and crypto:rand_bytes calls --- test/b64url_tests.erl | 36 ++++++++++++++---------------------- test/benchmark.escript | 9 +++------ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/test/b64url_tests.erl b/test/b64url_tests.erl index b3d7779..7b21459 100644 --- a/test/b64url_tests.erl +++ b/test/b64url_tests.erl @@ -64,16 +64,8 @@ decode_bad_length_test() -> gen_binary() -> - case get(random_seed) of - undefined -> - random:seed(os:timestamp()); - _ -> - ok - end, - % -1 here so that zero is a possibility - Length = random:uniform(?MAX_SIZE) - 1, - Bytes = tl([random:uniform(256)-1 || _ <- lists:seq(0, Length)]), - list_to_binary(Bytes). + Length = crypto:rand_uniform(0, ?MAX_SIZE), + crypto:strong_rand_bytes(Length). shallow_iolist() -> @@ -93,31 +85,31 @@ bad_len_binary() -> to_iolist(<<>>) -> - case random:uniform(2) of - 1 -> <<>>; - 2 -> [<<>>] + case crypto:rand_uniform(0,2) of + 0 -> <<>>; + 1 -> [<<>>] end; to_iolist(B) when is_binary(B), size(B) > 0 -> - S = random:uniform(size(B)), + S = crypto:rand_uniform(1, size(B) + 1), <<First:S/binary, Second/binary>> = B, - case random:uniform(3) of - 1 -> + case crypto:rand_uniform(0, 3) of + 0 -> [to_iolist(First), Second]; - 2 -> + 1 -> [First, to_iolist(Second)]; - 3 -> + 2 -> [First, Second] end. insert_error(B) when is_binary(B), size(B) < 2 -> - case random:uniform(2) of - 1 -> {<<122, 255>>, 0}; - 2 -> {<<122, 122, 255>>, 0} + case crypto:rand_uniform(0, 2) of + 0 -> {<<122, 255>>, 0}; + 1 -> {<<122, 122, 255>>, 0} end; insert_error(B) when is_binary(B) -> B64 = couch_encode_base64url(B), - S = random:uniform(size(B64)-1), + S = crypto:rand_uniform(0, size(B64)), <<First:S/binary, _:1/binary, Second/binary>> = B64, {<<First:S/binary, 255, Second/binary>>, 4 * (S div 4)}. diff --git a/test/benchmark.escript b/test/benchmark.escript index 5c2bdd5..e1a1f70 100755 --- a/test/benchmark.escript +++ b/test/benchmark.escript @@ -97,7 +97,6 @@ spawn_workers(NumWorkers, St) -> run_worker(St) -> - random:seed(erlang:now()), receive start -> ok end, @@ -116,8 +115,8 @@ run_worker(St, Started) -> do_round_trip(St) -> - Size = St#st.minsize + random:uniform(St#st.maxsize - St#st.minsize), - Data = crypto:rand_bytes(Size), + Size = crypto:rand_uniform(St#st.minsize, St#st.maxsize + 1), + Data = crypto:strong_rand_bytes(Size), Encoded = (St#st.module):encode(Data), Data = (St#st.module):decode(Encoded), St#st{total_bytes=St#st.total_bytes+Size}. @@ -137,10 +136,8 @@ decode(Url64) -> Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)), base64:decode(<<Url2/binary, Padding/binary>>). - randomize(List) -> - random:seed(erlang:now()), - List0 = [{random:uniform(), L} || L <- List], + List0 = [{crypto:rand_uniform(0, 1 bsl 32), L} || L <- List], List1 = lists:sort(List0), [L || {_, L} <- List1].