|
| 1 | +import ctypes |
| 2 | +import struct |
| 3 | +import unittest |
| 4 | + |
| 5 | +from qiling.os.posix.syscall.stat import ( |
| 6 | + LinuxARMEBStat, |
| 7 | + LinuxARMStat, |
| 8 | + LinuxARMStat64, |
| 9 | + __common_pack_stat_struct as common_pack_stat_struct, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class ARMStatTimestampUnitTest(unittest.TestCase): |
| 14 | + def _check_struct(self, stat_type, byteorder): |
| 15 | + timestamps = { |
| 16 | + "st_atime_ns": (1_791_143_806_588_454_438, 588_454_438), |
| 17 | + "st_mtime_ns": (1_791_143_806_592_454_430, 592_454_430), |
| 18 | + "st_ctime_ns": (1_791_143_806_592_454_430, 592_454_430), |
| 19 | + } |
| 20 | + seconds = { |
| 21 | + "st_atime": 1_791_143_806, |
| 22 | + "st_mtime": 1_791_143_806, |
| 23 | + "st_ctime": 1_791_143_806, |
| 24 | + } |
| 25 | + info = {field: 0 for field, _ in stat_type._fields_} |
| 26 | + info.update(seconds) |
| 27 | + info["st_size"] = 29 |
| 28 | + for field, (epoch_ns, _) in timestamps.items(): |
| 29 | + info[field] = epoch_ns |
| 30 | + |
| 31 | + stat = stat_type() |
| 32 | + packed = common_pack_stat_struct(stat, info) |
| 33 | + |
| 34 | + self.assertEqual(len(packed), ctypes.sizeof(stat_type)) |
| 35 | + self.assertEqual(stat.st_size, 29) |
| 36 | + for field, expected in seconds.items(): |
| 37 | + self.assertEqual(getattr(stat, field), expected) |
| 38 | + for field, (_, expected) in timestamps.items(): |
| 39 | + with self.subTest(field=field): |
| 40 | + self.assertEqual(getattr(stat, field), expected) |
| 41 | + offset = getattr(stat_type, field).offset |
| 42 | + actual = struct.unpack_from(byteorder + "I", packed, offset)[0] |
| 43 | + self.assertEqual(actual, expected) |
| 44 | + |
| 45 | + def test_little_endian_arm_stat_timestamps_use_subsecond_units(self): |
| 46 | + self._check_struct(LinuxARMStat, "<") |
| 47 | + |
| 48 | + def test_big_endian_arm_stat_timestamps_use_subsecond_units(self): |
| 49 | + self._check_struct(LinuxARMEBStat, ">") |
| 50 | + |
| 51 | + def test_little_endian_arm_stat64_timestamps_use_subsecond_units(self): |
| 52 | + self._check_struct(LinuxARMStat64, "<") |
0 commit comments