From b2f811dd7b46735f953cc2651704c4a32335f524 Mon Sep 17 00:00:00 2001 From: Om Thorat Date: Thu, 7 Nov 2024 20:59:25 +0530 Subject: [PATCH] feat: Added tests for UnauthorizedOperation Signed-off-by: Om Thorat --- pallets/asset/src/tests.rs | 220 ++++++++++++++++++++++++++++++++++++- 1 file changed, 219 insertions(+), 1 deletion(-) diff --git a/pallets/asset/src/tests.rs b/pallets/asset/src/tests.rs index f52ee2ce..d6515f9e 100644 --- a/pallets/asset/src/tests.rs +++ b/pallets/asset/src/tests.rs @@ -31,8 +31,9 @@ pub fn generate_asset_instance_id(digest: &SpaceCodeOf) -> AssetIn } pub(crate) const DID_00: SubjectId = SubjectId(AccountId32::new([1u8; 32])); -pub(crate) const DID_01: SubjectId = SubjectId(AccountId32::new([1u8; 32])); +pub(crate) const DID_01: SubjectId = SubjectId(AccountId32::new([2u8; 32])); pub(crate) const ACCOUNT_00: AccountId = AccountId::new([1u8; 32]); +pub(crate) const ACCOUNT_01: AccountId = AccountId::new([2u8; 32]); #[test] fn asset_create_should_succeed() { @@ -150,6 +151,223 @@ fn asset_create_duplicate_should_fail() { }); } +#[test] +fn asset_unauthorized_operation_should_fail() { + let creator = DID_00; + let unauthorized_creator = DID_01; + let unauthorized_author = ACCOUNT_01; + let author = ACCOUNT_00; + let capacity = 5u64; + + let raw_space = [2u8; 256].to_vec(); + let space_digest = ::Hashing::hash(&raw_space.encode()[..]); + let space_id_digest = ::Hashing::hash( + &[&space_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let space_id: SpaceIdOf = generate_space_id::(&space_id_digest); + + let auth_digest = ::Hashing::hash( + &[&space_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); + + let unauthorized_space_id_digest = ::Hashing::hash( + &[&space_digest.encode()[..], &unauthorized_creator.encode()[..]].concat()[..], + ); + let unauthorized_space_id: SpaceIdOf = generate_space_id::(&unauthorized_space_id_digest); + + let unauthorized_auth_digest = ::Hashing::hash( + &[ + &unauthorized_space_id.encode()[..], + &unauthorized_creator.encode()[..], + &unauthorized_creator.encode()[..], + ] + .concat()[..], + ); + + let unauthorized_authorization_id: Ss58Identifier = + generate_authorization_id::(&unauthorized_auth_digest); + + let asset_desc = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_tag = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_meta = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_qty = 10; + let asset_value = 10; + let asset_type = AssetTypeOf::MF; + + let entry = AssetInputEntryOf:: { + asset_desc, + asset_qty, + asset_type, + asset_value, + asset_tag, + asset_meta, + }; + + let digest = ::Hashing::hash(&[&entry.encode()[..]].concat()[..]); + + let issue_id_digest = ::Hashing::hash( + &[&digest.encode()[..], &space_id.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let asset_id: Ss58Identifier = generate_asset_id::(&issue_id_digest); + + let issue_entry = AssetIssuanceEntryOf:: { + asset_id: asset_id.clone(), + asset_owner: creator.clone(), + asset_issuance_qty: Some(10), + }; + + let issue_entry_digest = + ::Hashing::hash(&[&issue_entry.encode()[..]].concat()[..]); + + let instance_id_digest = ::Hashing::hash( + &[ + &asset_id.encode()[..], + &creator.encode()[..], + &space_id.encode()[..], + &creator.encode()[..], + &issue_entry_digest.encode()[..], + ] + .concat()[..], + ); + + let instance_id = generate_asset_instance_id::(&instance_id_digest); + + let transfer_entry = AssetTransferEntryOf:: { + asset_id: asset_id.clone(), + asset_instance_id: instance_id.clone(), + asset_owner: creator.clone(), + new_asset_owner: unauthorized_creator.clone(), + }; + + let unauthorized_transfer_entry = AssetTransferEntryOf:: { + asset_id: asset_id.clone(), + asset_instance_id: instance_id.clone(), + asset_owner: unauthorized_creator.clone(), + new_asset_owner: unauthorized_creator.clone(), + }; + + let unauthorized_entry_digest = ::Hashing::hash( + &[&unauthorized_transfer_entry.encode()[..]].concat()[..], + ); + + let transfer_entry_digest = + ::Hashing::hash(&[&transfer_entry.encode()[..]].concat()[..]); + + new_test_ext().execute_with(|| { + assert_ok!(Space::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + space_digest, + )); + + assert_ok!(Space::create( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + space_digest, + )); + + assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, capacity)); + + assert_ok!(Space::approve(RawOrigin::Root.into(), unauthorized_space_id, capacity)); + + assert_ok!(Asset::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + entry, + digest, + authorization_id.clone() + )); + + assert_err!(Asset::issue( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), // Unauthorized author attempting the action + issue_entry.clone(), + issue_entry_digest, + unauthorized_authorization_id.clone() + ), Error::::UnauthorizedOperation); + + assert_ok!(Asset::issue( + DoubleOrigin(author.clone(), creator.clone()).into(), + issue_entry.clone(), + issue_entry_digest, + authorization_id.clone() + )); + + assert_err!(Asset::transfer( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + transfer_entry.clone(), + transfer_entry_digest, + ), + Error::::UnauthorizedOperation + ); + + assert_err!(Asset::transfer( + DoubleOrigin(author.clone(), creator.clone()).into(), + unauthorized_transfer_entry.clone(), + unauthorized_entry_digest, + ), + Error::::UnauthorizedOperation + ); + + assert_err!(Asset::status_change( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + asset_id.clone(), + Some(instance_id.clone()), + AssetStatusOf::INACTIVE + ), + Error::::UnauthorizedOperation + ); + + assert_ok!(Asset::vc_create( + DoubleOrigin(author.clone(), creator.clone()).into(), + asset_qty, + digest, + authorization_id.clone() + )); + + assert_err!(Asset::vc_issue( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + issue_entry.clone(), + issue_entry_digest, + unauthorized_authorization_id + ), + Error::::UnauthorizedOperation + ); + + assert_ok!(Asset::vc_issue( + DoubleOrigin(author.clone(), creator.clone()).into(), + issue_entry, + issue_entry_digest, + authorization_id + )); + + assert_err!(Asset::vc_transfer( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + transfer_entry.clone(), + transfer_entry_digest, + ), + Error::::UnauthorizedOperation + ); + + assert_err!(Asset::vc_transfer( + DoubleOrigin(author.clone(), creator.clone()).into(), + unauthorized_transfer_entry.clone(), + unauthorized_entry_digest, + ), + Error::::UnauthorizedOperation + ); + + assert_err!(Asset::vc_status_change( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + asset_id.clone(), + Some(instance_id.clone()), + AssetStatusOf::INACTIVE + ), + Error::::UnauthorizedOperation + ); + + }); +} + #[test] fn asset_issue_should_succeed() { let creator = DID_00;