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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Tests for setexpr command
*
* Copyright 2020 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <console.h>
#include <mapmem.h>
#include <dm/test.h>
#include <test/suites.h>
#include <test/ut.h>
#define BUF_SIZE 0x100
/* Declare a new mem test */
#define SETEXPR_TEST(_name, _flags) UNIT_TEST(_name, _flags, setexpr_test)
/* Test 'setexpr' command with simply setting integers */
static int setexpr_test_int(struct unit_test_state *uts)
{
u8 *buf;
buf = map_sysmem(0, BUF_SIZE);
memset(buf, '\xff', BUF_SIZE);
/* byte */
buf[0x0] = 0x12;
ut_assertok(run_command("setexpr.b fred 0", 0));
ut_asserteq_str("0", env_get("fred"));
ut_assertok(run_command("setexpr.b fred *0", 0));
ut_asserteq_str("12", env_get("fred"));
/* 16-bit */
*(short *)buf = 0x2345;
ut_assertok(run_command("setexpr.w fred 0", 0));
ut_asserteq_str("0", env_get("fred"));
ut_assertok(run_command("setexpr.w fred *0", 0));
ut_asserteq_str("2345", env_get("fred"));
/* 32-bit */
*(u32 *)buf = 0x3456789a;
ut_assertok(run_command("setexpr.l fred 0", 0));
ut_asserteq_str("0", env_get("fred"));
ut_assertok(run_command("setexpr.l fred *0", 0));
ut_asserteq_str("3456789a", env_get("fred"));
/* 64-bit */
*(u64 *)buf = 0x456789abcdef0123;
ut_assertok(run_command("setexpr.q fred 0", 0));
ut_asserteq_str("0", env_get("fred"));
ut_assertok(run_command("setexpr.q fred *0", 0));
ut_asserteq_str("456789abcdef0123", env_get("fred"));
/* default */
ut_assertok(run_command("setexpr fred 0", 0));
ut_asserteq_str("0", env_get("fred"));
ut_assertok(run_command("setexpr fred *0", 0));
ut_asserteq_str("cdef0123", env_get("fred"));
unmap_sysmem(buf);
return 0;
}
SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC);
/* Test 'setexpr' command with + operator */
static int setexpr_test_plus(struct unit_test_state *uts)
{
char *buf;
buf = map_sysmem(0, BUF_SIZE);
memset(buf, '\xff', BUF_SIZE);
/* byte */
buf[0x0] = 0x12;
buf[0x10] = 0x34;
ut_assertok(run_command("setexpr.b fred *0 + *10", 0));
ut_asserteq_str("46", env_get("fred"));
/* 16-bit */
*(short *)buf = 0x2345;
*(short *)(buf + 0x10) = 0xf012;
ut_assertok(run_command("setexpr.w fred *0 + *10", 0));
ut_asserteq_str("11357", env_get("fred"));
/* 32-bit */
*(u32 *)buf = 0x3456789a;
*(u32 *)(buf + 0x10) = 0xc3384235;
ut_assertok(run_command("setexpr.l fred *0 + *10", 0));
ut_asserteq_str("f78ebacf", env_get("fred"));
/* 64-bit */
*(u64 *)buf = 0x456789abcdef0123;
*(u64 *)(buf + 0x10) = 0x4987328372849283;
ut_assertok(run_command("setexpr.q fred *0 + *10", 0));
ut_asserteq_str("8eeebc2f407393a6", env_get("fred"));
/* default */
ut_assertok(run_command("setexpr fred *0 + *10", 0));
ut_asserteq_str("1407393a6", env_get("fred"));
unmap_sysmem(buf);
return 0;
}
SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC);
/* Test 'setexpr' command with other operators */
static int setexpr_test_oper(struct unit_test_state *uts)
{
char *buf;
buf = map_sysmem(0, BUF_SIZE);
memset(buf, '\xff', BUF_SIZE);
*(u32 *)buf = 0x1234;
*(u32 *)(buf + 0x10) = 0x560000;
/* Quote | to avoid confusing hush */
ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0));
ut_asserteq_str("561234", env_get("fred"));
*(u32 *)buf = 0x561200;
*(u32 *)(buf + 0x10) = 0x1234;
/* Quote & to avoid confusing hush */
ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0));
ut_asserteq_str("1200", env_get("fred"));
ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0));
ut_asserteq_str("560034", env_get("fred"));
ut_assertok(run_command("setexpr.l fred *0 - *10", 0));
ut_asserteq_str("55ffcc", env_get("fred"));
ut_assertok(run_command("setexpr.l fred *0 * *10", 0));
ut_asserteq_str("61ebfa800", env_get("fred"));
ut_assertok(run_command("setexpr.l fred *0 / *10", 0));
ut_asserteq_str("4ba", env_get("fred"));
ut_assertok(run_command("setexpr.l fred *0 % *10", 0));
ut_asserteq_str("838", env_get("fred"));
unmap_sysmem(buf);
return 0;
}
SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC);
/* Test 'setexpr' command with regex */
static int setexpr_test_regex(struct unit_test_state *uts)
{
char *buf, *val;
buf = map_sysmem(0, BUF_SIZE);
/* Single substitution */
ut_assertok(run_command("setenv fred 'this is a test'", 0));
ut_assertok(run_command("setexpr fred sub is us", 0));
val = env_get("fred");
ut_asserteq_str("thus is a test", val);
/* Global substitution */
ut_assertok(run_command("setenv fred 'this is a test'", 0));
ut_assertok(run_command("setexpr fred gsub is us", 0));
val = env_get("fred");
ut_asserteq_str("thus us a test", val);
/* Global substitution */
ut_assertok(run_command("setenv fred 'this is a test'", 0));
ut_assertok(run_command("setenv mary 'this is a test'", 0));
ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0));
val = env_get("fred");
ut_asserteq_str("thus us a test", val);
val = env_get("mary");
ut_asserteq_str("this is a test", val);
unmap_sysmem(buf);
return 0;
}
SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC);
/* Test 'setexpr' command with regex replacement that expands the string */
static int setexpr_test_regex_inc(struct unit_test_state *uts)
{
char *buf, *val;
buf = map_sysmem(0, BUF_SIZE);
ut_assertok(run_command("setenv fred 'this is a test'", 0));
ut_assertok(run_command("setexpr fred gsub is much_longer_string", 0));
val = env_get("fred");
ut_asserteq_str("thmuch_longer_string much_longer_string a test", val);
unmap_sysmem(buf);
return 0;
}
SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC);
/* Test setexpr_regex_sub() directly to check buffer usage */
static int setexpr_test_sub(struct unit_test_state *uts)
{
char *buf, *nbuf;
int i;
buf = map_sysmem(0, BUF_SIZE);
nbuf = map_sysmem(0x1000, BUF_SIZE);
/* Add a pattern so we can check the buffer limits */
memset(buf, '\xff', BUF_SIZE);
memset(nbuf, '\xff', BUF_SIZE);
for (i = BUF_SIZE; i < 0x1000; i++) {
buf[i] = i & 0xff;
nbuf[i] = i & 0xff;
}
strcpy(buf, "this is a test");
/*
* This is a regression test, since a bug was found in the use of
* memmove() in setexpr
*/
ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE, "is",
"us it is longer", true));
ut_asserteq_str("thus it is longer us it is longer a test", buf);
for (i = BUF_SIZE; i < 0x1000; i++) {
ut_assertf(buf[i] == (char)i,
"buf byte at %x should be %02x, got %02x)\n",
i, i & 0xff, (u8)buf[i]);
ut_assertf(nbuf[i] == (char)i,
"nbuf byte at %x should be %02x, got %02x)\n",
i, i & 0xff, (u8)nbuf[i]);
}
unmap_sysmem(buf);
return 0;
}
SETEXPR_TEST(setexpr_test_sub, UT_TESTF_CONSOLE_REC);
/* Test setexpr_regex_sub() with back references */
static int setexpr_test_backref(struct unit_test_state *uts)
{
char *buf, *nbuf;
int i;
buf = map_sysmem(0, BUF_SIZE);
nbuf = map_sysmem(0x1000, BUF_SIZE);
/* Add a pattern so we can check the buffer limits */
memset(buf, '\xff', BUF_SIZE);
memset(nbuf, '\xff', BUF_SIZE);
for (i = BUF_SIZE; i < 0x1000; i++) {
buf[i] = i & 0xff;
nbuf[i] = i & 0xff;
}
strcpy(buf, "this is surely a test is it? yes this is indeed a test");
/*
* This is a regression test, since a bug was found in the use of
* memmove() in setexpr
*/
ut_assertok(setexpr_regex_sub(buf, BUF_SIZE, nbuf, BUF_SIZE,
"(this) (is) (surely|indeed)",
"us \\1 \\2 \\3!", true));
ut_asserteq_str("us this is surely! a test is it? yes us this is indeed! a test",
buf);
/* The following checks fail at present due to a bug in setexpr */
return 0;
for (i = BUF_SIZE; i < 0x1000; i++) {
ut_assertf(buf[i] == (char)i,
"buf byte at %x should be %02x, got %02x)\n",
i, i & 0xff, (u8)buf[i]);
ut_assertf(nbuf[i] == (char)i,
"nbuf byte at %x should be %02x, got %02x)\n",
i, i & 0xff, (u8)nbuf[i]);
}
unmap_sysmem(buf);
return 0;
}
SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC);
int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{
struct unit_test *tests = ll_entry_start(struct unit_test,
setexpr_test);
const int n_ents = ll_entry_count(struct unit_test, setexpr_test);
return cmd_ut_category("cmd_setexpr", "cmd_mem_", tests, n_ents, argc,
argv);
}
|