Skip to content

Commit

Permalink
Merge pull request #13790 from MinaProtocol/dannywillems/move-to-alco…
Browse files Browse the repository at this point in the history
…test-base58-and-add-test-vectors

Base58_check: move to alcotest + add test vectors
  • Loading branch information
dannywillems authored Aug 1, 2023
2 parents 6d143f7 + 117b705 commit 0d5562e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 50 deletions.
50 changes: 0 additions & 50 deletions src/lib/base58_check/base58_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,53 +104,3 @@ struct
end

module Version_bytes = Version_bytes

let%test_module "base58check tests" =
( module struct
module Base58_check = Make (struct
let description = "Base58check tests"

let version_byte = '\x53'
end)

open Base58_check

let test_roundtrip payload =
let encoded = encode payload in
let payload' = decode_exn encoded in
String.equal payload payload'

let%test "empty_string" = test_roundtrip ""

let%test "nonempty_string" =
test_roundtrip "Somewhere, over the rainbow, way up high"

let%test "longer_string" =
test_roundtrip
"Someday, I wish upon a star, wake up where the clouds are far behind \
me, where trouble melts like lemon drops, High above the chimney top, \
that's where you'll find me"

let%test "invalid checksum" =
try
let encoded = encode "Bluer than velvet were her eyes" in
let bytes = Bytes.of_string encoded in
let len = Bytes.length bytes in
let last_ch = Bytes.get bytes (len - 1) in
(* change last byte to invalidate checksum *)
let new_last_ch =
if Char.equal last_ch '\xFF' then '\x00'
else Char.of_int_exn (Char.to_int last_ch + 1)
in
Bytes.set bytes (len - 1) new_last_ch ;
let encoded_bad_checksum = Bytes.to_string bytes in
let _payload = decode_exn encoded_bad_checksum in
false
with Invalid_base58_checksum _ -> true

let%test "invalid length" =
try
let _payload = decode_exn "abcd" in
false
with Invalid_base58_check_length _ -> true
end )
3 changes: 3 additions & 0 deletions src/lib/base58_check/tests/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(tests
(names test_base58_check)
(libraries core_kernel base58_check alcotest))
75 changes: 75 additions & 0 deletions src/lib/base58_check/tests/test_base58_check.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module M = Base58_check.Make (struct
let description = "Base58check tests"

let version_byte = '\x53'
end)

open M

let helper_test_roundtrip payload =
let encoded = encode payload in
let payload' = decode_exn encoded in
assert (String.equal payload payload')

let test_roundtrip_empty_string () = helper_test_roundtrip ""

let test_roundtrip_nonempty_string () =
helper_test_roundtrip "Somewhere, over the rainbow, way up high"

let test_roundtrip_longer_string () =
helper_test_roundtrip
"Someday, I wish upon a star, wake up where the clouds are far behind me, \
where trouble melts like lemon drops, High above the chimney top, that's \
where you'll find me"

let test_invalid_checksum () =
try
let encoded = encode "Bluer than velvet were her eyes" in
let bytes = Bytes.of_string encoded in
let len = Bytes.length bytes in
let last_ch = Bytes.get bytes (len - 1) in
(* change last byte to invalidate checksum *)
let new_last_ch =
if Char.equal last_ch '\xFF' then '\x00'
else Core_kernel.Char.of_int_exn (Core_kernel.Char.to_int last_ch + 1)
in
Bytes.set bytes (len - 1) new_last_ch ;
let encoded_bad_checksum = Bytes.to_string bytes in
let _payload = decode_exn encoded_bad_checksum in
assert false
with Base58_check.Invalid_base58_checksum _ -> assert true

let test_invalid_length () =
try
let _payload = decode_exn "abcd" in
assert false
with Base58_check.Invalid_base58_check_length _ -> assert true

let test_vectors () =
let vectors =
[ ("", "AR3b7Dr")
; ("vectors", "2aML9fKacueS1p5W3")
; ("test", "24cUQZMy5c7Mj")
]
in
assert (
List.for_all
(fun (inp, exp_output) ->
let output = M.encode inp in
String.equal output exp_output )
vectors )

let () =
let open Alcotest in
run "Base58_check"
[ ( "test_roundtrip"
, [ test_case "empty string" `Quick test_roundtrip_empty_string
; test_case "non empty string" `Quick test_roundtrip_nonempty_string
; test_case "longer string" `Quick test_roundtrip_longer_string
] )
; ( "negative tests"
, [ test_case "invalid checksym" `Quick test_invalid_checksum
; test_case "invalid length" `Quick test_invalid_length
] )
; ("test vectors", [ test_case "vectors" `Quick test_vectors ])
]

0 comments on commit 0d5562e

Please sign in to comment.