1#![allow(non_upper_case_globals)]
42#![allow(non_camel_case_types)]
43#![allow(non_snake_case)]
44#![allow(dead_code)]
45#![allow(clippy::all)]
46
47#[cfg(all(target_os = "linux", not(libcrun_stub)))]
52include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
53
54#[cfg(any(not(target_os = "linux"), libcrun_stub))]
59pub mod stub {
60 use std::ffi::c_void;
66
67 pub type libcrun_error_t = *mut c_void;
69
70 pub type libcrun_container_t = *mut c_void;
72
73 #[repr(C)]
75 #[derive(Debug, Default)]
76 pub struct libcrun_context_s {
77 pub state_root: *const std::ffi::c_char,
78 pub id: *const std::ffi::c_char,
79 pub bundle: *const std::ffi::c_char,
80 pub console_socket: *const std::ffi::c_char,
81 pub pid_file: *const std::ffi::c_char,
82 pub notify_socket: *const std::ffi::c_char,
83 pub handler: *const c_void,
84 pub preserve_fds: i32,
85 pub listen_fds: i32,
86 pub output_handler: Option<unsafe extern "C" fn()>,
87 pub output_handler_arg: *mut c_void,
88 pub fifo_exec_wait_fd: i32,
89 pub systemd_cgroup: bool,
90 pub detach: bool,
91 pub no_new_keyring: bool,
92 pub force_no_cgroup: bool,
93 pub no_pivot: bool,
94 pub argv: *mut *mut std::ffi::c_char,
95 pub argc: i32,
96 pub handler_manager: *mut c_void,
97 }
98
99 pub type features_info_s = c_void;
101
102 pub unsafe fn libcrun_container_get_features(
108 _ctx: *mut libcrun_context_s,
109 _features: *mut *mut features_info_s,
110 _err: *mut libcrun_error_t,
111 ) -> i32 {
112 -1
113 }
114
115 pub unsafe fn libcrun_container_load_from_memory(
119 _json: *const std::ffi::c_char,
120 _err: *mut libcrun_error_t,
121 ) -> libcrun_container_t {
122 std::ptr::null_mut()
123 }
124
125 pub unsafe fn libcrun_container_run(
129 _ctx: *mut libcrun_context_s,
130 _container: libcrun_container_t,
131 _options: i32,
132 _err: *mut libcrun_error_t,
133 ) -> i32 {
134 -1
135 }
136
137 pub unsafe fn libcrun_container_kill(
141 _ctx: *mut libcrun_context_s,
142 _id: *const std::ffi::c_char,
143 _signal: *const std::ffi::c_char,
144 _err: *mut libcrun_error_t,
145 ) -> i32 {
146 -1
147 }
148
149 pub unsafe fn libcrun_container_delete(
153 _ctx: *mut libcrun_context_s,
154 _def: *mut c_void,
155 _id: *const std::ffi::c_char,
156 _force: bool,
157 _err: *mut libcrun_error_t,
158 ) -> i32 {
159 -1
160 }
161
162 pub unsafe fn libcrun_container_free(_container: libcrun_container_t) {}
166
167 pub unsafe fn crun_error_release(_err: *mut libcrun_error_t) {}
171}
172
173#[cfg(any(not(target_os = "linux"), libcrun_stub))]
174pub use stub::*;
175
176#[cfg(test)]
177mod tests {
178 use std::ptr;
179
180 #[cfg(target_os = "linux")]
185 use super::*;
186
187 #[cfg(target_os = "linux")]
189 #[test]
190 fn test_error_lifecycle() {
191 let err: libcrun_error_t = ptr::null_mut();
192 assert!(err.is_null());
193 }
194
195 #[cfg(target_os = "linux")]
198 #[test]
199 fn test_error_release_null_safe() {
200 unsafe {
201 let mut err: libcrun_error_t = ptr::null_mut();
203 crun_error_release(&mut err);
204 }
205 }
206
207 #[cfg(target_os = "linux")]
209 #[test]
210 fn test_context_struct_is_non_zero() {
211 let size = std::mem::size_of::<libcrun_context_s>();
213 assert!(size > 0, "libcrun_context_s should have non-zero size");
214 assert!(
216 size >= std::mem::size_of::<*const ()>(),
217 "libcrun_context_s too small"
218 );
219 }
220
221 #[cfg(target_os = "linux")]
223 #[test]
224 fn test_load_invalid_json_returns_null() {
225 use std::ffi::CString;
226
227 unsafe {
228 let mut err: libcrun_error_t = ptr::null_mut();
229 let invalid_json = CString::new("not valid json").unwrap();
230
231 let container = libcrun_container_load_from_memory(invalid_json.as_ptr(), &mut err);
232
233 assert!(container.is_null(), "Expected null for invalid JSON");
235
236 if !err.is_null() {
238 crun_error_release(&mut err);
239 }
240 }
241 }
242
243 #[cfg(any(not(target_os = "linux"), libcrun_stub))]
249 #[test]
250 fn test_stub_compiles() {
251 use super::stub::*;
252 let ctx = libcrun_context_s::default();
253 assert!(ctx.state_root.is_null());
254 assert!(ctx.id.is_null());
255 assert!(ctx.bundle.is_null());
256 }
257
258 #[cfg(any(not(target_os = "linux"), libcrun_stub))]
260 #[test]
261 fn test_stub_functions_return_errors() {
262 use super::stub::*;
263
264 unsafe {
265 let mut ctx = libcrun_context_s::default();
266 let mut err: libcrun_error_t = ptr::null_mut();
267
268 let result = libcrun_container_get_features(&mut ctx, ptr::null_mut(), &mut err);
270 assert_eq!(result, -1, "get_features should return -1");
271
272 let container = libcrun_container_load_from_memory(ptr::null(), &mut err);
274 assert!(container.is_null(), "load_from_memory should return null");
275
276 let result = libcrun_container_run(&mut ctx, ptr::null_mut(), 0, &mut err);
278 assert_eq!(result, -1, "run should return -1");
279
280 let result = libcrun_container_kill(&mut ctx, ptr::null(), ptr::null(), &mut err);
282 assert_eq!(result, -1, "kill should return -1");
283
284 let result =
286 libcrun_container_delete(&mut ctx, ptr::null_mut(), ptr::null(), false, &mut err);
287 assert_eq!(result, -1, "delete should return -1");
288 }
289 }
290
291 #[cfg(any(not(target_os = "linux"), libcrun_stub))]
293 #[test]
294 fn test_stub_free_functions_null_safe() {
295 use super::stub::*;
296
297 unsafe {
298 libcrun_container_free(ptr::null_mut());
300 crun_error_release(ptr::null_mut());
301 }
302 }
303
304 #[cfg(any(not(target_os = "linux"), libcrun_stub))]
306 #[test]
307 fn test_stub_context_fields() {
308 use super::stub::*;
309
310 let ctx = libcrun_context_s::default();
311
312 assert!(ctx.state_root.is_null());
314 assert!(ctx.id.is_null());
315 assert!(ctx.bundle.is_null());
316 assert!(ctx.console_socket.is_null());
317 assert!(ctx.pid_file.is_null());
318 assert!(ctx.notify_socket.is_null());
319 assert!(ctx.handler.is_null());
320 assert!(ctx.output_handler.is_none());
321 assert!(ctx.output_handler_arg.is_null());
322 assert!(ctx.argv.is_null());
323 assert!(ctx.handler_manager.is_null());
324
325 assert_eq!(ctx.preserve_fds, 0);
327 assert_eq!(ctx.listen_fds, 0);
328 assert_eq!(ctx.fifo_exec_wait_fd, 0);
329 assert_eq!(ctx.argc, 0);
330 assert!(!ctx.systemd_cgroup);
331 assert!(!ctx.detach);
332 assert!(!ctx.no_new_keyring);
333 assert!(!ctx.force_no_cgroup);
334 assert!(!ctx.no_pivot);
335 }
336}