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