qualia_core_db/net/qdnf/harness/
model_check.rs1use core::sync::atomic::{AtomicBool, Ordering};
4
5use crate::net::qdnf::authority::{
6 binding_for_controllers, AuthorityOwner, ContactState, ExecutionPermit,
7};
8use crate::net::qdnf::errors::QdnfError;
9use crate::net::qdnf::types::Generation;
10
11pub fn tla_plus_executed() -> bool {
13 false
14}
15
16static CHECKED: AtomicBool = AtomicBool::new(false);
17
18pub fn bounded_ownership_model_checked() -> bool {
20 CHECKED.load(Ordering::SeqCst)
21}
22
23fn expect_err(result: Result<(), QdnfError>, want: QdnfError) -> Result<(), QdnfError> {
24 match result {
25 Err(e) if e == want => Ok(()),
26 Err(e) => Err(e),
27 Ok(()) => Err(QdnfError::Conflict),
28 }
29}
30
31fn contact_at(i: usize) -> ContactState {
32 match i {
33 0 => ContactState::Request,
34 1 => ContactState::Consent,
35 2 => ContactState::Active,
36 3 => ContactState::Suspended,
37 _ => ContactState::Blocked,
38 }
39}
40
41pub fn check_ownership_state_machine() -> Result<usize, QdnfError> {
47 let mut checked = 0usize;
48
49 match Generation(u64::MAX).next() {
50 Err(QdnfError::StaleGeneration) => checked = checked.saturating_add(1),
51 _ => return Err(QdnfError::Conflict),
52 }
53
54 let mut contact_i = 0usize;
55 while contact_i < 5 {
56 let mut owner = AuthorityOwner::new();
57 let binding = binding_for_controllers(b"did:q42:a", b"did:q42:b", b"q42:QSync/1", b"op-a")?;
58 let contact = contact_at(contact_i);
59 let installed = owner.install_grant(binding, 10, 100, contact);
60 match (contact, installed) {
61 (ContactState::Active, Ok(_)) => checked = checked.saturating_add(1),
62 (ContactState::Active, Err(_)) => return Err(QdnfError::Conflict),
63 (_, Err(QdnfError::Unauthorized)) => checked = checked.saturating_add(1),
64 (_, Err(e)) => return Err(e),
65 (_, Ok(_)) => return Err(QdnfError::Unauthorized),
66 }
67 contact_i = contact_i.saturating_add(1);
68 }
69
70 let mut owner = AuthorityOwner::new();
71 let bind_a = binding_for_controllers(b"did:q42:a", b"did:q42:b", b"q42:QSync/1", b"op-a")?;
72 let bind_a2 = binding_for_controllers(b"did:q42:a", b"did:q42:b", b"q42:QSync/1", b"op-a2")?;
73 let bind_b = binding_for_controllers(b"did:q42:c", b"did:q42:d", b"q42:QSync/1", b"op-b")?;
74
75 let (cred, contact, handle) = owner.install_grant(bind_a, 10, 100, ContactState::Active)?;
76 checked = checked.saturating_add(1);
77
78 let permit = owner.issue_permit(handle, bind_a, 20, 256)?;
79 permit.matches_credential(&cred)?;
80 permit.matches_contact(&contact)?;
81 handle.matches_permit(&permit)?;
82 permit.matches_handle(&handle)?;
83 permit.current_at(20)?;
84 checked = checked.saturating_add(1);
85
86 expect_err(
87 owner.issue_permit(handle, bind_b, 20, 256).map(|_| ()),
88 QdnfError::Unauthorized,
89 )?;
90 checked = checked.saturating_add(1);
91
92 let (_c2, _k2, handle2) = owner.install_grant(bind_a2, 10, 100, ContactState::Active)?;
93 let permit2 = owner.issue_permit(handle2, bind_a2, 20, 256)?;
94 expect_err(handle.matches_permit(&permit2), QdnfError::StaleGeneration)?;
95 expect_err(permit2.matches_handle(&handle), QdnfError::StaleGeneration)?;
96 checked = checked.saturating_add(1);
97
98 expect_err(permit.current_at(100), QdnfError::Expired)?;
99 let grant = owner.current_grant(handle)?;
100 expect_err(permit.matches_grant(&grant, 100), QdnfError::Expired)?;
101 permit.matches_grant(&grant, 20)?;
102 checked = checked.saturating_add(1);
103
104 expect_err(
105 owner.issue_permit(handle, bind_a, 100, 256).map(|_| ()),
106 QdnfError::Expired,
107 )?;
108 checked = checked.saturating_add(1);
109
110 owner.revoke(handle)?;
111 expect_err(
112 owner.issue_permit(handle, bind_a, 20, 256).map(|_| ()),
113 QdnfError::Revoked,
114 )?;
115 checked = checked.saturating_add(1);
116
117 let mut owner2 = AuthorityOwner::new();
118 let (_c, _k, live) = owner2.install_grant(bind_a, 10, 100, ContactState::Active)?;
119 owner2.validate_handle(live)?;
120 let issued: Result<ExecutionPermit, QdnfError> = owner2.issue_permit(live, bind_a, 20, 0);
121 match issued {
122 Err(QdnfError::Capacity) => checked = checked.saturating_add(1),
123 Err(e) => return Err(e),
124 Ok(_) => return Err(QdnfError::Conflict),
125 }
126
127 CHECKED.store(true, Ordering::SeqCst);
128 Ok(checked)
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 #[test]
136 fn bounded_ownership_model_rejects_stale_and_unauthorized() {
137 assert!(!tla_plus_executed());
138 let n = check_ownership_state_machine().unwrap();
139 assert!(bounded_ownership_model_checked());
140 assert!(n >= 8);
141 assert!(!tla_plus_executed());
142 }
143
144 #[test]
145 fn generation_exhaustion_is_stale_not_wrap() {
146 assert_eq!(Generation(u64::MAX).next(), Err(QdnfError::StaleGeneration));
147 }
148}