From a13c25814b72cddc2470440b2279db72389d03b3 Mon Sep 17 00:00:00 2001 From: huyubiao Date: Tue, 28 Nov 2023 20:55:23 +0800 Subject: [PATCH 1/2] test(hwdb): add UT for hwdb fix: should_reload not effective fix: HwdbUtil::query panic --- libs/hwdb/src/hwdb_util.rs | 100 +++++++++++++++++++++++++++++++++---- libs/hwdb/src/sd_hwdb.rs | 36 +++++++++++-- 2 files changed, 122 insertions(+), 14 deletions(-) diff --git a/libs/hwdb/src/hwdb_util.rs b/libs/hwdb/src/hwdb_util.rs index a5c2eac0..bf0d749c 100644 --- a/libs/hwdb/src/hwdb_util.rs +++ b/libs/hwdb/src/hwdb_util.rs @@ -732,10 +732,16 @@ impl HwdbUtil { } match hwdb_bin_dir { - Some(dir) => bin_dir += &dir, - None => bin_dir += "/etc/devmaster/", + Some(dir) => { + if bin_dir.is_empty() { + bin_dir = dir; + } else { + bin_dir = bin_dir + "/" + &dir; + } + } + None => bin_dir += "/etc/devmaster", } - let hwdb_bin = bin_dir.clone() + "hwdb.bin"; + let hwdb_bin = bin_dir.clone() + "/" + "hwdb.bin"; let mut conf_file_dirs: Vec = Vec::new(); if let Some(p) = path { @@ -878,25 +884,97 @@ fn node_lookup(node: Rc>, c: u8) -> Option bool { let mut found: bool = false; - let duration = Duration::from_secs(self.st.st_mtime as u64); + let duration = Duration::new(self.st.st_mtime as u64, self.st.st_mtime_nsec as u32); let st_time = UNIX_EPOCH + duration; let mut time = st_time; @@ -273,7 +273,7 @@ impl SdHwdb { } /// get value by modalias and key - pub fn sd_hwdb_get(&mut self, modalias: String, key: String) -> Result { + pub fn get(&mut self, modalias: String, key: String) -> Result { if let Err(err) = self.properties_prepare(modalias) { return Err(err); } @@ -350,7 +350,7 @@ impl SdHwdb { ); } - if c != search.as_bytes()[i + p] { + if i + p >= search.len() || c != search.as_bytes()[i + p] { return Ok(()); } p += 1; @@ -776,3 +776,33 @@ impl LineBuf { self.rem(1); } } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::Path; + + #[test] + fn test_get() { + for hwdb_bin in HWDB_BIN_PATHS { + if Path::new(hwdb_bin).exists() { + let mut hwdb = SdHwdb::new().unwrap(); + let s = hwdb + .get( + "dmi:bvnLENOVO".to_string(), + "ID_SYSFS_ATTRIBUTE_MODEL".to_string(), + ) + .unwrap(); + assert_eq!(s, "product_version"); + assert_eq!( + hwdb.get( + "invalid_modalias".to_string(), + "ID_SYSFS_ATTRIBUTE_MODEL".to_string() + ), + Err(Errno::ENOENT) + ); + return; + } + } + } +} -- Gitee From 7f5ffa2095eef604a21e512fceff7aa716359682 Mon Sep 17 00:00:00 2001 From: huyubiao Date: Tue, 28 Nov 2023 20:58:01 +0800 Subject: [PATCH 2/2] fix: failed to execute test_prior_dir_read_one() test_update_tag() and test_post() --- exts/devmaster/src/lib/rules/node.rs | 6 +++++- libs/device/src/device.rs | 7 +++++-- libs/event/src/lib.rs | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/exts/devmaster/src/lib/rules/node.rs b/exts/devmaster/src/lib/rules/node.rs index de600f72..50276787 100644 --- a/exts/devmaster/src/lib/rules/node.rs +++ b/exts/devmaster/src/lib/rules/node.rs @@ -874,7 +874,11 @@ mod test { Ok(()) }) { - assert!(e.is_errno(nix::Error::EACCES) || e.is_errno(nix::Error::EBUSY)); + assert!( + e.is_errno(nix::Error::EACCES) + || e.is_errno(nix::Error::EBUSY) + || e.is_errno(nix::Error::EAGAIN) + ); } } diff --git a/libs/device/src/device.rs b/libs/device/src/device.rs index 945bf91b..0104307c 100644 --- a/libs/device/src/device.rs +++ b/libs/device/src/device.rs @@ -3528,8 +3528,11 @@ mod tests { } if let Err(e) = LoopDev::inner_process("/tmp/test_update_tag", 1024 * 10, inner_test) { - println!("e:{:?}", e); - assert!(e.is_errno(nix::Error::EACCES) || e.is_errno(nix::Error::EBUSY)); + assert!( + e.is_errno(nix::Error::EACCES) + || e.is_errno(nix::Error::EBUSY) + || e.is_errno(nix::Error::EAGAIN) + ); } } diff --git a/libs/event/src/lib.rs b/libs/event/src/lib.rs index c0dd1e46..25e74f86 100644 --- a/libs/event/src/lib.rs +++ b/libs/event/src/lib.rs @@ -572,7 +572,7 @@ mod tests { * Thus the assertion condition should be slightly relaxed. */ let count = *post_s.as_ref().count.borrow(); - assert!(count < 10); + assert!(count >= 1); e.del_source(s).unwrap(); e.del_source(timer_s).unwrap(); -- Gitee