9db12ce42a81813b6498c4317adb8c270c6ac02d
[reactos.git] / modules / rostests / winetests / shell32 / shlexec.c
1 /*
2 * Unit test of the ShellExecute function.
3 *
4 * Copyright 2005, 2016 Francois Gouget for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 /* TODO:
22 * - test the default verb selection
23 * - test selection of an alternate class
24 * - try running executables in more ways
25 * - try passing arguments to executables
26 * - ShellExecute("foo.shlexec") with no path should work if foo.shlexec is
27 * in the PATH
28 * - test associations that use %l, %L or "%1" instead of %1
29 * - ShellExecuteEx() also calls SetLastError() with meaningful values which
30 * we could check
31 */
32
33 /* Needed to get SEE_MASK_NOZONECHECKS with the PSDK */
34 #define NTDDI_WINXPSP1 0x05010100
35 #define NTDDI_VERSION NTDDI_WINXPSP1
36 #define _WIN32_WINNT 0x0501
37
38 #include <stdio.h>
39 #include <assert.h>
40
41 #include "wtypes.h"
42 #include "winbase.h"
43 #include "windef.h"
44 #include "shellapi.h"
45 #include "shlwapi.h"
46 #include "ddeml.h"
47 #include "wine/test.h"
48
49 #include "shell32_test.h"
50
51
52 static char argv0[MAX_PATH];
53 static int myARGC;
54 static char** myARGV;
55 static char tmpdir[MAX_PATH];
56 static char child_file[MAX_PATH];
57 static DLLVERSIONINFO dllver;
58 static BOOL skip_shlexec_tests = FALSE;
59 static BOOL skip_noassoc_tests = FALSE;
60 static HANDLE dde_ready_event;
61
62
63 /***
64 *
65 * Helpers to read from / write to the child process results file.
66 * (borrowed from dlls/kernel32/tests/process.c)
67 *
68 ***/
69
70 static const char* encodeA(const char* str)
71 {
72 static char encoded[2*1024+1];
73 char* ptr;
74 size_t len,i;
75
76 if (!str) return "";
77 len = strlen(str) + 1;
78 if (len >= sizeof(encoded)/2)
79 {
80 fprintf(stderr, "string is too long!\n");
81 assert(0);
82 }
83 ptr = encoded;
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 unsigned decode_char(char c)
91 {
92 if (c >= '0' && c <= '9') return c - '0';
93 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
94 assert(c >= 'A' && c <= 'F');
95 return c - 'A' + 10;
96 }
97
98 static char* decodeA(const char* str)
99 {
100 static char decoded[1024];
101 char* ptr;
102 size_t len,i;
103
104 len = strlen(str) / 2;
105 if (!len--) return NULL;
106 if (len >= sizeof(decoded))
107 {
108 fprintf(stderr, "string is too long!\n");
109 assert(0);
110 }
111 ptr = decoded;
112 for (i = 0; i < len; i++)
113 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
114 ptr[len] = '\0';
115 return ptr;
116 }
117
118 static void WINETEST_PRINTF_ATTR(2,3) childPrintf(HANDLE h, const char* fmt, ...)
119 {
120 va_list valist;
121 char buffer[1024];
122 DWORD w;
123
124 va_start(valist, fmt);
125 vsprintf(buffer, fmt, valist);
126 va_end(valist);
127 WriteFile(h, buffer, strlen(buffer), &w, NULL);
128 }
129
130 static char* getChildString(const char* sect, const char* key)
131 {
132 char buf[1024];
133 char* ret;
134
135 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
136 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
137 assert(!(strlen(buf) & 1));
138 ret = decodeA(buf);
139 return ret;
140 }
141
142
143 /***
144 *
145 * Child code
146 *
147 ***/
148
149 #define CHILD_DDE_TIMEOUT 2500
150 static DWORD ddeInst;
151 static HSZ hszTopic;
152 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
153 static BOOL post_quit_on_execute;
154
155 /* Handle DDE for doChild() and test_dde_default_app() */
156 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
157 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
158 ULONG_PTR dwData1, ULONG_PTR dwData2)
159 {
160 DWORD size = 0;
161
162 if (winetest_debug > 2)
163 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
164 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
165
166 switch (uType)
167 {
168 case XTYP_CONNECT:
169 if (!DdeCmpStringHandles(hsz1, hszTopic))
170 {
171 size = DdeQueryStringA(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
172 ok(size < MAX_PATH, "got size %d\n", size);
173 assert(size < MAX_PATH);
174 return (HDDEDATA)TRUE;
175 }
176 return (HDDEDATA)FALSE;
177
178 case XTYP_EXECUTE:
179 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0);
180 ok(size < MAX_PATH, "got size %d\n", size);
181 assert(size < MAX_PATH);
182 DdeFreeDataHandle(hData);
183 if (post_quit_on_execute)
184 PostQuitMessage(0);
185 return (HDDEDATA)DDE_FACK;
186
187 default:
188 return NULL;
189 }
190 }
191
192 static HANDLE hEvent;
193 static void init_event(const char* child_file)
194 {
195 char* event_name;
196 event_name=strrchr(child_file, '\\')+1;
197 hEvent=CreateEventA(NULL, FALSE, FALSE, event_name);
198 }
199
200 /*
201 * This is just to make sure the child won't run forever stuck in a
202 * GetMessage() loop when DDE fails for some reason.
203 */
204 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
205 {
206 trace("childTimeout called\n");
207
208 PostQuitMessage(0);
209 }
210
211 static void doChild(int argc, char** argv)
212 {
213 char *filename, buffer[MAX_PATH];
214 HANDLE hFile, map;
215 int i;
216 UINT_PTR timer;
217
218 filename=argv[2];
219 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
220 if (hFile == INVALID_HANDLE_VALUE)
221 return;
222
223 /* Arguments */
224 childPrintf(hFile, "[Child]\r\n");
225 if (winetest_debug > 2)
226 {
227 trace("cmdlineA='%s'\n", GetCommandLineA());
228 trace("argcA=%d\n", argc);
229 }
230 childPrintf(hFile, "cmdlineA=%s\r\n", encodeA(GetCommandLineA()));
231 childPrintf(hFile, "argcA=%d\r\n", argc);
232 for (i = 0; i < argc; i++)
233 {
234 if (winetest_debug > 2)
235 trace("argvA%d='%s'\n", i, argv[i]);
236 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
237 }
238 GetModuleFileNameA(GetModuleHandleA(NULL), buffer, sizeof(buffer));
239 childPrintf(hFile, "longPath=%s\r\n", encodeA(buffer));
240
241 /* Check environment variable inheritance */
242 *buffer = '\0';
243 SetLastError(0);
244 GetEnvironmentVariableA("ShlexecVar", buffer, sizeof(buffer));
245 childPrintf(hFile, "ShlexecVarLE=%d\r\n", GetLastError());
246 childPrintf(hFile, "ShlexecVar=%s\r\n", encodeA(buffer));
247
248 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
249 if (map != NULL)
250 {
251 HANDLE dde_ready;
252 char *shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
253 CloseHandle(map);
254 if (shared_block[0] != '\0' || shared_block[1] != '\0')
255 {
256 HDDEDATA hdde;
257 HSZ hszApplication;
258 MSG msg;
259 UINT rc;
260
261 post_quit_on_execute = TRUE;
262 ddeInst = 0;
263 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
264 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0);
265 ok(rc == DMLERR_NO_ERROR, "DdeInitializeA() returned %d\n", rc);
266 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
267 ok(hszApplication != NULL, "DdeCreateStringHandleA(%s) = NULL\n", shared_block);
268 shared_block += strlen(shared_block) + 1;
269 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
270 ok(hszTopic != NULL, "DdeCreateStringHandleA(%s) = NULL\n", shared_block);
271 hdde = DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER | DNS_FILTEROFF);
272 ok(hdde != NULL, "DdeNameService() failed le=%u\n", GetLastError());
273
274 timer = SetTimer(NULL, 0, CHILD_DDE_TIMEOUT, childTimeout);
275
276 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
277 SetEvent(dde_ready);
278 CloseHandle(dde_ready);
279
280 while (GetMessageA(&msg, NULL, 0, 0))
281 {
282 if (winetest_debug > 2)
283 trace("msg %d lParam=%ld wParam=%lu\n", msg.message, msg.lParam, msg.wParam);
284 DispatchMessageA(&msg);
285 }
286
287 Sleep(500);
288 KillTimer(NULL, timer);
289 hdde = DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
290 ok(hdde != NULL, "DdeNameService() failed le=%u\n", GetLastError());
291 ok(DdeFreeStringHandle(ddeInst, hszTopic), "DdeFreeStringHandle(topic)\n");
292 ok(DdeFreeStringHandle(ddeInst, hszApplication), "DdeFreeStringHandle(application)\n");
293 ok(DdeUninitialize(ddeInst), "DdeUninitialize() failed\n");
294 }
295 else
296 {
297 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
298 SetEvent(dde_ready);
299 CloseHandle(dde_ready);
300 }
301
302 UnmapViewOfFile(shared_block);
303
304 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
305 }
306
307 childPrintf(hFile, "Failures=%d\r\n", winetest_get_failures());
308 CloseHandle(hFile);
309
310 init_event(filename);
311 SetEvent(hEvent);
312 CloseHandle(hEvent);
313 }
314
315 static void dump_child_(const char* file, int line)
316 {
317 if (winetest_debug > 1)
318 {
319 char key[18];
320 char* str;
321 int i, c;
322
323 str=getChildString("Child", "cmdlineA");
324 trace_(file, line)("cmdlineA='%s'\n", str);
325 c=GetPrivateProfileIntA("Child", "argcA", -1, child_file);
326 trace_(file, line)("argcA=%d\n",c);
327 for (i=0;i<c;i++)
328 {
329 sprintf(key, "argvA%d", i);
330 str=getChildString("Child", key);
331 trace_(file, line)("%s='%s'\n", key, str);
332 }
333
334 c=GetPrivateProfileIntA("Child", "ShlexecVarLE", -1, child_file);
335 trace_(file, line)("ShlexecVarLE=%d\n", c);
336 str=getChildString("Child", "ShlexecVar");
337 trace_(file, line)("ShlexecVar='%s'\n", str);
338
339 c=GetPrivateProfileIntA("Child", "Failures", -1, child_file);
340 trace_(file, line)("Failures=%d\n", c);
341 }
342 }
343
344
345 /***
346 *
347 * Helpers to check the ShellExecute() / child process results.
348 *
349 ***/
350
351 static char shell_call[2048];
352 static void WINETEST_PRINTF_ATTR(2,3) _okShell(int condition, const char *msg, ...)
353 {
354 va_list valist;
355 char buffer[2048];
356
357 strcpy(buffer, shell_call);
358 strcat(buffer, " ");
359 va_start(valist, msg);
360 vsprintf(buffer+strlen(buffer), msg, valist);
361 va_end(valist);
362 winetest_ok(condition, "%s", buffer);
363 }
364 #define okShell_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : _okShell
365 #define okShell okShell_(__FILE__, __LINE__)
366
367 static char assoc_desc[2048];
368 static void reset_association_description(void)
369 {
370 *assoc_desc = '\0';
371 }
372
373 static void okChildString_(const char* file, int line, const char* key, const char* expected, const char* bad)
374 {
375 char* result;
376 result=getChildString("Child", key);
377 if (!result)
378 {
379 okShell_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
380 return;
381 }
382 okShell_(file, line)(lstrcmpiA(result, expected) == 0 ||
383 broken(lstrcmpiA(result, bad) == 0),
384 "%s expected '%s', got '%s'\n", key, expected, result);
385 }
386 #define okChildString(key, expected) okChildString_(__FILE__, __LINE__, (key), (expected), (expected))
387 #define okChildStringBroken(key, expected, broken) okChildString_(__FILE__, __LINE__, (key), (expected), (broken))
388
389 static int StrCmpPath(const char* s1, const char* s2)
390 {
391 if (!s1 && !s2) return 0;
392 if (!s2) return 1;
393 if (!s1) return -1;
394 while (*s1)
395 {
396 if (!*s2)
397 {
398 if (*s1=='.')
399 s1++;
400 return (*s1-*s2);
401 }
402 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
403 {
404 while (*s1=='/' || *s1=='\\')
405 s1++;
406 while (*s2=='/' || *s2=='\\')
407 s2++;
408 }
409 else if (toupper(*s1)==toupper(*s2))
410 {
411 s1++;
412 s2++;
413 }
414 else
415 {
416 return (*s1-*s2);
417 }
418 }
419 if (*s2=='.')
420 s2++;
421 if (*s2)
422 return -1;
423 return 0;
424 }
425
426 static void okChildPath_(const char* file, int line, const char* key, const char* expected)
427 {
428 char* result;
429 int equal, shortequal;
430 result=getChildString("Child", key);
431 if (!result)
432 {
433 okShell_(file,line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
434 return;
435 }
436 shortequal = FALSE;
437 equal = (StrCmpPath(result, expected) == 0);
438 if (!equal)
439 {
440 char altpath[MAX_PATH];
441 DWORD rc = GetLongPathNameA(expected, altpath, sizeof(altpath));
442 if (0 < rc && rc < sizeof(altpath))
443 equal = (StrCmpPath(result, altpath) == 0);
444 if (!equal)
445 {
446 rc = GetShortPathNameA(expected, altpath, sizeof(altpath));
447 if (0 < rc && rc < sizeof(altpath))
448 shortequal = (StrCmpPath(result, altpath) == 0);
449 }
450 }
451 okShell_(file,line)(equal || broken(shortequal) /* XP SP1 */,
452 "%s expected '%s', got '%s'\n", key, expected, result);
453 }
454 #define okChildPath(key, expected) okChildPath_(__FILE__, __LINE__, (key), (expected))
455
456 static void okChildInt_(const char* file, int line, const char* key, int expected)
457 {
458 INT result;
459 result=GetPrivateProfileIntA("Child", key, expected, child_file);
460 okShell_(file,line)(result == expected,
461 "%s expected %d, but got %d\n", key, expected, result);
462 }
463 #define okChildInt(key, expected) okChildInt_(__FILE__, __LINE__, (key), (expected))
464
465 static void okChildIntBroken_(const char* file, int line, const char* key, int expected)
466 {
467 INT result;
468 result=GetPrivateProfileIntA("Child", key, expected, child_file);
469 okShell_(file,line)(result == expected || broken(result != expected),
470 "%s expected %d, but got %d\n", key, expected, result);
471 }
472 #define okChildIntBroken(key, expected) okChildIntBroken_(__FILE__, __LINE__, (key), (expected))
473
474
475 /***
476 *
477 * ShellExecute wrappers
478 *
479 ***/
480
481 static void strcat_param(char* str, const char* name, const char* param)
482 {
483 if (param)
484 {
485 if (str[strlen(str)-1] == '"')
486 strcat(str, ", ");
487 strcat(str, name);
488 strcat(str, "=\"");
489 strcat(str, param);
490 strcat(str, "\"");
491 }
492 }
493
494 static int _todo_wait = 0;
495 #define todo_wait for (_todo_wait = 1; _todo_wait; _todo_wait = 0)
496
497 static int bad_shellexecute = 0;
498
499 static INT_PTR shell_execute_(const char* file, int line, LPCSTR verb, LPCSTR filename, LPCSTR parameters, LPCSTR directory)
500 {
501 INT_PTR rc, rcEmpty = 0;
502
503 if(!verb)
504 rcEmpty = shell_execute_(file, line, "", filename, parameters, directory);
505
506 strcpy(shell_call, "ShellExecute(");
507 strcat_param(shell_call, "verb", verb);
508 strcat_param(shell_call, "file", filename);
509 strcat_param(shell_call, "params", parameters);
510 strcat_param(shell_call, "dir", directory);
511 strcat(shell_call, ")");
512 strcat(shell_call, assoc_desc);
513 if (winetest_debug > 1)
514 trace_(file, line)("Called %s\n", shell_call);
515
516 DeleteFileA(child_file);
517 SetLastError(0xcafebabe);
518
519 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
520 * association it displays the 'Open With' dialog and I could not find
521 * a flag to prevent this.
522 */
523 rc=(INT_PTR)ShellExecuteA(NULL, verb, filename, parameters, directory, SW_HIDE);
524
525 if (rc > 32)
526 {
527 int wait_rc;
528 wait_rc=WaitForSingleObject(hEvent, 5000);
529 if (wait_rc == WAIT_TIMEOUT)
530 {
531 HWND wnd = FindWindowA("#32770", "Windows");
532 if (!wnd)
533 wnd = FindWindowA("Shell_Flyout", "");
534 if (wnd != NULL)
535 {
536 SendMessageA(wnd, WM_CLOSE, 0, 0);
537 win_skip("Skipping shellexecute of file with unassociated extension\n");
538 skip_noassoc_tests = TRUE;
539 rc = SE_ERR_NOASSOC;
540 }
541 }
542 todo_wine_if(_todo_wait)
543 okShell_(file, line)(wait_rc==WAIT_OBJECT_0 || rc <= 32,
544 "WaitForSingleObject returned %d\n", wait_rc);
545 }
546 /* The child process may have changed the result file, so let profile
547 * functions know about it
548 */
549 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
550 if (GetFileAttributesA(child_file) != INVALID_FILE_ATTRIBUTES)
551 {
552 int c;
553 dump_child_(file, line);
554 c = GetPrivateProfileIntA("Child", "Failures", -1, child_file);
555 if (c > 0)
556 winetest_add_failures(c);
557 okChildInt_(file, line, "ShlexecVarLE", 0);
558 okChildString_(file, line, "ShlexecVar", "Present", "Present");
559 }
560
561 if(!verb)
562 {
563 if (rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */
564 bad_shellexecute = 1;
565 okShell_(file, line)(rc == rcEmpty ||
566 broken(rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */,
567 "Got different return value with empty string: %lu %lu\n", rc, rcEmpty);
568 }
569
570 return rc;
571 }
572 #define shell_execute(verb, filename, parameters, directory) \
573 shell_execute_(__FILE__, __LINE__, verb, filename, parameters, directory)
574
575 static INT_PTR shell_execute_ex_(const char* file, int line,
576 DWORD mask, LPCSTR verb, LPCSTR filename,
577 LPCSTR parameters, LPCSTR directory,
578 LPCSTR class)
579 {
580 char smask[11];
581 SHELLEXECUTEINFOA sei;
582 BOOL success;
583 INT_PTR rc;
584
585 /* Add some flags so we can wait for the child process */
586 mask |= SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE;
587
588 strcpy(shell_call, "ShellExecuteEx(");
589 sprintf(smask, "0x%x", mask);
590 strcat_param(shell_call, "mask", smask);
591 strcat_param(shell_call, "verb", verb);
592 strcat_param(shell_call, "file", filename);
593 strcat_param(shell_call, "params", parameters);
594 strcat_param(shell_call, "dir", directory);
595 strcat_param(shell_call, "class", class);
596 strcat(shell_call, ")");
597 strcat(shell_call, assoc_desc);
598 if (winetest_debug > 1)
599 trace_(file, line)("Called %s\n", shell_call);
600
601 sei.cbSize=sizeof(sei);
602 sei.fMask=mask;
603 sei.hwnd=NULL;
604 sei.lpVerb=verb;
605 sei.lpFile=filename;
606 sei.lpParameters=parameters;
607 sei.lpDirectory=directory;
608 sei.nShow=SW_SHOWNORMAL;
609 sei.hInstApp=NULL; /* Out */
610 sei.lpIDList=NULL;
611 sei.lpClass=class;
612 sei.hkeyClass=NULL;
613 sei.dwHotKey=0;
614 U(sei).hIcon=NULL;
615 sei.hProcess=(HANDLE)0xdeadbeef; /* Out */
616
617 DeleteFileA(child_file);
618 SetLastError(0xcafebabe);
619 success=ShellExecuteExA(&sei);
620 rc=(INT_PTR)sei.hInstApp;
621 okShell_(file, line)((success && rc > 32) || (!success && rc <= 32),
622 "rc=%d and hInstApp=%ld is not allowed\n",
623 success, rc);
624
625 if (rc > 32)
626 {
627 DWORD wait_rc, rc;
628 if (sei.hProcess!=NULL)
629 {
630 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
631 okShell_(file, line)(wait_rc==WAIT_OBJECT_0,
632 "WaitForSingleObject(hProcess) returned %d\n",
633 wait_rc);
634 wait_rc = GetExitCodeProcess(sei.hProcess, &rc);
635 okShell_(file, line)(wait_rc, "GetExitCodeProcess() failed le=%u\n", GetLastError());
636 todo_wine_if(_todo_wait)
637 okShell_(file, line)(rc == 0, "child returned %u\n", rc);
638 CloseHandle(sei.hProcess);
639 }
640 wait_rc=WaitForSingleObject(hEvent, 5000);
641 todo_wine_if(_todo_wait)
642 okShell_(file, line)(wait_rc==WAIT_OBJECT_0,
643 "WaitForSingleObject returned %d\n", wait_rc);
644 }
645 else
646 okShell_(file, line)(sei.hProcess==NULL,
647 "returned a process handle %p\n", sei.hProcess);
648
649 /* The child process may have changed the result file, so let profile
650 * functions know about it
651 */
652 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
653 if (rc > 32 && GetFileAttributesA(child_file) != INVALID_FILE_ATTRIBUTES)
654 {
655 int c;
656 dump_child_(file, line);
657 c = GetPrivateProfileIntA("Child", "Failures", -1, child_file);
658 if (c > 0)
659 winetest_add_failures(c);
660 /* When NOZONECHECKS is specified the environment variables are not
661 * inherited if the process does not have elevated privileges.
662 */
663 if ((mask & SEE_MASK_NOZONECHECKS) && skip_shlexec_tests)
664 {
665 okChildInt_(file, line, "ShlexecVarLE", 203);
666 okChildString_(file, line, "ShlexecVar", "", "");
667 }
668 else
669 {
670 okChildInt_(file, line, "ShlexecVarLE", 0);
671 okChildString_(file, line, "ShlexecVar", "Present", "Present");
672 }
673 }
674
675 return rc;
676 }
677 #define shell_execute_ex(mask, verb, filename, parameters, directory, class) \
678 shell_execute_ex_(__FILE__, __LINE__, mask, verb, filename, parameters, directory, class)
679
680
681 /***
682 *
683 * Functions to create / delete associations wrappers
684 *
685 ***/
686
687 static BOOL create_test_class(const char* class, BOOL protocol)
688 {
689 HKEY hkey, hkey_shell;
690 LONG rc;
691
692 rc = RegCreateKeyExA(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
693 KEY_CREATE_SUB_KEY | KEY_SET_VALUE, NULL,
694 &hkey, NULL);
695 ok(rc == ERROR_SUCCESS || rc == ERROR_ACCESS_DENIED,
696 "could not create class %s (rc=%d)\n", class, rc);
697 if (rc != ERROR_SUCCESS)
698 return FALSE;
699
700 if (protocol)
701 {
702 rc = RegSetValueExA(hkey, "URL Protocol", 0, REG_SZ, (LPBYTE)"", 1);
703 ok(rc == ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
704 }
705
706 rc = RegCreateKeyExA(hkey, "shell", 0, NULL, 0,
707 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
708 ok(rc == ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
709
710 CloseHandle(hkey);
711 CloseHandle(hkey_shell);
712 return TRUE;
713 }
714
715 static BOOL create_test_association(const char* extension)
716 {
717 HKEY hkey;
718 char class[MAX_PATH];
719 LONG rc;
720
721 sprintf(class, "shlexec%s", extension);
722 rc=RegCreateKeyExA(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
723 NULL, &hkey, NULL);
724 ok(rc == ERROR_SUCCESS || rc == ERROR_ACCESS_DENIED,
725 "could not create association %s (rc=%d)\n", class, rc);
726 if (rc != ERROR_SUCCESS)
727 return FALSE;
728
729 rc=RegSetValueExA(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
730 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
731 CloseHandle(hkey);
732
733 return create_test_class(class, FALSE);
734 }
735
736 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
737 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
738 {
739 LONG ret;
740 DWORD dwMaxSubkeyLen, dwMaxValueLen;
741 DWORD dwMaxLen, dwSize;
742 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
743 HKEY hSubKey = hKey;
744
745 if(lpszSubKey)
746 {
747 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
748 if (ret) return ret;
749 }
750
751 /* Get highest length for keys, values */
752 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
753 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
754 if (ret) goto cleanup;
755
756 dwMaxSubkeyLen++;
757 dwMaxValueLen++;
758 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
759 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
760 {
761 /* Name too big: alloc a buffer for it */
762 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
763 {
764 ret = ERROR_NOT_ENOUGH_MEMORY;
765 goto cleanup;
766 }
767 }
768
769
770 /* Recursively delete all the subkeys */
771 while (TRUE)
772 {
773 dwSize = dwMaxLen;
774 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
775 NULL, NULL, NULL)) break;
776
777 ret = myRegDeleteTreeA(hSubKey, lpszName);
778 if (ret) goto cleanup;
779 }
780
781 if (lpszSubKey)
782 ret = RegDeleteKeyA(hKey, lpszSubKey);
783 else
784 while (TRUE)
785 {
786 dwSize = dwMaxLen;
787 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
788 NULL, NULL, NULL, NULL)) break;
789
790 ret = RegDeleteValueA(hKey, lpszName);
791 if (ret) goto cleanup;
792 }
793
794 cleanup:
795 /* Free buffer if allocated */
796 if (lpszName != szNameBuf)
797 HeapFree( GetProcessHeap(), 0, lpszName);
798 if(lpszSubKey)
799 RegCloseKey(hSubKey);
800 return ret;
801 }
802
803 static void delete_test_class(const char* classname)
804 {
805 myRegDeleteTreeA(HKEY_CLASSES_ROOT, classname);
806 }
807
808 static void delete_test_association(const char* extension)
809 {
810 char classname[MAX_PATH];
811
812 sprintf(classname, "shlexec%s", extension);
813 delete_test_class(classname);
814 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
815 }
816
817 static void create_test_verb_dde(const char* classname, const char* verb,
818 int rawcmd, const char* cmdtail, const char *ddeexec,
819 const char *application, const char *topic,
820 const char *ifexec)
821 {
822 HKEY hkey_shell, hkey_verb, hkey_cmd;
823 char shell[MAX_PATH];
824 char* cmd;
825 LONG rc;
826
827 strcpy(assoc_desc, " Assoc ");
828 strcat_param(assoc_desc, "class", classname);
829 strcat_param(assoc_desc, "verb", verb);
830 sprintf(shell, "%d", rawcmd);
831 strcat_param(assoc_desc, "rawcmd", shell);
832 strcat_param(assoc_desc, "cmdtail", cmdtail);
833 strcat_param(assoc_desc, "ddeexec", ddeexec);
834 strcat_param(assoc_desc, "app", application);
835 strcat_param(assoc_desc, "topic", topic);
836 strcat_param(assoc_desc, "ifexec", ifexec);
837
838 sprintf(shell, "%s\\shell", classname);
839 rc=RegOpenKeyExA(HKEY_CLASSES_ROOT, shell, 0,
840 KEY_CREATE_SUB_KEY, &hkey_shell);
841 ok(rc == ERROR_SUCCESS, "%s key creation failed with %d\n", shell, rc);
842
843 rc=RegCreateKeyExA(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
844 NULL, &hkey_verb, NULL);
845 ok(rc == ERROR_SUCCESS, "%s verb key creation failed with %d\n", verb, rc);
846
847 rc=RegCreateKeyExA(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
848 NULL, &hkey_cmd, NULL);
849 ok(rc == ERROR_SUCCESS, "\'command\' key creation failed with %d\n", rc);
850
851 if (rawcmd)
852 {
853 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
854 }
855 else
856 {
857 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
858 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
859 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
860 ok(rc == ERROR_SUCCESS, "setting command failed with %d\n", rc);
861 HeapFree(GetProcessHeap(), 0, cmd);
862 }
863
864 if (ddeexec)
865 {
866 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
867
868 rc=RegCreateKeyExA(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
869 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
870 ok(rc == ERROR_SUCCESS, "\'ddeexec\' key creation failed with %d\n", rc);
871 rc=RegSetValueExA(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
872 strlen(ddeexec)+1);
873 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
874
875 if (application)
876 {
877 rc=RegCreateKeyExA(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
878 NULL, &hkey_application, NULL);
879 ok(rc == ERROR_SUCCESS, "\'application\' key creation failed with %d\n", rc);
880
881 rc=RegSetValueExA(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
882 strlen(application)+1);
883 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
884 CloseHandle(hkey_application);
885 }
886 if (topic)
887 {
888 rc=RegCreateKeyExA(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
889 NULL, &hkey_topic, NULL);
890 ok(rc == ERROR_SUCCESS, "\'topic\' key creation failed with %d\n", rc);
891 rc=RegSetValueExA(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
892 strlen(topic)+1);
893 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
894 CloseHandle(hkey_topic);
895 }
896 if (ifexec)
897 {
898 rc=RegCreateKeyExA(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
899 NULL, &hkey_ifexec, NULL);
900 ok(rc == ERROR_SUCCESS, "\'ifexec\' key creation failed with %d\n", rc);
901 rc=RegSetValueExA(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
902 strlen(ifexec)+1);
903 ok(rc == ERROR_SUCCESS, "set value failed with %d\n", rc);
904 CloseHandle(hkey_ifexec);
905 }
906 CloseHandle(hkey_ddeexec);
907 }
908
909 CloseHandle(hkey_shell);
910 CloseHandle(hkey_verb);
911 CloseHandle(hkey_cmd);
912 }
913
914 /* Creates a class' non-DDE test verb.
915 * This function is meant to be used to create long term test verbs and thus
916 * does not trace them.
917 */
918 static void create_test_verb(const char* classname, const char* verb,
919 int rawcmd, const char* cmdtail)
920 {
921 create_test_verb_dde(classname, verb, rawcmd, cmdtail, NULL, NULL,
922 NULL, NULL);
923 reset_association_description();
924 }
925
926
927 /***
928 *
929 * GetLongPathNameA equivalent that supports Win95 and WinNT
930 *
931 ***/
932
933 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
934 {
935 char tmplongpath[MAX_PATH];
936 const char* p;
937 DWORD sp = 0, lp = 0;
938 DWORD tmplen;
939 WIN32_FIND_DATAA wfd;
940 HANDLE goit;
941
942 if (!shortpath || !shortpath[0])
943 return 0;
944
945 if (shortpath[1] == ':')
946 {
947 tmplongpath[0] = shortpath[0];
948 tmplongpath[1] = ':';
949 lp = sp = 2;
950 }
951
952 while (shortpath[sp])
953 {
954 /* check for path delimiters and reproduce them */
955 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
956 {
957 if (!lp || tmplongpath[lp-1] != '\\')
958 {
959 /* strip double "\\" */
960 tmplongpath[lp++] = '\\';
961 }
962 tmplongpath[lp] = 0; /* terminate string */
963 sp++;
964 continue;
965 }
966
967 p = shortpath + sp;
968 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
969 {
970 tmplongpath[lp++] = *p++;
971 tmplongpath[lp++] = *p++;
972 }
973 for (; *p && *p != '/' && *p != '\\'; p++);
974 tmplen = p - (shortpath + sp);
975 lstrcpynA(tmplongpath + lp, shortpath + sp, tmplen + 1);
976 /* Check if the file exists and use the existing file name */
977 goit = FindFirstFileA(tmplongpath, &wfd);
978 if (goit == INVALID_HANDLE_VALUE)
979 return 0;
980 FindClose(goit);
981 strcpy(tmplongpath + lp, wfd.cFileName);
982 lp += strlen(tmplongpath + lp);
983 sp += tmplen;
984 }
985 tmplen = strlen(shortpath) - 1;
986 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
987 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
988 tmplongpath[lp++] = shortpath[tmplen];
989 tmplongpath[lp] = 0;
990
991 tmplen = strlen(tmplongpath) + 1;
992 if (tmplen <= longlen)
993 {
994 strcpy(longpath, tmplongpath);
995 tmplen--; /* length without 0 */
996 }
997
998 return tmplen;
999 }
1000
1001
1002 /***
1003 *
1004 * Tests
1005 *
1006 ***/
1007
1008 static const char* testfiles[]=
1009 {
1010 "%s\\test file.shlexec",
1011 "%s\\%%nasty%% $file.shlexec",
1012 "%s\\test file.noassoc",
1013 "%s\\test file.noassoc.shlexec",
1014 "%s\\test file.shlexec.noassoc",
1015 "%s\\test_shortcut_shlexec.lnk",
1016 "%s\\test_shortcut_exe.lnk",
1017 "%s\\test file.shl",
1018 "%s\\test file.shlfoo",
1019 "%s\\test file.sha",
1020 "%s\\test file.sfe",
1021 "%s\\test file.shlproto",
1022 "%s\\masked file.shlexec",
1023 "%s\\masked",
1024 "%s\\test file.sde",
1025 "%s\\test file.exe",
1026 "%s\\test2.exe",
1027 "%s\\simple.shlexec",
1028 "%s\\drawback_file.noassoc",
1029 "%s\\drawback_file.noassoc foo.shlexec",
1030 "%s\\drawback_nonexist.noassoc foo.shlexec",
1031 NULL
1032 };
1033
1034 typedef struct
1035 {
1036 const char* verb;
1037 const char* basename;
1038 int todo;
1039 INT_PTR rc;
1040 } filename_tests_t;
1041
1042 static filename_tests_t filename_tests[]=
1043 {
1044 /* Test bad / nonexistent filenames */
1045 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
1046 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
1047
1048 /* Standard tests */
1049 {NULL, "%s\\test file.shlexec", 0x0, 33},
1050 {NULL, "%s\\test file.shlexec.", 0x0, 33},
1051 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
1052 {NULL, "%s/test file.shlexec", 0x0, 33},
1053
1054 /* Test filenames with no association */
1055 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
1056
1057 /* Test double extensions */
1058 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
1059 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
1060
1061 /* Test alternate verbs */
1062 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
1063 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
1064
1065 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
1066 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
1067
1068 {"notaverb", "%s\\test file.shlexec", 0x10, SE_ERR_NOASSOC},
1069
1070 {"averb", "%s\\test file.sha", 0x10, 33},
1071
1072 /* Test file masked due to space */
1073 {NULL, "%s\\masked file.shlexec", 0x0, 33},
1074 /* Test if quoting prevents the masking */
1075 {NULL, "%s\\masked file.shlexec", 0x40, 33},
1076 /* Test with incorrect quote */
1077 {NULL, "\"%s\\masked file.shlexec", 0x0, SE_ERR_FNF},
1078
1079 /* Test extension / URI protocol collision */
1080 {NULL, "%s\\test file.shlproto", 0x0, SE_ERR_NOASSOC},
1081
1082 {NULL, NULL, 0}
1083 };
1084
1085 static filename_tests_t noquotes_tests[]=
1086 {
1087 /* Test unquoted '%1' thingies */
1088 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
1089 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
1090 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
1091
1092 {NULL, NULL, 0}
1093 };
1094
1095 static void test_lpFile_parsed(void)
1096 {
1097 char fileA[MAX_PATH];
1098 INT_PTR rc;
1099
1100 if (skip_shlexec_tests)
1101 {
1102 skip("No filename parsing tests due to lack of .shlexec association\n");
1103 return;
1104 }
1105
1106 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
1107 sprintf(fileA, "%s\\drawback_file.noassoc foo.shlexec", tmpdir);
1108 rc=shell_execute(NULL, fileA, NULL, NULL);
1109 okShell(rc > 32, "failed: rc=%lu\n", rc);
1110
1111 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
1112 sprintf(fileA, "\"%s\\drawback_file.noassoc foo.shlexec\"", tmpdir);
1113 rc=shell_execute(NULL, fileA, NULL, NULL);
1114 okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
1115 "failed: rc=%lu\n", rc);
1116
1117 /* error should be SE_ERR_FNF, not SE_ERR_NOASSOC */
1118 sprintf(fileA, "\"%s\\drawback_file.noassoc\" foo.shlexec", tmpdir);
1119 rc=shell_execute(NULL, fileA, NULL, NULL);
1120 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
1121
1122 /* ""command"" not works on wine (and real win9x and w2k) */
1123 sprintf(fileA, "\"\"%s\\simple.shlexec\"\"", tmpdir);
1124 rc=shell_execute(NULL, fileA, NULL, NULL);
1125 todo_wine okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win9x/2000 */,
1126 "failed: rc=%lu\n", rc);
1127
1128 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
1129 sprintf(fileA, "%s\\drawback_nonexist.noassoc foo.shlexec", tmpdir);
1130 rc=shell_execute(NULL, fileA, NULL, NULL);
1131 okShell(rc > 32, "failed: rc=%lu\n", rc);
1132
1133 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
1134 rc=shell_execute(NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL);
1135 todo_wine okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
1136
1137 /* quoted */
1138 rc=shell_execute(NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL);
1139 todo_wine okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
1140
1141 /* test SEE_MASK_DOENVSUBST works */
1142 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1143 NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL, NULL);
1144 okShell(rc > 32, "failed: rc=%lu\n", rc);
1145
1146 /* quoted lpFile does not work on real win95 and nt4 */
1147 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1148 NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL, NULL);
1149 okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
1150 "failed: rc=%lu\n", rc);
1151 }
1152
1153 typedef struct
1154 {
1155 const char* cmd;
1156 const char* args[11];
1157 int todo;
1158 } cmdline_tests_t;
1159
1160 static const cmdline_tests_t cmdline_tests[] =
1161 {
1162 {"exe",
1163 {"exe", NULL}, 0},
1164
1165 {"exe arg1 arg2 \"arg three\" 'four five` six\\ $even)",
1166 {"exe", "arg1", "arg2", "arg three", "'four", "five`", "six\\", "$even)", NULL}, 0},
1167
1168 {"exe arg=1 arg-2 three\tfour\rfour\nfour ",
1169 {"exe", "arg=1", "arg-2", "three", "four\rfour\nfour", NULL}, 0},
1170
1171 {"exe arg\"one\" \"second\"arg thirdarg ",
1172 {"exe", "argone", "secondarg", "thirdarg", NULL}, 0},
1173
1174 /* Don't lose unclosed quoted arguments */
1175 {"exe arg1 \"unclosed",
1176 {"exe", "arg1", "unclosed", NULL}, 0},
1177
1178 {"exe arg1 \"",
1179 {"exe", "arg1", "", NULL}, 0},
1180
1181 /* cmd's metacharacters have no special meaning */
1182 {"exe \"one^\" \"arg\"&two three|four",
1183 {"exe", "one^", "arg&two", "three|four", NULL}, 0},
1184
1185 /* Environment variables are not interpreted either */
1186 {"exe %TMPDIR% %2",
1187 {"exe", "%TMPDIR%", "%2", NULL}, 0},
1188
1189 /* If not followed by a quote, backslashes go through as is */
1190 {"exe o\\ne t\\\\wo t\\\\\\ree f\\\\\\\\our ",
1191 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1192
1193 {"exe \"o\\ne\" \"t\\\\wo\" \"t\\\\\\ree\" \"f\\\\\\\\our\" ",
1194 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1195
1196 /* When followed by a quote their number is halved and the remainder
1197 * escapes the quote
1198 */
1199 {"exe \\\"one \\\\\"two\" \\\\\\\"three \\\\\\\\\"four\" end",
1200 {"exe", "\"one", "\\two", "\\\"three", "\\\\four", "end", NULL}, 0},
1201
1202 {"exe \"one\\\" still\" \"two\\\\\" \"three\\\\\\\" still\" \"four\\\\\\\\\" end",
1203 {"exe", "one\" still", "two\\", "three\\\" still", "four\\\\", "end", NULL}, 0},
1204
1205 /* One can put a quote in an unquoted string by tripling it, that is in
1206 * effect quoting it like so """ -> ". The general rule is as follows:
1207 * 3n quotes -> n quotes
1208 * 3n+1 quotes -> n quotes plus start of a quoted string
1209 * 3n+2 quotes -> n quotes (plus an empty string from the remaining pair)
1210 * Nicely, when n is 0 we get the standard rules back.
1211 */
1212 {"exe two\"\"quotes next",
1213 {"exe", "twoquotes", "next", NULL}, 0},
1214
1215 {"exe three\"\"\"quotes next",
1216 {"exe", "three\"quotes", "next", NULL}, 0},
1217
1218 {"exe four\"\"\"\" quotes\" next 4%3=1",
1219 {"exe", "four\" quotes", "next", "4%3=1", NULL}, 0},
1220
1221 {"exe five\"\"\"\"\"quotes next",
1222 {"exe", "five\"quotes", "next", NULL}, 0},
1223
1224 {"exe six\"\"\"\"\"\"quotes next",
1225 {"exe", "six\"\"quotes", "next", NULL}, 0},
1226
1227 {"exe seven\"\"\"\"\"\"\" quotes\" next 7%3=1",
1228 {"exe", "seven\"\" quotes", "next", "7%3=1", NULL}, 0},
1229
1230 {"exe twelve\"\"\"\"\"\"\"\"\"\"\"\"quotes next",
1231 {"exe", "twelve\"\"\"\"quotes", "next", NULL}, 0},
1232
1233 {"exe thirteen\"\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1234 {"exe", "thirteen\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1235
1236 /* Inside a quoted string the opening quote is added to the set of
1237 * consecutive quotes to get the effective quotes count. This gives:
1238 * 1+3n quotes -> n quotes
1239 * 1+3n+1 quotes -> n quotes plus closes the quoted string
1240 * 1+3n+2 quotes -> n+1 quotes plus closes the quoted string
1241 */
1242 {"exe \"two\"\"quotes next",
1243 {"exe", "two\"quotes", "next", NULL}, 0},
1244
1245 {"exe \"two\"\" next",
1246 {"exe", "two\"", "next", NULL}, 0},
1247
1248 {"exe \"three\"\"\" quotes\" next 4%3=1",
1249 {"exe", "three\" quotes", "next", "4%3=1", NULL}, 0},
1250
1251 {"exe \"four\"\"\"\"quotes next",
1252 {"exe", "four\"quotes", "next", NULL}, 0},
1253
1254 {"exe \"five\"\"\"\"\"quotes next",
1255 {"exe", "five\"\"quotes", "next", NULL}, 0},
1256
1257 {"exe \"six\"\"\"\"\"\" quotes\" next 7%3=1",
1258 {"exe", "six\"\" quotes", "next", "7%3=1", NULL}, 0},
1259
1260 {"exe \"eleven\"\"\"\"\"\"\"\"\"\"\"quotes next",
1261 {"exe", "eleven\"\"\"\"quotes", "next", NULL}, 0},
1262
1263 {"exe \"twelve\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1264 {"exe", "twelve\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1265
1266 /* Escaped consecutive quotes are fun */
1267 {"exe \"the crazy \\\\\"\"\"\\\\\" quotes",
1268 {"exe", "the crazy \\\"\\", "quotes", NULL}, 0},
1269
1270 /* The executable path has its own rules!!!
1271 * - Backslashes have no special meaning.
1272 * - If the first character is a quote, then the second quote ends the
1273 * executable path.
1274 * - The previous rule holds even if the next character is not a space!
1275 * - If the first character is not a quote, then quotes have no special
1276 * meaning either and the executable path stops at the first space.
1277 * - The consecutive quotes rules don't apply either.
1278 * - Even if there is no space between the executable path and the first
1279 * argument, the latter is parsed using the regular rules.
1280 */
1281 {"exe\"file\"path arg1",
1282 {"exe\"file\"path", "arg1", NULL}, 0},
1283
1284 {"exe\"file\"path\targ1",
1285 {"exe\"file\"path", "arg1", NULL}, 0},
1286
1287 {"exe\"path\\ arg1",
1288 {"exe\"path\\", "arg1", NULL}, 0},
1289
1290 {"\\\"exe \"arg one\"",
1291 {"\\\"exe", "arg one", NULL}, 0},
1292
1293 {"\"spaced exe\" \"next arg\"",
1294 {"spaced exe", "next arg", NULL}, 0},
1295
1296 {"\"spaced exe\"\t\"next arg\"",
1297 {"spaced exe", "next arg", NULL}, 0},
1298
1299 {"\"exe\"arg\" one\" argtwo",
1300 {"exe", "arg one", "argtwo", NULL}, 0},
1301
1302 {"\"spaced exe\\\"arg1 arg2",
1303 {"spaced exe\\", "arg1", "arg2", NULL}, 0},
1304
1305 {"\"two\"\" arg1 ",
1306 {"two", " arg1 ", NULL}, 0},
1307
1308 {"\"three\"\"\" arg2",
1309 {"three", "", "arg2", NULL}, 0},
1310
1311 {"\"four\"\"\"\"arg1",
1312 {"four", "\"arg1", NULL}, 0},
1313
1314 /* If the first character is a space then the executable path is empty */
1315 {" \"arg\"one argtwo",
1316 {"", "argone", "argtwo", NULL}, 0},
1317
1318 {NULL, {NULL}, 0}
1319 };
1320
1321 static BOOL test_one_cmdline(const cmdline_tests_t* test)
1322 {
1323 WCHAR cmdW[MAX_PATH], argW[MAX_PATH];
1324 LPWSTR *cl2a;
1325 int cl2a_count;
1326 LPWSTR *argsW;
1327 int i, count;
1328
1329 /* trace("----- cmd='%s'\n", test->cmd); */
1330 MultiByteToWideChar(CP_ACP, 0, test->cmd, -1, cmdW, sizeof(cmdW)/sizeof(*cmdW));
1331 argsW = cl2a = CommandLineToArgvW(cmdW, &cl2a_count);
1332 if (argsW == NULL && cl2a_count == -1)
1333 {
1334 win_skip("CommandLineToArgvW not implemented, skipping\n");
1335 return FALSE;
1336 }
1337 ok(!argsW[cl2a_count] || broken(argsW[cl2a_count] != NULL) /* before Vista */,
1338 "expected NULL-terminated list of commandline arguments\n");
1339
1340 count = 0;
1341 while (test->args[count])
1342 count++;
1343 todo_wine_if(test->todo & 0x1)
1344 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1345
1346 for (i = 0; i < cl2a_count; i++)
1347 {
1348 if (i < count)
1349 {
1350 MultiByteToWideChar(CP_ACP, 0, test->args[i], -1, argW, sizeof(argW)/sizeof(*argW));
1351 todo_wine_if(test->todo & (1 << (i+4)))
1352 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1353 }
1354 else todo_wine_if(test->todo & 0x1)
1355 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1356 argsW++;
1357 }
1358 LocalFree(cl2a);
1359 return TRUE;
1360 }
1361
1362 static void test_commandline2argv(void)
1363 {
1364 static const WCHAR exeW[] = {'e','x','e',0};
1365 const cmdline_tests_t* test;
1366 WCHAR strW[MAX_PATH];
1367 LPWSTR *args;
1368 int numargs;
1369 DWORD le;
1370
1371 test = cmdline_tests;
1372 while (test->cmd)
1373 {
1374 if (!test_one_cmdline(test))
1375 return;
1376 test++;
1377 }
1378
1379 SetLastError(0xdeadbeef);
1380 args = CommandLineToArgvW(exeW, NULL);
1381 le = GetLastError();
1382 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1383
1384 SetLastError(0xdeadbeef);
1385 args = CommandLineToArgvW(NULL, NULL);
1386 le = GetLastError();
1387 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1388
1389 *strW = 0;
1390 args = CommandLineToArgvW(strW, &numargs);
1391 ok(numargs == 1 || broken(numargs > 1), "expected 1 args, got %d\n", numargs);
1392 ok(!args || (!args[numargs] || broken(args[numargs] != NULL) /* before Vista */),
1393 "expected NULL-terminated list of commandline arguments\n");
1394 if (numargs == 1)
1395 {
1396 GetModuleFileNameW(NULL, strW, sizeof(strW)/sizeof(*strW));
1397 ok(!lstrcmpW(args[0], strW), "wrong path to the current executable: %s instead of %s\n", wine_dbgstr_w(args[0]), wine_dbgstr_w(strW));
1398 }
1399 if (args) LocalFree(args);
1400 }
1401
1402 /* The goal here is to analyze how ShellExecute() builds the command that
1403 * will be run. The tricky part is that there are three transformation
1404 * steps between the 'parameters' string we pass to ShellExecute() and the
1405 * argument list we observe in the child process:
1406 * - The parsing of 'parameters' string into individual arguments. The tests
1407 * show this is done differently from both CreateProcess() and
1408 * CommandLineToArgv()!
1409 * - The way the command 'formatting directives' such as %1, %2, etc are
1410 * handled.
1411 * - And the way the resulting command line is then parsed to yield the
1412 * argument list we check.
1413 */
1414 typedef struct
1415 {
1416 const char* verb;
1417 const char* params;
1418 int todo;
1419 cmdline_tests_t cmd;
1420 cmdline_tests_t broken;
1421 } argify_tests_t;
1422
1423 static const argify_tests_t argify_tests[] =
1424 {
1425 /* Start with three simple parameters. Notice that one can reorder and
1426 * duplicate the parameters. Also notice how %* take the raw input
1427 * parameters string, including the trailing spaces, no matter what
1428 * arguments have already been used.
1429 */
1430 {"Params232S", "p2 p3 p4 ", 0xc2,
1431 {" p2 p3 \"p2\" \"p2 p3 p4 \"",
1432 {"", "p2", "p3", "p2", "p2 p3 p4 ", NULL}, 0}},
1433
1434 /* Unquoted argument references like %2 don't automatically quote their
1435 * argument. Similarly, when they are quoted they don't escape the quotes
1436 * that their argument may contain.
1437 */
1438 {"Params232S", "\"p two\" p3 p4 ", 0x3f3,
1439 {" p two p3 \"p two\" \"\"p two\" p3 p4 \"",
1440 {"", "p", "two", "p3", "p two", "p", "two p3 p4 ", NULL}, 0}},
1441
1442 /* Only single digits are supported so only %1 to %9. Shown here with %20
1443 * because %10 is a pain.
1444 */
1445 {"Params20", "p", 0,
1446 {" \"p0\"",
1447 {"", "p0", NULL}, 0}},
1448
1449 /* Only (double-)quotes have a special meaning. */
1450 {"Params23456", "'p2 p3` p4\\ $even", 0x40,
1451 {" \"'p2\" \"p3`\" \"p4\\\" \"$even\" \"\"",
1452 {"", "'p2", "p3`", "p4\" $even \"", NULL}, 0}},
1453
1454 {"Params23456", "p=2 p-3 p4\tp4\rp4\np4", 0x1c2,
1455 {" \"p=2\" \"p-3\" \"p4\tp4\rp4\np4\" \"\" \"\"",
1456 {"", "p=2", "p-3", "p4\tp4\rp4\np4", "", "", NULL}, 0}},
1457
1458 /* In unquoted strings, quotes are treated are a parameter separator just
1459 * like spaces! However they can be doubled to get a literal quote.
1460 * Specifically:
1461 * 2n quotes -> n quotes
1462 * 2n+1 quotes -> n quotes and a parameter separator
1463 */
1464 {"Params23456789", "one\"quote \"p four\" one\"quote p7", 0xff3,
1465 {" \"one\" \"quote\" \"p four\" \"one\" \"quote\" \"p7\" \"\" \"\"",
1466 {"", "one", "quote", "p four", "one", "quote", "p7", "", "", NULL}, 0}},
1467
1468 {"Params23456789", "two\"\"quotes \"p three\" two\"\"quotes p5", 0xf2,
1469 {" \"two\"quotes\" \"p three\" \"two\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1470 {"", "twoquotes p", "three twoquotes", "p5", "", "", "", "", NULL}, 0}},
1471
1472 {"Params23456789", "three\"\"\"quotes \"p four\" three\"\"\"quotes p6", 0xff3,
1473 {" \"three\"\" \"quotes\" \"p four\" \"three\"\" \"quotes\" \"p6\" \"\" \"\"",
1474 {"", "three\"", "quotes", "p four", "three\"", "quotes", "p6", "", "", NULL}, 0}},
1475
1476 {"Params23456789", "four\"\"\"\"quotes \"p three\" four\"\"\"\"quotes p5", 0xf3,
1477 {" \"four\"\"quotes\" \"p three\" \"four\"\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1478 {"", "four\"quotes p", "three fourquotes p5 \"", "", "", "", NULL}, 0}},
1479
1480 /* Quoted strings cannot be continued by tacking on a non space character
1481 * either.
1482 */
1483 {"Params23456", "\"p two\"p3 \"p four\"p5 p6", 0x1f3,
1484 {" \"p two\" \"p3\" \"p four\" \"p5\" \"p6\"",
1485 {"", "p two", "p3", "p four", "p5", "p6", NULL}, 0}},
1486
1487 /* In quoted strings, the quotes are halved and an odd number closes the
1488 * string. Specifically:
1489 * 2n quotes -> n quotes
1490 * 2n+1 quotes -> n quotes and closes the string and hence the parameter
1491 */
1492 {"Params23456789", "\"one q\"uote \"p four\" \"one q\"uote p7", 0xff3,
1493 {" \"one q\" \"uote\" \"p four\" \"one q\" \"uote\" \"p7\" \"\" \"\"",
1494 {"", "one q", "uote", "p four", "one q", "uote", "p7", "", "", NULL}, 0}},
1495
1496 {"Params23456789", "\"two \"\" quotes\" \"p three\" \"two \"\" quotes\" p5", 0x1ff3,
1497 {" \"two \" quotes\" \"p three\" \"two \" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1498 {"", "two ", "quotes p", "three two", " quotes", "p5", "", "", "", "", NULL}, 0}},
1499
1500 {"Params23456789", "\"three q\"\"\"uotes \"p four\" \"three q\"\"\"uotes p7", 0xff3,
1501 {" \"three q\"\" \"uotes\" \"p four\" \"three q\"\" \"uotes\" \"p7\" \"\" \"\"",
1502 {"", "three q\"", "uotes", "p four", "three q\"", "uotes", "p7", "", "", NULL}, 0}},
1503
1504 {"Params23456789", "\"four \"\"\"\" quotes\" \"p three\" \"four \"\"\"\" quotes\" p5", 0xff3,
1505 {" \"four \"\" quotes\" \"p three\" \"four \"\" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1506 {"", "four \"", "quotes p", "three four", "", "quotes p5 \"", "", "", "", NULL}, 0}},
1507
1508 /* The quoted string rules also apply to consecutive quotes at the start
1509 * of a parameter but don't count the opening quote!
1510 */
1511 {"Params23456789", "\"\"twoquotes \"p four\" \"\"twoquotes p7", 0xbf3,
1512 {" \"\" \"twoquotes\" \"p four\" \"\" \"twoquotes\" \"p7\" \"\" \"\"",
1513 {"", "", "twoquotes", "p four", "", "twoquotes", "p7", "", "", NULL}, 0}},
1514
1515 {"Params23456789", "\"\"\"three quotes\" \"p three\" \"\"\"three quotes\" p5", 0x6f3,
1516 {" \"\"three quotes\" \"p three\" \"\"three quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1517 {"", "three", "quotes p", "three \"three", "quotes p5 \"", "", "", "", NULL}, 0}},
1518
1519 {"Params23456789", "\"\"\"\"fourquotes \"p four\" \"\"\"\"fourquotes p7", 0xbf3,
1520 {" \"\"\" \"fourquotes\" \"p four\" \"\"\" \"fourquotes\" \"p7\" \"\" \"\"",
1521 {"", "\"", "fourquotes", "p four", "\"", "fourquotes", "p7", "", "", NULL}, 0}},
1522
1523 /* An unclosed quoted string gets lost! */
1524 {"Params23456", "p2 \"p3\" \"p4 is lost", 0x1c3,
1525 {" \"p2\" \"p3\" \"\" \"\" \"\"",
1526 {"", "p2", "p3", "", "", "", NULL}, 0},
1527 {" \"p2\" \"p3\" \"p3\" \"\" \"\"",
1528 {"", "p2", "p3", "p3", "", "", NULL}, 0}},
1529
1530 /* Backslashes have no special meaning even when preceding quotes. All
1531 * they do is start an unquoted string.
1532 */
1533 {"Params23456", "\\\"p\\three \"pfour\\\" pfive", 0x73,
1534 {" \"\\\" \"p\\three\" \"pfour\\\" \"pfive\" \"\"",
1535 {"", "\" p\\three pfour\"", "pfive", "", NULL}, 0}},
1536
1537 /* Environment variables are left untouched. */
1538 {"Params23456", "%TMPDIR% %t %c", 0,
1539 {" \"%TMPDIR%\" \"%t\" \"%c\" \"\" \"\"",
1540 {"", "%TMPDIR%", "%t", "%c", "", "", NULL}, 0}},
1541
1542 /* %~2 is equivalent to %*. However %~3 and higher include the spaces
1543 * before the parameter!
1544 * (but not the previous parameter's closing quote fortunately)
1545 */
1546 {"Params2345Etc", "p2 p3 \"p4\" p5 p6 ", 0x3f3,
1547 {" ~2=\"p2 p3 \"p4\" p5 p6 \" ~3=\" p3 \"p4\" p5 p6 \" ~4=\" \"p4\" p5 p6 \" ~5= p5 p6 ",
1548 {"", "~2=p2 p3 p4 p5 p6 ", "~3= p3 p4 p5 p6 ", "~4= p4 p5 p6 ", "~5=", "p5", "p6", NULL}, 0}},
1549
1550 /* %~n works even if there is no nth parameter. */
1551 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 ", 0x12,
1552 {" ~9=\" \"",
1553 {"", "~9= ", NULL}, 0}},
1554
1555 {"Params9Etc", "p2 p3 p4 p5 p6 p7 ", 0x12,
1556 {" ~9=\"\"",
1557 {"", "~9=", NULL}, 0}},
1558
1559 /* The %~n directives also transmit the tenth parameter and beyond. */
1560 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 and beyond!", 0x12,
1561 {" ~9=\" p9 p10 p11 and beyond!\"",
1562 {"", "~9= p9 p10 p11 and beyond!", NULL}, 0}},
1563
1564 /* Bad formatting directives lose their % sign, except those followed by
1565 * a tilde! Environment variables are not expanded but lose their % sign.
1566 */
1567 {"ParamsBad", "p2 p3 p4 p5", 0x12,
1568 {" \"% - %~ %~0 %~1 %~a %~* a b c TMPDIR\"",
1569 {"", "% - %~ %~0 %~1 %~a %~* a b c TMPDIR", NULL}, 0}},
1570
1571 {NULL, NULL, 0, {NULL, {NULL}, 0}}
1572 };
1573
1574 static void test_argify(void)
1575 {
1576 BOOL has_cl2a = TRUE;
1577 char fileA[MAX_PATH], params[2*MAX_PATH+12];
1578 INT_PTR rc;
1579 const argify_tests_t* test;
1580 const cmdline_tests_t *bad;
1581 const char* cmd;
1582 unsigned i, count;
1583
1584 /* Test with a long parameter */
1585 for (rc = 0; rc < MAX_PATH; rc++)
1586 fileA[rc] = 'a' + rc % 26;
1587 fileA[MAX_PATH-1] = '\0';
1588 sprintf(params, "shlexec \"%s\" %s", child_file, fileA);
1589
1590 /* We need NOZONECHECKS on Win2003 to block a dialog */
1591 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params, NULL, NULL);
1592 okShell(rc > 32, "failed: rc=%lu\n", rc);
1593 okChildInt("argcA", 4);
1594 okChildPath("argvA3", fileA);
1595
1596 if (skip_shlexec_tests)
1597 {
1598 skip("No argify tests due to lack of .shlexec association\n");
1599 return;
1600 }
1601
1602 create_test_verb("shlexec.shlexec", "Params232S", 0, "Params232S %2 %3 \"%2\" \"%*\"");
1603 create_test_verb("shlexec.shlexec", "Params23456", 0, "Params23456 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\"");
1604 create_test_verb("shlexec.shlexec", "Params23456789", 0, "Params23456789 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\"");
1605 create_test_verb("shlexec.shlexec", "Params2345Etc", 0, "Params2345Etc ~2=\"%~2\" ~3=\"%~3\" ~4=\"%~4\" ~5=%~5");
1606 create_test_verb("shlexec.shlexec", "Params9Etc", 0, "Params9Etc ~9=\"%~9\"");
1607 create_test_verb("shlexec.shlexec", "Params20", 0, "Params20 \"%20\"");
1608 create_test_verb("shlexec.shlexec", "ParamsBad", 0, "ParamsBad \"%% %- %~ %~0 %~1 %~a %~* %a %b %c %TMPDIR%\"");
1609
1610 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1611
1612 test = argify_tests;
1613 while (test->params)
1614 {
1615 bad = test->broken.cmd ? &test->broken : &test->cmd;
1616
1617 /* trace("***** verb='%s' params='%s'\n", test->verb, test->params); */
1618 rc = shell_execute_ex(SEE_MASK_DOENVSUBST, test->verb, fileA, test->params, NULL, NULL);
1619 okShell(rc > 32, "failed: rc=%lu\n", rc);
1620
1621 count = 0;
1622 while (test->cmd.args[count])
1623 count++;
1624 /* +4 for the shlexec arguments, -1 because of the added ""
1625 * argument for the CommandLineToArgvW() tests.
1626 */
1627 todo_wine_if(test->todo & 0x1)
1628 okChildInt("argcA", 4 + count - 1);
1629
1630 cmd = getChildString("Child", "cmdlineA");
1631 /* Our commands are such that the verb immediately precedes the
1632 * part we are interested in.
1633 */
1634 if (cmd) cmd = strstr(cmd, test->verb);
1635 if (cmd) cmd += strlen(test->verb);
1636 if (!cmd) cmd = "(null)";
1637 todo_wine_if(test->todo & 0x2)
1638 okShell(!strcmp(cmd, test->cmd.cmd) || broken(!strcmp(cmd, bad->cmd)),
1639 "the cmdline is '%s' instead of '%s'\n", cmd, test->cmd.cmd);
1640
1641 for (i = 0; i < count - 1; i++)
1642 {
1643 char argname[18];
1644 sprintf(argname, "argvA%d", 4 + i);
1645 todo_wine_if(test->todo & (1 << (i+4)))
1646 okChildStringBroken(argname, test->cmd.args[i+1], bad->args[i+1]);
1647 }
1648
1649 if (has_cl2a)
1650 has_cl2a = test_one_cmdline(&(test->cmd));
1651 test++;
1652 }
1653 }
1654
1655 static void test_filename(void)
1656 {
1657 char filename[MAX_PATH];
1658 const filename_tests_t* test;
1659 char* c;
1660 INT_PTR rc;
1661
1662 if (skip_shlexec_tests)
1663 {
1664 skip("No ShellExecute/filename tests due to lack of .shlexec association\n");
1665 return;
1666 }
1667
1668 test=filename_tests;
1669 while (test->basename)
1670 {
1671 BOOL quotedfile = FALSE;
1672
1673 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1674 {
1675 win_skip("Skipping shellexecute of file with unassociated extension\n");
1676 test++;
1677 continue;
1678 }
1679
1680 sprintf(filename, test->basename, tmpdir);
1681 if (strchr(filename, '/'))
1682 {
1683 c=filename;
1684 while (*c)
1685 {
1686 if (*c=='\\')
1687 *c='/';
1688 c++;
1689 }
1690 }
1691 if ((test->todo & 0x40)==0)
1692 {
1693 rc=shell_execute(test->verb, filename, NULL, NULL);
1694 }
1695 else
1696 {
1697 char quoted[MAX_PATH + 2];
1698
1699 quotedfile = TRUE;
1700 sprintf(quoted, "\"%s\"", filename);
1701 rc=shell_execute(test->verb, quoted, NULL, NULL);
1702 }
1703 if (rc > 32)
1704 rc=33;
1705 okShell(rc==test->rc ||
1706 broken(quotedfile && rc == SE_ERR_FNF), /* NT4 */
1707 "failed: rc=%ld err=%u\n", rc, GetLastError());
1708 if (rc == 33)
1709 {
1710 const char* verb;
1711 todo_wine_if(test->todo & 0x2)
1712 okChildInt("argcA", 5);
1713 verb=(test->verb ? test->verb : "Open");
1714 todo_wine_if(test->todo & 0x4)
1715 okChildString("argvA3", verb);
1716 todo_wine_if(test->todo & 0x8)
1717 okChildPath("argvA4", filename);
1718 }
1719 test++;
1720 }
1721
1722 test=noquotes_tests;
1723 while (test->basename)
1724 {
1725 sprintf(filename, test->basename, tmpdir);
1726 rc=shell_execute(test->verb, filename, NULL, NULL);
1727 if (rc > 32)
1728 rc=33;
1729 todo_wine_if(test->todo & 0x1)
1730 okShell(rc==test->rc, "failed: rc=%ld err=%u\n", rc, GetLastError());
1731 if (rc==0)
1732 {
1733 int count;
1734 const char* verb;
1735 char* str;
1736
1737 verb=(test->verb ? test->verb : "Open");
1738 todo_wine_if(test->todo & 0x4)
1739 okChildString("argvA3", verb);
1740
1741 count=4;
1742 str=filename;
1743 while (1)
1744 {
1745 char attrib[18];
1746 char* space;
1747 space=strchr(str, ' ');
1748 if (space)
1749 *space='\0';
1750 sprintf(attrib, "argvA%d", count);
1751 todo_wine_if(test->todo & 0x8)
1752 okChildPath(attrib, str);
1753 count++;
1754 if (!space)
1755 break;
1756 str=space+1;
1757 }
1758 todo_wine_if(test->todo & 0x2)
1759 okChildInt("argcA", count);
1760 }
1761 test++;
1762 }
1763
1764 if (dllver.dwMajorVersion != 0)
1765 {
1766 /* The more recent versions of shell32.dll accept quoted filenames
1767 * while older ones (e.g. 4.00) don't. Still we want to test this
1768 * because IE 6 depends on the new behavior.
1769 * One day we may need to check the exact version of the dll but for
1770 * now making sure DllGetVersion() is present is sufficient.
1771 */
1772 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1773 rc=shell_execute(NULL, filename, NULL, NULL);
1774 okShell(rc > 32, "failed: rc=%ld err=%u\n", rc, GetLastError());
1775 okChildInt("argcA", 5);
1776 okChildString("argvA3", "Open");
1777 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1778 okChildPath("argvA4", filename);
1779 }
1780
1781 sprintf(filename, "\"%s\\test file.sha\"", tmpdir);
1782 rc=shell_execute(NULL, filename, NULL, NULL);
1783 todo_wine okShell(rc > 32, "failed: rc=%ld err=%u\n", rc, GetLastError());
1784 okChildInt("argcA", 5);
1785 todo_wine okChildString("argvA3", "averb");
1786 sprintf(filename, "%s\\test file.sha", tmpdir);
1787 todo_wine okChildPath("argvA4", filename);
1788 }
1789
1790 typedef struct
1791 {
1792 const char* urlprefix;
1793 const char* basename;
1794 int flags;
1795 int todo;
1796 } fileurl_tests_t;
1797
1798 #define URL_SUCCESS 0x1
1799 #define USE_COLON 0x2
1800 #define USE_BSLASH 0x4
1801
1802 static fileurl_tests_t fileurl_tests[]=
1803 {
1804 /* How many slashes does it take... */
1805 {"file:", "%s\\test file.shlexec", URL_SUCCESS, 0},
1806 {"file:/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1807 {"file://", "%s\\test file.shlexec", URL_SUCCESS, 0},
1808 {"file:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1809 {"File:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1810 {"file:////", "%s\\test file.shlexec", URL_SUCCESS, 0},
1811 {"file://///", "%s\\test file.shlexec", 0, 0},
1812
1813 /* Test with Windows-style paths */
1814 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_COLON, 0},
1815 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_BSLASH, 0},
1816
1817 /* Check handling of hostnames */
1818 {"file://localhost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1819 {"file://localhost:80/", "%s\\test file.shlexec", 0, 0},
1820 {"file://LocalHost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1821 {"file://127.0.0.1/", "%s\\test file.shlexec", 0, 0},
1822 {"file://::1/", "%s\\test file.shlexec", 0, 0},
1823 {"file://notahost/", "%s\\test file.shlexec", 0, 0},
1824
1825 /* Environment variables are not expanded in URLs */
1826 {"%urlprefix%", "%s\\test file.shlexec", 0, 0x1},
1827 {"file:///", "%%TMPDIR%%\\test file.shlexec", 0, 0},
1828
1829 /* Test shortcuts vs. URLs */
1830 {"file://///", "%s\\test_shortcut_shlexec.lnk", 0, 0x1d},
1831
1832 /* Confuse things by mixing protocols */
1833 {"file://", "shlproto://foo/bar", USE_COLON, 0},
1834
1835 {NULL, NULL, 0, 0}
1836 };
1837
1838 static void test_fileurls(void)
1839 {
1840 char filename[MAX_PATH], fileurl[MAX_PATH], longtmpdir[MAX_PATH];
1841 char command[MAX_PATH];
1842 const fileurl_tests_t* test;
1843 char *s;
1844 INT_PTR rc;
1845
1846 if (skip_shlexec_tests)
1847 {
1848 skip("No file URL tests due to lack of .shlexec association\n");
1849 return;
1850 }
1851
1852 rc = shell_execute_ex(SEE_MASK_FLAG_NO_UI, NULL,
1853 "file:///nosuchfile.shlexec", NULL, NULL, NULL);
1854 if (rc > 32)
1855 {
1856 win_skip("shell32 is too old (likely < 4.72). Skipping the file URL tests\n");
1857 return;
1858 }
1859
1860 get_long_path_name(tmpdir, longtmpdir, sizeof(longtmpdir)/sizeof(*longtmpdir));
1861 SetEnvironmentVariableA("urlprefix", "file:///");
1862
1863 test=fileurl_tests;
1864 while (test->basename)
1865 {
1866 /* Build the file URL */
1867 sprintf(filename, test->basename, longtmpdir);
1868 strcpy(fileurl, test->urlprefix);
1869 strcat(fileurl, filename);
1870 s = fileurl + strlen(test->urlprefix);
1871 while (*s)
1872 {
1873 if (!(test->flags & USE_COLON) && *s == ':')
1874 *s = '|';
1875 else if (!(test->flags & USE_BSLASH) && *s == '\\')
1876 *s = '/';
1877 s++;
1878 }
1879
1880 /* Test it first with FindExecutable() */
1881 rc = (INT_PTR)FindExecutableA(fileurl, NULL, command);
1882 ok(rc == SE_ERR_FNF, "FindExecutable(%s) failed: bad rc=%lu\n", fileurl, rc);
1883
1884 /* Then ShellExecute() */
1885 if ((test->todo & 0x10) == 0)
1886 rc = shell_execute(NULL, fileurl, NULL, NULL);
1887 else todo_wait
1888 rc = shell_execute(NULL, fileurl, NULL, NULL);
1889 if (bad_shellexecute)
1890 {
1891 win_skip("shell32 is too old (likely 4.72). Skipping the file URL tests\n");
1892 break;
1893 }
1894 if (test->flags & URL_SUCCESS)
1895 {
1896 todo_wine_if(test->todo & 0x1)
1897 okShell(rc > 32, "failed: bad rc=%lu\n", rc);
1898 }
1899 else
1900 {
1901 todo_wine_if(test->todo & 0x1)
1902 okShell(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1903 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1904 "failed: bad rc=%lu\n", rc);
1905 }
1906 if (rc == 33)
1907 {
1908 todo_wine_if(test->todo & 0x2)
1909 okChildInt("argcA", 5);
1910 todo_wine_if(test->todo & 0x4)
1911 okChildString("argvA3", "Open");
1912 todo_wine_if(test->todo & 0x8)
1913 okChildPath("argvA4", filename);
1914 }
1915 test++;
1916 }
1917
1918 SetEnvironmentVariableA("urlprefix", NULL);
1919 }
1920
1921 static void test_urls(void)
1922 {
1923 char url[MAX_PATH];
1924 INT_PTR rc;
1925
1926 if (!create_test_class("fakeproto", FALSE))
1927 {
1928 skip("Unable to create 'fakeproto' class for URL tests\n");
1929 return;
1930 }
1931 create_test_verb("fakeproto", "open", 0, "URL %1");
1932
1933 create_test_class("shlpaverb", TRUE);
1934 create_test_verb("shlpaverb", "averb", 0, "PAVerb \"%1\"");
1935
1936 /* Protocols must be properly declared */
1937 rc = shell_execute(NULL, "notaproto://foo", NULL, NULL);
1938 ok(rc == SE_ERR_NOASSOC || broken(rc == SE_ERR_ACCESSDENIED),
1939 "%s returned %lu\n", shell_call, rc);
1940
1941 rc = shell_execute(NULL, "fakeproto://foo/bar", NULL, NULL);
1942 todo_wine ok(rc == SE_ERR_NOASSOC || broken(rc == SE_ERR_ACCESSDENIED),
1943 "%s returned %lu\n", shell_call, rc);
1944
1945 /* Here's a real live one */
1946 rc = shell_execute(NULL, "shlproto://foo/bar", NULL, NULL);
1947 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1948 okChildInt("argcA", 5);
1949 okChildString("argvA3", "URL");
1950 okChildString("argvA4", "shlproto://foo/bar");
1951
1952 /* Check default verb detection */
1953 rc = shell_execute(NULL, "shlpaverb://foo/bar", NULL, NULL);
1954 todo_wine ok(rc > 32 || /* XP+IE7 - Win10 */
1955 broken(rc == SE_ERR_NOASSOC), /* XP+IE6 */
1956 "%s failed: rc=%lu\n", shell_call, rc);
1957 if (rc > 32)
1958 {
1959 okChildInt("argcA", 5);
1960 todo_wine okChildString("argvA3", "PAVerb");
1961 todo_wine okChildString("argvA4", "shlpaverb://foo/bar");
1962 }
1963
1964 /* But alternative verbs are a recent feature! */
1965 rc = shell_execute("averb", "shlproto://foo/bar", NULL, NULL);
1966 ok(rc > 32 || /* Win8 - Win10 */
1967 broken(rc == SE_ERR_ACCESSDENIED), /* XP - Win7 */
1968 "%s failed: rc=%lu\n", shell_call, rc);
1969 if (rc > 32)
1970 {
1971 okChildString("argvA3", "AVerb");
1972 okChildString("argvA4", "shlproto://foo/bar");
1973 }
1974
1975 /* A .lnk ending does not turn a URL into a shortcut */
1976 todo_wait rc = shell_execute(NULL, "shlproto://foo/bar.lnk", NULL, NULL);
1977 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1978 okChildInt("argcA", 5);
1979 todo_wine okChildString("argvA3", "URL");
1980 todo_wine okChildString("argvA4", "shlproto://foo/bar.lnk");
1981
1982 /* Neither does a .exe extension */
1983 rc = shell_execute(NULL, "shlproto://foo/bar.exe", NULL, NULL);
1984 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1985 okChildInt("argcA", 5);
1986 okChildString("argvA3", "URL");
1987 okChildString("argvA4", "shlproto://foo/bar.exe");
1988
1989 /* But a class name overrides it */
1990 rc = shell_execute(NULL, "shlproto://foo/bar", "shlexec.shlexec", NULL);
1991 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1992 okChildInt("argcA", 5);
1993 okChildString("argvA3", "URL");
1994 okChildString("argvA4", "shlproto://foo/bar");
1995
1996 /* Environment variables are expanded in URLs (but not in file URLs!) */
1997 rc = shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1998 NULL, "shlproto://%TMPDIR%/bar", NULL, NULL, NULL);
1999 okShell(rc > 32, "failed: rc=%lu\n", rc);
2000 okChildInt("argcA", 5);
2001 sprintf(url, "shlproto://%s/bar", tmpdir);
2002 okChildString("argvA3", "URL");
2003 okChildStringBroken("argvA4", url, "shlproto://%TMPDIR%/bar");
2004
2005 /* But only after the path has been identified as a URL */
2006 SetEnvironmentVariableA("urlprefix", "shlproto:///");
2007 rc = shell_execute(NULL, "%urlprefix%foo", NULL, NULL);
2008 todo_wine ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2009 SetEnvironmentVariableA("urlprefix", NULL);
2010
2011 delete_test_class("fakeproto");
2012 delete_test_class("shlpaverb");
2013 }
2014
2015 static void test_find_executable(void)
2016 {
2017 char notepad_path[MAX_PATH];
2018 char filename[MAX_PATH];
2019 char command[MAX_PATH];
2020 const filename_tests_t* test;
2021 INT_PTR rc;
2022
2023 if (!create_test_association(".sfe"))
2024 {
2025 skip("Unable to create association for '.sfe'\n");
2026 return;
2027 }
2028 create_test_verb("shlexec.sfe", "Open", 1, "%1");
2029
2030 /* Don't test FindExecutable(..., NULL), it always crashes */
2031
2032 strcpy(command, "your word");
2033 if (0) /* Can crash on Vista! */
2034 {
2035 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
2036 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
2037 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
2038 }
2039
2040 GetSystemDirectoryA( notepad_path, MAX_PATH );
2041 strcat( notepad_path, "\\notepad.exe" );
2042
2043 /* Search for something that should be in the system-wide search path (no default directory) */
2044 strcpy(command, "your word");
2045 rc=(INT_PTR)FindExecutableA("notepad.exe", NULL, command);
2046 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
2047 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
2048
2049 /* Search for something that should be in the system-wide search path (with default directory) */
2050 strcpy(command, "your word");
2051 rc=(INT_PTR)FindExecutableA("notepad.exe", tmpdir, command);
2052 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
2053 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
2054
2055 strcpy(command, "your word");
2056 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
2057 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
2058 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
2059
2060 sprintf(filename, "%s\\test file.sfe", tmpdir);
2061 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2062 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
2063 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
2064
2065 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
2066 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
2067
2068 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
2069 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
2070
2071 delete_test_association(".sfe");
2072
2073 if (!create_test_association(".shl"))
2074 {
2075 skip("Unable to create association for '.shl'\n");
2076 return;
2077 }
2078 create_test_verb("shlexec.shl", "Open", 0, "Open");
2079
2080 sprintf(filename, "%s\\test file.shl", tmpdir);
2081 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2082 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
2083
2084 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
2085 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2086
2087 delete_test_association(".shl");
2088
2089 if (rc > 32)
2090 {
2091 /* On Windows XP and 2003 FindExecutable() is completely broken.
2092 * Probably what it does is convert the filename to 8.3 format,
2093 * which as a side effect converts the '.shlfoo' extension to '.shl',
2094 * and then tries to find an association for '.shl'. This means it
2095 * will normally fail on most extensions with more than 3 characters,
2096 * like '.mpeg', etc.
2097 * Also it means we cannot do any other test.
2098 */
2099 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
2100 return;
2101 }
2102
2103 if (skip_shlexec_tests)
2104 {
2105 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
2106 return;
2107 }
2108
2109 test=filename_tests;
2110 while (test->basename)
2111 {
2112 sprintf(filename, test->basename, tmpdir);
2113 if (strchr(filename, '/'))
2114 {
2115 char* c;
2116 c=filename;
2117 while (*c)
2118 {
2119 if (*c=='\\')
2120 *c='/';
2121 c++;
2122 }
2123 }
2124 /* Win98 does not '\0'-terminate command! */
2125 memset(command, '\0', sizeof(command));
2126 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2127 if (rc > 32)
2128 rc=33;
2129 todo_wine_if(test->todo & 0x10)
2130 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
2131 if (rc > 32)
2132 {
2133 BOOL equal;
2134 equal=strcmp(command, argv0) == 0 ||
2135 /* NT4 returns an extra 0x8 character! */
2136 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
2137 todo_wine_if(test->todo & 0x20)
2138 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
2139 filename, command, argv0);
2140 }
2141 test++;
2142 }
2143 }
2144
2145
2146 static filename_tests_t lnk_tests[]=
2147 {
2148 /* Pass bad / nonexistent filenames as a parameter */
2149 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
2150 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
2151
2152 /* Pass regular paths as a parameter */
2153 {NULL, "%s\\test file.shlexec", 0xa, 33},
2154 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
2155
2156 /* Pass filenames with no association as a parameter */
2157 {NULL, "%s\\test file.noassoc", 0xa, 33},
2158
2159 {NULL, NULL, 0}
2160 };
2161
2162 static void test_lnks(void)
2163 {
2164 char filename[MAX_PATH];
2165 char params[MAX_PATH];
2166 const filename_tests_t* test;
2167 INT_PTR rc;
2168
2169 if (skip_shlexec_tests)
2170 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
2171 else
2172 {
2173 /* Should open through our association */
2174 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2175 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2176 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2177 okChildInt("argcA", 5);
2178 okChildString("argvA3", "Open");
2179 sprintf(params, "%s\\test file.shlexec", tmpdir);
2180 get_long_path_name(params, filename, sizeof(filename));
2181 okChildPath("argvA4", filename);
2182
2183 todo_wait rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_DOENVSUBST, NULL, "%TMPDIR%\\test_shortcut_shlexec.lnk", NULL, NULL, NULL);
2184 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2185 okChildInt("argcA", 5);
2186 todo_wine okChildString("argvA3", "Open");
2187 sprintf(params, "%s\\test file.shlexec", tmpdir);
2188 get_long_path_name(params, filename, sizeof(filename));
2189 todo_wine okChildPath("argvA4", filename);
2190 }
2191
2192 /* Should just run our executable */
2193 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2194 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2195 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2196 okChildInt("argcA", 4);
2197 okChildString("argvA3", "Lnk");
2198
2199 if (!skip_shlexec_tests)
2200 {
2201 /* An explicit class overrides lnk's ContextMenuHandler */
2202 rc=shell_execute_ex(SEE_MASK_CLASSNAME | SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, "shlexec.shlexec");
2203 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2204 okChildInt("argcA", 5);
2205 okChildString("argvA3", "Open");
2206 okChildPath("argvA4", filename);
2207 }
2208
2209 if (dllver.dwMajorVersion>=6)
2210 {
2211 char* c;
2212 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
2213 * Older versions don't or are quite buggy in this regard.
2214 */
2215 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2216 c=filename;
2217 while (*c)
2218 {
2219 if (*c=='\\')
2220 *c='/';
2221 c++;
2222 }
2223 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2224 okShell(rc > 32, "failed: rc=%lu err=%u\n", rc, GetLastError());
2225 okChildInt("argcA", 4);
2226 okChildString("argvA3", "Lnk");
2227 }
2228
2229 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2230 test=lnk_tests;
2231 while (test->basename)
2232 {
2233 params[0]='\"';
2234 sprintf(params+1, test->basename, tmpdir);
2235 strcat(params,"\"");
2236 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
2237 NULL, NULL);
2238 if (rc > 32)
2239 rc=33;
2240 todo_wine_if(test->todo & 0x1)
2241 okShell(rc==test->rc, "failed: rc=%lu err=%u\n", rc, GetLastError());
2242 if (rc==0)
2243 {
2244 todo_wine_if(test->todo & 0x2)
2245 okChildInt("argcA", 5);
2246 todo_wine_if(test->todo & 0x4)
2247 okChildString("argvA3", "Lnk");
2248 sprintf(params, test->basename, tmpdir);
2249 okChildPath("argvA4", params);
2250 }
2251 test++;
2252 }
2253 }
2254
2255
2256 static void test_exes(void)
2257 {
2258 char filename[MAX_PATH];
2259 char params[1024];
2260 INT_PTR rc;
2261
2262 sprintf(params, "shlexec \"%s\" Exec", child_file);
2263
2264 /* We need NOZONECHECKS on Win2003 to block a dialog */
2265 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
2266 NULL, NULL);
2267 okShell(rc > 32, "returned %lu\n", rc);
2268 okChildInt("argcA", 4);
2269 okChildString("argvA3", "Exec");
2270
2271 if (! skip_noassoc_tests)
2272 {
2273 sprintf(filename, "%s\\test file.noassoc", tmpdir);
2274 if (CopyFileA(argv0, filename, FALSE))
2275 {
2276 rc=shell_execute(NULL, filename, params, NULL);
2277 todo_wine {
2278 okShell(rc==SE_ERR_NOASSOC, "returned %lu\n", rc);
2279 }
2280 }
2281 }
2282 else
2283 {
2284 win_skip("Skipping shellexecute of file with unassociated extension\n");
2285 }
2286
2287 /* test combining executable and parameters */
2288 sprintf(filename, "%s shlexec \"%s\" Exec", argv0, child_file);
2289 rc = shell_execute(NULL, filename, NULL, NULL);
2290 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2291
2292 sprintf(filename, "\"%s\" shlexec \"%s\" Exec", argv0, child_file);
2293 rc = shell_execute(NULL, filename, NULL, NULL);
2294 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2295
2296 /* A verb, even if invalid, overrides the normal handling of executables */
2297 todo_wait rc = shell_execute_ex(SEE_MASK_FLAG_NO_UI,
2298 "notaverb", argv0, NULL, NULL, NULL);
2299 todo_wine okShell(rc == SE_ERR_NOASSOC, "returned %lu\n", rc);
2300
2301 if (!skip_shlexec_tests)
2302 {
2303 /* A class overrides the normal handling of executables too */
2304 /* FIXME SEE_MASK_FLAG_NO_UI is only needed due to Wine's bug */
2305 rc = shell_execute_ex(SEE_MASK_CLASSNAME | SEE_MASK_FLAG_NO_UI,
2306 NULL, argv0, NULL, NULL, ".shlexec");
2307 todo_wine okShell(rc > 32, "returned %lu\n", rc);
2308 okChildInt("argcA", 5);
2309 todo_wine okChildString("argvA3", "Open");
2310 todo_wine okChildPath("argvA4", argv0);
2311 }
2312 }
2313
2314 typedef struct
2315 {
2316 const char* command;
2317 const char* ddeexec;
2318 const char* application;
2319 const char* topic;
2320 const char* ifexec;
2321 int expectedArgs;
2322 const char* expectedDdeExec;
2323 BOOL broken;
2324 } dde_tests_t;
2325
2326 static dde_tests_t dde_tests[] =
2327 {
2328 /* Test passing and not passing command-line
2329 * argument, no DDE */
2330 {"", NULL, NULL, NULL, NULL, 0, ""},
2331 {"\"%1\"", NULL, NULL, NULL, NULL, 1, ""},
2332
2333 /* Test passing and not passing command-line
2334 * argument, with DDE */
2335 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, 0, "[open(\"%s\")]"},
2336 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, 1, "[open(\"%s\")]"},
2337
2338 /* Test unquoted %1 in command and ddeexec
2339 * (test filename has space) */
2340 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", TRUE /* before vista */},
2341
2342 /* Test ifexec precedence over ddeexec */
2343 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", 0, "[ifexec(\"%s\")]"},
2344
2345 /* Test default DDE topic */
2346 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, 0, "[open(\"%s\")]"},
2347
2348 /* Test default DDE application */
2349 {"", "[open(\"%1\")]", NULL, "dde", NULL, 0, "[open(\"%s\")]"},
2350
2351 {NULL}
2352 };
2353
2354 static int waitforinputidle_count;
2355 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
2356 {
2357 waitforinputidle_count++;
2358 if (winetest_debug > 1)
2359 trace("WaitForInputIdle() waiting for dde event timeout=min(%u,5s)\n", timeout);
2360 timeout = timeout < 5000 ? timeout : 5000;
2361 return WaitForSingleObject(dde_ready_event, timeout);
2362 }
2363
2364 /*
2365 * WaitForInputIdle() will normally return immediately for console apps. That's
2366 * a problem for us because ShellExecute will assume that an app is ready to
2367 * receive DDE messages after it has called WaitForInputIdle() on that app.
2368 * To work around that we install our own version of WaitForInputIdle() that
2369 * will wait for the child to explicitly tell us that it is ready. We do that
2370 * by changing the entry for WaitForInputIdle() in the shell32 import address
2371 * table.
2372 */
2373 static void hook_WaitForInputIdle(DWORD (WINAPI *new_func)(HANDLE, DWORD))
2374 {
2375 char *base;
2376 PIMAGE_NT_HEADERS nt_headers;
2377 DWORD import_directory_rva;
2378 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
2379 int hook_count = 0;
2380
2381 base = (char *) GetModuleHandleA("shell32.dll");
2382 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
2383 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
2384
2385 /* Search for the correct imported module by walking the import descriptors */
2386 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
2387 while (U(*import_descriptor).OriginalFirstThunk != 0)
2388 {
2389 char *import_module_name;
2390
2391 import_module_name = base + import_descriptor->Name;
2392 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
2393 lstrcmpiA(import_module_name, "user32") == 0)
2394 {
2395 PIMAGE_THUNK_DATA int_entry;
2396 PIMAGE_THUNK_DATA iat_entry;
2397
2398 /* The import name table and import address table are two parallel
2399 * arrays. We need the import name table to find the imported
2400 * routine and the import address table to patch the address, so
2401 * walk them side by side */
2402 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
2403 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
2404 while (int_entry->u1.Ordinal != 0)
2405 {
2406 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
2407 {
2408 PIMAGE_IMPORT_BY_NAME import_by_name;
2409 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
2410 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
2411 {
2412 /* Found the correct routine in the correct imported module. Patch it. */
2413 DWORD old_prot;
2414 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
2415 iat_entry->u1.Function = (ULONG_PTR) new_func;
2416 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
2417 if (winetest_debug > 1)
2418 trace("Hooked %s.WaitForInputIdle\n", import_module_name);
2419 hook_count++;
2420 break;
2421 }
2422 }
2423 int_entry++;
2424 iat_entry++;
2425 }
2426 break;
2427 }
2428
2429 import_descriptor++;
2430 }
2431 ok(hook_count, "Could not hook WaitForInputIdle()\n");
2432 }
2433
2434 static void test_dde(void)
2435 {
2436 char filename[MAX_PATH], defApplication[MAX_PATH];
2437 const dde_tests_t* test;
2438 char params[1024];
2439 INT_PTR rc;
2440 HANDLE map;
2441 char *shared_block;
2442 DWORD ddeflags;
2443
2444 hook_WaitForInputIdle(hooked_WaitForInputIdle);
2445
2446 sprintf(filename, "%s\\test file.sde", tmpdir);
2447
2448 /* Default service is application name minus path and extension */
2449 strcpy(defApplication, strrchr(argv0, '\\')+1);
2450 *strchr(defApplication, '.') = 0;
2451
2452 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
2453 4096, "winetest_shlexec_dde_map");
2454 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
2455
2456 ddeflags = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
2457 test = dde_tests;
2458 while (test->command)
2459 {
2460 if (!create_test_association(".sde"))
2461 {
2462 skip("Unable to create association for '.sde'\n");
2463 return;
2464 }
2465 create_test_verb_dde("shlexec.sde", "Open", 0, test->command, test->ddeexec,
2466 test->application, test->topic, test->ifexec);
2467
2468 if (test->application != NULL || test->topic != NULL)
2469 {
2470 strcpy(shared_block, test->application ? test->application : defApplication);
2471 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
2472 }
2473 else
2474 {
2475 shared_block[0] = '\0';
2476 shared_block[1] = '\0';
2477 }
2478 ddeExec[0] = 0;
2479
2480 waitforinputidle_count = 0;
2481 dde_ready_event = CreateEventA(NULL, TRUE, FALSE, "winetest_shlexec_dde_ready");
2482 rc = shell_execute_ex(ddeflags, NULL, filename, NULL, NULL, NULL);
2483 CloseHandle(dde_ready_event);
2484 if (!(ddeflags & SEE_MASK_WAITFORINPUTIDLE) && rc == SE_ERR_DDEFAIL &&
2485 GetLastError() == ERROR_FILE_NOT_FOUND &&
2486 strcmp(winetest_platform, "windows") == 0)
2487 {
2488 /* Windows 10 does not call WaitForInputIdle() for DDE which creates
2489 * a race condition as the DDE server may not have time to start up.
2490 * When that happens the test fails with the above results and we
2491 * compensate by forcing the WaitForInputIdle() call.
2492 */
2493 trace("Adding SEE_MASK_WAITFORINPUTIDLE for Windows 10\n");
2494 ddeflags |= SEE_MASK_WAITFORINPUTIDLE;
2495 delete_test_association(".sde");
2496 Sleep(CHILD_DDE_TIMEOUT);
2497 continue;
2498 }
2499 okShell(32 < rc, "failed: rc=%lu err=%u\n", rc, GetLastError());
2500 if (test->ddeexec)
2501 okShell(waitforinputidle_count == 1 ||
2502 broken(waitforinputidle_count == 0) /* Win10 race */,
2503 "WaitForInputIdle() was called %u times\n",
2504 waitforinputidle_count);
2505 else
2506 okShell(waitforinputidle_count == 0, "WaitForInputIdle() was called %u times for a non-DDE case\n", waitforinputidle_count);
2507
2508 if (32 < rc)
2509 {
2510 if (test->broken)
2511 okChildIntBroken("argcA", test->expectedArgs + 3);
2512 else
2513 okChildInt("argcA", test->expectedArgs + 3);
2514
2515 if (test->expectedArgs == 1) okChildPath("argvA3", filename);
2516
2517 sprintf(params, test->expectedDdeExec, filename);
2518 okChildPath("ddeExec", params);
2519 }
2520 reset_association_description();
2521
2522 delete_test_association(".sde");
2523 test++;
2524 }
2525
2526 UnmapViewOfFile(shared_block);
2527 CloseHandle(map);
2528 hook_WaitForInputIdle((void *) WaitForInputIdle);
2529 }
2530
2531 #define DDE_DEFAULT_APP_VARIANTS 3
2532 typedef struct
2533 {
2534 const char* command;
2535 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
2536 int todo;
2537 int rc[DDE_DEFAULT_APP_VARIANTS];
2538 } dde_default_app_tests_t;
2539
2540 static dde_default_app_tests_t dde_default_app_tests[] =
2541 {
2542 /* There are three possible sets of results: Windows <= 2000, XP SP1 and
2543 * >= XP SP2. Use the first two tests to determine which results to expect.
2544 */
2545
2546 /* Test unquoted existing filename with a space */
2547 {"%s\\test file.exe", {"test file", "test file", "test"}, 0x0, {33, 33, 33}},
2548 {"%s\\test2 file.exe", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2549
2550 /* Test unquoted existing filename with a space */
2551 {"%s\\test file.exe param", {"test file", "test file", "test"}, 0x0, {33, 33, 33}},
2552
2553 /* Test quoted existing filename with a space */
2554 {"\"%s\\test file.exe\"", {"test file", "test file", "test file"}, 0x0, {33, 33, 33}},
2555 {"\"%s\\test file.exe\" param", {"test file", "test file", "test file"}, 0x0, {33, 33, 33}},
2556
2557 /* Test unquoted filename with a space that doesn't exist, but
2558 * test2.exe does */
2559 {"%s\\test2 file.exe param", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2560
2561 /* Test quoted filename with a space that does not exist */
2562 {"\"%s\\test2 file.exe\"", {"", "", "test2 file"}, 0x0, {5, 2, 33}},
2563 {"\"%s\\test2 file.exe\" param", {"", "", "test2 file"}, 0x0, {5, 2, 33}},
2564
2565 /* Test filename supplied without the extension */
2566 {"%s\\test2", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2567 {"%s\\test2 param", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2568
2569 /* Test an unquoted nonexistent filename */
2570 {"%s\\notexist.exe", {"", "", "notexist"}, 0x0, {5, 2, 33}},
2571 {"%s\\notexist.exe param", {"", "", "notexist"}, 0x0, {5, 2, 33}},
2572
2573 /* Test an application that will be found on the path */
2574 {"cmd", {"cmd", "cmd", "cmd"}, 0x0, {33, 33, 33}},
2575 {"cmd param", {"cmd", "cmd", "cmd"}, 0x0, {33, 33, 33}},
2576
2577 /* Test an application that will not be found on the path */
2578 {"xyzwxyzwxyz", {"", "", "xyzwxyzwxyz"}, 0x0, {5, 2, 33}},
2579 {"xyzwxyzwxyz param", {"", "", "xyzwxyzwxyz"}, 0x0, {5, 2, 33}},
2580
2581 {NULL, {NULL}, 0, {0}}
2582 };
2583
2584 typedef struct
2585 {
2586 char *filename;
2587 DWORD threadIdParent;
2588 } dde_thread_info_t;
2589
2590 static DWORD CALLBACK ddeThread(LPVOID arg)
2591 {
2592 dde_thread_info_t *info = arg;
2593 assert(info && info->filename);
2594 PostThreadMessageA(info->threadIdParent,
2595 WM_QUIT,
2596 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL, NULL),
2597 0);
2598 ExitThread(0);
2599 }
2600
2601 static void test_dde_default_app(void)
2602 {
2603 char filename[MAX_PATH];
2604 HSZ hszApplication;
2605 dde_thread_info_t info = { filename, GetCurrentThreadId() };
2606 const dde_default_app_tests_t* test;
2607 char params[1024];
2608 DWORD threadId;
2609 MSG msg;
2610 INT_PTR rc;
2611 int which = 0;
2612 HDDEDATA ret;
2613 BOOL b;
2614
2615 post_quit_on_execute = FALSE;
2616 ddeInst = 0;
2617 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
2618 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0);
2619 ok(rc == DMLERR_NO_ERROR, "got %lx\n", rc);
2620
2621 sprintf(filename, "%s\\test file.sde", tmpdir);
2622
2623 /* It is strictly not necessary to register an application name here, but wine's
2624 * DdeNameService implementation complains if 0 is passed instead of
2625 * hszApplication with DNS_FILTEROFF */
2626 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2627 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2628 ok(hszApplication && hszTopic, "got %p and %p\n", hszApplication, hszTopic);
2629 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER | DNS_FILTEROFF);
2630 ok(ret != 0, "got %p\n", ret);
2631
2632 test = dde_default_app_tests;
2633 while (test->command)
2634 {
2635 HANDLE thread;
2636
2637 if (!create_test_association(".sde"))
2638 {
2639 skip("Unable to create association for '.sde'\n");
2640 return;
2641 }
2642 sprintf(params, test->command, tmpdir);
2643 create_test_verb_dde("shlexec.sde", "Open", 1, params, "[test]", NULL,
2644 "shlexec", NULL);
2645 ddeApplication[0] = 0;
2646
2647 /* No application will be run as we will respond to the first DDE event,
2648 * so don't wait for it */
2649 SetEvent(hEvent);
2650
2651 thread = CreateThread(NULL, 0, ddeThread, &info, 0, &threadId);
2652 ok(thread != NULL, "got %p\n", thread);
2653 while (GetMessageA(&msg, NULL, 0, 0)) DispatchMessageA(&msg);
2654 rc = msg.wParam > 32 ? 33 : msg.wParam;
2655
2656 /* The first two tests determine which set of results to expect.
2657 * First check the platform as only the first set of results is
2658 * acceptable for Wine.
2659 */
2660 if (strcmp(winetest_platform, "wine"))
2661 {
2662 if (test == dde_default_app_tests)
2663 {
2664 if (strcmp(ddeApplication, test->expectedDdeApplication[0]))
2665 which = 2;
2666 }
2667 else if (test == dde_default_app_tests + 1)
2668 {
2669 if (which == 0 && rc == test->rc[1])
2670 which = 1;
2671 trace("DDE result variant %d\n", which);
2672 }
2673 }
2674
2675 todo_wine_if(test->todo & 0x1)
2676 okShell(rc==test->rc[which], "failed: rc=%lu err=%u\n",
2677 rc, GetLastError());
2678 if (rc == 33)
2679 {
2680 todo_wine_if(test->todo & 0x2)
2681 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2682 "Expected application '%s', got '%s'\n",
2683 test->expectedDdeApplication[which], ddeApplication);
2684 }
2685 reset_association_description();
2686
2687 delete_test_association(".sde");
2688 test++;
2689 }
2690
2691 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
2692 ok(ret != 0, "got %p\n", ret);
2693 b = DdeFreeStringHandle(ddeInst, hszTopic);
2694 ok(b, "got %d\n", b);
2695 b = DdeFreeStringHandle(ddeInst, hszApplication);
2696 ok(b, "got %d\n", b);
2697 b = DdeUninitialize(ddeInst);
2698 ok(b, "got %d\n", b);
2699 }
2700
2701 static void init_test(void)
2702 {
2703 HMODULE hdll;
2704 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
2705 char filename[MAX_PATH];
2706 WCHAR lnkfile[MAX_PATH];
2707 char params[1024];
2708 const char* const * testfile;
2709 lnk_desc_t desc;
2710 DWORD rc;
2711 HRESULT r;
2712
2713 hdll=GetModuleHandleA("shell32.dll");
2714 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2715 if (pDllGetVersion)
2716 {
2717 dllver.cbSize=sizeof(dllver);
2718 pDllGetVersion(&dllver);
2719 trace("major=%d minor=%d build=%d platform=%d\n",
2720 dllver.dwMajorVersion, dllver.dwMinorVersion,
2721 dllver.dwBuildNumber, dllver.dwPlatformID);
2722 }
2723 else
2724 {
2725 memset(&dllver, 0, sizeof(dllver));
2726 }
2727
2728 r = CoInitialize(NULL);
2729 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2730 if (FAILED(r))
2731 exit(1);
2732
2733 rc=GetModuleFileNameA(NULL, argv0, sizeof(argv0));
2734 ok(rc != 0 && rc < sizeof(argv0), "got %d\n", rc);
2735 if (GetFileAttributesA(argv0)==INVALID_FILE_ATTRIBUTES)
2736 {
2737 strcat(argv0, ".so");
2738 ok(GetFileAttributesA(argv0)!=INVALID_FILE_ATTRIBUTES,
2739 "unable to find argv0!\n");
2740 }
2741
2742 /* Older versions (win 2k) fail tests if there is a space in
2743 the path. */
2744 if (dllver.dwMajorVersion <= 5)
2745 strcpy(filename, "c:\\");
2746 else
2747 GetTempPathA(sizeof(filename), filename);
2748 GetTempFileNameA(filename, "wt", 0, tmpdir);
2749 GetLongPathNameA(tmpdir, tmpdir, sizeof(tmpdir));
2750 DeleteFileA( tmpdir );
2751 rc = CreateDirectoryA( tmpdir, NULL );
2752 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2753 /* Set %TMPDIR% for the tests */
2754 SetEnvironmentVariableA("TMPDIR", tmpdir);
2755
2756 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2757 ok(rc != 0, "got %d\n", rc);
2758 init_event(child_file);
2759
2760 /* Set up the test files */
2761 testfile=testfiles;
2762 while (*testfile)
2763 {
2764 HANDLE hfile;
2765
2766 sprintf(filename, *testfile, tmpdir);
2767 hfile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2768 FILE_ATTRIBUTE_NORMAL, NULL);
2769 if (hfile==INVALID_HANDLE_VALUE)
2770 {
2771 trace("unable to create '%s': err=%u\n", filename, GetLastError());
2772 assert(0);
2773 }
2774 CloseHandle(hfile);
2775 testfile++;
2776 }
2777
2778 /* Setup the test shortcuts */
2779 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2780 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2781 desc.description=NULL;
2782 desc.workdir=NULL;
2783 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2784 desc.path=filename;
2785 desc.pidl=NULL;
2786 desc.arguments="ignored";
2787 desc.showcmd=0;
2788 desc.icon=NULL;
2789 desc.icon_id=0;
2790 desc.hotkey=0;
2791 create_lnk(lnkfile, &desc, 0);
2792
2793 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2794 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2795 desc.description=NULL;
2796 desc.workdir=NULL;
2797 desc.path=argv0;
2798 desc.pidl=NULL;
2799 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2800 desc.arguments=params;
2801 desc.showcmd=0;
2802 desc.icon=NULL;
2803 desc.icon_id=0;
2804 desc.hotkey=0;
2805 create_lnk(lnkfile, &desc, 0);
2806
2807 /* Create a basic association suitable for most tests */
2808 if (!create_test_association(".shlexec"))
2809 {
2810 skip_shlexec_tests = TRUE;
2811 skip("Unable to create association for '.shlexec'\n");
2812 return;
2813 }
2814 create_test_verb("shlexec.shlexec", "Open", 0, "Open \"%1\"");
2815 create_test_verb("shlexec.shlexec", "NoQuotes", 0, "NoQuotes %1");
2816 create_test_verb("shlexec.shlexec", "LowerL", 0, "LowerL %l");
2817 create_test_verb("shlexec.shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2818 create_test_verb("shlexec.shlexec", "UpperL", 0, "UpperL %L");
2819 create_test_verb("shlexec.shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2820
2821 create_test_association(".sha");
2822 create_test_verb("shlexec.sha", "averb", 0, "AVerb \"%1\"");
2823
2824 create_test_class("shlproto", TRUE);
2825 create_test_verb("shlproto", "open", 0, "URL \"%1\"");
2826 create_test_verb("shlproto", "averb", 0, "AVerb \"%1\"");
2827
2828 /* Set an environment variable to see if it is inherited */
2829 SetEnvironmentVariableA("ShlexecVar", "Present");
2830 }
2831
2832 static void cleanup_test(void)
2833 {
2834 char filename[MAX_PATH];
2835 const char* const * testfile;
2836
2837 /* Delete the test files */
2838 testfile=testfiles;
2839 while (*testfile)
2840 {
2841 sprintf(filename, *testfile, tmpdir);
2842 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2843 SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
2844 DeleteFileA(filename);
2845 testfile++;
2846 }
2847 DeleteFileA(child_file);
2848 RemoveDirectoryA(tmpdir);
2849
2850 /* Delete the test association */
2851 delete_test_association(".shlexec");
2852 delete_test_association(".sha");
2853 delete_test_class("shlproto");
2854
2855 CloseHandle(hEvent);
2856
2857 CoUninitialize();
2858 }
2859
2860 static void test_directory(void)
2861 {
2862 char path[MAX_PATH], curdir[MAX_PATH];
2863 char params[1024], dirpath[1024];
2864 INT_PTR rc;
2865
2866 sprintf(path, "%s\\test2.exe", tmpdir);
2867 CopyFileA(argv0, path, FALSE);
2868
2869 sprintf(params, "shlexec \"%s\" Exec", child_file);
2870
2871 /* Test with the current directory */
2872 GetCurrentDirectoryA(sizeof(curdir), curdir);
2873 SetCurrentDirectoryA(tmpdir);
2874 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2875 NULL, "test2.exe", params, NULL, NULL);
2876 okShell(rc > 32, "returned %lu\n", rc);
2877 okChildInt("argcA", 4);
2878 okChildString("argvA3", "Exec");
2879 todo_wine okChildPath("longPath", path);
2880 SetCurrentDirectoryA(curdir);
2881
2882 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2883 NULL, "test2.exe", params, NULL, NULL);
2884 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2885
2886 /* Explicitly specify the directory to use */
2887 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2888 NULL, "test2.exe", params, tmpdir, NULL);
2889 okShell(rc > 32, "returned %lu\n", rc);
2890 okChildInt("argcA", 4);
2891 okChildString("argvA3", "Exec");
2892 todo_wine okChildPath("longPath", path);
2893
2894 /* Specify it through an environment variable */
2895 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2896 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2897 todo_wine okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2898
2899 rc=shell_execute_ex(SEE_MASK_DOENVSUBST|SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2900 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2901 okShell(rc > 32, "returned %lu\n", rc);
2902 okChildInt("argcA", 4);
2903 okChildString("argvA3", "Exec");
2904 todo_wine okChildPath("longPath", path);
2905
2906 /* Not a colon-separated directory list */
2907 sprintf(dirpath, "%s:%s", curdir, tmpdir);
2908 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2909 NULL, "test2.exe", params, dirpath, NULL);
2910 okShell(rc == SE_ERR_FNF, "returned %lu\n", rc);
2911 }
2912
2913 START_TEST(shlexec)
2914 {
2915
2916 myARGC = winetest_get_mainargs(&myARGV);
2917 if (myARGC >= 3)
2918 {
2919 doChild(myARGC, myARGV);
2920 /* Skip the tests/failures trace for child processes */
2921 ExitProcess(winetest_get_failures());
2922 }
2923
2924 init_test();
2925
2926 test_commandline2argv();
2927 test_argify();
2928 test_lpFile_parsed();
2929 test_filename();
2930 test_fileurls();
2931 test_urls();
2932 test_find_executable();
2933 test_lnks();
2934 test_exes();
2935 test_dde();
2936 test_dde_default_app();
2937 test_directory();
2938
2939 cleanup_test();
2940 }