Added a few more Unicode digits from Unicode version 4.1.
[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
816 static BOOL is_str_env_drive_dir(const char* str)
817 {
818     return str[0] == '=' && str[1] >= 'A' && str[1] <= 'Z' && str[2] == ':' &&
819         str[3] == '=' && str[4] == str[1];
820 }
821
822 /* compared expected child's environment (in gesA) from actual
823  * environment our child got
824  */
825 static void cmpEnvironment(const char* gesA)
826 {
827     int                 i, clen;
828     const char*         ptrA;
829     char*               res;
830     char                key[32];
831     BOOL                found;
832
833     clen = GetPrivateProfileIntA("EnvironmentA", "len", 0, resfile);
834     
835     /* now look each parent env in child */
836     if ((ptrA = gesA) != NULL)
837     {
838         while (*ptrA)
839         {
840             for (i = 0; i < clen; i++)
841             {
842                 sprintf(key, "env%d", i);
843                 res = getChildString("EnvironmentA", key);
844                 if (strncmp(ptrA, res, MAX_LISTED_ENV_VAR - 1) == 0)
845                     break;
846             }
847             found = i < clen;
848             ok(found, "Parent-env string %s isn't in child process\n", ptrA);
849             
850             ptrA += strlen(ptrA) + 1;
851             release_memory();
852         }
853     }
854     /* and each child env in parent */
855     for (i = 0; i < clen; i++)
856     {
857         sprintf(key, "env%d", i);
858         res = getChildString("EnvironmentA", key);
859         if ((ptrA = gesA) != NULL)
860         {
861             while (*ptrA)
862             {
863                 if (strncmp(res, ptrA, MAX_LISTED_ENV_VAR - 1) == 0)
864                     break;
865                 ptrA += strlen(ptrA) + 1;
866             }
867             if (!*ptrA) ptrA = NULL;
868         }
869
870         if (!is_str_env_drive_dir(res))
871         {
872             found = ptrA != NULL;
873             ok(found, "Child-env string %s isn't in parent process\n", res);
874         }
875         /* else => should also test we get the right per drive default directory here... */
876     }
877 }
878
879 static void test_Environment(void)
880 {
881     char                buffer[MAX_PATH];
882     PROCESS_INFORMATION info;
883     STARTUPINFOA        startup;
884     char*               child_env;
885     int                 child_env_len;
886     char*               ptr;
887     char*               env;
888     int                 slen;
889
890     memset(&startup, 0, sizeof(startup));
891     startup.cb = sizeof(startup);
892     startup.dwFlags = STARTF_USESHOWWINDOW;
893     startup.wShowWindow = SW_SHOWNORMAL;
894
895     /* the basics */
896     get_file_name(resfile);
897     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
898     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
899     /* wait for child to terminate */
900     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
901     /* child process has changed result file, so let profile functions know about it */
902     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
903     
904     cmpEnvironment(GetEnvironmentStringsA());
905     release_memory();
906     assert(DeleteFileA(resfile) != 0);
907
908     memset(&startup, 0, sizeof(startup));
909     startup.cb = sizeof(startup);
910     startup.dwFlags = STARTF_USESHOWWINDOW;
911     startup.wShowWindow = SW_SHOWNORMAL;
912
913     /* the basics */
914     get_file_name(resfile);
915     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
916     
917     child_env_len = 0;
918     ptr = GetEnvironmentStringsA();
919     while(*ptr)
920     {
921         slen = strlen(ptr)+1;
922         child_env_len += slen;
923         ptr += slen;
924     }
925     /* Add space for additional environment variables */
926     child_env_len += 256;
927     child_env = HeapAlloc(GetProcessHeap(), 0, child_env_len);
928     
929     ptr = child_env;
930     sprintf(ptr, "=%c:=%s", 'C', "C:\\FOO\\BAR");
931     ptr += strlen(ptr) + 1;
932     strcpy(ptr, "PATH=C:\\WINDOWS;C:\\WINDOWS\\SYSTEM;C:\\MY\\OWN\\DIR");
933     ptr += strlen(ptr) + 1;
934     strcpy(ptr, "FOO=BAR");
935     ptr += strlen(ptr) + 1;
936     strcpy(ptr, "BAR=FOOBAR");
937     ptr += strlen(ptr) + 1;
938     /* copy all existing variables except:
939      * - WINELOADER
940      * - PATH (already set above)
941      * - the directory definitions (=[A-Z]:=)
942      */
943     for (env = GetEnvironmentStringsA(); *env; env += strlen(env) + 1)
944     {
945         if (strncmp(env, "PATH=", 5) != 0 &&
946             strncmp(env, "WINELOADER=", 11) != 0 &&
947             !is_str_env_drive_dir(env))
948         {
949             strcpy(ptr, env);
950             ptr += strlen(ptr) + 1;
951         }
952     }
953     *ptr = '\0';
954     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, child_env, NULL, &startup, &info), "CreateProcess\n");
955     /* wait for child to terminate */
956     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
957     /* child process has changed result file, so let profile functions know about it */
958     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
959     
960     cmpEnvironment(child_env);
961
962     HeapFree(GetProcessHeap(), 0, child_env);
963     release_memory();
964     assert(DeleteFileA(resfile) != 0);
965 }
966
967 static  void    test_SuspendFlag(void)
968 {
969     char                buffer[MAX_PATH];
970     PROCESS_INFORMATION info;
971     STARTUPINFOA       startup, us;
972     DWORD               exit_status;
973
974     /* let's start simplistic */
975     memset(&startup, 0, sizeof(startup));
976     startup.cb = sizeof(startup);
977     startup.dwFlags = STARTF_USESHOWWINDOW;
978     startup.wShowWindow = SW_SHOWNORMAL;
979
980     get_file_name(resfile);
981     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
982     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &startup, &info), "CreateProcess\n");
983
984     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
985     Sleep(8000);
986     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
987     ok(ResumeThread(info.hThread) == 1, "Resuming thread\n");
988
989     /* wait for child to terminate */
990     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
991     /* child process has changed result file, so let profile functions know about it */
992     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
993
994     GetStartupInfoA(&us);
995
996     okChildInt("StartupInfoA", "cb", startup.cb);
997     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
998     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
999     okChildInt("StartupInfoA", "dwX", startup.dwX);
1000     okChildInt("StartupInfoA", "dwY", startup.dwY);
1001     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1002     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1003     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1004     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1005     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1006     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1007     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1008     release_memory();
1009     assert(DeleteFileA(resfile) != 0);
1010 }
1011
1012 static  void    test_DebuggingFlag(void)
1013 {
1014     char                buffer[MAX_PATH];
1015     PROCESS_INFORMATION info;
1016     STARTUPINFOA       startup, us;
1017     DEBUG_EVENT         de;
1018     unsigned            dbg = 0;
1019
1020     /* let's start simplistic */
1021     memset(&startup, 0, sizeof(startup));
1022     startup.cb = sizeof(startup);
1023     startup.dwFlags = STARTF_USESHOWWINDOW;
1024     startup.wShowWindow = SW_SHOWNORMAL;
1025
1026     get_file_name(resfile);
1027     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1028     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1029
1030     /* get all startup events up to the entry point break exception */
1031     do 
1032     {
1033         ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
1034         ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
1035         if (de.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) dbg++;
1036     } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1037
1038     ok(dbg, "I have seen a debug event\n");
1039     /* wait for child to terminate */
1040     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1041     /* child process has changed result file, so let profile functions know about it */
1042     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1043
1044     GetStartupInfoA(&us);
1045
1046     okChildInt("StartupInfoA", "cb", startup.cb);
1047     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1048     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
1049     okChildInt("StartupInfoA", "dwX", startup.dwX);
1050     okChildInt("StartupInfoA", "dwY", startup.dwY);
1051     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1052     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1053     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1054     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1055     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1056     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1057     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1058     release_memory();
1059     assert(DeleteFileA(resfile) != 0);
1060 }
1061
1062 static BOOL is_console(HANDLE h)
1063 {
1064     return h != INVALID_HANDLE_VALUE && ((ULONG_PTR)h & 3) == 3;
1065 }
1066
1067 static void test_Console(void)
1068 {
1069     char                buffer[MAX_PATH];
1070     PROCESS_INFORMATION info;
1071     STARTUPINFOA       startup, us;
1072     SECURITY_ATTRIBUTES sa;
1073     CONSOLE_SCREEN_BUFFER_INFO  sbi, sbiC;
1074     DWORD               modeIn, modeOut, modeInC, modeOutC;
1075     DWORD               cpIn, cpOut, cpInC, cpOutC;
1076     DWORD               w;
1077     HANDLE              hChildIn, hChildInInh, hChildOut, hChildOutInh, hParentIn, hParentOut;
1078     const char*         msg = "This is a std-handle inheritance test.";
1079     unsigned            msg_len;
1080
1081     memset(&startup, 0, sizeof(startup));
1082     startup.cb = sizeof(startup);
1083     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1084     startup.wShowWindow = SW_SHOWNORMAL;
1085
1086     sa.nLength = sizeof(sa);
1087     sa.lpSecurityDescriptor = NULL;
1088     sa.bInheritHandle = TRUE;
1089
1090     startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1091     startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1092
1093     /* first, we need to be sure we're attached to a console */
1094     if (!is_console(startup.hStdInput) || !is_console(startup.hStdOutput))
1095     {
1096         /* we're not attached to a console, let's do it */
1097         AllocConsole();
1098         startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1099         startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1100     }
1101     /* now verify everything's ok */
1102     ok(startup.hStdInput != INVALID_HANDLE_VALUE, "Opening ConIn\n");
1103     ok(startup.hStdOutput != INVALID_HANDLE_VALUE, "Opening ConOut\n");
1104     startup.hStdError = startup.hStdOutput;
1105
1106     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbi), "Getting sb info\n");
1107     ok(GetConsoleMode(startup.hStdInput, &modeIn) && 
1108        GetConsoleMode(startup.hStdOutput, &modeOut), "Getting console modes\n");
1109     cpIn = GetConsoleCP();
1110     cpOut = GetConsoleOutputCP();
1111
1112     get_file_name(resfile);
1113     sprintf(buffer, "%s tests/process.c %s console", selfname, resfile);
1114     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1115
1116     /* wait for child to terminate */
1117     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1118     /* child process has changed result file, so let profile functions know about it */
1119     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1120
1121     /* now get the modification the child has made, and resets parents expected values */
1122     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbiC), "Getting sb info\n");
1123     ok(GetConsoleMode(startup.hStdInput, &modeInC) && 
1124        GetConsoleMode(startup.hStdOutput, &modeOutC), "Getting console modes\n");
1125
1126     SetConsoleMode(startup.hStdInput, modeIn);
1127     SetConsoleMode(startup.hStdOutput, modeOut);
1128
1129     cpInC = GetConsoleCP();
1130     cpOutC = GetConsoleOutputCP();
1131     SetConsoleCP(cpIn);
1132     SetConsoleOutputCP(cpOut);
1133
1134     GetStartupInfoA(&us);
1135
1136     okChildInt("StartupInfoA", "cb", startup.cb);
1137     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1138     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
1139     okChildInt("StartupInfoA", "dwX", startup.dwX);
1140     okChildInt("StartupInfoA", "dwY", startup.dwY);
1141     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1142     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1143     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1144     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1145     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1146     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1147     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1148
1149     /* check child correctly inherited the console */
1150     okChildInt("StartupInfoA", "hStdInput", (DWORD)startup.hStdInput);
1151     okChildInt("StartupInfoA", "hStdOutput", (DWORD)startup.hStdOutput);
1152     okChildInt("StartupInfoA", "hStdError", (DWORD)startup.hStdError);
1153     okChildInt("Console", "SizeX", (DWORD)sbi.dwSize.X);
1154     okChildInt("Console", "SizeY", (DWORD)sbi.dwSize.Y);
1155     okChildInt("Console", "CursorX", (DWORD)sbi.dwCursorPosition.X);
1156     okChildInt("Console", "CursorY", (DWORD)sbi.dwCursorPosition.Y);
1157     okChildInt("Console", "Attributes", sbi.wAttributes);
1158     okChildInt("Console", "winLeft", (DWORD)sbi.srWindow.Left);
1159     okChildInt("Console", "winTop", (DWORD)sbi.srWindow.Top);
1160     okChildInt("Console", "winRight", (DWORD)sbi.srWindow.Right);
1161     okChildInt("Console", "winBottom", (DWORD)sbi.srWindow.Bottom);
1162     okChildInt("Console", "maxWinWidth", (DWORD)sbi.dwMaximumWindowSize.X);
1163     okChildInt("Console", "maxWinHeight", (DWORD)sbi.dwMaximumWindowSize.Y);
1164     okChildInt("Console", "InputCP", cpIn);
1165     okChildInt("Console", "OutputCP", cpOut);
1166     okChildInt("Console", "InputMode", modeIn);
1167     okChildInt("Console", "OutputMode", modeOut);
1168
1169     todo_wine ok(cpInC == 1252, "Wrong console CP (expected 1252 got %ld/%ld)\n", cpInC, cpIn);
1170     todo_wine ok(cpOutC == 1252, "Wrong console-SB CP (expected 1252 got %ld/%ld)\n", cpOutC, cpOut);
1171     ok(modeInC == (modeIn ^ 1), "Wrong console mode\n");
1172     ok(modeOutC == (modeOut ^ 1), "Wrong console-SB mode\n");
1173     ok(sbiC.dwCursorPosition.X == (sbi.dwCursorPosition.X ^ 1), "Wrong cursor position\n");
1174     ok(sbiC.dwCursorPosition.Y == (sbi.dwCursorPosition.Y ^ 1), "Wrong cursor position\n");
1175
1176     release_memory();
1177     assert(DeleteFileA(resfile) != 0);
1178
1179     ok(CreatePipe(&hParentIn, &hChildOut, NULL, 0), "Creating parent-input pipe\n");
1180     ok(DuplicateHandle(GetCurrentProcess(), hChildOut, GetCurrentProcess(), 
1181                        &hChildOutInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1182        "Duplicating as inheritable child-output pipe\n");
1183     CloseHandle(hChildOut);
1184  
1185     ok(CreatePipe(&hChildIn, &hParentOut, NULL, 0), "Creating parent-output pipe\n");
1186     ok(DuplicateHandle(GetCurrentProcess(), hChildIn, GetCurrentProcess(), 
1187                        &hChildInInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1188        "Duplicating as inheritable child-input pipe\n");
1189     CloseHandle(hChildIn); 
1190     
1191     memset(&startup, 0, sizeof(startup));
1192     startup.cb = sizeof(startup);
1193     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1194     startup.wShowWindow = SW_SHOWNORMAL;
1195     startup.hStdInput = hChildInInh;
1196     startup.hStdOutput = hChildOutInh;
1197     startup.hStdError = hChildOutInh;
1198
1199     get_file_name(resfile);
1200     sprintf(buffer, "%s tests/process.c %s stdhandle", selfname, resfile);
1201     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1202     ok(CloseHandle(hChildInInh), "Closing handle\n");
1203     ok(CloseHandle(hChildOutInh), "Closing handle\n");
1204
1205     msg_len = strlen(msg) + 1;
1206     ok(WriteFile(hParentOut, msg, msg_len, &w, NULL), "Writing to child\n");
1207     ok(w == msg_len, "Should have written %u bytes, actually wrote %lu\n", msg_len, w);
1208     memset(buffer, 0, sizeof(buffer));
1209     ok(ReadFile(hParentIn, buffer, sizeof(buffer), &w, NULL), "Reading from child\n");
1210     ok(strcmp(buffer, msg) == 0, "Should have received '%s'\n", msg);
1211
1212     /* wait for child to terminate */
1213     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1214     /* child process has changed result file, so let profile functions know about it */
1215     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1216
1217     okChildString("StdHandle", "msg", msg);
1218
1219     release_memory();
1220     assert(DeleteFileA(resfile) != 0);
1221 }
1222
1223 static  void    test_ExitCode(void)
1224 {
1225     char                buffer[MAX_PATH];
1226     PROCESS_INFORMATION info;
1227     STARTUPINFOA        startup;
1228     DWORD               code;
1229
1230     /* let's start simplistic */
1231     memset(&startup, 0, sizeof(startup));
1232     startup.cb = sizeof(startup);
1233     startup.dwFlags = STARTF_USESHOWWINDOW;
1234     startup.wShowWindow = SW_SHOWNORMAL;
1235
1236     get_file_name(resfile);
1237     sprintf(buffer, "%s tests/process.c %s exit_code", selfname, resfile);
1238     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1239
1240     /* wait for child to terminate */
1241     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1242     /* child process has changed result file, so let profile functions know about it */
1243     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1244
1245     ok(GetExitCodeProcess(info.hProcess, &code), "Getting exit code\n");
1246     okChildInt("ExitCode", "value", code);
1247
1248     release_memory();
1249     assert(DeleteFileA(resfile) != 0);
1250 }
1251
1252 START_TEST(process)
1253 {
1254     int b = init();
1255     ok(b, "Basic init of CreateProcess test\n");
1256     if (!b) return;
1257
1258     if (myARGC >= 3)
1259     {
1260         doChild(myARGV[2], (myARGC == 3) ? NULL : myARGV[3]);
1261         return;
1262     }
1263     test_Startup();
1264     test_CommandLine();
1265     test_Directory();
1266     test_Environment();
1267     test_SuspendFlag();
1268     test_DebuggingFlag();
1269     test_Console();
1270     test_ExitCode();
1271     /* things that can be tested:
1272      *  lookup:         check the way program to be executed is searched
1273      *  handles:        check the handle inheritance stuff (+sec options)
1274      *  console:        check if console creation parameters work
1275      */
1276 }