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