From d74c3b8ee11d08b3b108cb0dffaaee9e65fb818e Mon Sep 17 00:00:00 2001 From: Roel de Jong <12800443+twiggler@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:32:08 +0200 Subject: [PATCH] Extend stat unit test --- dissect/target/filesystems/xfs.py | 9 ++---- tests/filesystems/test_xfs.py | 47 +++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/dissect/target/filesystems/xfs.py b/dissect/target/filesystems/xfs.py index 90fe3c68c..7c00180d1 100644 --- a/dissect/target/filesystems/xfs.py +++ b/dissect/target/filesystems/xfs.py @@ -32,10 +32,6 @@ def _detect(fh: BinaryIO) -> bool: def get(self, path: str) -> FilesystemEntry: return XfsFilesystemEntry(self, path, self._get_node(path)) - @property - def block_size(self) -> int: - return self.xfs.block_size - def _get_node(self, path: str, node: Optional[xfs.INode] = None) -> xfs.INode: try: return self.xfs.get(path, node) @@ -134,13 +130,14 @@ def lstat(self) -> fsutil.stat_result: st_info.st_mtime_ns = self.entry.mtime_ns st_info.st_ctime_ns = self.entry.ctime_ns - st_info.st_blksize = self.fs.block_size + st_info.st_blksize = self.fs.xfs.block_size # Convert number of filesystem blocks to basic blocks # Reference: https://github.com/torvalds/linux/blob/e32cde8d2bd7d251a8f9b434143977ddf13dcec6/fs/xfs/xfs_iops.c#L602 # noqa: E501 - st_info.st_blocks = self.entry.number_of_blocks * (self.fs.block_size // 512) + st_info.st_blocks = self.entry.nblocks * (self.fs.xfs.block_size // 512) # XFS has a birth time, since inode version 3 (version 5 of filesystem) st_info.st_birthtime = self.entry.crtime.timestamp() + st_info.st_birthtime_ns = self.entry.crtime_ns return st_info diff --git a/tests/filesystems/test_xfs.py b/tests/filesystems/test_xfs.py index b4d2d9d7f..a17871b44 100644 --- a/tests/filesystems/test_xfs.py +++ b/tests/filesystems/test_xfs.py @@ -3,10 +3,11 @@ from unittest.mock import Mock, patch import pytest -from dissect.xfs.xfs import INode from dissect.target.filesystems.xfs import XfsFilesystem, XfsFilesystemEntry +NANOSECONDS_IN_SECOND = 1_000_000_000 + @pytest.fixture def xfs_fs() -> Iterator[XfsFilesystem]: @@ -19,21 +20,49 @@ def xfs_fs() -> Iterator[XfsFilesystem]: @pytest.fixture def xfs_fs_entry(xfs_fs: XfsFilesystem) -> Iterator[XfsFilesystemEntry]: - mock_datetime = datetime(2023, 10, 1, 12, 0, 0) + atime = datetime(2024, 10, 1, 12, 0, 0) + mtime = datetime(2024, 10, 2, 12, 0, 0) + ctime = datetime(2024, 10, 3, 12, 0, 0) + crtime = datetime(2024, 10, 4, 12, 0, 0) + + dinode = Mock(di_mode=33272, di_nlink=1, di_uid=1000, di_gid=999) inode = Mock( - spec=INode, - number_of_blocks=10, + nblocks=10, + inode=dinode, inum=4, - atime=mock_datetime, - mtime=mock_datetime, - ctime=mock_datetime, - crtime=mock_datetime, + size=4594, + atime=atime, + atime_ns=atime.timestamp() * NANOSECONDS_IN_SECOND, + mtime=mtime, + mtime_ns=mtime.timestamp() * NANOSECONDS_IN_SECOND, + ctime=ctime, + ctime_ns=ctime.timestamp() * NANOSECONDS_IN_SECOND, + crtime=crtime, + crtime_ns=crtime.timestamp() * NANOSECONDS_IN_SECOND, ) entry = XfsFilesystemEntry(xfs_fs, "/some_file", inode) yield entry -def test_es_filesystems_xfs_stat_blocks(xfs_fs: XfsFilesystem, xfs_fs_entry: XfsFilesystemEntry): +def test_es_filesystems_xfs_stat(xfs_fs: XfsFilesystem, xfs_fs_entry: XfsFilesystemEntry) -> None: stat = xfs_fs_entry.stat() + entry = xfs_fs_entry.entry + assert stat.st_mode == entry.inode.di_mode + assert stat.st_ino == entry.inum + assert stat.st_dev == id(xfs_fs) + assert stat.st_nlink == entry.inode.di_nlink + assert stat.st_uid == entry.inode.di_uid + assert stat.st_gid == entry.inode.di_gid + assert stat.st_size == entry.size + assert stat.st_atime == entry.atime.timestamp() + assert stat.st_atime_ns == entry.atime_ns + assert stat.st_mtime == entry.mtime.timestamp() + assert stat.st_mtime_ns == entry.mtime_ns + assert stat.st_ctime == entry.ctime.timestamp() + assert stat.st_ctime_ns == entry.ctime_ns + assert stat.st_birthtime == entry.crtime.timestamp() + assert stat.st_birthtime_ns == entry.crtime_ns + + assert stat.st_blksize == 4096 assert stat.st_blocks == 10 * 4096 // 512