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>
22 DECLARE_GLOBAL_DATA_PTR;
24 #define LOGF_TEST (BIT(LOGF_FUNC) | BIT(LOGF_MSG))
27 * struct sb_log_env - private data for sandbox ethernet driver
29 * This structure is used for the private data of the sandbox ethernet
32 * @expected: string expected to be written by the syslog driver
33 * @uts: unit test state
37 struct unit_test_state *uts;
41 * sb_log_tx_handler() - transmit callback function
43 * This callback function is invoked when a network package is sent using the
44 * sandbox Ethernet driver. The private data of the driver holds a sb_log_env
45 * structure with the unit test state and the expected UDP payload.
47 * The following checks are executed:
49 * * the Ethernet packet indicates a IP broadcast message
50 * * the IP header is for a local UDP broadcast message to port 514
51 * * the UDP payload matches the expected string
53 * After testing the pointer to the expected string is set to NULL to signal
54 * that the callback function has been called.
56 * @dev: sandbox ethernet device
57 * @packet: Ethernet packet
58 * @len: length of Ethernet packet
61 static int sb_log_tx_handler(struct udevice *dev, void *packet,
64 struct eth_sandbox_priv *priv = dev_get_priv(dev);
65 struct sb_log_env *env = priv->priv;
66 /* uts is updated by the ut_assert* macros */
67 struct unit_test_state *uts = env->uts;
69 struct ethernet_hdr *eth_hdr = packet;
70 struct ip_udp_hdr *ip_udp_hdr;
72 /* Check Ethernet header */
73 ut_asserteq_mem(ð_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
74 ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
77 buf += sizeof(struct ethernet_hdr);
78 ip_udp_hdr = (struct ip_udp_hdr *)buf;
79 ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
80 ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
81 ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
82 ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
83 ntohs(ip_udp_hdr->udp_len));
86 buf += sizeof(struct ip_udp_hdr);
87 ut_asserteq_mem(env->expected, buf,
88 ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
90 /* Signal that the callback function has been executed */
97 * log_test_syslog_err() - test log_err() function
99 * @uts: unit test state
100 * Return: 0 = success
102 static int log_test_syslog_err(struct unit_test_state *uts)
104 int old_log_level = gd->default_log_level;
105 struct sb_log_env env;
107 gd->log_fmt = LOGF_TEST;
108 gd->default_log_level = LOGL_INFO;
109 env_set("ethact", "eth@10002000");
110 env_set("log_hostname", "sandbox");
111 env.expected = "<3>sandbox uboot: log_test_syslog_err() "
114 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
115 /* Used by ut_assert macros in the tx_handler */
116 sandbox_eth_set_priv(0, &env);
117 log_err("testing %s\n", "log_err");
118 /* Check that the callback function was called */
119 sandbox_eth_set_tx_handler(0, NULL);
120 gd->default_log_level = old_log_level;
121 gd->log_fmt = log_get_default_format();
125 LOG_TEST(log_test_syslog_err);
128 * log_test_syslog_warning() - test log_warning() function
130 * @uts: unit test state
131 * Return: 0 = success
133 static int log_test_syslog_warning(struct unit_test_state *uts)
135 int old_log_level = gd->default_log_level;
136 struct sb_log_env env;
138 gd->log_fmt = LOGF_TEST;
139 gd->default_log_level = LOGL_INFO;
140 env_set("ethact", "eth@10002000");
141 env_set("log_hostname", "sandbox");
142 env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
143 "testing log_warning\n";
145 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
146 /* Used by ut_assert macros in the tx_handler */
147 sandbox_eth_set_priv(0, &env);
148 log_warning("testing %s\n", "log_warning");
149 sandbox_eth_set_tx_handler(0, NULL);
150 /* Check that the callback function was called */
151 ut_assertnull(env.expected);
152 gd->default_log_level = old_log_level;
153 gd->log_fmt = log_get_default_format();
157 LOG_TEST(log_test_syslog_warning);
160 * log_test_syslog_notice() - test log_notice() function
162 * @uts: unit test state
163 * Return: 0 = success
165 static int log_test_syslog_notice(struct unit_test_state *uts)
167 int old_log_level = gd->default_log_level;
168 struct sb_log_env env;
170 gd->log_fmt = LOGF_TEST;
171 gd->default_log_level = LOGL_INFO;
172 env_set("ethact", "eth@10002000");
173 env_set("log_hostname", "sandbox");
174 env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
175 "testing log_notice\n";
177 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
178 /* Used by ut_assert macros in the tx_handler */
179 sandbox_eth_set_priv(0, &env);
180 log_notice("testing %s\n", "log_notice");
181 sandbox_eth_set_tx_handler(0, NULL);
182 /* Check that the callback function was called */
183 ut_assertnull(env.expected);
184 gd->default_log_level = old_log_level;
185 gd->log_fmt = log_get_default_format();
189 LOG_TEST(log_test_syslog_notice);
192 * log_test_syslog_info() - test log_info() function
194 * @uts: unit test state
195 * Return: 0 = success
197 static int log_test_syslog_info(struct unit_test_state *uts)
199 int old_log_level = gd->default_log_level;
200 struct sb_log_env env;
202 gd->log_fmt = LOGF_TEST;
203 gd->default_log_level = LOGL_INFO;
204 env_set("ethact", "eth@10002000");
205 env_set("log_hostname", "sandbox");
206 env.expected = "<6>sandbox uboot: log_test_syslog_info() "
207 "testing log_info\n";
209 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
210 /* Used by ut_assert macros in the tx_handler */
211 sandbox_eth_set_priv(0, &env);
212 log_info("testing %s\n", "log_info");
213 sandbox_eth_set_tx_handler(0, NULL);
214 /* Check that the callback function was called */
215 ut_assertnull(env.expected);
216 gd->default_log_level = old_log_level;
217 gd->log_fmt = log_get_default_format();
221 LOG_TEST(log_test_syslog_info);
224 * log_test_syslog_debug() - test log_debug() function
226 * @uts: unit test state
227 * Return: 0 = success
229 static int log_test_syslog_debug(struct unit_test_state *uts)
231 int old_log_level = gd->default_log_level;
232 struct sb_log_env env;
234 gd->log_fmt = LOGF_TEST;
235 gd->default_log_level = LOGL_DEBUG;
236 env_set("ethact", "eth@10002000");
237 env_set("log_hostname", "sandbox");
238 env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
239 "testing log_debug\n";
241 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
242 /* Used by ut_assert macros in the tx_handler */
243 sandbox_eth_set_priv(0, &env);
244 log_debug("testing %s\n", "log_debug");
245 sandbox_eth_set_tx_handler(0, NULL);
246 /* Check that the callback function was called */
247 ut_assertnull(env.expected);
248 gd->default_log_level = old_log_level;
249 gd->log_fmt = log_get_default_format();
253 LOG_TEST(log_test_syslog_debug);
256 * log_test_syslog_nodebug() - test logging level filter
258 * Verify that log_debug() does not lead to a log message if the logging level
259 * is set to LOGL_INFO.
261 * @uts: unit test state
262 * Return: 0 = success
264 static int log_test_syslog_nodebug(struct unit_test_state *uts)
266 int old_log_level = gd->default_log_level;
267 struct sb_log_env env;
269 gd->log_fmt = LOGF_TEST;
270 gd->default_log_level = LOGL_INFO;
271 env_set("ethact", "eth@10002000");
272 env_set("log_hostname", "sandbox");
273 env.expected = "<7>sandbox uboot: log_test_syslog_nodebug() "
274 "testing log_debug\n";
276 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
277 /* Used by ut_assert macros in the tx_handler */
278 sandbox_eth_set_priv(0, &env);
279 log_debug("testing %s\n", "log_debug");
280 sandbox_eth_set_tx_handler(0, NULL);
281 /* Check that the callback function was not called */
282 ut_assertnonnull(env.expected);
283 gd->default_log_level = old_log_level;
284 gd->log_fmt = log_get_default_format();
288 LOG_TEST(log_test_syslog_nodebug);