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 <asm/global_data.h>
14 #include <dm/device.h>
15 #include <hexdump.h>
16 #include <test/log.h>
17 #include <test/test.h>
18 #include <test/ut.h>
19 #include <asm/eth.h>
20 #include "syslog_test.h"
21 
22 DECLARE_GLOBAL_DATA_PTR;
23 
sb_log_tx_handler(struct udevice * dev,void * packet,unsigned int len)24 int sb_log_tx_handler(struct udevice *dev, void *packet, unsigned int len)
25 {
26 	struct eth_sandbox_priv *priv = dev_get_priv(dev);
27 	struct sb_log_env *env = priv->priv;
28 	/* uts is updated by the ut_assert* macros */
29 	struct unit_test_state *uts = env->uts;
30 	char *buf = packet;
31 	struct ethernet_hdr *eth_hdr = packet;
32 	struct ip_udp_hdr *ip_udp_hdr;
33 
34 	/* Check Ethernet header */
35 	ut_asserteq_mem(&eth_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
36 	ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
37 
38 	/* Check IP header */
39 	buf += sizeof(struct ethernet_hdr);
40 	ip_udp_hdr = (struct ip_udp_hdr *)buf;
41 	ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
42 	ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
43 	ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
44 	ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
45 		    ntohs(ip_udp_hdr->udp_len));
46 
47 	/* Check payload */
48 	buf += sizeof(struct ip_udp_hdr);
49 	ut_asserteq_mem(env->expected, buf,
50 			ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
51 
52 	/* Signal that the callback function has been executed */
53 	env->expected = NULL;
54 
55 	return 0;
56 }
57 
syslog_test_setup(struct unit_test_state * uts)58 int syslog_test_setup(struct unit_test_state *uts)
59 {
60 	ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), true));
61 
62 	return 0;
63 }
64 
syslog_test_finish(struct unit_test_state * uts)65 int syslog_test_finish(struct unit_test_state *uts)
66 {
67 	ut_assertok(log_device_set_enable(LOG_GET_DRIVER(syslog), false));
68 
69 	return 0;
70 }
71 
72 /**
73  * log_test_syslog_err() - test log_err() function
74  *
75  * @uts:	unit test state
76  * Return:	0 = success
77  */
log_test_syslog_err(struct unit_test_state * uts)78 static int log_test_syslog_err(struct unit_test_state *uts)
79 {
80 	int old_log_level = gd->default_log_level;
81 	struct sb_log_env env;
82 
83 	ut_assertok(syslog_test_setup(uts));
84 	gd->log_fmt = LOGF_TEST;
85 	gd->default_log_level = LOGL_INFO;
86 	env_set("ethact", "eth@10002000");
87 	env_set("log_hostname", "sandbox");
88 	env.expected = "<3>sandbox uboot: log_test_syslog_err() "
89 		       "testing log_err\n";
90 	env.uts = uts;
91 	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
92 	/* Used by ut_assert macros in the tx_handler */
93 	sandbox_eth_set_priv(0, &env);
94 	log_err("testing %s\n", "log_err");
95 	/* Check that the callback function was called */
96 	sandbox_eth_set_tx_handler(0, NULL);
97 	gd->default_log_level = old_log_level;
98 	gd->log_fmt = log_get_default_format();
99 	ut_assertok(syslog_test_finish(uts));
100 
101 	return 0;
102 }
103 LOG_TEST(log_test_syslog_err);
104 
105 /**
106  * log_test_syslog_warning() - test log_warning() function
107  *
108  * @uts:	unit test state
109  * Return:	0 = success
110  */
log_test_syslog_warning(struct unit_test_state * uts)111 static int log_test_syslog_warning(struct unit_test_state *uts)
112 {
113 	int old_log_level = gd->default_log_level;
114 	struct sb_log_env env;
115 
116 	ut_assertok(syslog_test_setup(uts));
117 	gd->log_fmt = LOGF_TEST;
118 	gd->default_log_level = LOGL_INFO;
119 	env_set("ethact", "eth@10002000");
120 	env_set("log_hostname", "sandbox");
121 	env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
122 		       "testing log_warning\n";
123 	env.uts = uts;
124 	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
125 	/* Used by ut_assert macros in the tx_handler */
126 	sandbox_eth_set_priv(0, &env);
127 	log_warning("testing %s\n", "log_warning");
128 	sandbox_eth_set_tx_handler(0, NULL);
129 	/* Check that the callback function was called */
130 	ut_assertnull(env.expected);
131 	gd->default_log_level = old_log_level;
132 	gd->log_fmt = log_get_default_format();
133 	ut_assertok(syslog_test_finish(uts));
134 
135 	return 0;
136 }
137 LOG_TEST(log_test_syslog_warning);
138 
139 /**
140  * log_test_syslog_notice() - test log_notice() function
141  *
142  * @uts:	unit test state
143  * Return:	0 = success
144  */
log_test_syslog_notice(struct unit_test_state * uts)145 static int log_test_syslog_notice(struct unit_test_state *uts)
146 {
147 	int old_log_level = gd->default_log_level;
148 	struct sb_log_env env;
149 
150 	ut_assertok(syslog_test_setup(uts));
151 	gd->log_fmt = LOGF_TEST;
152 	gd->default_log_level = LOGL_INFO;
153 	env_set("ethact", "eth@10002000");
154 	env_set("log_hostname", "sandbox");
155 	env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
156 		       "testing log_notice\n";
157 	env.uts = uts;
158 	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
159 	/* Used by ut_assert macros in the tx_handler */
160 	sandbox_eth_set_priv(0, &env);
161 	log_notice("testing %s\n", "log_notice");
162 	sandbox_eth_set_tx_handler(0, NULL);
163 	/* Check that the callback function was called */
164 	ut_assertnull(env.expected);
165 	gd->default_log_level = old_log_level;
166 	gd->log_fmt = log_get_default_format();
167 	ut_assertok(syslog_test_finish(uts));
168 
169 	return 0;
170 }
171 LOG_TEST(log_test_syslog_notice);
172 
173 /**
174  * log_test_syslog_info() - test log_info() function
175  *
176  * @uts:	unit test state
177  * Return:	0 = success
178  */
log_test_syslog_info(struct unit_test_state * uts)179 static int log_test_syslog_info(struct unit_test_state *uts)
180 {
181 	int old_log_level = gd->default_log_level;
182 	struct sb_log_env env;
183 
184 	ut_assertok(syslog_test_setup(uts));
185 	gd->log_fmt = LOGF_TEST;
186 	gd->default_log_level = LOGL_INFO;
187 	env_set("ethact", "eth@10002000");
188 	env_set("log_hostname", "sandbox");
189 	env.expected = "<6>sandbox uboot: log_test_syslog_info() "
190 		       "testing log_info\n";
191 	env.uts = uts;
192 	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
193 	/* Used by ut_assert macros in the tx_handler */
194 	sandbox_eth_set_priv(0, &env);
195 	log_info("testing %s\n", "log_info");
196 	sandbox_eth_set_tx_handler(0, NULL);
197 	/* Check that the callback function was called */
198 	ut_assertnull(env.expected);
199 	gd->default_log_level = old_log_level;
200 	gd->log_fmt = log_get_default_format();
201 	ut_assertok(syslog_test_finish(uts));
202 
203 	return 0;
204 }
205 LOG_TEST(log_test_syslog_info);
206 
207 /**
208  * log_test_syslog_debug() - test log_debug() function
209  *
210  * @uts:	unit test state
211  * Return:	0 = success
212  */
log_test_syslog_debug(struct unit_test_state * uts)213 static int log_test_syslog_debug(struct unit_test_state *uts)
214 {
215 	int old_log_level = gd->default_log_level;
216 	struct sb_log_env env;
217 
218 	ut_assertok(syslog_test_setup(uts));
219 	gd->log_fmt = LOGF_TEST;
220 	gd->default_log_level = LOGL_DEBUG;
221 	env_set("ethact", "eth@10002000");
222 	env_set("log_hostname", "sandbox");
223 	env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
224 		       "testing log_debug\n";
225 	env.uts = uts;
226 	sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
227 	/* Used by ut_assert macros in the tx_handler */
228 	sandbox_eth_set_priv(0, &env);
229 	log_debug("testing %s\n", "log_debug");
230 	sandbox_eth_set_tx_handler(0, NULL);
231 	/* Check that the callback function was called */
232 	ut_assertnull(env.expected);
233 	gd->default_log_level = old_log_level;
234 	gd->log_fmt = log_get_default_format();
235 	ut_assertok(syslog_test_finish(uts));
236 
237 	return 0;
238 }
239 LOG_TEST(log_test_syslog_debug);
240