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