diff --git a/src/test/contracts/replaceable_id.ligo b/src/test/contracts/replaceable_id.ligo new file mode 100644 index 000000000..3b116e69b --- /dev/null +++ b/src/test/contracts/replaceable_id.ligo @@ -0,0 +1,35 @@ +// storage type +type storage_t is address; + +// entry points parameter types +type change_addr_pt is address + +type message_t is list (operation) ; +type pass_message_pt is (unit -> message_t) + +type contract_return_t is (list(operation) * storage_t) + +type entry_point_t is +| Change_address of change_addr_pt +| Pass_message of pass_message_pt + +function change_address (const param : change_addr_pt; + const s : storage_t) : contract_return_t is + begin + if sender =/= s then failwith("Unauthorized sender") + else skip +end with ((nil : list(operation)), param) + +function pass_message ( const param: pass_message_pt; + const s : storage_t ) : contract_return_t is + begin + if sender =/= s then failwith("Unauthorized sender") else skip ; + var message : pass_message_pt := param ; +end with (param(unit),s) + +function main(const param : entry_point_t; const s : storage_t) : contract_return_t is +block {skip} with + case param of + | Change_address (p) -> change_address(p,s) + | Pass_message (p) -> pass_message(p,s) +end \ No newline at end of file diff --git a/src/test/replaceable_id_tests.ml b/src/test/replaceable_id_tests.ml new file mode 100644 index 000000000..ca04640c1 --- /dev/null +++ b/src/test/replaceable_id_tests.ml @@ -0,0 +1,90 @@ +open Trace +open Test_helpers + +let type_file f = + let%bind simplified = Ligo.Compile.Of_source.compile f (Syntax_name "pascaligo") in + let%bind typed,state = Ligo.Compile.Of_simplified.compile simplified in + ok @@ (typed,state) + +let get_program = + let s = ref None in + fun () -> match !s with + | Some s -> ok s + | None -> ( + let%bind program = type_file "./contracts/replaceable_id.ligo" in + s := Some program ; + ok program + ) + +let compile_main () = + let%bind simplified = Ligo.Compile.Of_source.compile "./contracts/replaceable_id.ligo" (Syntax_name "pascaligo") in + let%bind typed_prg,_ = Ligo.Compile.Of_simplified.compile simplified in + let%bind mini_c_prg = Ligo.Compile.Of_typed.compile typed_prg in + let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg "main" in + let%bind (_contract: Tezos_utils.Michelson.michelson) = + (* fails if the given entry point is not a valid contract *) + Ligo.Compile.Of_mini_c.build_contract michelson_prg in + ok () +open Ast_simplified + +let empty_op_list = + (e_typed_list [] t_operation) +let empty_message = e_lambda (Var.of_name "arguments") + (Some t_unit) (Some (t_list t_operation)) + empty_op_list + +let storage id = e_address @@ addr id +let entry_change_addr id = e_constructor "Change_address" + @@ e_address @@ addr @@ id +let entry_pass_message = e_constructor "Pass_message" + @@ empty_message + +let change_addr_success () = + let%bind program,_ = get_program () in + let init_storage = storage 1 in + let param = entry_change_addr 2 in + let options = + let source = contract 1 in + Proto_alpha_utils.Memory_proto_alpha.make_options ~source () in + expect_eq ~options program "main" + (e_pair param init_storage) (e_pair empty_op_list (storage 2)) + +let change_addr_fail () = + let%bind program,_ = get_program () in + let init_storage = storage 1 in + let param = entry_change_addr 2 in + let options = + let source = contract 3 in + Proto_alpha_utils.Memory_proto_alpha.make_options ~source () in + let exp_failwith = "Unauthorized sender" in + expect_string_failwith ~options program "main" + (e_pair param init_storage) exp_failwith + +let pass_message_success () = + let%bind program,_ = get_program () in + let init_storage = storage 1 in + let param = entry_pass_message in + let options = + let source = contract 1 in + Proto_alpha_utils.Memory_proto_alpha.make_options ~source () in + expect_eq ~options program "main" + (e_pair param init_storage) (e_pair empty_op_list init_storage) + +let pass_message_fail () = + let%bind program,_ = get_program () in + let init_storage = storage 1 in + let param = entry_pass_message in + let options = + let source = contract 2 in + Proto_alpha_utils.Memory_proto_alpha.make_options ~source () in + let exp_failwith = "Unauthorized sender" in + expect_string_failwith ~options program "main" + (e_pair param init_storage) exp_failwith + +let main = test_suite "Replaceable ID" [ + test "compile" compile_main ; + test "change_addr_success" change_addr_success ; + test "change_addr_fail" change_addr_fail ; + test "pass_message_success" pass_message_success ; + test "pass_message_fail" pass_message_fail ; + ] diff --git a/src/test/test.ml b/src/test/test.ml index 4db386e5e..b1cafc9cf 100644 --- a/src/test/test.ml +++ b/src/test/test.ml @@ -12,5 +12,6 @@ let () = Vote_tests.main ; Multisig_tests.main ; Multisig_v2_tests.main ; + Replaceable_id_tests.main ; ] ; ()