aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/test
diff options
context:
space:
mode:
authorGuilherme Andrade <[email protected]>2017-03-12 17:20:00 +0000
committerGuilherme Andrade <[email protected]>2017-03-14 23:53:39 +0000
commitd07008a0562d1f83dcab144fdec9fd920deb2b96 (patch)
tree1e2aa60f891b6f2c47e85ded243d94d918bef9f4 /lib/crypto/test
parent27df945c35aa541330700d75b6844de9886361b2 (diff)
downloadotp-d07008a0562d1f83dcab144fdec9fd920deb2b96.tar.gz
otp-d07008a0562d1f83dcab144fdec9fd920deb2b96.tar.bz2
otp-d07008a0562d1f83dcab144fdec9fd920deb2b96.zip
Support generation of strong random numbers
Diffstat (limited to 'lib/crypto/test')
-rw-r--r--lib/crypto/test/crypto_SUITE.erl50
1 files changed, 49 insertions, 1 deletions
diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl
index 1d7037d003..6e3a3879c4 100644
--- a/lib/crypto/test/crypto_SUITE.erl
+++ b/lib/crypto/test/crypto_SUITE.erl
@@ -36,7 +36,9 @@ all() ->
{group, non_fips},
mod_pow,
exor,
- rand_uniform
+ rand_uniform,
+ strong_rand_uniform_float,
+ strong_rand_uniform_integer
].
groups() ->
@@ -486,6 +488,44 @@ rand_uniform(Config) when is_list(Config) ->
10 = byte_size(crypto:strong_rand_bytes(10)).
%%--------------------------------------------------------------------
+strong_rand_uniform_float() ->
+ [{doc, "strong_rand_uniform float testing"}].
+strong_rand_uniform_float(Config) when is_list(Config) ->
+ Samples = [crypto:strong_rand_uniform() || _ <- lists:seq(1, 10000)],
+ allmap(
+ fun (V) ->
+ (V >= 0.0 andalso V < 1.0)
+ orelse {false, ct:fail({"Not in interval", V, 0.0, 1.0})}
+ end,
+ Samples).
+
+strong_rand_uniform_integer() ->
+ [{doc, "strong_rand_uniform integer testing"}].
+strong_rand_uniform_integer(Config) when is_list(Config) ->
+ MaxCeiling = 1 bsl 32,
+ Ceilings = [1 | % edge case where the ceiling equals the floor
+ [crypto:strong_rand_uniform(MaxCeiling)
+ || _ <- lists:seq(1, 99)]],
+
+ allmap(
+ fun (Ceiling) ->
+ case Ceiling >= 1 andalso Ceiling =< MaxCeiling of
+ false ->
+ {false, ct:fail({"Ceiling not in interval", Ceiling, 1, MaxCeiling})};
+ true ->
+ Samples = [crypto:strong_rand_uniform(Ceiling)
+ || _ <- lists:seq(1, 100)],
+ allmap(
+ fun (V) ->
+ (V >= 1 andalso V =< Ceiling)
+ orelse {false, ct:fail({"Sample not in interval", V, 1, Ceiling})}
+ end,
+ Samples)
+ end
+ end,
+ Ceilings).
+
+%%--------------------------------------------------------------------
%% Internal functions ------------------------------------------------
%%--------------------------------------------------------------------
hash(_, [], []) ->
@@ -951,6 +991,14 @@ crypto_rand_uniform(L,H) ->
ct:fail({"Not in interval", R1, L, H})
end.
+allmap(_Fun, []) ->
+ true;
+allmap(Fun, [H|T]) ->
+ case Fun(H) of
+ true -> allmap(Fun, T);
+ {false, Result} -> Result
+ end.
+
%%--------------------------------------------------------------------
%% Test data ------------------------------------------------
%%--------------------------------------------------------------------