From 3d50e1497dd07929655636cfea0c48ccadd3ab8e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 3 May 2022 10:44:26 +0300 Subject: test: add test for storing, retrieving u64::MAX values in JSON The test passes. We create a FilesystemEntry with a length field containing u64::MAX, store that into a generation, and read it back. This works. The entry is serialised into JSON for storing in SQLite, and this proves we can handle any u64 value in an entry. serde_json deals with it fine, and we don't need to worry about it. Sponsored-by: author --- src/db.rs | 1 - src/fsentry.rs | 51 +++++++++++++++++++++++++++++++++++++++------------ src/generation.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/db.rs b/src/db.rs index 46ba16a..da24e96 100644 --- a/src/db.rs +++ b/src/db.rs @@ -631,7 +631,6 @@ mod test { } assert_eq!(values, expected); } - #[test] fn round_trips_int_max() { let tmp = tempdir().unwrap(); diff --git a/src/fsentry.rs b/src/fsentry.rs index 90afd70..a04a3de 100644 --- a/src/fsentry.rs +++ b/src/fsentry.rs @@ -70,12 +70,19 @@ pub enum FsEntryError { #[allow(clippy::len_without_is_empty)] impl FilesystemEntry { /// Create an `FsEntry` from a file's metadata. - pub fn from_metadata( + pub(crate) fn new( path: &Path, - meta: &Metadata, + kind: FilesystemKind, + uid: u32, + gid: u32, + len: u64, + mode: u32, + mtime: i64, + mtime_ns: i64, + atime: i64, + atime_ns: i64, cache: &mut UsersCache, ) -> Result { - let kind = FilesystemKind::from_file_type(meta.file_type()); let symlink_target = if kind == FilesystemKind::Symlink { debug!("reading symlink target for {:?}", path); let target = @@ -85,8 +92,6 @@ impl FilesystemEntry { None }; - let uid = meta.st_uid(); - let gid = meta.st_gid(); let user: String = if let Some(user) = cache.get_user_by_uid(uid) { user.name().to_string_lossy().to_string() } else { @@ -100,13 +105,13 @@ impl FilesystemEntry { Ok(Self { path: path.to_path_buf().into_os_string().into_vec(), - kind: FilesystemKind::from_file_type(meta.file_type()), - len: meta.len(), - mode: meta.st_mode(), - mtime: meta.st_mtime(), - mtime_ns: meta.st_mtime_nsec(), - atime: meta.st_atime(), - atime_ns: meta.st_atime_nsec(), + kind, + len, + mode, + mtime, + mtime_ns, + atime, + atime_ns, symlink_target, uid, gid, @@ -115,6 +120,28 @@ impl FilesystemEntry { }) } + /// Create an `FsEntry` from a file's metadata. + pub fn from_metadata( + path: &Path, + meta: &Metadata, + cache: &mut UsersCache, + ) -> Result { + let kind = FilesystemKind::from_file_type(meta.file_type()); + Self::new( + path, + kind, + meta.st_uid(), + meta.st_gid(), + meta.len(), + meta.st_mode(), + meta.st_mtime(), + meta.st_mtime_nsec(), + meta.st_atime(), + meta.st_atime_nsec(), + cache, + ) + } + /// Return the kind of file the entry refers to. pub fn kind(&self) -> FilesystemKind { self.kind diff --git a/src/generation.rs b/src/generation.rs index 180efbe..cec3d14 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -296,8 +296,44 @@ impl LocalGeneration { #[cfg(test)] mod test { - use super::{LabelChecksumKind, LocalGeneration, NascentGeneration, SchemaVersion}; - use tempfile::NamedTempFile; + use super::{LabelChecksumKind, LocalGeneration, NascentGeneration, Reason, SchemaVersion}; + use crate::fsentry::FilesystemEntry; + use crate::fsentry::FilesystemKind; + use std::path::Path; + use tempfile::{tempdir, NamedTempFile}; + use users::UsersCache; + + #[test] + fn round_trips_u64_max() { + let tmp = tempdir().unwrap(); + let filename = tmp.path().join("test.db"); + let mut cache = UsersCache::new(); + let schema = SchemaVersion::new(0, 0); + { + let e = FilesystemEntry::new( + Path::new("/"), + FilesystemKind::Directory, + 0, + 0, + u64::MAX, + 0, + 0, + 0, + 0, + 0, + &mut cache, + ) + .unwrap(); + let mut gen = + NascentGeneration::create(&filename, schema, LabelChecksumKind::Sha256).unwrap(); + gen.insert(e, &[], Reason::IsNew, false).unwrap(); + gen.close().unwrap(); + } + + let db = LocalGeneration::open(&filename).unwrap(); + let e = db.get_file(Path::new("/")).unwrap().unwrap(); + assert_eq!(e.len(), u64::MAX); + } #[test] fn empty() { -- cgit v1.2.1