1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit tests for FAT filesystems.
5 * Copyright (C) 2020 Google LLC.
6 * Author: David Gow <davidgow@google.com>
9 #include <kunit/test.h>
13 static void fat_checksum_test(struct kunit *test)
15 /* With no extension. */
16 KUNIT_EXPECT_EQ(test, fat_checksum("VMLINUX "), (u8)44);
17 /* With 3-letter extension. */
18 KUNIT_EXPECT_EQ(test, fat_checksum("README TXT"), (u8)115);
19 /* With short (1-letter) extension. */
20 KUNIT_EXPECT_EQ(test, fat_checksum("ABCDEFGHA "), (u8)98);
23 struct fat_timestamp_testcase {
32 static struct fat_timestamp_testcase time_test_cases[] = {
34 .name = "Earliest possible UTC (1980-01-01 00:00:00)",
35 .ts = {.tv_sec = 315532800LL, .tv_nsec = 0L},
36 .time = cpu_to_le16(0),
37 .date = cpu_to_le16(33),
42 .name = "Latest possible UTC (2107-12-31 23:59:58)",
43 .ts = {.tv_sec = 4354819198LL, .tv_nsec = 0L},
44 .time = cpu_to_le16(49021),
45 .date = cpu_to_le16(65439),
50 .name = "Earliest possible (UTC-11) (== 1979-12-31 13:00:00 UTC)",
51 .ts = {.tv_sec = 315493200LL, .tv_nsec = 0L},
52 .time = cpu_to_le16(0),
53 .date = cpu_to_le16(33),
55 .time_offset = 11 * 60,
58 .name = "Latest possible (UTC+11) (== 2108-01-01 10:59:58 UTC)",
59 .ts = {.tv_sec = 4354858798LL, .tv_nsec = 0L},
60 .time = cpu_to_le16(49021),
61 .date = cpu_to_le16(65439),
63 .time_offset = -11 * 60,
66 .name = "Leap Day / Year (1996-02-29 00:00:00)",
67 .ts = {.tv_sec = 825552000LL, .tv_nsec = 0L},
68 .time = cpu_to_le16(0),
69 .date = cpu_to_le16(8285),
74 .name = "Year 2000 is leap year (2000-02-29 00:00:00)",
75 .ts = {.tv_sec = 951782400LL, .tv_nsec = 0L},
76 .time = cpu_to_le16(0),
77 .date = cpu_to_le16(10333),
82 .name = "Year 2100 not leap year (2100-03-01 00:00:00)",
83 .ts = {.tv_sec = 4107542400LL, .tv_nsec = 0L},
84 .time = cpu_to_le16(0),
85 .date = cpu_to_le16(61537),
90 .name = "Leap year + timezone UTC+1 (== 2004-02-29 00:30:00 UTC)",
91 .ts = {.tv_sec = 1078014600LL, .tv_nsec = 0L},
92 .time = cpu_to_le16(48064),
93 .date = cpu_to_le16(12380),
98 .name = "Leap year + timezone UTC-1 (== 2004-02-29 23:30:00 UTC)",
99 .ts = {.tv_sec = 1078097400LL, .tv_nsec = 0L},
100 .time = cpu_to_le16(960),
101 .date = cpu_to_le16(12385),
106 .name = "VFAT odd-second resolution (1999-12-31 23:59:59)",
107 .ts = {.tv_sec = 946684799LL, .tv_nsec = 0L},
108 .time = cpu_to_le16(49021),
109 .date = cpu_to_le16(10143),
114 .name = "VFAT 10ms resolution (1980-01-01 00:00:00:0010)",
115 .ts = {.tv_sec = 315532800LL, .tv_nsec = 10000000L},
116 .time = cpu_to_le16(0),
117 .date = cpu_to_le16(33),
123 static void time_testcase_desc(struct fat_timestamp_testcase *t,
126 strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
129 KUNIT_ARRAY_PARAM(fat_time, time_test_cases, time_testcase_desc);
131 static void fat_time_fat2unix_test(struct kunit *test)
133 static struct msdos_sb_info fake_sb;
134 struct timespec64 ts;
135 struct fat_timestamp_testcase *testcase =
136 (struct fat_timestamp_testcase *)test->param_value;
138 fake_sb.options.tz_set = 1;
139 fake_sb.options.time_offset = testcase->time_offset;
141 fat_time_fat2unix(&fake_sb, &ts,
145 KUNIT_EXPECT_EQ_MSG(test,
148 "Timestamp mismatch (seconds)\n");
149 KUNIT_EXPECT_EQ_MSG(test,
150 testcase->ts.tv_nsec,
152 "Timestamp mismatch (nanoseconds)\n");
155 static void fat_time_unix2fat_test(struct kunit *test)
157 static struct msdos_sb_info fake_sb;
160 struct fat_timestamp_testcase *testcase =
161 (struct fat_timestamp_testcase *)test->param_value;
163 fake_sb.options.tz_set = 1;
164 fake_sb.options.time_offset = testcase->time_offset;
166 fat_time_unix2fat(&fake_sb, &testcase->ts,
168 KUNIT_EXPECT_EQ_MSG(test,
169 le16_to_cpu(testcase->time),
172 KUNIT_EXPECT_EQ_MSG(test,
173 le16_to_cpu(testcase->date),
176 KUNIT_EXPECT_EQ_MSG(test,
179 "Centisecond mismatch\n");
182 static struct kunit_case fat_test_cases[] = {
183 KUNIT_CASE(fat_checksum_test),
184 KUNIT_CASE_PARAM(fat_time_fat2unix_test, fat_time_gen_params),
185 KUNIT_CASE_PARAM(fat_time_unix2fat_test, fat_time_gen_params),
189 static struct kunit_suite fat_test_suite = {
191 .test_cases = fat_test_cases,
194 kunit_test_suites(&fat_test_suite);
196 MODULE_LICENSE("GPL v2");