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
|
/*
* Copyright 2023 Zhiyi Zhang for CodeWeavers
*
* 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 <windows.h>
#include <psapi.h>
#include "wine/test.h"
#define MAX_BUFFER 65536
static char stdout_buffer[MAX_BUFFER], stderr_buffer[MAX_BUFFER];
static DWORD stdout_size, stderr_size;
static void read_all_from_handle(HANDLE handle, char *buffer, DWORD *size)
{
char bytes[4096];
DWORD bytes_read;
memset(buffer, 0, MAX_BUFFER);
*size = 0;
for (;;)
{
BOOL success = ReadFile(handle, bytes, sizeof(bytes), &bytes_read, NULL);
if (!success || !bytes_read)
break;
if (*size + bytes_read > MAX_BUFFER)
{
ok(FALSE, "Insufficient buffer.\n");
break;
}
memcpy(buffer + *size, bytes, bytes_read);
*size += bytes_read;
}
}
#define run_tasklist(a, b) _run_tasklist(__FILE__, __LINE__, a, b)
static void _run_tasklist(const char *file, int line, const char *commandline, int exitcode_expected)
{
HANDLE child_stdout_write, child_stderr_write, parent_stdout_read, parent_stderr_read;
SECURITY_ATTRIBUTES security_attributes = {0};
PROCESS_INFORMATION process_info = {0};
STARTUPINFOA startup_info = {0};
DWORD exitcode;
char cmd[256];
security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
security_attributes.bInheritHandle = TRUE;
CreatePipe(&parent_stdout_read, &child_stdout_write, &security_attributes, 0);
CreatePipe(&parent_stderr_read, &child_stderr_write, &security_attributes, 0);
SetHandleInformation(parent_stdout_read, HANDLE_FLAG_INHERIT, 0);
SetHandleInformation(parent_stderr_read, HANDLE_FLAG_INHERIT, 0);
startup_info.cb = sizeof(STARTUPINFOA);
startup_info.hStdOutput = child_stdout_write;
startup_info.hStdError = child_stderr_write;
startup_info.dwFlags |= STARTF_USESTDHANDLES;
sprintf(cmd, "tasklist.exe %s", commandline);
CreateProcessA(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startup_info, &process_info);
CloseHandle(child_stdout_write);
CloseHandle(child_stderr_write);
read_all_from_handle(parent_stdout_read, stdout_buffer, &stdout_size);
read_all_from_handle(parent_stderr_read, stderr_buffer, &stderr_size);
CloseHandle(parent_stdout_read);
CloseHandle(parent_stderr_read);
WaitForSingleObject(process_info.hProcess, INFINITE);
GetExitCodeProcess(process_info.hProcess, &exitcode);
CloseHandle(process_info.hProcess);
CloseHandle(process_info.hThread);
ok_(file, line)(exitcode == exitcode_expected, "Expected exitcode %d, got %ld\n",
exitcode_expected, exitcode);
}
static void test_basic(void)
{
char *pos;
/* No options */
run_tasklist("", 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, "\r\n"
"Image Name PID Session Name Session# Mem Usage\r\n"
"========================= ======== ================ =========== ============\r\n");
ok(pos == stdout_buffer, "Got the wrong first line.\n");
pos = strstr(stdout_buffer, "tasklist.exe");
ok(!!pos, "Failed to list tasklist.exe.\n");
/* /? */
run_tasklist("/?", 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
}
static void test_no_header(void)
{
char *pos;
/* /nh */
run_tasklist("/nh", 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, "Image Name");
ok(!pos, "Got header.\n");
pos = strstr(stdout_buffer, "=");
ok(!pos, "Got header.\n");
}
static void test_format(void)
{
char *pos;
/* /fo */
run_tasklist("/fo", 1);
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size > 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
/* /fo invalid */
run_tasklist("/fo invalid", 1);
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size > 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
/* /fo TABLE */
run_tasklist("/fo TABLE", 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, "\r\n"
"Image Name PID Session Name Session# Mem Usage\r\n"
"========================= ======== ================ =========== ============\r\n");
ok(pos == stdout_buffer, "Got the wrong first line.\n");
pos = strstr(stdout_buffer, "tasklist.exe");
ok(!!pos, "Failed to list tasklist.exe.\n");
/* /fo CSV */
run_tasklist("/fo CSV", 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, "\"Image Name\",\"PID\",\"Session Name\",\"Session#\",\"Mem Usage\"");
ok(pos == stdout_buffer, "Got the wrong first line.\n");
pos = strstr(stdout_buffer, "\"tasklist.exe\",");
ok(!!pos, "Failed to list tasklist.exe.\n");
/* /fo LIST */
run_tasklist("/fo LIST", 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, "Image Name: tasklist.exe");
ok(!!pos, "Failed to list tasklist.exe.\n");
}
static void test_filter(void)
{
char options[256], *pos, basename[64];
HANDLE current_process;
DWORD current_pid;
current_pid = GetCurrentProcessId();
current_process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, current_pid);
GetModuleBaseNameA(current_process, NULL, basename, ARRAY_SIZE(basename));
CloseHandle(current_process);
/* /fi */
/* no value for fi */
run_tasklist("/fi", 1);
ok(stdout_size == 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size > 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
/* IMAGENAME eq */
sprintf(options, "/fi \"IMAGENAME eq %s\"", basename);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!!pos, "Failed to list %s.\n", basename);
/* IMAGENAME ne */
sprintf(options, "/fi \"IMAGENAME ne %s\"", basename);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!pos, "Got %s.\n", basename);
pos = strstr(stdout_buffer, "tasklist.exe");
ok(!!pos, "Failed to list tasklist.exe.\n");
/* PID eq */
sprintf(options, "/fi \"PID eq %ld\"", current_pid);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!!pos, "Failed to list %s.\n", basename);
/* PID ne */
sprintf(options, "/fi \"PID ne %ld\"", current_pid);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!pos, "Got %s.\n", basename);
pos = strstr(stdout_buffer, "tasklist.exe");
ok(!!pos, "Failed to list tasklist.exe.\n");
/* PID gt */
sprintf(options, "/fi \"PID gt %ld\"", current_pid - 1);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!!pos, "Failed to list %s.\n", basename);
/* PID lt */
sprintf(options, "/fi \"PID lt %ld\"", current_pid + 1);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!!pos, "Failed to list %s.\n", basename);
/* PID ge */
sprintf(options, "/fi \"PID ge %ld\"", current_pid);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!!pos, "Failed to list %s.\n", basename);
/* PID le */
sprintf(options, "/fi \"PID le %ld\"", current_pid);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!!pos, "Failed to list %s.\n", basename);
/* IMAGENAME eq + PID eq */
sprintf(options, "/fi \"IMAGENAME eq %s\" /fi \"PID eq %ld\"", basename, current_pid);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!!pos, "Failed to list %s.\n", basename);
/* IMAGENAME eq + PID eq with wrong PID */
sprintf(options, "/fi \"IMAGENAME eq %s\" /fi \"PID eq %ld\"", basename, current_pid + 1);
run_tasklist(options, 0);
ok(stdout_size > 0, "Unexpected stdout buffer size %ld.\n", stdout_size);
ok(stderr_size == 0, "Unexpected stderr buffer size %ld.\n", stderr_size);
pos = strstr(stdout_buffer, basename);
ok(!pos, "Got %s.\n", basename);
}
START_TEST(tasklist)
{
if (PRIMARYLANGID(GetUserDefaultUILanguage()) != LANG_ENGLISH)
{
skip("Tests only work with English locale.\n");
return;
}
test_basic();
test_no_header();
test_format();
test_filter();
}
|