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