From 5804a93d4baef68883d37df1de20e39ad721aa69 Mon Sep 17 00:00:00 2001 From: Gauthier SEBILLE Date: Fri, 5 May 2023 11:22:54 +0200 Subject: [PATCH 1/2] DAC: make Dac_plugin.raw_hash encoding JSON only DAC: add ppx_expect inline test for raw_hash encoding DAC: remove useless change Revert "DAC: remove useless change" This reverts commit 1b3a9e4d22a03fc89638a02a38ad8e988fe49f22. DAC: remove useless change DAC: trying to revert.. Revert "DAC: trying to revert.." This reverts commit 0951410f29f7bb040fc9e533ade54cf0637afc29. DAC: revert snoop-command.t DAC: switch from ppx_expect to classic tests suite DAC: remove ppx_expect --- manifest/main.ml | 2 +- src/lib_dac/dac_plugin.ml | 12 +++- src/lib_dac/test/dune | 2 +- src/lib_dac/test/test_dac_plugin.ml | 92 +++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/lib_dac/test/test_dac_plugin.ml diff --git a/manifest/main.ml b/manifest/main.ml index 6f750b2af855..5bc04e1d536f 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -3960,7 +3960,7 @@ let _octez_dac_node_lib_tests = let _octez_dac_lib_tests = tezt - ["test_certificate"] + ["test_certificate"; "test_dac_plugin"] ~path:"src/lib_dac/test" ~opam:"tezos-dac-lib-test" ~synopsis:"Test for dac lib" diff --git a/src/lib_dac/dac_plugin.ml b/src/lib_dac/dac_plugin.ml index ad5abcfbda6a..b6408281c638 100644 --- a/src/lib_dac/dac_plugin.ml +++ b/src/lib_dac/dac_plugin.ml @@ -38,7 +38,17 @@ type supported_hashes = Blake2B let raw_compare = Bytes.compare -let raw_hash_encoding = Data_encoding.bytes' Hex +let raw_hash_encoding = + let open Data_encoding in + union + [ + case + Json_only + Data_encoding.bytes + ~title:"raw_hash" + (fun raw_hash -> Some raw_hash) + (fun raw_hash -> raw_hash); + ] let hash_to_raw = Fun.id diff --git a/src/lib_dac/test/dune b/src/lib_dac/test/dune index 6a36bf519e4c..e1248db8c06b 100644 --- a/src/lib_dac/test/dune +++ b/src/lib_dac/test/dune @@ -26,7 +26,7 @@ -open Tezos_base_test_helpers -open Tezos_dac_lib -open Octez_alcotezt) - (modules test_certificate)) + (modules test_certificate test_dac_plugin)) (executable (name main) diff --git a/src/lib_dac/test/test_dac_plugin.ml b/src/lib_dac/test/test_dac_plugin.ml new file mode 100644 index 000000000000..37ed5307081e --- /dev/null +++ b/src/lib_dac/test/test_dac_plugin.ml @@ -0,0 +1,92 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2023 Marigold *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** Testing + ------- + Component: Lib_dac Dac_plugin + Invocation: dune exec src/lib_dac/test/main.exe -- --file test_dac_plugin.ml + Subject: Tests for the Dac_plugin.raw_hash. +*) + +let test_encode_decode_json () = + let open Lwt_result_syntax in + let raw_hash = + Dac_plugin.raw_hash_of_bytes + (Bytes.of_string + "00dce42551fb786c51b29b723f4abba3ea04eb3d239a9a59ec5a5434e151e105e4") + in + let encoded_raw_hash = + Data_encoding.Json.construct Dac_plugin.raw_hash_encoding raw_hash + in + let decoded_raw_hash = + Data_encoding.Json.destruct Dac_plugin.raw_hash_encoding encoded_raw_hash + in + return @@ assert (raw_hash = decoded_raw_hash) + +let test_cannot_encode_binary () = + let open Lwt_result_syntax in + let raw_hash = + Dac_plugin.raw_hash_of_bytes + (Bytes.of_string + "00dce42551fb786c51b29b723f4abba3ea04eb3d239a9a59ec5a5434e151e105e4") + in + let encoded_raw_hash = + Data_encoding.Binary.to_string Dac_plugin.raw_hash_encoding raw_hash + in + match encoded_raw_hash with + | Error _ -> return @@ assert true + | Ok _ -> return @@ assert false + +let test_cannot_decode_binary () = + let open Lwt_result_syntax in + let raw_hash = + "00dce42551fb786c51b29b723f4abba3ea04eb3d239a9a59ec5a5434e151e105e4" + in + let encoded_raw_hash = + Data_encoding.Binary.of_string Dac_plugin.raw_hash_encoding raw_hash + in + match encoded_raw_hash with + | Error _ -> return @@ assert true + | Ok _ -> return @@ assert false + +let tests = + [ + Tztest.tztest + "Encode and decode to JSON leads to the same Dac_plugin.raw_hash" + `Quick + test_encode_decode_json; + Tztest.tztest + "Cannot encode a raw_hash to binary" + `Quick + test_cannot_encode_binary; + Tztest.tztest + "Cannot decode a raw_hash to binary" + `Quick + test_cannot_decode_binary; + ] + +let () = + Alcotest_lwt.run ~__FILE__ "lib_dac" [("Dac_plugin.ml", tests)] + |> Lwt_main.run -- GitLab From e8902ebef24ffbc05fdad6690a173f5a337adbce Mon Sep 17 00:00:00 2001 From: Gauthier SEBILLE Date: Wed, 17 May 2023 15:56:49 +0200 Subject: [PATCH 2/2] DAC: rework return of tests + get rid of Lwt --- src/lib_dac/test/test_dac_plugin.ml | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/lib_dac/test/test_dac_plugin.ml b/src/lib_dac/test/test_dac_plugin.ml index 37ed5307081e..9aec42672244 100644 --- a/src/lib_dac/test/test_dac_plugin.ml +++ b/src/lib_dac/test/test_dac_plugin.ml @@ -30,8 +30,9 @@ Subject: Tests for the Dac_plugin.raw_hash. *) +(* On all the following tests, the raw_hash used is coming from + tezt/tests/dac_example_payloads/preimage.json *) let test_encode_decode_json () = - let open Lwt_result_syntax in let raw_hash = Dac_plugin.raw_hash_of_bytes (Bytes.of_string @@ -43,10 +44,9 @@ let test_encode_decode_json () = let decoded_raw_hash = Data_encoding.Json.destruct Dac_plugin.raw_hash_encoding encoded_raw_hash in - return @@ assert (raw_hash = decoded_raw_hash) + assert (raw_hash = decoded_raw_hash) let test_cannot_encode_binary () = - let open Lwt_result_syntax in let raw_hash = Dac_plugin.raw_hash_of_bytes (Bytes.of_string @@ -55,38 +55,31 @@ let test_cannot_encode_binary () = let encoded_raw_hash = Data_encoding.Binary.to_string Dac_plugin.raw_hash_encoding raw_hash in - match encoded_raw_hash with - | Error _ -> return @@ assert true - | Ok _ -> return @@ assert false + match encoded_raw_hash with Error _ -> () | Ok _ -> assert false let test_cannot_decode_binary () = - let open Lwt_result_syntax in let raw_hash = "00dce42551fb786c51b29b723f4abba3ea04eb3d239a9a59ec5a5434e151e105e4" in let encoded_raw_hash = Data_encoding.Binary.of_string Dac_plugin.raw_hash_encoding raw_hash in - match encoded_raw_hash with - | Error _ -> return @@ assert true - | Ok _ -> return @@ assert false + match encoded_raw_hash with Error _ -> () | Ok _ -> assert false let tests = [ - Tztest.tztest + Alcotest.test_case "Encode and decode to JSON leads to the same Dac_plugin.raw_hash" `Quick test_encode_decode_json; - Tztest.tztest + Alcotest.test_case "Cannot encode a raw_hash to binary" `Quick test_cannot_encode_binary; - Tztest.tztest + Alcotest.test_case "Cannot decode a raw_hash to binary" `Quick test_cannot_decode_binary; ] -let () = - Alcotest_lwt.run ~__FILE__ "lib_dac" [("Dac_plugin.ml", tests)] - |> Lwt_main.run +let () = Alcotest.run ~__FILE__ "lib_dac" [("Dac_plugin.ml", tests)] -- GitLab