diff mbox series

[v3,5/6] test: log: test syslog logging driver

Message ID 20200215101417.20482-6-xypron.glpk@gmx.de
State Superseded, archived
Delegated to: Simon Glass
Headers show
Series log: syslog logging driver | expand

Commit Message

Heinrich Schuchardt Feb. 15, 2020, 10:14 a.m. UTC
Provide unit tests for the syslog logging driver.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
v3:
	new patch
---
 test/log/Makefile      |   4 +
 test/log/syslog_test.c | 186 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)
 create mode 100644 test/log/syslog_test.c

--
2.25.0

Comments

Simon Glass Feb. 16, 2020, 7:02 p.m. UTC | #1
On Sat, 15 Feb 2020 at 03:14, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> Provide unit tests for the syslog logging driver.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
> v3:
>         new patch
> ---
>  test/log/Makefile      |   4 +
>  test/log/syslog_test.c | 186 +++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 190 insertions(+)
>  create mode 100644 test/log/syslog_test.c
>

Reviewed-by: Simon Glass <sjg@chromium.org>

This is a great set of tests.

But please see below.

> diff --git a/test/log/Makefile b/test/log/Makefile
> index 98178f5e2b..4c92550f6e 100644
> --- a/test/log/Makefile
> +++ b/test/log/Makefile
> @@ -8,6 +8,10 @@ ifdef CONFIG_UT_LOG
>
>  obj-y += test-main.o
>
> +ifdef CONFIG_SANDBOX
> +obj-$(CONFIG_LOG_SYSLOG) += syslog_test.o
> +endif
> +
>  ifndef CONFIG_LOG
>  obj-$(CONFIG_CONSOLE_RECORD) += nolog_test.o
>  endif
> diff --git a/test/log/syslog_test.c b/test/log/syslog_test.c
> new file mode 100644
> index 0000000000..7f3321f680
> --- /dev/null
> +++ b/test/log/syslog_test.c
> @@ -0,0 +1,186 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
> + *
> + * Logging function tests for CONFIG_LOG_SYSLOG=y.
> + *
> + * Invoke the test with: ./u-boot -d arch/sandbox/dts/test.dtb
> + */
> +
> +/* Override CONFIG_LOG_MAX_LEVEL */
> +#define LOG_DEBUG
> +
> +#include <common.h>
> +#include <dm/device.h>
> +#include <hexdump.h>
> +#include <test/log.h>
> +#include <test/test.h>
> +#include <test/suites.h>
> +#include <test/ut.h>
> +#include <asm/eth.h>
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +const char *expected;

comment? It seems to have strange behaviour.

> +
> +static int sb_log_tx_handler(struct udevice *dev, void *packet,
> +                            unsigned int len)

Add function comment

> +{
> +       struct eth_sandbox_priv *priv = dev_get_priv(dev);
> +       struct unit_test_state *uts = priv->priv;
> +       char *buf = packet;
> +       struct ethernet_hdr *eth_hdr = packet;
> +       struct ip_udp_hdr *ip_udp_hdr;
> +
> +       /* Check Ethernet header */
> +       ut_asserteq_mem(&eth_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
> +       ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
> +
> +       /* Check IP header */
> +       buf += sizeof(struct ethernet_hdr);
> +       ip_udp_hdr = (struct ip_udp_hdr *)buf;
> +       ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
> +       ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
> +       ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
> +       ut_asserteq(UDP_HDR_SIZE + strlen(expected) + 1,
> +                   ntohs(ip_udp_hdr->udp_len));
> +
> +       /* Check payload */
> +       buf += sizeof(struct ip_udp_hdr);
> +       ut_asserteq_mem(expected, buf,
> +                       ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
> +       expected = NULL;
> +
> +       return 0;
> +}
> +
> +static int syslog_test_log_err(struct unit_test_state *uts)
> +{
> +       int old_log_level = gd->default_log_level;
> +
> +       gd->log_fmt = LOGF_DEFAULT;
> +       gd->default_log_level = LOGL_INFO;
> +       env_set("ethact", "eth@10002000");
> +       env_set("log_hostname", "sandbox");
> +       expected = "<3>sandbox uboot[1]: syslog_test_log_err() "
> +                  "testing log_err\n";
> +       sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
> +       /* Used by ut_assert macros in the tx_handler */
> +       sandbox_eth_set_priv(0, uts);
> +       log_err("testing %s\n", "log_err");
> +       sandbox_eth_set_tx_handler(0, NULL);
> +       gd->default_log_level = old_log_level;
> +
> +       return 0;
> +}
> +LOG_TEST(syslog_test_log_err);
> +
> +static int syslog_test_log_warning(struct unit_test_state *uts)
> +{
> +       int old_log_level = gd->default_log_level;
> +
> +       gd->log_fmt = LOGF_DEFAULT;
> +       gd->default_log_level = LOGL_INFO;
> +       env_set("ethact", "eth@10002000");
> +       env_set("log_hostname", "sandbox");
> +       expected = "<4>sandbox uboot[1]: syslog_test_log_warning() "
> +                  "testing log_warning\n";
> +       sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
> +       /* Used by ut_assert macros in the tx_handler */
> +       sandbox_eth_set_priv(0, uts);
> +       log_warning("testing %s\n", "log_warning");
> +       sandbox_eth_set_tx_handler(0, NULL);
> +       ut_assertnull(expected);

Comment here that expected is set to NULL in the tx handler

> +       gd->default_log_level = old_log_level;
> +
> +       return 0;
> +}
> +LOG_TEST(syslog_test_log_warning);
> +

Regards,
Simon
diff mbox series

Patch

diff --git a/test/log/Makefile b/test/log/Makefile
index 98178f5e2b..4c92550f6e 100644
--- a/test/log/Makefile
+++ b/test/log/Makefile
@@ -8,6 +8,10 @@  ifdef CONFIG_UT_LOG

 obj-y += test-main.o

+ifdef CONFIG_SANDBOX
+obj-$(CONFIG_LOG_SYSLOG) += syslog_test.o
+endif
+
 ifndef CONFIG_LOG
 obj-$(CONFIG_CONSOLE_RECORD) += nolog_test.o
 endif
diff --git a/test/log/syslog_test.c b/test/log/syslog_test.c
new file mode 100644
index 0000000000..7f3321f680
--- /dev/null
+++ b/test/log/syslog_test.c
@@ -0,0 +1,186 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
+ *
+ * Logging function tests for CONFIG_LOG_SYSLOG=y.
+ *
+ * Invoke the test with: ./u-boot -d arch/sandbox/dts/test.dtb
+ */
+
+/* Override CONFIG_LOG_MAX_LEVEL */
+#define LOG_DEBUG
+
+#include <common.h>
+#include <dm/device.h>
+#include <hexdump.h>
+#include <test/log.h>
+#include <test/test.h>
+#include <test/suites.h>
+#include <test/ut.h>
+#include <asm/eth.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+const char *expected;
+
+static int sb_log_tx_handler(struct udevice *dev, void *packet,
+			     unsigned int len)
+{
+	struct eth_sandbox_priv *priv = dev_get_priv(dev);
+	struct unit_test_state *uts = priv->priv;
+	char *buf = packet;
+	struct ethernet_hdr *eth_hdr = packet;
+	struct ip_udp_hdr *ip_udp_hdr;
+
+	/* Check Ethernet header */
+	ut_asserteq_mem(&eth_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
+	ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
+
+	/* Check IP header */
+	buf += sizeof(struct ethernet_hdr);
+	ip_udp_hdr = (struct ip_udp_hdr *)buf;
+	ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
+	ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
+	ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
+	ut_asserteq(UDP_HDR_SIZE + strlen(expected) + 1,
+		    ntohs(ip_udp_hdr->udp_len));
+
+	/* Check payload */
+	buf += sizeof(struct ip_udp_hdr);
+	ut_asserteq_mem(expected, buf,
+			ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
+	expected = NULL;
+
+	return 0;
+}
+
+static int syslog_test_log_err(struct unit_test_state *uts)
+{
+	int old_log_level = gd->default_log_level;
+
+	gd->log_fmt = LOGF_DEFAULT;
+	gd->default_log_level = LOGL_INFO;
+	env_set("ethact", "eth@10002000");
+	env_set("log_hostname", "sandbox");
+	expected = "<3>sandbox uboot[1]: syslog_test_log_err() "
+		   "testing log_err\n";
+	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
+	/* Used by ut_assert macros in the tx_handler */
+	sandbox_eth_set_priv(0, uts);
+	log_err("testing %s\n", "log_err");
+	sandbox_eth_set_tx_handler(0, NULL);
+	gd->default_log_level = old_log_level;
+
+	return 0;
+}
+LOG_TEST(syslog_test_log_err);
+
+static int syslog_test_log_warning(struct unit_test_state *uts)
+{
+	int old_log_level = gd->default_log_level;
+
+	gd->log_fmt = LOGF_DEFAULT;
+	gd->default_log_level = LOGL_INFO;
+	env_set("ethact", "eth@10002000");
+	env_set("log_hostname", "sandbox");
+	expected = "<4>sandbox uboot[1]: syslog_test_log_warning() "
+		   "testing log_warning\n";
+	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
+	/* Used by ut_assert macros in the tx_handler */
+	sandbox_eth_set_priv(0, uts);
+	log_warning("testing %s\n", "log_warning");
+	sandbox_eth_set_tx_handler(0, NULL);
+	ut_assertnull(expected);
+	gd->default_log_level = old_log_level;
+
+	return 0;
+}
+LOG_TEST(syslog_test_log_warning);
+
+static int syslog_test_log_notice(struct unit_test_state *uts)
+{
+	int old_log_level = gd->default_log_level;
+
+	gd->log_fmt = LOGF_DEFAULT;
+	gd->default_log_level = LOGL_INFO;
+	env_set("ethact", "eth@10002000");
+	env_set("log_hostname", "sandbox");
+	expected = "<5>sandbox uboot[1]: syslog_test_log_notice() "
+		   "testing log_notice\n";
+	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
+	/* Used by ut_assert macros in the tx_handler */
+	sandbox_eth_set_priv(0, uts);
+	log_notice("testing %s\n", "log_notice");
+	sandbox_eth_set_tx_handler(0, NULL);
+	ut_assertnull(expected);
+	gd->default_log_level = old_log_level;
+
+	return 0;
+}
+LOG_TEST(syslog_test_log_notice);
+
+static int syslog_test_log_info(struct unit_test_state *uts)
+{
+	int old_log_level = gd->default_log_level;
+
+	gd->log_fmt = LOGF_DEFAULT;
+	gd->default_log_level = LOGL_INFO;
+	env_set("ethact", "eth@10002000");
+	env_set("log_hostname", "sandbox");
+	expected = "<6>sandbox uboot[1]: syslog_test_log_info() "
+		   "testing log_info\n";
+	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
+	/* Used by ut_assert macros in the tx_handler */
+	sandbox_eth_set_priv(0, uts);
+	log_info("testing %s\n", "log_info");
+	sandbox_eth_set_tx_handler(0, NULL);
+	ut_assertnull(expected);
+	gd->default_log_level = old_log_level;
+
+	return 0;
+}
+LOG_TEST(syslog_test_log_info);
+
+static int syslog_test_log_debug(struct unit_test_state *uts)
+{
+	int old_log_level = gd->default_log_level;
+
+	gd->log_fmt = LOGF_DEFAULT;
+	gd->default_log_level = LOGL_DEBUG;
+	env_set("ethact", "eth@10002000");
+	env_set("log_hostname", "sandbox");
+	expected = "<7>sandbox uboot[1]: syslog_test_log_debug() "
+		   "testing log_debug\n";
+	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
+	/* Used by ut_assert macros in the tx_handler */
+	sandbox_eth_set_priv(0, uts);
+	log_debug("testing %s\n", "log_debug");
+	sandbox_eth_set_tx_handler(0, NULL);
+	ut_assertnull(expected);
+	gd->default_log_level = old_log_level;
+
+	return 0;
+}
+LOG_TEST(syslog_test_log_debug);
+
+static int syslog_test_log_nodebug(struct unit_test_state *uts)
+{
+	int old_log_level = gd->default_log_level;
+
+	gd->log_fmt = LOGF_DEFAULT;
+	gd->default_log_level = LOGL_INFO;
+	env_set("ethact", "eth@10002000");
+	env_set("log_hostname", "sandbox");
+	expected = "<7>sandbox uboot[1]: syslog_test_log_nodebug() "
+		   "testing log_debug\n";
+	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
+	/* Used by ut_assert macros in the tx_handler */
+	sandbox_eth_set_priv(0, uts);
+	log_debug("testing %s\n", "log_debug");
+	sandbox_eth_set_tx_handler(0, NULL);
+	ut_assertnonnull(expected);
+	gd->default_log_level = old_log_level;
+
+	return 0;
+}
+LOG_TEST(syslog_test_log_nodebug);