modify the shadow file and test the binary

This commit is contained in:
Dietrich 2020-11-15 17:04:31 +01:00
parent ed77466bd6
commit 3dae3c4109
5 changed files with 93 additions and 198 deletions

177
Cargo.lock generated
View File

@ -1,19 +1,9 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "addr2line"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c0929d69e78dd9bf5408269919fcbcaeb2e35e5d43e5815517cdc6a8e11a423"
dependencies = [
"gimli",
]
[[package]] [[package]]
name = "adduser" name = "adduser"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"assert_cli",
"chrono", "chrono",
"clap", "clap",
"derive_builder", "derive_builder",
@ -23,14 +13,9 @@ dependencies = [
"regex", "regex",
"simplelog", "simplelog",
"tempfile", "tempfile",
"test_bin",
] ]
[[package]]
name = "adler"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.13" version = "0.7.13"
@ -40,20 +25,6 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "assert_cli"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a29ab7c0ed62970beb0534d637a8688842506d0ff9157de83286dacd065c8149"
dependencies = [
"colored",
"difference",
"environment",
"failure",
"failure_derive",
"serde_json",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -71,20 +42,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2baad346b2d4e94a24347adeee9c7a93f412ee94b9cc26e5b59dea23848e9f28"
dependencies = [
"addr2line",
"cfg-if 1.0.0",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -97,12 +54,6 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.19" version = "0.4.19"
@ -148,17 +99,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "colored"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.10.2" version = "0.10.2"
@ -219,12 +159,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "difference"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.8.1" version = "0.8.1"
@ -238,34 +172,6 @@ dependencies = [
"termcolor", "termcolor",
] ]
[[package]]
name = "environment"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4b14e20978669064c33b4c1e0fb4083412e40fe56cbea2eae80fd7591503ee"
[[package]]
name = "failure"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -278,17 +184,11 @@ version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if",
"libc", "libc",
"wasi 0.9.0+wasi-snapshot-preview1", "wasi 0.9.0+wasi-snapshot-preview1",
] ]
[[package]]
name = "gimli"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.9.1" version = "0.9.1"
@ -335,12 +235,6 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -359,7 +253,7 @@ version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if",
] ]
[[package]] [[package]]
@ -368,16 +262,6 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "miniz_oxide"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f2d26ec3309788e423cfbf68ad1800f061638098d76a83681af979dc4eda19d"
dependencies = [
"adler",
"autocfg",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.43" version = "0.1.43"
@ -397,12 +281,6 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "object"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
[[package]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "2.4.0" version = "2.4.0"
@ -531,35 +409,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232"
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "serde"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
[[package]]
name = "serde_json"
version = "1.0.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "simplelog" name = "simplelog"
version = "0.8.0" version = "0.8.0"
@ -594,25 +443,13 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "synstructure"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.1.0" version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if",
"libc", "libc",
"rand", "rand",
"redox_syscall", "redox_syscall",
@ -629,6 +466,12 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "test_bin"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1937bab04b0bd0f2c19628d3408fb6dd78faf5aa60f4bdb03212507a9b7314ba"
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.12.1" version = "0.12.1"

View File

@ -21,10 +21,11 @@ chrono = "0.4"
env_logger = "0.8" env_logger = "0.8"
derive_builder = "0.9" derive_builder = "0.9"
clap = "3.0.0-beta" clap = "3.0.0-beta"
assert_cli = "0.6"
[dev-dependencies] [dev-dependencies]
tempfile = "3.1" tempfile = "3.1"
test_bin= "0.3"
# optimize for size at cost of compilation speed. # optimize for size at cost of compilation speed.
[profile.release] [profile.release]

View File

@ -26,6 +26,11 @@ impl Shadow {
pub fn get_username(&self) -> &str { pub fn get_username(&self) -> &str {
&self.username.username &self.username.username
} }
pub fn set_username(&mut self, username: crate::Username) {
self.username = username;
}
#[must_use] #[must_use]
pub fn get_password(&self) -> &str { pub fn get_password(&self) -> &str {
&self.password.password &self.password.password

View File

@ -231,10 +231,16 @@ impl UserDBWrite for UserDBLocal {
Err("Failed to create the user. A user with the same Name already exists".into()) Err("Failed to create the user. A user with the same Name already exists".into())
} else { } else {
let opened = self.source_files.lock_all_get(); let opened = self.source_files.lock_all_get();
let (mut locked_p, mut _locked_s, mut _locked_g) = let (mut locked_p, mut locked_s, mut _locked_g) =
opened.expect("failed to lock files!"); opened.expect("failed to lock files!");
dbg!(&locked_p); //dbg!(&locked_p);
locked_p.append(format!("{}", new_user))?; locked_p.append(format!("{}", new_user))?;
if let Some(shadow) = new_user.get_shadow() {
info!("Adding shadow entry {}", shadow);
locked_s.append(format!("{}", shadow))?;
} else {
warn!("Omitting shadow entry!")
}
assert!(self assert!(self
.users .users
.insert(args.username.to_owned(), new_user) .insert(args.username.to_owned(), new_user)

View File

@ -1,4 +1,5 @@
extern crate adduser; extern crate adduser;
extern crate test_bin;
mod testfiles; mod testfiles;
#[test] #[test]
@ -30,54 +31,93 @@ fn test_create_user_function() {
.build() .build()
.unwrap(), .unwrap(),
); );
let pf2 = fs::read_to_string(&p.path).unwrap(); let password_file_string = fs::read_to_string(&p.path).unwrap();
let shadow_file_string = fs::read_to_string(&p.path).unwrap();
assert_eq!(user_res.unwrap().get_username().unwrap(), "test2"); assert_eq!(user_res.unwrap().get_username().unwrap(), "test2");
let pflines = pf.lines(); let pflines = pf.lines();
let pflines2 = pf2.lines(); let pflines2 = password_file_string.lines();
for (l1, l2) in pflines.zip(pflines2) { for (l1, l2) in pflines.zip(pflines2) {
dbg!(l1, l2); dbg!(l1, l2);
assert!(l1 == l2); assert!(l1 == l2);
} }
assert!(pf2.lines().last().unwrap().starts_with("test2")); assert!(password_file_string
.lines()
.last()
.unwrap()
.starts_with("test2"));
assert!(shadow_file_string
.lines()
.last()
.unwrap()
.starts_with("test2"));
} }
#[test] #[test]
fn test_create_user_binary() { fn test_create_user_binary() {
use testfiles::Fixture; use testfiles::Fixture;
use adduser::api::UserDBWrite;
use adduser::api::UserRead;
use std::fs; use std::fs;
let p = Fixture::copy("passwd"); let p = Fixture::copy("passwd");
let s = Fixture::copy("shadow"); let s = Fixture::copy("shadow");
let g = Fixture::copy("group"); let g = Fixture::copy("group");
dbg!(&p, &s, &g); //dbg!(&p, &s, &g);
let pf = fs::read_to_string(&p.path).unwrap(); let passwd_string = fs::read_to_string(&p.path).unwrap();
let passwd_lines = passwd_string.lines();
let shadow_string = fs::read_to_string(&s.path).unwrap();
let shadow_lines = shadow_string.lines();
let mf = adduser::Files { let out = test_bin::get_test_bin("create_user")
passwd: Some(p.path.clone()), .args(&[
shadow: Some(s.path), "testuser3",
group: Some(g.path), "-p",
}; p.path.to_str().unwrap(),
"-s",
let mut db = adduser::UserDBLocal::load_files(mf).unwrap(); s.path.to_str().unwrap(),
"-g",
let user_res: Result<&adduser::User, adduser::UserLibError> = db.new_user( g.path.to_str().unwrap(),
adduser::api::CreateUserArgs::builder() ])
.username("test2") .output()
// .delete_home(adduser::api::DeleteHome::Delete) .expect("Failed to run the command");
.build() println!(
.unwrap(), "The output after running: {}",
String::from_utf8_lossy(&out.stdout)
); );
let pf2 = fs::read_to_string(&p.path).unwrap(); println!(
assert_eq!(user_res.unwrap().get_username().unwrap(), "test2"); "The error after running: {}",
let pflines = pf.lines(); String::from_utf8_lossy(&out.stderr)
let pflines2 = pf2.lines(); );
for (l1, l2) in pflines.zip(pflines2) { assert_eq!(String::from_utf8_lossy(&out.stdout), "");
dbg!(l1, l2); assert_eq!(String::from_utf8_lossy(&out.stderr), "");
let passwd_string_after = fs::read_to_string(&p.path).unwrap();
let passwd_lines_after = passwd_string_after.lines();
let shadow_string_after = fs::read_to_string(&s.path).unwrap();
let shadow_lines_after = shadow_string_after.lines();
for (l1, l2) in passwd_lines.zip(passwd_lines_after) {
//dbg!(l1, l2);
assert!(l1 == l2); assert!(l1 == l2);
} }
assert!(pf2.lines().last().unwrap().starts_with("test2")); assert_eq!(
passwd_string_after
.lines()
.last()
.unwrap()
.starts_with("testuser3"),
true
);
//dbg!(&shadow_string_after);
for (l1, l2) in shadow_lines.zip(shadow_lines_after) {
//dbg!(l1, l2);
assert!(l1 == l2);
}
assert_eq!(
shadow_string_after
.lines()
.last()
.unwrap()
.starts_with("testuser3"),
true
);
} }