Skip to content

Commit

Permalink
Rename test-cases to dynamic-storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jgcrosta committed Aug 20, 2024
1 parent bf96775 commit 3e16e4f
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
edition = "2021"
name = "dynamic-instance-storage-vulnerable-2"
name = "dynamic-storage-remediated-1"
version = "0.1.0"

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
edition = "2021"
name = "dynamic-instance-storage-remediated-1"
name = "dynamic-storage-vulnerable-1"
version = "0.1.0"

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
edition = "2021"
name = "dynamic-instance-storage-vulnerable-1"
name = "dynamic-storage-remediated-2"
version = "0.1.0"

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
edition = "2021"
name = "dynamic-instance-storage-remediated-2"
name = "dynamic-storage-vulnerable-2"
version = "0.1.0"

[lib]
Expand Down
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"]
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);
}
}
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"]
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"))
);
}
}

0 comments on commit 3e16e4f

Please sign in to comment.