From 553a4cf7d30bd1916c8586cf478d4e4073cf763c Mon Sep 17 00:00:00 2001 From: Todd Nowacki Date: Tue, 17 May 2022 10:38:54 -0700 Subject: [PATCH] [verifier] Migrate struct_with_key tests (#1991) - Migrated to expected output tests --- Cargo.lock | 8 + Cargo.toml | 1 + .../Cargo.toml | 2 +- .../src/test_adapter.rs | 26 ++- .../Cargo.toml | 16 ++ .../src/lib.rs | 6 + .../key_struct_first_field_not_id.exp | 4 + .../key_struct_first_field_not_id.mvir | 10 ++ ...ruct_id_field_incorrect_struct_address.exp | 4 + ...uct_id_field_incorrect_struct_address.mvir | 11 ++ ..._struct_id_field_incorrect_struct_name.exp | 4 + ...struct_id_field_incorrect_struct_name.mvir | 10 ++ .../key_struct_id_field_incorrect_type.exp | 4 + .../key_struct_id_field_incorrect_type.mvir | 10 ++ .../key_struct_id_field_valid.exp | 5 + .../key_struct_id_field_valid.mvir | 10 ++ .../key_struct_second_field_id.exp | 4 + .../key_struct_second_field_id.mvir | 11 ++ .../struct_with_key/key_struct_with_drop.exp | 4 + .../struct_with_key/key_struct_with_drop.mvir | 10 ++ .../tests/tests.rs | 7 + .../struct_with_key_verification_test.rs | 156 ------------------ 22 files changed, 152 insertions(+), 171 deletions(-) create mode 100644 crates/sui-verifier-transactional-tests/Cargo.toml create mode 100644 crates/sui-verifier-transactional-tests/src/lib.rs create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.exp create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.mvir create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.exp create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.mvir create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.exp create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.mvir create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.exp create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.mvir create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.exp create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.mvir create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.exp create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.mvir create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.exp create mode 100644 crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.mvir create mode 100644 crates/sui-verifier-transactional-tests/tests/tests.rs delete mode 100644 crates/sui-verifier/tests/struct_with_key_verification_test.rs diff --git a/Cargo.lock b/Cargo.lock index cca0546e6f002..40fe6ed5c7e13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5680,6 +5680,14 @@ dependencies = [ "sui-types", ] +[[package]] +name = "sui-verifier-transactional-tests" +version = "0.1.0" +dependencies = [ + "datatest-stable", + "sui-transactional-test-runner", +] + [[package]] name = "sui_core" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 61432019504a3..345af9abb9ca5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "crates/sui-transactional-test-runner", "crates/sui-types", "crates/sui-verifier", + "crates/sui-verifier-transactional-tests", "crates/x", "sui", "sui/open_rpc", diff --git a/crates/sui-adapter-transactional-tests/Cargo.toml b/crates/sui-adapter-transactional-tests/Cargo.toml index 8fa4d9867975d..cbbd5f273a5d7 100644 --- a/crates/sui-adapter-transactional-tests/Cargo.toml +++ b/crates/sui-adapter-transactional-tests/Cargo.toml @@ -2,7 +2,7 @@ name = "sui-adapter-transactional-tests" version = "0.1.0" authors = ["Mysten Labs "] -description = "Adapter and accompanying CLI for local sui development" +description = "Transactional tests for Sui Adapter" license = "Apache-2.0" publish = false edition = "2021" diff --git a/crates/sui-transactional-test-runner/src/test_adapter.rs b/crates/sui-transactional-test-runner/src/test_adapter.rs index ede7b2472360c..d39c3196773b0 100644 --- a/crates/sui-transactional-test-runner/src/test_adapter.rs +++ b/crates/sui-transactional-test-runner/src/test_adapter.rs @@ -205,10 +205,6 @@ impl<'a> MoveTestAdapter<'a> for SuiTestAdapter<'a> { extra: Self::ExtraPublishArgs, ) -> anyhow::Result<(Option, CompiledModule)> { let SuiPublishArgs { sender } = extra; - let named_addr = named_addr_opt.expect( - "Cannot publish without a named address. \ - This named address will be associated with the published package", - ); let module_name = module.self_id().name().to_string(); let module_bytes = { let mut buf = vec![]; @@ -234,17 +230,19 @@ impl<'a> MoveTestAdapter<'a> for SuiTestAdapter<'a> { }) .unwrap(); let package_addr = NumericalAddress::new(created_package.into_bytes(), NumberFormat::Hex); - let prev_package = self - .compiled_state - .named_address_mapping - .insert(named_addr.to_string(), package_addr); - match prev_package.map(|a| a.into_inner()) { - Some(addr) if addr != AccountAddress::ZERO => panic!( - "Cannot reuse named address '{}' for multiple packages. \ + if let Some(named_addr) = named_addr_opt { + let prev_package = self + .compiled_state + .named_address_mapping + .insert(named_addr.to_string(), package_addr); + match prev_package.map(|a| a.into_inner()) { + Some(addr) if addr != AccountAddress::ZERO => panic!( + "Cannot reuse named address '{}' for multiple packages. \ It should be set to 0 initially", - named_addr - ), - _ => (), + named_addr + ), + _ => (), + } } let view_events = false; let output = self.object_summary_output(&summary, view_events); diff --git a/crates/sui-verifier-transactional-tests/Cargo.toml b/crates/sui-verifier-transactional-tests/Cargo.toml new file mode 100644 index 0000000000000..8c95db13b7156 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "sui-verifier-transactional-tests" +version = "0.1.0" +authors = ["Mysten Labs "] +description = "Transactional tests for Sui Verifier" +license = "Apache-2.0" +publish = false +edition = "2021" + +[dev-dependencies] +datatest-stable = "0.1.1" +sui-transactional-test-runner = { path = "../sui-transactional-test-runner" } + +[[test]] +name = "tests" +harness = false diff --git a/crates/sui-verifier-transactional-tests/src/lib.rs b/crates/sui-verifier-transactional-tests/src/lib.rs new file mode 100644 index 0000000000000..771dd46ce4af8 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/src/lib.rs @@ -0,0 +1,6 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +#![forbid(unsafe_code)] + +// Empty src/lib.rs to get rusty-tags working. diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.exp b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.exp new file mode 100644 index 0000000000000..160f38f5952bb --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.exp @@ -0,0 +1,4 @@ +processed 1 task + +task 0 'publish'. lines 4-10: +Error: Failed to verify the Move module, reason: "First field of struct S must be 'id', flag found". diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.mvir b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.mvir new file mode 100644 index 0000000000000..8ffcf25a12ea6 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_first_field_not_id.mvir @@ -0,0 +1,10 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//# publish +module 0x0.M { + import 0x2.ID; + struct S has key { + flag: bool + } +} diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.exp b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.exp new file mode 100644 index 0000000000000..b96e8025d7ef1 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.exp @@ -0,0 +1,4 @@ +processed 1 task + +task 0 'publish'. lines 4-11: +Error: Failed to verify the Move module, reason: "First field of struct S must be of type Sui::ID::VersionedID, _::ID::VersionedID type found". diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.mvir b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.mvir new file mode 100644 index 0000000000000..abf46df3f193d --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_address.mvir @@ -0,0 +1,11 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//# publish +module 0x0.ID { + import 0x2.ID; + struct VersionedID has store { flag: bool } + struct S has key { + id: Self.VersionedID + } +} diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.exp b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.exp new file mode 100644 index 0000000000000..1aa4003d88016 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.exp @@ -0,0 +1,4 @@ +processed 1 task + +task 0 'publish'. lines 4-10: +Error: Failed to verify the Move module, reason: "First field of struct S must be of type Sui::ID::VersionedID, Sui::ID::ID type found". diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.mvir b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.mvir new file mode 100644 index 0000000000000..0ef56803c9188 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_struct_name.mvir @@ -0,0 +1,10 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//# publish +module 0x0.M { + import 0x2.ID; + struct S has key { + id: ID.ID + } +} diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.exp b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.exp new file mode 100644 index 0000000000000..e3a2c34d05923 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.exp @@ -0,0 +1,4 @@ +processed 1 task + +task 0 'publish'. lines 4-10: +Error: Failed to verify the Move module, reason: "First field of struct S must be of ID type, Bool type found". diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.mvir b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.mvir new file mode 100644 index 0000000000000..b67456b97f590 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_incorrect_type.mvir @@ -0,0 +1,10 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//# publish +module 0x0.M { + import 0x2.ID; + struct S has key { + id: bool + } +} diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.exp b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.exp new file mode 100644 index 0000000000000..939fe39acec26 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.exp @@ -0,0 +1,5 @@ +processed 1 task + +task 0 'publish'. lines 4-10: +created: object(103) +written: object(102) diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.mvir b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.mvir new file mode 100644 index 0000000000000..59f4da707f4e7 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_id_field_valid.mvir @@ -0,0 +1,10 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//# publish +module 0x0.M { + import 0x2.ID; + struct S has key { + id: ID.VersionedID + } +} diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.exp b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.exp new file mode 100644 index 0000000000000..a2eda753e36f6 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.exp @@ -0,0 +1,4 @@ +processed 1 task + +task 0 'publish'. lines 4-11: +Error: Failed to verify the Move module, reason: "First field of struct S must be 'id', flag found". diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.mvir b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.mvir new file mode 100644 index 0000000000000..8b4d93954291f --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_second_field_id.mvir @@ -0,0 +1,11 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//# publish +module 0x0.M { + import 0x2.ID; + struct S has key { + flag: bool, + id: ID.VersionedID, + } +} diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.exp b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.exp new file mode 100644 index 0000000000000..3a5134d72708b --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.exp @@ -0,0 +1,4 @@ +processed 1 task + +task 0 'publish'. lines 4-10: +Error: Failed to verify the Move module, reason: "Struct S cannot have both key and drop abilities". diff --git a/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.mvir b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.mvir new file mode 100644 index 0000000000000..7af13fc1b96da --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/struct_with_key/key_struct_with_drop.mvir @@ -0,0 +1,10 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//# publish +module 0x0.M { + import 0x2.ID; + struct S has key, drop { + flag: bool + } +} diff --git a/crates/sui-verifier-transactional-tests/tests/tests.rs b/crates/sui-verifier-transactional-tests/tests/tests.rs new file mode 100644 index 0000000000000..5e82698a7fb74 --- /dev/null +++ b/crates/sui-verifier-transactional-tests/tests/tests.rs @@ -0,0 +1,7 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +pub const TEST_DIR: &str = "tests"; +use sui_transactional_test_runner::run_test; + +datatest_stable::harness!(run_test, TEST_DIR, r".*\.(mvir|move)$"); diff --git a/crates/sui-verifier/tests/struct_with_key_verification_test.rs b/crates/sui-verifier/tests/struct_with_key_verification_test.rs deleted file mode 100644 index fc9a32c2ccf12..0000000000000 --- a/crates/sui-verifier/tests/struct_with_key_verification_test.rs +++ /dev/null @@ -1,156 +0,0 @@ -// Copyright (c) 2021, Facebook, Inc. and its affiliates -// Copyright (c) 2022, Mysten Labs, Inc. -// SPDX-License-Identifier: Apache-2.0 - -mod common; - -pub use common::module_builder::ModuleBuilder; -use move_binary_format::file_format::*; -use move_core_types::account_address::AccountAddress; -use sui_types::SUI_FRAMEWORK_ADDRESS; -use sui_verifier::struct_with_key_verifier::verify_module; - -#[test] -fn key_struct_with_drop() { - let (mut module, id_struct) = ModuleBuilder::default(); - module.add_struct( - module.get_self_index(), - "S", - AbilitySet::EMPTY | Ability::Key | Ability::Drop, - vec![("id", SignatureToken::Struct(id_struct.handle))], - ); - assert!(verify_module(module.get_module()) - .unwrap_err() - .to_string() - .contains("Struct S cannot have both key and drop abilities")); -} - -#[test] -fn non_key_struct_without_fields() { - let (mut module, _) = ModuleBuilder::default(); - module.add_struct(module.get_self_index(), "S", AbilitySet::EMPTY, vec![]); - assert!(verify_module(module.get_module()).is_ok()); -} - -#[test] -fn key_struct_without_fields() { - let (mut module, _) = ModuleBuilder::default(); - module.add_struct( - module.get_self_index(), - "S", - AbilitySet::EMPTY | Ability::Key, - vec![], - ); - assert!(verify_module(module.get_module()) - .unwrap_err() - .to_string() - .contains("First field of struct S must be 'id', no field found")); -} - -#[test] -fn key_struct_first_field_not_id() { - let (mut module, id_struct) = ModuleBuilder::default(); - module.add_struct( - module.get_self_index(), - "S", - AbilitySet::EMPTY | Ability::Key, - vec![("foo", SignatureToken::Struct(id_struct.handle))], - ); - assert!(verify_module(module.get_module()) - .unwrap_err() - .to_string() - .contains("First field of struct S must be 'id', foo found")); -} - -#[test] -fn key_struct_second_field_id() { - let (mut module, id_struct) = ModuleBuilder::default(); - module.add_struct( - module.get_self_index(), - "S", - AbilitySet::EMPTY | Ability::Key, - vec![ - ("foo", SignatureToken::Struct(id_struct.handle)), - ("id", SignatureToken::Struct(id_struct.handle)), - ], - ); - assert!(verify_module(module.get_module()) - .unwrap_err() - .to_string() - .contains("First field of struct S must be 'id', foo found")); -} - -#[test] -fn key_struct_id_field_incorrect_type() { - let (mut module, _) = ModuleBuilder::default(); - module.add_struct( - module.get_self_index(), - "S", - AbilitySet::EMPTY | Ability::Key, - vec![("id", SignatureToken::U64)], - ); - assert!(verify_module(module.get_module()) - .unwrap_err() - .to_string() - .contains("First field of struct S must be of ID type, U64 type found")); -} - -#[test] -fn key_struct_id_field_incorrect_struct_address() { - let (mut module, _) = ModuleBuilder::default(); - let addr = AccountAddress::new([1u8; AccountAddress::LENGTH]); - let new_module_idx = module.add_module(addr, "ID"); - let fake_id_struct = module.add_struct( - new_module_idx, - "VersionedID", - AbilitySet::EMPTY | Ability::Store | Ability::Drop, - vec![], - ); - module.add_struct( - new_module_idx, - "S", - AbilitySet::EMPTY | Ability::Key, - vec![("id", SignatureToken::Struct(fake_id_struct.handle))], - ); - let err_str = verify_module(module.get_module()).unwrap_err().to_string(); - assert!(err_str.contains(&format!( - "First field of struct S must be of type {}::ID::VersionedID, {}::ID::VersionedID type found", - SUI_FRAMEWORK_ADDRESS, addr - ))); -} - -#[test] -fn key_struct_id_field_incorrect_struct_name() { - let (mut module, _) = ModuleBuilder::default(); - let fake_id_struct = module.add_struct( - module.get_self_index(), - "FOO", - AbilitySet::EMPTY | Ability::Store | Ability::Drop, - vec![], - ); - module.add_struct( - module.get_self_index(), - "S", - AbilitySet::EMPTY | Ability::Key, - vec![("id", SignatureToken::Struct(fake_id_struct.handle))], - ); - assert!(verify_module(module.get_module()) - .unwrap_err() - .to_string() - .contains(&format!( - "First field of struct S must be of type {0}::ID::VersionedID, {0}::ID::FOO type found", - SUI_FRAMEWORK_ADDRESS - ))); -} - -#[test] -fn key_struct_id_field_valid() { - let (mut module, id_struct) = ModuleBuilder::default(); - module.add_struct( - module.get_self_index(), - "S", - AbilitySet::EMPTY | Ability::Key, - vec![("id", SignatureToken::Struct(id_struct.handle))], - ); - assert!(verify_module(module.get_module()).is_ok()); -}