reset: add basic reset controller for pciesys
[platform/kernel/u-boot.git] / test / log / syslog_test.c
1 // 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
22 DECLARE_GLOBAL_DATA_PTR;
23
24 #define LOGF_TEST (BIT(LOGF_FUNC) | BIT(LOGF_MSG))
25
26 /**
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  */
35 struct 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  */
61 static 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 /**
97  * log_test_syslog_err() - test log_err() function
98  *
99  * @uts:        unit test state
100  * Return:      0 = success
101  */
102 static int log_test_syslog_err(struct unit_test_state *uts)
103 {
104         int old_log_level = gd->default_log_level;
105         struct sb_log_env env;
106
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() "
112                        "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;
121         gd->log_fmt = log_get_default_format();
122
123         return 0;
124 }
125 LOG_TEST(log_test_syslog_err);
126
127 /**
128  * log_test_syslog_warning() - test log_warning() function
129  *
130  * @uts:        unit test state
131  * Return:      0 = success
132  */
133 static int log_test_syslog_warning(struct unit_test_state *uts)
134 {
135         int old_log_level = gd->default_log_level;
136         struct sb_log_env env;
137
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";
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;
153         gd->log_fmt = log_get_default_format();
154
155         return 0;
156 }
157 LOG_TEST(log_test_syslog_warning);
158
159 /**
160  * log_test_syslog_notice() - test log_notice() function
161  *
162  * @uts:        unit test state
163  * Return:      0 = success
164  */
165 static int log_test_syslog_notice(struct unit_test_state *uts)
166 {
167         int old_log_level = gd->default_log_level;
168         struct sb_log_env env;
169
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";
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;
185         gd->log_fmt = log_get_default_format();
186
187         return 0;
188 }
189 LOG_TEST(log_test_syslog_notice);
190
191 /**
192  * log_test_syslog_info() - test log_info() function
193  *
194  * @uts:        unit test state
195  * Return:      0 = success
196  */
197 static int log_test_syslog_info(struct unit_test_state *uts)
198 {
199         int old_log_level = gd->default_log_level;
200         struct sb_log_env env;
201
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";
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;
217         gd->log_fmt = log_get_default_format();
218
219         return 0;
220 }
221 LOG_TEST(log_test_syslog_info);
222
223 /**
224  * log_test_syslog_debug() - test log_debug() function
225  *
226  * @uts:        unit test state
227  * Return:      0 = success
228  */
229 static int log_test_syslog_debug(struct unit_test_state *uts)
230 {
231         int old_log_level = gd->default_log_level;
232         struct sb_log_env env;
233
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";
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;
249         gd->log_fmt = log_get_default_format();
250
251         return 0;
252 }
253 LOG_TEST(log_test_syslog_debug);
254
255 /**
256  * log_test_syslog_nodebug() - test logging level filter
257  *
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  */
264 static int log_test_syslog_nodebug(struct unit_test_state *uts)
265 {
266         int old_log_level = gd->default_log_level;
267         struct sb_log_env env;
268
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";
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;
284         gd->log_fmt = log_get_default_format();
285
286         return 0;
287 }
288 LOG_TEST(log_test_syslog_nodebug);