Added stubs for SendIMEMessageEx[A,W].
[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             strncpy(env_var, ptrA, MAX_LISTED_ENV_VAR - 1);
272             env_var[MAX_LISTED_ENV_VAR - 1] = '\0';
273             childPrintf(hFile, "env%d=%s\n", i, encodeA(env_var));
274             i++;
275             ptrA += strlen(ptrA) + 1;
276         }
277         childPrintf(hFile, "len=%d\n\n", i);
278     }
279
280     /* output of environment (Unicode) */
281     ptrW = GetEnvironmentStringsW();
282     if (ptrW)
283     {
284         WCHAR   env_var[MAX_LISTED_ENV_VAR];
285
286         childPrintf(hFile, "[EnvironmentW]\n");
287         i = 0;
288         while (*ptrW)
289         {
290             lstrcpynW(env_var, ptrW, MAX_LISTED_ENV_VAR - 1);
291             env_var[MAX_LISTED_ENV_VAR - 1] = '\0';
292             childPrintf(hFile, "env%d=%s\n", i, encodeW(env_var));
293             i++;
294             ptrW += lstrlenW(ptrW) + 1;
295         }
296         childPrintf(hFile, "len=%d\n\n", i);
297     }
298
299     childPrintf(hFile, "[Misc]\n");
300     if (GetCurrentDirectoryA(sizeof(bufA), bufA))
301         childPrintf(hFile, "CurrDirA=%s\n", encodeA(bufA));
302     if (GetCurrentDirectoryW(sizeof(bufW) / sizeof(bufW[0]), bufW))
303         childPrintf(hFile, "CurrDirW=%s\n", encodeW(bufW));
304     childPrintf(hFile, "\n");
305
306     if (option && strcmp(option, "console") == 0)
307     {
308         CONSOLE_SCREEN_BUFFER_INFO      sbi;
309         HANDLE hConIn  = GetStdHandle(STD_INPUT_HANDLE);
310         HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
311         DWORD modeIn, modeOut;
312
313         childPrintf(hFile, "[Console]\n");
314         if (GetConsoleScreenBufferInfo(hConOut, &sbi))
315         {
316             childPrintf(hFile, "SizeX=%d\nSizeY=%d\nCursorX=%d\nCursorY=%d\nAttributes=%d\n",
317                         sbi.dwSize.X, sbi.dwSize.Y, sbi.dwCursorPosition.X, sbi.dwCursorPosition.Y, sbi.wAttributes);
318             childPrintf(hFile, "winLeft=%d\nwinTop=%d\nwinRight=%d\nwinBottom=%d\n",
319                         sbi.srWindow.Left, sbi.srWindow.Top, sbi.srWindow.Right, sbi.srWindow.Bottom);
320             childPrintf(hFile, "maxWinWidth=%d\nmaxWinHeight=%d\n",
321                         sbi.dwMaximumWindowSize.X, sbi.dwMaximumWindowSize.Y);
322         }
323         childPrintf(hFile, "InputCP=%d\nOutputCP=%d\n",
324                     GetConsoleCP(), GetConsoleOutputCP());
325         if (GetConsoleMode(hConIn, &modeIn))
326             childPrintf(hFile, "InputMode=%ld\n", modeIn);
327         if (GetConsoleMode(hConOut, &modeOut))
328             childPrintf(hFile, "OutputMode=%ld\n", modeOut);
329
330         /* now that we have written all relevant information, let's change it */
331         ok(SetConsoleCP(1252), "Setting CP\n");
332         ok(SetConsoleOutputCP(1252), "Setting SB CP\n");
333         ret = SetConsoleMode(hConIn, modeIn ^ 1);
334         ok( ret, "Setting mode (%ld)\n", GetLastError());
335         ret = SetConsoleMode(hConOut, modeOut ^ 1);
336         ok( ret, "Setting mode (%ld)\n", GetLastError());
337         sbi.dwCursorPosition.X ^= 1;
338         sbi.dwCursorPosition.Y ^= 1;
339         ret = SetConsoleCursorPosition(hConOut, sbi.dwCursorPosition);
340         ok( ret, "Setting cursor position (%ld)\n", GetLastError());
341     }
342     if (option && strcmp(option, "stdhandle") == 0)
343     {
344         HANDLE hStdIn  = GetStdHandle(STD_INPUT_HANDLE);
345         HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
346
347         if (hStdIn != INVALID_HANDLE_VALUE || hStdOut != INVALID_HANDLE_VALUE)
348         {
349             char buf[1024];
350             DWORD r, w;
351
352             ok(ReadFile(hStdIn, buf, sizeof(buf), &r, NULL) && r > 0, "Reading message from input pipe\n");
353             childPrintf(hFile, "[StdHandle]\nmsg=%s\n\n", encodeA(buf));
354             ok(WriteFile(hStdOut, buf, r, &w, NULL) && w == r, "Writing message to output pipe\n");
355         }
356     }
357
358     if (option && strcmp(option, "exit_code") == 0)
359     {
360         childPrintf(hFile, "[ExitCode]\nvalue=%d\n\n", 123);
361         CloseHandle(hFile);
362         ExitProcess(123);
363     }
364
365     CloseHandle(hFile);
366 }
367
368 static char* getChildString(const char* sect, const char* key)
369 {
370     char        buf[1024+4*MAX_LISTED_ENV_VAR];
371     char*       ret;
372
373     GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), resfile);
374     if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
375     assert(!(strlen(buf) & 1));
376     ret = decodeA(buf);
377     return ret;
378 }
379
380 /* FIXME: this may be moved to the wtmain.c file, because it may be needed by
381  * others... (windows uses stricmp while Un*x uses strcasecmp...)
382  */
383 static int wtstrcasecmp(const char* p1, const char* p2)
384 {
385     char c1, c2;
386
387     c1 = c2 = '@';
388     while (c1 == c2 && c1)
389     {
390         c1 = *p1++; c2 = *p2++;
391         if (c1 != c2)
392         {
393             c1 = toupper(c1); c2 = toupper(c2);
394         }
395     }
396     return c1 - c2;
397 }
398
399 static int strCmp(const char* s1, const char* s2, BOOL sensitive)
400 {
401     if (!s1 && !s2) return 0;
402     if (!s2) return -1;
403     if (!s1) return 1;
404     return (sensitive) ? strcmp(s1, s2) : wtstrcasecmp(s1, s2);
405 }
406
407 #define okChildString(sect, key, expect) \
408     do { \
409         char* result = getChildString((sect), (key)); \
410         ok(strCmp(result, expect, 1) == 0, "%s:%s expected '%s', got '%s'\n", (sect), (key), (expect)?(expect):"(null)", result); \
411     } while (0)
412
413 #define okChildIString(sect, key, expect) \
414     do { \
415         char* result = getChildString(sect, key); \
416         ok(strCmp(result, expect, 0) == 0, "%s:%s expected '%s', got '%s'\n", sect, key, expect, result); \
417     } while (0)
418
419 /* using !expect insures that the test will fail if the sect/key isn't present
420  * in result file
421  */
422 #define okChildInt(sect, key, expect) \
423     do { \
424         UINT result = GetPrivateProfileIntA((sect), (key), !(expect), resfile); \
425         ok(result == expect, "%s:%s expected %d, but got %d\n", (sect), (key), (int)(expect), result); \
426    } while (0)
427
428 static void test_Startup(void)
429 {
430     char                buffer[MAX_PATH];
431     PROCESS_INFORMATION info;
432     STARTUPINFOA        startup,si;
433
434     /* let's start simplistic */
435     memset(&startup, 0, sizeof(startup));
436     startup.cb = sizeof(startup);
437     startup.dwFlags = STARTF_USESHOWWINDOW;
438     startup.wShowWindow = SW_SHOWNORMAL;
439
440     get_file_name(resfile);
441     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
442     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
443     /* wait for child to terminate */
444     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
445     /* child process has changed result file, so let profile functions know about it */
446     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
447
448     GetStartupInfoA(&si);
449     okChildInt("StartupInfoA", "cb", startup.cb);
450     okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
451     okChildString("StartupInfoA", "lpTitle", si.lpTitle);
452     okChildInt("StartupInfoA", "dwX", startup.dwX);
453     okChildInt("StartupInfoA", "dwY", startup.dwY);
454     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
455     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
456     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
457     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
458     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
459     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
460     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
461     release_memory();
462     assert(DeleteFileA(resfile) != 0);
463
464     /* not so simplistic now */
465     memset(&startup, 0, sizeof(startup));
466     startup.cb = sizeof(startup);
467     startup.dwFlags = STARTF_USESHOWWINDOW;
468     startup.wShowWindow = SW_SHOWNORMAL;
469     startup.lpTitle = "I'm the title string";
470     startup.lpDesktop = "I'm the desktop string";
471     startup.dwXCountChars = 0x12121212;
472     startup.dwYCountChars = 0x23232323;
473     startup.dwX = 0x34343434;
474     startup.dwY = 0x45454545;
475     startup.dwXSize = 0x56565656;
476     startup.dwYSize = 0x67676767;
477     startup.dwFillAttribute = 0xA55A;
478
479     get_file_name(resfile);
480     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
481     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
482     /* wait for child to terminate */
483     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
484     /* child process has changed result file, so let profile functions know about it */
485     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
486
487     okChildInt("StartupInfoA", "cb", startup.cb);
488     okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
489     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
490     okChildInt("StartupInfoA", "dwX", startup.dwX);
491     okChildInt("StartupInfoA", "dwY", startup.dwY);
492     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
493     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
494     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
495     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
496     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
497     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
498     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
499     release_memory();
500     assert(DeleteFileA(resfile) != 0);
501
502     /* not so simplistic now */
503     memset(&startup, 0, sizeof(startup));
504     startup.cb = sizeof(startup);
505     startup.dwFlags = STARTF_USESHOWWINDOW;
506     startup.wShowWindow = SW_SHOWNORMAL;
507     startup.lpTitle = "I'm the title string";
508     startup.lpDesktop = NULL;
509     startup.dwXCountChars = 0x12121212;
510     startup.dwYCountChars = 0x23232323;
511     startup.dwX = 0x34343434;
512     startup.dwY = 0x45454545;
513     startup.dwXSize = 0x56565656;
514     startup.dwYSize = 0x67676767;
515     startup.dwFillAttribute = 0xA55A;
516
517     get_file_name(resfile);
518     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
519     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
520     /* wait for child to terminate */
521     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
522     /* child process has changed result file, so let profile functions know about it */
523     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
524
525     okChildInt("StartupInfoA", "cb", startup.cb);
526     okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
527     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
528     okChildInt("StartupInfoA", "dwX", startup.dwX);
529     okChildInt("StartupInfoA", "dwY", startup.dwY);
530     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
531     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
532     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
533     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
534     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
535     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
536     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
537     release_memory();
538     assert(DeleteFileA(resfile) != 0);
539
540     /* not so simplistic now */
541     memset(&startup, 0, sizeof(startup));
542     startup.cb = sizeof(startup);
543     startup.dwFlags = STARTF_USESHOWWINDOW;
544     startup.wShowWindow = SW_SHOWNORMAL;
545     startup.lpTitle = "I'm the title string";
546     startup.lpDesktop = "";
547     startup.dwXCountChars = 0x12121212;
548     startup.dwYCountChars = 0x23232323;
549     startup.dwX = 0x34343434;
550     startup.dwY = 0x45454545;
551     startup.dwXSize = 0x56565656;
552     startup.dwYSize = 0x67676767;
553     startup.dwFillAttribute = 0xA55A;
554
555     get_file_name(resfile);
556     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
557     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
558     /* wait for child to terminate */
559     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
560     /* child process has changed result file, so let profile functions know about it */
561     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
562
563     okChildInt("StartupInfoA", "cb", startup.cb);
564     todo_wine okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
565     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
566     okChildInt("StartupInfoA", "dwX", startup.dwX);
567     okChildInt("StartupInfoA", "dwY", startup.dwY);
568     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
569     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
570     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
571     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
572     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
573     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
574     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
575     release_memory();
576     assert(DeleteFileA(resfile) != 0);
577
578     /* not so simplistic now */
579     memset(&startup, 0, sizeof(startup));
580     startup.cb = sizeof(startup);
581     startup.dwFlags = STARTF_USESHOWWINDOW;
582     startup.wShowWindow = SW_SHOWNORMAL;
583     startup.lpTitle = NULL;
584     startup.lpDesktop = "I'm the desktop string";
585     startup.dwXCountChars = 0x12121212;
586     startup.dwYCountChars = 0x23232323;
587     startup.dwX = 0x34343434;
588     startup.dwY = 0x45454545;
589     startup.dwXSize = 0x56565656;
590     startup.dwYSize = 0x67676767;
591     startup.dwFillAttribute = 0xA55A;
592
593     get_file_name(resfile);
594     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
595     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
596     /* wait for child to terminate */
597     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
598     /* child process has changed result file, so let profile functions know about it */
599     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
600
601     okChildInt("StartupInfoA", "cb", startup.cb);
602     okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
603     okChildString("StartupInfoA", "lpTitle", si.lpTitle);
604     okChildInt("StartupInfoA", "dwX", startup.dwX);
605     okChildInt("StartupInfoA", "dwY", startup.dwY);
606     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
607     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
608     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
609     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
610     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
611     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
612     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
613     release_memory();
614     assert(DeleteFileA(resfile) != 0);
615
616     /* not so simplistic now */
617     memset(&startup, 0, sizeof(startup));
618     startup.cb = sizeof(startup);
619     startup.dwFlags = STARTF_USESHOWWINDOW;
620     startup.wShowWindow = SW_SHOWNORMAL;
621     startup.lpTitle = "";
622     startup.lpDesktop = "I'm the desktop string";
623     startup.dwXCountChars = 0x12121212;
624     startup.dwYCountChars = 0x23232323;
625     startup.dwX = 0x34343434;
626     startup.dwY = 0x45454545;
627     startup.dwXSize = 0x56565656;
628     startup.dwYSize = 0x67676767;
629     startup.dwFillAttribute = 0xA55A;
630
631     get_file_name(resfile);
632     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
633     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
634     /* wait for child to terminate */
635     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
636     /* child process has changed result file, so let profile functions know about it */
637     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
638
639     okChildInt("StartupInfoA", "cb", startup.cb);
640     okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
641     todo_wine okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
642     okChildInt("StartupInfoA", "dwX", startup.dwX);
643     okChildInt("StartupInfoA", "dwY", startup.dwY);
644     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
645     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
646     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
647     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
648     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
649     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
650     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
651     release_memory();
652     assert(DeleteFileA(resfile) != 0);
653
654     /* not so simplistic now */
655     memset(&startup, 0, sizeof(startup));
656     startup.cb = sizeof(startup);
657     startup.dwFlags = STARTF_USESHOWWINDOW;
658     startup.wShowWindow = SW_SHOWNORMAL;
659     startup.lpTitle = "";
660     startup.lpDesktop = "";
661     startup.dwXCountChars = 0x12121212;
662     startup.dwYCountChars = 0x23232323;
663     startup.dwX = 0x34343434;
664     startup.dwY = 0x45454545;
665     startup.dwXSize = 0x56565656;
666     startup.dwYSize = 0x67676767;
667     startup.dwFillAttribute = 0xA55A;
668
669     get_file_name(resfile);
670     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
671     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
672     /* wait for child to terminate */
673     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
674     /* child process has changed result file, so let profile functions know about it */
675     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
676
677     okChildInt("StartupInfoA", "cb", startup.cb);
678     todo_wine okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
679     todo_wine okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
680     okChildInt("StartupInfoA", "dwX", startup.dwX);
681     okChildInt("StartupInfoA", "dwY", startup.dwY);
682     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
683     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
684     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
685     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
686     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
687     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
688     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
689     release_memory();
690     assert(DeleteFileA(resfile) != 0);
691
692     /* TODO: test for A/W and W/A and W/W */
693 }
694
695 static void test_CommandLine(void)
696 {
697     char                buffer[MAX_PATH];
698     PROCESS_INFORMATION info;
699     STARTUPINFOA        startup;
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
746 static void test_Directory(void)
747 {
748     char                buffer[MAX_PATH];
749     PROCESS_INFORMATION info;
750     STARTUPINFOA        startup;
751     char windir[MAX_PATH];
752
753     memset(&startup, 0, sizeof(startup));
754     startup.cb = sizeof(startup);
755     startup.dwFlags = STARTF_USESHOWWINDOW;
756     startup.wShowWindow = SW_SHOWNORMAL;
757
758     /* the basics */
759     get_file_name(resfile);
760     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
761     GetWindowsDirectoryA( windir, sizeof(windir) );
762     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, windir, &startup, &info), "CreateProcess\n");
763     /* wait for child to terminate */
764     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
765     /* child process has changed result file, so let profile functions know about it */
766     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
767
768     okChildIString("Misc", "CurrDirA", windir);
769     release_memory();
770     assert(DeleteFileA(resfile) != 0);
771 }
772
773 static BOOL is_str_env_drive_dir(const char* str)
774 {
775     return str[0] == '=' && str[1] >= 'A' && str[1] <= 'Z' && str[2] == ':' &&
776         str[3] == '=' && str[4] == str[1];
777 }
778
779 /* compared expected child's environment (in gesA) from actual
780  * environment our child got
781  */
782 static void cmpEnvironment(const char* gesA)
783 {
784     int                 i, clen;
785     const char*         ptrA;
786     char*               res;
787     char                key[32];
788     BOOL                found;
789
790     clen = GetPrivateProfileIntA("EnvironmentA", "len", 0, resfile);
791     
792     /* now look each parent env in child */
793     if ((ptrA = gesA) != NULL)
794     {
795         while (*ptrA)
796         {
797             for (i = 0; i < clen; i++)
798             {
799                 sprintf(key, "env%d", i);
800                 res = getChildString("EnvironmentA", key);
801                 if (strncmp(ptrA, res, MAX_LISTED_ENV_VAR - 1) == 0)
802                     break;
803             }
804             found = i < clen;
805             ok(found, "Parent-env string %s isn't in child process\n", ptrA);
806             
807             ptrA += strlen(ptrA) + 1;
808             release_memory();
809         }
810     }
811     /* and each child env in parent */
812     for (i = 0; i < clen; i++)
813     {
814         sprintf(key, "env%d", i);
815         res = getChildString("EnvironmentA", key);
816         if ((ptrA = gesA) != NULL)
817         {
818             while (*ptrA)
819             {
820                 if (strncmp(res, ptrA, MAX_LISTED_ENV_VAR - 1) == 0)
821                     break;
822                 ptrA += strlen(ptrA) + 1;
823             }
824             if (!*ptrA) ptrA = NULL;
825         }
826
827         if (!is_str_env_drive_dir(res))
828         {
829             found = ptrA != NULL;
830             ok(found, "Child-env string %s isn't in parent process\n", res);
831         }
832         /* else => should also test we get the right per drive default directory here... */
833     }
834 }
835
836 static void test_Environment(void)
837 {
838     char                buffer[MAX_PATH];
839     PROCESS_INFORMATION info;
840     STARTUPINFOA        startup;
841     char*               child_env;
842     int                 child_env_len;
843     char*               ptr;
844     char*               env;
845     int                 slen;
846
847     memset(&startup, 0, sizeof(startup));
848     startup.cb = sizeof(startup);
849     startup.dwFlags = STARTF_USESHOWWINDOW;
850     startup.wShowWindow = SW_SHOWNORMAL;
851
852     /* the basics */
853     get_file_name(resfile);
854     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
855     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
856     /* wait for child to terminate */
857     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
858     /* child process has changed result file, so let profile functions know about it */
859     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
860     
861     cmpEnvironment(GetEnvironmentStringsA());
862     release_memory();
863     assert(DeleteFileA(resfile) != 0);
864
865     memset(&startup, 0, sizeof(startup));
866     startup.cb = sizeof(startup);
867     startup.dwFlags = STARTF_USESHOWWINDOW;
868     startup.wShowWindow = SW_SHOWNORMAL;
869
870     /* the basics */
871     get_file_name(resfile);
872     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
873     
874     child_env_len = 0;
875     ptr = GetEnvironmentStringsA();
876     while(*ptr)
877     {
878         slen = strlen(ptr)+1;
879         child_env_len += slen;
880         ptr += slen;
881     }
882     /* Add space for additional environment variables */
883     child_env_len += 256;
884     child_env = HeapAlloc(GetProcessHeap(), 0, child_env_len);
885     
886     ptr = child_env;
887     sprintf(ptr, "=%c:=%s", 'C', "C:\\FOO\\BAR");
888     ptr += strlen(ptr) + 1;
889     strcpy(ptr, "PATH=C:\\WINDOWS;C:\\WINDOWS\\SYSTEM;C:\\MY\\OWN\\DIR");
890     ptr += strlen(ptr) + 1;
891     strcpy(ptr, "FOO=BAR");
892     ptr += strlen(ptr) + 1;
893     strcpy(ptr, "BAR=FOOBAR");
894     ptr += strlen(ptr) + 1;
895     /* copy all existing variables except:
896      * - WINELOADER
897      * - PATH (already set above)
898      * - the directory definitions (=[A-Z]:=)
899      */
900     for (env = GetEnvironmentStringsA(); *env; env += strlen(env) + 1)
901     {
902         if (strncmp(env, "PATH=", 5) != 0 &&
903             strncmp(env, "WINELOADER=", 11) != 0 &&
904             !is_str_env_drive_dir(env))
905         {
906             strcpy(ptr, env);
907             ptr += strlen(ptr) + 1;
908         }
909     }
910     *ptr = '\0';
911     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, child_env, NULL, &startup, &info), "CreateProcess\n");
912     /* wait for child to terminate */
913     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
914     /* child process has changed result file, so let profile functions know about it */
915     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
916     
917     cmpEnvironment(child_env);
918
919     HeapFree(GetProcessHeap(), 0, child_env);
920     release_memory();
921     assert(DeleteFileA(resfile) != 0);
922 }
923
924 static  void    test_SuspendFlag(void)
925 {
926     char                buffer[MAX_PATH];
927     PROCESS_INFORMATION info;
928     STARTUPINFOA       startup, us;
929     DWORD               exit_status;
930
931     /* let's start simplistic */
932     memset(&startup, 0, sizeof(startup));
933     startup.cb = sizeof(startup);
934     startup.dwFlags = STARTF_USESHOWWINDOW;
935     startup.wShowWindow = SW_SHOWNORMAL;
936
937     get_file_name(resfile);
938     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
939     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &startup, &info), "CreateProcess\n");
940
941     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
942     Sleep(8000);
943     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
944     ok(ResumeThread(info.hThread) == 1, "Resuming thread\n");
945
946     /* wait for child to terminate */
947     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
948     /* child process has changed result file, so let profile functions know about it */
949     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
950
951     GetStartupInfoA(&us);
952
953     okChildInt("StartupInfoA", "cb", startup.cb);
954     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
955     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
956     okChildInt("StartupInfoA", "dwX", startup.dwX);
957     okChildInt("StartupInfoA", "dwY", startup.dwY);
958     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
959     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
960     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
961     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
962     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
963     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
964     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
965     release_memory();
966     assert(DeleteFileA(resfile) != 0);
967 }
968
969 static  void    test_DebuggingFlag(void)
970 {
971     char                buffer[MAX_PATH];
972     PROCESS_INFORMATION info;
973     STARTUPINFOA       startup, us;
974     DEBUG_EVENT         de;
975     unsigned            dbg = 0;
976
977     /* let's start simplistic */
978     memset(&startup, 0, sizeof(startup));
979     startup.cb = sizeof(startup);
980     startup.dwFlags = STARTF_USESHOWWINDOW;
981     startup.wShowWindow = SW_SHOWNORMAL;
982
983     get_file_name(resfile);
984     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
985     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
986
987     /* get all startup events up to the entry point break exception */
988     do 
989     {
990         ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
991         ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
992         if (de.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) dbg++;
993     } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
994
995     ok(dbg, "I have seen a debug event\n");
996     /* wait for child to terminate */
997     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
998     /* child process has changed result file, so let profile functions know about it */
999     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1000
1001     GetStartupInfoA(&us);
1002
1003     okChildInt("StartupInfoA", "cb", startup.cb);
1004     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1005     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
1006     okChildInt("StartupInfoA", "dwX", startup.dwX);
1007     okChildInt("StartupInfoA", "dwY", startup.dwY);
1008     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1009     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1010     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1011     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1012     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1013     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1014     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1015     release_memory();
1016     assert(DeleteFileA(resfile) != 0);
1017 }
1018
1019 static void test_Console(void)
1020 {
1021     char                buffer[MAX_PATH];
1022     PROCESS_INFORMATION info;
1023     STARTUPINFOA       startup, us;
1024     SECURITY_ATTRIBUTES sa;
1025     CONSOLE_SCREEN_BUFFER_INFO  sbi, sbiC;
1026     DWORD               modeIn, modeOut, modeInC, modeOutC;
1027     DWORD               cpIn, cpOut, cpInC, cpOutC;
1028     DWORD               w;
1029     HANDLE              hChildIn, hChildInInh, hChildOut, hChildOutInh, hParentIn, hParentOut;
1030     const char*         msg = "This is a std-handle inheritance test.";
1031     unsigned            msg_len;
1032
1033     memset(&startup, 0, sizeof(startup));
1034     startup.cb = sizeof(startup);
1035     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1036     startup.wShowWindow = SW_SHOWNORMAL;
1037
1038     sa.nLength = sizeof(sa);
1039     sa.lpSecurityDescriptor = NULL;
1040     sa.bInheritHandle = TRUE;
1041
1042     startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1043     startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1044
1045     /* first, we need to be sure we're attached to a console */
1046     if (startup.hStdInput == INVALID_HANDLE_VALUE || startup.hStdOutput == INVALID_HANDLE_VALUE)
1047     {
1048         /* we're not attached to a console, let's do it */
1049         AllocConsole();
1050         startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1051         startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1052     }
1053     /* now verify everything's ok */
1054     ok(startup.hStdInput != INVALID_HANDLE_VALUE, "Opening ConIn\n");
1055     ok(startup.hStdOutput != INVALID_HANDLE_VALUE, "Opening ConOut\n");
1056     startup.hStdError = startup.hStdOutput;
1057
1058     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbi), "Getting sb info\n");
1059     ok(GetConsoleMode(startup.hStdInput, &modeIn) && 
1060        GetConsoleMode(startup.hStdOutput, &modeOut), "Getting console modes\n");
1061     cpIn = GetConsoleCP();
1062     cpOut = GetConsoleOutputCP();
1063
1064     get_file_name(resfile);
1065     sprintf(buffer, "%s tests/process.c %s console", selfname, resfile);
1066     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1067
1068     /* wait for child to terminate */
1069     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1070     /* child process has changed result file, so let profile functions know about it */
1071     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1072
1073     /* now get the modification the child has made, and resets parents expected values */
1074     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbiC), "Getting sb info\n");
1075     ok(GetConsoleMode(startup.hStdInput, &modeInC) && 
1076        GetConsoleMode(startup.hStdOutput, &modeOutC), "Getting console modes\n");
1077
1078     SetConsoleMode(startup.hStdInput, modeIn);
1079     SetConsoleMode(startup.hStdOutput, modeOut);
1080
1081     cpInC = GetConsoleCP();
1082     cpOutC = GetConsoleOutputCP();
1083     SetConsoleCP(cpIn);
1084     SetConsoleOutputCP(cpOut);
1085
1086     GetStartupInfoA(&us);
1087
1088     okChildInt("StartupInfoA", "cb", startup.cb);
1089     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1090     okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
1091     okChildInt("StartupInfoA", "dwX", startup.dwX);
1092     okChildInt("StartupInfoA", "dwY", startup.dwY);
1093     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1094     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1095     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1096     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1097     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1098     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1099     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1100
1101     /* check child correctly inherited the console */
1102     okChildInt("StartupInfoA", "hStdInput", (DWORD)startup.hStdInput);
1103     okChildInt("StartupInfoA", "hStdOutput", (DWORD)startup.hStdOutput);
1104     okChildInt("StartupInfoA", "hStdError", (DWORD)startup.hStdError);
1105     okChildInt("Console", "SizeX", (DWORD)sbi.dwSize.X);
1106     okChildInt("Console", "SizeY", (DWORD)sbi.dwSize.Y);
1107     okChildInt("Console", "CursorX", (DWORD)sbi.dwCursorPosition.X);
1108     okChildInt("Console", "CursorY", (DWORD)sbi.dwCursorPosition.Y);
1109     okChildInt("Console", "Attributes", sbi.wAttributes);
1110     okChildInt("Console", "winLeft", (DWORD)sbi.srWindow.Left);
1111     okChildInt("Console", "winTop", (DWORD)sbi.srWindow.Top);
1112     okChildInt("Console", "winRight", (DWORD)sbi.srWindow.Right);
1113     okChildInt("Console", "winBottom", (DWORD)sbi.srWindow.Bottom);
1114     okChildInt("Console", "maxWinWidth", (DWORD)sbi.dwMaximumWindowSize.X);
1115     okChildInt("Console", "maxWinHeight", (DWORD)sbi.dwMaximumWindowSize.Y);
1116     okChildInt("Console", "InputCP", cpIn);
1117     okChildInt("Console", "OutputCP", cpOut);
1118     okChildInt("Console", "InputMode", modeIn);
1119     okChildInt("Console", "OutputMode", modeOut);
1120
1121     todo_wine ok(cpInC == 1252, "Wrong console CP (expected 1252 got %ld/%ld)\n", cpInC, cpIn);
1122     todo_wine ok(cpOutC == 1252, "Wrong console-SB CP (expected 1252 got %ld/%ld)\n", cpOutC, cpOut);
1123     ok(modeInC == (modeIn ^ 1), "Wrong console mode\n");
1124     ok(modeOutC == (modeOut ^ 1), "Wrong console-SB mode\n");
1125     ok(sbiC.dwCursorPosition.X == (sbi.dwCursorPosition.X ^ 1), "Wrong cursor position\n");
1126     ok(sbiC.dwCursorPosition.Y == (sbi.dwCursorPosition.Y ^ 1), "Wrong cursor position\n");
1127
1128     release_memory();
1129     assert(DeleteFileA(resfile) != 0);
1130
1131     ok(CreatePipe(&hParentIn, &hChildOut, NULL, 0), "Creating parent-input pipe\n");
1132     ok(DuplicateHandle(GetCurrentProcess(), hChildOut, GetCurrentProcess(), 
1133                        &hChildOutInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1134        "Duplicating as inheritable child-output pipe\n");
1135     CloseHandle(hChildOut);
1136  
1137     ok(CreatePipe(&hChildIn, &hParentOut, NULL, 0), "Creating parent-output pipe\n");
1138     ok(DuplicateHandle(GetCurrentProcess(), hChildIn, GetCurrentProcess(), 
1139                        &hChildInInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1140        "Duplicating as inheritable child-input pipe\n");
1141     CloseHandle(hChildIn); 
1142     
1143     memset(&startup, 0, sizeof(startup));
1144     startup.cb = sizeof(startup);
1145     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1146     startup.wShowWindow = SW_SHOWNORMAL;
1147     startup.hStdInput = hChildInInh;
1148     startup.hStdOutput = hChildOutInh;
1149     startup.hStdError = hChildOutInh;
1150
1151     get_file_name(resfile);
1152     sprintf(buffer, "%s tests/process.c %s stdhandle", selfname, resfile);
1153     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1154     ok(CloseHandle(hChildInInh), "Closing handle\n");
1155     ok(CloseHandle(hChildOutInh), "Closing handle\n");
1156
1157     msg_len = strlen(msg) + 1;
1158     ok(WriteFile(hParentOut, msg, msg_len, &w, NULL), "Writing to child\n");
1159     ok(w == msg_len, "Should have written %u bytes, actually wrote %lu\n", msg_len, w);
1160     memset(buffer, 0, sizeof(buffer));
1161     ok(ReadFile(hParentIn, buffer, sizeof(buffer), &w, NULL), "Reading from child\n");
1162     ok(strcmp(buffer, msg) == 0, "Should have received '%s'\n", msg);
1163
1164     /* wait for child to terminate */
1165     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1166     /* child process has changed result file, so let profile functions know about it */
1167     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1168
1169     okChildString("StdHandle", "msg", msg);
1170
1171     release_memory();
1172     assert(DeleteFileA(resfile) != 0);
1173 }
1174
1175 static  void    test_ExitCode(void)
1176 {
1177     char                buffer[MAX_PATH];
1178     PROCESS_INFORMATION info;
1179     STARTUPINFOA        startup;
1180     DWORD               code;
1181
1182     /* let's start simplistic */
1183     memset(&startup, 0, sizeof(startup));
1184     startup.cb = sizeof(startup);
1185     startup.dwFlags = STARTF_USESHOWWINDOW;
1186     startup.wShowWindow = SW_SHOWNORMAL;
1187
1188     get_file_name(resfile);
1189     sprintf(buffer, "%s tests/process.c %s exit_code", selfname, resfile);
1190     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1191
1192     /* wait for child to terminate */
1193     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1194     /* child process has changed result file, so let profile functions know about it */
1195     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1196
1197     ok(GetExitCodeProcess(info.hProcess, &code), "Getting exit code\n");
1198     okChildInt("ExitCode", "value", code);
1199
1200     release_memory();
1201     assert(DeleteFileA(resfile) != 0);
1202 }
1203
1204 START_TEST(process)
1205 {
1206     int b = init();
1207     ok(b, "Basic init of CreateProcess test\n");
1208     if (!b) return;
1209
1210     if (myARGC >= 3)
1211     {
1212         doChild(myARGV[2], (myARGC == 3) ? NULL : myARGV[3]);
1213         return;
1214     }
1215     test_Startup();
1216     test_CommandLine();
1217     test_Directory();
1218     test_Environment();
1219     test_SuspendFlag();
1220     test_DebuggingFlag();
1221     test_Console();
1222     test_ExitCode();
1223     /* things that can be tested:
1224      *  lookup:         check the way program to be executed is searched
1225      *  handles:        check the handle inheritance stuff (+sec options)
1226      *  console:        check if console creation parameters work
1227      */
1228 }