From a189a411dc1fa0b326d0d281c3a7f1f40be1c3a8 Mon Sep 17 00:00:00 2001 From: Kaipeng Zeng Date: Sun, 19 Apr 2015 10:35:58 +0800 Subject: [PATCH 1/2] findstr: Added tests for findstr. --- configure.ac | 1 + programs/findstr/tests/Makefile.in | 5 ++ programs/findstr/tests/findstr.c | 164 +++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 programs/findstr/tests/Makefile.in create mode 100644 programs/findstr/tests/findstr.c diff --git a/configure.ac b/configure.ac index d23227a..822affe 100644 --- a/configure.ac +++ b/configure.ac @@ -3432,6 +3432,7 @@ WINE_CONFIG_PROGRAM(expand,,[install]) WINE_CONFIG_PROGRAM(explorer,,[install,po]) WINE_CONFIG_PROGRAM(extrac32,,[install]) WINE_CONFIG_PROGRAM(findstr,,[install]) +WINE_CONFIG_TEST(programs/findstr/tests) WINE_CONFIG_PROGRAM(hh,,[install]) WINE_CONFIG_PROGRAM(hostname,,[install,po]) WINE_CONFIG_PROGRAM(icinfo,,[install]) diff --git a/programs/findstr/tests/Makefile.in b/programs/findstr/tests/Makefile.in new file mode 100644 index 0000000..b5195d0 --- /dev/null +++ b/programs/findstr/tests/Makefile.in @@ -0,0 +1,5 @@ +TESTDLL = findstr.exe + +C_SRCS = \ + findstr.c + diff --git a/programs/findstr/tests/findstr.c b/programs/findstr/tests/findstr.c new file mode 100644 index 0000000..705fd89 --- /dev/null +++ b/programs/findstr/tests/findstr.c @@ -0,0 +1,164 @@ +/* + * Copyright 2015 Kaipeng Zeng + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include + +#include "wine/test.h" + + +static DWORD runcmd(const char *cmd, LPCSTR *output) +{ + char *wcmd; + SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, TRUE}; + HANDLE outfile, hRead; + STARTUPINFOA si = {sizeof(si)}; + PROCESS_INFORMATION pi; + DWORD filesize, result, bytesRead; + + wcmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlen(cmd) + 1); + if(!wcmd) + return 260; + strcpy(wcmd, cmd); + + outfile = CreateFileA("test.out", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + ok(outfile != INVALID_HANDLE_VALUE, "CreateFile failed.\n"); + if(outfile == INVALID_HANDLE_VALUE) + return 260; + + si.hStdOutput = outfile; + si.dwFlags = STARTF_USESTDHANDLES; + if(!CreateProcessA(NULL, wcmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) + { + HeapFree(GetProcessHeap(), 0, wcmd); + CloseHandle(outfile); + return 260; + } + HeapFree(GetProcessHeap(), 0, wcmd); + + WaitForSingleObject(pi.hProcess, INFINITE); + GetExitCodeProcess(pi.hProcess, &result); + CloseHandle(outfile); + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + + hRead = CreateFileA("test.out", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); + ok(hRead != INVALID_HANDLE_VALUE, "CreateFile failed.\n"); + if(hRead == INVALID_HANDLE_VALUE) + return 260; + filesize = GetFileSize(hRead, NULL); + *output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, filesize + 1); + if(!*output) + return 260; + ReadFile(hRead, (LPVOID)*output, filesize, &bytesRead, NULL); + CloseHandle(hRead); + + return result; +} + +static void create_file(char *buffer) +{ + char text_for_test[] = "Wine Is Not an Emulator.\n" + "Wine will always be free software.\n" + "Wine is heavily reliant on its user community too."; + HANDLE file; + DWORD dwBytesWritten = 0; + char path[1024],filename[1024]; + + GetTempPathA(1024, path); + GetTempFileNameA(path, "fst", 9, filename); + strcpy(buffer, filename); + file = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + ok(file != INVALID_HANDLE_VALUE, "CreateFile failed.\n"); + ok(WriteFile(file, text_for_test, strlen(text_for_test) * sizeof(CHAR), + &dwBytesWritten, NULL), "WriteFile failed: 0x%08x\n.", GetLastError()); + CloseHandle(file); +} + +static LPCSTR generate_cmdline(const char *keyword, char *filename) +{ + char *cmdline; + + cmdline = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlen(keyword) + strlen(filename) + 11); + strcat(cmdline, "findstr "); + strcat(cmdline, keyword); + strcat(cmdline, " "); + strcat(cmdline, filename); + return cmdline; +} + +static void cleanup(LPCSTR *buffer, LPCSTR *cmdline) +{ + HeapFree(GetProcessHeap(), 0, (LPVOID)*buffer); + HeapFree(GetProcessHeap(), 0, (LPVOID)*cmdline); + DeleteFileA("test.out"); +} + +static void test_without_arg(void) +{ + char filename[MAX_PATH]; + LPCSTR buffer; + LPCSTR cmdline; + int res; + + create_file(filename); + + cmdline = generate_cmdline("free", filename); + res = runcmd(cmdline, &buffer); + todo_wine ok(!res, "Return %d expected 0.\n", res); + todo_wine ok(!strcmp(buffer, "Wine will always be free software.\n"), + "Return wrong string: %s.\n", buffer); + cleanup(&buffer, &cmdline); + + cmdline = generate_cmdline("", filename); + res = runcmd(cmdline, &buffer); + todo_wine ok(res == 1, "Return %d expected 1.\n", res); + todo_wine ok(!strcmp(buffer, ""), + "Return wrong string: %s.\n", buffer); + cleanup(&buffer, &cmdline); + + cmdline = generate_cmdline("Free", filename); + res = runcmd(cmdline, &buffer); + todo_wine ok(res == 1, "Return %d expected 1.\n", res); + todo_wine ok(!strcmp(buffer, ""), + "Return wrong string: %s.\n", buffer); + cleanup(&buffer, &cmdline); + + cmdline = generate_cmdline("omg", filename); + res = runcmd(cmdline, &buffer); + todo_wine ok(res == 1, "Return %d expected 1.\n", res); + todo_wine ok(!strcmp(buffer, ""), + "Return wrong string: %s.\n", buffer); + cleanup(&buffer, &cmdline); + + cmdline = generate_cmdline("Wine", filename); + res = runcmd(cmdline, &buffer); + todo_wine ok(!res, "Return %d expected 0.\n", res); + todo_wine ok(!strcmp(buffer, "Wine Is Not an Emulator.\nWine will always be free software.\n" + "Wine is heavily reliant on its user community too."), + "Return wrong string: %s.\n", buffer); + cleanup(&buffer, &cmdline); + + DeleteFileA(filename); +} + +START_TEST(findstr) +{ + test_without_arg(); +} -- 2.1.0