|
| 1 | +use ansi_term::Style; |
| 2 | + |
| 3 | +use crate::output::cell::TextCell; |
| 4 | +use crate::fs::fields as f; |
| 5 | + |
| 6 | +impl f::OctalPermissions { |
| 7 | + fn bits_to_octal(r: bool, w: bool, x: bool) -> u8 { |
| 8 | + (r as u8) * 4 + (w as u8) * 2 + (x as u8) * 1 |
| 9 | + } |
| 10 | + |
| 11 | + pub fn render(&self, style: Style) -> TextCell { |
| 12 | + |
| 13 | + let perm = &self.permissions; |
| 14 | + let octal_sticky = Self::bits_to_octal(perm.setuid, perm.setgid, perm.sticky); |
| 15 | + let octal_owner = Self::bits_to_octal(perm.user_read, perm.user_write, perm.user_execute); |
| 16 | + let octal_group = Self::bits_to_octal(perm.group_read, perm.group_write, perm.group_execute); |
| 17 | + let octal_other = Self::bits_to_octal(perm.other_read, perm.other_write, perm.other_execute); |
| 18 | + |
| 19 | + TextCell::paint(style, format!("{}{}{}{}", octal_sticky, octal_owner, octal_group, octal_other)) |
| 20 | + } |
| 21 | + |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(test)] |
| 25 | +pub mod test { |
| 26 | + use crate::output::cell::TextCell; |
| 27 | + use crate::fs::fields as f; |
| 28 | + |
| 29 | + use ansi_term::Colour::*; |
| 30 | + |
| 31 | + |
| 32 | + #[test] |
| 33 | + fn normal_folder() { |
| 34 | + let bits = f::Permissions { |
| 35 | + user_read: true, user_write: true, user_execute: true, setuid: false, |
| 36 | + group_read: true, group_write: false, group_execute: true, setgid: false, |
| 37 | + other_read: true, other_write: false, other_execute: true, sticky: false, |
| 38 | + }; |
| 39 | + |
| 40 | + let octal = f::OctalPermissions{ permissions: bits }; |
| 41 | + |
| 42 | + let expected = TextCell::paint_str(Purple.bold(), "0755"); |
| 43 | + assert_eq!(expected, octal.render(Purple.bold()).into()); |
| 44 | + } |
| 45 | + |
| 46 | + #[test] |
| 47 | + fn normal_file() { |
| 48 | + let bits = f::Permissions { |
| 49 | + user_read: true, user_write: true, user_execute: false, setuid: false, |
| 50 | + group_read: true, group_write: false, group_execute: false, setgid: false, |
| 51 | + other_read: true, other_write: false, other_execute: false, sticky: false, |
| 52 | + }; |
| 53 | + |
| 54 | + let octal = f::OctalPermissions{ permissions: bits }; |
| 55 | + |
| 56 | + let expected = TextCell::paint_str(Purple.bold(), "0644"); |
| 57 | + assert_eq!(expected, octal.render(Purple.bold()).into()); |
| 58 | + } |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn secret_file() { |
| 62 | + let bits = f::Permissions { |
| 63 | + user_read: true, user_write: true, user_execute: false, setuid: false, |
| 64 | + group_read: false, group_write: false, group_execute: false, setgid: false, |
| 65 | + other_read: false, other_write: false, other_execute: false, sticky: false, |
| 66 | + }; |
| 67 | + |
| 68 | + let octal = f::OctalPermissions{ permissions: bits }; |
| 69 | + |
| 70 | + let expected = TextCell::paint_str(Purple.bold(), "0600"); |
| 71 | + assert_eq!(expected, octal.render(Purple.bold()).into()); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn sticky1() { |
| 76 | + let bits = f::Permissions { |
| 77 | + user_read: true, user_write: true, user_execute: true, setuid: true, |
| 78 | + group_read: true, group_write: true, group_execute: true, setgid: false, |
| 79 | + other_read: true, other_write: true, other_execute: true, sticky: false, |
| 80 | + }; |
| 81 | + |
| 82 | + let octal = f::OctalPermissions{ permissions: bits }; |
| 83 | + |
| 84 | + let expected = TextCell::paint_str(Purple.bold(), "4777"); |
| 85 | + assert_eq!(expected, octal.render(Purple.bold()).into()); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn sticky2() { |
| 91 | + let bits = f::Permissions { |
| 92 | + user_read: true, user_write: true, user_execute: true, setuid: false, |
| 93 | + group_read: true, group_write: true, group_execute: true, setgid: true, |
| 94 | + other_read: true, other_write: true, other_execute: true, sticky: false, |
| 95 | + }; |
| 96 | + |
| 97 | + let octal = f::OctalPermissions{ permissions: bits }; |
| 98 | + |
| 99 | + let expected = TextCell::paint_str(Purple.bold(), "2777"); |
| 100 | + assert_eq!(expected, octal.render(Purple.bold()).into()); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn sticky3() { |
| 105 | + let bits = f::Permissions { |
| 106 | + user_read: true, user_write: true, user_execute: true, setuid: false, |
| 107 | + group_read: true, group_write: true, group_execute: true, setgid: false, |
| 108 | + other_read: true, other_write: true, other_execute: true, sticky: true, |
| 109 | + }; |
| 110 | + |
| 111 | + let octal = f::OctalPermissions{ permissions: bits }; |
| 112 | + |
| 113 | + let expected = TextCell::paint_str(Purple.bold(), "1777"); |
| 114 | + assert_eq!(expected, octal.render(Purple.bold()).into()); |
| 115 | + } |
| 116 | +} |
0 commit comments