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