Adding tests

This commit is contained in:
Dietrich 2020-09-21 14:37:21 +02:00
parent cd19cb7115
commit 1bd6031e5a
2 changed files with 48 additions and 5 deletions

View File

@ -2,10 +2,10 @@ extern crate adduser;
use adduser::passwd::Passwd; use adduser::passwd::Passwd;
use std::fs::File; use std::fs::File;
use std::io::{self, prelude::*, BufReader}; use std::io::{prelude::*, BufReader};
fn main() { fn main() {
let mut file = File::open("/etc/passwd").unwrap(); let file = File::open("/etc/passwd").unwrap();
let reader = BufReader::new(file); let reader = BufReader::new(file);
for line in reader.lines() { for line in reader.lines() {

View File

@ -110,17 +110,19 @@ fn parse_gecos(source: &str) -> Result<Gecos, &str> {
impl Default for Passwd<'_> { impl Default for Passwd<'_> {
fn default() -> Self { fn default() -> Self {
Passwd { Passwd {
pw_name: Username { pw_name: "howdy" }, pw_name: Username {
pw_name: "defaultuser",
},
pw_passwd: Password { pw_passwd: Password {
pw_passwd: "notencrypted", pw_passwd: "notencrypted",
}, },
pw_uid: Uid { pw_uid: 1001 }, pw_uid: Uid { pw_uid: 1001 },
pw_gid: Gid { pw_gid: 1001 }, pw_gid: Gid { pw_gid: 1001 },
pw_gecos: Gecos::Simple { pw_gecos: Gecos::Simple {
comment: "not done", comment: "gecos default comment",
}, },
pw_dir: HomeDir { pw_dir: HomeDir {
pw_dir: "/home/test", pw_dir: "/home/default",
}, },
pw_shell: ShellDir { pw_shell: ShellDir {
pw_shell: "/bin/bash", pw_shell: "/bin/bash",
@ -199,3 +201,44 @@ impl Display for ShellDir<'_> {
write!(f, "{}", self.pw_shell,) write!(f, "{}", self.pw_shell,)
} }
} }
#[test]
fn default_user() {
let pwd = Passwd::default();
assert_eq!(pwd.pw_name.pw_name, "defaultuser");
assert_eq!(pwd.pw_dir.pw_dir, "/home/default");
assert_eq!(pwd.pw_uid.pw_uid, 1001);
}
#[test]
fn test_new_from_string() {
let pwd =
Passwd::new_from_string("testuser:testpassword:1001:1001:testcomment:/home/test:/bin/test")
.unwrap();
let pwd2 =
Passwd::new_from_string("testuser:testpassword:1001:1001:full Name,004,000342,001-2312,myemail@test.com:/home/test:/bin/test")
.unwrap();
assert_eq!(pwd.pw_name.pw_name, "testuser");
assert_eq!(pwd.pw_dir.pw_dir, "/home/test");
assert_eq!(pwd.pw_uid.pw_uid, 1001);
match pwd.pw_gecos {
Gecos::Simple { comment } => assert_eq!(comment, "testcomment"),
_ => unreachable!(),
}
match pwd2.pw_gecos {
Gecos::Detail {
full_name,
room,
phone_work,
phone_home,
other,
} => {
assert_eq!(full_name, "full Name");
assert_eq!(room, "004");
assert_eq!(phone_work, "000342");
assert_eq!(phone_home, "001-2312");
assert_eq!(other, "myemail@test.com");
}
_ => unreachable!(),
}
}