2020-02-26 20:48:19 +00:00
|
|
|
// 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>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2020-02-26 20:48:19 +00:00
|
|
|
#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>
|
2020-10-13 19:20:52 +00:00
|
|
|
#include "syslog_test.h"
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2020-09-12 17:13:34 +00:00
|
|
|
int sb_log_tx_handler(struct udevice *dev, void *packet, unsigned int len)
|
2020-02-26 20:48:19 +00:00
|
|
|
{
|
|
|
|
struct eth_sandbox_priv *priv = dev_get_priv(dev);
|
|
|
|
struct sb_log_env *env = priv->priv;
|
|
|
|
/* uts is updated by the ut_assert* macros */
|
|
|
|
struct unit_test_state *uts = env->uts;
|
|
|
|
char *buf = packet;
|
|
|
|
struct ethernet_hdr *eth_hdr = packet;
|
|
|
|
struct ip_udp_hdr *ip_udp_hdr;
|
|
|
|
|
|
|
|
/* Check Ethernet header */
|
|
|
|
ut_asserteq_mem(ð_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(env->expected) + 1,
|
|
|
|
ntohs(ip_udp_hdr->udp_len));
|
|
|
|
|
|
|
|
/* Check payload */
|
|
|
|
buf += sizeof(struct ip_udp_hdr);
|
|
|
|
ut_asserteq_mem(env->expected, buf,
|
|
|
|
ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
|
|
|
|
|
|
|
|
/* Signal that the callback function has been executed */
|
|
|
|
env->expected = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-12 18:28:50 +00:00
|
|
|
int syslog_test_setup(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), true));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int syslog_test_finish(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), false));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:48:19 +00:00
|
|
|
/**
|
2020-05-06 16:26:08 +00:00
|
|
|
* log_test_syslog_err() - test log_err() function
|
2020-02-26 20:48:19 +00:00
|
|
|
*
|
|
|
|
* @uts: unit test state
|
|
|
|
* Return: 0 = success
|
|
|
|
*/
|
2020-05-06 16:26:08 +00:00
|
|
|
static int log_test_syslog_err(struct unit_test_state *uts)
|
2020-02-26 20:48:19 +00:00
|
|
|
{
|
|
|
|
int old_log_level = gd->default_log_level;
|
|
|
|
struct sb_log_env env;
|
|
|
|
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_setup(uts));
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = LOGF_TEST;
|
2020-02-26 20:48:19 +00:00
|
|
|
gd->default_log_level = LOGL_INFO;
|
|
|
|
env_set("ethact", "eth@10002000");
|
|
|
|
env_set("log_hostname", "sandbox");
|
2020-05-06 16:26:08 +00:00
|
|
|
env.expected = "<3>sandbox uboot: log_test_syslog_err() "
|
2020-02-26 20:48:19 +00:00
|
|
|
"testing log_err\n";
|
|
|
|
env.uts = uts;
|
|
|
|
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
|
|
|
|
/* Used by ut_assert macros in the tx_handler */
|
|
|
|
sandbox_eth_set_priv(0, &env);
|
|
|
|
log_err("testing %s\n", "log_err");
|
|
|
|
/* Check that the callback function was called */
|
|
|
|
sandbox_eth_set_tx_handler(0, NULL);
|
|
|
|
gd->default_log_level = old_log_level;
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = log_get_default_format();
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_finish(uts));
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-06 16:26:08 +00:00
|
|
|
LOG_TEST(log_test_syslog_err);
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
/**
|
2020-05-06 16:26:08 +00:00
|
|
|
* log_test_syslog_warning() - test log_warning() function
|
2020-02-26 20:48:19 +00:00
|
|
|
*
|
|
|
|
* @uts: unit test state
|
|
|
|
* Return: 0 = success
|
|
|
|
*/
|
2020-05-06 16:26:08 +00:00
|
|
|
static int log_test_syslog_warning(struct unit_test_state *uts)
|
2020-02-26 20:48:19 +00:00
|
|
|
{
|
|
|
|
int old_log_level = gd->default_log_level;
|
|
|
|
struct sb_log_env env;
|
|
|
|
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_setup(uts));
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = LOGF_TEST;
|
2020-02-26 20:48:19 +00:00
|
|
|
gd->default_log_level = LOGL_INFO;
|
|
|
|
env_set("ethact", "eth@10002000");
|
|
|
|
env_set("log_hostname", "sandbox");
|
2020-05-06 16:26:08 +00:00
|
|
|
env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
|
2020-02-26 20:48:19 +00:00
|
|
|
"testing log_warning\n";
|
|
|
|
env.uts = uts;
|
|
|
|
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
|
|
|
|
/* Used by ut_assert macros in the tx_handler */
|
|
|
|
sandbox_eth_set_priv(0, &env);
|
|
|
|
log_warning("testing %s\n", "log_warning");
|
|
|
|
sandbox_eth_set_tx_handler(0, NULL);
|
|
|
|
/* Check that the callback function was called */
|
|
|
|
ut_assertnull(env.expected);
|
|
|
|
gd->default_log_level = old_log_level;
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = log_get_default_format();
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_finish(uts));
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-06 16:26:08 +00:00
|
|
|
LOG_TEST(log_test_syslog_warning);
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
/**
|
2020-05-06 16:26:08 +00:00
|
|
|
* log_test_syslog_notice() - test log_notice() function
|
2020-02-26 20:48:19 +00:00
|
|
|
*
|
|
|
|
* @uts: unit test state
|
|
|
|
* Return: 0 = success
|
|
|
|
*/
|
2020-05-06 16:26:08 +00:00
|
|
|
static int log_test_syslog_notice(struct unit_test_state *uts)
|
2020-02-26 20:48:19 +00:00
|
|
|
{
|
|
|
|
int old_log_level = gd->default_log_level;
|
|
|
|
struct sb_log_env env;
|
|
|
|
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_setup(uts));
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = LOGF_TEST;
|
2020-02-26 20:48:19 +00:00
|
|
|
gd->default_log_level = LOGL_INFO;
|
|
|
|
env_set("ethact", "eth@10002000");
|
|
|
|
env_set("log_hostname", "sandbox");
|
2020-05-06 16:26:08 +00:00
|
|
|
env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
|
2020-02-26 20:48:19 +00:00
|
|
|
"testing log_notice\n";
|
|
|
|
env.uts = uts;
|
|
|
|
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
|
|
|
|
/* Used by ut_assert macros in the tx_handler */
|
|
|
|
sandbox_eth_set_priv(0, &env);
|
|
|
|
log_notice("testing %s\n", "log_notice");
|
|
|
|
sandbox_eth_set_tx_handler(0, NULL);
|
|
|
|
/* Check that the callback function was called */
|
|
|
|
ut_assertnull(env.expected);
|
|
|
|
gd->default_log_level = old_log_level;
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = log_get_default_format();
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_finish(uts));
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-06 16:26:08 +00:00
|
|
|
LOG_TEST(log_test_syslog_notice);
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
/**
|
2020-05-06 16:26:08 +00:00
|
|
|
* log_test_syslog_info() - test log_info() function
|
2020-02-26 20:48:19 +00:00
|
|
|
*
|
|
|
|
* @uts: unit test state
|
|
|
|
* Return: 0 = success
|
|
|
|
*/
|
2020-05-06 16:26:08 +00:00
|
|
|
static int log_test_syslog_info(struct unit_test_state *uts)
|
2020-02-26 20:48:19 +00:00
|
|
|
{
|
|
|
|
int old_log_level = gd->default_log_level;
|
|
|
|
struct sb_log_env env;
|
|
|
|
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_setup(uts));
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = LOGF_TEST;
|
2020-02-26 20:48:19 +00:00
|
|
|
gd->default_log_level = LOGL_INFO;
|
|
|
|
env_set("ethact", "eth@10002000");
|
|
|
|
env_set("log_hostname", "sandbox");
|
2020-05-06 16:26:08 +00:00
|
|
|
env.expected = "<6>sandbox uboot: log_test_syslog_info() "
|
2020-02-26 20:48:19 +00:00
|
|
|
"testing log_info\n";
|
|
|
|
env.uts = uts;
|
|
|
|
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
|
|
|
|
/* Used by ut_assert macros in the tx_handler */
|
|
|
|
sandbox_eth_set_priv(0, &env);
|
|
|
|
log_info("testing %s\n", "log_info");
|
|
|
|
sandbox_eth_set_tx_handler(0, NULL);
|
|
|
|
/* Check that the callback function was called */
|
|
|
|
ut_assertnull(env.expected);
|
|
|
|
gd->default_log_level = old_log_level;
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = log_get_default_format();
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_finish(uts));
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-06 16:26:08 +00:00
|
|
|
LOG_TEST(log_test_syslog_info);
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
/**
|
2020-05-06 16:26:08 +00:00
|
|
|
* log_test_syslog_debug() - test log_debug() function
|
2020-02-26 20:48:19 +00:00
|
|
|
*
|
|
|
|
* @uts: unit test state
|
|
|
|
* Return: 0 = success
|
|
|
|
*/
|
2020-05-06 16:26:08 +00:00
|
|
|
static int log_test_syslog_debug(struct unit_test_state *uts)
|
2020-02-26 20:48:19 +00:00
|
|
|
{
|
|
|
|
int old_log_level = gd->default_log_level;
|
|
|
|
struct sb_log_env env;
|
|
|
|
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_setup(uts));
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = LOGF_TEST;
|
2020-02-26 20:48:19 +00:00
|
|
|
gd->default_log_level = LOGL_DEBUG;
|
|
|
|
env_set("ethact", "eth@10002000");
|
|
|
|
env_set("log_hostname", "sandbox");
|
2020-05-06 16:26:08 +00:00
|
|
|
env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
|
2020-02-26 20:48:19 +00:00
|
|
|
"testing log_debug\n";
|
|
|
|
env.uts = uts;
|
|
|
|
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
|
|
|
|
/* Used by ut_assert macros in the tx_handler */
|
|
|
|
sandbox_eth_set_priv(0, &env);
|
|
|
|
log_debug("testing %s\n", "log_debug");
|
|
|
|
sandbox_eth_set_tx_handler(0, NULL);
|
|
|
|
/* Check that the callback function was called */
|
|
|
|
ut_assertnull(env.expected);
|
|
|
|
gd->default_log_level = old_log_level;
|
2020-06-17 19:52:44 +00:00
|
|
|
gd->log_fmt = log_get_default_format();
|
2020-09-12 18:28:50 +00:00
|
|
|
ut_assertok(syslog_test_finish(uts));
|
2020-02-26 20:48:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-06 16:26:08 +00:00
|
|
|
LOG_TEST(log_test_syslog_debug);
|