Crypto: more alcotesty tests
This commit is contained in:
parent
cd8a63f543
commit
3ceaed4dd3
@ -8,39 +8,27 @@
|
||||
(**************************************************************************)
|
||||
|
||||
|
||||
let test_rt_opt name pp enc dec input =
|
||||
let encoded = enc input in
|
||||
match dec encoded with
|
||||
| Some output ->
|
||||
if output = input then
|
||||
()
|
||||
else
|
||||
Format.kasprintf failwith
|
||||
"%s failed for %a: got %a" name pp input pp output
|
||||
| None ->
|
||||
Format.kasprintf failwith
|
||||
"%s failed for %a: unable to decode" name pp input
|
||||
| exception exc ->
|
||||
Format.kasprintf failwith
|
||||
"%s failed for %a: exception whilst decoding: %s"
|
||||
name pp input (Printexc.to_string exc)
|
||||
let test_rt_opt name testable enc dec input =
|
||||
try
|
||||
let roundtripped = dec (enc input) in
|
||||
Alcotest.check (Alcotest.option testable) name (Some input) roundtripped
|
||||
with
|
||||
exc ->
|
||||
Alcotest.failf "%s failed for %a: exception whilst decoding: %s"
|
||||
name (Alcotest.pp testable) input (Printexc.to_string exc)
|
||||
|
||||
let test_decode_opt_safe name pp dec encoded =
|
||||
let test_decode_opt_safe name testable dec encoded =
|
||||
match dec encoded with
|
||||
| Some _ | None -> ()
|
||||
| exception exc ->
|
||||
Format.kasprintf failwith
|
||||
"%s failed for %a: exception whilst decoding: %s"
|
||||
name pp encoded (Printexc.to_string exc)
|
||||
Alcotest.failf "%s failed for %a: exception whilst decoding: %s"
|
||||
name (Alcotest.pp testable) encoded (Printexc.to_string exc)
|
||||
|
||||
let test_decode_opt_fail name pp dec encoded =
|
||||
match dec encoded with
|
||||
| Some _ ->
|
||||
Format.kasprintf failwith
|
||||
"%s failed for %a: successful decoding of invalid input"
|
||||
name pp encoded
|
||||
| None -> ()
|
||||
| exception exc ->
|
||||
Format.kasprintf failwith
|
||||
"%s failed for %a: exception whilst decoding: %s"
|
||||
name pp encoded (Printexc.to_string exc)
|
||||
let test_decode_opt_fail name testable dec encoded =
|
||||
try
|
||||
let decoded = dec encoded in
|
||||
Alcotest.check (Alcotest.option testable) name None decoded
|
||||
with
|
||||
exc ->
|
||||
Alcotest.failf "%s failed: exception whilst decoding: %s"
|
||||
name (Printexc.to_string exc)
|
||||
|
@ -10,17 +10,15 @@
|
||||
let test_roundtrip_safe input =
|
||||
Roundtrips.test_rt_opt
|
||||
"safe base58"
|
||||
Format.pp_print_string
|
||||
Base58.safe_encode
|
||||
Base58.safe_decode
|
||||
Alcotest.string
|
||||
Base58.safe_encode Base58.safe_decode
|
||||
input
|
||||
|
||||
let test_roundtrip_raw input =
|
||||
Roundtrips.test_rt_opt
|
||||
"raw base58"
|
||||
Format.pp_print_string
|
||||
Base58.raw_encode
|
||||
Base58.raw_decode
|
||||
Alcotest.string
|
||||
Base58.raw_encode Base58.raw_decode
|
||||
input
|
||||
|
||||
let inputs = [
|
||||
@ -59,7 +57,7 @@ let test_roundtrip_raws () = List.iter test_roundtrip_raw inputs
|
||||
let test_safety input =
|
||||
Roundtrips.test_decode_opt_safe
|
||||
"safe base58"
|
||||
Format.pp_print_string
|
||||
Alcotest.string
|
||||
Base58.safe_decode
|
||||
input
|
||||
|
||||
|
@ -11,7 +11,10 @@ let test_hashed_roundtrip name enc dec input =
|
||||
(* this wrapper to start with hashing *)
|
||||
Roundtrips.test_rt_opt
|
||||
name
|
||||
(Alcotest.testable
|
||||
(fun fmt (input, _) -> Format.fprintf fmt "%s" input)
|
||||
(fun (_, hashed) (_, decoded) -> hashed = decoded)
|
||||
)
|
||||
(fun (_, hashed) -> enc hashed)
|
||||
(fun encoded -> match dec encoded with
|
||||
| None -> None
|
||||
|
@ -21,7 +21,12 @@ let test_b58check_roundtrip
|
||||
: type t. (module B58CHECK with type t = t) -> t -> unit
|
||||
= fun m input ->
|
||||
let module M = (val m) in
|
||||
Roundtrips.test_rt_opt "b58check" M.pp M.to_b58check M.of_b58check_opt input
|
||||
let testable = Alcotest.testable M.pp (=) in
|
||||
Roundtrips.test_rt_opt
|
||||
"b58check"
|
||||
testable
|
||||
M.to_b58check M.of_b58check_opt
|
||||
input
|
||||
|
||||
let test_b58check_roundtrips () =
|
||||
let (pubkey_hash, pubkey, seckey) = get_keys () in
|
||||
@ -33,7 +38,7 @@ let test_b58check_roundtrips () =
|
||||
let test_b58check_invalid input =
|
||||
Roundtrips.test_decode_opt_fail
|
||||
"b58check"
|
||||
Format.pp_print_string
|
||||
(Alcotest.testable Ed25519.Public_key_hash.pp Ed25519.Public_key_hash.(=))
|
||||
Ed25519.Public_key_hash.of_b58check_opt
|
||||
input
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user