Debian Codegraph @

debian/rust-vendor/selinux/src/call_back/tests.rs

raw
1 use alloc::fmt;
2 use core::mem;
3 use std::os::raw::{c_char, c_int, c_void};
4
5 fn template<T>(call_back: <T as super::CallBack>::CallBackType)
6 where
7 T: super::CallBack + Default + fmt::Debug,
8 <T as super::CallBack>::CallBackType: fmt::Debug + Eq + Copy + Sized,
9 {
10 // TODO: We need to arrange for the call back to be actually called.
11
12 let _ignored = format!("{:?}", T::default());
13
14 let old_call_back = T::get_call_back();
15
16 T::set_call_back(None);
17 assert!(T::get_call_back().is_none());
18
19 let call_back: Option<<T as super::CallBack>::CallBackType> = Some(call_back);
20 assert_ne!(old_call_back, call_back);
21
22 T::set_call_back(call_back);
23 assert_eq!(T::get_call_back(), call_back);
24
25 T::set_call_back(old_call_back);
26 assert_eq!(T::get_call_back(), old_call_back);
27 }
28
29 #[test]
30 fn log() {
31 // # Safety
32 //
33 // For now, stable Rust does not allow defining variadic functions.
34 // Once that becomes possible, we should define a variadic function that
35 // calls libc::abort(), and call that instead.
36 // For the moment, we allow calling abort() with a different prototype,
37 // which only "works" in some ABIs, and probably fails horribly in others.
38 template::<super::Log>(unsafe {
39 #[allow(clippy::missing_transmute_annotations, clippy::as_conversions)]
40 mem::transmute(libc::abort as unsafe extern "C" fn() -> !)
41 });
42 }
43
44 #[test]
45 fn audit() {
46 template::<super::Audit>(audit_call_back);
47 }
48
49 #[test]
50 fn context_validation() {
51 template::<super::ContextValidation>(context_validation_call_back);
52 }
53
54 #[test]
55 fn enforcing_change() {
56 template::<super::EnforcingChange>(enforcing_change_call_back);
57 }
58
59 #[test]
60 fn security_policy_reload() {
61 template::<super::SecurityPolicyReload>(security_policy_reload_call_back);
62 }
63
64 // Dummy call back functions, of the correct prototypes.
65
66 unsafe extern "C" fn audit_call_back(
67 _audit_data: *mut c_void,
68 _security_class: selinux_sys::security_class_t,
69 _message_buffer: *mut c_char,
70 _message_buffer_size: usize,
71 ) -> c_int {
72 0_i32
73 }
74
75 unsafe extern "C" fn context_validation_call_back(_context_ptr: *mut *mut c_char) -> c_int {
76 0_i32
77 }
78
79 unsafe extern "C" fn enforcing_change_call_back(_enforcing: c_int) -> c_int {
80 0_i32
81 }
82
83 unsafe extern "C" fn security_policy_reload_call_back(_seqno: c_int) -> c_int {
84 0_i32
85 }