fat_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit tests for FAT filesystems.
  4. *
  5. * Copyright (C) 2020 Google LLC.
  6. * Author: David Gow <[email protected]>
  7. */
  8. #include <kunit/test.h>
  9. #include "fat.h"
  10. static void fat_checksum_test(struct kunit *test)
  11. {
  12. /* With no extension. */
  13. KUNIT_EXPECT_EQ(test, fat_checksum("VMLINUX "), (u8)44);
  14. /* With 3-letter extension. */
  15. KUNIT_EXPECT_EQ(test, fat_checksum("README TXT"), (u8)115);
  16. /* With short (1-letter) extension. */
  17. KUNIT_EXPECT_EQ(test, fat_checksum("ABCDEFGHA "), (u8)98);
  18. }
  19. struct fat_timestamp_testcase {
  20. const char *name;
  21. struct timespec64 ts;
  22. __le16 time;
  23. __le16 date;
  24. u8 cs;
  25. int time_offset;
  26. };
  27. static struct fat_timestamp_testcase time_test_cases[] = {
  28. {
  29. .name = "Earliest possible UTC (1980-01-01 00:00:00)",
  30. .ts = {.tv_sec = 315532800LL, .tv_nsec = 0L},
  31. .time = cpu_to_le16(0),
  32. .date = cpu_to_le16(33),
  33. .cs = 0,
  34. .time_offset = 0,
  35. },
  36. {
  37. .name = "Latest possible UTC (2107-12-31 23:59:58)",
  38. .ts = {.tv_sec = 4354819198LL, .tv_nsec = 0L},
  39. .time = cpu_to_le16(49021),
  40. .date = cpu_to_le16(65439),
  41. .cs = 0,
  42. .time_offset = 0,
  43. },
  44. {
  45. .name = "Earliest possible (UTC-11) (== 1979-12-31 13:00:00 UTC)",
  46. .ts = {.tv_sec = 315493200LL, .tv_nsec = 0L},
  47. .time = cpu_to_le16(0),
  48. .date = cpu_to_le16(33),
  49. .cs = 0,
  50. .time_offset = 11 * 60,
  51. },
  52. {
  53. .name = "Latest possible (UTC+11) (== 2108-01-01 10:59:58 UTC)",
  54. .ts = {.tv_sec = 4354858798LL, .tv_nsec = 0L},
  55. .time = cpu_to_le16(49021),
  56. .date = cpu_to_le16(65439),
  57. .cs = 0,
  58. .time_offset = -11 * 60,
  59. },
  60. {
  61. .name = "Leap Day / Year (1996-02-29 00:00:00)",
  62. .ts = {.tv_sec = 825552000LL, .tv_nsec = 0L},
  63. .time = cpu_to_le16(0),
  64. .date = cpu_to_le16(8285),
  65. .cs = 0,
  66. .time_offset = 0,
  67. },
  68. {
  69. .name = "Year 2000 is leap year (2000-02-29 00:00:00)",
  70. .ts = {.tv_sec = 951782400LL, .tv_nsec = 0L},
  71. .time = cpu_to_le16(0),
  72. .date = cpu_to_le16(10333),
  73. .cs = 0,
  74. .time_offset = 0,
  75. },
  76. {
  77. .name = "Year 2100 not leap year (2100-03-01 00:00:00)",
  78. .ts = {.tv_sec = 4107542400LL, .tv_nsec = 0L},
  79. .time = cpu_to_le16(0),
  80. .date = cpu_to_le16(61537),
  81. .cs = 0,
  82. .time_offset = 0,
  83. },
  84. {
  85. .name = "Leap year + timezone UTC+1 (== 2004-02-29 00:30:00 UTC)",
  86. .ts = {.tv_sec = 1078014600LL, .tv_nsec = 0L},
  87. .time = cpu_to_le16(48064),
  88. .date = cpu_to_le16(12380),
  89. .cs = 0,
  90. .time_offset = -60,
  91. },
  92. {
  93. .name = "Leap year + timezone UTC-1 (== 2004-02-29 23:30:00 UTC)",
  94. .ts = {.tv_sec = 1078097400LL, .tv_nsec = 0L},
  95. .time = cpu_to_le16(960),
  96. .date = cpu_to_le16(12385),
  97. .cs = 0,
  98. .time_offset = 60,
  99. },
  100. {
  101. .name = "VFAT odd-second resolution (1999-12-31 23:59:59)",
  102. .ts = {.tv_sec = 946684799LL, .tv_nsec = 0L},
  103. .time = cpu_to_le16(49021),
  104. .date = cpu_to_le16(10143),
  105. .cs = 100,
  106. .time_offset = 0,
  107. },
  108. {
  109. .name = "VFAT 10ms resolution (1980-01-01 00:00:00:0010)",
  110. .ts = {.tv_sec = 315532800LL, .tv_nsec = 10000000L},
  111. .time = cpu_to_le16(0),
  112. .date = cpu_to_le16(33),
  113. .cs = 1,
  114. .time_offset = 0,
  115. },
  116. };
  117. static void time_testcase_desc(struct fat_timestamp_testcase *t,
  118. char *desc)
  119. {
  120. strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
  121. }
  122. KUNIT_ARRAY_PARAM(fat_time, time_test_cases, time_testcase_desc);
  123. static void fat_time_fat2unix_test(struct kunit *test)
  124. {
  125. static struct msdos_sb_info fake_sb;
  126. struct timespec64 ts;
  127. struct fat_timestamp_testcase *testcase =
  128. (struct fat_timestamp_testcase *)test->param_value;
  129. fake_sb.options.tz_set = 1;
  130. fake_sb.options.time_offset = testcase->time_offset;
  131. fat_time_fat2unix(&fake_sb, &ts,
  132. testcase->time,
  133. testcase->date,
  134. testcase->cs);
  135. KUNIT_EXPECT_EQ_MSG(test,
  136. testcase->ts.tv_sec,
  137. ts.tv_sec,
  138. "Timestamp mismatch (seconds)\n");
  139. KUNIT_EXPECT_EQ_MSG(test,
  140. testcase->ts.tv_nsec,
  141. ts.tv_nsec,
  142. "Timestamp mismatch (nanoseconds)\n");
  143. }
  144. static void fat_time_unix2fat_test(struct kunit *test)
  145. {
  146. static struct msdos_sb_info fake_sb;
  147. __le16 date, time;
  148. u8 cs;
  149. struct fat_timestamp_testcase *testcase =
  150. (struct fat_timestamp_testcase *)test->param_value;
  151. fake_sb.options.tz_set = 1;
  152. fake_sb.options.time_offset = testcase->time_offset;
  153. fat_time_unix2fat(&fake_sb, &testcase->ts,
  154. &time, &date, &cs);
  155. KUNIT_EXPECT_EQ_MSG(test,
  156. le16_to_cpu(testcase->time),
  157. le16_to_cpu(time),
  158. "Time mismatch\n");
  159. KUNIT_EXPECT_EQ_MSG(test,
  160. le16_to_cpu(testcase->date),
  161. le16_to_cpu(date),
  162. "Date mismatch\n");
  163. KUNIT_EXPECT_EQ_MSG(test,
  164. testcase->cs,
  165. cs,
  166. "Centisecond mismatch\n");
  167. }
  168. static struct kunit_case fat_test_cases[] = {
  169. KUNIT_CASE(fat_checksum_test),
  170. KUNIT_CASE_PARAM(fat_time_fat2unix_test, fat_time_gen_params),
  171. KUNIT_CASE_PARAM(fat_time_unix2fat_test, fat_time_gen_params),
  172. {},
  173. };
  174. static struct kunit_suite fat_test_suite = {
  175. .name = "fat_test",
  176. .test_cases = fat_test_cases,
  177. };
  178. kunit_test_suites(&fat_test_suite);
  179. MODULE_LICENSE("GPL v2");