dmcompos: Replaced && 0xff by & 0xff.
[wine] / dlls / kernel32 / tests / process.c
1 /*
2  * Unit test suite for CreateProcess function.
3  *
4  * Copyright 2002 Eric Pouech
5  * Copyright 2006 Dmitry Timoshkov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "wine/test.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "wincon.h"
32 #include "winnls.h"
33
34 static char     base[MAX_PATH];
35 static char     selfname[MAX_PATH];
36 static char     resfile[MAX_PATH];
37
38 static int      myARGC;
39 static char**   myARGV;
40
41 /* As some environment variables get very long on Unix, we only test for
42  * the first 127 bytes.
43  * Note that increasing this value past 256 may exceed the buffer size
44  * limitations of the *Profile functions (at least on Wine).
45  */
46 #define MAX_LISTED_ENV_VAR      128
47
48 /* ---------------- portable memory allocation thingie */
49
50 static char     memory[1024*32];
51 static char*    memory_index = memory;
52
53 static char*    grab_memory(size_t len)
54 {
55     char*       ret = memory_index;
56     /* align on dword */
57     len = (len + 3) & ~3;
58     memory_index += len;
59     assert(memory_index <= memory + sizeof(memory));
60     return ret;
61 }
62
63 static void     release_memory(void)
64 {
65     memory_index = memory;
66 }
67
68 /* ---------------- simplistic tool to encode/decode strings (to hide \ " ' and such) */
69
70 static const char* encodeA(const char* str)
71 {
72     char*       ptr;
73     size_t      len,i;
74
75     if (!str) return "";
76     len = strlen(str) + 1;
77     ptr = grab_memory(len * 2 + 1);
78     for (i = 0; i < len; i++)
79         sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
80     ptr[2 * len] = '\0';
81     return ptr;
82 }
83
84 static const char* encodeW(const WCHAR* str)
85 {
86     char*       ptr;
87     size_t      len,i;
88
89     if (!str) return "";
90     len = lstrlenW(str) + 1;
91     ptr = grab_memory(len * 4 + 1);
92     assert(ptr);
93     for (i = 0; i < len; i++)
94         sprintf(&ptr[i * 4], "%04x", (unsigned int)(unsigned short)str[i]);
95     ptr[4 * len] = '\0';
96     return ptr;
97 }
98
99 static unsigned decode_char(char c)
100 {
101     if (c >= '0' && c <= '9') return c - '0';
102     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
103     assert(c >= 'A' && c <= 'F');
104     return c - 'A' + 10;
105 }
106
107 static char*    decodeA(const char* str)
108 {
109     char*       ptr;
110     size_t      len,i;
111
112     len = strlen(str) / 2;
113     if (!len--) return NULL;
114     ptr = grab_memory(len + 1);
115     for (i = 0; i < len; i++)
116         ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
117     ptr[len] = '\0';
118     return ptr;
119 }
120
121 #if 0
122 /* This will be needed to decode Unicode strings saved by the child process
123  * when we test Unicode functions.
124  */
125 static WCHAR*   decodeW(const char* str)
126 {
127     size_t      len;
128     WCHAR*      ptr;
129     int         i;
130
131     len = strlen(str) / 4;
132     if (!len--) return NULL;
133     ptr = (WCHAR*)grab_memory(len * 2 + 1);
134     for (i = 0; i < len; i++)
135         ptr[i] = (decode_char(str[4 * i]) << 12) |
136             (decode_char(str[4 * i + 1]) << 8) |
137             (decode_char(str[4 * i + 2]) << 4) |
138             (decode_char(str[4 * i + 3]) << 0);
139     ptr[len] = '\0';
140     return ptr;
141 }
142 #endif
143
144 /******************************************************************
145  *              init
146  *
147  * generates basic information like:
148  *      base:           absolute path to curr dir
149  *      selfname:       the way to reinvoke ourselves
150  */
151 static int     init(void)
152 {
153     myARGC = winetest_get_mainargs( &myARGV );
154     if (!GetCurrentDirectoryA(sizeof(base), base)) return 0;
155     strcpy(selfname, myARGV[0]);
156     return 1;
157 }
158
159 /******************************************************************
160  *              get_file_name
161  *
162  * generates an absolute file_name for temporary file
163  *
164  */
165 static void     get_file_name(char* buf)
166 {
167     char        path[MAX_PATH];
168
169     buf[0] = '\0';
170     GetTempPathA(sizeof(path), path);
171     GetTempFileNameA(path, "wt", 0, buf);
172 }
173
174 /******************************************************************
175  *              static void     childPrintf
176  *
177  */
178 static void     childPrintf(HANDLE h, const char* fmt, ...)
179 {
180     va_list     valist;
181     char        buffer[1024+4*MAX_LISTED_ENV_VAR];
182     DWORD       w;
183
184     va_start(valist, fmt);
185     vsprintf(buffer, fmt, valist);
186     va_end(valist);
187     WriteFile(h, buffer, strlen(buffer), &w, NULL);
188 }
189
190
191 /******************************************************************
192  *              doChild
193  *
194  * output most of the information in the child process
195  */
196 static void     doChild(const char* file, const char* option)
197 {
198     STARTUPINFOA        siA;
199     STARTUPINFOW        siW;
200     int                 i;
201     char*               ptrA;
202     WCHAR*              ptrW;
203     char                bufA[MAX_PATH];
204     WCHAR               bufW[MAX_PATH];
205     HANDLE              hFile = CreateFileA(file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
206     BOOL ret;
207
208     if (hFile == INVALID_HANDLE_VALUE) return;
209
210     /* output of startup info (Ansi) */
211     GetStartupInfoA(&siA);
212     childPrintf(hFile,
213                 "[StartupInfoA]\ncb=%08ld\nlpDesktop=%s\nlpTitle=%s\n"
214                 "dwX=%lu\ndwY=%lu\ndwXSize=%lu\ndwYSize=%lu\n"
215                 "dwXCountChars=%lu\ndwYCountChars=%lu\ndwFillAttribute=%lu\n"
216                 "dwFlags=%lu\nwShowWindow=%u\n"
217                 "hStdInput=%lu\nhStdOutput=%lu\nhStdError=%lu\n\n",
218                 siA.cb, encodeA(siA.lpDesktop), encodeA(siA.lpTitle),
219                 siA.dwX, siA.dwY, siA.dwXSize, siA.dwYSize,
220                 siA.dwXCountChars, siA.dwYCountChars, siA.dwFillAttribute,
221                 siA.dwFlags, siA.wShowWindow,
222                 (DWORD)siA.hStdInput, (DWORD)siA.hStdOutput, (DWORD)siA.hStdError);
223
224     /* since GetStartupInfoW is only implemented in win2k,
225      * zero out before calling so we can notice the difference
226      */
227     memset(&siW, 0, sizeof(siW));
228     GetStartupInfoW(&siW);
229     childPrintf(hFile,
230                 "[StartupInfoW]\ncb=%08ld\nlpDesktop=%s\nlpTitle=%s\n"
231                 "dwX=%lu\ndwY=%lu\ndwXSize=%lu\ndwYSize=%lu\n"
232                 "dwXCountChars=%lu\ndwYCountChars=%lu\ndwFillAttribute=%lu\n"
233                 "dwFlags=%lu\nwShowWindow=%u\n"
234                 "hStdInput=%lu\nhStdOutput=%lu\nhStdError=%lu\n\n",
235                 siW.cb, encodeW(siW.lpDesktop), encodeW(siW.lpTitle),
236                 siW.dwX, siW.dwY, siW.dwXSize, siW.dwYSize,
237                 siW.dwXCountChars, siW.dwYCountChars, siW.dwFillAttribute,
238                 siW.dwFlags, siW.wShowWindow,
239                 (DWORD)siW.hStdInput, (DWORD)siW.hStdOutput, (DWORD)siW.hStdError);
240
241     /* Arguments */
242     childPrintf(hFile, "[Arguments]\nargcA=%d\n", myARGC);
243     for (i = 0; i < myARGC; i++)
244     {
245         childPrintf(hFile, "argvA%d=%s\n", i, encodeA(myARGV[i]));
246     }
247     childPrintf(hFile, "CommandLineA=%s\n", encodeA(GetCommandLineA()));
248
249 #if 0
250     int                 argcW;
251     WCHAR**             argvW;
252
253     /* this is part of shell32... and should be tested there */
254     argvW = CommandLineToArgvW(GetCommandLineW(), &argcW);
255     for (i = 0; i < argcW; i++)
256     {
257         childPrintf(hFile, "argvW%d=%s\n", i, encodeW(argvW[i]));
258     }
259 #endif
260     childPrintf(hFile, "CommandLineW=%s\n\n", encodeW(GetCommandLineW()));
261
262     /* output of environment (Ansi) */
263     ptrA = GetEnvironmentStringsA();
264     if (ptrA)
265     {
266         char    env_var[MAX_LISTED_ENV_VAR];
267
268         childPrintf(hFile, "[EnvironmentA]\n");
269         i = 0;
270         while (*ptrA)
271         {
272             lstrcpynA(env_var, ptrA, MAX_LISTED_ENV_VAR);
273             childPrintf(hFile, "env%d=%s\n", i, encodeA(env_var));
274             i++;
275             ptrA += strlen(ptrA) + 1;
276         }
277         childPrintf(hFile, "len=%d\n\n", i);
278     }
279
280     /* output of environment (Unicode) */
281     ptrW = GetEnvironmentStringsW();
282     if (ptrW)
283     {
284         WCHAR   env_var[MAX_LISTED_ENV_VAR];
285
286         childPrintf(hFile, "[EnvironmentW]\n");
287         i = 0;
288         while (*ptrW)
289         {
290             lstrcpynW(env_var, ptrW, MAX_LISTED_ENV_VAR - 1);
291             env_var[MAX_LISTED_ENV_VAR - 1] = '\0';
292             childPrintf(hFile, "env%d=%s\n", i, encodeW(env_var));
293             i++;
294             ptrW += lstrlenW(ptrW) + 1;
295         }
296         childPrintf(hFile, "len=%d\n\n", i);
297     }
298
299     childPrintf(hFile, "[Misc]\n");
300     if (GetCurrentDirectoryA(sizeof(bufA), bufA))
301         childPrintf(hFile, "CurrDirA=%s\n", encodeA(bufA));
302     if (GetCurrentDirectoryW(sizeof(bufW) / sizeof(bufW[0]), bufW))
303         childPrintf(hFile, "CurrDirW=%s\n", encodeW(bufW));
304     childPrintf(hFile, "\n");
305
306     if (option && strcmp(option, "console") == 0)
307     {
308         CONSOLE_SCREEN_BUFFER_INFO      sbi;
309         HANDLE hConIn  = GetStdHandle(STD_INPUT_HANDLE);
310         HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
311         DWORD modeIn, modeOut;
312
313         childPrintf(hFile, "[Console]\n");
314         if (GetConsoleScreenBufferInfo(hConOut, &sbi))
315         {
316             childPrintf(hFile, "SizeX=%d\nSizeY=%d\nCursorX=%d\nCursorY=%d\nAttributes=%d\n",
317                         sbi.dwSize.X, sbi.dwSize.Y, sbi.dwCursorPosition.X, sbi.dwCursorPosition.Y, sbi.wAttributes);
318             childPrintf(hFile, "winLeft=%d\nwinTop=%d\nwinRight=%d\nwinBottom=%d\n",
319                         sbi.srWindow.Left, sbi.srWindow.Top, sbi.srWindow.Right, sbi.srWindow.Bottom);
320             childPrintf(hFile, "maxWinWidth=%d\nmaxWinHeight=%d\n",
321                         sbi.dwMaximumWindowSize.X, sbi.dwMaximumWindowSize.Y);
322         }
323         childPrintf(hFile, "InputCP=%d\nOutputCP=%d\n",
324                     GetConsoleCP(), GetConsoleOutputCP());
325         if (GetConsoleMode(hConIn, &modeIn))
326             childPrintf(hFile, "InputMode=%ld\n", modeIn);
327         if (GetConsoleMode(hConOut, &modeOut))
328             childPrintf(hFile, "OutputMode=%ld\n", modeOut);
329
330         /* now that we have written all relevant information, let's change it */
331         ok(SetConsoleCP(1252), "Setting CP\n");
332         ok(SetConsoleOutputCP(1252), "Setting SB CP\n");
333         ret = SetConsoleMode(hConIn, modeIn ^ 1);
334         ok( ret, "Setting mode (%d)\n", GetLastError());
335         ret = SetConsoleMode(hConOut, modeOut ^ 1);
336         ok( ret, "Setting mode (%d)\n", GetLastError());
337         sbi.dwCursorPosition.X ^= 1;
338         sbi.dwCursorPosition.Y ^= 1;
339         ret = SetConsoleCursorPosition(hConOut, sbi.dwCursorPosition);
340         ok( ret, "Setting cursor position (%d)\n", GetLastError());
341     }
342     if (option && strcmp(option, "stdhandle") == 0)
343     {
344         HANDLE hStdIn  = GetStdHandle(STD_INPUT_HANDLE);
345         HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
346
347         if (hStdIn != INVALID_HANDLE_VALUE || hStdOut != INVALID_HANDLE_VALUE)
348         {
349             char buf[1024];
350             DWORD r, w;
351
352             ok(ReadFile(hStdIn, buf, sizeof(buf), &r, NULL) && r > 0, "Reading message from input pipe\n");
353             childPrintf(hFile, "[StdHandle]\nmsg=%s\n\n", encodeA(buf));
354             ok(WriteFile(hStdOut, buf, r, &w, NULL) && w == r, "Writing message to output pipe\n");
355         }
356     }
357
358     if (option && strcmp(option, "exit_code") == 0)
359     {
360         childPrintf(hFile, "[ExitCode]\nvalue=%d\n\n", 123);
361         CloseHandle(hFile);
362         ExitProcess(123);
363     }
364
365     CloseHandle(hFile);
366 }
367
368 static char* getChildString(const char* sect, const char* key)
369 {
370     char        buf[1024+4*MAX_LISTED_ENV_VAR];
371     char*       ret;
372
373     GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), resfile);
374     if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
375     assert(!(strlen(buf) & 1));
376     ret = decodeA(buf);
377     return ret;
378 }
379
380 /* FIXME: this may be moved to the wtmain.c file, because it may be needed by
381  * others... (windows uses stricmp while Un*x uses strcasecmp...)
382  */
383 static int wtstrcasecmp(const char* p1, const char* p2)
384 {
385     char c1, c2;
386
387     c1 = c2 = '@';
388     while (c1 == c2 && c1)
389     {
390         c1 = *p1++; c2 = *p2++;
391         if (c1 != c2)
392         {
393             c1 = toupper(c1); c2 = toupper(c2);
394         }
395     }
396     return c1 - c2;
397 }
398
399 static int strCmp(const char* s1, const char* s2, BOOL sensitive)
400 {
401     if (!s1 && !s2) return 0;
402     if (!s2) return -1;
403     if (!s1) return 1;
404     return (sensitive) ? strcmp(s1, s2) : wtstrcasecmp(s1, s2);
405 }
406
407 #define okChildString(sect, key, expect) \
408     do { \
409         char* result = getChildString((sect), (key)); \
410         ok(strCmp(result, expect, 1) == 0, "%s:%s expected '%s', got '%s'\n", (sect), (key), (expect)?(expect):"(null)", result); \
411     } while (0)
412
413 #define okChildIString(sect, key, expect) \
414     do { \
415         char* result = getChildString(sect, key); \
416         ok(strCmp(result, expect, 0) == 0, "%s:%s expected '%s', got '%s'\n", sect, key, expect, result); \
417     } while (0)
418
419 /* using !expect ensures that the test will fail if the sect/key isn't present
420  * in result file
421  */
422 #define okChildInt(sect, key, expect) \
423     do { \
424         UINT result = GetPrivateProfileIntA((sect), (key), !(expect), resfile); \
425         ok(result == expect, "%s:%s expected %d, but got %d\n", (sect), (key), (int)(expect), result); \
426    } while (0)
427
428 static void test_Startup(void)
429 {
430     char                buffer[MAX_PATH];
431     PROCESS_INFORMATION info;
432     STARTUPINFOA        startup,si;
433     static CHAR title[]   = "I'm the title string",
434                 desktop[] = "I'm the desktop string",
435                 empty[]   = "";
436
437     /* let's start simplistic */
438     memset(&startup, 0, sizeof(startup));
439     startup.cb = sizeof(startup);
440     startup.dwFlags = STARTF_USESHOWWINDOW;
441     startup.wShowWindow = SW_SHOWNORMAL;
442
443     get_file_name(resfile);
444     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
445     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
446     /* wait for child to terminate */
447     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
448     /* child process has changed result file, so let profile functions know about it */
449     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
450
451     GetStartupInfoA(&si);
452     okChildInt("StartupInfoA", "cb", startup.cb);
453     okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
454     okChildString("StartupInfoA", "lpTitle", si.lpTitle);
455     okChildInt("StartupInfoA", "dwX", startup.dwX);
456     okChildInt("StartupInfoA", "dwY", startup.dwY);
457     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
458     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
459     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
460     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
461     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
462     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
463     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
464     release_memory();
465     assert(DeleteFileA(resfile) != 0);
466
467     /* not so simplistic now */
468     memset(&startup, 0, sizeof(startup));
469     startup.cb = sizeof(startup);
470     startup.dwFlags = STARTF_USESHOWWINDOW;
471     startup.wShowWindow = SW_SHOWNORMAL;
472     startup.lpTitle = title;
473     startup.lpDesktop = desktop;
474     startup.dwXCountChars = 0x12121212;
475     startup.dwYCountChars = 0x23232323;
476     startup.dwX = 0x34343434;
477     startup.dwY = 0x45454545;
478     startup.dwXSize = 0x56565656;
479     startup.dwYSize = 0x67676767;
480     startup.dwFillAttribute = 0xA55A;
481
482     get_file_name(resfile);
483     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
484     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
485     /* wait for child to terminate */
486     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
487     /* child process has changed result file, so let profile functions know about it */
488     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
489
490     okChildInt("StartupInfoA", "cb", startup.cb);
491     okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
492     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
493     okChildInt("StartupInfoA", "dwX", startup.dwX);
494     okChildInt("StartupInfoA", "dwY", startup.dwY);
495     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
496     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
497     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
498     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
499     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
500     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
501     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
502     release_memory();
503     assert(DeleteFileA(resfile) != 0);
504
505     /* not so simplistic now */
506     memset(&startup, 0, sizeof(startup));
507     startup.cb = sizeof(startup);
508     startup.dwFlags = STARTF_USESHOWWINDOW;
509     startup.wShowWindow = SW_SHOWNORMAL;
510     startup.lpTitle = title;
511     startup.lpDesktop = NULL;
512     startup.dwXCountChars = 0x12121212;
513     startup.dwYCountChars = 0x23232323;
514     startup.dwX = 0x34343434;
515     startup.dwY = 0x45454545;
516     startup.dwXSize = 0x56565656;
517     startup.dwYSize = 0x67676767;
518     startup.dwFillAttribute = 0xA55A;
519
520     get_file_name(resfile);
521     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
522     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
523     /* wait for child to terminate */
524     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
525     /* child process has changed result file, so let profile functions know about it */
526     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
527
528     okChildInt("StartupInfoA", "cb", startup.cb);
529     okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
530     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
531     okChildInt("StartupInfoA", "dwX", startup.dwX);
532     okChildInt("StartupInfoA", "dwY", startup.dwY);
533     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
534     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
535     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
536     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
537     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
538     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
539     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
540     release_memory();
541     assert(DeleteFileA(resfile) != 0);
542
543     /* not so simplistic now */
544     memset(&startup, 0, sizeof(startup));
545     startup.cb = sizeof(startup);
546     startup.dwFlags = STARTF_USESHOWWINDOW;
547     startup.wShowWindow = SW_SHOWNORMAL;
548     startup.lpTitle = title;
549     startup.lpDesktop = empty;
550     startup.dwXCountChars = 0x12121212;
551     startup.dwYCountChars = 0x23232323;
552     startup.dwX = 0x34343434;
553     startup.dwY = 0x45454545;
554     startup.dwXSize = 0x56565656;
555     startup.dwYSize = 0x67676767;
556     startup.dwFillAttribute = 0xA55A;
557
558     get_file_name(resfile);
559     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
560     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
561     /* wait for child to terminate */
562     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
563     /* child process has changed result file, so let profile functions know about it */
564     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
565
566     okChildInt("StartupInfoA", "cb", startup.cb);
567     todo_wine okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
568     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
569     okChildInt("StartupInfoA", "dwX", startup.dwX);
570     okChildInt("StartupInfoA", "dwY", startup.dwY);
571     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
572     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
573     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
574     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
575     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
576     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
577     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
578     release_memory();
579     assert(DeleteFileA(resfile) != 0);
580
581     /* not so simplistic now */
582     memset(&startup, 0, sizeof(startup));
583     startup.cb = sizeof(startup);
584     startup.dwFlags = STARTF_USESHOWWINDOW;
585     startup.wShowWindow = SW_SHOWNORMAL;
586     startup.lpTitle = NULL;
587     startup.lpDesktop = desktop;
588     startup.dwXCountChars = 0x12121212;
589     startup.dwYCountChars = 0x23232323;
590     startup.dwX = 0x34343434;
591     startup.dwY = 0x45454545;
592     startup.dwXSize = 0x56565656;
593     startup.dwYSize = 0x67676767;
594     startup.dwFillAttribute = 0xA55A;
595
596     get_file_name(resfile);
597     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
598     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
599     /* wait for child to terminate */
600     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
601     /* child process has changed result file, so let profile functions know about it */
602     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
603
604     okChildInt("StartupInfoA", "cb", startup.cb);
605     okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
606     okChildString("StartupInfoA", "lpTitle", si.lpTitle);
607     okChildInt("StartupInfoA", "dwX", startup.dwX);
608     okChildInt("StartupInfoA", "dwY", startup.dwY);
609     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
610     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
611     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
612     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
613     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
614     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
615     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
616     release_memory();
617     assert(DeleteFileA(resfile) != 0);
618
619     /* not so simplistic now */
620     memset(&startup, 0, sizeof(startup));
621     startup.cb = sizeof(startup);
622     startup.dwFlags = STARTF_USESHOWWINDOW;
623     startup.wShowWindow = SW_SHOWNORMAL;
624     startup.lpTitle = empty;
625     startup.lpDesktop = desktop;
626     startup.dwXCountChars = 0x12121212;
627     startup.dwYCountChars = 0x23232323;
628     startup.dwX = 0x34343434;
629     startup.dwY = 0x45454545;
630     startup.dwXSize = 0x56565656;
631     startup.dwYSize = 0x67676767;
632     startup.dwFillAttribute = 0xA55A;
633
634     get_file_name(resfile);
635     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
636     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
637     /* wait for child to terminate */
638     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
639     /* child process has changed result file, so let profile functions know about it */
640     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
641
642     okChildInt("StartupInfoA", "cb", startup.cb);
643     okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
644     todo_wine okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
645     okChildInt("StartupInfoA", "dwX", startup.dwX);
646     okChildInt("StartupInfoA", "dwY", startup.dwY);
647     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
648     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
649     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
650     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
651     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
652     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
653     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
654     release_memory();
655     assert(DeleteFileA(resfile) != 0);
656
657     /* not so simplistic now */
658     memset(&startup, 0, sizeof(startup));
659     startup.cb = sizeof(startup);
660     startup.dwFlags = STARTF_USESHOWWINDOW;
661     startup.wShowWindow = SW_SHOWNORMAL;
662     startup.lpTitle = empty;
663     startup.lpDesktop = empty;
664     startup.dwXCountChars = 0x12121212;
665     startup.dwYCountChars = 0x23232323;
666     startup.dwX = 0x34343434;
667     startup.dwY = 0x45454545;
668     startup.dwXSize = 0x56565656;
669     startup.dwYSize = 0x67676767;
670     startup.dwFillAttribute = 0xA55A;
671
672     get_file_name(resfile);
673     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
674     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
675     /* wait for child to terminate */
676     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
677     /* child process has changed result file, so let profile functions know about it */
678     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
679
680     okChildInt("StartupInfoA", "cb", startup.cb);
681     todo_wine okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
682     todo_wine okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
683     okChildInt("StartupInfoA", "dwX", startup.dwX);
684     okChildInt("StartupInfoA", "dwY", startup.dwY);
685     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
686     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
687     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
688     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
689     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
690     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
691     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
692     release_memory();
693     assert(DeleteFileA(resfile) != 0);
694
695     /* TODO: test for A/W and W/A and W/W */
696 }
697
698 static void test_CommandLine(void)
699 {
700     char                buffer[MAX_PATH], fullpath[MAX_PATH], *lpFilePart, *p;
701     PROCESS_INFORMATION info;
702     STARTUPINFOA        startup;
703     DWORD               len;
704
705     memset(&startup, 0, sizeof(startup));
706     startup.cb = sizeof(startup);
707     startup.dwFlags = STARTF_USESHOWWINDOW;
708     startup.wShowWindow = SW_SHOWNORMAL;
709
710     /* the basics */
711     get_file_name(resfile);
712     sprintf(buffer, "%s tests/process.c %s \"C:\\Program Files\\my nice app.exe\"", selfname, resfile);
713     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
714     /* wait for child to terminate */
715     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
716     /* child process has changed result file, so let profile functions know about it */
717     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
718
719     okChildInt("Arguments", "argcA", 4);
720     okChildString("Arguments", "argvA3", "C:\\Program Files\\my nice app.exe");
721     okChildString("Arguments", "argvA4", NULL);
722     okChildString("Arguments", "CommandLineA", buffer);
723     release_memory();
724     assert(DeleteFileA(resfile) != 0);
725
726     memset(&startup, 0, sizeof(startup));
727     startup.cb = sizeof(startup);
728     startup.dwFlags = STARTF_USESHOWWINDOW;
729     startup.wShowWindow = SW_SHOWNORMAL;
730
731     /* from Frangois */
732     get_file_name(resfile);
733     sprintf(buffer, "%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", selfname, resfile);
734     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
735     /* wait for child to terminate */
736     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
737     /* child process has changed result file, so let profile functions know about it */
738     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
739
740     okChildInt("Arguments", "argcA", 6);
741     okChildString("Arguments", "argvA3", "a\"b\\");
742     okChildString("Arguments", "argvA4", "c\"");
743     okChildString("Arguments", "argvA5", "d");
744     okChildString("Arguments", "argvA6", NULL);
745     okChildString("Arguments", "CommandLineA", buffer);
746     release_memory();
747     assert(DeleteFileA(resfile) != 0);
748
749     /* Test for Bug1330 to show that XP doesn't change '/' to '\\' in argv[0]*/
750     get_file_name(resfile);
751     sprintf(buffer, "./%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", selfname, resfile);
752     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
753     /* wait for child to terminate */
754     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
755     /* child process has changed result file, so let profile functions know about it */
756     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
757     sprintf(buffer, "./%s", selfname);
758     okChildString("Arguments", "argvA0", buffer);
759     release_memory();
760     assert(DeleteFileA(resfile) != 0);
761
762     get_file_name(resfile);
763     sprintf(buffer, ".\\%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", selfname, resfile);
764     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
765     /* wait for child to terminate */
766     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
767     /* child process has changed result file, so let profile functions know about it */
768     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
769     sprintf(buffer, ".\\%s", selfname);
770     okChildString("Arguments", "argvA0", buffer);
771     release_memory();
772     assert(DeleteFileA(resfile) != 0);
773     
774     get_file_name(resfile);
775     len = GetFullPathNameA(selfname, MAX_PATH, fullpath, &lpFilePart);
776     assert ( lpFilePart != 0);
777     *(lpFilePart -1 ) = 0;
778     p = strrchr(fullpath, '\\');
779     assert (p);
780     sprintf(buffer, "..%s/%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", p, selfname, resfile);
781     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
782     /* wait for child to terminate */
783     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
784     /* child process has changed result file, so let profile functions know about it */
785     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
786     sprintf(buffer, "..%s/%s", p, selfname);
787     okChildString("Arguments", "argvA0", buffer);
788     release_memory();
789     assert(DeleteFileA(resfile) != 0);
790     
791 }
792
793 static void test_Directory(void)
794 {
795     char                buffer[MAX_PATH];
796     PROCESS_INFORMATION info;
797     STARTUPINFOA        startup;
798     char windir[MAX_PATH];
799     static CHAR cmdline[] = "winver.exe";
800
801     memset(&startup, 0, sizeof(startup));
802     startup.cb = sizeof(startup);
803     startup.dwFlags = STARTF_USESHOWWINDOW;
804     startup.wShowWindow = SW_SHOWNORMAL;
805
806     /* the basics */
807     get_file_name(resfile);
808     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
809     GetWindowsDirectoryA( windir, sizeof(windir) );
810     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, windir, &startup, &info), "CreateProcess\n");
811     /* wait for child to terminate */
812     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
813     /* child process has changed result file, so let profile functions know about it */
814     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
815
816     okChildIString("Misc", "CurrDirA", windir);
817     release_memory();
818     assert(DeleteFileA(resfile) != 0);
819
820     /* search PATH for the exe if directory is NULL */
821     ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
822     ok(TerminateProcess(info.hProcess, 0), "Child process termination\n");
823
824     /* if any directory is provided, don't search PATH, error on bad directory */
825     SetLastError(0xdeadbeef);
826     memset(&info, 0, sizeof(info));
827     ok(!CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L,
828                        NULL, "non\\existent\\directory", &startup, &info), "CreateProcess\n");
829     ok(GetLastError() == ERROR_DIRECTORY, "Expected ERROR_DIRECTORY, got %d\n", GetLastError());
830     ok(!TerminateProcess(info.hProcess, 0), "Child process should not exist\n");
831 }
832
833 static BOOL is_str_env_drive_dir(const char* str)
834 {
835     return str[0] == '=' && str[1] >= 'A' && str[1] <= 'Z' && str[2] == ':' &&
836         str[3] == '=' && str[4] == str[1];
837 }
838
839 /* compared expected child's environment (in gesA) from actual
840  * environment our child got
841  */
842 static void cmpEnvironment(const char* gesA)
843 {
844     int                 i, clen;
845     const char*         ptrA;
846     char*               res;
847     char                key[32];
848     BOOL                found;
849
850     clen = GetPrivateProfileIntA("EnvironmentA", "len", 0, resfile);
851     
852     /* now look each parent env in child */
853     if ((ptrA = gesA) != NULL)
854     {
855         while (*ptrA)
856         {
857             for (i = 0; i < clen; i++)
858             {
859                 sprintf(key, "env%d", i);
860                 res = getChildString("EnvironmentA", key);
861                 if (strncmp(ptrA, res, MAX_LISTED_ENV_VAR - 1) == 0)
862                     break;
863             }
864             found = i < clen;
865             ok(found, "Parent-env string %s isn't in child process\n", ptrA);
866             
867             ptrA += strlen(ptrA) + 1;
868             release_memory();
869         }
870     }
871     /* and each child env in parent */
872     for (i = 0; i < clen; i++)
873     {
874         sprintf(key, "env%d", i);
875         res = getChildString("EnvironmentA", key);
876         if ((ptrA = gesA) != NULL)
877         {
878             while (*ptrA)
879             {
880                 if (strncmp(res, ptrA, MAX_LISTED_ENV_VAR - 1) == 0)
881                     break;
882                 ptrA += strlen(ptrA) + 1;
883             }
884             if (!*ptrA) ptrA = NULL;
885         }
886
887         if (!is_str_env_drive_dir(res))
888         {
889             found = ptrA != NULL;
890             ok(found, "Child-env string %s isn't in parent process\n", res);
891         }
892         /* else => should also test we get the right per drive default directory here... */
893     }
894 }
895
896 static void test_Environment(void)
897 {
898     char                buffer[MAX_PATH];
899     PROCESS_INFORMATION info;
900     STARTUPINFOA        startup;
901     char*               child_env;
902     int                 child_env_len;
903     char*               ptr;
904     char*               env;
905     int                 slen;
906
907     memset(&startup, 0, sizeof(startup));
908     startup.cb = sizeof(startup);
909     startup.dwFlags = STARTF_USESHOWWINDOW;
910     startup.wShowWindow = SW_SHOWNORMAL;
911
912     /* the basics */
913     get_file_name(resfile);
914     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
915     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
916     /* wait for child to terminate */
917     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
918     /* child process has changed result file, so let profile functions know about it */
919     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
920     
921     cmpEnvironment(GetEnvironmentStringsA());
922     release_memory();
923     assert(DeleteFileA(resfile) != 0);
924
925     memset(&startup, 0, sizeof(startup));
926     startup.cb = sizeof(startup);
927     startup.dwFlags = STARTF_USESHOWWINDOW;
928     startup.wShowWindow = SW_SHOWNORMAL;
929
930     /* the basics */
931     get_file_name(resfile);
932     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
933     
934     child_env_len = 0;
935     ptr = GetEnvironmentStringsA();
936     while(*ptr)
937     {
938         slen = strlen(ptr)+1;
939         child_env_len += slen;
940         ptr += slen;
941     }
942     /* Add space for additional environment variables */
943     child_env_len += 256;
944     child_env = HeapAlloc(GetProcessHeap(), 0, child_env_len);
945     
946     ptr = child_env;
947     sprintf(ptr, "=%c:=%s", 'C', "C:\\FOO\\BAR");
948     ptr += strlen(ptr) + 1;
949     strcpy(ptr, "PATH=C:\\WINDOWS;C:\\WINDOWS\\SYSTEM;C:\\MY\\OWN\\DIR");
950     ptr += strlen(ptr) + 1;
951     strcpy(ptr, "FOO=BAR");
952     ptr += strlen(ptr) + 1;
953     strcpy(ptr, "BAR=FOOBAR");
954     ptr += strlen(ptr) + 1;
955     /* copy all existing variables except:
956      * - WINELOADER
957      * - PATH (already set above)
958      * - the directory definitions (=[A-Z]:=)
959      */
960     for (env = GetEnvironmentStringsA(); *env; env += strlen(env) + 1)
961     {
962         if (strncmp(env, "PATH=", 5) != 0 &&
963             strncmp(env, "WINELOADER=", 11) != 0 &&
964             !is_str_env_drive_dir(env))
965         {
966             strcpy(ptr, env);
967             ptr += strlen(ptr) + 1;
968         }
969     }
970     *ptr = '\0';
971     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, child_env, NULL, &startup, &info), "CreateProcess\n");
972     /* wait for child to terminate */
973     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
974     /* child process has changed result file, so let profile functions know about it */
975     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
976     
977     cmpEnvironment(child_env);
978
979     HeapFree(GetProcessHeap(), 0, child_env);
980     release_memory();
981     assert(DeleteFileA(resfile) != 0);
982 }
983
984 static  void    test_SuspendFlag(void)
985 {
986     char                buffer[MAX_PATH];
987     PROCESS_INFORMATION info;
988     STARTUPINFOA       startup, us;
989     DWORD               exit_status;
990
991     /* let's start simplistic */
992     memset(&startup, 0, sizeof(startup));
993     startup.cb = sizeof(startup);
994     startup.dwFlags = STARTF_USESHOWWINDOW;
995     startup.wShowWindow = SW_SHOWNORMAL;
996
997     get_file_name(resfile);
998     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
999     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &startup, &info), "CreateProcess\n");
1000
1001     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1002     Sleep(8000);
1003     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1004     ok(ResumeThread(info.hThread) == 1, "Resuming thread\n");
1005
1006     /* wait for child to terminate */
1007     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1008     /* child process has changed result file, so let profile functions know about it */
1009     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1010
1011     GetStartupInfoA(&us);
1012
1013     okChildInt("StartupInfoA", "cb", startup.cb);
1014     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1015     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
1016     okChildInt("StartupInfoA", "dwX", startup.dwX);
1017     okChildInt("StartupInfoA", "dwY", startup.dwY);
1018     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1019     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1020     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1021     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1022     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1023     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1024     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1025     release_memory();
1026     assert(DeleteFileA(resfile) != 0);
1027 }
1028
1029 static  void    test_DebuggingFlag(void)
1030 {
1031     char                buffer[MAX_PATH];
1032     PROCESS_INFORMATION info;
1033     STARTUPINFOA       startup, us;
1034     DEBUG_EVENT         de;
1035     unsigned            dbg = 0;
1036
1037     /* let's start simplistic */
1038     memset(&startup, 0, sizeof(startup));
1039     startup.cb = sizeof(startup);
1040     startup.dwFlags = STARTF_USESHOWWINDOW;
1041     startup.wShowWindow = SW_SHOWNORMAL;
1042
1043     get_file_name(resfile);
1044     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1045     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1046
1047     /* get all startup events up to the entry point break exception */
1048     do 
1049     {
1050         ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
1051         ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
1052         if (de.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) dbg++;
1053     } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1054
1055     ok(dbg, "I have seen a debug event\n");
1056     /* wait for child to terminate */
1057     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1058     /* child process has changed result file, so let profile functions know about it */
1059     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1060
1061     GetStartupInfoA(&us);
1062
1063     okChildInt("StartupInfoA", "cb", startup.cb);
1064     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1065     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
1066     okChildInt("StartupInfoA", "dwX", startup.dwX);
1067     okChildInt("StartupInfoA", "dwY", startup.dwY);
1068     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1069     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1070     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1071     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1072     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1073     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1074     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1075     release_memory();
1076     assert(DeleteFileA(resfile) != 0);
1077 }
1078
1079 static BOOL is_console(HANDLE h)
1080 {
1081     return h != INVALID_HANDLE_VALUE && ((ULONG_PTR)h & 3) == 3;
1082 }
1083
1084 static void test_Console(void)
1085 {
1086     char                buffer[MAX_PATH];
1087     PROCESS_INFORMATION info;
1088     STARTUPINFOA       startup, us;
1089     SECURITY_ATTRIBUTES sa;
1090     CONSOLE_SCREEN_BUFFER_INFO  sbi, sbiC;
1091     DWORD               modeIn, modeOut, modeInC, modeOutC;
1092     DWORD               cpIn, cpOut, cpInC, cpOutC;
1093     DWORD               w;
1094     HANDLE              hChildIn, hChildInInh, hChildOut, hChildOutInh, hParentIn, hParentOut;
1095     const char*         msg = "This is a std-handle inheritance test.";
1096     unsigned            msg_len;
1097
1098     memset(&startup, 0, sizeof(startup));
1099     startup.cb = sizeof(startup);
1100     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1101     startup.wShowWindow = SW_SHOWNORMAL;
1102
1103     sa.nLength = sizeof(sa);
1104     sa.lpSecurityDescriptor = NULL;
1105     sa.bInheritHandle = TRUE;
1106
1107     startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1108     startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1109
1110     /* first, we need to be sure we're attached to a console */
1111     if (!is_console(startup.hStdInput) || !is_console(startup.hStdOutput))
1112     {
1113         /* we're not attached to a console, let's do it */
1114         AllocConsole();
1115         startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1116         startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1117     }
1118     /* now verify everything's ok */
1119     ok(startup.hStdInput != INVALID_HANDLE_VALUE, "Opening ConIn\n");
1120     ok(startup.hStdOutput != INVALID_HANDLE_VALUE, "Opening ConOut\n");
1121     startup.hStdError = startup.hStdOutput;
1122
1123     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbi), "Getting sb info\n");
1124     ok(GetConsoleMode(startup.hStdInput, &modeIn) && 
1125        GetConsoleMode(startup.hStdOutput, &modeOut), "Getting console modes\n");
1126     cpIn = GetConsoleCP();
1127     cpOut = GetConsoleOutputCP();
1128
1129     get_file_name(resfile);
1130     sprintf(buffer, "%s tests/process.c %s console", selfname, resfile);
1131     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1132
1133     /* wait for child to terminate */
1134     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1135     /* child process has changed result file, so let profile functions know about it */
1136     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1137
1138     /* now get the modification the child has made, and resets parents expected values */
1139     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbiC), "Getting sb info\n");
1140     ok(GetConsoleMode(startup.hStdInput, &modeInC) && 
1141        GetConsoleMode(startup.hStdOutput, &modeOutC), "Getting console modes\n");
1142
1143     SetConsoleMode(startup.hStdInput, modeIn);
1144     SetConsoleMode(startup.hStdOutput, modeOut);
1145
1146     cpInC = GetConsoleCP();
1147     cpOutC = GetConsoleOutputCP();
1148     SetConsoleCP(cpIn);
1149     SetConsoleOutputCP(cpOut);
1150
1151     GetStartupInfoA(&us);
1152
1153     okChildInt("StartupInfoA", "cb", startup.cb);
1154     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1155     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
1156     okChildInt("StartupInfoA", "dwX", startup.dwX);
1157     okChildInt("StartupInfoA", "dwY", startup.dwY);
1158     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1159     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1160     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1161     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1162     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1163     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1164     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1165
1166     /* check child correctly inherited the console */
1167     okChildInt("StartupInfoA", "hStdInput", (DWORD)startup.hStdInput);
1168     okChildInt("StartupInfoA", "hStdOutput", (DWORD)startup.hStdOutput);
1169     okChildInt("StartupInfoA", "hStdError", (DWORD)startup.hStdError);
1170     okChildInt("Console", "SizeX", (DWORD)sbi.dwSize.X);
1171     okChildInt("Console", "SizeY", (DWORD)sbi.dwSize.Y);
1172     okChildInt("Console", "CursorX", (DWORD)sbi.dwCursorPosition.X);
1173     okChildInt("Console", "CursorY", (DWORD)sbi.dwCursorPosition.Y);
1174     okChildInt("Console", "Attributes", sbi.wAttributes);
1175     okChildInt("Console", "winLeft", (DWORD)sbi.srWindow.Left);
1176     okChildInt("Console", "winTop", (DWORD)sbi.srWindow.Top);
1177     okChildInt("Console", "winRight", (DWORD)sbi.srWindow.Right);
1178     okChildInt("Console", "winBottom", (DWORD)sbi.srWindow.Bottom);
1179     okChildInt("Console", "maxWinWidth", (DWORD)sbi.dwMaximumWindowSize.X);
1180     okChildInt("Console", "maxWinHeight", (DWORD)sbi.dwMaximumWindowSize.Y);
1181     okChildInt("Console", "InputCP", cpIn);
1182     okChildInt("Console", "OutputCP", cpOut);
1183     okChildInt("Console", "InputMode", modeIn);
1184     okChildInt("Console", "OutputMode", modeOut);
1185
1186     todo_wine ok(cpInC == 1252, "Wrong console CP (expected 1252 got %d/%d)\n", cpInC, cpIn);
1187     todo_wine ok(cpOutC == 1252, "Wrong console-SB CP (expected 1252 got %d/%d)\n", cpOutC, cpOut);
1188     ok(modeInC == (modeIn ^ 1), "Wrong console mode\n");
1189     ok(modeOutC == (modeOut ^ 1), "Wrong console-SB mode\n");
1190     ok(sbiC.dwCursorPosition.X == (sbi.dwCursorPosition.X ^ 1), "Wrong cursor position\n");
1191     ok(sbiC.dwCursorPosition.Y == (sbi.dwCursorPosition.Y ^ 1), "Wrong cursor position\n");
1192
1193     release_memory();
1194     assert(DeleteFileA(resfile) != 0);
1195
1196     ok(CreatePipe(&hParentIn, &hChildOut, NULL, 0), "Creating parent-input pipe\n");
1197     ok(DuplicateHandle(GetCurrentProcess(), hChildOut, GetCurrentProcess(), 
1198                        &hChildOutInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1199        "Duplicating as inheritable child-output pipe\n");
1200     CloseHandle(hChildOut);
1201  
1202     ok(CreatePipe(&hChildIn, &hParentOut, NULL, 0), "Creating parent-output pipe\n");
1203     ok(DuplicateHandle(GetCurrentProcess(), hChildIn, GetCurrentProcess(), 
1204                        &hChildInInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1205        "Duplicating as inheritable child-input pipe\n");
1206     CloseHandle(hChildIn); 
1207     
1208     memset(&startup, 0, sizeof(startup));
1209     startup.cb = sizeof(startup);
1210     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1211     startup.wShowWindow = SW_SHOWNORMAL;
1212     startup.hStdInput = hChildInInh;
1213     startup.hStdOutput = hChildOutInh;
1214     startup.hStdError = hChildOutInh;
1215
1216     get_file_name(resfile);
1217     sprintf(buffer, "%s tests/process.c %s stdhandle", selfname, resfile);
1218     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1219     ok(CloseHandle(hChildInInh), "Closing handle\n");
1220     ok(CloseHandle(hChildOutInh), "Closing handle\n");
1221
1222     msg_len = strlen(msg) + 1;
1223     ok(WriteFile(hParentOut, msg, msg_len, &w, NULL), "Writing to child\n");
1224     ok(w == msg_len, "Should have written %u bytes, actually wrote %u\n", msg_len, w);
1225     memset(buffer, 0, sizeof(buffer));
1226     ok(ReadFile(hParentIn, buffer, sizeof(buffer), &w, NULL), "Reading from child\n");
1227     ok(strcmp(buffer, msg) == 0, "Should have received '%s'\n", msg);
1228
1229     /* wait for child to terminate */
1230     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1231     /* child process has changed result file, so let profile functions know about it */
1232     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1233
1234     okChildString("StdHandle", "msg", msg);
1235
1236     release_memory();
1237     assert(DeleteFileA(resfile) != 0);
1238 }
1239
1240 static  void    test_ExitCode(void)
1241 {
1242     char                buffer[MAX_PATH];
1243     PROCESS_INFORMATION info;
1244     STARTUPINFOA        startup;
1245     DWORD               code;
1246
1247     /* let's start simplistic */
1248     memset(&startup, 0, sizeof(startup));
1249     startup.cb = sizeof(startup);
1250     startup.dwFlags = STARTF_USESHOWWINDOW;
1251     startup.wShowWindow = SW_SHOWNORMAL;
1252
1253     get_file_name(resfile);
1254     sprintf(buffer, "%s tests/process.c %s exit_code", selfname, resfile);
1255     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1256
1257     /* wait for child to terminate */
1258     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1259     /* child process has changed result file, so let profile functions know about it */
1260     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1261
1262     ok(GetExitCodeProcess(info.hProcess, &code), "Getting exit code\n");
1263     okChildInt("ExitCode", "value", code);
1264
1265     release_memory();
1266     assert(DeleteFileA(resfile) != 0);
1267 }
1268
1269 static void test_OpenProcess(void)
1270 {
1271     HANDLE hproc;
1272     void *addr1;
1273     MEMORY_BASIC_INFORMATION info;
1274     SIZE_T dummy, read_bytes;
1275
1276     /* without PROCESS_VM_OPERATION */
1277     hproc = OpenProcess(PROCESS_ALL_ACCESS & ~PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1278     ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1279
1280     SetLastError(0xdeadbeef);
1281     addr1 = VirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1282 todo_wine {
1283     ok(!addr1, "VirtualAllocEx should fail\n");
1284     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1285 }
1286
1287     read_bytes = 0xdeadbeef;
1288     SetLastError(0xdeadbeef);
1289     ok(ReadProcessMemory(hproc, test_OpenProcess, &dummy, sizeof(dummy), &read_bytes),
1290        "ReadProcessMemory error %d\n", GetLastError());
1291     ok(read_bytes == sizeof(dummy), "wrong read bytes %ld\n", read_bytes);
1292
1293     CloseHandle(hproc);
1294
1295     hproc = OpenProcess(PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1296     ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1297
1298     addr1 = VirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1299 todo_wine {
1300     ok(addr1 != NULL, "VirtualAllocEx error %d\n", GetLastError());
1301 }
1302     if (addr1 == NULL) /* FIXME: remove once Wine is fixed */
1303         addr1 = VirtualAllocEx(GetCurrentProcess(), 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1304
1305     /* without PROCESS_QUERY_INFORMATION */
1306     SetLastError(0xdeadbeef);
1307     ok(!VirtualQueryEx(hproc, addr1, &info, sizeof(info)),
1308        "VirtualQueryEx without PROCESS_QUERY_INFORMATION rights should fail\n");
1309     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1310
1311     /* without PROCESS_VM_READ */
1312     read_bytes = 0xdeadbeef;
1313     SetLastError(0xdeadbeef);
1314     ok(!ReadProcessMemory(hproc, addr1, &dummy, sizeof(dummy), &read_bytes),
1315        "ReadProcessMemory without PROCESS_VM_READ rights should fail\n");
1316     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1317     ok(read_bytes == 0, "wrong read bytes %ld\n", read_bytes);
1318
1319     CloseHandle(hproc);
1320
1321     hproc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1322
1323     memset(&info, 0xaa, sizeof(info));
1324     ok(VirtualQueryEx(hproc, addr1, &info, sizeof(info)) == sizeof(info),
1325        "VirtualQueryEx error %d\n", GetLastError());
1326
1327     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
1328     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
1329     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
1330     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
1331     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
1332     /* NT reports Protect == 0 for a not committed memory block */
1333     ok(info.Protect == 0 /* NT */ ||
1334        info.Protect == PAGE_NOACCESS, /* Win9x */
1335         "%x != PAGE_NOACCESS\n", info.Protect);
1336     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
1337
1338     SetLastError(0xdeadbeef);
1339 todo_wine {
1340     ok(!VirtualFreeEx(hproc, addr1, 0, MEM_RELEASE),
1341        "VirtualFreeEx without PROCESS_VM_OPERATION rights should fail\n");
1342     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1343 }
1344
1345     CloseHandle(hproc);
1346
1347 todo_wine {
1348     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
1349 }
1350 }
1351
1352 START_TEST(process)
1353 {
1354     int b = init();
1355     ok(b, "Basic init of CreateProcess test\n");
1356     if (!b) return;
1357
1358     if (myARGC >= 3)
1359     {
1360         doChild(myARGV[2], (myARGC == 3) ? NULL : myARGV[3]);
1361         return;
1362     }
1363     test_Startup();
1364     test_CommandLine();
1365     test_Directory();
1366     test_Environment();
1367     test_SuspendFlag();
1368     test_DebuggingFlag();
1369     test_Console();
1370     test_ExitCode();
1371     test_OpenProcess();
1372     /* things that can be tested:
1373      *  lookup:         check the way program to be executed is searched
1374      *  handles:        check the handle inheritance stuff (+sec options)
1375      *  console:        check if console creation parameters work
1376      */
1377 }