From 729f2acab467dc1af657a357b0747ec3e07d2e93 Mon Sep 17 00:00:00 2001 From: mihaicalinluca Date: Fri, 13 Oct 2023 13:29:02 +0200 Subject: [PATCH] added order condition and unit tests --- .../contract/output_contract/oc_validate.rs | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/framework/meta/src/cmd/contract/output_contract/oc_validate.rs b/framework/meta/src/cmd/contract/output_contract/oc_validate.rs index fe7a701049..81dcccc94f 100644 --- a/framework/meta/src/cmd/contract/output_contract/oc_validate.rs +++ b/framework/meta/src/cmd/contract/output_contract/oc_validate.rs @@ -19,12 +19,13 @@ fn check_single_constructor(output_contract: &OutputContract) -> Result<(), Stri /// Note: promise callbacks not included, since they have `#[call_value]` arguments, that are currently not modelled. fn validate_contract_var_args(abi: &ContractAbi) -> Result<(), String> { for endpoint_abi in abi.constructors.iter().chain(abi.endpoints.iter()) { - validate_endpoint_var_args(endpoint_abi)?; + validate_endpoint_var_args_number(endpoint_abi)?; + validate_endpoint_var_args_order(endpoint_abi)?; } Ok(()) } -fn validate_endpoint_var_args(endpoint_abi: &EndpointAbi) -> Result<(), String> { +fn validate_endpoint_var_args_number(endpoint_abi: &EndpointAbi) -> Result<(), String> { let num_var_args = endpoint_abi .inputs .iter() @@ -35,6 +36,22 @@ fn validate_endpoint_var_args(endpoint_abi: &EndpointAbi) -> Result<(), String> "Multiple var args found in {}. Use #[allow_multiple_var_args] if you want to enable this feature", &endpoint_abi.rust_method_name)); } + + Ok(()) +} + +fn validate_endpoint_var_args_order(endpoint_abi: &EndpointAbi) -> Result<(), String> { + let mut var_args_encountered = false; + for arg in &endpoint_abi.inputs { + if arg.multi_arg { + var_args_encountered = true; + } else if var_args_encountered { + return Err(format!( + "Found regular arguments after var-args in method {}. This is not allowed, because it makes it impossible to parse the arguments.", + &endpoint_abi.rust_method_name)); + } + } + Ok(()) } @@ -45,7 +62,7 @@ mod tests { use super::*; #[test] - fn validate_endpoint_var_args_test() { + fn validate_endpoint_var_args_number_test() { let mut endpoint_def = EndpointAbi::default(); let var_arg_1 = InputAbi { arg_name: "arg_1", @@ -63,9 +80,36 @@ mod tests { assert!(!endpoint_def.allow_multiple_var_args); assert_eq!(Err(format!( "Multiple var args found in {}. Use #[allow_multiple_var_args] if you want to enable this feature", - &endpoint_def.rust_method_name)), validate_endpoint_var_args(&endpoint_def)); + &endpoint_def.rust_method_name)), validate_endpoint_var_args_number(&endpoint_def)); endpoint_def.allow_multiple_var_args = true; - assert_eq!(Ok(()), validate_endpoint_var_args(&endpoint_def)); + assert_eq!(Ok(()), validate_endpoint_var_args_number(&endpoint_def)); + } + + #[test] + fn validate_endpoint_var_args_order_test() { + let mut endpoint_def = EndpointAbi::default(); + let arg = InputAbi { + arg_name: "arg_1", + type_name: TypeName::new(), + multi_arg: false, + }; + let var_arg_1 = InputAbi { + arg_name: "arg_2", + type_name: TypeName::new(), + multi_arg: true, + }; + + endpoint_def.inputs.push(var_arg_1.clone()); + endpoint_def.inputs.push(arg.clone()); + assert_eq!(Err(format!( + "Found regular arguments after var-args in method {}. This is not allowed, because it makes it impossible to parse the arguments.", + &endpoint_def.rust_method_name)), validate_endpoint_var_args_order(&endpoint_def)); + + endpoint_def.inputs.clear(); + + endpoint_def.inputs.push(arg); + endpoint_def.inputs.push(var_arg_1); + assert_eq!(Ok(()), validate_endpoint_var_args_order(&endpoint_def)); } }