Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: #312 Add test cases for MaxEncodedSchemaLimitExceeded error #543

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pallets/schema/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,54 @@ fn test_schema_lookup() {
}
});
}
#[test]
fn test_schema_creation_exceeds_max_encoded_limit() {
let creator = DID_00;
let author = ACCOUNT_00;

let raw_space = [2u8; 256].to_vec();
let space_digest = <Test as frame_system::Config>::Hashing::hash(&raw_space.encode()[..]);
let space_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_digest.encode()[..], &creator.encode()[..]].concat()[..],
);
let space_id: SpaceIdOf = generate_space_id::<Test>(&space_id_digest);

let max_schema_data = vec![2u8; MaxEncodedSchemaLength::get() as usize];
let max_schema: BoundedVec<u8, MaxEncodedSchemaLength> = BoundedVec::try_from(max_schema_data)
.expect("Schema at max length should be convertible to BoundedVec");

let too_large_schema_data = vec![2u8; MaxEncodedSchemaLength::get() as usize + 1];

let auth_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..],
);
let authorization_id: Ss58Identifier = generate_authorization_id::<Test>(&auth_digest);

new_test_ext().execute_with(|| {
assert_ok!(Space::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
space_digest,
));
assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, 10));

assert_ok!(Schema::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
max_schema,
authorization_id.clone()
));

assert!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assert_err with the error code instead of manual assertion.

Copy link
Contributor Author

@Vaibhavsahu2810 Vaibhavsahu2810 Dec 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @vatsa287 ,when i am trying to use assert_err, panick get caused, the schema data can't even be converted to a BoundedVec because it exceeds the size limit, so we never get to the point of calling Schema::create
image

BoundedVec::<u8, MaxEncodedSchemaLength>::try_from(too_large_schema_data.clone())
.is_err(),
"BoundedVec conversion should fail for oversized schema"
);

let result = Schema::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
BoundedVec::try_from(too_large_schema_data).unwrap_or_default(),
authorization_id.clone(),
);

assert!(result.is_err(), "Schema creation with oversized data should fail");
});
}
Loading