From eb8cd516b24ac9a65f51e41dd2b96689ff16a4d6 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Sat, 28 Nov 2020 03:25:57 +0700 Subject: [PATCH] Add file tests --- Cargo.lock | 130 ++++++++++++++++++ Cargo.toml | 1 + .../database/administration/registered.json | 13 ++ fixtures/database/game/general.json | 12 ++ fixtures/database/game/perks.json | 12 ++ src/database/io.rs | 25 +++- src/database/mod.rs | 5 + src/database/tests.rs | 84 +++++++++++ 8 files changed, 276 insertions(+), 6 deletions(-) create mode 100644 fixtures/database/administration/registered.json create mode 100644 fixtures/database/game/general.json create mode 100644 fixtures/database/game/perks.json diff --git a/Cargo.lock b/Cargo.lock index ec3ebc1..d521002 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,9 +12,15 @@ dependencies = [ "custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", + "serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cfg-if" version = "0.1.10" @@ -32,6 +38,14 @@ dependencies = [ "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "custom_error" version = "1.8.0" @@ -42,11 +56,24 @@ name = "itoa" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lock_api" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "log" version = "0.4.11" @@ -72,11 +99,59 @@ dependencies = [ "autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "parking_lot_core" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.80 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ryu" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "serde" version = "1.0.117" @@ -92,6 +167,26 @@ dependencies = [ "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serial_test" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serial_test_derive" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "simplelog" version = "0.8.0" @@ -102,6 +197,21 @@ dependencies = [ "termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "smallvec" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syn" +version = "1.0.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termcolor" version = "1.1.2" @@ -120,6 +230,11 @@ dependencies = [ "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" @@ -154,20 +269,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum chrono 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51ac5e99a7fea3ee8a03fa4721a47e2efd3fbb38358fc61192a54d4c6f866c12" "checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +"checksum lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" "checksum log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" "checksum num-integer 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" "checksum num-traits 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +"checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" +"checksum parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" +"checksum proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +"checksum redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)" = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" "checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +"checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" "checksum serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" "checksum serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)" = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" +"checksum serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fef5f7c7434b2f2c598adc6f9494648a1e41274a75c0ba4056f680ae0c117fd6" +"checksum serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d08338d8024b227c62bd68a12c7c9883f5c66780abaef15c550dc56f46ee6515" "checksum simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2b2736f58087298a448859961d3f4a0850b832e72619d75adc69da7993c2cd3c" +"checksum smallvec 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7acad6f34eb9e8a259d3283d1e8c1d34d7415943d4895f65cc73813c7396fc85" +"checksum syn 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)" = "6c1e438504729046a5cfae47f97c30d6d083c7d91d94603efdae3477fc070d4c" "checksum termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" "checksum time 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" "checksum wasi 0.10.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" "checksum winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index f9c8363..6dfe7a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ simplelog = "0.8" log = "0.4" serde_json = "1.0" custom_error = "1.8.0" +serial_test = "0.4" diff --git a/fixtures/database/administration/registered.json b/fixtures/database/administration/registered.json new file mode 100644 index 0000000..aad23a4 --- /dev/null +++ b/fixtures/database/administration/registered.json @@ -0,0 +1,13 @@ +{ + "76561198025127722": { + "allowed_ips": ["127.0.0.1", "192.168.0.100"], + "groups": ["admin"], + "ip_lock": true, + "password_hash": "fce798e0804dfb217f929bdba26745024f37f6b6ba7406f3775176e20dd5089d" + }, + "76561198044316328": { + "groups": ["admin"], + "ip_lock": false, + "password_hash": "fce798e0804dfb217f929bdba26745024f37f6b6ba7406f3775176e20dd5089d" + } +} diff --git a/fixtures/database/game/general.json b/fixtures/database/game/general.json new file mode 100644 index 0000000..24cc980 --- /dev/null +++ b/fixtures/database/game/general.json @@ -0,0 +1,12 @@ +{ + "76561198025127722": { + "walked": 1073, + "dosh_thrown": 483482, + "achievements": ["kf:LabCleaner", "kf:ChickenFarmer", "scrn:playedscrn"] + }, + "76561198044316328": { + "walked": 1693, + "dosh_thrown": 527624, + "achievements": ["kf:PubCrawl", "kf:FascistDietitian", "kf:GimliThatAxe!", "scrn:playedscrn"] + } +} diff --git a/fixtures/database/game/perks.json b/fixtures/database/game/perks.json new file mode 100644 index 0000000..a35c51d --- /dev/null +++ b/fixtures/database/game/perks.json @@ -0,0 +1,12 @@ +{ + "76561198025127722": { + "headshots": 582, + "assault_rifle_damage": 9067, + "stalker_kills": 143 + }, + "76561198044316328": { + "explosive_damage": 19674, + "shotgun_damage": 3835, + "welded_amount": 1 + } +} diff --git a/src/database/io.rs b/src/database/io.rs index 2b761f0..62decb1 100644 --- a/src/database/io.rs +++ b/src/database/io.rs @@ -1,5 +1,5 @@ use super::*; -use log::{info, error, warn}; +use log::{error, info, warn}; use std::collections::HashMap; use std::error::Error; use std::fs; @@ -9,7 +9,7 @@ use std::path::Path; extern crate custom_error; use custom_error::custom_error; -const JSON_EXTENSION: &str = "JSON"; +const JSON_EXTENSION: &str = "json"; custom_error! { pub IOError NotDirectory{path: String} = "Path to the database should point at a directory: {path}", @@ -17,7 +17,10 @@ custom_error! { pub IOError pub fn read(db_path: &path::Path) -> Result, Box> { if !db_path.is_dir() { - error!("Loading database from a non-directory {} was attempted.", db_path.display()); + error!( + "Loading database from a non-directory {} was attempted.", + db_path.display() + ); return Err(Box::new(IOError::NotDirectory { path: db_path.display().to_string(), })); @@ -44,7 +47,10 @@ pub fn read(db_path: &path::Path) -> Result, Box> { pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box> { if db_path.exists() && !db_path.is_dir() { - error!("Cannot write database into a non-directory {}", db_path.display()); + error!( + "Cannot write database into a non-directory {}", + db_path.display() + ); return Err(Box::new(IOError::NotDirectory { path: db_path.display().to_string(), })); @@ -56,7 +62,7 @@ pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box> fs::create_dir(group_path.clone())?; } for file in db.file_names_in(group).iter() { - let file_path = group_path.join(file); + let file_path = group_path.join(format!("{}.{}", file, JSON_EXTENSION)); match db.file(group, file) { Some(file) => fs::write(file_path, file.to_string())?, _ => (), @@ -67,7 +73,14 @@ pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box> } pub fn clear_dir(db_path: &path::Path) -> Result<(), Box> { - info!("Clearing directory {} from database files.", db_path.display()); + info!( + "Clearing directory {} from database files.", + db_path.display() + ); + if !db_path.exists() { + info!("Directory not found, nothing to do."); + return Ok(()); + } for entry in fs::read_dir(db_path)? { let dir_path = match entry { Ok(r) => r, diff --git a/src/database/mod.rs b/src/database/mod.rs index 7e90cd9..08e1b2d 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -61,6 +61,11 @@ impl Database { Ok(()) } + pub fn save(&self) -> Result<(), Box> { + self.write_copy(&self.storage_path)?; + Ok(()) + } + pub fn erase(self) -> Result<(), Box> { io::clear_dir(&self.storage_path)?; Ok(()) diff --git a/src/database/tests.rs b/src/database/tests.rs index 9bb2fc3..3dccc60 100644 --- a/src/database/tests.rs +++ b/src/database/tests.rs @@ -1,11 +1,38 @@ use super::*; use serde_json::json; +use std::fs; use std::path; +use serial_test::serial; + const TEST_DB_PATH: &str = "./fixtures/database"; +const TEST_DB_COPY_PATH: &str = "./fixtures/copy"; +const TEST_DB_MOVED_COPY_PATH: &str = "./fixtures/copy_moved"; const NO_DB_MESSAGE: &str = "Can not find/load test database"; +struct TestCleanup; + +impl Drop for TestCleanup { + fn drop(&mut self) { + clear_test_db(); + } +} + +fn prepare_db_copy() -> TestCleanup { + clear_test_db(); + let original_db = Database::new(path::Path::new(TEST_DB_PATH)).expect(NO_DB_MESSAGE); + original_db + .write_copy(path::Path::new(TEST_DB_COPY_PATH)) + .expect("Should be able to create a new copy of the fixture database."); + TestCleanup +} + +fn clear_test_db() { + let _ = fs::remove_dir_all(TEST_DB_COPY_PATH); + let _ = fs::remove_dir_all(TEST_DB_MOVED_COPY_PATH); +} + #[test] fn db_path() { let mut db = Database::new(path::Path::new(TEST_DB_PATH)).expect(NO_DB_MESSAGE); @@ -293,3 +320,60 @@ fn db_remove_value() { assert!(file.contains("/76561198025127722/allowed_ips")); assert!(!file.contains("/76561198025127722/allowed_ips/0")); } + +#[test] +#[serial] +fn db_save() { + let _cleanup = prepare_db_copy(); + // Change something up and save + let mut db = Database::new(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); + db.remove_group("administration") + .expect(r#"Should be able to remove "administration" group"#); + db.save() + .expect("Should be able to save copy of the database."); + // Reload and check changes + let db = Database::new(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); + assert_eq!(db.group_names().len(), 1); + assert_eq!(db.group_names().get(0), Some(&"game".to_owned())); + assert_eq!(db.file_names_in("game").len(), 2); + assert!(db.contains_file("game", "general")); + assert!(db.contains_file("game", "perks")); +} + +#[test] +#[serial] +fn db_change_path() { + let _cleanup = prepare_db_copy(); + // Change something up and move + let mut db = Database::new(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); + db.remove_group("administration") + .expect(r#"Should be able to remove "administration" group"#); + db.file_mut("game", "perks") + .unwrap() + .insert("", json!({"var":7})) + .expect("Should be able to insert into root."); + db.change_path(path::Path::new(TEST_DB_MOVED_COPY_PATH)) + .expect("Should be able to change database's path."); + assert!(!path::Path::new(TEST_DB_COPY_PATH).exists()); + assert!(path::Path::new(TEST_DB_MOVED_COPY_PATH).exists()); + // Reload and check the changes + let db = Database::new(path::Path::new(TEST_DB_MOVED_COPY_PATH)).expect(NO_DB_MESSAGE); + assert_eq!(db.group_names().len(), 1); + assert_eq!(db.group_names().get(0), Some(&"game".to_owned())); + assert_eq!(db.file_names_in("game").len(), 2); + assert!(db.contains_file("game", "general")); + assert!(db.contains_file("game", "perks")); + assert_eq!( + db.file("game", "perks").unwrap().root().to_string(), + r#"{"var":7}"#.to_owned() + ); +} + +#[test] +#[serial] +fn db_erase() { + let _cleanup = prepare_db_copy(); + let db = Database::new(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); + db.erase().expect("Should be able to erase data."); + assert!(!path::Path::new(TEST_DB_COPY_PATH).exists()); +}