From 3afe7c399b652ef98cd7b2ed35df8a0bfa951d84 Mon Sep 17 00:00:00 2001 From: swrup Date: Tue, 7 Oct 2025 16:26:05 +0200 Subject: [PATCH] test binary_formats --- src/binary_formats.ml | 24 ++++++++++++------- test/dune | 7 ++++++ test/signatures.c | 55 +++++++++++++++++++++++++++++++++++++++++++ test/test.ml | 7 +++++- 4 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 test/signatures.c diff --git a/src/binary_formats.ml b/src/binary_formats.ml index f51dc198..a750e089 100644 --- a/src/binary_formats.ml +++ b/src/binary_formats.ml @@ -451,15 +451,23 @@ module WithdrawConfirmationPS = struct noreveal_index: int32; } - let size = Purpose.size + HashPlanchetsP.size + int32_size - let purpose = Purpose.make ~size Taler_signatures.exchange_confirm_withdraw + (*let size = Purpose.size + HashPlanchetsP.size + int32_size*) + + open Bin let bin = - let open Bin in - record (fun _urpose h_planchets noreveal_index -> - { h_planchets; noreveal_index }) - |+ field Purpose.bin (Fun.const purpose) - |+ field HashPlanchetsP.bin (fun t -> t.h_planchets) - |+ field beint32 (fun t -> t.noreveal_index) + record (fun purpose h_planchets noreveal_index -> + (purpose, { h_planchets; noreveal_index })) + |+ field Purpose.bin (fun (purpose, _t) -> purpose) + |+ field HashPlanchetsP.bin (fun (_p, t) -> t.h_planchets) + |+ field beint32 (fun (_p, t) -> t.noreveal_index) |> sealr + + let size = + match Size.of_value (Size.size_of bin) with + | Dynamic _ | Unknown -> Fmt.failwith "size_of failure: size is not Static" + | Static n -> n + + let purpose = Purpose.make ~size Taler_signatures.exchange_confirm_withdraw + let bin = map bin (fun (_p, t) -> t) (fun t -> (purpose, t)) end diff --git a/test/dune b/test/dune index 57d2e7e5..1eefa8ef 100644 --- a/test/dune +++ b/test/dune @@ -2,3 +2,10 @@ (name test) (modules test) (libraries mte fmt)) + +; TODO +; cram test? +; gcc -o a.out ./test/signatures.c && ./a.out > a.output +; rm ./a.out +; dune exec ./test/test.exe > b.output +; cmp -l a.output b.output diff --git a/test/signatures.c b/test/signatures.c new file mode 100644 index 00000000..bcebe993 --- /dev/null +++ b/test/signatures.c @@ -0,0 +1,55 @@ +#include +#include + +struct H64 { + uint8_t hash[64]; +}; +struct Hhh { + struct H64 hash; +}; +struct Purpose { + uint32_t size; + uint32_t purpose; +}; + +struct PS { + struct Purpose purpose; + struct Hhh h; + uint32_t noreveal_index; +}; + +int main(void) { + struct Purpose purpose; + struct PS ps; + + purpose.size = 3 * 4 + 64; + purpose.purpose = 1050; + + struct Hhh h = { + .hash = { + .hash = { + 1 , 2 , 3 , 4 , 5, 6, 7, 8, + 9 , 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, + 57, 58, 59, 60, 61, 62, 63, 64 + } + } + }; + + uint32_t noreveal_index = 0; + + ps.purpose = purpose; + ps.h = h; + ps.noreveal_index = noreveal_index; + + + fwrite(&ps, sizeof(ps), 1, stdout); + + // printf("\n"); + + return 0; +} diff --git a/test/test.ml b/test/test.ml index d84a6004..8f233513 100644 --- a/test/test.ml +++ b/test/test.ml @@ -74,9 +74,14 @@ let () = let () = let open Binary_formats.WithdrawConfirmationPS in - let str64 = String.make 64 '0' in + let str64 = String.init 64 (fun i -> Char.unsafe_chr (i + 1)) in let dummy_t = { h_planchets= { v= { v= str64 } }; noreveal_index= 0_l } in let size' = Bin.size_of_value bin dummy_t |> Option.get in assert (size = size'); assert (size = 76); + + (* for cmp test with signatures.c output *) + let raw_str = Bin.to_string bin dummy_t in + Printf.printf "%s" raw_str; + ()