1- % %%
2- % %% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3- % %% Version 2, December 2004
4- % %%
5- % %% Copyright (C) 2011 Adam Rutkowski <hq@mtod.org>
6- % %%
7- % %% Everyone is permitted to copy and distribute verbatim or modified
8- % %% copies of this license document, and changing it is allowed as long
9- % %% as the name is changed.
10- % %%
11- % %% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
12- % %% TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
13- % %%
14-
151% % @doc Pretty printer for Erlang binaries.
16- % %
172-module (binpp ).
183-author ('Adam Rutkowski hq@mtod.org' ).
194
@@ -217,8 +202,24 @@ buckets(X, N, M, [H|T], [A|Acc]) ->
217202-define (MAX_BIN_SIZE , 2048 ).
218203-define (RUNS , 100 ).
219204
205+ -ifdef (rand_only ).
206+ -define (random , rand ).
207+ -else .
208+ -define (random , random ).
209+ -endif .
210+
211+ -ifdef (rand_only ).
212+ random_seed () ->
213+ % % the rand module self-seeds
214+ ok .
215+ -else .
216+ random_seed () ->
217+ <<A :32 , B :32 , C :32 >> = crypto :rand_bytes (12 ),
218+ random :seed ({A ,B ,C }).
219+ -endif .
220+
220221setup_random () ->
221- _ = random : seed ( erlang : now () ),
222+ _ = random_seed ( ),
222223 ok .
223224
224225binpp_random_test_ () ->
@@ -345,20 +346,22 @@ convert_bin_test_() ->
345346
346347rand_pprint () ->
347348 F = fun pprint /1 ,
348- Tests = [ { crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )), ok } || _ <- lists :seq (1 , ? RUNS ) ],
349+ Tests = [ { crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )), ok } || _ <- lists :seq (1 , ? RUNS ) ],
349350 [ { <<" Random pprint" >>, fun () -> ? assertEqual (R , F (I )) end }
350351 || { I , R } <- Tests ].
351352
352353rand_pprint_bitstring () ->
353354 F = fun pprint /1 ,
354- Tests = [ { << (crypto :rand_bytes (random :uniform (? MAX_BIN_SIZE )))/binary , 0 :(random :uniform (7 ))>>, ok }
355+ Tests = [ { <<
356+ (crypto :strong_rand_bytes (? random :uniform (? MAX_BIN_SIZE )))/binary ,
357+ 0 :(? random :uniform (7 ))>>, ok }
355358 || _ <- lists :seq (1 , ? RUNS ) ],
356359 [ { <<" Random pprint (bitstring)" >>, fun () -> ? assertEqual (R , F (I )) end }
357360 || { I , R } <- Tests ].
358361
359362rand_compare () ->
360363 F = fun compare /2 ,
361- Rand = fun () -> crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )) end ,
364+ Rand = fun () -> crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )) end ,
362365 Tests = [ { { Rand (), Rand () }, ok } || _ <- lists :seq (1 , ? RUNS ) ],
363366 [ { <<" Random compare" >>, fun () -> ? assertEqual (R , F (I1 , I2 )) end }
364367 || { {I1 , I2 }, R } <- Tests ].
@@ -375,8 +378,8 @@ rand_pprint_opts() ->
375378 ],
376379 Range = length (OptsMap ),
377380 Rand = fun () ->
378- Input = crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )),
379- {Opt , Predicate } = lists :nth (random :uniform (Range ), OptsMap ),
381+ Input = crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )),
382+ {Opt , Predicate } = lists :nth (? random :uniform (Range ), OptsMap ),
380383 {Input , Opt , Predicate }
381384 end ,
382385 Tests = [ Rand () || _ <- lists :seq (1 , ? RUNS ) ],
@@ -388,9 +391,9 @@ rand_pprint_opts() ->
388391rand_pprint_slice () ->
389392 F = fun pprint /3 ,
390393 Rand = fun () ->
391- Bytes = crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )),
392- Pos = random :uniform (byte_size (Bytes )),
393- Len = random :uniform (byte_size (Bytes )),
394+ Bytes = crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )),
395+ Pos = ? random :uniform (byte_size (Bytes )),
396+ Len = ? random :uniform (byte_size (Bytes )),
394397 {Bytes , Pos , Len }
395398 end ,
396399 Tests = [ Rand () || _ <- lists :seq (1 , ? RUNS ) ],
@@ -403,9 +406,9 @@ rand_pprint_slice() ->
403406rand_from_str () ->
404407 F = fun from_str /1 ,
405408 Rand = fun () ->
406- Bytes = crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )),
409+ Bytes = crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )),
407410 {ok , Converted } = convert (Bytes ),
408- case random :uniform (2 ) of
411+ case ? random :uniform (2 ) of
409412 1 -> {lists :flatten (Converted ), Bytes };
410413 2 -> {string :join (Converted , " " ), Bytes }
411414 end
0 commit comments