diff --git a/Cargo.lock b/Cargo.lock index 8d12bfc..31d28d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,19 +1,9 @@ # This file is automatically @generated by Cargo. # 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]] name = "adduser" version = "0.1.0" dependencies = [ - "assert_cli", "chrono", "clap", "derive_builder", @@ -23,14 +13,9 @@ dependencies = [ "regex", "simplelog", "tempfile", + "test_bin", ] -[[package]] -name = "adler" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" - [[package]] name = "aho-corasick" version = "0.7.13" @@ -40,20 +25,6 @@ dependencies = [ "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]] name = "atty" version = "0.2.14" @@ -71,20 +42,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "bitflags" version = "1.2.1" @@ -97,12 +54,6 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "chrono" version = "0.4.19" @@ -148,17 +99,6 @@ dependencies = [ "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]] name = "darling" version = "0.10.2" @@ -219,12 +159,6 @@ dependencies = [ "syn", ] -[[package]] -name = "difference" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" - [[package]] name = "env_logger" version = "0.8.1" @@ -238,34 +172,6 @@ dependencies = [ "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]] name = "fnv" version = "1.0.7" @@ -278,17 +184,11 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libc", "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]] name = "hashbrown" version = "0.9.1" @@ -335,12 +235,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "itoa" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" - [[package]] name = "lazy_static" version = "1.4.0" @@ -359,7 +253,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", ] [[package]] @@ -368,16 +262,6 @@ version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "num-integer" version = "0.1.43" @@ -397,12 +281,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "object" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" - [[package]] name = "os_str_bytes" version = "2.4.0" @@ -531,35 +409,6 @@ dependencies = [ "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]] name = "simplelog" version = "0.8.0" @@ -594,25 +443,13 @@ dependencies = [ "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]] name = "tempfile" version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libc", "rand", "redox_syscall", @@ -629,6 +466,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "test_bin" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1937bab04b0bd0f2c19628d3408fb6dd78faf5aa60f4bdb03212507a9b7314ba" + [[package]] name = "textwrap" version = "0.12.1" diff --git a/Cargo.toml b/Cargo.toml index 057e55d..0750c6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,11 @@ chrono = "0.4" env_logger = "0.8" derive_builder = "0.9" clap = "3.0.0-beta" -assert_cli = "0.6" [dev-dependencies] tempfile = "3.1" +test_bin= "0.3" + # optimize for size at cost of compilation speed. [profile.release] diff --git a/src/user/shadow_fields.rs b/src/user/shadow_fields.rs index 115e39a..14629c3 100644 --- a/src/user/shadow_fields.rs +++ b/src/user/shadow_fields.rs @@ -26,6 +26,11 @@ impl Shadow { pub fn get_username(&self) -> &str { &self.username.username } + + pub fn set_username(&mut self, username: crate::Username) { + self.username = username; + } + #[must_use] pub fn get_password(&self) -> &str { &self.password.password diff --git a/src/userlib/mod.rs b/src/userlib/mod.rs index e504254..6e25227 100644 --- a/src/userlib/mod.rs +++ b/src/userlib/mod.rs @@ -231,10 +231,16 @@ impl UserDBWrite for UserDBLocal { Err("Failed to create the user. A user with the same Name already exists".into()) } else { 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!"); - dbg!(&locked_p); + //dbg!(&locked_p); 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 .users .insert(args.username.to_owned(), new_user) diff --git a/tests/create_user_test.rs b/tests/create_user_test.rs index cb214e4..d26d349 100644 --- a/tests/create_user_test.rs +++ b/tests/create_user_test.rs @@ -1,4 +1,5 @@ extern crate adduser; +extern crate test_bin; mod testfiles; #[test] @@ -30,54 +31,93 @@ fn test_create_user_function() { .build() .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"); let pflines = pf.lines(); - let pflines2 = pf2.lines(); + let pflines2 = password_file_string.lines(); for (l1, l2) in pflines.zip(pflines2) { dbg!(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] fn test_create_user_binary() { use testfiles::Fixture; - use adduser::api::UserDBWrite; - use adduser::api::UserRead; use std::fs; let p = Fixture::copy("passwd"); let s = Fixture::copy("shadow"); 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 { - passwd: Some(p.path.clone()), - shadow: Some(s.path), - group: Some(g.path), - }; - - let mut db = adduser::UserDBLocal::load_files(mf).unwrap(); - - let user_res: Result<&adduser::User, adduser::UserLibError> = db.new_user( - adduser::api::CreateUserArgs::builder() - .username("test2") - // .delete_home(adduser::api::DeleteHome::Delete) - .build() - .unwrap(), + let out = test_bin::get_test_bin("create_user") + .args(&[ + "testuser3", + "-p", + p.path.to_str().unwrap(), + "-s", + s.path.to_str().unwrap(), + "-g", + g.path.to_str().unwrap(), + ]) + .output() + .expect("Failed to run the command"); + println!( + "The output after running: {}", + String::from_utf8_lossy(&out.stdout) ); - let pf2 = fs::read_to_string(&p.path).unwrap(); - assert_eq!(user_res.unwrap().get_username().unwrap(), "test2"); - let pflines = pf.lines(); - let pflines2 = pf2.lines(); - for (l1, l2) in pflines.zip(pflines2) { - dbg!(l1, l2); + println!( + "The error after running: {}", + String::from_utf8_lossy(&out.stderr) + ); + assert_eq!(String::from_utf8_lossy(&out.stdout), ""); + 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!(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 + ); }