1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
*
* Logging function tests for CONFIG_LOG_SYSLOG=y.
*
* Invoke the test with: ./u-boot -d arch/sandbox/dts/test.dtb
*/
/* Override CONFIG_LOG_MAX_LEVEL */
#define LOG_DEBUG
#include <common.h>
#include <dm/device.h>
#include <hexdump.h>
#include <test/log.h>
#include <test/test.h>
#include <test/suites.h>
#include <test/ut.h>
#include <asm/eth.h>
DECLARE_GLOBAL_DATA_PTR;
/**
* struct sb_log_env - private data for sandbox ethernet driver
*
* This structure is used for the private data of the sandbox ethernet
* driver.
*
* @expected: string expected to be written by the syslog driver
* @uts: unit test state
*/
struct sb_log_env {
const char *expected;
struct unit_test_state *uts;
};
/**
* sb_log_tx_handler() - transmit callback function
*
* This callback function is invoked when a network package is sent using the
* sandbox Ethernet driver. The private data of the driver holds a sb_log_env
* structure with the unit test state and the expected UDP payload.
*
* The following checks are executed:
*
* * the Ethernet packet indicates a IP broadcast message
* * the IP header is for a local UDP broadcast message to port 514
* * the UDP payload matches the expected string
*
* After testing the pointer to the expected string is set to NULL to signal
* that the callback function has been called.
*
* @dev: sandbox ethernet device
* @packet: Ethernet packet
* @len: length of Ethernet packet
* Return: 0 = success
*/
static int sb_log_tx_handler(struct udevice *dev, void *packet,
unsigned int len)
{
struct eth_sandbox_priv *priv = dev_get_priv(dev);
struct sb_log_env *env = priv->priv;
/* uts is updated by the ut_assert* macros */
struct unit_test_state *uts = env->uts;
char *buf = packet;
struct ethernet_hdr *eth_hdr = packet;
struct ip_udp_hdr *ip_udp_hdr;
/* Check Ethernet header */
ut_asserteq_mem(ð_hdr->et_dest, net_bcast_ethaddr, ARP_HLEN);
ut_asserteq(ntohs(eth_hdr->et_protlen), PROT_IP);
/* Check IP header */
buf += sizeof(struct ethernet_hdr);
ip_udp_hdr = (struct ip_udp_hdr *)buf;
ut_asserteq(ip_udp_hdr->ip_p, IPPROTO_UDP);
ut_asserteq(ip_udp_hdr->ip_dst.s_addr, 0xffffffff);
ut_asserteq(ntohs(ip_udp_hdr->udp_dst), 514);
ut_asserteq(UDP_HDR_SIZE + strlen(env->expected) + 1,
ntohs(ip_udp_hdr->udp_len));
/* Check payload */
buf += sizeof(struct ip_udp_hdr);
ut_asserteq_mem(env->expected, buf,
ntohs(ip_udp_hdr->udp_len) - UDP_HDR_SIZE);
/* Signal that the callback function has been executed */
env->expected = NULL;
return 0;
}
/**
* log_test_syslog_err() - test log_err() function
*
* @uts: unit test state
* Return: 0 = success
*/
static int log_test_syslog_err(struct unit_test_state *uts)
{
int old_log_level = gd->default_log_level;
struct sb_log_env env;
gd->log_fmt = LOGF_DEFAULT;
gd->default_log_level = LOGL_INFO;
env_set("ethact", "eth@10002000");
env_set("log_hostname", "sandbox");
env.expected = "<3>sandbox uboot: log_test_syslog_err() "
"testing log_err\n";
env.uts = uts;
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
/* Used by ut_assert macros in the tx_handler */
sandbox_eth_set_priv(0, &env);
log_err("testing %s\n", "log_err");
/* Check that the callback function was called */
sandbox_eth_set_tx_handler(0, NULL);
gd->default_log_level = old_log_level;
return 0;
}
LOG_TEST(log_test_syslog_err);
/**
* log_test_syslog_warning() - test log_warning() function
*
* @uts: unit test state
* Return: 0 = success
*/
static int log_test_syslog_warning(struct unit_test_state *uts)
{
int old_log_level = gd->default_log_level;
struct sb_log_env env;
gd->log_fmt = LOGF_DEFAULT;
gd->default_log_level = LOGL_INFO;
env_set("ethact", "eth@10002000");
env_set("log_hostname", "sandbox");
env.expected = "<4>sandbox uboot: log_test_syslog_warning() "
"testing log_warning\n";
env.uts = uts;
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
/* Used by ut_assert macros in the tx_handler */
sandbox_eth_set_priv(0, &env);
log_warning("testing %s\n", "log_warning");
sandbox_eth_set_tx_handler(0, NULL);
/* Check that the callback function was called */
ut_assertnull(env.expected);
gd->default_log_level = old_log_level;
return 0;
}
LOG_TEST(log_test_syslog_warning);
/**
* log_test_syslog_notice() - test log_notice() function
*
* @uts: unit test state
* Return: 0 = success
*/
static int log_test_syslog_notice(struct unit_test_state *uts)
{
int old_log_level = gd->default_log_level;
struct sb_log_env env;
gd->log_fmt = LOGF_DEFAULT;
gd->default_log_level = LOGL_INFO;
env_set("ethact", "eth@10002000");
env_set("log_hostname", "sandbox");
env.expected = "<5>sandbox uboot: log_test_syslog_notice() "
"testing log_notice\n";
env.uts = uts;
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
/* Used by ut_assert macros in the tx_handler */
sandbox_eth_set_priv(0, &env);
log_notice("testing %s\n", "log_notice");
sandbox_eth_set_tx_handler(0, NULL);
/* Check that the callback function was called */
ut_assertnull(env.expected);
gd->default_log_level = old_log_level;
return 0;
}
LOG_TEST(log_test_syslog_notice);
/**
* log_test_syslog_info() - test log_info() function
*
* @uts: unit test state
* Return: 0 = success
*/
static int log_test_syslog_info(struct unit_test_state *uts)
{
int old_log_level = gd->default_log_level;
struct sb_log_env env;
gd->log_fmt = LOGF_DEFAULT;
gd->default_log_level = LOGL_INFO;
env_set("ethact", "eth@10002000");
env_set("log_hostname", "sandbox");
env.expected = "<6>sandbox uboot: log_test_syslog_info() "
"testing log_info\n";
env.uts = uts;
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
/* Used by ut_assert macros in the tx_handler */
sandbox_eth_set_priv(0, &env);
log_info("testing %s\n", "log_info");
sandbox_eth_set_tx_handler(0, NULL);
/* Check that the callback function was called */
ut_assertnull(env.expected);
gd->default_log_level = old_log_level;
return 0;
}
LOG_TEST(log_test_syslog_info);
/**
* log_test_syslog_debug() - test log_debug() function
*
* @uts: unit test state
* Return: 0 = success
*/
static int log_test_syslog_debug(struct unit_test_state *uts)
{
int old_log_level = gd->default_log_level;
struct sb_log_env env;
gd->log_fmt = LOGF_DEFAULT;
gd->default_log_level = LOGL_DEBUG;
env_set("ethact", "eth@10002000");
env_set("log_hostname", "sandbox");
env.expected = "<7>sandbox uboot: log_test_syslog_debug() "
"testing log_debug\n";
env.uts = uts;
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
/* Used by ut_assert macros in the tx_handler */
sandbox_eth_set_priv(0, &env);
log_debug("testing %s\n", "log_debug");
sandbox_eth_set_tx_handler(0, NULL);
/* Check that the callback function was called */
ut_assertnull(env.expected);
gd->default_log_level = old_log_level;
return 0;
}
LOG_TEST(log_test_syslog_debug);
/**
* log_test_syslog_nodebug() - test logging level filter
*
* Verify that log_debug() does not lead to a log message if the logging level
* is set to LOGL_INFO.
*
* @uts: unit test state
* Return: 0 = success
*/
static int log_test_syslog_nodebug(struct unit_test_state *uts)
{
int old_log_level = gd->default_log_level;
struct sb_log_env env;
gd->log_fmt = LOGF_DEFAULT;
gd->default_log_level = LOGL_INFO;
env_set("ethact", "eth@10002000");
env_set("log_hostname", "sandbox");
env.expected = "<7>sandbox uboot: log_test_syslog_nodebug() "
"testing log_debug\n";
env.uts = uts;
sandbox_eth_set_tx_handler(0, sb_log_tx_handler);
/* Used by ut_assert macros in the tx_handler */
sandbox_eth_set_priv(0, &env);
log_debug("testing %s\n", "log_debug");
sandbox_eth_set_tx_handler(0, NULL);
/* Check that the callback function was not called */
ut_assertnonnull(env.expected);
gd->default_log_level = old_log_level;
return 0;
}
LOG_TEST(log_test_syslog_nodebug);
|