1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test of ext4 inode that verify the seconds part of [a/c/m]
4 * timestamps in ext4 inode structs are decoded correctly.
7 #include <kunit/test.h>
8 #include <linux/kernel.h>
9 #include <linux/time64.h>
14 * For constructing the nonnegative timestamp lower bound value.
15 * binary: 00000000 00000000 00000000 00000000
17 #define LOWER_MSB_0 0L
19 * For constructing the nonnegative timestamp upper bound value.
20 * binary: 01111111 11111111 11111111 11111111
23 #define UPPER_MSB_0 0x7fffffffL
25 * For constructing the negative timestamp lower bound value.
26 * binary: 10000000 00000000 00000000 00000000
28 #define LOWER_MSB_1 (-(UPPER_MSB_0) - 1L) /* avoid overflow */
30 * For constructing the negative timestamp upper bound value.
31 * binary: 11111111 11111111 11111111 11111111
33 #define UPPER_MSB_1 (-1L)
35 * Upper bound for nanoseconds value supported by the encoding.
36 * binary: 00111111 11111111 11111111 11111111
38 #define MAX_NANOSECONDS ((1L << 30) - 1)
40 #define CASE_NAME_FORMAT "%s: msb:%x lower_bound:%x extra_bits: %x"
42 #define LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE\
43 "1901-12-13 Lower bound of 32bit < 0 timestamp, no extra bits"
44 #define UPPER_BOUND_NEG_NO_EXTRA_BITS_CASE\
45 "1969-12-31 Upper bound of 32bit < 0 timestamp, no extra bits"
46 #define LOWER_BOUND_NONNEG_NO_EXTRA_BITS_CASE\
47 "1970-01-01 Lower bound of 32bit >=0 timestamp, no extra bits"
48 #define UPPER_BOUND_NONNEG_NO_EXTRA_BITS_CASE\
49 "2038-01-19 Upper bound of 32bit >=0 timestamp, no extra bits"
50 #define LOWER_BOUND_NEG_LO_1_CASE\
51 "2038-01-19 Lower bound of 32bit <0 timestamp, lo extra sec bit on"
52 #define UPPER_BOUND_NEG_LO_1_CASE\
53 "2106-02-07 Upper bound of 32bit <0 timestamp, lo extra sec bit on"
54 #define LOWER_BOUND_NONNEG_LO_1_CASE\
55 "2106-02-07 Lower bound of 32bit >=0 timestamp, lo extra sec bit on"
56 #define UPPER_BOUND_NONNEG_LO_1_CASE\
57 "2174-02-25 Upper bound of 32bit >=0 timestamp, lo extra sec bit on"
58 #define LOWER_BOUND_NEG_HI_1_CASE\
59 "2174-02-25 Lower bound of 32bit <0 timestamp, hi extra sec bit on"
60 #define UPPER_BOUND_NEG_HI_1_CASE\
61 "2242-03-16 Upper bound of 32bit <0 timestamp, hi extra sec bit on"
62 #define LOWER_BOUND_NONNEG_HI_1_CASE\
63 "2242-03-16 Lower bound of 32bit >=0 timestamp, hi extra sec bit on"
64 #define UPPER_BOUND_NONNEG_HI_1_CASE\
65 "2310-04-04 Upper bound of 32bit >=0 timestamp, hi extra sec bit on"
66 #define UPPER_BOUND_NONNEG_HI_1_NS_1_CASE\
67 "2310-04-04 Upper bound of 32bit>=0 timestamp, hi extra sec bit 1. 1 ns"
68 #define LOWER_BOUND_NONNEG_HI_1_NS_MAX_CASE\
69 "2378-04-22 Lower bound of 32bit>= timestamp. Extra sec bits 1. Max ns"
70 #define LOWER_BOUND_NONNEG_EXTRA_BITS_1_CASE\
71 "2378-04-22 Lower bound of 32bit >=0 timestamp. All extra sec bits on"
72 #define UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE\
73 "2446-05-10 Upper bound of 32bit >=0 timestamp. All extra sec bits on"
75 struct timestamp_expectation {
76 const char *test_case_name;
77 struct timespec64 expected;
83 static time64_t get_32bit_time(const struct timestamp_expectation * const test)
86 if (test->lower_bound)
92 if (test->lower_bound)
99 * Test data is derived from the table in the Inode Timestamps section of
100 * Documentation/filesystems/ext4/inodes.rst.
102 static void inode_test_xtimestamp_decoding(struct kunit *test)
104 const struct timestamp_expectation test_data[] = {
106 .test_case_name = LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE,
110 .expected = {.tv_sec = -0x80000000LL, .tv_nsec = 0L},
114 .test_case_name = UPPER_BOUND_NEG_NO_EXTRA_BITS_CASE,
116 .lower_bound = false,
118 .expected = {.tv_sec = -1LL, .tv_nsec = 0L},
122 .test_case_name = LOWER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
126 .expected = {0LL, 0L},
130 .test_case_name = UPPER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
132 .lower_bound = false,
134 .expected = {.tv_sec = 0x7fffffffLL, .tv_nsec = 0L},
138 .test_case_name = LOWER_BOUND_NEG_LO_1_CASE,
142 .expected = {.tv_sec = 0x80000000LL, .tv_nsec = 0L},
146 .test_case_name = UPPER_BOUND_NEG_LO_1_CASE,
148 .lower_bound = false,
150 .expected = {.tv_sec = 0xffffffffLL, .tv_nsec = 0L},
154 .test_case_name = LOWER_BOUND_NONNEG_LO_1_CASE,
158 .expected = {.tv_sec = 0x100000000LL, .tv_nsec = 0L},
162 .test_case_name = UPPER_BOUND_NONNEG_LO_1_CASE,
164 .lower_bound = false,
166 .expected = {.tv_sec = 0x17fffffffLL, .tv_nsec = 0L},
170 .test_case_name = LOWER_BOUND_NEG_HI_1_CASE,
174 .expected = {.tv_sec = 0x180000000LL, .tv_nsec = 0L},
178 .test_case_name = UPPER_BOUND_NEG_HI_1_CASE,
180 .lower_bound = false,
182 .expected = {.tv_sec = 0x1ffffffffLL, .tv_nsec = 0L},
186 .test_case_name = LOWER_BOUND_NONNEG_HI_1_CASE,
190 .expected = {.tv_sec = 0x200000000LL, .tv_nsec = 0L},
194 .test_case_name = UPPER_BOUND_NONNEG_HI_1_CASE,
196 .lower_bound = false,
198 .expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 0L},
202 .test_case_name = UPPER_BOUND_NONNEG_HI_1_NS_1_CASE,
204 .lower_bound = false,
206 .expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 1L},
210 .test_case_name = LOWER_BOUND_NONNEG_HI_1_NS_MAX_CASE,
213 .extra_bits = 0xFFFFFFFF,
214 .expected = {.tv_sec = 0x300000000LL,
215 .tv_nsec = MAX_NANOSECONDS},
219 .test_case_name = LOWER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
223 .expected = {.tv_sec = 0x300000000LL, .tv_nsec = 0L},
227 .test_case_name = UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
229 .lower_bound = false,
231 .expected = {.tv_sec = 0x37fffffffLL, .tv_nsec = 0L},
235 struct timespec64 timestamp;
238 for (i = 0; i < ARRAY_SIZE(test_data); ++i) {
239 timestamp.tv_sec = get_32bit_time(&test_data[i]);
240 ext4_decode_extra_time(×tamp,
241 cpu_to_le32(test_data[i].extra_bits));
243 KUNIT_EXPECT_EQ_MSG(test,
244 test_data[i].expected.tv_sec,
247 test_data[i].test_case_name,
248 test_data[i].msb_set,
249 test_data[i].lower_bound,
250 test_data[i].extra_bits);
251 KUNIT_EXPECT_EQ_MSG(test,
252 test_data[i].expected.tv_nsec,
255 test_data[i].test_case_name,
256 test_data[i].msb_set,
257 test_data[i].lower_bound,
258 test_data[i].extra_bits);
262 static struct kunit_case ext4_inode_test_cases[] = {
263 KUNIT_CASE(inode_test_xtimestamp_decoding),
267 static struct kunit_suite ext4_inode_test_suite = {
268 .name = "ext4_inode_test",
269 .test_cases = ext4_inode_test_cases,
272 kunit_test_suites(&ext4_inode_test_suite);
274 MODULE_LICENSE("GPL v2");