diff --git a/manifest/main.ml b/manifest/main.ml index 6f750b2af855f41df8a47da96f04ed54ff3c0d7e..5bc04e1d536fa85bd2e91b2c7f14b64140d8203e 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 ad5abcfbda6a3b7f3b2ab67331dc563bf91a6143..b6408281c638e95410f7e648ea4d455bf5861dab 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 6a36bf519e4c842b67d9d2f30e477a3c004385d8..e1248db8c06b82f7256ac0c04725c107b3842b47 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 0000000000000000000000000000000000000000..9aec426722447e1e23ec06cb571e4b7d7e16550f --- /dev/null +++ b/src/lib_dac/test/test_dac_plugin.ml @@ -0,0 +1,85 @@ +(*****************************************************************************) +(* *) +(* 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. +*) + +(* 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 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 + assert (raw_hash = decoded_raw_hash) + +let test_cannot_encode_binary () = + 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 _ -> () | Ok _ -> assert false + +let test_cannot_decode_binary () = + 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 _ -> () | Ok _ -> assert false + +let tests = + [ + Alcotest.test_case + "Encode and decode to JSON leads to the same Dac_plugin.raw_hash" + `Quick + test_encode_decode_json; + Alcotest.test_case + "Cannot encode a raw_hash to binary" + `Quick + test_cannot_encode_binary; + Alcotest.test_case + "Cannot decode a raw_hash to binary" + `Quick + test_cannot_decode_binary; + ] + +let () = Alcotest.run ~__FILE__ "lib_dac" [("Dac_plugin.ml", tests)]