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