Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/findstr/tests/findstr.c
5970 views
1
/*
2
* Copyright 2023 Zhiyi Zhang for CodeWeavers
3
*
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
8
*
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
13
*
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17
*/
18
19
#include <windows.h>
20
#include "wine/test.h"
21
22
#define MAX_BUFFER 65536
23
24
static char stdout_buffer[MAX_BUFFER], stderr_buffer[MAX_BUFFER];
25
static DWORD stdout_size, stderr_size;
26
27
static void read_all_from_handle(HANDLE handle, char *buffer, DWORD *size)
28
{
29
char bytes[4096];
30
DWORD bytes_read;
31
32
memset(buffer, 0, MAX_BUFFER);
33
*size = 0;
34
for (;;)
35
{
36
BOOL success = ReadFile(handle, bytes, sizeof(bytes), &bytes_read, NULL);
37
if (!success || !bytes_read)
38
break;
39
if (*size + bytes_read > MAX_BUFFER)
40
{
41
ok(FALSE, "Insufficient buffer.\n");
42
break;
43
}
44
memcpy(buffer + *size, bytes, bytes_read);
45
*size += bytes_read;
46
}
47
}
48
49
static void write_to_handle(HANDLE handle, const char *str, int len)
50
{
51
DWORD dummy;
52
53
WriteFile(handle, str, len, &dummy, NULL);
54
}
55
56
#define run_find_stdin(a, b, c) _run_find_stdin(__FILE__, __LINE__, a, b, c)
57
static void _run_find_stdin(const char *file, int line, const char *commandline, const char *input,
58
int exitcode_expected)
59
{
60
HANDLE parent_stdin_write, parent_stdout_read, parent_stderr_read;
61
HANDLE child_stdin_read, child_stdout_write, child_stderr_write;
62
SECURITY_ATTRIBUTES security_attributes = {0};
63
PROCESS_INFORMATION process_info = {0};
64
STARTUPINFOA startup_info = {0};
65
char cmd[4096];
66
DWORD exitcode;
67
68
security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
69
security_attributes.bInheritHandle = TRUE;
70
71
CreatePipe(&child_stdin_read, &parent_stdin_write, &security_attributes, 0);
72
CreatePipe(&parent_stdout_read, &child_stdout_write, &security_attributes, 0);
73
CreatePipe(&parent_stderr_read, &child_stderr_write, &security_attributes, 0);
74
75
SetHandleInformation(parent_stdin_write, HANDLE_FLAG_INHERIT, 0);
76
SetHandleInformation(parent_stdout_read, HANDLE_FLAG_INHERIT, 0);
77
SetHandleInformation(parent_stderr_read, HANDLE_FLAG_INHERIT, 0);
78
79
startup_info.cb = sizeof(STARTUPINFOA);
80
startup_info.hStdInput = child_stdin_read;
81
startup_info.hStdOutput = child_stdout_write;
82
startup_info.hStdError = child_stderr_write;
83
startup_info.dwFlags |= STARTF_USESTDHANDLES;
84
85
sprintf(cmd, "findstr.exe %s", commandline);
86
87
CreateProcessA(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startup_info, &process_info);
88
CloseHandle(child_stdin_read);
89
CloseHandle(child_stdout_write);
90
CloseHandle(child_stderr_write);
91
92
write_to_handle(parent_stdin_write, input, lstrlenA(input));
93
CloseHandle(parent_stdin_write);
94
95
read_all_from_handle(parent_stdout_read, stdout_buffer, &stdout_size);
96
read_all_from_handle(parent_stderr_read, stderr_buffer, &stderr_size);
97
CloseHandle(parent_stdout_read);
98
CloseHandle(parent_stderr_read);
99
100
WaitForSingleObject(process_info.hProcess, INFINITE);
101
GetExitCodeProcess(process_info.hProcess, &exitcode);
102
CloseHandle(process_info.hProcess);
103
CloseHandle(process_info.hThread);
104
ok_(file, line)(exitcode == exitcode_expected, "Expected exitcode %d, got %ld\n",
105
exitcode_expected, exitcode);
106
}
107
108
#define run_find_file(a, b, c) _run_find_file(__FILE__, __LINE__, a, b, c)
109
static void _run_find_file(const char *file, int line, const char *commandline, const char *input,
110
int exitcode_expected)
111
{
112
char path_temp_file[MAX_PATH], path_temp_dir[MAX_PATH], commandline_new[MAX_PATH];
113
HANDLE handle_file;
114
115
GetTempPathA(ARRAY_SIZE(path_temp_dir), path_temp_dir);
116
GetTempFileNameA(path_temp_dir, "", 0, path_temp_file);
117
handle_file = CreateFileA(path_temp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
118
write_to_handle(handle_file, input, lstrlenA(input));
119
CloseHandle(handle_file);
120
121
sprintf(commandline_new, "%s %s", commandline, path_temp_file);
122
_run_find_stdin(file, line, commandline_new, "", exitcode_expected);
123
124
DeleteFileA(path_temp_file);
125
}
126
127
static void test_basic(void)
128
{
129
int ret;
130
131
/* No options */
132
run_find_stdin("", "", 2);
133
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
134
ok(stderr_size > 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
135
ret = strcmp(stderr_buffer, "FINDSTR: Bad command line\r\n");
136
ok(!ret, "Got the wrong result.\n");
137
138
/* /? */
139
run_find_stdin("/?", "", 0);
140
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
141
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
142
143
/* find string in stdin */
144
run_find_stdin("abc", "abc", 0);
145
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
146
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
147
ret = strcmp(stdout_buffer, "abc\r\n");
148
ok(!ret, "Got the wrong result.\n");
149
150
/* find string in stdin, LF */
151
run_find_stdin("/L abc", "abc\n", 0);
152
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
153
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
154
ret = strcmp(stdout_buffer, "abc\n");
155
ok(!ret, "Got the wrong result.\n");
156
157
/* find string in stdin, CR/LF */
158
run_find_stdin("/L abc", "abc\r\n", 0);
159
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
160
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
161
ret = strcmp(stdout_buffer, "abc\r\n");
162
ok(!ret, "Got the wrong result.\n");
163
164
/* find string in stdin fails */
165
run_find_stdin("/L abc", "cba", 1);
166
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
167
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
168
169
/* find string in file */
170
run_find_file("/L abc", "abc", 0);
171
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
172
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
173
ret = strcmp(stdout_buffer, "abc");
174
ok(!ret, "Got the wrong result.\n");
175
176
/* find string in file fails */
177
run_find_file("/L abc", "cba", 1);
178
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
179
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
180
181
/* find string in stdin with space separator */
182
run_find_stdin("/L \"abc cba\"", "abc", 0);
183
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
184
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
185
ret = strcmp(stdout_buffer, "abc\r\n");
186
ok(!ret, "Got the wrong result.\n");
187
188
/* find string in stdin with /C: */
189
run_find_stdin("/L /C:abc", "abc", 0);
190
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
191
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
192
ret = strcmp(stdout_buffer, "abc\r\n");
193
ok(!ret, "Got the wrong result.\n");
194
195
/* find string in stdin with /C:"abc" */
196
run_find_stdin("/L /C:\"abc\"", "abc", 0);
197
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
198
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
199
ret = strcmp(stdout_buffer, "abc\r\n");
200
ok(!ret, "Got the wrong result.\n");
201
202
/* find string in stdin with /C:"abc cba" fails */
203
run_find_stdin("/L /C:\"abc cba\"", "abc", 1);
204
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
205
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
206
207
/* find string in stdin with /C: fails */
208
run_find_stdin("/L /C:abc", "cba", 1);
209
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
210
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
211
212
/* find string in file, case insensitive */
213
run_find_file("/L /I aBc", "abc", 0);
214
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
215
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
216
ret = strcmp(stdout_buffer, "abc");
217
ok(!ret, "Got the wrong result.\n");
218
219
/* find string in file, regular expression */
220
run_find_file("/R abc", "abc", 0);
221
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
222
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
223
ret = strcmp(stdout_buffer, "abc");
224
ok(!ret, "Got the wrong result.\n");
225
226
/* find string in file, regular expression, LF */
227
run_find_file("/R abc", "abc\n", 0);
228
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
229
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
230
ret = strcmp(stdout_buffer, "abc\n");
231
ok(!ret, "Got the wrong result.\n");
232
233
/* find string in file, regular expression, CR/LF */
234
run_find_file("/R abc", "abc\r\n", 0);
235
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
236
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
237
ret = strcmp(stdout_buffer, "abc\r\n");
238
ok(!ret, "Got the wrong result.\n");
239
240
/* find string in stdin, regular expression */
241
run_find_stdin("/R abc", "abc", 0);
242
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
243
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
244
ret = strcmp(stdout_buffer, "abc\r\n");
245
ok(!ret, "Got the wrong result.\n");
246
247
/* find string in stdin, regular expression, LF */
248
run_find_stdin("/R abc", "abc\n", 0);
249
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
250
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
251
ret = strcmp(stdout_buffer, "abc\n");
252
ok(!ret, "Got the wrong result.\n");
253
254
/* find string in stdin, regular expression, CR/LF */
255
run_find_stdin("/R abc", "abc\r\n", 0);
256
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
257
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
258
ret = strcmp(stdout_buffer, "abc\r\n");
259
ok(!ret, "Got the wrong result.\n");
260
261
/* find string in file with /C:, regular expression */
262
run_find_file("/R /C:^abc", "abc", 0);
263
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
264
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
265
ret = strcmp(stdout_buffer, "abc");
266
ok(!ret, "Got the wrong result.\n");
267
268
/* find string in file, regular expression, case insensitive */
269
run_find_file("/I /R /C:.Bc", "abc", 0);
270
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
271
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
272
ret = strcmp(stdout_buffer, "abc");
273
ok(!ret, "Got the wrong result.\n");
274
275
/* find string in file, regular expression, escape */
276
run_find_file("/R /C:\\.bc", "abc", 1);
277
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
278
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
279
280
/* $ doesn't match if there's no newline */
281
run_find_file("/R /C:abc$", "abc", 1);
282
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
283
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
284
285
run_find_file("/R /C:abc$", "abc\r\n", 0);
286
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
287
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
288
ret = strcmp(stdout_buffer, "abc\r\n");
289
ok(!ret, "Got the wrong result.\n");
290
291
/* escaped . before * */
292
run_find_file("/R /C:\\.*", "...", 0);
293
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
294
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
295
ret = strcmp(stdout_buffer, "...");
296
ok(!ret, "Got the wrong result. '%s'\n", stdout_buffer);
297
298
run_find_file("/R /C:\\.*", "abc", 0);
299
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
300
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
301
ret = strcmp(stdout_buffer, "abc");
302
ok(!ret, "Got the wrong result. '%s'\n", stdout_buffer);
303
304
/* ^ after first character */
305
run_find_file("/R /C:a^bc", "a^bc", 0);
306
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
307
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
308
ret = strcmp(stdout_buffer, "a^bc");
309
ok(!ret, "Got the wrong result. '%s'\n", stdout_buffer);
310
311
/* $ before last character */
312
run_find_file("/R /C:ab$c", "ab$c", 0);
313
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
314
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
315
ret = strcmp(stdout_buffer, "ab$c");
316
ok(!ret, "Got the wrong result. '%s'\n", stdout_buffer);
317
318
run_find_file("/R .", "a", 0);
319
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
320
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
321
ret = strcmp(stdout_buffer, "a");
322
ok(!ret, "Got the wrong result. '%s'\n", stdout_buffer);
323
324
run_find_file(".", "a", 0);
325
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
326
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
327
ret = strcmp(stdout_buffer, "a");
328
ok(!ret, "Got the wrong result. '%s'\n", stdout_buffer);
329
330
run_find_file("/L .", "a", 1);
331
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
332
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
333
ok(!ret, "Got the wrong result. '%s'\n", stdout_buffer);
334
}
335
336
START_TEST(findstr)
337
{
338
if (PRIMARYLANGID(GetUserDefaultUILanguage()) != LANG_ENGLISH)
339
{
340
skip("Tests only work with English locale.\n");
341
return;
342
}
343
344
test_basic();
345
}
346
347