diff --git a/test-cases/dynamic-instance-storage/Cargo.toml b/test-cases/dynamic-storage/Cargo.toml similarity index 79% rename from test-cases/dynamic-instance-storage/Cargo.toml rename to test-cases/dynamic-storage/Cargo.toml index 2af55099..6ac24264 100644 --- a/test-cases/dynamic-instance-storage/Cargo.toml +++ b/test-cases/dynamic-storage/Cargo.toml @@ -1,10 +1,10 @@ [workspace] exclude = [".cargo", "target"] -members = ["dynamic-instance-storage-*/*"] +members = ["dynamic-storage-*/*"] resolver = "2" [workspace.dependencies] -soroban-sdk = { version = "=21.4.0" } +soroban-sdk = { version = "=21.5.1" } [profile.release] codegen-units = 1 diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-2/vulnerable-example/Cargo.toml b/test-cases/dynamic-storage/dynamic-storage-1/remediated-example/Cargo.toml similarity index 84% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-2/vulnerable-example/Cargo.toml rename to test-cases/dynamic-storage/dynamic-storage-1/remediated-example/Cargo.toml index 9241c0f4..f096e323 100644 --- a/test-cases/dynamic-instance-storage/dynamic-instance-storage-2/vulnerable-example/Cargo.toml +++ b/test-cases/dynamic-storage/dynamic-storage-1/remediated-example/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "dynamic-instance-storage-vulnerable-2" +name = "dynamic-storage-remediated-1" version = "0.1.0" [lib] diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-1/remediated-example/src/lib.rs b/test-cases/dynamic-storage/dynamic-storage-1/remediated-example/src/lib.rs similarity index 100% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-1/remediated-example/src/lib.rs rename to test-cases/dynamic-storage/dynamic-storage-1/remediated-example/src/lib.rs diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-1/remediated-example/Cargo.toml b/test-cases/dynamic-storage/dynamic-storage-1/vulnerable-example/Cargo.toml similarity index 84% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-1/remediated-example/Cargo.toml rename to test-cases/dynamic-storage/dynamic-storage-1/vulnerable-example/Cargo.toml index 6da39cf1..f1ef1214 100644 --- a/test-cases/dynamic-instance-storage/dynamic-instance-storage-1/remediated-example/Cargo.toml +++ b/test-cases/dynamic-storage/dynamic-storage-1/vulnerable-example/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "dynamic-instance-storage-remediated-1" +name = "dynamic-storage-vulnerable-1" version = "0.1.0" [lib] diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-1/vulnerable-example/src/lib.rs b/test-cases/dynamic-storage/dynamic-storage-1/vulnerable-example/src/lib.rs similarity index 100% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-1/vulnerable-example/src/lib.rs rename to test-cases/dynamic-storage/dynamic-storage-1/vulnerable-example/src/lib.rs diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-1/vulnerable-example/Cargo.toml b/test-cases/dynamic-storage/dynamic-storage-2/remediated-example/Cargo.toml similarity index 84% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-1/vulnerable-example/Cargo.toml rename to test-cases/dynamic-storage/dynamic-storage-2/remediated-example/Cargo.toml index 7122885e..e0bfcd9c 100644 --- a/test-cases/dynamic-instance-storage/dynamic-instance-storage-1/vulnerable-example/Cargo.toml +++ b/test-cases/dynamic-storage/dynamic-storage-2/remediated-example/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "dynamic-instance-storage-vulnerable-1" +name = "dynamic-storage-remediated-2" version = "0.1.0" [lib] diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-2/remediated-example/src/lib.rs b/test-cases/dynamic-storage/dynamic-storage-2/remediated-example/src/lib.rs similarity index 100% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-2/remediated-example/src/lib.rs rename to test-cases/dynamic-storage/dynamic-storage-2/remediated-example/src/lib.rs diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-2/remediated-example/Cargo.toml b/test-cases/dynamic-storage/dynamic-storage-2/vulnerable-example/Cargo.toml similarity index 84% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-2/remediated-example/Cargo.toml rename to test-cases/dynamic-storage/dynamic-storage-2/vulnerable-example/Cargo.toml index 7d90d213..93954019 100644 --- a/test-cases/dynamic-instance-storage/dynamic-instance-storage-2/remediated-example/Cargo.toml +++ b/test-cases/dynamic-storage/dynamic-storage-2/vulnerable-example/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "dynamic-instance-storage-remediated-2" +name = "dynamic-storage-vulnerable-2" version = "0.1.0" [lib] diff --git a/test-cases/dynamic-instance-storage/dynamic-instance-storage-2/vulnerable-example/src/lib.rs b/test-cases/dynamic-storage/dynamic-storage-2/vulnerable-example/src/lib.rs similarity index 100% rename from test-cases/dynamic-instance-storage/dynamic-instance-storage-2/vulnerable-example/src/lib.rs rename to test-cases/dynamic-storage/dynamic-storage-2/vulnerable-example/src/lib.rs diff --git a/test-cases/dynamic-storage/dynamic-storage-3/remediated-example/Cargo.toml b/test-cases/dynamic-storage/dynamic-storage-3/remediated-example/Cargo.toml new file mode 100644 index 00000000..32465e65 --- /dev/null +++ b/test-cases/dynamic-storage/dynamic-storage-3/remediated-example/Cargo.toml @@ -0,0 +1,16 @@ +[package] +edition = "2021" +name = "dynamic-storage-remediated-3" +version = "0.1.0" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +soroban-sdk = { workspace = true } + +[dev-dependencies] +soroban-sdk = { workspace = true, features = ["testutils"] } + +[features] +testutils = ["soroban-sdk/testutils"] diff --git a/test-cases/dynamic-storage/dynamic-storage-3/remediated-example/src/lib.rs b/test-cases/dynamic-storage/dynamic-storage-3/remediated-example/src/lib.rs new file mode 100644 index 00000000..60f47e41 --- /dev/null +++ b/test-cases/dynamic-storage/dynamic-storage-3/remediated-example/src/lib.rs @@ -0,0 +1,44 @@ +#![no_std] +use soroban_sdk::{contract, contractimpl, Env, Map, Symbol}; + +#[contract] +pub struct MapStorage; + +#[contractimpl] +impl MapStorage { + pub fn store_map(e: Env, data: Map) { + data.iter().for_each(|(key, value)| { + e.storage().instance().set(&key, &value); + }); + } + + pub fn get_key(e: Env, key: Symbol) -> i32 { + e.storage().instance().get(&key).unwrap() + } +} + +#[cfg(test)] +mod test { + use super::*; + use soroban_sdk::{symbol_short, Env, Map}; + + #[test] + fn test_map_storage() { + // Given + let env = Env::default(); + let contract_id = env.register_contract(None, MapStorage); + let client = MapStorageClient::new(&env, &contract_id); + + // When + let mut test_map = Map::new(&env); + test_map.set(symbol_short!("key1"), 1); + test_map.set(symbol_short!("key2"), 2); + client.store_map(&test_map); + + // Then + let key1 = client.get_key(&symbol_short!("key1")); + let key2 = client.get_key(&symbol_short!("key2")); + assert_eq!(test_map.get(symbol_short!("key1")).unwrap(), key1); + assert_eq!(test_map.get(symbol_short!("key2")).unwrap(), key2); + } +} diff --git a/test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/Cargo.toml b/test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/Cargo.toml new file mode 100644 index 00000000..37605628 --- /dev/null +++ b/test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/Cargo.toml @@ -0,0 +1,16 @@ +[package] +edition = "2021" +name = "dynamic-storage-vulnerable-3" +version = "0.1.0" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +soroban-sdk = { workspace = true } + +[dev-dependencies] +soroban-sdk = { workspace = true, features = ["testutils"] } + +[features] +testutils = ["soroban-sdk/testutils"] diff --git a/test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/src/lib.rs b/test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/src/lib.rs new file mode 100644 index 00000000..20a33671 --- /dev/null +++ b/test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/src/lib.rs @@ -0,0 +1,52 @@ +#![no_std] +use soroban_sdk::{contract, contractimpl, Env, Map, Symbol}; + +#[contract] +pub struct MapStorage; + +#[contractimpl] +impl MapStorage { + pub fn store_map(e: Env, data: Map) { + e.storage() + .instance() + .set(&Symbol::new(&e, "map_data"), &data); + } + + pub fn get_map(e: Env) -> Map { + e.storage() + .instance() + .get(&Symbol::new(&e, "map_data")) + .unwrap() + } +} + +#[cfg(test)] +mod test { + use super::*; + use soroban_sdk::{symbol_short, Env, Map}; + + #[test] + fn test_map_storage() { + // Given + let env = Env::default(); + let contract_id = env.register_contract(None, MapStorage); + let client = MapStorageClient::new(&env, &contract_id); + + // When + let mut test_map = Map::new(&env); + test_map.set(symbol_short!("key1"), 1); + test_map.set(symbol_short!("key2"), 2); + client.store_map(&test_map); + + // Then + let retrieved_map = client.get_map(); + assert_eq!( + test_map.get(symbol_short!("key1")), + retrieved_map.get(symbol_short!("key1")) + ); + assert_eq!( + test_map.get(symbol_short!("key2")), + retrieved_map.get(symbol_short!("key2")) + ); + } +}