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