1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
5 * Logging function tests for CONFIG_LOG_SYSLOG=y.
7 * Invoke the test with: ./u-boot -d arch/sandbox/dts/test.dtb
10 /* Override CONFIG_LOG_MAX_LEVEL */
14 #include <dm/device.h>
17 #include <test/test.h>
18 #include <test/suites.h>
21 #include "syslog_test.h"
23 DECLARE_GLOBAL_DATA_PTR;
25 int sb_log_tx_handler(struct udevice *dev, void *packet, unsigned int len)
27 struct eth_sandbox_priv *priv = dev_get_priv(dev);
28 struct sb_log_env *env = priv->priv;
29 /* uts is updated by the ut_assert* macros */
30 struct unit_test_state *uts = env->uts;
32 struct ethernet_hdr *eth_hdr = packet;
33 struct ip_udp_hdr *ip_udp_hdr;
35 /* Check Ethernet header */
36 ut_asserteq_mem(ð_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
37 ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
40 buf += sizeof(struct ethernet_hdr);
41 ip_udp_hdr = (struct ip_udp_hdr *)buf;
42 ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
43 ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
44 ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
45 ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
46 ntohs(ip_udp_hdr->udp_len));
49 buf += sizeof(struct ip_udp_hdr);
50 ut_asserteq_mem(env->expected, buf,
51 ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
53 /* Signal that the callback function has been executed */
59 int syslog_test_setup(struct unit_test_state *uts)
61 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), true));
66 int syslog_test_finish(struct unit_test_state *uts)
68 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), false));
74 * log_test_syslog_err() - test log_err() function
76 * @uts: unit test state
79 static int log_test_syslog_err(struct unit_test_state *uts)
81 int old_log_level = gd->default_log_level;
82 struct sb_log_env env;
84 ut_assertok(syslog_test_setup(uts));
85 gd->log_fmt = LOGF_TEST;
86 gd->default_log_level = LOGL_INFO;
87 env_set("ethact", "eth@10002000");
88 env_set("log_hostname", "sandbox");
89 env.expected = "<3>sandbox uboot: log_test_syslog_err() "
92 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
93 /* Used by ut_assert macros in the tx_handler */
94 sandbox_eth_set_priv(0, &env);
95 log_err("testing %s\n", "log_err");
96 /* Check that the callback function was called */
97 sandbox_eth_set_tx_handler(0, NULL);
98 gd->default_log_level = old_log_level;
99 gd->log_fmt = log_get_default_format();
100 ut_assertok(syslog_test_finish(uts));
104 LOG_TEST(log_test_syslog_err);
107 * log_test_syslog_warning() - test log_warning() function
109 * @uts: unit test state
110 * Return: 0 = success
112 static int log_test_syslog_warning(struct unit_test_state *uts)
114 int old_log_level = gd->default_log_level;
115 struct sb_log_env env;
117 ut_assertok(syslog_test_setup(uts));
118 gd->log_fmt = LOGF_TEST;
119 gd->default_log_level = LOGL_INFO;
120 env_set("ethact", "eth@10002000");
121 env_set("log_hostname", "sandbox");
122 env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
123 "testing log_warning\n";
125 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
126 /* Used by ut_assert macros in the tx_handler */
127 sandbox_eth_set_priv(0, &env);
128 log_warning("testing %s\n", "log_warning");
129 sandbox_eth_set_tx_handler(0, NULL);
130 /* Check that the callback function was called */
131 ut_assertnull(env.expected);
132 gd->default_log_level = old_log_level;
133 gd->log_fmt = log_get_default_format();
134 ut_assertok(syslog_test_finish(uts));
138 LOG_TEST(log_test_syslog_warning);
141 * log_test_syslog_notice() - test log_notice() function
143 * @uts: unit test state
144 * Return: 0 = success
146 static int log_test_syslog_notice(struct unit_test_state *uts)
148 int old_log_level = gd->default_log_level;
149 struct sb_log_env env;
151 ut_assertok(syslog_test_setup(uts));
152 gd->log_fmt = LOGF_TEST;
153 gd->default_log_level = LOGL_INFO;
154 env_set("ethact", "eth@10002000");
155 env_set("log_hostname", "sandbox");
156 env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
157 "testing log_notice\n";
159 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
160 /* Used by ut_assert macros in the tx_handler */
161 sandbox_eth_set_priv(0, &env);
162 log_notice("testing %s\n", "log_notice");
163 sandbox_eth_set_tx_handler(0, NULL);
164 /* Check that the callback function was called */
165 ut_assertnull(env.expected);
166 gd->default_log_level = old_log_level;
167 gd->log_fmt = log_get_default_format();
168 ut_assertok(syslog_test_finish(uts));
172 LOG_TEST(log_test_syslog_notice);
175 * log_test_syslog_info() - test log_info() function
177 * @uts: unit test state
178 * Return: 0 = success
180 static int log_test_syslog_info(struct unit_test_state *uts)
182 int old_log_level = gd->default_log_level;
183 struct sb_log_env env;
185 ut_assertok(syslog_test_setup(uts));
186 gd->log_fmt = LOGF_TEST;
187 gd->default_log_level = LOGL_INFO;
188 env_set("ethact", "eth@10002000");
189 env_set("log_hostname", "sandbox");
190 env.expected = "<6>sandbox uboot: log_test_syslog_info() "
191 "testing log_info\n";
193 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
194 /* Used by ut_assert macros in the tx_handler */
195 sandbox_eth_set_priv(0, &env);
196 log_info("testing %s\n", "log_info");
197 sandbox_eth_set_tx_handler(0, NULL);
198 /* Check that the callback function was called */
199 ut_assertnull(env.expected);
200 gd->default_log_level = old_log_level;
201 gd->log_fmt = log_get_default_format();
202 ut_assertok(syslog_test_finish(uts));
206 LOG_TEST(log_test_syslog_info);
209 * log_test_syslog_debug() - test log_debug() function
211 * @uts: unit test state
212 * Return: 0 = success
214 static int log_test_syslog_debug(struct unit_test_state *uts)
216 int old_log_level = gd->default_log_level;
217 struct sb_log_env env;
219 ut_assertok(syslog_test_setup(uts));
220 gd->log_fmt = LOGF_TEST;
221 gd->default_log_level = LOGL_DEBUG;
222 env_set("ethact", "eth@10002000");
223 env_set("log_hostname", "sandbox");
224 env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
225 "testing log_debug\n";
227 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
228 /* Used by ut_assert macros in the tx_handler */
229 sandbox_eth_set_priv(0, &env);
230 log_debug("testing %s\n", "log_debug");
231 sandbox_eth_set_tx_handler(0, NULL);
232 /* Check that the callback function was called */
233 ut_assertnull(env.expected);
234 gd->default_log_level = old_log_level;
235 gd->log_fmt = log_get_default_format();
236 ut_assertok(syslog_test_finish(uts));
240 LOG_TEST(log_test_syslog_debug);