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