blob: 4db649db82267941216708e584c1e4a26b663023 [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>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010015#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>
Sean Anderson3d19a7e2020-10-13 15:20:52 -040022#include "syslog_test.h"
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010023
24DECLARE_GLOBAL_DATA_PTR;
25
Simon Glass52d3df72020-09-12 11:13:34 -060026int sb_log_tx_handler(struct udevice *dev, void *packet, unsigned int len)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010027{
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
Simon Glassc7f5b852020-09-12 12:28:50 -060060int 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
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010074/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +020075 * log_test_syslog_err() - test log_err() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010076 *
77 * @uts: unit test state
78 * Return: 0 = success
79 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +020080static int log_test_syslog_err(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010081{
82 int old_log_level = gd->default_log_level;
83 struct sb_log_env env;
84
Simon Glassc7f5b852020-09-12 12:28:50 -060085 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +020086 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010087 gd->default_log_level = LOGL_INFO;
88 env_set("ethact", "eth@10002000");
89 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +020090 env.expected = "<3>sandbox uboot: log_test_syslog_err() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +010091 "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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200100 gd->log_fmt = log_get_default_format();
Simon Glassc7f5b852020-09-12 12:28:50 -0600101 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100102
103 return 0;
104}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200105LOG_TEST(log_test_syslog_err);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100106
107/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200108 * log_test_syslog_warning() - test log_warning() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100109 *
110 * @uts: unit test state
111 * Return: 0 = success
112 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200113static int log_test_syslog_warning(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100114{
115 int old_log_level = gd->default_log_level;
116 struct sb_log_env env;
117
Simon Glassc7f5b852020-09-12 12:28:50 -0600118 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200119 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100120 gd->default_log_level = LOGL_INFO;
121 env_set("ethact", "eth@10002000");
122 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200123 env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100124 "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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200134 gd->log_fmt = log_get_default_format();
Simon Glassc7f5b852020-09-12 12:28:50 -0600135 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100136
137 return 0;
138}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200139LOG_TEST(log_test_syslog_warning);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100140
141/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200142 * log_test_syslog_notice() - test log_notice() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100143 *
144 * @uts: unit test state
145 * Return: 0 = success
146 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200147static int log_test_syslog_notice(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100148{
149 int old_log_level = gd->default_log_level;
150 struct sb_log_env env;
151
Simon Glassc7f5b852020-09-12 12:28:50 -0600152 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200153 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100154 gd->default_log_level = LOGL_INFO;
155 env_set("ethact", "eth@10002000");
156 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200157 env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100158 "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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200168 gd->log_fmt = log_get_default_format();
Simon Glassc7f5b852020-09-12 12:28:50 -0600169 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100170
171 return 0;
172}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200173LOG_TEST(log_test_syslog_notice);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100174
175/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200176 * log_test_syslog_info() - test log_info() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100177 *
178 * @uts: unit test state
179 * Return: 0 = success
180 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200181static int log_test_syslog_info(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100182{
183 int old_log_level = gd->default_log_level;
184 struct sb_log_env env;
185
Simon Glassc7f5b852020-09-12 12:28:50 -0600186 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200187 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100188 gd->default_log_level = LOGL_INFO;
189 env_set("ethact", "eth@10002000");
190 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200191 env.expected = "<6>sandbox uboot: log_test_syslog_info() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100192 "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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200202 gd->log_fmt = log_get_default_format();
Simon Glassc7f5b852020-09-12 12:28:50 -0600203 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100204
205 return 0;
206}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200207LOG_TEST(log_test_syslog_info);
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100208
209/**
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200210 * log_test_syslog_debug() - test log_debug() function
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100211 *
212 * @uts: unit test state
213 * Return: 0 = success
214 */
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200215static int log_test_syslog_debug(struct unit_test_state *uts)
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100216{
217 int old_log_level = gd->default_log_level;
218 struct sb_log_env env;
219
Simon Glassc7f5b852020-09-12 12:28:50 -0600220 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200221 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100222 gd->default_log_level = LOGL_DEBUG;
223 env_set("ethact", "eth@10002000");
224 env_set("log_hostname", "sandbox");
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200225 env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100226 "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;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200236 gd->log_fmt = log_get_default_format();
Simon Glassc7f5b852020-09-12 12:28:50 -0600237 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt7b912ed2020-02-26 21:48:19 +0100238
239 return 0;
240}
Heinrich Schuchardtbe51c3c2020-05-06 18:26:08 +0200241LOG_TEST(log_test_syslog_debug);