From: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
To: barebox@lists.infradead.org
Cc: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
Subject: [PATCH v4 19/21] tftp: add selftest
Date: Tue, 30 Aug 2022 09:38:14 +0200 [thread overview]
Message-ID: <20220830073816.2694734-20-enrico.scholz@sigma-chemnitz.de> (raw)
In-Reply-To: <20220830073816.2694734-1-enrico.scholz@sigma-chemnitz.de>
Unittest for window cache functions.
Signed-off-by: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
---
fs/tftp-selftest.h | 56 ++++++++++++++++++++++++
fs/tftp.c | 106 ++++++++++++++++++++++++++++++++++++++++++++-
test/self/Kconfig | 7 +++
3 files changed, 168 insertions(+), 1 deletion(-)
create mode 100644 fs/tftp-selftest.h
diff --git a/fs/tftp-selftest.h b/fs/tftp-selftest.h
new file mode 100644
index 000000000000..2406ed329ecc
--- /dev/null
+++ b/fs/tftp-selftest.h
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0+
+// SPDX-FileCopyrightText: 2022 Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
+
+#ifndef H_BAREBOX_FS_TFTP_SELFTEST_H
+#define H_BAREBOX_FS_TFTP_SELFTEST_H
+
+#include <bselftest.h>
+
+#define _expect_fmt(_v) _Generic(_v, \
+ void const *: "%p", \
+ void *: "%p", \
+ bool: "%d", \
+ long int: "%lld", \
+ long unsigned int: "%llu", \
+ unsigned short: "%h", \
+ signed int: "%d", \
+ unsigned int: "%u")
+
+#define _expect_op(_a, _b, _op) \
+ ({ \
+ __typeof__(_a) __a = (_a); \
+ __typeof__(_b) __b = (_b); \
+ \
+ total_tests++; \
+ \
+ if (!(__a _op __b)) { \
+ char fmt[sizeof "%s:%d: failed: %XXX %XXX\n" # _op]; \
+ strcpy(fmt, "%s:%d: failed: "); \
+ strcat(fmt, _expect_fmt(__a)); \
+ strcat(fmt, " " # _op " "); \
+ strcat(fmt, _expect_fmt(__b)); \
+ strcat(fmt, "\n"); \
+ \
+ failed_tests++; \
+ printf(fmt, __func__, __LINE__, __a, __b); \
+ } \
+ })
+
+#define _as_void(_p) ({ \
+ void const *__res = (_p); \
+ __res; \
+ })
+
+#define expect_eq(_a, _b) _expect_op(_a, _b, ==);
+#define expect_ne(_a, _b) _expect_op(_a, _b, !=);
+#define expect_it(_a) _expect_op(_a, true, ==);
+
+#define expect_err(_a) _expect_op(_a, 0, <);
+#define expect_ok(_a) _expect_op(_a, 0, ==);
+
+/* _Generic() does not match 'void *' for typed pointers; cast them to raw
+ 'void *' first */
+#define expect_NULL(_a) _expect_op(_as_void(_a), NULL, ==);
+#define expect_PTR(_a) _expect_op(_as_void(_a), NULL, !=);
+
+#endif /* H_BAREBOX_FS_TFTP_SELFTEST_H */
diff --git a/fs/tftp.c b/fs/tftp.c
index b010a6be6dbb..a9cc0ff3b118 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -36,6 +36,8 @@
#include <parseopt.h>
#include <linux/sizes.h>
+#include "tftp-selftest.h"
+
#define TFTP_PORT 69 /* Well known TFTP port number */
/* Seconds to wait before remote server is allowed to resend a lost packet */
@@ -81,7 +83,7 @@
#define TFTP_ERR_RESEND 1
-#ifdef DEBUG
+#if defined(DEBUG) || IS_ENABLED(CONFIG_SELFTEST_TFTP)
# define debug_assert(_cond) BUG_ON(!(_cond))
#else
# define debug_assert(_cond) do { \
@@ -1208,3 +1210,105 @@ static int tftp_init(void)
return register_fs_driver(&tftp_driver);
}
coredevice_initcall(tftp_init);
+
+
+BSELFTEST_GLOBALS();
+
+static int __maybe_unused tftp_window_cache_selftest(void)
+{
+ struct tftp_cache *cache = malloc(sizeof *cache);
+
+ if (!cache)
+ return -ENOMEM;
+
+ (void)skipped_tests;
+
+ expect_it( is_block_before(0, 1));
+ expect_it(!is_block_before(1, 0));
+ expect_it( is_block_before(65535, 0));
+ expect_it(!is_block_before(0, 65535));
+
+ expect_eq(get_block_delta(0, 1), 1);
+ expect_eq(get_block_delta(65535, 0), 1);
+ expect_eq(get_block_delta(65535, 1), 2);
+
+ expect_it(!in_window(0, 1, 3));
+ expect_it( in_window(1, 1, 3));
+ expect_it( in_window(2, 1, 3));
+ expect_it( in_window(3, 1, 3));
+ expect_it(!in_window(4, 1, 3));
+
+ expect_it(!in_window(65534, 65535, 1));
+ expect_it( in_window(65535, 65535, 1));
+ expect_it( in_window( 0, 65535, 1));
+ expect_it( in_window( 1, 65535, 1));
+ expect_it(!in_window( 2, 65535, 1));
+
+
+ tftp_window_cache_init(cache, 512, 5);
+
+ if (tftp_window_cache_size(cache) < 4)
+ goto out;
+
+ expect_eq(tftp_window_cache_size(cache), 4);
+
+ /* sequence 1 */
+ expect_ok (tftp_window_cache_insert(cache, 20, "20", 2));
+ expect_ok (tftp_window_cache_insert(cache, 22, "22", 2));
+ expect_ok (tftp_window_cache_insert(cache, 21, "21", 2));
+ expect_ok (tftp_window_cache_insert(cache, 23, "23", 2));
+ expect_err(tftp_window_cache_insert(cache, 24, "24", 2));
+ expect_err(tftp_window_cache_insert(cache, 19, "19", 2));
+ expect_ok (tftp_window_cache_insert(cache, 22, "22", 2));
+ expect_ok (tftp_window_cache_insert(cache, 20, "20", 2));
+
+ expect_eq(tftp_window_cache_pop(cache)->id, 20);
+ expect_eq(tftp_window_cache_pop(cache)->id, 21);
+ expect_eq(tftp_window_cache_pop(cache)->id, 22);
+ expect_eq(tftp_window_cache_pop(cache)->id, 23);
+ expect_eq(cache->id, TFTP_CACHE_NO_ID);
+
+ /* sequence 2 */
+ expect_ok (tftp_window_cache_insert(cache, 30, "30", 2));
+ expect_ok (tftp_window_cache_insert(cache, 32, "32", 2));
+ expect_err(tftp_window_cache_insert(cache, 34, "34", 2));
+
+ expect_it(tftp_window_cache_starts_with(cache, 30));
+ expect_eq(tftp_window_cache_pop(cache)->id, 30);
+
+ expect_ok (tftp_window_cache_insert(cache, 34, "34", 2));
+ expect_err(tftp_window_cache_insert(cache, 35, "35", 2));
+
+ expect_it(!tftp_window_cache_starts_with(cache, 30));
+ expect_it(!tftp_window_cache_starts_with(cache, 31));
+ expect_it(!tftp_window_cache_starts_with(cache, 32));
+ expect_NULL(tftp_window_cache_pop(cache));
+
+ expect_it(tftp_window_cache_starts_with(cache, 32));
+ expect_eq(tftp_window_cache_pop(cache)->id, 32);
+
+ expect_NULL(tftp_window_cache_pop(cache));
+ expect_eq(tftp_window_cache_pop(cache)->id, 34);
+
+ expect_eq(cache->id, TFTP_CACHE_NO_ID);
+
+ /* sequence 3 */
+ expect_ok(tftp_window_cache_insert(cache, 40, "40", 2));
+ expect_ok(tftp_window_cache_insert(cache, 42, "42", 2));
+ expect_ok(tftp_window_cache_insert(cache, 43, "43", 2));
+
+ expect_it(!tftp_window_cache_remove_id(cache, 30));
+ expect_it(!tftp_window_cache_remove_id(cache, 41));
+ expect_it(!tftp_window_cache_remove_id(cache, 44));
+
+ expect_it( tftp_window_cache_remove_id(cache, 42));
+ expect_it(!tftp_window_cache_remove_id(cache, 42));
+
+out:
+ tftp_window_cache_free(cache);
+
+ return 0;
+}
+#ifdef CONFIG_SELFTEST_TFTP
+bselftest(core, tftp_window_cache_selftest);
+#endif
diff --git a/test/self/Kconfig b/test/self/Kconfig
index cf11efe54486..9366f99300c5 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -29,6 +29,7 @@ config SELFTEST_ENABLE_ALL
bool "Enable all self-tests"
select SELFTEST_PRINTF
select SELFTEST_PROGRESS_NOTIFIER
+ imply SELFTEST_TFTP
help
Selects all self-tests compatible with current configuration
@@ -51,4 +52,10 @@ config SELFTEST_OF_MANIPULATION
config SELFTEST_PROGRESS_NOTIFIER
bool "progress notifier selftest"
+config SELFTEST_TFTP
+ bool "tftp selftest"
+ depends on FS_TFTP
+ help
+ Tests tftp functionality
+
endif
--
2.37.2
next prev parent reply other threads:[~2022-08-30 7:48 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-30 7:37 [PATCH v4 00/21] add "windowsize" (RFC 7440) support for tftp Enrico Scholz
2022-08-30 7:37 ` [PATCH v4 01/21] tftp: add some 'const' annotations Enrico Scholz
2022-08-30 7:37 ` [PATCH v4 02/21] tftp: allow to change tftp port Enrico Scholz
2022-08-30 7:37 ` [PATCH v4 03/21] cmd:tftp: add '-P' option to set tftp server port number Enrico Scholz
2022-08-30 7:37 ` [PATCH v4 04/21] tftp: do not set 'tsize' in WRQ requests Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 05/21] tftp: assign 'priv->block' later in WRQ Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 06/21] tftp: minor refactoring of RRQ/WRQ packet generation code Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 07/21] tftp: replace hardcoded blksize by global constant Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 08/21] tftp: remove sanity check of first block Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 09/21] tftp: add debug_assert() macro Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 10/21] tftp: allocate buffers and fifo dynamically Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 11/21] tftp: add sanity check for OACK response Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 12/21] tftp: record whether tftp file is opened for lookup operation only Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 13/21] tftp: reduce block size on lookup requests Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 14/21] tftp: refactor data processing Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 15/21] tftp: detect out-of-memory situations Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 16/21] tftp: implement 'windowsize' (RFC 7440) support Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 17/21] tftp: do not use 'priv->block' for RRQ Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 18/21] tftp: reorder tftp packets Enrico Scholz
2022-08-30 7:38 ` Enrico Scholz [this message]
2022-08-30 7:38 ` [PATCH v4 20/21] tftp: accept OACK + DATA datagrams only in certain states Enrico Scholz
2022-08-30 7:38 ` [PATCH v4 21/21] tftp: add some documentation about windowsize support Enrico Scholz
2022-08-31 6:40 ` [PATCH v4 00/21] add "windowsize" (RFC 7440) support for tftp Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220830073816.2694734-20-enrico.scholz@sigma-chemnitz.de \
--to=enrico.scholz@sigma-chemnitz.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox