Debian Codegraph @

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

raw
1 use core::ptr;
2 use std::collections::{HashMap, HashSet};
3 use std::io::Write;
4 use std::os::raw::{c_char, c_int};
5 use std::path::Path;
6 use std::{fs, io, process};
7
8 use assert_matches::assert_matches;
9
10 use crate::utils::*;
11
12 #[test]
13 fn security_context_from_c_str() {
14 let s = c"xyz";
15 let mut context = super::SecurityContext::from_c_str(s, false);
16 assert_eq!(context.as_ptr(), s.as_ptr());
17 assert_eq!(context.as_mut_ptr(), s.as_ptr().cast_mut());
18 assert_eq!(context.as_bytes().len(), 3);
19 assert!(!context.is_raw_format());
20
21 let _ignored = format!("{:?}", &context);
22 }
23
24 #[test]
25 fn security_context_from_result() {
26 super::SecurityContext::from_result("xyz", 0, ptr::null_mut(), false).unwrap_err();
27
28 errno::set_errno(errno::Errno(1));
29
30 #[allow(clippy::as_conversions)]
31 super::SecurityContext::from_result("xyz", -1, 0x1000 as *mut c_char, false).unwrap_err();
32 crate::errors::Error::clear_errno();
33 }
34
35 #[test]
36 fn security_context_from_result_with_name() {
37 super::SecurityContext::from_result_with_name("xyz", 0, ptr::null_mut(), "abc", false)
38 .unwrap_err();
39
40 errno::set_errno(errno::Errno(1));
41
42 #[allow(clippy::as_conversions)]
43 super::SecurityContext::from_result_with_name("xyz", -1, 0x1000 as *mut c_char, "abc", false)
44 .unwrap_err();
45 crate::errors::Error::clear_errno();
46 }
47
48 #[test]
49 fn security_context_from_result_with_pid() {
50 super::SecurityContext::from_result_with_pid("xyz", 0, ptr::null_mut(), 1, false).unwrap_err();
51
52 errno::set_errno(errno::Errno(1));
53
54 #[allow(clippy::as_conversions)]
55 super::SecurityContext::from_result_with_pid("xyz", -1, 0x1000 as *mut c_char, 1, false)
56 .unwrap_err();
57 crate::errors::Error::clear_errno();
58 }
59
60 #[test]
61 fn security_context_parse_context_color() {
62 use super::{LayerColors, RGB, SecurityContext, SecurityContextColors};
63
64 for &bytes in &[
65 b"".as_slice(),
66 b" ",
67 b" ",
68 b"s",
69 b"s t",
70 b"s t u",
71 b"s t u v",
72 b"s t u v w",
73 b"s t u v w x",
74 b"s t u v w x y",
75 b"s t u v w x y z",
76 b"# # # # # # # #",
77 b"#s #t #u #v #w #x #y #z",
78 b"#0",
79 b"#0 #0",
80 b"#0 #0 #0",
81 b"#0 #0 #0 #0",
82 b"#0 #0 #0 #0 #0",
83 b"#0 #0 #0 #0 #0 #0",
84 b"#0 #0 #0 #0 #0 #0 #0",
85 b"#-1 #0 #0 #0 #0 #0 #0 #0",
86 b"#100000000 #0 #0 #0 #0 #0 #0 #0",
87 b"#1000000 #0 #0 #0 #0 #0 #0 #0",
88 ] {
89 SecurityContext::parse_context_color(bytes).unwrap_err();
90 }
91
92 let bytes = b"#11 #22 #aa #bb #cc #dd #ee #ff";
93 let colors = SecurityContext::parse_context_color(bytes).unwrap();
94 let expected_colors = SecurityContextColors::new(
95 LayerColors::new(RGB::new(0x22, 0, 0), RGB::new(0x11, 0, 0)),
96 LayerColors::new(RGB::new(0xbb, 0, 0), RGB::new(0xaa, 0, 0)),
97 LayerColors::new(RGB::new(0xdd, 0, 0), RGB::new(0xcc, 0, 0)),
98 LayerColors::new(RGB::new(0xff, 0, 0), RGB::new(0xee, 0, 0)),
99 );
100 assert_eq!(colors, expected_colors);
101 }
102
103 #[test]
104 fn security_context_color() {
105 use super::{LayerColors, RGB, SecurityContextColors};
106
107 let scc = SecurityContextColors::new(
108 LayerColors::new(RGB::new(0x22, 0, 0), RGB::new(0x11, 0, 0)),
109 LayerColors::new(RGB::new(0xbb, 0, 0), RGB::new(0xaa, 0, 0)),
110 LayerColors::new(RGB::new(0xdd, 0, 0), RGB::new(0xcc, 0, 0)),
111 LayerColors::new(RGB::new(0xff, 0, 0), RGB::new(0xee, 0, 0)),
112 );
113
114 let scc_clone = super::SecurityContextColors::clone(&scc);
115 assert_eq!(scc, scc_clone);
116 assert!(scc >= scc_clone);
117 assert!(scc <= scc_clone);
118 assert_ne!(scc, super::SecurityContextColors::default());
119 let _ignored = format!("{:?}", &scc);
120 let mut ht = HashSet::new();
121 ht.insert(scc_clone);
122 }
123
124 #[test]
125 fn security_context_current() {
126 let mut context = super::SecurityContext::current(false).unwrap();
127 assert!(!context.as_ptr().is_null());
128 assert!(!context.as_mut_ptr().is_null());
129 assert!(!context.as_bytes().is_empty());
130
131 if let Err(err) = context.is_customizable() {
132 assert_matches!(err, crate::errors::Error::IO { .. });
133 if let crate::errors::Error::IO { source, .. } = err {
134 let errno = source.raw_os_error().unwrap();
135 assert!(errno == libc::EINVAL || errno == libc::ENOTDIR);
136 }
137 }
138
139 let r = context.check();
140 assert!(r.is_none() || r == Some(true));
141
142 let _canon_context = context.canonicalize().unwrap();
143
144 let _securetty = context.check_securetty_context();
145
146 //let _color = context.to_color().unwrap();
147
148 context.to_translated_format().unwrap_err();
149
150 let raw_context = context.to_raw_format().unwrap();
151 raw_context.to_raw_format().unwrap_err();
152
153 let _canon_raw_context = raw_context.canonicalize().unwrap();
154
155 let r = raw_context.check();
156 assert!(r.is_none() || r == Some(true));
157
158 let _context = raw_context.to_translated_format().unwrap();
159
160 let _raw_context = super::SecurityContext::current(true).unwrap();
161
162 let _cmp = context.compare_user_insensitive(&raw_context);
163 }
164
165 #[test]
166 fn security_context_previous() {
167 let _context = super::SecurityContext::previous(false).unwrap();
168 let _context = super::SecurityContext::previous(true).unwrap();
169 }
170
171 #[test]
172 fn security_context_set_as_current() {
173 for &raw_format in &[false, true] {
174 let context = super::SecurityContext::current(raw_format).unwrap();
175 context.set_as_current().unwrap();
176 }
177 }
178
179 #[test]
180 fn security_context_of_next_exec() {
181 let _context = super::SecurityContext::of_next_exec(false).unwrap();
182 let _context = super::SecurityContext::of_next_exec(true).unwrap();
183 }
184
185 #[test]
186 fn security_context_set_default_context_for_next_exec() {
187 super::SecurityContext::set_default_context_for_next_exec().unwrap();
188 }
189
190 #[test]
191 fn security_context_set_for_next_exec() {
192 for &raw_format in &[false, true] {
193 let old_context = super::SecurityContext::of_next_exec(raw_format).unwrap();
194
195 let context = super::SecurityContext::current(raw_format).unwrap();
196 context.set_for_next_exec().unwrap();
197
198 if let Some(context) = old_context {
199 context.set_for_next_exec().unwrap();
200 } else {
201 super::SecurityContext::set_default_context_for_next_exec().unwrap();
202 }
203 }
204 }
205
206 #[test]
207 fn security_context_of_new_file_system_objects() {
208 let _context = super::SecurityContext::of_new_file_system_objects(false).unwrap();
209 let _context = super::SecurityContext::of_new_file_system_objects(true).unwrap();
210 }
211
212 #[test]
213 fn security_context_set_default_context_for_new_file_system_objects() {
214 super::SecurityContext::set_default_context_for_new_file_system_objects().unwrap();
215 }
216
217 #[test]
218 fn security_context_set_for_new_file_system_objects() {
219 for &raw_format in &[false, true] {
220 let old_context = super::SecurityContext::of_new_file_system_objects(raw_format).unwrap();
221
222 let context = super::SecurityContext::current(raw_format).unwrap();
223 context.set_for_new_file_system_objects(raw_format).unwrap();
224
225 if let Some(context) = old_context {
226 context.set_for_new_file_system_objects(raw_format).unwrap();
227 } else {
228 super::SecurityContext::set_default_context_for_new_file_system_objects().unwrap();
229 }
230 }
231 }
232
233 #[test]
234 fn security_context_of_new_kernel_key_rings() {
235 let _context = super::SecurityContext::of_new_kernel_key_rings(false).unwrap();
236 let _context = super::SecurityContext::of_new_kernel_key_rings(true).unwrap();
237 }
238
239 #[test]
240 fn security_context_set_default_context_for_new_kernel_key_rings() {
241 super::SecurityContext::set_default_context_for_new_kernel_key_rings().unwrap();
242 }
243
244 #[test]
245 fn security_context_set_for_new_kernel_key_rings() {
246 for &raw_format in &[false, true] {
247 let old_context = super::SecurityContext::of_new_kernel_key_rings(raw_format).unwrap();
248
249 let context = super::SecurityContext::current(raw_format).unwrap();
250 context.set_for_new_kernel_key_rings(raw_format).unwrap();
251
252 if let Some(context) = old_context {
253 context.set_for_new_kernel_key_rings(raw_format).unwrap();
254 } else {
255 super::SecurityContext::set_default_context_for_new_kernel_key_rings().unwrap();
256 }
257 }
258 }
259
260 #[test]
261 fn security_context_of_new_labeled_sockets() {
262 let _context = super::SecurityContext::of_new_labeled_sockets(false).unwrap();
263 let _context = super::SecurityContext::of_new_labeled_sockets(true).unwrap();
264 }
265
266 #[test]
267 fn security_context_set_default_context_for_new_labeled_sockets() {
268 super::SecurityContext::set_default_context_for_new_labeled_sockets().unwrap();
269 }
270
271 #[test]
272 fn security_context_set_for_new_labeled_sockets() {
273 for &raw_format in &[false, true] {
274 let old_context = super::SecurityContext::of_new_labeled_sockets(raw_format).unwrap();
275
276 let context = super::SecurityContext::current(raw_format).unwrap();
277 context.set_for_new_labeled_sockets(raw_format).unwrap();
278
279 if let Some(context) = old_context {
280 context.set_for_new_labeled_sockets(raw_format).unwrap();
281 } else {
282 super::SecurityContext::set_default_context_for_new_labeled_sockets().unwrap();
283 }
284 }
285 }
286
287 #[test]
288 fn security_context_of_initial_kernel_context() {
289 for &raw_format in &[false, true] {
290 let _context =
291 super::SecurityContext::of_initial_kernel_context("unlabeled", raw_format).unwrap();
292 }
293 }
294
295 #[test]
296 fn security_context_of_process() {
297 #[allow(clippy::cast_possible_wrap, clippy::as_conversions)]
298 let pid = process::id() as c_int;
299 for &raw_format in &[false, true] {
300 let _context = super::SecurityContext::of_process(pid, raw_format).unwrap();
301 }
302 }
303
304 #[test]
305 fn security_context_of_se_user_with_selected_context() {
306 //let _context =
307 // super::SecurityContext::of_se_user_with_selected_context("unconfined_u", false).unwrap();
308 }
309
310 #[test]
311 fn security_context_default_for_se_user() {
312 let _context =
313 super::SecurityContext::default_for_se_user("unconfined_u", None, None, None, false)
314 .unwrap();
315
316 let _context = super::SecurityContext::default_for_se_user(
317 "unconfined_u",
318 Some("unconfined_r"),
319 None,
320 None,
321 false,
322 )
323 .unwrap();
324
325 let _context =
326 super::SecurityContext::default_for_se_user("unconfined_u", None, Some("low"), None, false)
327 .unwrap();
328
329 let _context = super::SecurityContext::default_for_se_user(
330 "unconfined_u",
331 Some("unconfined_r"),
332 Some("low"),
333 None,
334 false,
335 )
336 .unwrap();
337
338 let context = super::SecurityContext::current(false).unwrap();
339 let _context = super::SecurityContext::default_for_se_user(
340 "unconfined_u",
341 None,
342 None,
343 Some(&context),
344 false,
345 )
346 .unwrap();
347 }
348
349 #[test]
350 fn security_context_of_media_type() {
351 super::SecurityContext::of_media_type("invalid").unwrap_err();
352 //let _context = super::SecurityContext::of_media_type("unlabeled").unwrap();
353 }
354
355 #[test]
356 fn security_context_of_labeling_decision() {
357 let context = super::SecurityContext::current(false).unwrap();
358 let raw_context = super::SecurityContext::current(true).unwrap();
359 let target_class = super::SecurityClass::from_name("process").unwrap();
360 context
361 .of_labeling_decision(&raw_context, target_class, "process")
362 .unwrap_err();
363 let _new_context = context
364 .of_labeling_decision(&context, target_class, "process")
365 .unwrap();
366 let _new_context = raw_context
367 .of_labeling_decision(&raw_context, target_class, "process")
368 .unwrap();
369 }
370
371 #[test]
372 fn security_context_of_relabeling_decision() {
373 let context = super::SecurityContext::current(false).unwrap();
374 let raw_context = super::SecurityContext::current(true).unwrap();
375 let target_class = super::SecurityClass::from_name("process").unwrap();
376 context
377 .of_relabeling_decision(&raw_context, target_class)
378 .unwrap_err();
379 let _new_context = context
380 .of_relabeling_decision(&context, target_class)
381 .unwrap();
382 let _new_context = raw_context
383 .of_relabeling_decision(&raw_context, target_class)
384 .unwrap();
385 }
386
387 #[test]
388 fn security_context_of_polyinstantiation_member_decision() {
389 let context = super::SecurityContext::current(false).unwrap();
390 let raw_context = super::SecurityContext::current(true).unwrap();
391 let target_class = super::SecurityClass::from_name("process").unwrap();
392 context
393 .of_polyinstantiation_member_decision(&raw_context, target_class)
394 .unwrap_err();
395 let _new_context = context
396 .of_polyinstantiation_member_decision(&context, target_class)
397 .unwrap();
398 let _new_context = raw_context
399 .of_polyinstantiation_member_decision(&raw_context, target_class)
400 .unwrap();
401 }
402
403 #[test]
404 fn security_context_validate_transition() {
405 let context = super::SecurityContext::current(false).unwrap();
406 let raw_context = super::SecurityContext::current(true).unwrap();
407 let target_class = super::SecurityClass::from_name("process").unwrap();
408 context
409 .validate_transition(&raw_context, target_class, &raw_context)
410 .unwrap_err();
411
412 if let Err(r) = context.validate_transition(&context, target_class, &context) {
413 assert_eq!(r.io_source().unwrap().raw_os_error(), Some(libc::ENOSYS));
414 }
415
416 if let Err(r) = raw_context.validate_transition(&raw_context, target_class, &raw_context) {
417 assert_eq!(r.io_source().unwrap().raw_os_error(), Some(libc::ENOSYS));
418 }
419 }
420
421 #[test]
422 fn security_context_query_access_decision() {
423 let context = super::SecurityContext::current(false).unwrap();
424 let raw_context = super::SecurityContext::current(true).unwrap();
425 let target_class = super::SecurityClass::from_name("process").unwrap();
426 context
427 .query_access_decision(&raw_context, target_class, 0)
428 .unwrap_err();
429 let _new_context = context
430 .query_access_decision(&context, target_class, 0)
431 .unwrap();
432 let _new_context = raw_context
433 .query_access_decision(&raw_context, target_class, 0)
434 .unwrap();
435 }
436
437 #[test]
438 fn security_context_check_access() {
439 let context = super::SecurityContext::current(false).unwrap();
440 let raw_context = super::SecurityContext::current(true).unwrap();
441 let _new_context = context
442 .check_access(&context, "process", "read", ptr::null_mut())
443 .unwrap();
444 let _new_context = raw_context
445 .check_access(&raw_context, "process", "read", ptr::null_mut())
446 .unwrap();
447 }
448
449 #[test]
450 fn security_context_of_path() {
451 for &raw_format in &[false, true] {
452 for &follow_symbolic_links in &[false, true] {
453 for &path in &["/", "/etc/fstab"] {
454 let r = super::SecurityContext::of_path(path, follow_symbolic_links, raw_format);
455 let _context = r.unwrap();
456 }
457 }
458 }
459
460 let _context = super::SecurityContext::of_path("/non-existent", false, false).unwrap_err();
461 }
462
463 #[test]
464 fn security_context_set_default_for_path() {
465 super::SecurityContext::set_default_for_path("non-existent").unwrap_err();
466
467 /*
468 let file = tempfile::NamedTempFile::new().unwrap();
469 super::SecurityContext::set_default_for_path(file.path()).unwrap();
470 */
471 }
472
473 #[test]
474 fn security_context_set_for_path() {
475 use std::os::unix::fs::symlink;
476
477 let context = super::SecurityContext::current(false).unwrap();
478 context
479 .set_for_path(Path::new("/non-existent"), false, false)
480 .unwrap_err();
481
482 let context = super::SecurityContext::from_c_str(c"unconfined_u:object_r:user_tmp_t:s0", false);
483
484 let dir = tempfile::TempDir::new().unwrap();
485 let a = dir.path().join("a.txt");
486 let la = dir.path().join("la.txt");
487 fs::write(&a, "empty file").unwrap();
488 symlink(&a, &la).unwrap();
489
490 for &raw_format in &[false, true] {
491 for &follow_symbolic_links in &[false, true] {
492 context
493 .set_for_path(la.as_path(), follow_symbolic_links, raw_format)
494 .unwrap();
495 }
496 }
497
498 super::SecurityContext::verify_file_context("/non-existent", None).unwrap_err();
499
500 /*
501 super::SecurityContext::verify_file_context(
502 &la,
503 super::FileAccessMode::new(libc::S_IFREG | libc::S_IRUSR),
504 )
505 .unwrap();
506 */
507 }
508
509 #[test]
510 fn security_context_of_file() {
511 let mut file = tempfile::tempfile().unwrap();
512 writeln!(file, "empty file").unwrap();
513 let optional_context = super::SecurityContext::of_file(&file, false).unwrap();
514 let optional_raw_context = super::SecurityContext::of_file(&file, true).unwrap();
515
516 if let Some(context) = optional_context {
517 context.set_for_file(&file).unwrap();
518 }
519
520 if let Some(raw_context) = optional_raw_context {
521 raw_context.set_for_file(&file).unwrap();
522 }
523 }
524
525 #[test]
526 fn security_context_of_peer_socket() {
527 let (s1, s2) = socketpair::socketpair_stream().unwrap();
528
529 let _context = super::SecurityContext::of_peer_socket(&s1, false).unwrap();
530 let _raw_context = super::SecurityContext::of_peer_socket(&s2, true).unwrap();
531 }
532
533 #[test]
534 fn rgb() {
535 let rgb = super::RGB::new(0x22, 0, 0);
536 let rgb_clone = super::RGB::clone(&rgb);
537 assert_eq!(rgb, rgb_clone);
538 assert!(rgb >= rgb_clone);
539 assert!(rgb <= rgb_clone);
540 assert_ne!(rgb, super::RGB::default());
541 let _ignored = format!("{:?}", &rgb);
542 let mut ht = HashSet::new();
543 ht.insert(rgb_clone);
544 }
545
546 #[test]
547 fn layer_colors() {
548 let lc = super::LayerColors::new(super::RGB::new(0x22, 0, 0), super::RGB::new(0x11, 0, 0));
549 let lc_clone = super::LayerColors::clone(&lc);
550 assert_eq!(lc, lc_clone);
551 assert!(lc >= lc_clone);
552 assert!(lc <= lc_clone);
553 assert_ne!(lc, super::LayerColors::default());
554 let _ignored = format!("{:?}", &lc);
555 let mut ht = HashSet::new();
556 ht.insert(lc_clone);
557 }
558
559 #[test]
560 fn file_access_mode() {
561 assert!(super::FileAccessMode::new(0).is_none());
562
563 let mode = super::FileAccessMode::new(42);
564 assert_eq!(mode, Some(super::FileAccessMode(42)));
565 assert_eq!(mode.unwrap().mode(), 42);
566
567 let _ignored = format!("{:?}", &mode);
568 }
569
570 #[test]
571 fn security_class_new() {
572 super::SecurityClass::new(0).unwrap_err();
573
574 let sc = super::SecurityClass::new(1).unwrap();
575 assert_eq!(sc.value(), 1);
576
577 let _ignored = format!("{:?}", &sc);
578 let _ignored = format!("{}", &sc);
579
580 let _sc = super::SecurityClass::from_name("invalid").unwrap_err();
581
582 let _sc = super::SecurityClass::try_from(
583 super::FileAccessMode::new(libc::S_IFREG | libc::S_IRUSR).unwrap(),
584 )
585 .unwrap();
586
587 super::SecurityClass::try_from(super::FileAccessMode::new(1).unwrap()).unwrap_err();
588
589 let sc = super::SecurityClass::from_name("process").unwrap();
590 let _ignored = format!("{:?}", &sc);
591 let _ignored = format!("{}", &sc);
592
593 unsafe { sc.access_vector_bit_name(0) }.unwrap_err();
594 let _name = unsafe { sc.access_vector_bit_name(1) }.unwrap();
595
596 let _name = sc.full_access_vector_name(0).unwrap();
597 let _name = sc.full_access_vector_name(1).unwrap();
598 let _name = sc.full_access_vector_name(u32::MAX).unwrap();
599
600 sc.access_vector_bit("invalid").unwrap_err();
601
602 let _av = sc.access_vector_bit("signal").unwrap();
603 }
604
605 #[test]
606 fn opaque_security_context() {
607 for &context in &[
608 "",
609 "user1",
610 "user1:role1",
611 "user1:role1:type1:range1:other1:other2:other3",
612 ] {
613 super::OpaqueSecurityContext::new(context).unwrap_err();
614 }
615
616 for &context in &[
617 "user1:role1:type1",
618 "user1:role1:type1:range1",
619 "user1:role1:type1:range1:other1",
620 "user1:role1:type1:range1:other1:other2",
621 ] {
622 let mut osc = super::OpaqueSecurityContext::new(context).unwrap();
623
624 assert!(!osc.as_ptr().is_null());
625 assert!(!osc.as_mut_ptr().is_null());
626
627 let s = osc.to_c_string().unwrap();
628 assert!(!s.as_bytes().is_empty());
629 assert_eq!(s.to_string_lossy(), format!("{}", &osc));
630
631 assert_eq!(osc.user().unwrap().to_str().ok(), Some("user1"));
632 assert_eq!(osc.role().unwrap().to_str().ok(), Some("role1"));
633 assert_eq!(osc.the_type().unwrap().to_str().ok(), Some("type1"));
634
635 let expected_range = context.splitn(4, ':').nth(3);
636 if let Ok(range) = osc.range() {
637 assert_eq!(range.to_str().ok(), expected_range);
638 } else {
639 assert!(expected_range.is_none());
640 }
641
642 osc.set_user_str("user2").unwrap();
643
644 assert_eq!(osc.user().unwrap().to_str().ok(), Some("user2"));
645 assert_eq!(osc.role().unwrap().to_str().ok(), Some("role1"));
646 assert_eq!(osc.the_type().unwrap().to_str().ok(), Some("type1"));
647 if let Ok(range) = osc.range() {
648 assert_eq!(range.to_str().ok(), expected_range);
649 } else {
650 assert!(expected_range.is_none());
651 }
652
653 osc.set_role_str("role2").unwrap();
654
655 assert_eq!(osc.user().unwrap().to_str().ok(), Some("user2"));
656 assert_eq!(osc.role().unwrap().to_str().ok(), Some("role2"));
657 assert_eq!(osc.the_type().unwrap().to_str().ok(), Some("type1"));
658 if let Ok(range) = osc.range() {
659 assert_eq!(range.to_str().ok(), expected_range);
660 } else {
661 assert!(expected_range.is_none());
662 }
663
664 osc.set_type_str("type2").unwrap();
665
666 assert_eq!(osc.user().unwrap().to_str().ok(), Some("user2"));
667 assert_eq!(osc.role().unwrap().to_str().ok(), Some("role2"));
668 assert_eq!(osc.the_type().unwrap().to_str().ok(), Some("type2"));
669 if let Ok(range) = osc.range() {
670 assert_eq!(range.to_str().ok(), expected_range);
671 } else {
672 assert!(expected_range.is_none());
673 }
674
675 osc.set_range_str("range2").unwrap();
676
677 assert_eq!(osc.user().unwrap().to_str().ok(), Some("user2"));
678 assert_eq!(osc.role().unwrap().to_str().ok(), Some("role2"));
679 assert_eq!(osc.the_type().unwrap().to_str().ok(), Some("type2"));
680 assert_eq!(osc.range().unwrap().to_str().ok(), Some("range2"));
681
682 let _ignored = format!("{:?}", &osc);
683 }
684 }
685
686 #[test]
687 fn kernel_support() {
688 let r = super::kernel_support();
689 let _ignored = format!("{r:?}");
690 }
691
692 #[test]
693 fn boot_mode() {
694 if let Err(err) = super::boot_mode() {
695 assert_matches!(err, crate::errors::Error::IO { .. });
696 if let crate::errors::Error::IO { source, .. } = err {
697 assert_eq!(source.kind(), io::ErrorKind::NotFound);
698 }
699
700 fs::symlink_metadata("/etc/selinux/config").unwrap_err();
701 }
702 }
703
704 #[test]
705 fn current_mode() {
706 let r = super::current_mode();
707 let _ignored = format!("{r:?}");
708 }
709
710 #[test]
711 fn undefined_handling() {
712 if let Err(err) = super::undefined_handling() {
713 assert_matches!(err, crate::errors::Error::IO { .. });
714 if let crate::errors::Error::IO { source, .. } = err {
715 assert_eq!(source.kind(), io::ErrorKind::NotFound);
716 }
717 }
718 }
719
720 #[test]
721 fn protection_checking_mode() {
722 if let Err(err) = super::protection_checking_mode() {
723 assert_matches!(err, crate::errors::Error::IO { .. });
724 if let crate::errors::Error::IO { source, .. } = err {
725 assert_eq!(source.kind(), io::ErrorKind::NotFound);
726 }
727 }
728 }
729
730 #[test]
731 fn dynamic_mapping_into_native_form() {
732 let mut c_string_storage = HashMap::default();
733
734 let empty: &[(&str, &[&str]); 0] = &[];
735 let c_map = super::dynamic_mapping_into_native_form(empty, &mut c_string_storage).unwrap();
736 assert_eq!(c_map.len(), 1 + empty.len());
737 assert!(c_map[0].name.is_null());
738
739 let mapping: &[(&str, &[&str]); 1] = &[("", &[])];
740 let c_map = super::dynamic_mapping_into_native_form(mapping, &mut c_string_storage).unwrap();
741 assert_eq!(c_map.len(), 1 + mapping.len());
742 assert!(c_str_ptr_to_str(c_map[0].name).unwrap().is_empty());
743 assert!(c_map[0].perms[0].is_null());
744 assert!(c_map[1].name.is_null());
745
746 let mapping: &[(&str, &[&str]); 1] = &[("", &["", ""])];
747 let c_map = super::dynamic_mapping_into_native_form(mapping, &mut c_string_storage).unwrap();
748 assert_eq!(c_map.len(), 1 + mapping.len());
749 assert!(c_str_ptr_to_str(c_map[0].name).unwrap().is_empty());
750 assert!(c_str_ptr_to_str(c_map[0].perms[0]).unwrap().is_empty());
751 assert!(c_str_ptr_to_str(c_map[0].perms[1]).unwrap().is_empty());
752 assert!(c_map[0].perms[2].is_null());
753 assert!(c_map[1].name.is_null());
754
755 let mapping: &[(&str, &[&str]); 1] = &[("socket", &["bind"])];
756 let c_map = super::dynamic_mapping_into_native_form(mapping, &mut c_string_storage).unwrap();
757 assert_eq!(c_map.len(), 1 + mapping.len());
758 assert_eq!(c_str_ptr_to_str(c_map[0].name).unwrap(), "socket");
759 assert_eq!(c_str_ptr_to_str(c_map[0].perms[0]).unwrap(), "bind");
760 assert!(c_map[0].perms[1].is_null());
761 assert!(c_map[1].name.is_null());
762
763 let mapping: &[(&str, &[&str]); 2] = &[
764 ("socket", &["bind"]),
765 ("file", &["create", "unlink", "read", "write"]),
766 ];
767 let c_map = super::dynamic_mapping_into_native_form(mapping, &mut c_string_storage).unwrap();
768 assert_eq!(c_map.len(), 1 + mapping.len());
769 assert_eq!(c_str_ptr_to_str(c_map[0].name).unwrap(), "socket");
770 assert_eq!(c_str_ptr_to_str(c_map[0].perms[0]).unwrap(), "bind");
771 assert!(c_map[0].perms[1].is_null());
772 assert_eq!(c_str_ptr_to_str(c_map[1].name).unwrap(), "file");
773 assert_eq!(c_str_ptr_to_str(c_map[1].perms[0]).unwrap(), "create");
774 assert_eq!(c_str_ptr_to_str(c_map[1].perms[1]).unwrap(), "unlink");
775 assert_eq!(c_str_ptr_to_str(c_map[1].perms[2]).unwrap(), "read");
776 assert_eq!(c_str_ptr_to_str(c_map[1].perms[3]).unwrap(), "write");
777 assert!(c_map[1].perms[4].is_null());
778 assert!(c_map[2].name.is_null());
779 }
780
781 #[test]
782 fn security_context_list_of_se_user() {
783 let mut se_list = super::SecurityContextList::of_se_user("unconfined_u", None, None).unwrap();
784 assert_eq!(se_list.as_ptr(), se_list.as_mut_ptr().cast());
785 assert!(!se_list.is_empty());
786 assert_ne!(se_list.len(), 0);
787
788 assert!(se_list.get(usize::MAX, false).is_none());
789 let _context = se_list.get(0, false).unwrap();
790
791 let _ignored = format!("{:?}", &se_list);
792
793 super::SecurityContextList::of_se_user("invalid", None, None).unwrap_err();
794
795 let _se_list =
796 super::SecurityContextList::of_se_user("unconfined_u", Some("low"), None).unwrap();
797
798 let context = super::SecurityContext::current(false).unwrap();
799 let _se_list =
800 super::SecurityContextList::of_se_user("unconfined_u", None, Some(&context)).unwrap();
801 }
802
803 #[test]
804 fn set_current_mode() {
805 super::set_current_mode(super::SELinuxMode::NotRunning).unwrap_err();
806 super::set_current_mode(super::SELinuxMode::Permissive).unwrap_err();
807 super::set_current_mode(super::SELinuxMode::Enforcing).unwrap_err();
808 }
809
810 #[test]
811 fn se_user_and_level() {
812 let (se_user, level) = super::se_user_and_level("root", None).unwrap();
813 assert!(!se_user.as_c_str().to_bytes().is_empty());
814 assert!(!level.as_c_str().to_bytes().is_empty());
815
816 let (_se_user, _level) = super::se_user_and_level("root", Some("file")).unwrap();
817 }
818
819 #[test]
820 fn reset_config() {
821 super::reset_config();
822 }
823
824 #[test]
825 fn default_type_for_role() {
826 //let _type = super::default_type_for_role("unconfined_r").unwrap();
827 }
828
829 #[test]
830 #[allow(clippy::as_conversions)]
831 fn set_dynamic_mapping() {
832 super::set_dynamic_mapping(&[] as &[(&str, &[&str])]).unwrap();
833 super::set_dynamic_mapping(&[("file", &["read", "write"] as &[&str])]).unwrap();
834 }