blob: a058d8f56959317cfb92395f65c2ce5121d399a1 [file] [log] [blame]
Heinrich Schuchardt9a3a7f12020-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>
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>
Sean Andersonc848c8a2020-10-13 15:20:52 -040021#include "syslog_test.h"
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +010022
23DECLARE_GLOBAL_DATA_PTR;
24
Simon Glass9c83cac2020-09-12 11:13:34 -060025int sb_log_tx_handler(struct udevice *dev, void *packet, unsigned int len)
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +010026{
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
Simon Glasse2cd0fa2020-09-12 12:28:50 -060059int 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
66int 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
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +010073/**
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +020074 * log_test_syslog_err() - test log_err() function
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +010075 *
76 * @uts: unit test state
77 * Return: 0 = success
78 */
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +020079static int log_test_syslog_err(struct unit_test_state *uts)
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +010080{
81 int old_log_level = gd->default_log_level;
82 struct sb_log_env env;
83
Simon Glasse2cd0fa2020-09-12 12:28:50 -060084 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +020085 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +010086 gd->default_log_level = LOGL_INFO;
87 env_set("ethact", "eth@10002000");
88 env_set("log_hostname", "sandbox");
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +020089 env.expected = "<3>sandbox uboot: log_test_syslog_err() "
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +010090 "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;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +020099 gd->log_fmt = log_get_default_format();
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600100 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100101
102 return 0;
103}
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200104LOG_TEST(log_test_syslog_err);
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100105
106/**
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200107 * log_test_syslog_warning() - test log_warning() function
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100108 *
109 * @uts: unit test state
110 * Return: 0 = success
111 */
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200112static int log_test_syslog_warning(struct unit_test_state *uts)
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100113{
114 int old_log_level = gd->default_log_level;
115 struct sb_log_env env;
116
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600117 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200118 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100119 gd->default_log_level = LOGL_INFO;
120 env_set("ethact", "eth@10002000");
121 env_set("log_hostname", "sandbox");
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200122 env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100123 "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;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200133 gd->log_fmt = log_get_default_format();
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600134 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100135
136 return 0;
137}
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200138LOG_TEST(log_test_syslog_warning);
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100139
140/**
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200141 * log_test_syslog_notice() - test log_notice() function
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100142 *
143 * @uts: unit test state
144 * Return: 0 = success
145 */
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200146static int log_test_syslog_notice(struct unit_test_state *uts)
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100147{
148 int old_log_level = gd->default_log_level;
149 struct sb_log_env env;
150
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600151 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200152 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100153 gd->default_log_level = LOGL_INFO;
154 env_set("ethact", "eth@10002000");
155 env_set("log_hostname", "sandbox");
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200156 env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100157 "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;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200167 gd->log_fmt = log_get_default_format();
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600168 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100169
170 return 0;
171}
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200172LOG_TEST(log_test_syslog_notice);
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100173
174/**
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200175 * log_test_syslog_info() - test log_info() function
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100176 *
177 * @uts: unit test state
178 * Return: 0 = success
179 */
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200180static int log_test_syslog_info(struct unit_test_state *uts)
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100181{
182 int old_log_level = gd->default_log_level;
183 struct sb_log_env env;
184
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600185 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200186 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100187 gd->default_log_level = LOGL_INFO;
188 env_set("ethact", "eth@10002000");
189 env_set("log_hostname", "sandbox");
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200190 env.expected = "<6>sandbox uboot: log_test_syslog_info() "
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100191 "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;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200201 gd->log_fmt = log_get_default_format();
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600202 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100203
204 return 0;
205}
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200206LOG_TEST(log_test_syslog_info);
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100207
208/**
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200209 * log_test_syslog_debug() - test log_debug() function
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100210 *
211 * @uts: unit test state
212 * Return: 0 = success
213 */
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200214static int log_test_syslog_debug(struct unit_test_state *uts)
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100215{
216 int old_log_level = gd->default_log_level;
217 struct sb_log_env env;
218
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600219 ut_assertok(syslog_test_setup(uts));
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200220 gd->log_fmt = LOGF_TEST;
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100221 gd->default_log_level = LOGL_DEBUG;
222 env_set("ethact", "eth@10002000");
223 env_set("log_hostname", "sandbox");
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200224 env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100225 "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;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200235 gd->log_fmt = log_get_default_format();
Simon Glasse2cd0fa2020-09-12 12:28:50 -0600236 ut_assertok(syslog_test_finish(uts));
Heinrich Schuchardt9a3a7f12020-02-26 21:48:19 +0100237
238 return 0;
239}
Heinrich Schuchardt8d9c4c22020-05-06 18:26:08 +0200240LOG_TEST(log_test_syslog_debug);