Skip to content

Commit 64c095f

Browse files
committed
preparation for fuzz testing
1 parent e578983 commit 64c095f

File tree

8 files changed

+94
-13
lines changed

8 files changed

+94
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ic-wasi-polyfill"
3-
version = "0.8.2"
3+
version = "0.8.3"
44
edition = "2024"
55
keywords = ["ic", "wasi", "wasi-polyfill"]
66
description = "The project provides polyfill implementation of *wasi_snapshot_preview1* functions using IC System API."

test/canisters/fs_tests/dfx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"candid": "src/fs_tests_backend/fs_tests_backend.did",
55
"package": "fs_tests_backend",
66
"wasm": "target/wasm32-wasip1/release/fs_tests_backend_nowasi.wasm",
7-
"type": "rust"
7+
"build": "scripts/build.sh",
8+
"type": "custom"
89
}
910
},
1011
"defaults": {

test/canisters/fs_tests/scripts/install.sh renamed to test/canisters/fs_tests/scripts/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ic-wasm $RELEASE_DIR/fs_tests_backend.wasm -o $RELEASE_DIR/fs_tests_backend_meta
1111
wasi2ic $RELEASE_DIR/fs_tests_backend_meta.wasm $RELEASE_DIR/fs_tests_backend_nowasi.wasm
1212

1313

14-
dfx canister create fs_tests_backend
14+
#dfx canister create fs_tests_backend
1515

16-
dfx canister install --mode reinstall fs_tests_backend --wasm $RELEASE_DIR/fs_tests_backend_nowasi.wasm -y
16+
#dfx canister install --mode reinstall fs_tests_backend --wasm $RELEASE_DIR/fs_tests_backend_nowasi.wasm -y
1717

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/bin/sh
22

33
# try reading a file if it exists, then
4-
dfx canister call fs_tests_backend read_file '(".test_hidden/testdir/test.txt")'
4+
#dfx canister call fs_tests_backend read_file '(".test_hidden/testdir/test.txt")'
55

66
# write some content into file
77
dfx canister call fs_tests_backend write_file '(".test_hidden/testdir/test.txt", "some test content")'
88

99
# try reading again
1010
dfx canister call fs_tests_backend read_file '(".test_hidden/testdir/test.txt")'
11+
12+
# compute hash
13+
dfx canister call fs_tests_backend compute_file_hash '(".test_hidden/testdir/test.txt")'
14+

test/canisters/fs_tests/src/fs_tests_backend/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ edition = "2021"
88
[lib]
99
crate-type = ["cdylib"]
1010

11+
[[bin]]
12+
name = "fs_test"
13+
path = "src/main.rs"
14+
1115
[dependencies]
1216
candid = "0.10"
1317
ic-cdk = "0.18.3"
1418
ic-stable-structures = "0.6.7"
1519

1620
ic-wasi-polyfill = {path="../../../../../../ic-wasi-polyfill"}
1721
#ic-wasi-polyfill = "0.5"
22+
23+
sha2 = "0.10"
24+
anyhow = "1.0.98"
25+
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
service : {
2+
"greet": (text) -> (text) query;
3+
"basic_fs_test": () -> ();
4+
5+
"compute_file_hash": (text) -> (text);
26

37
"read_file": (text) -> (text);
48

59
"write_file": (text, text) -> ();
6-
710
}

test/canisters/fs_tests/src/fs_tests_backend/src/lib.rs

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
use ic_stable_structures::{
2-
memory_manager::{MemoryId, MemoryManager},
32
DefaultMemoryImpl, Memory,
3+
memory_manager::{MemoryId, MemoryManager},
44
};
55

6+
#[ic_cdk::query]
7+
fn greet(name: String) -> String {
8+
format!("Hello, {}!", name)
9+
}
10+
11+
pub struct SimpleRng {
12+
state: u64,
13+
}
14+
15+
impl SimpleRng {
16+
pub fn new(seed: u64) -> Self {
17+
Self { state: seed }
18+
}
19+
20+
pub fn next(&mut self) -> u64 {
21+
// Constants from Numerical Recipes
22+
const A: u64 = 6364136223846793005;
23+
const C: u64 = 1;
24+
25+
self.state = self.state.wrapping_mul(A).wrapping_add(C);
26+
self.state
27+
}
28+
}
29+
630
use std::{
731
cell::RefCell,
832
fs::{self},
933
};
1034

1135
const PROFILING: MemoryId = MemoryId::new(100);
12-
const WASI_MEMORY_ID: MemoryId = MemoryId::new(1);
36+
const WASI_MEMORY_ID: MemoryId = MemoryId::new(11);
1337

1438
thread_local! {
1539
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
@@ -36,12 +60,48 @@ fn init() {
3660
});
3761
}
3862

39-
#[ic_cdk::post_upgrade]
40-
fn post_upgrade() {
41-
profiling_init();
63+
use sha2::{Digest, Sha256};
64+
use std::fs::File;
65+
use std::io::BufReader;
66+
use std::io::Read;
67+
use std::io::Write;
4268

43-
let wasi_memory = MEMORY_MANAGER.with(|m| m.borrow().get(WASI_MEMORY_ID));
44-
ic_wasi_polyfill::init_with_memory(&[0u8; 32], &[], wasi_memory);
69+
#[ic_cdk::update]
70+
fn compute_file_hash(path: String) -> String {
71+
let file = File::open(path).expect("I/O error");
72+
let mut reader = BufReader::new(file);
73+
let mut hasher = Sha256::new();
74+
let mut buffer = [0u8; 4096];
75+
76+
loop {
77+
let n = reader.read(&mut buffer).expect("I/O error");
78+
79+
if n == 0 {
80+
break;
81+
}
82+
hasher.update(&buffer[..n]);
83+
}
84+
85+
let hash = hasher.finalize();
86+
87+
format!("{:x}", hash)
88+
}
89+
90+
#[ic_cdk::update]
91+
pub fn basic_fs_test() {
92+
let file_path = "test_file.txt";
93+
let test_content = "Hello, Rust!";
94+
95+
let mut file = File::create(file_path).expect("Failed to create file");
96+
file.write_all(test_content.as_bytes())
97+
.expect("Failed to write to file");
98+
99+
let mut read_content = String::new();
100+
let mut file = File::open(file_path).expect("Failed to open file");
101+
file.read_to_string(&mut read_content)
102+
.expect("Failed to read file");
103+
104+
assert_eq!(read_content, test_content);
45105
}
46106

47107
#[ic_cdk::update]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod lib;
2+
3+
fn main() {
4+
lib::basic_fs_test();
5+
}

0 commit comments

Comments
 (0)