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;
25 * struct sb_log_env - private data for sandbox ethernet driver
27 * This structure is used for the private data of the sandbox ethernet
30 * @expected: string expected to be written by the syslog driver
31 * @uts: unit test state
35 struct unit_test_state *uts;
39 * sb_log_tx_handler() - transmit callback function
41 * This callback function is invoked when a network package is sent using the
42 * sandbox Ethernet driver. The private data of the driver holds a sb_log_env
43 * structure with the unit test state and the expected UDP payload.
45 * The following checks are executed:
47 * * the Ethernet packet indicates a IP broadcast message
48 * * the IP header is for a local UDP broadcast message to port 514
49 * * the UDP payload matches the expected string
51 * After testing the pointer to the expected string is set to NULL to signal
52 * that the callback function has been called.
54 * @dev: sandbox ethernet device
55 * @packet: Ethernet packet
56 * @len: length of Ethernet packet
59 static int sb_log_tx_handler(struct udevice *dev, void *packet,
62 struct eth_sandbox_priv *priv = dev_get_priv(dev);
63 struct sb_log_env *env = priv->priv;
64 /* uts is updated by the ut_assert* macros */
65 struct unit_test_state *uts = env->uts;
67 struct ethernet_hdr *eth_hdr = packet;
68 struct ip_udp_hdr *ip_udp_hdr;
70 /* Check Ethernet header */
71 ut_asserteq_mem(ð_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
72 ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
75 buf += sizeof(struct ethernet_hdr);
76 ip_udp_hdr = (struct ip_udp_hdr *)buf;
77 ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
78 ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
79 ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
80 ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
81 ntohs(ip_udp_hdr->udp_len));
84 buf += sizeof(struct ip_udp_hdr);
85 ut_asserteq_mem(env->expected, buf,
86 ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
88 /* Signal that the callback function has been executed */
95 * syslog_test_log_err() - test log_err() function
97 * @uts: unit test state
100 static int syslog_test_log_err(struct unit_test_state *uts)
102 int old_log_level = gd->default_log_level;
103 struct sb_log_env env;
105 gd->log_fmt = LOGF_DEFAULT;
106 gd->default_log_level = LOGL_INFO;
107 env_set("ethact", "eth@10002000");
108 env_set("log_hostname", "sandbox");
109 env.expected = "<3>sandbox uboot: syslog_test_log_err() "
112 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
113 /* Used by ut_assert macros in the tx_handler */
114 sandbox_eth_set_priv(0, &env);
115 log_err("testing %s\n", "log_err");
116 /* Check that the callback function was called */
117 sandbox_eth_set_tx_handler(0, NULL);
118 gd->default_log_level = old_log_level;
122 LOG_TEST(syslog_test_log_err);
125 * syslog_test_log_warning() - test log_warning() function
127 * @uts: unit test state
128 * Return: 0 = success
130 static int syslog_test_log_warning(struct unit_test_state *uts)
132 int old_log_level = gd->default_log_level;
133 struct sb_log_env env;
135 gd->log_fmt = LOGF_DEFAULT;
136 gd->default_log_level = LOGL_INFO;
137 env_set("ethact", "eth@10002000");
138 env_set("log_hostname", "sandbox");
139 env.expected = "<4>sandbox uboot: syslog_test_log_warning() "
140 "testing log_warning\n";
142 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
143 /* Used by ut_assert macros in the tx_handler */
144 sandbox_eth_set_priv(0, &env);
145 log_warning("testing %s\n", "log_warning");
146 sandbox_eth_set_tx_handler(0, NULL);
147 /* Check that the callback function was called */
148 ut_assertnull(env.expected);
149 gd->default_log_level = old_log_level;
153 LOG_TEST(syslog_test_log_warning);
156 * syslog_test_log_notice() - test log_notice() function
158 * @uts: unit test state
159 * Return: 0 = success
161 static int syslog_test_log_notice(struct unit_test_state *uts)
163 int old_log_level = gd->default_log_level;
164 struct sb_log_env env;
166 gd->log_fmt = LOGF_DEFAULT;
167 gd->default_log_level = LOGL_INFO;
168 env_set("ethact", "eth@10002000");
169 env_set("log_hostname", "sandbox");
170 env.expected = "<5>sandbox uboot: syslog_test_log_notice() "
171 "testing log_notice\n";
173 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
174 /* Used by ut_assert macros in the tx_handler */
175 sandbox_eth_set_priv(0, &env);
176 log_notice("testing %s\n", "log_notice");
177 sandbox_eth_set_tx_handler(0, NULL);
178 /* Check that the callback function was called */
179 ut_assertnull(env.expected);
180 gd->default_log_level = old_log_level;
184 LOG_TEST(syslog_test_log_notice);
187 * syslog_test_log_info() - test log_info() function
189 * @uts: unit test state
190 * Return: 0 = success
192 static int syslog_test_log_info(struct unit_test_state *uts)
194 int old_log_level = gd->default_log_level;
195 struct sb_log_env env;
197 gd->log_fmt = LOGF_DEFAULT;
198 gd->default_log_level = LOGL_INFO;
199 env_set("ethact", "eth@10002000");
200 env_set("log_hostname", "sandbox");
201 env.expected = "<6>sandbox uboot: syslog_test_log_info() "
202 "testing log_info\n";
204 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
205 /* Used by ut_assert macros in the tx_handler */
206 sandbox_eth_set_priv(0, &env);
207 log_info("testing %s\n", "log_info");
208 sandbox_eth_set_tx_handler(0, NULL);
209 /* Check that the callback function was called */
210 ut_assertnull(env.expected);
211 gd->default_log_level = old_log_level;
215 LOG_TEST(syslog_test_log_info);
218 * syslog_test_log_debug() - test log_debug() function
220 * @uts: unit test state
221 * Return: 0 = success
223 static int syslog_test_log_debug(struct unit_test_state *uts)
225 int old_log_level = gd->default_log_level;
226 struct sb_log_env env;
228 gd->log_fmt = LOGF_DEFAULT;
229 gd->default_log_level = LOGL_DEBUG;
230 env_set("ethact", "eth@10002000");
231 env_set("log_hostname", "sandbox");
232 env.expected = "<7>sandbox uboot: syslog_test_log_debug() "
233 "testing log_debug\n";
235 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
236 /* Used by ut_assert macros in the tx_handler */
237 sandbox_eth_set_priv(0, &env);
238 log_debug("testing %s\n", "log_debug");
239 sandbox_eth_set_tx_handler(0, NULL);
240 /* Check that the callback function was called */
241 ut_assertnull(env.expected);
242 gd->default_log_level = old_log_level;
246 LOG_TEST(syslog_test_log_debug);
249 * syslog_test_log_nodebug() - test logging level filter
251 * Verify that log_debug() does not lead to a log message if the logging level
252 * is set to LOGL_INFO.
254 * @uts: unit test state
255 * Return: 0 = success
257 static int syslog_test_log_nodebug(struct unit_test_state *uts)
259 int old_log_level = gd->default_log_level;
260 struct sb_log_env env;
262 gd->log_fmt = LOGF_DEFAULT;
263 gd->default_log_level = LOGL_INFO;
264 env_set("ethact", "eth@10002000");
265 env_set("log_hostname", "sandbox");
266 env.expected = "<7>sandbox uboot: syslog_test_log_nodebug() "
267 "testing log_debug\n";
269 sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
270 /* Used by ut_assert macros in the tx_handler */
271 sandbox_eth_set_priv(0, &env);
272 log_debug("testing %s\n", "log_debug");
273 sandbox_eth_set_tx_handler(0, NULL);
274 /* Check that the callback function was not called */
275 ut_assertnonnull(env.expected);
276 gd->default_log_level = old_log_level;
280 LOG_TEST(syslog_test_log_nodebug);