Skip to main content

ic_testkit/
lib.rs

1//! Focused `PocketIC` test-harness utilities for Internet Computer canisters.
2//!
3//! Import native `PocketIC` clients and DTOs directly from `ic_platform_pocket_ic`.
4//! `pic` adds optional typed Wire calls, extension traits for
5//! installation, diagnostics and snapshots, startup errors, caller-owned
6//! managed-server startup and bounded readiness polling. It does not provide a
7//! simulator wrapper or a host-wide runtime lock.
8//!
9//! The crate also provides:
10//!
11//! - marker parsing, aggregation, comparison, and reports in [`benchmark`];
12//! - canister-side marker emission in `performance` (Wasm only);
13//! - deterministic test principals through [`fake_principal`].
14//!
15//! The `pic` helpers are unavailable
16//! when compiling for `wasm32`; parsing and analysis are native-only. Benchmark
17//! data types, checked marker formatting and marker emission remain
18//! available to canister code.
19
20pub mod benchmark;
21
22#[cfg(not(target_arch = "wasm32"))]
23pub mod pic;
24
25#[cfg(target_arch = "wasm32")]
26pub mod performance;
27
28use ic_platform_types::principal::ExternalPrincipal as Principal;
29
30/// Deterministically derive a test [`Principal`] from `seed`.
31///
32/// Values are derived directly from a numeric seed, making fixtures stable
33/// without embedding textual principal literals.
34#[must_use]
35pub fn fake_principal(seed: u32) -> Principal {
36    let mut buf = [0u8; 29];
37    buf[..4].copy_from_slice(&seed.to_be_bytes());
38
39    Principal::from_slice(&buf)
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn fake_principal_is_deterministic_and_unique() {
48        let p1: Principal = fake_principal(7);
49        let p2 = fake_principal(7);
50        let q = fake_principal(8);
51
52        assert_eq!(p1, p2, "fake_principal should be deterministic");
53        assert_ne!(p1, q, "fake_principal should differ for different seeds");
54
55        let bytes = p1.as_slice();
56        assert_eq!(bytes.len(), 29, "Principal must be 29 bytes");
57    }
58}