Add is system user

This commit is contained in:
dietrich 2020-10-02 12:40:03 +02:00
parent 9b4956c78a
commit c6af13f3af

View File

@ -96,6 +96,14 @@ impl TryFrom<&str> for Uid {
} }
} }
impl Uid {
#[must_use]
pub const fn is_system_uid(&self) -> bool {
// since it is a u32 it cannot be smaller than 0
self.uid < 1000
}
}
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct Gid { pub struct Gid {
gid: u32, gid: u32,
@ -116,6 +124,14 @@ impl TryFrom<&str> for Gid {
} }
} }
impl Gid {
#[must_use]
pub const fn is_system_gid(&self) -> bool {
// since it is a u32 it cannot be smaller than 0
self.gid < 1000
}
}
/// The gecos field of a user. /// The gecos field of a user.
/// ///
/// In the `/etc/passwd` file this field is a `,` sepparated list of items. /// In the `/etc/passwd` file this field is a `,` sepparated list of items.
@ -455,6 +471,16 @@ fn test_default_user() {
assert_eq!(pwd.uid.uid, 1001); assert_eq!(pwd.uid.uid, 1001);
} }
#[test]
fn test_guid_system_user() {
// Check uids of system users.
let values = vec![("999", true), ("0", true), ("1000", false)];
for val in values {
assert_eq!(Uid::try_from(val.0).unwrap().is_system_uid(), val.1);
assert_eq!(Gid::try_from(val.0).unwrap().is_system_gid(), val.1);
}
}
#[test] #[test]
fn test_parse_gecos() { fn test_parse_gecos() {
// test if the Gecos field can be parsed and the resulting struct is populated correctly. // test if the Gecos field can be parsed and the resulting struct is populated correctly.