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