Skip to main content

confium_coordinator/
coordinator_proptest.rs

1//! Property-based tests for the coordinator state machine.
2
3#[cfg(test)]
4mod proptest {
5    use crate::coordinator::coordinator::Coordinator;
6    use crate::coordinator::session::{SessionRequest, SessionState};
7    use proptest::prelude::*;
8
9    fn make_request(threshold: u32, parties: u32) -> SessionRequest {
10        SessionRequest {
11            quorum_id: "q".into(),
12            scheme: "CMP20".into(),
13            message: vec![0; 32],
14            threshold,
15            num_parties: parties,
16            unlock_window_minutes: 60,
17            requested_by: "test".into(),
18        }
19    }
20
21    proptest! {
22        #![proptest_config(ProptestConfig::with_cases(64))]
23
24        #[test]
25        fn prop_session_count_never_exceeds_created(n in 0u32..20) {
26            let mut coord = Coordinator::new();
27            let mut ids = Vec::new();
28            for _ in 0..n {
29                let id = coord.create_session(make_request(2, 3)).unwrap();
30                ids.push(id);
31            }
32            prop_assert_eq!(coord.session_count(), n as usize);
33            prop_assert_eq!(coord.session_ids().len(), n as usize);
34        }
35
36        #[test]
37        fn prop_session_ids_unique(n in 1u32..10) {
38            let mut coord = Coordinator::new();
39            let mut ids = Vec::new();
40            for _ in 0..n {
41                ids.push(coord.create_session(make_request(2, 3)).unwrap());
42            }
43            let mut sorted = ids.clone();
44            sorted.sort();
45            sorted.dedup();
46            prop_assert_eq!(sorted.len(), ids.len(), "session IDs must be unique");
47        }
48
49        #[test]
50        fn prop_created_session_is_pending(threshold in 1u32..5, parties in 2u32..6) {
51            prop_assume!(parties >= threshold);
52            let mut coord = Coordinator::new();
53            let id = coord.create_session(make_request(threshold, parties)).unwrap();
54            let state = coord.session_state(&id);
55            prop_assert_eq!(state, Some(SessionState::Pending));
56        }
57
58        #[test]
59        fn prop_set_state_persists(n in 1u32..5) {
60            let mut coord = Coordinator::new();
61            let id = coord.create_session(make_request(2, 3)).unwrap();
62            coord.set_session_state(&id, SessionState::Completed);
63            prop_assert_eq!(coord.session_state(&id), Some(SessionState::Completed));
64            let _ = n;
65        }
66
67        #[test]
68        fn prop_audit_log_grows_with_sessions(n in 0u32..10) {
69            let mut coord = Coordinator::new();
70            for _ in 0..n {
71                coord.create_session(make_request(2, 3)).unwrap();
72            }
73            let audit = coord.audit_log();
74            prop_assert_eq!(audit.count(), n as usize);
75        }
76    }
77}