From 3ceaed4dd337a75c099837cbb333e9a9e621eace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 18 Apr 2018 15:32:01 +0800 Subject: [PATCH] Crypto: more alcotesty tests --- src/lib_crypto/test/roundtrips.ml | 50 +++++++++++------------------ src/lib_crypto/test/test_base58.ml | 12 +++---- src/lib_crypto/test/test_blake2b.ml | 5 ++- src/lib_crypto/test/test_ed25519.ml | 9 ++++-- 4 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/lib_crypto/test/roundtrips.ml b/src/lib_crypto/test/roundtrips.ml index 796ff642b..bbdef011d 100644 --- a/src/lib_crypto/test/roundtrips.ml +++ b/src/lib_crypto/test/roundtrips.ml @@ -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) diff --git a/src/lib_crypto/test/test_base58.ml b/src/lib_crypto/test/test_base58.ml index 717435588..290c9d375 100644 --- a/src/lib_crypto/test/test_base58.ml +++ b/src/lib_crypto/test/test_base58.ml @@ -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 diff --git a/src/lib_crypto/test/test_blake2b.ml b/src/lib_crypto/test/test_blake2b.ml index c4d44ca22..38d1cfb63 100644 --- a/src/lib_crypto/test/test_blake2b.ml +++ b/src/lib_crypto/test/test_blake2b.ml @@ -11,7 +11,10 @@ let test_hashed_roundtrip name enc dec input = (* this wrapper to start with hashing *) Roundtrips.test_rt_opt name - (fun fmt (input, _) -> Format.fprintf fmt "%s" input) + (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 diff --git a/src/lib_crypto/test/test_ed25519.ml b/src/lib_crypto/test/test_ed25519.ml index 572dd0e9a..b498b0edb 100644 --- a/src/lib_crypto/test/test_ed25519.ml +++ b/src/lib_crypto/test/test_ed25519.ml @@ -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