forked from dfinity/ic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUN-641: Stable memory integrity test
- Loading branch information
1 parent
bc9f69d
commit 8220223
Showing
8 changed files
with
746 additions
and
0 deletions.
There are no files selected for viewing
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
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,54 @@ | ||
load("//bazel:canisters.bzl", "rust_canister") | ||
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
rust_library( | ||
name = "stable_memory_integrity", | ||
srcs = glob(["src/**/*.rs"]), | ||
aliases = {}, | ||
crate_name = "ic_stable_memory_integrity", | ||
proc_macro_deps = [], | ||
version = "0.8.0", | ||
deps = [ | ||
"@crate_index//:candid", | ||
"@crate_index//:ic-cdk", | ||
"@crate_index//:serde", | ||
], | ||
) | ||
|
||
rust_canister( | ||
name = "stable_memory_integrity_canister", | ||
srcs = ["bin/main.rs"], | ||
proc_macro_deps = ["@crate_index//:ic-cdk-macros"], | ||
service_file = ":stable_memory_integrity.did", | ||
deps = [ | ||
"//rs/rust_canisters/stable_memory_integrity", | ||
"@crate_index//:candid", | ||
"@crate_index//:ic-cdk", | ||
], | ||
) | ||
|
||
rust_test( | ||
name = "tests", | ||
srcs = ["tests/stable_memory_integrity.rs"], | ||
aliases = {}, | ||
crate_root = "tests/stable_memory_integrity.rs", | ||
data = [ | ||
":stable_memory_integrity_canister.wasm", | ||
], | ||
env = { | ||
"STABLE_MEMORY_INTEGRITY_CANISTER_WASM_PATH": "$(rootpath :stable_memory_integrity_canister.wasm)", | ||
}, | ||
proc_macro_deps = [], | ||
deps = [ | ||
"//rs/rust_canisters/canister_test", | ||
"//rs/rust_canisters/stable_memory_integrity", | ||
"//rs/state_machine_tests", | ||
"//rs/types/types", | ||
"@crate_index//:assert_matches", | ||
"@crate_index//:candid", | ||
"@crate_index//:lazy_static", | ||
"@crate_index//:proptest", | ||
], | ||
) |
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,22 @@ | ||
[package] | ||
name = "stable_memory_integrity" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
assert_matches = "1.5.0" | ||
candid = "0.8.1" | ||
canister-test = { path = "../canister_test" } | ||
ic-cdk = "0.6.0" | ||
ic-cdk-macros = "0.6.0" | ||
ic-state-machine-tests = { path = "../../state_machine_tests" } | ||
ic-types = { path = "../../types/types" } | ||
lazy-static = "1.4.0" | ||
serde = "1.0.136" | ||
proptest = "1.2.0" | ||
|
||
[[bin]] | ||
name = "stable_memory_integrity" | ||
path = "bin/main.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,5 @@ | ||
# Stable Memory Integrity Canister | ||
|
||
This canister can perform arbitrary operations on stable memory. It's purpose is | ||
to used when testing the correctness of the stable memory implementation in the | ||
replica. |
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,25 @@ | ||
use ic_cdk::api::stable::{stable64_read, stable64_size}; | ||
use ic_cdk_macros::{query, update}; | ||
|
||
use ic_stable_memory_integrity::StableOperationResult; | ||
|
||
#[update] | ||
fn perform_and_check_ops(ops: Vec<StableOperationResult>) { | ||
for op in ops { | ||
op.perform_and_check() | ||
} | ||
} | ||
|
||
#[query] | ||
fn final_size() -> u64 { | ||
stable64_size() | ||
} | ||
|
||
#[query] | ||
fn read(start: u64, length: u64) -> Vec<u8> { | ||
let mut result = vec![0; length as usize]; | ||
stable64_read(start, &mut result); | ||
result | ||
} | ||
|
||
fn main() {} |
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,74 @@ | ||
use candid::CandidType; | ||
use ic_cdk::api::stable::{ | ||
stable64_grow, stable64_read, stable64_size, stable64_write, stable_read, stable_write, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, CandidType, PartialEq)] | ||
pub enum StableOperationResult { | ||
Size(u64), | ||
Grow { | ||
new_pages: u64, | ||
result: Result<u64, ()>, | ||
}, | ||
Read { | ||
start: u64, | ||
result: Vec<u8>, | ||
}, | ||
Write { | ||
start: u64, | ||
contents: Vec<u8>, | ||
}, | ||
Read32 { | ||
start: u32, | ||
result: Vec<u8>, | ||
}, | ||
Write32 { | ||
start: u32, | ||
contents: Vec<u8>, | ||
}, | ||
} | ||
|
||
impl StableOperationResult { | ||
pub fn perform_and_check(self) { | ||
match self { | ||
StableOperationResult::Size(expected_size) => { | ||
let result = stable64_size(); | ||
assert_eq!(expected_size, result); | ||
} | ||
StableOperationResult::Grow { new_pages, result } => { | ||
let initial_size = stable64_size(); | ||
let actual_result = stable64_grow(new_pages).map_err(|_| ()); | ||
assert_eq!(result, actual_result); | ||
let new_size = stable64_size(); | ||
if actual_result.is_ok() { | ||
assert_eq!(new_size, initial_size + new_pages); | ||
} else { | ||
assert_eq!(new_size, initial_size); | ||
} | ||
} | ||
StableOperationResult::Read { start, result } => { | ||
let mut actual_result = vec![0; result.len()]; | ||
stable64_read(start, &mut actual_result); | ||
assert_eq!(result, actual_result); | ||
} | ||
StableOperationResult::Write { start, contents } => { | ||
stable64_write(start, &contents); | ||
let mut result = vec![0; contents.len()]; | ||
stable64_read(start, &mut result); | ||
assert_eq!(contents, result); | ||
} | ||
StableOperationResult::Read32 { start, result } => { | ||
let mut actual_result = vec![0; result.len()]; | ||
stable_read(start, &mut actual_result); | ||
assert_eq!(result, actual_result); | ||
} | ||
StableOperationResult::Write32 { start, contents } => { | ||
stable_write(start, &contents); | ||
let mut result = vec![0; contents.len()]; | ||
stable_read(start, &mut result); | ||
assert_eq!(contents, result); | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
rs/rust_canisters/stable_memory_integrity/stable_memory_integrity.did
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 @@ | ||
service : {} |
Oops, something went wrong.