linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fat: Add KUnit tests for checksums and timestamps
@ 2020-10-17  6:41 David Gow
  2020-10-17  8:19 ` OGAWA Hirofumi
  2020-10-18  0:19 ` kernel test robot
  0 siblings, 2 replies; 3+ messages in thread
From: David Gow @ 2020-10-17  6:41 UTC (permalink / raw)
  To: OGAWA Hirofumi, Brendan Higgins, shuah
  Cc: linux-kselftest, kunit-dev, linux-kernel, David Gow

Add some basic sanity-check tests for the fat_checksum() function and
the fat_time_unix2fat() and fat_time_fat2unix() functions. These unit
tests verify these functions return correct output for a number of test
inputs.

These tests were inspored by -- and serve a similar purpose to -- the
timestamp parsing KUnit tests in ext4[1].

[1]:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/ext4/inode-test.c

Signed-off-by: David Gow <davidgow@google.com>
---
 fs/fat/Kconfig    |  13 +++
 fs/fat/Makefile   |   2 +
 fs/fat/fat_test.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 212 insertions(+)
 create mode 100644 fs/fat/fat_test.c

diff --git a/fs/fat/Kconfig b/fs/fat/Kconfig
index 66532a71e8fd..fdef03b79c69 100644
--- a/fs/fat/Kconfig
+++ b/fs/fat/Kconfig
@@ -115,3 +115,16 @@ config FAT_DEFAULT_UTF8
 	  Say Y if you use UTF-8 encoding for file names, N otherwise.
 
 	  See <file:Documentation/filesystems/vfat.rst> for more information.
+
+config FAT_KUNIT_TEST
+	tristate "Unit Tests for FAT filesystems" if !KUNIT_ALL_TESTS
+	select FAT_FS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the FAT KUnit tests
+
+	  For more information on KUnit and unit tests in general, please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit
+
+	  If unsure, say N
diff --git a/fs/fat/Makefile b/fs/fat/Makefile
index 70645ce2f7fc..2b034112690d 100644
--- a/fs/fat/Makefile
+++ b/fs/fat/Makefile
@@ -10,3 +10,5 @@ obj-$(CONFIG_MSDOS_FS) += msdos.o
 fat-y := cache.o dir.o fatent.o file.o inode.o misc.o nfs.o
 vfat-y := namei_vfat.o
 msdos-y := namei_msdos.o
+
+obj-$(CONFIG_FAT_KUNIT_TEST) += fat_test.o
diff --git a/fs/fat/fat_test.c b/fs/fat/fat_test.c
new file mode 100644
index 000000000000..c1b4348b9b3b
--- /dev/null
+++ b/fs/fat/fat_test.c
@@ -0,0 +1,197 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for FAT filesystems.
+ *
+ * Copyright (C) 2020 Google LLC.
+ * Author: David Gow <davidgow@google.com>
+ */
+
+#include <kunit/test.h>
+
+#include "fat.h"
+
+static void fat_checksum_test(struct kunit *test)
+{
+	/* With no extension. */
+	KUNIT_EXPECT_EQ(test, fat_checksum("VMLINUX    "), 44);
+	/* With 3-letter extension. */
+	KUNIT_EXPECT_EQ(test, fat_checksum("README  TXT"), 115);
+	/* With short (1-letter) extension. */
+	KUNIT_EXPECT_EQ(test, fat_checksum("ABCDEFGHA  "), 98);
+}
+
+
+struct fat_timestamp_testcase {
+	const char *name;
+	struct timespec64 ts;
+	__le16 time;
+	__le16 date;
+	u8 cs;
+	int time_offset;
+};
+
+const static struct fat_timestamp_testcase time_test_cases[] = {
+	{
+		.name = "Earliest possible UTC (1980-01-01 00:00:00)",
+		.ts = {.tv_sec = 315532800LL, .tv_nsec = 0L},
+		.time = 0,
+		.date = 33,
+		.cs = 0,
+		.time_offset = 0,
+	},
+	{
+		.name = "Latest possible UTC (2107-12-31 23:59:58)",
+		.ts = {.tv_sec = 4354819198LL, .tv_nsec = 0L},
+		.time = 49021,
+		.date = 65439,
+		.cs = 0,
+		.time_offset = 0,
+	},
+	{
+		.name = "Earliest possible (UTC-11) (== 1979-12-31 13:00:00 UTC)",
+		.ts = {.tv_sec = 315493200LL, .tv_nsec = 0L},
+		.time = 0,
+		.date = 33,
+		.cs = 0,
+		.time_offset = 11 * 60,
+	},
+	{
+		.name = "Latest possible (UTC+11) (== 2108-01-01 10:59:58 UTC)",
+		.ts = {.tv_sec = 4354858798LL, .tv_nsec = 0L},
+		.time = 49021,
+		.date = 65439,
+		.cs = 0,
+		.time_offset = -11 * 60,
+	},
+	{
+		.name = "Leap Day / Year (1996-02-29 00:00:00)",
+		.ts = {.tv_sec = 825552000LL, .tv_nsec = 0L},
+		.time = 0,
+		.date = 8285,
+		.cs = 0,
+		.time_offset = 0,
+	},
+	{
+		.name = "Year 2000 is leap year (2000-02-29 00:00:00)",
+		.ts = {.tv_sec = 951782400LL, .tv_nsec = 0L},
+		.time = 0,
+		.date = 10333,
+		.cs = 0,
+		.time_offset = 0,
+	},
+	{
+		.name = "Year 2100 not leap year (2100-03-01 00:00:00)",
+		.ts = {.tv_sec = 4107542400LL, .tv_nsec = 0L},
+		.time = 0,
+		.date = 61537,
+		.cs = 0,
+		.time_offset = 0,
+	},
+	{
+		.name = "Leap year + timezone UTC+1 (== 2004-02-29 00:30:00 UTC)",
+		.ts = {.tv_sec = 1078014600LL, .tv_nsec = 0L},
+		.time = 48064,
+		.date = 12380,
+		.cs = 0,
+		.time_offset = -60,
+	},
+	{
+		.name = "Leap year + timezone UTC-1 (== 2004-02-29 23:30:00 UTC)",
+		.ts = {.tv_sec = 1078097400LL, .tv_nsec = 0L},
+		.time = 960,
+		.date = 12385,
+		.cs = 0,
+		.time_offset = 60,
+	},
+	{
+		.name = "VFAT odd-second resolution (1999-12-31 23:59:59)",
+		.ts = {.tv_sec = 946684799LL, .tv_nsec = 0L},
+		.time = 49021,
+		.date = 10143,
+		.cs = 100,
+		.time_offset = 0,
+	},
+	{
+		.name = "VFAT 10ms resolution (1980-01-01 00:00:00:0010)",
+		.ts = {.tv_sec = 315532800LL, .tv_nsec = 10000000L},
+		.time = 0,
+		.date = 33,
+		.cs = 1,
+		.time_offset = 0,
+	},
+};
+
+static void fat_time_fat2unix_test(struct kunit *test)
+{
+	struct msdos_sb_info fake_sb;
+	int i;
+	struct timespec64 ts;
+
+	for (i = 0; i < ARRAY_SIZE(time_test_cases); ++i) {
+		fake_sb.options.tz_set = 1;
+		fake_sb.options.time_offset = time_test_cases[i].time_offset;
+
+		fat_time_fat2unix(&fake_sb, &ts,
+				  time_test_cases[i].time,
+				  time_test_cases[i].date,
+				  time_test_cases[i].cs);
+		KUNIT_EXPECT_EQ_MSG(test,
+				    time_test_cases[i].ts.tv_sec,
+				    ts.tv_sec,
+				    "Timestamp mismatch (seconds) in case \"%s\"\n",
+				    time_test_cases[i].name);
+		KUNIT_EXPECT_EQ_MSG(test,
+				    time_test_cases[i].ts.tv_nsec,
+				    ts.tv_nsec,
+				    "Timestamp mismatch (nanoseconds) in case \"%s\"\n",
+				    time_test_cases[i].name);
+	}
+}
+
+static void fat_time_unix2fat_test(struct kunit *test)
+{
+	struct msdos_sb_info fake_sb;
+	int i;
+	__le16 date, time;
+	u8 cs;
+
+	for (i = 0; i < ARRAY_SIZE(time_test_cases); ++i) {
+		fake_sb.options.tz_set = 1;
+		fake_sb.options.time_offset = time_test_cases[i].time_offset;
+
+		fat_time_unix2fat(&fake_sb, &time_test_cases[i].ts,
+				  &time, &date, &cs);
+		KUNIT_EXPECT_EQ_MSG(test,
+				    time_test_cases[i].time,
+				    time,
+				    "Time mismatch in case \"%s\"\n",
+				    time_test_cases[i].name);
+		KUNIT_EXPECT_EQ_MSG(test,
+				    time_test_cases[i].date,
+				    date,
+				    "Date mismatch in case \"%s\"\n",
+				    time_test_cases[i].name);
+		KUNIT_EXPECT_EQ_MSG(test,
+				    time_test_cases[i].cs,
+				    cs,
+				    "Centisecond mismatch in case \"%s\"\n",
+				    time_test_cases[i].name);
+	}
+}
+
+static struct kunit_case fat_test_cases[] = {
+	KUNIT_CASE(fat_checksum_test),
+	KUNIT_CASE(fat_time_fat2unix_test),
+	KUNIT_CASE(fat_time_unix2fat_test),
+	{},
+};
+
+static struct kunit_suite fat_test_suite = {
+	.name = "fat_test",
+	.test_cases = fat_test_cases,
+};
+
+kunit_test_suites(&fat_test_suite);
+
+MODULE_LICENSE("GPL v2");
+
-- 
2.29.0.rc1.297.gfa9743e501-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] fat: Add KUnit tests for checksums and timestamps
  2020-10-17  6:41 [PATCH] fat: Add KUnit tests for checksums and timestamps David Gow
@ 2020-10-17  8:19 ` OGAWA Hirofumi
  2020-10-18  0:19 ` kernel test robot
  1 sibling, 0 replies; 3+ messages in thread
From: OGAWA Hirofumi @ 2020-10-17  8:19 UTC (permalink / raw)
  To: David Gow
  Cc: Brendan Higgins, shuah, linux-kselftest, kunit-dev, linux-kernel

David Gow <davidgow@google.com> writes:

> Add some basic sanity-check tests for the fat_checksum() function and
> the fat_time_unix2fat() and fat_time_fat2unix() functions. These unit
> tests verify these functions return correct output for a number of test
> inputs.
>
> These tests were inspored by -- and serve a similar purpose to -- the
> timestamp parsing KUnit tests in ext4[1].
>
> [1]:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/ext4/inode-test.c
>
> Signed-off-by: David Gow <davidgow@google.com>

Looks good, thanks.

Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

> ---
>  fs/fat/Kconfig    |  13 +++
>  fs/fat/Makefile   |   2 +
>  fs/fat/fat_test.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 212 insertions(+)
>  create mode 100644 fs/fat/fat_test.c
>
> diff --git a/fs/fat/Kconfig b/fs/fat/Kconfig
> index 66532a71e8fd..fdef03b79c69 100644
> --- a/fs/fat/Kconfig
> +++ b/fs/fat/Kconfig
> @@ -115,3 +115,16 @@ config FAT_DEFAULT_UTF8
>  	  Say Y if you use UTF-8 encoding for file names, N otherwise.
>  
>  	  See <file:Documentation/filesystems/vfat.rst> for more information.
> +
> +config FAT_KUNIT_TEST
> +	tristate "Unit Tests for FAT filesystems" if !KUNIT_ALL_TESTS
> +	select FAT_FS
> +	depends on KUNIT
> +	default KUNIT_ALL_TESTS
> +	help
> +	  This builds the FAT KUnit tests
> +
> +	  For more information on KUnit and unit tests in general, please refer
> +	  to the KUnit documentation in Documentation/dev-tools/kunit
> +
> +	  If unsure, say N
> diff --git a/fs/fat/Makefile b/fs/fat/Makefile
> index 70645ce2f7fc..2b034112690d 100644
> --- a/fs/fat/Makefile
> +++ b/fs/fat/Makefile
> @@ -10,3 +10,5 @@ obj-$(CONFIG_MSDOS_FS) += msdos.o
>  fat-y := cache.o dir.o fatent.o file.o inode.o misc.o nfs.o
>  vfat-y := namei_vfat.o
>  msdos-y := namei_msdos.o
> +
> +obj-$(CONFIG_FAT_KUNIT_TEST) += fat_test.o
> diff --git a/fs/fat/fat_test.c b/fs/fat/fat_test.c
> new file mode 100644
> index 000000000000..c1b4348b9b3b
> --- /dev/null
> +++ b/fs/fat/fat_test.c
> @@ -0,0 +1,197 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * KUnit tests for FAT filesystems.
> + *
> + * Copyright (C) 2020 Google LLC.
> + * Author: David Gow <davidgow@google.com>
> + */
> +
> +#include <kunit/test.h>
> +
> +#include "fat.h"
> +
> +static void fat_checksum_test(struct kunit *test)
> +{
> +	/* With no extension. */
> +	KUNIT_EXPECT_EQ(test, fat_checksum("VMLINUX    "), 44);
> +	/* With 3-letter extension. */
> +	KUNIT_EXPECT_EQ(test, fat_checksum("README  TXT"), 115);
> +	/* With short (1-letter) extension. */
> +	KUNIT_EXPECT_EQ(test, fat_checksum("ABCDEFGHA  "), 98);
> +}
> +
> +
> +struct fat_timestamp_testcase {
> +	const char *name;
> +	struct timespec64 ts;
> +	__le16 time;
> +	__le16 date;
> +	u8 cs;
> +	int time_offset;
> +};
> +
> +const static struct fat_timestamp_testcase time_test_cases[] = {
> +	{
> +		.name = "Earliest possible UTC (1980-01-01 00:00:00)",
> +		.ts = {.tv_sec = 315532800LL, .tv_nsec = 0L},
> +		.time = 0,
> +		.date = 33,
> +		.cs = 0,
> +		.time_offset = 0,
> +	},
> +	{
> +		.name = "Latest possible UTC (2107-12-31 23:59:58)",
> +		.ts = {.tv_sec = 4354819198LL, .tv_nsec = 0L},
> +		.time = 49021,
> +		.date = 65439,
> +		.cs = 0,
> +		.time_offset = 0,
> +	},
> +	{
> +		.name = "Earliest possible (UTC-11) (== 1979-12-31 13:00:00 UTC)",
> +		.ts = {.tv_sec = 315493200LL, .tv_nsec = 0L},
> +		.time = 0,
> +		.date = 33,
> +		.cs = 0,
> +		.time_offset = 11 * 60,
> +	},
> +	{
> +		.name = "Latest possible (UTC+11) (== 2108-01-01 10:59:58 UTC)",
> +		.ts = {.tv_sec = 4354858798LL, .tv_nsec = 0L},
> +		.time = 49021,
> +		.date = 65439,
> +		.cs = 0,
> +		.time_offset = -11 * 60,
> +	},
> +	{
> +		.name = "Leap Day / Year (1996-02-29 00:00:00)",
> +		.ts = {.tv_sec = 825552000LL, .tv_nsec = 0L},
> +		.time = 0,
> +		.date = 8285,
> +		.cs = 0,
> +		.time_offset = 0,
> +	},
> +	{
> +		.name = "Year 2000 is leap year (2000-02-29 00:00:00)",
> +		.ts = {.tv_sec = 951782400LL, .tv_nsec = 0L},
> +		.time = 0,
> +		.date = 10333,
> +		.cs = 0,
> +		.time_offset = 0,
> +	},
> +	{
> +		.name = "Year 2100 not leap year (2100-03-01 00:00:00)",
> +		.ts = {.tv_sec = 4107542400LL, .tv_nsec = 0L},
> +		.time = 0,
> +		.date = 61537,
> +		.cs = 0,
> +		.time_offset = 0,
> +	},
> +	{
> +		.name = "Leap year + timezone UTC+1 (== 2004-02-29 00:30:00 UTC)",
> +		.ts = {.tv_sec = 1078014600LL, .tv_nsec = 0L},
> +		.time = 48064,
> +		.date = 12380,
> +		.cs = 0,
> +		.time_offset = -60,
> +	},
> +	{
> +		.name = "Leap year + timezone UTC-1 (== 2004-02-29 23:30:00 UTC)",
> +		.ts = {.tv_sec = 1078097400LL, .tv_nsec = 0L},
> +		.time = 960,
> +		.date = 12385,
> +		.cs = 0,
> +		.time_offset = 60,
> +	},
> +	{
> +		.name = "VFAT odd-second resolution (1999-12-31 23:59:59)",
> +		.ts = {.tv_sec = 946684799LL, .tv_nsec = 0L},
> +		.time = 49021,
> +		.date = 10143,
> +		.cs = 100,
> +		.time_offset = 0,
> +	},
> +	{
> +		.name = "VFAT 10ms resolution (1980-01-01 00:00:00:0010)",
> +		.ts = {.tv_sec = 315532800LL, .tv_nsec = 10000000L},
> +		.time = 0,
> +		.date = 33,
> +		.cs = 1,
> +		.time_offset = 0,
> +	},
> +};
> +
> +static void fat_time_fat2unix_test(struct kunit *test)
> +{
> +	struct msdos_sb_info fake_sb;
> +	int i;
> +	struct timespec64 ts;
> +
> +	for (i = 0; i < ARRAY_SIZE(time_test_cases); ++i) {
> +		fake_sb.options.tz_set = 1;
> +		fake_sb.options.time_offset = time_test_cases[i].time_offset;
> +
> +		fat_time_fat2unix(&fake_sb, &ts,
> +				  time_test_cases[i].time,
> +				  time_test_cases[i].date,
> +				  time_test_cases[i].cs);
> +		KUNIT_EXPECT_EQ_MSG(test,
> +				    time_test_cases[i].ts.tv_sec,
> +				    ts.tv_sec,
> +				    "Timestamp mismatch (seconds) in case \"%s\"\n",
> +				    time_test_cases[i].name);
> +		KUNIT_EXPECT_EQ_MSG(test,
> +				    time_test_cases[i].ts.tv_nsec,
> +				    ts.tv_nsec,
> +				    "Timestamp mismatch (nanoseconds) in case \"%s\"\n",
> +				    time_test_cases[i].name);
> +	}
> +}
> +
> +static void fat_time_unix2fat_test(struct kunit *test)
> +{
> +	struct msdos_sb_info fake_sb;
> +	int i;
> +	__le16 date, time;
> +	u8 cs;
> +
> +	for (i = 0; i < ARRAY_SIZE(time_test_cases); ++i) {
> +		fake_sb.options.tz_set = 1;
> +		fake_sb.options.time_offset = time_test_cases[i].time_offset;
> +
> +		fat_time_unix2fat(&fake_sb, &time_test_cases[i].ts,
> +				  &time, &date, &cs);
> +		KUNIT_EXPECT_EQ_MSG(test,
> +				    time_test_cases[i].time,
> +				    time,
> +				    "Time mismatch in case \"%s\"\n",
> +				    time_test_cases[i].name);
> +		KUNIT_EXPECT_EQ_MSG(test,
> +				    time_test_cases[i].date,
> +				    date,
> +				    "Date mismatch in case \"%s\"\n",
> +				    time_test_cases[i].name);
> +		KUNIT_EXPECT_EQ_MSG(test,
> +				    time_test_cases[i].cs,
> +				    cs,
> +				    "Centisecond mismatch in case \"%s\"\n",
> +				    time_test_cases[i].name);
> +	}
> +}
> +
> +static struct kunit_case fat_test_cases[] = {
> +	KUNIT_CASE(fat_checksum_test),
> +	KUNIT_CASE(fat_time_fat2unix_test),
> +	KUNIT_CASE(fat_time_unix2fat_test),
> +	{},
> +};
> +
> +static struct kunit_suite fat_test_suite = {
> +	.name = "fat_test",
> +	.test_cases = fat_test_cases,
> +};
> +
> +kunit_test_suites(&fat_test_suite);
> +
> +MODULE_LICENSE("GPL v2");
> +

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] fat: Add KUnit tests for checksums and timestamps
  2020-10-17  6:41 [PATCH] fat: Add KUnit tests for checksums and timestamps David Gow
  2020-10-17  8:19 ` OGAWA Hirofumi
@ 2020-10-18  0:19 ` kernel test robot
  1 sibling, 0 replies; 3+ messages in thread
From: kernel test robot @ 2020-10-18  0:19 UTC (permalink / raw)
  To: David Gow, OGAWA Hirofumi, Brendan Higgins, shuah
  Cc: kbuild-all, linux-kselftest, kunit-dev, linux-kernel, David Gow

[-- Attachment #1: Type: text/plain, Size: 1664 bytes --]

Hi David,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.9 next-20201016]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/David-Gow/fat-Add-KUnit-tests-for-checksums-and-timestamps/20201017-144157
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 071a0578b0ce0b0e543d1e38ee6926b9cc21c198
config: m68k-allmodconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/318413ec0ade19d4570ae13fc42afb289246d50f
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review David-Gow/fat-Add-KUnit-tests-for-checksums-and-timestamps/20201017-144157
        git checkout 318413ec0ade19d4570ae13fc42afb289246d50f
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=m68k 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "fat_time_fat2unix" [fs/fat/fat_test.ko] undefined!

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 58570 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-10-18  0:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-17  6:41 [PATCH] fat: Add KUnit tests for checksums and timestamps David Gow
2020-10-17  8:19 ` OGAWA Hirofumi
2020-10-18  0:19 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).