inode-test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test of ext4 inode that verify the seconds part of [a/c/m]
  4. * timestamps in ext4 inode structs are decoded correctly.
  5. */
  6. #include <kunit/test.h>
  7. #include <linux/kernel.h>
  8. #include <linux/time64.h>
  9. #include "ext4.h"
  10. /*
  11. * For constructing the nonnegative timestamp lower bound value.
  12. * binary: 00000000 00000000 00000000 00000000
  13. */
  14. #define LOWER_MSB_0 0L
  15. /*
  16. * For constructing the nonnegative timestamp upper bound value.
  17. * binary: 01111111 11111111 11111111 11111111
  18. *
  19. */
  20. #define UPPER_MSB_0 0x7fffffffL
  21. /*
  22. * For constructing the negative timestamp lower bound value.
  23. * binary: 10000000 00000000 00000000 00000000
  24. */
  25. #define LOWER_MSB_1 (-(UPPER_MSB_0) - 1L) /* avoid overflow */
  26. /*
  27. * For constructing the negative timestamp upper bound value.
  28. * binary: 11111111 11111111 11111111 11111111
  29. */
  30. #define UPPER_MSB_1 (-1L)
  31. /*
  32. * Upper bound for nanoseconds value supported by the encoding.
  33. * binary: 00111111 11111111 11111111 11111111
  34. */
  35. #define MAX_NANOSECONDS ((1L << 30) - 1)
  36. #define CASE_NAME_FORMAT "%s: msb:%x lower_bound:%x extra_bits: %x"
  37. #define LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE\
  38. "1901-12-13 Lower bound of 32bit < 0 timestamp, no extra bits"
  39. #define UPPER_BOUND_NEG_NO_EXTRA_BITS_CASE\
  40. "1969-12-31 Upper bound of 32bit < 0 timestamp, no extra bits"
  41. #define LOWER_BOUND_NONNEG_NO_EXTRA_BITS_CASE\
  42. "1970-01-01 Lower bound of 32bit >=0 timestamp, no extra bits"
  43. #define UPPER_BOUND_NONNEG_NO_EXTRA_BITS_CASE\
  44. "2038-01-19 Upper bound of 32bit >=0 timestamp, no extra bits"
  45. #define LOWER_BOUND_NEG_LO_1_CASE\
  46. "2038-01-19 Lower bound of 32bit <0 timestamp, lo extra sec bit on"
  47. #define UPPER_BOUND_NEG_LO_1_CASE\
  48. "2106-02-07 Upper bound of 32bit <0 timestamp, lo extra sec bit on"
  49. #define LOWER_BOUND_NONNEG_LO_1_CASE\
  50. "2106-02-07 Lower bound of 32bit >=0 timestamp, lo extra sec bit on"
  51. #define UPPER_BOUND_NONNEG_LO_1_CASE\
  52. "2174-02-25 Upper bound of 32bit >=0 timestamp, lo extra sec bit on"
  53. #define LOWER_BOUND_NEG_HI_1_CASE\
  54. "2174-02-25 Lower bound of 32bit <0 timestamp, hi extra sec bit on"
  55. #define UPPER_BOUND_NEG_HI_1_CASE\
  56. "2242-03-16 Upper bound of 32bit <0 timestamp, hi extra sec bit on"
  57. #define LOWER_BOUND_NONNEG_HI_1_CASE\
  58. "2242-03-16 Lower bound of 32bit >=0 timestamp, hi extra sec bit on"
  59. #define UPPER_BOUND_NONNEG_HI_1_CASE\
  60. "2310-04-04 Upper bound of 32bit >=0 timestamp, hi extra sec bit on"
  61. #define UPPER_BOUND_NONNEG_HI_1_NS_1_CASE\
  62. "2310-04-04 Upper bound of 32bit>=0 timestamp, hi extra sec bit 1. 1 ns"
  63. #define LOWER_BOUND_NONNEG_HI_1_NS_MAX_CASE\
  64. "2378-04-22 Lower bound of 32bit>= timestamp. Extra sec bits 1. Max ns"
  65. #define LOWER_BOUND_NONNEG_EXTRA_BITS_1_CASE\
  66. "2378-04-22 Lower bound of 32bit >=0 timestamp. All extra sec bits on"
  67. #define UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE\
  68. "2446-05-10 Upper bound of 32bit >=0 timestamp. All extra sec bits on"
  69. struct timestamp_expectation {
  70. const char *test_case_name;
  71. struct timespec64 expected;
  72. u32 extra_bits;
  73. bool msb_set;
  74. bool lower_bound;
  75. };
  76. static const struct timestamp_expectation test_data[] = {
  77. {
  78. .test_case_name = LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE,
  79. .msb_set = true,
  80. .lower_bound = true,
  81. .extra_bits = 0,
  82. .expected = {.tv_sec = -0x80000000LL, .tv_nsec = 0L},
  83. },
  84. {
  85. .test_case_name = UPPER_BOUND_NEG_NO_EXTRA_BITS_CASE,
  86. .msb_set = true,
  87. .lower_bound = false,
  88. .extra_bits = 0,
  89. .expected = {.tv_sec = -1LL, .tv_nsec = 0L},
  90. },
  91. {
  92. .test_case_name = LOWER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
  93. .msb_set = false,
  94. .lower_bound = true,
  95. .extra_bits = 0,
  96. .expected = {0LL, 0L},
  97. },
  98. {
  99. .test_case_name = UPPER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
  100. .msb_set = false,
  101. .lower_bound = false,
  102. .extra_bits = 0,
  103. .expected = {.tv_sec = 0x7fffffffLL, .tv_nsec = 0L},
  104. },
  105. {
  106. .test_case_name = LOWER_BOUND_NEG_LO_1_CASE,
  107. .msb_set = true,
  108. .lower_bound = true,
  109. .extra_bits = 1,
  110. .expected = {.tv_sec = 0x80000000LL, .tv_nsec = 0L},
  111. },
  112. {
  113. .test_case_name = UPPER_BOUND_NEG_LO_1_CASE,
  114. .msb_set = true,
  115. .lower_bound = false,
  116. .extra_bits = 1,
  117. .expected = {.tv_sec = 0xffffffffLL, .tv_nsec = 0L},
  118. },
  119. {
  120. .test_case_name = LOWER_BOUND_NONNEG_LO_1_CASE,
  121. .msb_set = false,
  122. .lower_bound = true,
  123. .extra_bits = 1,
  124. .expected = {.tv_sec = 0x100000000LL, .tv_nsec = 0L},
  125. },
  126. {
  127. .test_case_name = UPPER_BOUND_NONNEG_LO_1_CASE,
  128. .msb_set = false,
  129. .lower_bound = false,
  130. .extra_bits = 1,
  131. .expected = {.tv_sec = 0x17fffffffLL, .tv_nsec = 0L},
  132. },
  133. {
  134. .test_case_name = LOWER_BOUND_NEG_HI_1_CASE,
  135. .msb_set = true,
  136. .lower_bound = true,
  137. .extra_bits = 2,
  138. .expected = {.tv_sec = 0x180000000LL, .tv_nsec = 0L},
  139. },
  140. {
  141. .test_case_name = UPPER_BOUND_NEG_HI_1_CASE,
  142. .msb_set = true,
  143. .lower_bound = false,
  144. .extra_bits = 2,
  145. .expected = {.tv_sec = 0x1ffffffffLL, .tv_nsec = 0L},
  146. },
  147. {
  148. .test_case_name = LOWER_BOUND_NONNEG_HI_1_CASE,
  149. .msb_set = false,
  150. .lower_bound = true,
  151. .extra_bits = 2,
  152. .expected = {.tv_sec = 0x200000000LL, .tv_nsec = 0L},
  153. },
  154. {
  155. .test_case_name = UPPER_BOUND_NONNEG_HI_1_CASE,
  156. .msb_set = false,
  157. .lower_bound = false,
  158. .extra_bits = 2,
  159. .expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 0L},
  160. },
  161. {
  162. .test_case_name = UPPER_BOUND_NONNEG_HI_1_NS_1_CASE,
  163. .msb_set = false,
  164. .lower_bound = false,
  165. .extra_bits = 6,
  166. .expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 1L},
  167. },
  168. {
  169. .test_case_name = LOWER_BOUND_NONNEG_HI_1_NS_MAX_CASE,
  170. .msb_set = false,
  171. .lower_bound = true,
  172. .extra_bits = 0xFFFFFFFF,
  173. .expected = {.tv_sec = 0x300000000LL,
  174. .tv_nsec = MAX_NANOSECONDS},
  175. },
  176. {
  177. .test_case_name = LOWER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
  178. .msb_set = false,
  179. .lower_bound = true,
  180. .extra_bits = 3,
  181. .expected = {.tv_sec = 0x300000000LL, .tv_nsec = 0L},
  182. },
  183. {
  184. .test_case_name = UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
  185. .msb_set = false,
  186. .lower_bound = false,
  187. .extra_bits = 3,
  188. .expected = {.tv_sec = 0x37fffffffLL, .tv_nsec = 0L},
  189. }
  190. };
  191. static void timestamp_expectation_to_desc(const struct timestamp_expectation *t,
  192. char *desc)
  193. {
  194. strscpy(desc, t->test_case_name, KUNIT_PARAM_DESC_SIZE);
  195. }
  196. KUNIT_ARRAY_PARAM(ext4_inode, test_data, timestamp_expectation_to_desc);
  197. static time64_t get_32bit_time(const struct timestamp_expectation * const test)
  198. {
  199. if (test->msb_set) {
  200. if (test->lower_bound)
  201. return LOWER_MSB_1;
  202. return UPPER_MSB_1;
  203. }
  204. if (test->lower_bound)
  205. return LOWER_MSB_0;
  206. return UPPER_MSB_0;
  207. }
  208. /*
  209. * Test data is derived from the table in the Inode Timestamps section of
  210. * Documentation/filesystems/ext4/inodes.rst.
  211. */
  212. static void inode_test_xtimestamp_decoding(struct kunit *test)
  213. {
  214. struct timespec64 timestamp;
  215. struct timestamp_expectation *test_param =
  216. (struct timestamp_expectation *)(test->param_value);
  217. timestamp = ext4_decode_extra_time(
  218. cpu_to_le32(get_32bit_time(test_param)),
  219. cpu_to_le32(test_param->extra_bits));
  220. KUNIT_EXPECT_EQ_MSG(test,
  221. test_param->expected.tv_sec,
  222. timestamp.tv_sec,
  223. CASE_NAME_FORMAT,
  224. test_param->test_case_name,
  225. test_param->msb_set,
  226. test_param->lower_bound,
  227. test_param->extra_bits);
  228. KUNIT_EXPECT_EQ_MSG(test,
  229. test_param->expected.tv_nsec,
  230. timestamp.tv_nsec,
  231. CASE_NAME_FORMAT,
  232. test_param->test_case_name,
  233. test_param->msb_set,
  234. test_param->lower_bound,
  235. test_param->extra_bits);
  236. }
  237. static struct kunit_case ext4_inode_test_cases[] = {
  238. KUNIT_CASE_PARAM(inode_test_xtimestamp_decoding, ext4_inode_gen_params),
  239. {}
  240. };
  241. static struct kunit_suite ext4_inode_test_suite = {
  242. .name = "ext4_inode_test",
  243. .test_cases = ext4_inode_test_cases,
  244. };
  245. kunit_test_suites(&ext4_inode_test_suite);
  246. MODULE_DESCRIPTION("KUnit test of ext4 inode timestamp decoding");
  247. MODULE_LICENSE("GPL v2");