qualia_vision/biosense/policy/
policy_permit_check.rs1use crate::biosense::consent::BiosenseConsent;
15use crate::biosense::policy::{evaluate_processing_act, PolicyDecision, ProcessingAct};
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct PermitAnswer {
21 pub decision: PolicyDecision,
22 pub reason: &'static str,
23 pub camera_id: u64,
24}
25
26pub fn policy_permit_check(
31 camera_id: u64,
32 act: ProcessingAct,
33 consent: BiosenseConsent,
34) -> PermitAnswer {
35 let decision = evaluate_processing_act(consent, act);
36 let reason = reason_for(act, decision, consent.allow_process);
37 PermitAnswer {
38 decision,
39 reason,
40 camera_id,
41 }
42}
43
44fn reason_for(act: ProcessingAct, decision: PolicyDecision, allow_process: bool) -> &'static str {
46 match decision {
47 PolicyDecision::Permit => match act {
48 ProcessingAct::MotionOnly => "motion-only processing permitted",
49 ProcessingAct::FaceDetect => {
50 "face detection permitted under surveillance-policy purpose"
51 }
52 ProcessingAct::FaceEmbed | ProcessingAct::Identify1N => {
53 "identification permitted: purpose-bound security grant with template consent"
54 }
55 ProcessingAct::Rppg => "rPPG permitted for self-monitoring / research purpose",
56 ProcessingAct::Affect => {
57 "affect estimation permitted for self-monitoring / research purpose"
58 }
59 ProcessingAct::RecordStore => {
60 "record storage permitted by template or graph-observation consent"
61 }
62 },
63 PolicyDecision::Forbid => {
64 if !allow_process {
65 return "fail-closed: no processing consent for this camera";
66 }
67 match act {
68 ProcessingAct::FaceEmbed | ProcessingAct::Identify1N => {
69 "identification requires a purpose-bound security grant"
70 }
71 ProcessingAct::FaceDetect => {
72 "face detection requires a surveillance-policy purpose"
73 }
74 ProcessingAct::Rppg => "rPPG requires a self-monitoring or research purpose",
75 ProcessingAct::Affect => {
76 "affect estimation requires a self-monitoring or research purpose"
77 }
78 ProcessingAct::RecordStore => {
79 "record storage requires template or graph-observation consent"
80 }
81 ProcessingAct::MotionOnly => {
83 "motion-only processing not permitted for this consent"
84 }
85 }
86 }
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93 use crate::biosense::consent::BiosensePurpose;
94
95 #[test]
96 fn denied_consent_forbids_with_reason() {
97 let consent = BiosenseConsent::denied(BiosensePurpose::Security);
98 let a = policy_permit_check(0xCA51, ProcessingAct::FaceEmbed, consent);
99 assert_eq!(a.decision, PolicyDecision::Forbid);
100 assert_eq!(a.camera_id, 0xCA51);
101 assert_eq!(
102 a.reason,
103 "fail-closed: no processing consent for this camera"
104 );
105 assert!(!a.reason.is_empty());
106 }
107
108 #[test]
109 fn security_template_grant_permits_face_embed() {
110 let consent = BiosenseConsent::grant_security_template(0xABC);
111 let a = policy_permit_check(0xCA52, ProcessingAct::FaceEmbed, consent);
112 assert_eq!(a.decision, PolicyDecision::Permit);
113 assert_eq!(a.camera_id, 0xCA52);
114 assert!(!a.reason.is_empty());
115 }
116
117 #[test]
118 fn surveillance_face_embed_forbidden_needs_security_grant() {
119 let consent = BiosenseConsent::grant_process(BiosensePurpose::SurveillancePolicy, 1);
120 let a = policy_permit_check(0xCA53, ProcessingAct::FaceEmbed, consent);
121 assert_eq!(a.decision, PolicyDecision::Forbid);
122 assert_eq!(
123 a.reason,
124 "identification requires a purpose-bound security grant"
125 );
126 }
127
128 #[test]
129 fn motion_only_always_permitted() {
130 let consent = BiosenseConsent::grant_process(BiosensePurpose::SurveillancePolicy, 1);
132 let a = policy_permit_check(0xCA54, ProcessingAct::MotionOnly, consent);
133 assert_eq!(a.decision, PolicyDecision::Permit);
134 assert_eq!(a.reason, "motion-only processing permitted");
135 assert_eq!(a.camera_id, 0xCA54);
136 }
137
138 #[test]
139 fn every_answer_carries_camera_id_and_nonempty_reason() {
140 let grants = [
141 BiosenseConsent::denied(BiosensePurpose::Research),
142 BiosenseConsent::grant_process(BiosensePurpose::WellfairSelfMonitor, 2),
143 BiosenseConsent::grant_security_template(3),
144 ];
145 let acts = [
146 ProcessingAct::MotionOnly,
147 ProcessingAct::FaceDetect,
148 ProcessingAct::FaceEmbed,
149 ProcessingAct::Identify1N,
150 ProcessingAct::Rppg,
151 ProcessingAct::Affect,
152 ProcessingAct::RecordStore,
153 ];
154 for (i, &c) in grants.iter().enumerate() {
155 for &act in &acts {
156 let cam = 0x1000 + i as u64;
157 let a = policy_permit_check(cam, act, c);
158 assert_eq!(a.camera_id, cam);
159 assert!(!a.reason.is_empty(), "reason must be non-empty for {act:?}");
160 }
161 }
162 }
163}