-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename test-cases to dynamic-storage
- Loading branch information
Showing
13 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...cases/dynamic-instance-storage/Cargo.toml → test-cases/dynamic-storage/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e-storage-2/vulnerable-example/Cargo.toml → ...c-storage-1/remediated-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...e-storage-1/remediated-example/Cargo.toml → ...c-storage-1/vulnerable-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...e-storage-1/vulnerable-example/Cargo.toml → ...c-storage-2/remediated-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...e-storage-2/remediated-example/Cargo.toml → ...c-storage-2/vulnerable-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
16 changes: 16 additions & 0 deletions
16
test-cases/dynamic-storage/dynamic-storage-3/remediated-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
44 changes: 44 additions & 0 deletions
44
test-cases/dynamic-storage/dynamic-storage-3/remediated-example/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Symbol, i32>) { | ||
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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
52 changes: 52 additions & 0 deletions
52
test-cases/dynamic-storage/dynamic-storage-3/vulnerable-example/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Symbol, i32>) { | ||
e.storage() | ||
.instance() | ||
.set(&Symbol::new(&e, "map_data"), &data); | ||
} | ||
|
||
pub fn get_map(e: Env) -> Map<Symbol, i32> { | ||
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")) | ||
); | ||
} | ||
} |