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