blob: 120a8b2537b9ae8d0389de48206552c90dfabf59 [file] [log] [blame]
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
4 *
5 * Logging function tests for CONFIG_LOG_SYSLOG=y.
6 *
7 * Invoke the test with: ./u-boot -d arch/sandbox/dts/test.dtb
8 */
9
10/* Override CONFIG_LOG_MAX_LEVEL */
11#define LOG_DEBUG
12
13#include <common.h>
14#include <dm/device.h>
15#include <hexdump.h>
16#include <test/log.h>
17#include <test/test.h>
18#include <test/suites.h>
19#include <test/ut.h>
20#include <asm/eth.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +020024#define LOGF_TEST (BIT(LOGF_FUNC) | BIT(LOGF_MSG))
25
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010026/**
27 * struct sb_log_env - private data for sandbox ethernet driver
28 *
29 * This structure is used for the private data of the sandbox ethernet
30 * driver.
31 *
32 * @expected: string expected to be written by the syslog driver
33 * @uts: unit test state
34 */
35struct sb_log_env {
36 const char *expected;
37 struct unit_test_state *uts;
38};
39
40/**
41 * sb_log_tx_handler() - transmit callback function
42 *
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.
46 *
47 * The following checks are executed:
48 *
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
52 *
53 * After testing the pointer to the expected string is set to NULL to signal
54 * that the callback function has been called.
55 *
56 * @dev: sandbox ethernet device
57 * @packet: Ethernet packet
58 * @len: length of Ethernet packet
59 * Return: 0 = success
60 */
61static int sb_log_tx_handler(struct udevice *dev, void *packet,
62 unsigned int len)
63{
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;
68 char *buf = packet;
69 struct ethernet_hdr *eth_hdr = packet;
70 struct ip_udp_hdr *ip_udp_hdr;
71
72 /* Check Ethernet header */
73 ut_asserteq_mem(&eth_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
74 ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
75
76 /* Check IP header */
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));
84
85 /* Check payload */
86 buf += sizeof(struct ip_udp_hdr);
87 ut_asserteq_mem(env->expected, buf,
88 ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
89
90 /* Signal that the callback function has been executed */
91 env->expected = NULL;
92
93 return 0;
94}
95
96/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +020097 * log_test_syslog_err() - test log_err() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010098 *
99 * @uts: unit test state
100 * Return: 0 = success
101 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200102static int log_test_syslog_err(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100103{
104 int old_log_level = gd->default_log_level;
105 struct sb_log_env env;
106
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200107 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100108 gd->default_log_level = LOGL_INFO;
109 env_set("ethact", "eth@10002000");
110 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200111 env.expected = "<3>sandbox uboot: log_test_syslog_err() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100112 "testing log_err\n";
113 env.uts = uts;
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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200121 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100122
123 return 0;
124}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200125LOG_TEST(log_test_syslog_err);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100126
127/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200128 * log_test_syslog_warning() - test log_warning() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100129 *
130 * @uts: unit test state
131 * Return: 0 = success
132 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200133static int log_test_syslog_warning(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100134{
135 int old_log_level = gd->default_log_level;
136 struct sb_log_env env;
137
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200138 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100139 gd->default_log_level = LOGL_INFO;
140 env_set("ethact", "eth@10002000");
141 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200142 env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100143 "testing log_warning\n";
144 env.uts = uts;
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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200153 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100154
155 return 0;
156}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200157LOG_TEST(log_test_syslog_warning);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100158
159/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200160 * log_test_syslog_notice() - test log_notice() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100161 *
162 * @uts: unit test state
163 * Return: 0 = success
164 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200165static int log_test_syslog_notice(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100166{
167 int old_log_level = gd->default_log_level;
168 struct sb_log_env env;
169
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200170 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100171 gd->default_log_level = LOGL_INFO;
172 env_set("ethact", "eth@10002000");
173 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200174 env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100175 "testing log_notice\n";
176 env.uts = uts;
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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200185 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100186
187 return 0;
188}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200189LOG_TEST(log_test_syslog_notice);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100190
191/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200192 * log_test_syslog_info() - test log_info() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100193 *
194 * @uts: unit test state
195 * Return: 0 = success
196 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200197static int log_test_syslog_info(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100198{
199 int old_log_level = gd->default_log_level;
200 struct sb_log_env env;
201
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200202 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100203 gd->default_log_level = LOGL_INFO;
204 env_set("ethact", "eth@10002000");
205 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200206 env.expected = "<6>sandbox uboot: log_test_syslog_info() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100207 "testing log_info\n";
208 env.uts = uts;
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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200217 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100218
219 return 0;
220}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200221LOG_TEST(log_test_syslog_info);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100222
223/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200224 * log_test_syslog_debug() - test log_debug() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100225 *
226 * @uts: unit test state
227 * Return: 0 = success
228 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200229static int log_test_syslog_debug(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100230{
231 int old_log_level = gd->default_log_level;
232 struct sb_log_env env;
233
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200234 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100235 gd->default_log_level = LOGL_DEBUG;
236 env_set("ethact", "eth@10002000");
237 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200238 env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100239 "testing log_debug\n";
240 env.uts = uts;
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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200249 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100250
251 return 0;
252}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200253LOG_TEST(log_test_syslog_debug);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100254
255/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200256 * log_test_syslog_nodebug() - test logging level filter
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100257 *
258 * Verify that log_debug() does not lead to a log message if the logging level
259 * is set to LOGL_INFO.
260 *
261 * @uts: unit test state
262 * Return: 0 = success
263 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200264static int log_test_syslog_nodebug(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100265{
266 int old_log_level = gd->default_log_level;
267 struct sb_log_env env;
268
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200269 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100270 gd->default_log_level = LOGL_INFO;
271 env_set("ethact", "eth@10002000");
272 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200273 env.expected = "<7>sandbox uboot: log_test_syslog_nodebug() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100274 "testing log_debug\n";
275 env.uts = uts;
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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200284 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100285
286 return 0;
287}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200288LOG_TEST(log_test_syslog_nodebug);