From 5e8aa3633b0814857292da061e5e844bb6a457ed Mon Sep 17 00:00:00 2001 From: HarishKarthickS Date: Thu, 25 Apr 2024 17:49:07 +0530 Subject: [PATCH 1/3] Adding Test max encoded schema limit exceeded to pallet/schema --- pallets/schema/src/tests.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pallets/schema/src/tests.rs b/pallets/schema/src/tests.rs index 24715895b..647c8931e 100644 --- a/pallets/schema/src/tests.rs +++ b/pallets/schema/src/tests.rs @@ -314,3 +314,33 @@ fn test_schema_lookup() { } }); } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_max_encoded_schema_limit_exceeded() { + // Arrange: Set up test environment + let mut mock_storage = new_test_ext().0; // Initialize mock storage + + // Replace MAX_SCHEMA_SIZE with the actual limit from your code + const MAX_SCHEMA_SIZE: usize = 1024; // Replace with the actual value + + // Act: Perform the action that might trigger the error + let mut large_schema_data = vec![0u8; MAX_SCHEMA_SIZE + 1]; // Create data exceeding the limit + + // Replace some_function_that_encodes_schema with the actual function + let result = Schema::create( + Origin::signed(DID_00), // Replace with appropriate origin type based on your pallet + large_schema_data.clone(), + Default::default(), // Replace with any additional arguments if needed + ); + + // Assert: Verify the expected error + assert_eq!( + result.err().unwrap(), + >::MaxEncodedSchemaLimitExceeded, // Replace with the appropriate error type + ); + } +} From 63d1ff361d68a6bd1d93016679e21d431a899b3c Mon Sep 17 00:00:00 2001 From: HarishKarthickS Date: Thu, 25 Apr 2024 18:14:08 +0530 Subject: [PATCH 2/3] Adding Test max encoded schema limit exceeded to pallet/schema --- pallets/schema/src/tests.rs | 47 ++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/pallets/schema/src/tests.rs b/pallets/schema/src/tests.rs index 647c8931e..44026afaa 100644 --- a/pallets/schema/src/tests.rs +++ b/pallets/schema/src/tests.rs @@ -317,30 +317,45 @@ fn test_schema_lookup() { #[cfg(test)] mod tests { + // This module contains unit tests for the `pallet/schema` module. + // Unit tests are isolated tests that focus on the functionality of a single module + // and are typically run during development and testing to ensure the module + // behaves as expected. + // The `#[cfg(test)]` attribute ensures this module is only compiled when the + // `test` feature is enabled. use super::*; + use mock::MockStorage; // Assuming you have a mock for storage #[test] - fn test_max_encoded_schema_limit_exceeded() { - // Arrange: Set up test environment - let mut mock_storage = new_test_ext().0; // Initialize mock storage - - // Replace MAX_SCHEMA_SIZE with the actual limit from your code + fn test_max_encoded_schema_limit_exceeded_create() { + // Arrange + let mut mock_storage = MockStorage::new(); const MAX_SCHEMA_SIZE: usize = 1024; // Replace with the actual value - // Act: Perform the action that might trigger the error - let mut large_schema_data = vec![0u8; MAX_SCHEMA_SIZE + 1]; // Create data exceeding the limit - - // Replace some_function_that_encodes_schema with the actual function + // Act + let large_schema_data = vec![0u8; MAX_SCHEMA_SIZE + 1]; let result = Schema::create( - Origin::signed(DID_00), // Replace with appropriate origin type based on your pallet + Origin::signed(DID_00), // Replace with appropriate origin type large_schema_data.clone(), - Default::default(), // Replace with any additional arguments if needed + Default::default(), ); - // Assert: Verify the expected error - assert_eq!( - result.err().unwrap(), - >::MaxEncodedSchemaLimitExceeded, // Replace with the appropriate error type - ); + // Assert + assert_eq!(result.err().unwrap(), Error::::MaxEncodedSchemaLimitExceeded,); + } + + // Add similar test cases for other functions that might return the error + + #[test] + fn test_max_encoded_schema_limit_exceeded_edge_case() { + // Arrange (similar to above) + + // Act + let almost_large_data = vec![0u8; MAX_SCHEMA_SIZE - 1]; + let result = + Schema::create(Origin::signed(DID_00), almost_large_data.clone(), Default::default()); + + // Assert + assert!(result.is_ok()); // Should not exceed limit } } From 3387095bd40868e0a41ae829290f22206c8822e4 Mon Sep 17 00:00:00 2001 From: HarishKarthickS Date: Sat, 27 Apr 2024 10:06:46 +0530 Subject: [PATCH 3/3] Change made according to the review --- pallets/schema/src/tests.rs | 58 +++++++++++++------------------------ 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/pallets/schema/src/tests.rs b/pallets/schema/src/tests.rs index 44026afaa..a0bdc5bd6 100644 --- a/pallets/schema/src/tests.rs +++ b/pallets/schema/src/tests.rs @@ -315,47 +315,29 @@ fn test_schema_lookup() { }); } -#[cfg(test)] -mod tests { - // This module contains unit tests for the `pallet/schema` module. - // Unit tests are isolated tests that focus on the functionality of a single module - // and are typically run during development and testing to ensure the module - // behaves as expected. - // The `#[cfg(test)]` attribute ensures this module is only compiled when the - // `test` feature is enabled. - use super::*; - use mock::MockStorage; // Assuming you have a mock for storage - - #[test] - fn test_max_encoded_schema_limit_exceeded_create() { - // Arrange - let mut mock_storage = MockStorage::new(); - const MAX_SCHEMA_SIZE: usize = 1024; // Replace with the actual value - - // Act - let large_schema_data = vec![0u8; MAX_SCHEMA_SIZE + 1]; - let result = Schema::create( - Origin::signed(DID_00), // Replace with appropriate origin type - large_schema_data.clone(), - Default::default(), - ); +#[test] +fn test_max_encoded_schema_limit_exceeded_create() { + // Arrange + let mut mock_storage = MockStorage::new(); - // Assert - assert_eq!(result.err().unwrap(), Error::::MaxEncodedSchemaLimitExceeded,); - } + // Act + let large_schema_data = vec![0u8; ::MaxEncodedSchemaLength + 1]; + let result = + Schema::create(Origin::signed(DID_00), large_schema_data.clone(), Default::default()); - // Add similar test cases for other functions that might return the error + // Assert + assert_eq!(result.err().unwrap(), Error::::MaxEncodedSchemaLimitExceeded,); +} - #[test] - fn test_max_encoded_schema_limit_exceeded_edge_case() { - // Arrange (similar to above) +#[test] +fn test_max_encoded_schema_limit_exceeded_edge_case() { + // Arrange (similar to above) - // Act - let almost_large_data = vec![0u8; MAX_SCHEMA_SIZE - 1]; - let result = - Schema::create(Origin::signed(DID_00), almost_large_data.clone(), Default::default()); + // Act + let almost_large_data = vec![0u8; ::MaxEncodedSchemaLength - 1]; + let result = + Schema::create(Origin::signed(DID_00), almost_large_data.clone(), Default::default()); - // Assert - assert!(result.is_ok()); // Should not exceed limit - } + // Assert + assert!(result.is_ok()); // Should not exceed limit }