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 const struct timestamp_expectation test_data[] = {
85 .test_case_name = LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE,
89 .expected = {.tv_sec = -0x80000000LL, .tv_nsec = 0L},
93 .test_case_name = UPPER_BOUND_NEG_NO_EXTRA_BITS_CASE,
97 .expected = {.tv_sec = -1LL, .tv_nsec = 0L},
101 .test_case_name = LOWER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
105 .expected = {0LL, 0L},
109 .test_case_name = UPPER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
111 .lower_bound = false,
113 .expected = {.tv_sec = 0x7fffffffLL, .tv_nsec = 0L},
117 .test_case_name = LOWER_BOUND_NEG_LO_1_CASE,
121 .expected = {.tv_sec = 0x80000000LL, .tv_nsec = 0L},
125 .test_case_name = UPPER_BOUND_NEG_LO_1_CASE,
127 .lower_bound = false,
129 .expected = {.tv_sec = 0xffffffffLL, .tv_nsec = 0L},
133 .test_case_name = LOWER_BOUND_NONNEG_LO_1_CASE,
137 .expected = {.tv_sec = 0x100000000LL, .tv_nsec = 0L},
141 .test_case_name = UPPER_BOUND_NONNEG_LO_1_CASE,
143 .lower_bound = false,
145 .expected = {.tv_sec = 0x17fffffffLL, .tv_nsec = 0L},
149 .test_case_name = LOWER_BOUND_NEG_HI_1_CASE,
153 .expected = {.tv_sec = 0x180000000LL, .tv_nsec = 0L},
157 .test_case_name = UPPER_BOUND_NEG_HI_1_CASE,
159 .lower_bound = false,
161 .expected = {.tv_sec = 0x1ffffffffLL, .tv_nsec = 0L},
165 .test_case_name = LOWER_BOUND_NONNEG_HI_1_CASE,
169 .expected = {.tv_sec = 0x200000000LL, .tv_nsec = 0L},
173 .test_case_name = UPPER_BOUND_NONNEG_HI_1_CASE,
175 .lower_bound = false,
177 .expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 0L},
181 .test_case_name = UPPER_BOUND_NONNEG_HI_1_NS_1_CASE,
183 .lower_bound = false,
185 .expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 1L},
189 .test_case_name = LOWER_BOUND_NONNEG_HI_1_NS_MAX_CASE,
192 .extra_bits = 0xFFFFFFFF,
193 .expected = {.tv_sec = 0x300000000LL,
194 .tv_nsec = MAX_NANOSECONDS},
198 .test_case_name = LOWER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
202 .expected = {.tv_sec = 0x300000000LL, .tv_nsec = 0L},
206 .test_case_name = UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
208 .lower_bound = false,
210 .expected = {.tv_sec = 0x37fffffffLL, .tv_nsec = 0L},
214 static void timestamp_expectation_to_desc(const struct timestamp_expectation *t,
217 strscpy(desc, t->test_case_name, KUNIT_PARAM_DESC_SIZE);
220 KUNIT_ARRAY_PARAM(ext4_inode, test_data, timestamp_expectation_to_desc);
222 static time64_t get_32bit_time(const struct timestamp_expectation * const test)
225 if (test->lower_bound)
231 if (test->lower_bound)
238 * Test data is derived from the table in the Inode Timestamps section of
239 * Documentation/filesystems/ext4/inodes.rst.
241 static void inode_test_xtimestamp_decoding(struct kunit *test)
243 struct timespec64 timestamp;
245 struct timestamp_expectation *test_param =
246 (struct timestamp_expectation *)(test->param_value);
248 timestamp = ext4_decode_extra_time(
249 cpu_to_le32(get_32bit_time(test_param)),
250 cpu_to_le32(test_param->extra_bits));
252 KUNIT_EXPECT_EQ_MSG(test,
253 test_param->expected.tv_sec,
256 test_param->test_case_name,
258 test_param->lower_bound,
259 test_param->extra_bits);
260 KUNIT_EXPECT_EQ_MSG(test,
261 test_param->expected.tv_nsec,
264 test_param->test_case_name,
266 test_param->lower_bound,
267 test_param->extra_bits);
270 static struct kunit_case ext4_inode_test_cases[] = {
271 KUNIT_CASE_PARAM(inode_test_xtimestamp_decoding, ext4_inode_gen_params),
275 static struct kunit_suite ext4_inode_test_suite = {
276 .name = "ext4_inode_test",
277 .test_cases = ext4_inode_test_cases,
280 kunit_test_suites(&ext4_inode_test_suite);
282 MODULE_LICENSE("GPL v2");