1 // SPDX-License-Identifier: GPL-2.0+
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 <asm/global_data.h>
15 #include <dm/device.h>
18 #include <test/test.h>
19 #include <test/suites.h>
22 #include "syslog_test.h"
24 DECLARE_GLOBAL_DATA_PTR;
26 int sb_log_tx_handler(struct udevice *dev, void *packet, unsigned int len)
28 struct eth_sandbox_priv *priv = dev_get_priv(dev);
29 struct sb_log_env *env = priv->priv;
30 /* uts is updated by the ut_assert* macros */
31 struct unit_test_state *uts = env->uts;
33 struct ethernet_hdr *eth_hdr = packet;
34 struct ip_udp_hdr *ip_udp_hdr;
36 /* Check Ethernet header */
37 ut_asserteq_mem(ð_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
38 ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
41 buf += sizeof(struct ethernet_hdr);
42 ip_udp_hdr = (struct ip_udp_hdr *)buf;
43 ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
44 ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
45 ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
46 ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
47 ntohs(ip_udp_hdr->udp_len));
50 buf += sizeof(struct ip_udp_hdr);
51 ut_asserteq_mem(env->expected, buf,
52 ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
54 /* Signal that the callback function has been executed */
60 int syslog_test_setup(struct unit_test_state *uts)
62 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), true));
67 int syslog_test_finish(struct unit_test_state *uts)
69 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), false));
75 * log_test_syslog_err() - test log_err() function
77 * @uts: unit test state
80 static int log_test_syslog_err(struct unit_test_state *uts)
82 int old_log_level = gd->default_log_level;
83 struct sb_log_env env;
85 ut_assertok(syslog_test_setup(uts));
86 gd->log_fmt = LOGF_TEST;
87 gd->default_log_level = LOGL_INFO;
88 env_set("ethact", "eth@10002000");
89 env_set("log_hostname", "sandbox");
90 env.expected = "<3>sandbox uboot: log_test_syslog_err() "
93 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
94 /* Used by ut_assert macros in the tx_handler */
95 sandbox_eth_set_priv(0, &env);
96 log_err("testing %s\n", "log_err");
97 /* Check that the callback function was called */
98 sandbox_eth_set_tx_handler(0, NULL);
99 gd->default_log_level = old_log_level;
100 gd->log_fmt = log_get_default_format();
101 ut_assertok(syslog_test_finish(uts));
105 LOG_TEST(log_test_syslog_err);
108 * log_test_syslog_warning() - test log_warning() function
110 * @uts: unit test state
111 * Return: 0 = success
113 static int log_test_syslog_warning(struct unit_test_state *uts)
115 int old_log_level = gd->default_log_level;
116 struct sb_log_env env;
118 ut_assertok(syslog_test_setup(uts));
119 gd->log_fmt = LOGF_TEST;
120 gd->default_log_level = LOGL_INFO;
121 env_set("ethact", "eth@10002000");
122 env_set("log_hostname", "sandbox");
123 env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
124 "testing log_warning\n";
126 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
127 /* Used by ut_assert macros in the tx_handler */
128 sandbox_eth_set_priv(0, &env);
129 log_warning("testing %s\n", "log_warning");
130 sandbox_eth_set_tx_handler(0, NULL);
131 /* Check that the callback function was called */
132 ut_assertnull(env.expected);
133 gd->default_log_level = old_log_level;
134 gd->log_fmt = log_get_default_format();
135 ut_assertok(syslog_test_finish(uts));
139 LOG_TEST(log_test_syslog_warning);
142 * log_test_syslog_notice() - test log_notice() function
144 * @uts: unit test state
145 * Return: 0 = success
147 static int log_test_syslog_notice(struct unit_test_state *uts)
149 int old_log_level = gd->default_log_level;
150 struct sb_log_env env;
152 ut_assertok(syslog_test_setup(uts));
153 gd->log_fmt = LOGF_TEST;
154 gd->default_log_level = LOGL_INFO;
155 env_set("ethact", "eth@10002000");
156 env_set("log_hostname", "sandbox");
157 env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
158 "testing log_notice\n";
160 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
161 /* Used by ut_assert macros in the tx_handler */
162 sandbox_eth_set_priv(0, &env);
163 log_notice("testing %s\n", "log_notice");
164 sandbox_eth_set_tx_handler(0, NULL);
165 /* Check that the callback function was called */
166 ut_assertnull(env.expected);
167 gd->default_log_level = old_log_level;
168 gd->log_fmt = log_get_default_format();
169 ut_assertok(syslog_test_finish(uts));
173 LOG_TEST(log_test_syslog_notice);
176 * log_test_syslog_info() - test log_info() function
178 * @uts: unit test state
179 * Return: 0 = success
181 static int log_test_syslog_info(struct unit_test_state *uts)
183 int old_log_level = gd->default_log_level;
184 struct sb_log_env env;
186 ut_assertok(syslog_test_setup(uts));
187 gd->log_fmt = LOGF_TEST;
188 gd->default_log_level = LOGL_INFO;
189 env_set("ethact", "eth@10002000");
190 env_set("log_hostname", "sandbox");
191 env.expected = "<6>sandbox uboot: log_test_syslog_info() "
192 "testing log_info\n";
194 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
195 /* Used by ut_assert macros in the tx_handler */
196 sandbox_eth_set_priv(0, &env);
197 log_info("testing %s\n", "log_info");
198 sandbox_eth_set_tx_handler(0, NULL);
199 /* Check that the callback function was called */
200 ut_assertnull(env.expected);
201 gd->default_log_level = old_log_level;
202 gd->log_fmt = log_get_default_format();
203 ut_assertok(syslog_test_finish(uts));
207 LOG_TEST(log_test_syslog_info);
210 * log_test_syslog_debug() - test log_debug() function
212 * @uts: unit test state
213 * Return: 0 = success
215 static int log_test_syslog_debug(struct unit_test_state *uts)
217 int old_log_level = gd->default_log_level;
218 struct sb_log_env env;
220 ut_assertok(syslog_test_setup(uts));
221 gd->log_fmt = LOGF_TEST;
222 gd->default_log_level = LOGL_DEBUG;
223 env_set("ethact", "eth@10002000");
224 env_set("log_hostname", "sandbox");
225 env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
226 "testing log_debug\n";
228 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
229 /* Used by ut_assert macros in the tx_handler */
230 sandbox_eth_set_priv(0, &env);
231 log_debug("testing %s\n", "log_debug");
232 sandbox_eth_set_tx_handler(0, NULL);
233 /* Check that the callback function was called */
234 ut_assertnull(env.expected);
235 gd->default_log_level = old_log_level;
236 gd->log_fmt = log_get_default_format();
237 ut_assertok(syslog_test_finish(uts));
241 LOG_TEST(log_test_syslog_debug);