From c6af13f3af5ccd467fd08c5afb8752e5201c9f15 Mon Sep 17 00:00:00 2001 From: dietrich Date: Fri, 2 Oct 2020 12:40:03 +0200 Subject: [PATCH] Add is system user --- src/passwd.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/passwd.rs b/src/passwd.rs index 4dd412e..0285894 100644 --- a/src/passwd.rs +++ b/src/passwd.rs @@ -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)] pub struct Gid { 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. /// /// 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); } +#[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] fn test_parse_gecos() { // test if the Gecos field can be parsed and the resulting struct is populated correctly.