include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / shell32 / tests / shlexec.c
blobbd32ece8705bddb26ede343bb5d4ab95693a7300
1 /*
2 * Unit test of the ShellExecute function.
4 * Copyright 2005, 2016 Francois Gouget for CodeWeavers
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.
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.
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
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
33 #include <stdio.h>
34 #include <assert.h>
36 #include "wtypes.h"
37 #include "winbase.h"
38 #include "windef.h"
39 #include "shellapi.h"
40 #include "shlwapi.h"
41 #include "ddeml.h"
43 #include "wine/test.h"
45 #include "shell32_test.h"
48 static char argv0[MAX_PATH];
49 static int myARGC;
50 static char** myARGV;
51 static char tmpdir[MAX_PATH];
52 static char child_file[MAX_PATH];
53 static DLLVERSIONINFO dllver;
54 static BOOL skip_shlexec_tests = FALSE;
55 static BOOL skip_noassoc_tests = FALSE;
56 static HANDLE dde_ready_event;
57 static BOOL is_elevated;
60 /***
62 * Helpers to read from / write to the child process results file.
63 * (borrowed from dlls/kernel32/tests/process.c)
65 ***/
67 static const char* encodeA(const char* str)
69 static char encoded[2*1024+1];
70 char* ptr;
71 size_t len,i;
73 if (!str) return "";
74 len = strlen(str) + 1;
75 if (len >= sizeof(encoded)/2)
77 fprintf(stderr, "string is too long!\n");
78 assert(0);
80 ptr = encoded;
81 for (i = 0; i < len; i++)
82 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
83 ptr[2 * len] = '\0';
84 return ptr;
87 static unsigned decode_char(char c)
89 if (c >= '0' && c <= '9') return c - '0';
90 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
91 assert(c >= 'A' && c <= 'F');
92 return c - 'A' + 10;
95 static char* decodeA(const char* str)
97 static char decoded[1024];
98 char* ptr;
99 size_t len,i;
101 len = strlen(str) / 2;
102 if (!len--) return NULL;
103 if (len >= sizeof(decoded))
105 fprintf(stderr, "string is too long!\n");
106 assert(0);
108 ptr = decoded;
109 for (i = 0; i < len; i++)
110 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
111 ptr[len] = '\0';
112 return ptr;
115 static void WINAPIV __WINE_PRINTF_ATTR(2,3) childPrintf(HANDLE h, const char* fmt, ...)
117 va_list valist;
118 char buffer[1024];
119 DWORD w;
121 va_start(valist, fmt);
122 vsprintf(buffer, fmt, valist);
123 va_end(valist);
124 WriteFile(h, buffer, strlen(buffer), &w, NULL);
127 static char* getChildString(const char* sect, const char* key)
129 char buf[1024];
130 char* ret;
132 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
133 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
134 assert(!(strlen(buf) & 1));
135 ret = decodeA(buf);
136 return ret;
140 /***
142 * Child code
144 ***/
146 #define CHILD_DDE_TIMEOUT 2500
147 static DWORD ddeInst;
148 static HSZ hszTopic;
149 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
150 static BOOL post_quit_on_execute;
152 /* Handle DDE for doChild() and test_dde_default_app() */
153 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
154 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
155 ULONG_PTR dwData1, ULONG_PTR dwData2)
157 DWORD size = 0;
159 if (winetest_debug > 2)
160 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08Ix, %08Ix\n",
161 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
163 switch (uType)
165 case XTYP_CONNECT:
166 if (!DdeCmpStringHandles(hsz1, hszTopic))
168 size = DdeQueryStringA(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
169 ok(size < MAX_PATH, "got size %ld\n", size);
170 assert(size < MAX_PATH);
171 return (HDDEDATA)TRUE;
173 return (HDDEDATA)FALSE;
175 case XTYP_EXECUTE:
176 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0);
177 ok(size < MAX_PATH, "got size %ld\n", size);
178 assert(size < MAX_PATH);
179 DdeFreeDataHandle(hData);
180 if (post_quit_on_execute)
181 PostQuitMessage(0);
182 return (HDDEDATA)DDE_FACK;
184 default:
185 return NULL;
189 static HANDLE hEvent;
190 static void init_event(const char* child_file)
192 char* event_name;
193 event_name=strrchr(child_file, '\\')+1;
194 hEvent=CreateEventA(NULL, FALSE, FALSE, event_name);
197 static HANDLE hChildFile;
199 * This is just to make sure the child won't run forever stuck in a
200 * GetMessage() loop when DDE fails for some reason.
202 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
204 trace("childTimeout called\n");
205 childPrintf(hChildFile, "Timeout=1\r\n");
207 PostQuitMessage(0);
210 static void doChild(int argc, char** argv)
212 char *filename, buffer[MAX_PATH];
213 HANDLE hFile, map;
214 int i;
215 UINT_PTR timer;
217 filename=argv[2];
218 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
219 if (hFile == INVALID_HANDLE_VALUE)
220 return;
222 hChildFile = hFile;
223 /* Arguments */
224 childPrintf(hFile, "[Child]\r\n");
225 if (winetest_debug > 2)
227 trace("cmdlineA='%s'\n", GetCommandLineA());
228 trace("argcA=%d\n", argc);
230 childPrintf(hFile, "cmdlineA=%s\r\n", encodeA(GetCommandLineA()));
231 childPrintf(hFile, "argcA=%d\r\n", argc);
232 for (i = 0; i < argc; i++)
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]));
238 GetModuleFileNameA(GetModuleHandleA(NULL), buffer, sizeof(buffer));
239 childPrintf(hFile, "longPath=%s\r\n", encodeA(buffer));
241 /* Check environment variable inheritance */
242 *buffer = '\0';
243 SetLastError(0);
244 GetEnvironmentVariableA("ShlexecVar", buffer, sizeof(buffer));
245 childPrintf(hFile, "ShlexecVarLE=%ld\r\n", GetLastError());
246 childPrintf(hFile, "ShlexecVar=%s\r\n", encodeA(buffer));
248 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
249 if (map != NULL)
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')
256 HDDEDATA hdde;
257 HSZ hszApplication;
258 MSG msg;
259 UINT rc;
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=%lu\n", GetLastError());
274 timer = SetTimer(NULL, 0, CHILD_DDE_TIMEOUT, childTimeout);
276 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
277 SetEvent(dde_ready);
278 CloseHandle(dde_ready);
280 while (GetMessageA(&msg, NULL, 0, 0))
282 if (winetest_debug > 2)
283 trace("msg %d lParam=%Id wParam=%Iu\n", msg.message, msg.lParam, msg.wParam);
284 DispatchMessageA(&msg);
287 Sleep(500);
288 KillTimer(NULL, timer);
289 hdde = DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
290 ok(hdde != NULL, "DdeNameService() failed le=%lu\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");
295 else
297 dde_ready = OpenEventA(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
298 SetEvent(dde_ready);
299 CloseHandle(dde_ready);
302 UnmapViewOfFile(shared_block);
304 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
307 childPrintf(hFile, "Failures=%ld\r\n", winetest_get_failures());
308 CloseHandle(hFile);
310 init_event(filename);
311 SetEvent(hEvent);
312 CloseHandle(hEvent);
315 static void dump_child_(const char* file, int line)
317 if (winetest_debug > 1)
319 char key[18];
320 char* str;
321 int i, c;
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++)
329 sprintf(key, "argvA%d", i);
330 str=getChildString("Child", key);
331 trace_(file, line)("%s='%s'\n", key, str);
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);
339 c=GetPrivateProfileIntA("Child", "Failures", -1, child_file);
340 trace_(file, line)("Failures=%d\n", c);
345 /***
347 * Helpers to check the ShellExecute() / child process results.
349 ***/
351 static char shell_call[2048];
352 static void WINAPIV __WINE_PRINTF_ATTR(2,3) _okShell(int condition, const char *msg, ...)
354 va_list valist;
355 char buffer[2048];
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);
364 #define okShell_(file, line) (winetest_set_location(file, line), 0) ? (void)0 : _okShell
365 #define okShell okShell_(__FILE__, __LINE__)
367 static char assoc_desc[2048];
368 static void reset_association_description(void)
370 *assoc_desc = '\0';
373 static void okChildString_(const char* file, int line, const char* key, const char* expected, const char* bad)
375 char* result;
376 result=getChildString("Child", key);
377 if (!result)
379 okShell_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
380 return;
382 okShell_(file, line)(lstrcmpiA(result, expected) == 0 ||
383 broken(lstrcmpiA(result, bad) == 0),
384 "%s expected '%s', got '%s'\n", key, expected, result);
386 #define okChildString(key, expected) okChildString_(__FILE__, __LINE__, (key), (expected), (expected))
387 #define okChildStringBroken(key, expected, broken) okChildString_(__FILE__, __LINE__, (key), (expected), (broken))
389 static int StrCmpPath(const char* s1, const char* s2)
391 if (!s1 && !s2) return 0;
392 if (!s2) return 1;
393 if (!s1) return -1;
394 while (*s1)
396 if (!*s2)
398 if (*s1=='.')
399 s1++;
400 return (*s1-*s2);
402 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
404 while (*s1=='/' || *s1=='\\')
405 s1++;
406 while (*s2=='/' || *s2=='\\')
407 s2++;
409 else if (toupper(*s1)==toupper(*s2))
411 s1++;
412 s2++;
414 else
416 return (*s1-*s2);
419 if (*s2=='.')
420 s2++;
421 if (*s2)
422 return -1;
423 return 0;
426 static void okChildPath_(const char* file, int line, const char* key, const char* expected)
428 char* result;
429 int equal, shortequal;
430 result=getChildString("Child", key);
431 if (!result)
433 okShell_(file,line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
434 return;
436 shortequal = FALSE;
437 equal = (StrCmpPath(result, expected) == 0);
438 if (!equal)
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)
446 rc = GetShortPathNameA(expected, altpath, sizeof(altpath));
447 if (0 < rc && rc < sizeof(altpath))
448 shortequal = (StrCmpPath(result, altpath) == 0);
451 okShell_(file,line)(equal || broken(shortequal) /* XP SP1 */,
452 "%s expected '%s', got '%s'\n", key, expected, result);
454 #define okChildPath(key, expected) okChildPath_(__FILE__, __LINE__, (key), (expected))
456 static void okChildInt_(const char* file, int line, const char* key, int expected)
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);
463 #define okChildInt(key, expected) okChildInt_(__FILE__, __LINE__, (key), (expected))
465 static void okChildIntBroken_(const char* file, int line, const char* key, int expected)
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);
472 #define okChildIntBroken(key, expected) okChildIntBroken_(__FILE__, __LINE__, (key), (expected))
475 /***
477 * ShellExecute wrappers
479 ***/
481 static void strcat_param(char* str, const char* name, const char* param)
483 if (param)
485 if (str[strlen(str)-1] == '"')
486 strcat(str, ", ");
487 strcat(str, name);
488 strcat(str, "=\"");
489 strcat(str, param);
490 strcat(str, "\"");
494 static int _todo_wait = 0;
495 #define todo_wait for (_todo_wait = 1; _todo_wait; _todo_wait = 0)
497 static int bad_shellexecute = 0;
499 static INT_PTR shell_execute_(const char* file, int line, LPCSTR verb, LPCSTR filename, LPCSTR parameters, LPCSTR directory)
501 INT_PTR rc, rcEmpty = 0;
503 if(!verb)
504 rcEmpty = shell_execute_(file, line, "", filename, parameters, directory);
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);
516 DeleteFileA(child_file);
517 SetLastError(0xcafebabe);
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.
523 rc=(INT_PTR)ShellExecuteA(NULL, verb, filename, parameters, directory, SW_HIDE);
525 if (rc > 32)
527 int wait_rc;
528 wait_rc=WaitForSingleObject(hEvent, 5000);
529 if (wait_rc == WAIT_TIMEOUT)
531 HWND wnd = FindWindowA("#32770", "Windows");
532 if (!wnd)
533 wnd = FindWindowA("Shell_Flyout", "");
534 if (wnd != NULL)
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;
542 todo_wine_if(_todo_wait)
543 okShell_(file, line)(wait_rc==WAIT_OBJECT_0 || rc <= 32,
544 "WaitForSingleObject returned %d\n", wait_rc);
546 /* The child process may have changed the result file, so let profile
547 * functions know about it
549 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
550 if (GetFileAttributesA(child_file) != INVALID_FILE_ATTRIBUTES)
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");
561 if(!verb)
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: %Iu %Iu\n", rc, rcEmpty);
570 return rc;
572 #define shell_execute(verb, filename, parameters, directory) \
573 shell_execute_(__FILE__, __LINE__, verb, filename, parameters, directory)
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)
580 char smask[11];
581 SHELLEXECUTEINFOA sei;
582 BOOL success;
583 INT_PTR rc;
585 /* Add some flags so we can wait for the child process */
586 mask |= SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE;
588 strcpy(shell_call, "ShellExecuteEx(");
589 sprintf(smask, "0x%lx", 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);
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 sei.hIcon=NULL;
615 sei.hProcess=(HANDLE)0xdeadbeef; /* Out */
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=%Id is not allowed\n",
623 success, rc);
625 if (rc > 32)
627 DWORD wait_rc, rc;
628 if (sei.hProcess!=NULL)
630 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
631 okShell_(file, line)(wait_rc==WAIT_OBJECT_0,
632 "WaitForSingleObject(hProcess) returned %ld\n",
633 wait_rc);
634 wait_rc = GetExitCodeProcess(sei.hProcess, &rc);
635 okShell_(file, line)(wait_rc, "GetExitCodeProcess() failed le=%lu\n", GetLastError());
636 todo_wine_if(_todo_wait)
637 okShell_(file, line)(rc == 0, "child returned %lu\n", rc);
638 CloseHandle(sei.hProcess);
640 wait_rc=WaitForSingleObject(hEvent, 5000);
641 todo_wine_if(_todo_wait)
642 okShell_(file, line)(wait_rc==WAIT_OBJECT_0,
643 "WaitForSingleObject returned %ld\n", wait_rc);
645 else
646 okShell_(file, line)(sei.hProcess==NULL,
647 "returned a process handle %p\n", sei.hProcess);
649 /* The child process may have changed the result file, so let profile
650 * functions know about it
652 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
653 if (rc > 32 && GetFileAttributesA(child_file) != INVALID_FILE_ATTRIBUTES)
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 c = GetPrivateProfileIntA("Child", "Timeout", -1, child_file);
661 if (c > 0)
663 /* Inform caller of the timeout... these two following bits
664 * are directly returned by some Windows10 versions.
665 * Return the same bits when we detect a timeout in child.
667 SetLastError(ERROR_FILE_NOT_FOUND);
668 rc = SE_ERR_DDEFAIL;
670 /* When NOZONECHECKS is specified the environment variables are not
671 * inherited if the process does not have elevated privileges.
673 else if ((mask & SEE_MASK_NOZONECHECKS) && skip_shlexec_tests)
675 okChildInt_(file, line, "ShlexecVarLE", 203);
676 okChildString_(file, line, "ShlexecVar", "", "");
678 else
680 okChildInt_(file, line, "ShlexecVarLE", 0);
681 okChildString_(file, line, "ShlexecVar", "Present", "Present");
685 return rc;
687 #define shell_execute_ex(mask, verb, filename, parameters, directory, class) \
688 shell_execute_ex_(__FILE__, __LINE__, mask, verb, filename, parameters, directory, class)
691 /***
693 * Functions to create / delete associations wrappers
695 ***/
697 static BOOL create_test_class(const char* class, BOOL protocol)
699 HKEY hkey, hkey_shell;
700 LONG rc;
702 rc = RegCreateKeyExA(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
703 KEY_CREATE_SUB_KEY | KEY_SET_VALUE, NULL,
704 &hkey, NULL);
705 ok(rc == ERROR_SUCCESS || rc == ERROR_ACCESS_DENIED,
706 "could not create class %s (rc=%ld)\n", class, rc);
707 if (rc != ERROR_SUCCESS)
708 return FALSE;
710 if (protocol)
712 rc = RegSetValueExA(hkey, "URL Protocol", 0, REG_SZ, (LPBYTE)"", 1);
713 ok(rc == ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %ld\n", class, rc);
716 rc = RegCreateKeyExA(hkey, "shell", 0, NULL, 0,
717 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
718 ok(rc == ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %ld\n", rc);
720 CloseHandle(hkey);
721 CloseHandle(hkey_shell);
722 return TRUE;
725 static BOOL create_test_association_key(const char* extension, const char *class)
727 HKEY hkey;
728 LONG rc;
730 rc = RegCreateKeyExA(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
731 NULL, &hkey, NULL);
732 ok(rc == ERROR_SUCCESS || rc == ERROR_ACCESS_DENIED,
733 "could not create association %s (rc=%ld)\n", class, rc);
734 if (rc != ERROR_SUCCESS)
735 return FALSE;
737 rc = RegSetValueExA(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
738 ok(rc == ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %ld\n", class, rc);
739 CloseHandle(hkey);
740 return TRUE;
743 static BOOL create_test_association(const char* extension)
745 char class[MAX_PATH];
747 sprintf(class, "shlexec%s", extension);
748 if (!create_test_association_key(extension, class))
749 return FALSE;
751 return create_test_class(class, FALSE);
754 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
755 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
757 LONG ret;
758 DWORD dwMaxSubkeyLen, dwMaxValueLen;
759 DWORD dwMaxLen, dwSize;
760 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
761 HKEY hSubKey = hKey;
763 if(lpszSubKey)
765 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
766 if (ret) return ret;
769 /* Get highest length for keys, values */
770 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
771 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
772 if (ret) goto cleanup;
774 dwMaxSubkeyLen++;
775 dwMaxValueLen++;
776 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
777 if (dwMaxLen > ARRAY_SIZE(szNameBuf))
779 /* Name too big: alloc a buffer for it */
780 if (!(lpszName = malloc(dwMaxLen*sizeof(CHAR))))
782 ret = ERROR_NOT_ENOUGH_MEMORY;
783 goto cleanup;
788 /* Recursively delete all the subkeys */
789 while (TRUE)
791 dwSize = dwMaxLen;
792 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
793 NULL, NULL, NULL)) break;
795 ret = myRegDeleteTreeA(hSubKey, lpszName);
796 if (ret) goto cleanup;
799 if (lpszSubKey)
800 ret = RegDeleteKeyA(hKey, lpszSubKey);
801 else
802 while (TRUE)
804 dwSize = dwMaxLen;
805 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
806 NULL, NULL, NULL, NULL)) break;
808 ret = RegDeleteValueA(hKey, lpszName);
809 if (ret) goto cleanup;
812 cleanup:
813 /* Free buffer if allocated */
814 if (lpszName != szNameBuf)
815 free(lpszName);
816 if(lpszSubKey)
817 RegCloseKey(hSubKey);
818 return ret;
821 static void delete_test_class(const char* classname)
823 myRegDeleteTreeA(HKEY_CLASSES_ROOT, classname);
826 static void delete_test_association(const char* extension)
828 char classname[MAX_PATH];
830 sprintf(classname, "shlexec%s", extension);
831 delete_test_class(classname);
832 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
835 static void create_test_verb_dde(const char* classname, const char* verb,
836 int rawcmd, const char* cmdtail, const char *ddeexec,
837 const char *application, const char *topic,
838 const char *ifexec)
840 HKEY hkey_shell, hkey_verb, hkey_cmd;
841 char shell[MAX_PATH];
842 char* cmd;
843 LONG rc;
845 strcpy(assoc_desc, " Assoc ");
846 strcat_param(assoc_desc, "class", classname);
847 strcat_param(assoc_desc, "verb", verb);
848 sprintf(shell, "%d", rawcmd);
849 strcat_param(assoc_desc, "rawcmd", shell);
850 strcat_param(assoc_desc, "cmdtail", cmdtail);
851 strcat_param(assoc_desc, "ddeexec", ddeexec);
852 strcat_param(assoc_desc, "app", application);
853 strcat_param(assoc_desc, "topic", topic);
854 strcat_param(assoc_desc, "ifexec", ifexec);
856 sprintf(shell, "%s\\shell", classname);
857 rc=RegOpenKeyExA(HKEY_CLASSES_ROOT, shell, 0,
858 KEY_CREATE_SUB_KEY, &hkey_shell);
859 ok(rc == ERROR_SUCCESS, "%s key creation failed with %ld\n", shell, rc);
861 rc=RegCreateKeyExA(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
862 NULL, &hkey_verb, NULL);
863 ok(rc == ERROR_SUCCESS, "%s verb key creation failed with %ld\n", verb, rc);
865 rc=RegCreateKeyExA(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
866 NULL, &hkey_cmd, NULL);
867 ok(rc == ERROR_SUCCESS, "\'command\' key creation failed with %ld\n", rc);
869 if (rawcmd)
871 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
873 else
875 cmd = malloc(strlen(argv0) + 10 + strlen(child_file) + 2 + strlen(cmdtail) + 1);
876 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
877 rc=RegSetValueExA(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
878 ok(rc == ERROR_SUCCESS, "setting command failed with %ld\n", rc);
879 free(cmd);
882 if (ddeexec)
884 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
886 rc=RegCreateKeyExA(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
887 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
888 ok(rc == ERROR_SUCCESS, "\'ddeexec\' key creation failed with %ld\n", rc);
889 rc=RegSetValueExA(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
890 strlen(ddeexec)+1);
891 ok(rc == ERROR_SUCCESS, "set value failed with %ld\n", rc);
893 if (application)
895 rc=RegCreateKeyExA(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
896 NULL, &hkey_application, NULL);
897 ok(rc == ERROR_SUCCESS, "\'application\' key creation failed with %ld\n", rc);
899 rc=RegSetValueExA(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
900 strlen(application)+1);
901 ok(rc == ERROR_SUCCESS, "set value failed with %ld\n", rc);
902 CloseHandle(hkey_application);
904 if (topic)
906 rc=RegCreateKeyExA(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
907 NULL, &hkey_topic, NULL);
908 ok(rc == ERROR_SUCCESS, "\'topic\' key creation failed with %ld\n", rc);
909 rc=RegSetValueExA(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
910 strlen(topic)+1);
911 ok(rc == ERROR_SUCCESS, "set value failed with %ld\n", rc);
912 CloseHandle(hkey_topic);
914 if (ifexec)
916 rc=RegCreateKeyExA(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
917 NULL, &hkey_ifexec, NULL);
918 ok(rc == ERROR_SUCCESS, "\'ifexec\' key creation failed with %ld\n", rc);
919 rc=RegSetValueExA(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
920 strlen(ifexec)+1);
921 ok(rc == ERROR_SUCCESS, "set value failed with %ld\n", rc);
922 CloseHandle(hkey_ifexec);
924 CloseHandle(hkey_ddeexec);
927 CloseHandle(hkey_shell);
928 CloseHandle(hkey_verb);
929 CloseHandle(hkey_cmd);
932 /* Creates a class' non-DDE test verb.
933 * This function is meant to be used to create long term test verbs and thus
934 * does not trace them.
936 static void create_test_verb(const char* classname, const char* verb,
937 int rawcmd, const char* cmdtail)
939 create_test_verb_dde(classname, verb, rawcmd, cmdtail, NULL, NULL,
940 NULL, NULL);
941 reset_association_description();
945 /***
947 * GetLongPathNameA equivalent that supports Win95 and WinNT
949 ***/
951 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
953 char tmplongpath[MAX_PATH];
954 const char* p;
955 DWORD sp = 0, lp = 0;
956 DWORD tmplen;
957 WIN32_FIND_DATAA wfd;
958 HANDLE goit;
960 if (!shortpath || !shortpath[0])
961 return 0;
963 if (shortpath[1] == ':')
965 tmplongpath[0] = shortpath[0];
966 tmplongpath[1] = ':';
967 lp = sp = 2;
970 while (shortpath[sp])
972 /* check for path delimiters and reproduce them */
973 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
975 if (!lp || tmplongpath[lp-1] != '\\')
977 /* strip double "\\" */
978 tmplongpath[lp++] = '\\';
980 tmplongpath[lp] = 0; /* terminate string */
981 sp++;
982 continue;
985 p = shortpath + sp;
986 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
988 tmplongpath[lp++] = *p++;
989 tmplongpath[lp++] = *p++;
991 for (; *p && *p != '/' && *p != '\\'; p++);
992 tmplen = p - (shortpath + sp);
993 lstrcpynA(tmplongpath + lp, shortpath + sp, tmplen + 1);
994 /* Check if the file exists and use the existing file name */
995 goit = FindFirstFileA(tmplongpath, &wfd);
996 if (goit == INVALID_HANDLE_VALUE)
997 return 0;
998 FindClose(goit);
999 strcpy(tmplongpath + lp, wfd.cFileName);
1000 lp += strlen(tmplongpath + lp);
1001 sp += tmplen;
1003 tmplen = strlen(shortpath) - 1;
1004 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
1005 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
1006 tmplongpath[lp++] = shortpath[tmplen];
1007 tmplongpath[lp] = 0;
1009 tmplen = strlen(tmplongpath) + 1;
1010 if (tmplen <= longlen)
1012 strcpy(longpath, tmplongpath);
1013 tmplen--; /* length without 0 */
1016 return tmplen;
1020 /***
1022 * Tests
1024 ***/
1026 static const char* testfiles[]=
1028 "%s\\test file.shlexec",
1029 "%s\\%%nasty%% $file.shlexec",
1030 "%s\\test file.noassoc",
1031 "%s\\test file.noassoc.shlexec",
1032 "%s\\test file.shlexec.noassoc",
1033 "%s\\test_shortcut_shlexec.lnk",
1034 "%s\\test_shortcut_exe.lnk",
1035 "%s\\test file.shl",
1036 "%s\\test file.shlfoo",
1037 "%s\\test file.sha",
1038 "%s\\test file.sfe",
1039 "%s\\test file.shlproto",
1040 "%s\\masked file.shlexec",
1041 "%s\\masked",
1042 "%s\\test file.sde",
1043 "%s\\test file.exe",
1044 "%s\\test2.exe",
1045 "%s\\simple.shlexec",
1046 "%s\\drawback_file.noassoc",
1047 "%s\\drawback_file.noassoc foo.shlexec",
1048 "%s\\drawback_nonexist.noassoc foo.shlexec",
1049 NULL
1052 typedef struct
1054 const char* verb;
1055 const char* basename;
1056 int todo;
1057 INT_PTR rc;
1058 } filename_tests_t;
1060 static filename_tests_t filename_tests[]=
1062 /* Test bad / nonexistent filenames */
1063 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
1064 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
1066 /* Standard tests */
1067 {NULL, "%s\\test file.shlexec", 0x0, 33},
1068 {NULL, "%s\\test file.shlexec.", 0x0, 33},
1069 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
1070 {NULL, "%s/test file.shlexec", 0x0, 33},
1072 /* Test filenames with no association */
1073 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
1075 /* Test double extensions */
1076 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
1077 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
1079 /* Test alternate verbs */
1080 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
1081 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
1083 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
1084 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
1086 {"notaverb", "%s\\test file.shlexec", 0x10, SE_ERR_NOASSOC},
1088 {"averb", "%s\\test file.sha", 0x10, 33},
1090 /* Test file masked due to space */
1091 {NULL, "%s\\masked file.shlexec", 0x0, 33},
1092 /* Test if quoting prevents the masking */
1093 {NULL, "%s\\masked file.shlexec", 0x40, 33},
1094 /* Test with incorrect quote */
1095 {NULL, "\"%s\\masked file.shlexec", 0x0, SE_ERR_FNF},
1097 /* Test extension / URI protocol collision */
1098 {NULL, "%s\\test file.shlproto", 0x0, SE_ERR_NOASSOC},
1100 {NULL, NULL, 0}
1103 static filename_tests_t noquotes_tests[]=
1105 /* Test unquoted '%1' thingies */
1106 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
1107 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
1108 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
1110 {NULL, NULL, 0}
1113 static void test_lpFile_parsed(void)
1115 char fileA[MAX_PATH + 38];
1116 INT_PTR rc;
1118 if (skip_shlexec_tests)
1120 skip("No filename parsing tests due to lack of .shlexec association\n");
1121 return;
1124 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on Wine */
1125 sprintf(fileA, "%s\\drawback_file.noassoc foo.shlexec", tmpdir);
1126 rc=shell_execute(NULL, fileA, NULL, NULL);
1127 okShell(rc > 32, "failed: rc=%Iu\n", rc);
1129 /* if quoted, existing "drawback_file.noassoc" does not prevent finding "drawback_file.noassoc foo.shlexec" on Wine */
1130 sprintf(fileA, "\"%s\\drawback_file.noassoc foo.shlexec\"", tmpdir);
1131 rc=shell_execute(NULL, fileA, NULL, NULL);
1132 okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
1133 "failed: rc=%Iu\n", rc);
1135 /* error should be SE_ERR_FNF, not SE_ERR_NOASSOC */
1136 sprintf(fileA, "\"%s\\drawback_file.noassoc\" foo.shlexec", tmpdir);
1137 rc=shell_execute(NULL, fileA, NULL, NULL);
1138 okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
1140 /* ""command"" not works on wine (and real win9x and w2k) */
1141 sprintf(fileA, "\"\"%s\\simple.shlexec\"\"", tmpdir);
1142 rc=shell_execute(NULL, fileA, NULL, NULL);
1143 todo_wine okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win9x/2000 */,
1144 "failed: rc=%Iu\n", rc);
1146 /* nonexistent "drawback_nonexist.noassoc" does not prevent finding "drawback_nonexist.noassoc foo.shlexec" on Wine */
1147 sprintf(fileA, "%s\\drawback_nonexist.noassoc foo.shlexec", tmpdir);
1148 rc=shell_execute(NULL, fileA, NULL, NULL);
1149 okShell(rc > 32, "failed: rc=%Iu\n", rc);
1151 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
1152 rc=shell_execute(NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL);
1153 todo_wine okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
1155 /* quoted */
1156 rc=shell_execute(NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL);
1157 todo_wine okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
1159 /* test SEE_MASK_DOENVSUBST works */
1160 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1161 NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL, NULL);
1162 okShell(rc > 32, "failed: rc=%Iu\n", rc);
1164 /* quoted lpFile does not work on real win95 and nt4 */
1165 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1166 NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL, NULL);
1167 okShell(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
1168 "failed: rc=%Iu\n", rc);
1171 typedef struct
1173 const char* cmd;
1174 const char* args[11];
1175 int todo;
1176 } cmdline_tests_t;
1178 static const cmdline_tests_t cmdline_tests[] =
1180 {"exe",
1181 {"exe", NULL}, 0},
1183 {"exe arg1 arg2 \"arg three\" 'four five` six\\ $even)",
1184 {"exe", "arg1", "arg2", "arg three", "'four", "five`", "six\\", "$even)", NULL}, 0},
1186 {"exe arg=1 arg-2 three\tfour\rfour\nfour ",
1187 {"exe", "arg=1", "arg-2", "three", "four\rfour\nfour", NULL}, 0},
1189 {"exe arg\"one\" \"second\"arg thirdarg ",
1190 {"exe", "argone", "secondarg", "thirdarg", NULL}, 0},
1192 /* Don't lose unclosed quoted arguments */
1193 {"exe arg1 \"unclosed",
1194 {"exe", "arg1", "unclosed", NULL}, 0},
1196 {"exe arg1 \"",
1197 {"exe", "arg1", "", NULL}, 0},
1199 /* cmd's metacharacters have no special meaning */
1200 {"exe \"one^\" \"arg\"&two three|four",
1201 {"exe", "one^", "arg&two", "three|four", NULL}, 0},
1203 /* Environment variables are not interpreted either */
1204 {"exe %TMPDIR% %2",
1205 {"exe", "%TMPDIR%", "%2", NULL}, 0},
1207 /* If not followed by a quote, backslashes go through as is */
1208 {"exe o\\ne t\\\\wo t\\\\\\ree f\\\\\\\\our ",
1209 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1211 {"exe \"o\\ne\" \"t\\\\wo\" \"t\\\\\\ree\" \"f\\\\\\\\our\" ",
1212 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1214 /* When followed by a quote their number is halved and the remainder
1215 * escapes the quote
1217 {"exe \\\"one \\\\\"two\" \\\\\\\"three \\\\\\\\\"four\" end",
1218 {"exe", "\"one", "\\two", "\\\"three", "\\\\four", "end", NULL}, 0},
1220 {"exe \"one\\\" still\" \"two\\\\\" \"three\\\\\\\" still\" \"four\\\\\\\\\" end",
1221 {"exe", "one\" still", "two\\", "three\\\" still", "four\\\\", "end", NULL}, 0},
1223 /* One can put a quote in an unquoted string by tripling it, that is in
1224 * effect quoting it like so """ -> ". The general rule is as follows:
1225 * 3n quotes -> n quotes
1226 * 3n+1 quotes -> n quotes plus start of a quoted string
1227 * 3n+2 quotes -> n quotes (plus an empty string from the remaining pair)
1228 * Nicely, when n is 0 we get the standard rules back.
1230 {"exe two\"\"quotes next",
1231 {"exe", "twoquotes", "next", NULL}, 0},
1233 {"exe three\"\"\"quotes next",
1234 {"exe", "three\"quotes", "next", NULL}, 0},
1236 {"exe four\"\"\"\" quotes\" next 4%3=1",
1237 {"exe", "four\" quotes", "next", "4%3=1", NULL}, 0},
1239 {"exe five\"\"\"\"\"quotes next",
1240 {"exe", "five\"quotes", "next", NULL}, 0},
1242 {"exe six\"\"\"\"\"\"quotes next",
1243 {"exe", "six\"\"quotes", "next", NULL}, 0},
1245 {"exe seven\"\"\"\"\"\"\" quotes\" next 7%3=1",
1246 {"exe", "seven\"\" quotes", "next", "7%3=1", NULL}, 0},
1248 {"exe twelve\"\"\"\"\"\"\"\"\"\"\"\"quotes next",
1249 {"exe", "twelve\"\"\"\"quotes", "next", NULL}, 0},
1251 {"exe thirteen\"\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1252 {"exe", "thirteen\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1254 /* Inside a quoted string the opening quote is added to the set of
1255 * consecutive quotes to get the effective quotes count. This gives:
1256 * 1+3n quotes -> n quotes
1257 * 1+3n+1 quotes -> n quotes plus closes the quoted string
1258 * 1+3n+2 quotes -> n+1 quotes plus closes the quoted string
1260 {"exe \"two\"\"quotes next",
1261 {"exe", "two\"quotes", "next", NULL}, 0},
1263 {"exe \"two\"\" next",
1264 {"exe", "two\"", "next", NULL}, 0},
1266 {"exe \"three\"\"\" quotes\" next 4%3=1",
1267 {"exe", "three\" quotes", "next", "4%3=1", NULL}, 0},
1269 {"exe \"four\"\"\"\"quotes next",
1270 {"exe", "four\"quotes", "next", NULL}, 0},
1272 {"exe \"five\"\"\"\"\"quotes next",
1273 {"exe", "five\"\"quotes", "next", NULL}, 0},
1275 {"exe \"six\"\"\"\"\"\" quotes\" next 7%3=1",
1276 {"exe", "six\"\" quotes", "next", "7%3=1", NULL}, 0},
1278 {"exe \"eleven\"\"\"\"\"\"\"\"\"\"\"quotes next",
1279 {"exe", "eleven\"\"\"\"quotes", "next", NULL}, 0},
1281 {"exe \"twelve\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1282 {"exe", "twelve\"\"\"\" quotes", "next", "13%3=1", NULL}, 0},
1284 /* Escaped consecutive quotes are fun */
1285 {"exe \"the crazy \\\\\"\"\"\\\\\" quotes",
1286 {"exe", "the crazy \\\"\\", "quotes", NULL}, 0},
1288 /* The executable path has its own rules!!!
1289 * - Backslashes have no special meaning.
1290 * - If the first character is a quote, then the second quote ends the
1291 * executable path.
1292 * - The previous rule holds even if the next character is not a space!
1293 * - If the first character is not a quote, then quotes have no special
1294 * meaning either and the executable path stops at the first space.
1295 * - The consecutive quotes rules don't apply either.
1296 * - Even if there is no space between the executable path and the first
1297 * argument, the latter is parsed using the regular rules.
1299 {"exe\"file\"path arg1",
1300 {"exe\"file\"path", "arg1", NULL}, 0},
1302 {"exe\"file\"path\targ1",
1303 {"exe\"file\"path", "arg1", NULL}, 0},
1305 {"exe\"path\\ arg1",
1306 {"exe\"path\\", "arg1", NULL}, 0},
1308 {"\\\"exe \"arg one\"",
1309 {"\\\"exe", "arg one", NULL}, 0},
1311 {"\"spaced exe\" \"next arg\"",
1312 {"spaced exe", "next arg", NULL}, 0},
1314 {"\"spaced exe\"\t\"next arg\"",
1315 {"spaced exe", "next arg", NULL}, 0},
1317 {"\"exe\"arg\" one\" argtwo",
1318 {"exe", "arg one", "argtwo", NULL}, 0},
1320 {"\"spaced exe\\\"arg1 arg2",
1321 {"spaced exe\\", "arg1", "arg2", NULL}, 0},
1323 {"\"two\"\" arg1 ",
1324 {"two", " arg1 ", NULL}, 0},
1326 {"\"three\"\"\" arg2",
1327 {"three", "", "arg2", NULL}, 0},
1329 {"\"four\"\"\"\"arg1",
1330 {"four", "\"arg1", NULL}, 0},
1332 /* If the first character is a space then the executable path is empty */
1333 {" \"arg\"one argtwo",
1334 {"", "argone", "argtwo", NULL}, 0},
1336 {NULL, {NULL}, 0}
1339 static BOOL test_one_cmdline(const cmdline_tests_t* test)
1341 WCHAR cmdW[MAX_PATH], argW[MAX_PATH];
1342 LPWSTR *cl2a;
1343 int cl2a_count;
1344 LPWSTR *argsW;
1345 int i, count;
1347 /* trace("----- cmd='%s'\n", test->cmd); */
1348 MultiByteToWideChar(CP_ACP, 0, test->cmd, -1, cmdW, ARRAY_SIZE(cmdW));
1349 argsW = cl2a = CommandLineToArgvW(cmdW, &cl2a_count);
1350 if (argsW == NULL && cl2a_count == -1)
1352 win_skip("CommandLineToArgvW not implemented, skipping\n");
1353 return FALSE;
1355 ok(!argsW[cl2a_count] || broken(argsW[cl2a_count] != NULL) /* before Vista */,
1356 "expected NULL-terminated list of commandline arguments\n");
1358 count = 0;
1359 while (test->args[count])
1360 count++;
1361 todo_wine_if(test->todo & 0x1)
1362 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1364 for (i = 0; i < cl2a_count; i++)
1366 if (i < count)
1368 MultiByteToWideChar(CP_ACP, 0, test->args[i], -1, argW, ARRAY_SIZE(argW));
1369 todo_wine_if(test->todo & (1 << (i+4)))
1370 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1372 else todo_wine_if(test->todo & 0x1)
1373 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1374 argsW++;
1376 LocalFree(cl2a);
1377 return TRUE;
1380 static void test_commandline2argv(void)
1382 static const WCHAR exeW[] = {'e','x','e',0};
1383 const cmdline_tests_t* test;
1384 WCHAR strW[MAX_PATH];
1385 LPWSTR *args;
1386 int numargs;
1387 DWORD le;
1389 test = cmdline_tests;
1390 while (test->cmd)
1392 if (!test_one_cmdline(test))
1393 return;
1394 test++;
1397 SetLastError(0xdeadbeef);
1398 args = CommandLineToArgvW(exeW, NULL);
1399 le = GetLastError();
1400 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %lu\n", args, le);
1402 SetLastError(0xdeadbeef);
1403 args = CommandLineToArgvW(NULL, NULL);
1404 le = GetLastError();
1405 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %lu\n", args, le);
1407 *strW = 0;
1408 args = CommandLineToArgvW(strW, &numargs);
1409 ok(numargs == 1 || broken(numargs > 1), "expected 1 args, got %d\n", numargs);
1410 ok(!args || (!args[numargs] || broken(args[numargs] != NULL) /* before Vista */),
1411 "expected NULL-terminated list of commandline arguments\n");
1412 if (numargs == 1)
1414 GetModuleFileNameW(NULL, strW, ARRAY_SIZE(strW));
1415 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));
1417 if (args) LocalFree(args);
1420 /* The goal here is to analyze how ShellExecute() builds the command that
1421 * will be run. The tricky part is that there are three transformation
1422 * steps between the 'parameters' string we pass to ShellExecute() and the
1423 * argument list we observe in the child process:
1424 * - The parsing of 'parameters' string into individual arguments. The tests
1425 * show this is done differently from both CreateProcess() and
1426 * CommandLineToArgv()!
1427 * - The way the command 'formatting directives' such as %1, %2, etc are
1428 * handled.
1429 * - And the way the resulting command line is then parsed to yield the
1430 * argument list we check.
1432 typedef struct
1434 const char* verb;
1435 const char* params;
1436 int todo;
1437 const char *cmd;
1438 const char *broken;
1439 } argify_tests_t;
1441 static const argify_tests_t argify_tests[] =
1443 {"ParamsS", "p2 p3 \"p4 ", FALSE, " p2 p3 \"p4 "},
1445 /* Notice that one can reorder and duplicate the parameters.
1446 * Also notice how %* take the raw input parameters string, including
1447 * the trailing spaces, no matter what arguments have already been used. */
1448 {"Params232S", "p2 p3 p4 ", TRUE,
1449 " p2 p3 \"p2\" \"p2 p3 p4 \""},
1451 /* Unquoted argument references like %2 don't automatically quote their
1452 * argument. Similarly, when they are quoted they don't escape the quotes
1453 * that their argument may contain.
1455 {"Params232S", "\"p two\" p3 p4 ", TRUE,
1456 " p two p3 \"p two\" \"\"p two\" p3 p4 \""},
1458 /* Only single digits are supported so only %1 to %9. Shown here with %20
1459 * because %10 is a pain.
1461 {"Params20", "p", FALSE,
1462 " \"p0\""},
1464 /* Only (double-)quotes have a special meaning. */
1465 {"Params23456", "'p2 p3` p4\\ $even", FALSE,
1466 " \"'p2\" \"p3`\" \"p4\\\" \"$even\" \"\""},
1468 {"Params23456", "p=2 p-3 p4\tp4\rp4\np4", TRUE,
1469 " \"p=2\" \"p-3\" \"p4\tp4\rp4\np4\" \"\" \"\""},
1471 /* In unquoted strings, quotes are treated are a parameter separator just
1472 * like spaces! However they can be doubled to get a literal quote.
1473 * Specifically:
1474 * 2n quotes -> n quotes
1475 * 2n+1 quotes -> n quotes and a parameter separator
1477 {"Params23456789", "one\"quote \"p four\" one\"quote p7", TRUE,
1478 " \"one\" \"quote\" \"p four\" \"one\" \"quote\" \"p7\" \"\" \"\""},
1480 {"Params23456789", "two\"\"quotes \"p three\" two\"\"quotes p5", TRUE,
1481 " \"two\"quotes\" \"p three\" \"two\"quotes\" \"p5\" \"\" \"\" \"\" \"\""},
1483 {"Params23456789", "three\"\"\"quotes \"p four\" three\"\"\"quotes p6", TRUE,
1484 " \"three\"\" \"quotes\" \"p four\" \"three\"\" \"quotes\" \"p6\" \"\" \"\""},
1486 {"Params23456789", "four\"\"\"\"quotes \"p three\" four\"\"\"\"quotes p5", TRUE,
1487 " \"four\"\"quotes\" \"p three\" \"four\"\"quotes\" \"p5\" \"\" \"\" \"\" \"\""},
1489 /* Quoted strings cannot be continued by tacking on a non space character
1490 * either.
1492 {"Params23456", "\"p two\"p3 \"p four\"p5 p6", TRUE,
1493 " \"p two\" \"p3\" \"p four\" \"p5\" \"p6\""},
1495 /* In quoted strings, the quotes are halved and an odd number closes the
1496 * string. Specifically:
1497 * 2n quotes -> n quotes
1498 * 2n+1 quotes -> n quotes and closes the string and hence the parameter
1500 {"Params23456789", "\"one q\"uote \"p four\" \"one q\"uote p7", TRUE,
1501 " \"one q\" \"uote\" \"p four\" \"one q\" \"uote\" \"p7\" \"\" \"\""},
1503 {"Params23456789", "\"two \"\" quotes\" \"p three\" \"two \"\" quotes\" p5", TRUE,
1504 " \"two \" quotes\" \"p three\" \"two \" quotes\" \"p5\" \"\" \"\" \"\" \"\""},
1506 {"Params23456789", "\"three q\"\"\"uotes \"p four\" \"three q\"\"\"uotes p7", TRUE,
1507 " \"three q\"\" \"uotes\" \"p four\" \"three q\"\" \"uotes\" \"p7\" \"\" \"\""},
1509 {"Params23456789", "\"four \"\"\"\" quotes\" \"p three\" \"four \"\"\"\" quotes\" p5", TRUE,
1510 " \"four \"\" quotes\" \"p three\" \"four \"\" quotes\" \"p5\" \"\" \"\" \"\" \"\""},
1512 /* The quoted string rules also apply to consecutive quotes at the start
1513 * of a parameter but don't count the opening quote!
1515 {"Params23456789", "\"\"twoquotes \"p four\" \"\"twoquotes p7", TRUE,
1516 " \"\" \"twoquotes\" \"p four\" \"\" \"twoquotes\" \"p7\" \"\" \"\""},
1518 {"Params23456789", "\"\"\"three quotes\" \"p three\" \"\"\"three quotes\" p5", TRUE,
1519 " \"\"three quotes\" \"p three\" \"\"three quotes\" \"p5\" \"\" \"\" \"\" \"\""},
1521 {"Params23456789", "\"\"\"\"fourquotes \"p four\" \"\"\"\"fourquotes p7", TRUE,
1522 " \"\"\" \"fourquotes\" \"p four\" \"\"\" \"fourquotes\" \"p7\" \"\" \"\""},
1524 /* An unclosed quoted string gets lost! */
1525 {"Params23456", "p2 \"p3\" \"p4 is lost", TRUE,
1526 " \"p2\" \"p3\" \"\" \"\" \"\"",
1527 " \"p2\" \"p3\" \"p3\" \"\" \"\""}, /* NT4/2k */
1529 /* Backslashes have no special meaning even when preceding quotes. All
1530 * they do is start an unquoted string.
1532 {"Params23456", "\\\"p\\three \"pfour\\\" pfive", TRUE,
1533 " \"\\\" \"p\\three\" \"pfour\\\" \"pfive\" \"\""},
1535 /* Environment variables are left untouched. */
1536 {"Params23456", "%TMPDIR% %t %c", FALSE,
1537 " \"%TMPDIR%\" \"%t\" \"%c\" \"\" \"\""},
1539 /* %~2 is equivalent to %*. However %~3 and higher include the spaces
1540 * before the parameter!
1541 * (but not the previous parameter's closing quote fortunately)
1543 {"Params2345Etc", "p2 p3 \"p4\" p5 p6 ", TRUE,
1544 " ~2=\"p2 p3 \"p4\" p5 p6 \" ~3=\" p3 \"p4\" p5 p6 \" ~4=\" \"p4\" p5 p6 \" ~5= p5 p6 "},
1546 /* %~n works even if there is no nth parameter. */
1547 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 ", TRUE,
1548 " ~9=\" \""},
1550 {"Params9Etc", "p2 p3 p4 p5 p6 p7 ", TRUE,
1551 " ~9=\"\""},
1553 /* The %~n directives also transmit the tenth parameter and beyond. */
1554 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 and beyond!", TRUE,
1555 " ~9=\" p9 p10 p11 and beyond!\""},
1557 /* Bad formatting directives lose their % sign, except those followed by
1558 * a tilde! Environment variables are not expanded but lose their % sign.
1560 {"ParamsBad", "p2 p3 p4 p5", TRUE,
1561 " \"% - %~ %~0 %~1 %~a %~* a b c TMPDIR\""},
1566 static void test_argify(void)
1568 char fileA[MAX_PATH + 18], params[2 * MAX_PATH + 28];
1569 INT_PTR rc;
1570 const argify_tests_t* test;
1571 const char *bad;
1572 const char* cmd;
1574 /* Test with a long parameter */
1575 for (rc = 0; rc < MAX_PATH; rc++)
1576 fileA[rc] = 'a' + rc % 26;
1577 fileA[MAX_PATH-1] = '\0';
1578 sprintf(params, "shlexec \"%s\" %s", child_file, fileA);
1580 /* We need NOZONECHECKS on Win2003 to block a dialog */
1581 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params, NULL, NULL);
1582 okShell(rc > 32, "failed: rc=%Iu\n", rc);
1583 okChildInt("argcA", 4);
1584 okChildPath("argvA3", fileA);
1586 if (skip_shlexec_tests)
1588 skip("No argify tests due to lack of .shlexec association\n");
1589 return;
1592 create_test_verb("shlexec.shlexec", "ParamsS", 0, "ParamsS %*");
1593 create_test_verb("shlexec.shlexec", "Params232S", 0, "Params232S %2 %3 \"%2\" \"%*\"");
1594 create_test_verb("shlexec.shlexec", "Params23456", 0, "Params23456 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\"");
1595 create_test_verb("shlexec.shlexec", "Params23456789", 0, "Params23456789 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\"");
1596 create_test_verb("shlexec.shlexec", "Params2345Etc", 0, "Params2345Etc ~2=\"%~2\" ~3=\"%~3\" ~4=\"%~4\" ~5=%~5");
1597 create_test_verb("shlexec.shlexec", "Params9Etc", 0, "Params9Etc ~9=\"%~9\"");
1598 create_test_verb("shlexec.shlexec", "Params20", 0, "Params20 \"%20\"");
1599 create_test_verb("shlexec.shlexec", "ParamsBad", 0, "ParamsBad \"%% %- %~ %~0 %~1 %~a %~* %a %b %c %TMPDIR%\"");
1601 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1603 test = argify_tests;
1604 while (test->params)
1606 bad = test->broken ? test->broken : test->cmd;
1608 rc = shell_execute_ex(SEE_MASK_DOENVSUBST, test->verb, fileA, test->params, NULL, NULL);
1609 okShell(rc > 32, "failed: rc=%Iu\n", rc);
1611 cmd = getChildString("Child", "cmdlineA");
1612 /* Our commands are such that the verb immediately precedes the
1613 * part we are interested in.
1615 if (cmd) cmd = strstr(cmd, test->verb);
1616 if (cmd) cmd += strlen(test->verb);
1617 if (!cmd) cmd = "(null)";
1618 todo_wine_if(test->todo)
1619 okShell(!strcmp(cmd, test->cmd) || broken(!strcmp(cmd, bad)),
1620 "expected '%s', got '%s'\n", test->cmd, cmd);
1621 test++;
1625 static void test_filename(void)
1627 char filename[MAX_PATH + 20], curdir[MAX_PATH];
1628 const filename_tests_t* test;
1629 char* c;
1630 INT_PTR rc;
1632 if (skip_shlexec_tests)
1634 skip("No ShellExecute/filename tests due to lack of .shlexec association\n");
1635 return;
1638 GetCurrentDirectoryA(sizeof(curdir), curdir);
1640 SetCurrentDirectoryA(tmpdir);
1641 rc=shell_execute("QuotedLowerL", "simple.shlexec", NULL, NULL);
1642 if (rc > 32)
1643 rc=33;
1644 okShell(rc == 33, "failed: rc=%Id err=%lu\n", rc, GetLastError());
1645 okChildInt("argcA", 5);
1646 okChildString("argvA3", "QuotedLowerL");
1647 strcpy(filename, tmpdir);
1648 strcat(filename, "\\simple.shlexec");
1649 okChildPath("argvA4", filename);
1651 rc=shell_execute("QuotedUpperL", "simple.shlexec", NULL, NULL);
1652 if (rc > 32)
1653 rc=33;
1654 okShell(rc == 33, "failed: rc=%Id err=%lu\n", rc, GetLastError());
1655 okChildInt("argcA", 5);
1656 okChildString("argvA3", "QuotedUpperL");
1657 strcpy(filename, tmpdir);
1658 strcat(filename, "\\simple.shlexec");
1659 okChildPath("argvA4", filename);
1661 SetCurrentDirectoryA(curdir);
1663 test=filename_tests;
1664 while (test->basename)
1666 BOOL quotedfile = FALSE;
1668 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1670 win_skip("Skipping shellexecute of file with unassociated extension\n");
1671 test++;
1672 continue;
1675 sprintf(filename, test->basename, tmpdir);
1676 if (strchr(filename, '/'))
1678 c=filename;
1679 while (*c)
1681 if (*c=='\\')
1682 *c='/';
1683 c++;
1686 if ((test->todo & 0x40)==0)
1688 rc=shell_execute(test->verb, filename, NULL, NULL);
1690 else
1692 char quoted[MAX_PATH + 22];
1694 quotedfile = TRUE;
1695 sprintf(quoted, "\"%s\"", filename);
1696 rc=shell_execute(test->verb, quoted, NULL, NULL);
1698 if (rc > 32)
1699 rc=33;
1700 okShell(rc==test->rc ||
1701 broken(quotedfile && rc == SE_ERR_FNF), /* NT4 */
1702 "failed: rc=%Id err=%lu\n", rc, GetLastError());
1703 if (rc == 33)
1705 const char* verb;
1706 todo_wine_if(test->todo & 0x2)
1707 okChildInt("argcA", 5);
1708 verb=(test->verb ? test->verb : "Open");
1709 todo_wine_if(test->todo & 0x4)
1710 okChildString("argvA3", verb);
1711 todo_wine_if(test->todo & 0x8)
1712 okChildPath("argvA4", filename);
1714 test++;
1717 test=noquotes_tests;
1718 while (test->basename)
1720 sprintf(filename, test->basename, tmpdir);
1721 rc=shell_execute(test->verb, filename, NULL, NULL);
1722 if (rc > 32)
1723 rc=33;
1724 todo_wine_if(test->todo & 0x1)
1725 okShell(rc==test->rc, "failed: rc=%Id err=%lu\n", rc, GetLastError());
1726 if (rc==0)
1728 int count;
1729 const char* verb;
1730 char* str;
1732 verb=(test->verb ? test->verb : "Open");
1733 todo_wine_if(test->todo & 0x4)
1734 okChildString("argvA3", verb);
1736 count=4;
1737 str=filename;
1738 while (1)
1740 char attrib[18];
1741 char* space;
1742 space=strchr(str, ' ');
1743 if (space)
1744 *space='\0';
1745 sprintf(attrib, "argvA%d", count);
1746 todo_wine_if(test->todo & 0x8)
1747 okChildPath(attrib, str);
1748 count++;
1749 if (!space)
1750 break;
1751 str=space+1;
1753 todo_wine_if(test->todo & 0x2)
1754 okChildInt("argcA", count);
1756 test++;
1759 if (dllver.dwMajorVersion != 0)
1761 /* The more recent versions of shell32.dll accept quoted filenames
1762 * while older ones (e.g. 4.00) don't. Still we want to test this
1763 * because IE 6 depends on the new behavior.
1764 * One day we may need to check the exact version of the dll but for
1765 * now making sure DllGetVersion() is present is sufficient.
1767 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1768 rc=shell_execute(NULL, filename, NULL, NULL);
1769 okShell(rc > 32, "failed: rc=%Id err=%lu\n", rc, GetLastError());
1770 okChildInt("argcA", 5);
1771 okChildString("argvA3", "Open");
1772 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1773 okChildPath("argvA4", filename);
1776 sprintf(filename, "\"%s\\test file.sha\"", tmpdir);
1777 rc=shell_execute(NULL, filename, NULL, NULL);
1778 todo_wine okShell(rc > 32, "failed: rc=%Id err=%lu\n", rc, GetLastError());
1779 okChildInt("argcA", 5);
1780 todo_wine okChildString("argvA3", "averb");
1781 sprintf(filename, "%s\\test file.sha", tmpdir);
1782 todo_wine okChildPath("argvA4", filename);
1785 typedef struct
1787 const char* urlprefix;
1788 const char* basename;
1789 int flags;
1790 int todo;
1791 } fileurl_tests_t;
1793 #define URL_SUCCESS 0x1
1794 #define USE_COLON 0x2
1795 #define USE_BSLASH 0x4
1797 static fileurl_tests_t fileurl_tests[]=
1799 /* How many slashes does it take... */
1800 {"file:", "%s\\test file.shlexec", URL_SUCCESS, 0},
1801 {"file:/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1802 {"file://", "%s\\test file.shlexec", URL_SUCCESS, 0},
1803 {"file:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1804 {"File:///", "%s\\test file.shlexec", URL_SUCCESS, 0},
1805 {"file:////", "%s\\test file.shlexec", URL_SUCCESS, 0},
1806 {"file://///", "%s\\test file.shlexec", 0, 0},
1808 /* Test with Windows-style paths */
1809 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_COLON, 0},
1810 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_BSLASH, 0},
1812 /* Check handling of hostnames */
1813 {"file://localhost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1814 {"file://localhost:80/", "%s\\test file.shlexec", 0, 0},
1815 {"file://LocalHost/", "%s\\test file.shlexec", URL_SUCCESS, 0},
1816 {"file://127.0.0.1/", "%s\\test file.shlexec", 0, 0},
1817 {"file://::1/", "%s\\test file.shlexec", 0, 0},
1818 {"file://notahost/", "%s\\test file.shlexec", 0, 0},
1820 /* Environment variables are not expanded in URLs */
1821 {"%urlprefix%", "%s\\test file.shlexec", 0, 0x1},
1822 {"file:///", "%%TMPDIR%%\\test file.shlexec", 0, 0},
1824 /* Test shortcuts vs. URLs */
1825 {"file://///", "%s\\test_shortcut_shlexec.lnk", 0, 0x1c},
1827 /* Confuse things by mixing protocols */
1828 {"file://", "shlproto://foo/bar", USE_COLON, 0},
1830 {NULL, NULL, 0, 0}
1833 static void test_fileurls(void)
1835 char filename[MAX_PATH], fileurl[MAX_PATH], longtmpdir[MAX_PATH];
1836 char command[MAX_PATH];
1837 const fileurl_tests_t* test;
1838 char *s;
1839 INT_PTR rc;
1841 if (skip_shlexec_tests)
1843 skip("No file URL tests due to lack of .shlexec association\n");
1844 return;
1847 rc = shell_execute_ex(SEE_MASK_FLAG_NO_UI, NULL,
1848 "file:///nosuchfile.shlexec", NULL, NULL, NULL);
1849 if (rc > 32)
1851 win_skip("shell32 is too old (likely < 4.72). Skipping the file URL tests\n");
1852 return;
1855 get_long_path_name(tmpdir, longtmpdir, ARRAY_SIZE(longtmpdir));
1856 SetEnvironmentVariableA("urlprefix", "file:///");
1858 test=fileurl_tests;
1859 while (test->basename)
1861 /* Build the file URL */
1862 sprintf(filename, test->basename, longtmpdir);
1863 strcpy(fileurl, test->urlprefix);
1864 strcat(fileurl, filename);
1865 s = fileurl + strlen(test->urlprefix);
1866 while (*s)
1868 if (!(test->flags & USE_COLON) && *s == ':')
1869 *s = '|';
1870 else if (!(test->flags & USE_BSLASH) && *s == '\\')
1871 *s = '/';
1872 s++;
1875 /* Test it first with FindExecutable() */
1876 rc = (INT_PTR)FindExecutableA(fileurl, NULL, command);
1877 ok(rc == SE_ERR_FNF, "FindExecutable(%s) failed: bad rc=%Iu\n", fileurl, rc);
1879 /* Then ShellExecute() */
1880 if ((test->todo & 0x10) == 0)
1881 rc = shell_execute(NULL, fileurl, NULL, NULL);
1882 else todo_wait
1883 rc = shell_execute(NULL, fileurl, NULL, NULL);
1884 if (bad_shellexecute)
1886 win_skip("shell32 is too old (likely 4.72). Skipping the file URL tests\n");
1887 break;
1889 if (test->flags & URL_SUCCESS)
1891 todo_wine_if(test->todo & 0x1)
1892 okShell(rc > 32, "failed: bad rc=%Iu\n", rc);
1894 else
1896 todo_wine_if(test->todo & 0x1)
1897 okShell(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1898 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1899 "failed: bad rc=%Iu\n", rc);
1901 if (rc == 33)
1903 todo_wine_if(test->todo & 0x2)
1904 okChildInt("argcA", 5);
1905 todo_wine_if(test->todo & 0x4)
1906 okChildString("argvA3", "Open");
1907 todo_wine_if(test->todo & 0x8)
1908 okChildPath("argvA4", filename);
1910 test++;
1913 SetEnvironmentVariableA("urlprefix", NULL);
1916 static void test_urls(void)
1918 char url[MAX_PATH + 15];
1919 INT_PTR rc;
1921 if (!create_test_class("fakeproto", FALSE))
1923 skip("Unable to create 'fakeproto' class for URL tests\n");
1924 return;
1926 create_test_verb("fakeproto", "open", 0, "URL %1");
1928 create_test_class("shlpaverb", TRUE);
1929 create_test_verb("shlpaverb", "averb", 0, "PAVerb \"%1\"");
1931 /* Protocols must be properly declared */
1932 rc = shell_execute(NULL, "notaproto://foo", NULL, NULL);
1933 ok(rc == SE_ERR_NOASSOC || broken(rc == SE_ERR_ACCESSDENIED),
1934 "%s returned %Iu\n", shell_call, rc);
1936 rc = shell_execute(NULL, "fakeproto://foo/bar", NULL, NULL);
1937 todo_wine ok(rc == SE_ERR_NOASSOC || broken(rc == SE_ERR_ACCESSDENIED),
1938 "%s returned %Iu\n", shell_call, rc);
1940 /* Here's a real live one */
1941 rc = shell_execute(NULL, "shlproto://foo/bar", NULL, NULL);
1942 ok(rc > 32, "%s failed: rc=%Iu\n", shell_call, rc);
1943 okChildInt("argcA", 5);
1944 okChildString("argvA3", "URL");
1945 okChildString("argvA4", "shlproto://foo/bar");
1947 /* Check default verb detection */
1948 rc = shell_execute(NULL, "shlpaverb://foo/bar", NULL, NULL);
1949 todo_wine ok(rc > 32 || /* XP+IE7 - Win10 */
1950 broken(rc == SE_ERR_NOASSOC), /* XP+IE6 */
1951 "%s failed: rc=%Iu\n", shell_call, rc);
1952 if (rc > 32)
1954 okChildInt("argcA", 5);
1955 todo_wine okChildString("argvA3", "PAVerb");
1956 todo_wine okChildString("argvA4", "shlpaverb://foo/bar");
1959 /* But alternative verbs are a recent feature! */
1960 rc = shell_execute("averb", "shlproto://foo/bar", NULL, NULL);
1961 ok(rc > 32 || /* Win8 - Win10 */
1962 broken(rc == SE_ERR_ACCESSDENIED), /* XP - Win7 */
1963 "%s failed: rc=%Iu\n", shell_call, rc);
1964 if (rc > 32)
1966 okChildString("argvA3", "AVerb");
1967 okChildString("argvA4", "shlproto://foo/bar");
1970 /* A .lnk ending does not turn a URL into a shortcut */
1971 rc = shell_execute(NULL, "shlproto://foo/bar.lnk", NULL, NULL);
1972 ok(rc > 32, "%s failed: rc=%Iu\n", shell_call, rc);
1973 okChildInt("argcA", 5);
1974 okChildString("argvA3", "URL");
1975 okChildString("argvA4", "shlproto://foo/bar.lnk");
1977 /* Neither does a .exe extension */
1978 rc = shell_execute(NULL, "shlproto://foo/bar.exe", NULL, NULL);
1979 ok(rc > 32, "%s failed: rc=%Iu\n", shell_call, rc);
1980 okChildInt("argcA", 5);
1981 okChildString("argvA3", "URL");
1982 okChildString("argvA4", "shlproto://foo/bar.exe");
1984 /* But a class name overrides it */
1985 rc = shell_execute(NULL, "shlproto://foo/bar", "shlexec.shlexec", NULL);
1986 ok(rc > 32, "%s failed: rc=%Iu\n", shell_call, rc);
1987 okChildInt("argcA", 5);
1988 okChildString("argvA3", "URL");
1989 okChildString("argvA4", "shlproto://foo/bar");
1991 /* Environment variables are expanded in URLs (but not in file URLs!) */
1992 rc = shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
1993 NULL, "shlproto://%TMPDIR%/bar", NULL, NULL, NULL);
1994 okShell(rc > 32, "failed: rc=%Iu\n", rc);
1995 okChildInt("argcA", 5);
1996 sprintf(url, "shlproto://%s/bar", tmpdir);
1997 okChildString("argvA3", "URL");
1998 okChildStringBroken("argvA4", url, "shlproto://%TMPDIR%/bar");
2000 /* But only after the path has been identified as a URL */
2001 SetEnvironmentVariableA("urlprefix", "shlproto:///");
2002 rc = shell_execute(NULL, "%urlprefix%foo", NULL, NULL);
2003 todo_wine ok(rc == SE_ERR_FNF, "%s returned %Iu\n", shell_call, rc);
2004 SetEnvironmentVariableA("urlprefix", NULL);
2006 delete_test_class("fakeproto");
2007 delete_test_class("shlpaverb");
2010 static void test_find_executable(void)
2012 char curdir[MAX_PATH];
2013 char notepad_path[MAX_PATH];
2014 char filename[MAX_PATH + 17];
2015 char command[MAX_PATH];
2016 char *basename = strrchr(argv0, '\\') + 1;
2017 const filename_tests_t* test;
2018 INT_PTR rc;
2020 if (!create_test_association(".sfe"))
2022 skip("Unable to create association for '.sfe'\n");
2023 return;
2025 create_test_verb("shlexec.sfe", "Open", 1, "%1");
2027 /* Don't test FindExecutable(..., NULL), it always crashes */
2029 strcpy(command, "your word");
2030 if (0) /* Can crash on Vista! */
2032 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
2033 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %Id\n", rc);
2034 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
2037 GetSystemDirectoryA( notepad_path, MAX_PATH );
2038 strcat( notepad_path, "\\notepad.exe" );
2040 /* Search for something that should be in the system-wide search path (no default directory) */
2041 strcpy(command, "your word");
2042 rc=(INT_PTR)FindExecutableA("notepad.exe", NULL, command);
2043 ok(rc > 32, "FindExecutable(%s) returned %Id\n", "notepad.exe", rc);
2044 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
2046 /* Search for something that should be in the system-wide search path (with default directory) */
2047 strcpy(command, "your word");
2048 rc=(INT_PTR)FindExecutableA("notepad.exe", tmpdir, command);
2049 ok(rc > 32, "FindExecutable(%s) returned %Id\n", "notepad.exe", rc);
2050 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
2052 strcpy(command, "your word");
2053 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
2054 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %Id\n", rc);
2055 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
2057 /* Search for the current executabe itself */
2058 strcpy(command, "your word");
2059 rc=(INT_PTR)FindExecutableA(argv0, NULL, command);
2060 ok(rc > 32, "FindExecutable(%s) returned %Id\n", argv0, rc);
2062 /* Make sure FindExecutable uses the correct current directory */
2063 GetCurrentDirectoryA(MAX_PATH, curdir);
2064 SetCurrentDirectoryA(tmpdir);
2065 rc=(INT_PTR)FindExecutableA(basename, NULL, command);
2066 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %Id\n", basename, rc);
2067 SetCurrentDirectoryA(curdir);
2069 sprintf(filename, "%s\\test file.sfe", tmpdir);
2070 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2071 ok(rc > 32, "FindExecutable(%s) returned %Id\n", filename, rc);
2072 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
2074 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
2075 ok(rc > 32, "FindExecutable(%s) returned %Id\n", filename, rc);
2077 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
2078 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %Id\n", filename, rc);
2080 delete_test_association(".sfe");
2082 if (!create_test_association(".shl"))
2084 skip("Unable to create association for '.shl'\n");
2085 return;
2087 create_test_verb("shlexec.shl", "Open", 0, "Open");
2089 sprintf(filename, "%s\\test file.shl", tmpdir);
2090 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2091 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %Id\n", filename, rc);
2093 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
2094 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2096 delete_test_association(".shl");
2098 if (rc > 32)
2100 /* On Windows XP and 2003 FindExecutable() is completely broken.
2101 * Probably what it does is convert the filename to 8.3 format,
2102 * which as a side effect converts the '.shlfoo' extension to '.shl',
2103 * and then tries to find an association for '.shl'. This means it
2104 * will normally fail on most extensions with more than 3 characters,
2105 * like '.mpeg', etc.
2106 * Also it means we cannot do any other test.
2108 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
2109 return;
2112 if (skip_shlexec_tests)
2114 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
2115 return;
2118 test=filename_tests;
2119 while (test->basename)
2121 sprintf(filename, test->basename, tmpdir);
2122 if (strchr(filename, '/'))
2124 char* c;
2125 c=filename;
2126 while (*c)
2128 if (*c=='\\')
2129 *c='/';
2130 c++;
2133 /* Win98 does not '\0'-terminate command! */
2134 memset(command, '\0', sizeof(command));
2135 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
2136 if (rc > 32)
2137 rc=33;
2138 todo_wine_if(test->todo & 0x10)
2139 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%Id\n", filename, rc);
2140 if (rc > 32)
2142 BOOL equal;
2143 equal=strcmp(command, argv0) == 0 ||
2144 /* NT4 returns an extra 0x8 character! */
2145 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
2146 todo_wine_if(test->todo & 0x20)
2147 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
2148 filename, command, argv0);
2150 test++;
2155 static filename_tests_t lnk_tests[]=
2157 /* Pass bad / nonexistent filenames as a parameter */
2158 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
2159 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
2161 /* Pass regular paths as a parameter */
2162 {NULL, "%s\\test file.shlexec", 0xa, 33},
2163 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
2165 /* Pass filenames with no association as a parameter */
2166 {NULL, "%s\\test file.noassoc", 0xa, 33},
2168 {NULL, NULL, 0}
2171 static void test_lnks(void)
2173 char filename[MAX_PATH + 26];
2174 char params[MAX_PATH + 18];
2175 const filename_tests_t* test;
2176 INT_PTR rc;
2178 if (skip_shlexec_tests)
2179 skip("No FindExecutable/filename tests due to lack of .shlexec association\n");
2180 else
2182 /* Should open through our association */
2183 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2184 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2185 okShell(rc > 32, "failed: rc=%Iu err=%lu\n", rc, GetLastError());
2186 okChildInt("argcA", 5);
2187 okChildString("argvA3", "Open");
2188 sprintf(params, "%s\\test file.shlexec", tmpdir);
2189 get_long_path_name(params, filename, sizeof(filename));
2190 okChildPath("argvA4", filename);
2192 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_DOENVSUBST, NULL, "%TMPDIR%\\test_shortcut_shlexec.lnk", NULL, NULL, NULL);
2193 okShell(rc > 32, "failed: rc=%Iu err=%lu\n", rc, GetLastError());
2194 okChildInt("argcA", 5);
2195 okChildString("argvA3", "Open");
2196 sprintf(params, "%s\\test file.shlexec", tmpdir);
2197 get_long_path_name(params, filename, sizeof(filename));
2198 okChildPath("argvA4", filename);
2201 /* Should just run our executable */
2202 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2203 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2204 okShell(rc > 32, "failed: rc=%Iu err=%lu\n", rc, GetLastError());
2205 okChildInt("argcA", 4);
2206 okChildString("argvA3", "Lnk");
2208 if (!skip_shlexec_tests)
2210 /* An explicit class overrides lnk's ContextMenuHandler */
2211 rc=shell_execute_ex(SEE_MASK_CLASSNAME | SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, "shlexec.shlexec");
2212 okShell(rc > 32, "failed: rc=%Iu err=%lu\n", rc, GetLastError());
2213 okChildInt("argcA", 5);
2214 okChildString("argvA3", "Open");
2215 okChildPath("argvA4", filename);
2218 if (dllver.dwMajorVersion>=6)
2220 char* c;
2221 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
2222 * Older versions don't or are quite buggy in this regard.
2224 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2225 c=filename;
2226 while (*c)
2228 if (*c=='\\')
2229 *c='/';
2230 c++;
2232 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
2233 okShell(rc > 32, "failed: rc=%Iu err=%lu\n", rc, GetLastError());
2234 okChildInt("argcA", 4);
2235 okChildString("argvA3", "Lnk");
2238 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2239 test=lnk_tests;
2240 while (test->basename)
2242 params[0]='\"';
2243 sprintf(params+1, test->basename, tmpdir);
2244 strcat(params,"\"");
2245 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
2246 NULL, NULL);
2247 if (rc > 32)
2248 rc=33;
2249 todo_wine_if(test->todo & 0x1)
2250 okShell(rc==test->rc, "failed: rc=%Iu err=%lu\n", rc, GetLastError());
2251 if (rc==0)
2253 todo_wine_if(test->todo & 0x2)
2254 okChildInt("argcA", 5);
2255 todo_wine_if(test->todo & 0x4)
2256 okChildString("argvA3", "Lnk");
2257 sprintf(params, test->basename, tmpdir);
2258 okChildPath("argvA4", params);
2260 test++;
2265 static void test_exes(void)
2267 char filename[2 * MAX_PATH + 17];
2268 char params[1024];
2269 char curdir[MAX_PATH];
2270 char relative_basename[MAX_PATH];
2271 char *basename = strrchr(argv0, '\\') + 1, *dirname = strdup(argv0);
2272 INT_PTR rc;
2274 *strrchr(dirname, '\\') = '\0';
2275 sprintf(params, "shlexec \"%s\" Exec", child_file);
2277 /* We need NOZONECHECKS on Win2003 to block a dialog */
2278 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
2279 NULL, NULL);
2280 okShell(rc > 32, "returned %Iu\n", rc);
2281 okChildInt("argcA", 4);
2282 okChildString("argvA3", "Exec");
2284 /* Check non-filespec paths */
2285 snprintf(relative_basename, ARRAY_SIZE(relative_basename), ".\\\\%s", basename);
2286 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS | SEE_MASK_FLAG_NO_UI, NULL, relative_basename, params,
2287 dirname, NULL);
2288 okShell(rc > 32, "returned %Iu\n", rc);
2289 okChildInt("argcA", 4);
2290 okChildString("argvA3", "Exec");
2291 free(dirname);
2293 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS | SEE_MASK_CLASSNAME | SEE_MASK_FLAG_NO_UI, NULL, argv0, params,
2294 NULL, ".exe");
2295 okShell(rc > 32, "returned %Iu\n", rc);
2296 okChildInt("argcA", 4);
2297 okChildString("argvA3", "Exec");
2299 if (create_test_association_key(".qqqq", "exefile"))
2301 rc = shell_execute_ex(SEE_MASK_NOZONECHECKS | SEE_MASK_CLASSNAME | SEE_MASK_FLAG_NO_UI, NULL, argv0, params,
2302 NULL, ".qqqq");
2303 okShell(rc > 32, "returned %Iu\n", rc);
2304 okChildInt("argcA", 4);
2305 okChildString("argvA3", "Exec");
2306 delete_test_association(".qqqq");
2308 else
2310 skip("Could not create associtation.\n");
2313 if (create_test_association_key("qqqq", "exefile"))
2315 rc = shell_execute_ex(SEE_MASK_NOZONECHECKS | SEE_MASK_CLASSNAME | SEE_MASK_FLAG_NO_UI, NULL, argv0, params,
2316 NULL, "qqqq");
2317 okShell(rc < 32, "returned %Iu\n", rc);
2318 todo_wine okShell(rc == SE_ERR_NOASSOC, "returned %Iu\n", rc);
2319 delete_test_association("qqqq");
2321 else
2323 skip("Could not create associtation.\n");
2326 if (is_elevated)
2328 rc = shell_execute_ex(SEE_MASK_NOZONECHECKS | SEE_MASK_CLASSNAME | SEE_MASK_FLAG_NO_UI, "runas", argv0, params,
2329 NULL, ".exe");
2330 okShell(rc > 32, "returned %Iu\n", rc);
2331 okChildInt("argcA", 4);
2332 okChildString("argvA3", "Exec");
2334 else
2336 skip("No admin privileges, skipping runas test.\n");
2339 if (! skip_noassoc_tests)
2341 sprintf(filename, "%s\\test file.noassoc", tmpdir);
2342 if (CopyFileA(argv0, filename, FALSE))
2344 rc=shell_execute(NULL, filename, params, NULL);
2345 okShell(rc==SE_ERR_NOASSOC, "returned %Iu\n", rc);
2348 else
2350 win_skip("Skipping shellexecute of file with unassociated extension\n");
2353 /* test combining executable and parameters */
2354 sprintf(filename, "%s shlexec \"%s\" Exec", argv0, child_file);
2355 rc = shell_execute(NULL, filename, NULL, NULL);
2356 okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
2358 sprintf(filename, "\"%s\" shlexec \"%s\" Exec", argv0, child_file);
2359 rc = shell_execute(NULL, filename, NULL, NULL);
2360 okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
2362 /* A verb, even if invalid, overrides the normal handling of executables */
2363 todo_wait rc = shell_execute_ex(SEE_MASK_FLAG_NO_UI,
2364 "notaverb", argv0, NULL, NULL, NULL);
2365 todo_wine okShell(rc == SE_ERR_NOASSOC, "returned %Iu\n", rc);
2367 /* Check the correct search path is used */
2368 GetCurrentDirectoryA(MAX_PATH, curdir);
2369 SetCurrentDirectoryA(tmpdir);
2370 rc = shell_execute(NULL, basename, params, NULL);
2371 okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
2372 SetCurrentDirectoryA(curdir);
2374 if (!skip_shlexec_tests)
2376 /* A class overrides the normal handling of executables too */
2377 /* FIXME SEE_MASK_FLAG_NO_UI is only needed due to Wine's bug */
2378 rc = shell_execute_ex(SEE_MASK_CLASSNAME | SEE_MASK_FLAG_NO_UI,
2379 NULL, argv0, NULL, NULL, ".shlexec");
2380 okShell(rc > 32, "returned %Iu\n", rc);
2381 okChildInt("argcA", 5);
2382 okChildString("argvA3", "Open");
2383 okChildPath("argvA4", argv0);
2387 typedef struct
2389 const char* command;
2390 const char* ddeexec;
2391 const char* application;
2392 const char* topic;
2393 const char* ifexec;
2394 int expectedArgs;
2395 const char* expectedDdeExec;
2396 BOOL broken;
2397 } dde_tests_t;
2399 static dde_tests_t dde_tests[] =
2401 /* Test passing and not passing command-line
2402 * argument, no DDE */
2403 {"", NULL, NULL, NULL, NULL, 0, ""},
2404 {"\"%1\"", NULL, NULL, NULL, NULL, 1, ""},
2406 /* Test passing and not passing command-line
2407 * argument, with DDE */
2408 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, 0, "[open(\"%s\")]"},
2409 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, 1, "[open(\"%s\")]"},
2411 /* Test unquoted %1 in command and ddeexec
2412 * (test filename has space) */
2413 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", TRUE /* before vista */},
2415 /* Test ifexec precedence over ddeexec */
2416 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", 0, "[ifexec(\"%s\")]"},
2418 /* Test default DDE topic */
2419 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, 0, "[open(\"%s\")]"},
2421 /* Test default DDE application */
2422 {"", "[open(\"%1\")]", NULL, "dde", NULL, 0, "[open(\"%s\")]"},
2424 {NULL}
2427 static int waitforinputidle_count;
2428 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
2430 waitforinputidle_count++;
2431 if (winetest_debug > 1)
2432 trace("WaitForInputIdle() waiting for dde event timeout=min(%lu,5s)\n", timeout);
2433 timeout = timeout < 5000 ? timeout : 5000;
2434 return WaitForSingleObject(dde_ready_event, timeout);
2438 * WaitForInputIdle() will normally return immediately for console apps. That's
2439 * a problem for us because ShellExecute will assume that an app is ready to
2440 * receive DDE messages after it has called WaitForInputIdle() on that app.
2441 * To work around that we install our own version of WaitForInputIdle() that
2442 * will wait for the child to explicitly tell us that it is ready. We do that
2443 * by changing the entry for WaitForInputIdle() in the shell32 import address
2444 * table.
2446 static void hook_WaitForInputIdle(DWORD (WINAPI *new_func)(HANDLE, DWORD))
2448 char *base;
2449 PIMAGE_NT_HEADERS nt_headers;
2450 DWORD import_directory_rva;
2451 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
2452 int hook_count = 0;
2454 base = (char *) GetModuleHandleA("shell32.dll");
2455 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
2456 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
2458 /* Search for the correct imported module by walking the import descriptors */
2459 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
2460 while (import_descriptor->OriginalFirstThunk != 0)
2462 char *import_module_name;
2464 import_module_name = base + import_descriptor->Name;
2465 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
2466 lstrcmpiA(import_module_name, "user32") == 0)
2468 PIMAGE_THUNK_DATA int_entry;
2469 PIMAGE_THUNK_DATA iat_entry;
2471 /* The import name table and import address table are two parallel
2472 * arrays. We need the import name table to find the imported
2473 * routine and the import address table to patch the address, so
2474 * walk them side by side */
2475 int_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->OriginalFirstThunk);
2476 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
2477 while (int_entry->u1.Ordinal != 0)
2479 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
2481 PIMAGE_IMPORT_BY_NAME import_by_name;
2482 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
2483 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
2485 /* Found the correct routine in the correct imported module. Patch it. */
2486 DWORD old_prot;
2487 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
2488 iat_entry->u1.Function = (ULONG_PTR) new_func;
2489 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
2490 if (winetest_debug > 1)
2491 trace("Hooked %s.WaitForInputIdle\n", import_module_name);
2492 hook_count++;
2493 break;
2496 int_entry++;
2497 iat_entry++;
2499 break;
2502 import_descriptor++;
2504 ok(hook_count, "Could not hook WaitForInputIdle()\n");
2507 static void test_dde(void)
2509 char filename[MAX_PATH + 14], defApplication[MAX_PATH];
2510 const dde_tests_t* test;
2511 char params[1024];
2512 INT_PTR rc;
2513 HANDLE map;
2514 char *shared_block;
2515 DWORD ddeflags;
2517 hook_WaitForInputIdle(hooked_WaitForInputIdle);
2519 sprintf(filename, "%s\\test file.sde", tmpdir);
2521 /* Default service is application name minus path and extension */
2522 strcpy(defApplication, strrchr(argv0, '\\')+1);
2523 *strchr(defApplication, '.') = 0;
2525 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
2526 4096, "winetest_shlexec_dde_map");
2527 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
2529 ddeflags = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
2530 test = dde_tests;
2531 while (test->command)
2533 if (!create_test_association(".sde"))
2535 skip("Unable to create association for '.sde'\n");
2536 return;
2538 create_test_verb_dde("shlexec.sde", "Open", 0, test->command, test->ddeexec,
2539 test->application, test->topic, test->ifexec);
2541 if (test->application != NULL || test->topic != NULL)
2543 strcpy(shared_block, test->application ? test->application : defApplication);
2544 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
2546 else
2548 shared_block[0] = '\0';
2549 shared_block[1] = '\0';
2551 ddeExec[0] = 0;
2553 waitforinputidle_count = 0;
2554 dde_ready_event = CreateEventA(NULL, TRUE, FALSE, "winetest_shlexec_dde_ready");
2555 rc = shell_execute_ex(ddeflags, NULL, filename, NULL, NULL, NULL);
2556 CloseHandle(dde_ready_event);
2557 if (!(ddeflags & SEE_MASK_WAITFORINPUTIDLE) && rc == SE_ERR_DDEFAIL &&
2558 GetLastError() == ERROR_FILE_NOT_FOUND &&
2559 strcmp(winetest_platform, "windows") == 0)
2561 /* Windows 10 does not call WaitForInputIdle() for DDE which creates
2562 * a race condition as the DDE server may not have time to start up.
2563 * When that happens the test fails with the above results and we
2564 * compensate by forcing the WaitForInputIdle() call.
2566 trace("Adding SEE_MASK_WAITFORINPUTIDLE for Windows 10\n");
2567 ddeflags |= SEE_MASK_WAITFORINPUTIDLE;
2568 delete_test_association(".sde");
2569 Sleep(CHILD_DDE_TIMEOUT);
2570 continue;
2572 okShell(32 < rc, "failed: rc=%Iu err=%lu\n", rc, GetLastError());
2573 if (test->ddeexec)
2574 okShell(waitforinputidle_count == 1 ||
2575 broken(waitforinputidle_count == 0) /* Win10 race */,
2576 "WaitForInputIdle() was called %u times\n",
2577 waitforinputidle_count);
2578 else
2579 okShell(waitforinputidle_count == 0, "WaitForInputIdle() was called %u times for a non-DDE case\n", waitforinputidle_count);
2581 if (32 < rc)
2583 if (test->broken)
2584 okChildIntBroken("argcA", test->expectedArgs + 3);
2585 else
2586 okChildInt("argcA", test->expectedArgs + 3);
2588 if (test->expectedArgs == 1) okChildPath("argvA3", filename);
2590 sprintf(params, test->expectedDdeExec, filename);
2591 okChildPath("ddeExec", params);
2593 reset_association_description();
2595 delete_test_association(".sde");
2596 test++;
2599 UnmapViewOfFile(shared_block);
2600 CloseHandle(map);
2601 hook_WaitForInputIdle((void *) WaitForInputIdle);
2604 #define DDE_DEFAULT_APP_VARIANTS 3
2605 typedef struct
2607 const char* command;
2608 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
2609 int todo;
2610 int rc[DDE_DEFAULT_APP_VARIANTS];
2611 } dde_default_app_tests_t;
2613 static dde_default_app_tests_t dde_default_app_tests[] =
2615 /* There are three possible sets of results: Windows <= 2000, XP SP1 and
2616 * >= XP SP2. Use the first two tests to determine which results to expect.
2619 /* Test unquoted existing filename with a space */
2620 {"%s\\test file.exe", {"test file", "test file", "test"}, 0x0, {33, 33, 33}},
2621 {"%s\\test2 file.exe", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2623 /* Test unquoted existing filename with a space */
2624 {"%s\\test file.exe param", {"test file", "test file", "test"}, 0x0, {33, 33, 33}},
2626 /* Test quoted existing filename with a space */
2627 {"\"%s\\test file.exe\"", {"test file", "test file", "test file"}, 0x0, {33, 33, 33}},
2628 {"\"%s\\test file.exe\" param", {"test file", "test file", "test file"}, 0x0, {33, 33, 33}},
2630 /* Test unquoted filename with a space that doesn't exist, but
2631 * test2.exe does */
2632 {"%s\\test2 file.exe param", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2634 /* Test quoted filename with a space that does not exist */
2635 {"\"%s\\test2 file.exe\"", {"", "", "test2 file"}, 0x0, {5, 2, 33}},
2636 {"\"%s\\test2 file.exe\" param", {"", "", "test2 file"}, 0x0, {5, 2, 33}},
2638 /* Test filename supplied without the extension */
2639 {"%s\\test2", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2640 {"%s\\test2 param", {"test2", "", "test2"}, 0x0, {33, 5, 33}},
2642 /* Test an unquoted nonexistent filename */
2643 {"%s\\notexist.exe", {"", "", "notexist"}, 0x0, {5, 2, 33}},
2644 {"%s\\notexist.exe param", {"", "", "notexist"}, 0x0, {5, 2, 33}},
2646 /* Test an application that will be found on the path */
2647 {"cmd", {"cmd", "cmd", "cmd"}, 0x0, {33, 33, 33}},
2648 {"cmd param", {"cmd", "cmd", "cmd"}, 0x0, {33, 33, 33}},
2650 /* Test an application that will not be found on the path */
2651 {"xyzwxyzwxyz", {"", "", "xyzwxyzwxyz"}, 0x0, {5, 2, 33}},
2652 {"xyzwxyzwxyz param", {"", "", "xyzwxyzwxyz"}, 0x0, {5, 2, 33}},
2654 {NULL, {NULL}, 0, {0}}
2657 typedef struct
2659 char *filename;
2660 DWORD threadIdParent;
2661 } dde_thread_info_t;
2663 static DWORD CALLBACK ddeThread(LPVOID arg)
2665 dde_thread_info_t *info = arg;
2666 assert(info && info->filename);
2667 PostThreadMessageA(info->threadIdParent,
2668 WM_QUIT,
2669 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL, NULL),
2671 ExitThread(0);
2674 static void test_dde_default_app(void)
2676 char filename[MAX_PATH + 14];
2677 HSZ hszApplication;
2678 dde_thread_info_t info = { filename, GetCurrentThreadId() };
2679 const dde_default_app_tests_t* test;
2680 char params[1024];
2681 DWORD threadId;
2682 MSG msg;
2683 INT_PTR rc;
2684 int which = 0;
2685 HDDEDATA ret;
2686 BOOL b;
2688 post_quit_on_execute = FALSE;
2689 ddeInst = 0;
2690 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
2691 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0);
2692 ok(rc == DMLERR_NO_ERROR, "got %Ix\n", rc);
2694 sprintf(filename, "%s\\test file.sde", tmpdir);
2696 /* It is strictly not necessary to register an application name here, but wine's
2697 * DdeNameService implementation complains if 0 is passed instead of
2698 * hszApplication with DNS_FILTEROFF */
2699 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2700 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2701 ok(hszApplication && hszTopic, "got %p and %p\n", hszApplication, hszTopic);
2702 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER | DNS_FILTEROFF);
2703 ok(ret != 0, "got %p\n", ret);
2705 test = dde_default_app_tests;
2706 while (test->command)
2708 HANDLE thread;
2710 if (!create_test_association(".sde"))
2712 skip("Unable to create association for '.sde'\n");
2713 return;
2715 sprintf(params, test->command, tmpdir);
2716 create_test_verb_dde("shlexec.sde", "Open", 1, params, "[test]", NULL,
2717 "shlexec", NULL);
2718 ddeApplication[0] = 0;
2720 /* No application will be run as we will respond to the first DDE event,
2721 * so don't wait for it */
2722 SetEvent(hEvent);
2724 thread = CreateThread(NULL, 0, ddeThread, &info, 0, &threadId);
2725 ok(thread != NULL, "got %p\n", thread);
2726 while (GetMessageA(&msg, NULL, 0, 0)) DispatchMessageA(&msg);
2727 rc = msg.wParam > 32 ? 33 : msg.wParam;
2729 /* The first two tests determine which set of results to expect.
2730 * First check the platform as only the first set of results is
2731 * acceptable for Wine.
2733 if (strcmp(winetest_platform, "wine"))
2735 if (test == dde_default_app_tests)
2737 if (strcmp(ddeApplication, test->expectedDdeApplication[0]))
2738 which = 2;
2740 else if (test == dde_default_app_tests + 1)
2742 if (which == 0 && rc == test->rc[1])
2743 which = 1;
2744 trace("DDE result variant %d\n", which);
2748 todo_wine_if(test->todo & 0x1)
2749 okShell(rc==test->rc[which], "failed: rc=%Iu err=%lu\n",
2750 rc, GetLastError());
2751 if (rc == 33)
2753 todo_wine_if(test->todo & 0x2)
2754 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2755 "Expected application '%s', got '%s'\n",
2756 test->expectedDdeApplication[which], ddeApplication);
2758 reset_association_description();
2760 delete_test_association(".sde");
2761 test++;
2764 ret = DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
2765 ok(ret != 0, "got %p\n", ret);
2766 b = DdeFreeStringHandle(ddeInst, hszTopic);
2767 ok(b, "got %d\n", b);
2768 b = DdeFreeStringHandle(ddeInst, hszApplication);
2769 ok(b, "got %d\n", b);
2770 b = DdeUninitialize(ddeInst);
2771 ok(b, "got %d\n", b);
2774 static void init_test(void)
2776 HMODULE hdll;
2777 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
2778 char filename[MAX_PATH + 26];
2779 WCHAR lnkfile[MAX_PATH];
2780 char params[1024];
2781 const char* const * testfile;
2782 lnk_desc_t desc;
2783 DWORD rc;
2784 HRESULT r;
2785 TOKEN_ELEVATION elevation;
2786 HANDLE token;
2787 BOOL ret;
2789 hdll=GetModuleHandleA("shell32.dll");
2790 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2791 if (pDllGetVersion)
2793 dllver.cbSize=sizeof(dllver);
2794 pDllGetVersion(&dllver);
2795 trace("major=%ld minor=%ld build=%ld platform=%ld\n",
2796 dllver.dwMajorVersion, dllver.dwMinorVersion,
2797 dllver.dwBuildNumber, dllver.dwPlatformID);
2799 else
2801 memset(&dllver, 0, sizeof(dllver));
2804 r = CoInitialize(NULL);
2805 ok(r == S_OK, "CoInitialize failed (0x%08lx)\n", r);
2806 if (FAILED(r))
2807 exit(1);
2809 rc=GetModuleFileNameA(NULL, argv0, sizeof(argv0));
2810 ok(rc != 0 && rc < sizeof(argv0), "got %ld\n", rc);
2811 if (GetFileAttributesA(argv0)==INVALID_FILE_ATTRIBUTES)
2813 strcat(argv0, ".so");
2814 ok(GetFileAttributesA(argv0)!=INVALID_FILE_ATTRIBUTES,
2815 "unable to find argv0!\n");
2818 /* Older versions (win 2k) fail tests if there is a space in
2819 the path. */
2820 if (dllver.dwMajorVersion <= 5)
2821 strcpy(tmpdir, "c:\\");
2822 else
2823 GetTempPathA(sizeof(tmpdir), tmpdir);
2824 GetLongPathNameA(tmpdir, tmpdir, sizeof(tmpdir));
2826 /* In case of a failure it is necessary to show the path that was passed to
2827 * ShellExecute(). That means the paths must not be randomized so as not to
2828 * prevent the TestBot from detecting new failures.
2830 strcat(tmpdir, "wtShlexecDir");
2831 DeleteFileA( tmpdir );
2832 rc = CreateDirectoryA( tmpdir, NULL );
2833 ok( rc || GetLastError() == ERROR_ALREADY_EXISTS,
2834 "failed to create %s err %lu\n", tmpdir, GetLastError() );
2835 /* Set %TMPDIR% for the tests */
2836 SetEnvironmentVariableA("TMPDIR", tmpdir);
2838 strcpy(child_file, tmpdir);
2839 strcat(child_file, "\\wtShlexecFile");
2840 init_event(child_file);
2842 /* Set up the test files */
2843 testfile=testfiles;
2844 while (*testfile)
2846 HANDLE hfile;
2848 sprintf(filename, *testfile, tmpdir);
2849 hfile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2850 FILE_ATTRIBUTE_NORMAL, NULL);
2851 if (hfile==INVALID_HANDLE_VALUE)
2853 trace("unable to create '%s': err=%lu\n", filename, GetLastError());
2854 assert(0);
2856 CloseHandle(hfile);
2857 testfile++;
2860 /* Setup the test shortcuts */
2861 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2862 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, ARRAY_SIZE(lnkfile));
2863 desc.description=NULL;
2864 desc.workdir=NULL;
2865 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2866 desc.path=filename;
2867 desc.pidl=NULL;
2868 desc.arguments="ignored";
2869 desc.showcmd=0;
2870 desc.icon=NULL;
2871 desc.icon_id=0;
2872 desc.hotkey=0;
2873 create_lnk(lnkfile, &desc);
2875 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2876 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, ARRAY_SIZE(lnkfile));
2877 desc.description=NULL;
2878 desc.workdir=NULL;
2879 desc.path=argv0;
2880 desc.pidl=NULL;
2881 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2882 desc.arguments=params;
2883 desc.showcmd=0;
2884 desc.icon=NULL;
2885 desc.icon_id=0;
2886 desc.hotkey=0;
2887 create_lnk(lnkfile, &desc);
2889 /* Create a basic association suitable for most tests */
2890 if (!create_test_association(".shlexec"))
2892 skip_shlexec_tests = TRUE;
2893 skip("Unable to create association for '.shlexec'\n");
2894 return;
2896 create_test_verb("shlexec.shlexec", "Open", 0, "Open \"%1\"");
2897 create_test_verb("shlexec.shlexec", "NoQuotes", 0, "NoQuotes %1");
2898 create_test_verb("shlexec.shlexec", "LowerL", 0, "LowerL %l");
2899 create_test_verb("shlexec.shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2900 create_test_verb("shlexec.shlexec", "UpperL", 0, "UpperL %L");
2901 create_test_verb("shlexec.shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2903 create_test_association(".sha");
2904 create_test_verb("shlexec.sha", "averb", 0, "AVerb \"%1\"");
2906 create_test_class("shlproto", TRUE);
2907 create_test_verb("shlproto", "open", 0, "URL \"%1\"");
2908 create_test_verb("shlproto", "averb", 0, "AVerb \"%1\"");
2910 /* Set an environment variable to see if it is inherited */
2911 SetEnvironmentVariableA("ShlexecVar", "Present");
2913 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token);
2914 ok(ret, "OpenProcessToken failed.\n");
2915 ret = GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &rc);
2916 ok(ret, "GetTokenInformation failed.\n");
2917 is_elevated = elevation.TokenIsElevated;
2918 CloseHandle(token);
2921 static void cleanup_test(void)
2923 char filename[MAX_PATH];
2924 const char* const * testfile;
2926 /* Delete the test files */
2927 testfile=testfiles;
2928 while (*testfile)
2930 sprintf(filename, *testfile, tmpdir);
2931 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2932 SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
2933 DeleteFileA(filename);
2934 testfile++;
2936 DeleteFileA(child_file);
2937 RemoveDirectoryA(tmpdir);
2939 /* Delete the test association */
2940 delete_test_association(".shlexec");
2941 delete_test_association(".sha");
2942 delete_test_class("shlproto");
2944 CloseHandle(hEvent);
2946 CoUninitialize();
2949 static void test_directory(void)
2951 char path[MAX_PATH + 10], curdir[MAX_PATH];
2952 char params[1024], dirpath[1024];
2953 INT_PTR rc;
2954 BOOL ret;
2956 sprintf(path, "%s\\test2.exe", tmpdir);
2957 CopyFileA(argv0, path, FALSE);
2959 sprintf(params, "shlexec \"%s\" Exec", child_file);
2961 /* Test with the current directory */
2962 GetCurrentDirectoryA(sizeof(curdir), curdir);
2963 SetCurrentDirectoryA(tmpdir);
2964 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2965 NULL, "test2.exe", params, NULL, NULL);
2966 okShell(rc > 32, "returned %Iu\n", rc);
2967 okChildInt("argcA", 4);
2968 okChildString("argvA0", path);
2969 okChildString("argvA3", "Exec");
2970 okChildPath("longPath", path);
2971 SetCurrentDirectoryA(curdir);
2973 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2974 NULL, "test2.exe", params, NULL, NULL);
2975 okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
2977 /* Explicitly specify the directory to use */
2978 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2979 NULL, "test2.exe", params, tmpdir, NULL);
2980 okShell(rc > 32, "returned %Iu\n", rc);
2981 okChildInt("argcA", 4);
2982 okChildString("argvA0", path);
2983 okChildString("argvA3", "Exec");
2984 okChildPath("longPath", path);
2986 /* Specify it through an environment variable */
2987 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2988 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2989 todo_wine okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
2991 rc=shell_execute_ex(SEE_MASK_DOENVSUBST|SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2992 NULL, "test2.exe", params, "%TMPDIR%", NULL);
2993 okShell(rc > 32, "returned %Iu\n", rc);
2994 okChildInt("argcA", 4);
2995 okChildString("argvA0", path);
2996 okChildString("argvA3", "Exec");
2997 okChildPath("longPath", path);
2999 /* Not a colon-separated directory list */
3000 sprintf(dirpath, "%s:%s", curdir, tmpdir);
3001 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
3002 NULL, "test2.exe", params, dirpath, NULL);
3003 okShell(rc == SE_ERR_FNF, "returned %Iu\n", rc);
3005 /* Same-named executable in different directory */
3006 snprintf(path, ARRAY_SIZE(path), "%s%s", tmpdir, strrchr(argv0, '\\'));
3007 CopyFileA(argv0, path, FALSE);
3008 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
3009 NULL, strrchr(argv0, '\\') + 1, params, tmpdir, NULL);
3010 okShell(rc > 32, "returned %Iu\n", rc);
3011 okChildInt("argcA", 4);
3012 okChildString("argvA0", path);
3013 okChildString("argvA3", "Exec");
3014 okChildPath("longPath", path);
3016 SetCurrentDirectoryA(tmpdir);
3017 ret = CreateDirectoryA( "tmp", NULL );
3018 ok(ret || GetLastError() == ERROR_ALREADY_EXISTS, "Failed to create 'tmp' err %lu\n", GetLastError());
3019 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
3020 NULL, path, params, "tmp", NULL);
3021 okShell(rc > 32, "returned %Iu\n", rc);
3023 DeleteFileA(path);
3025 RemoveDirectoryA("tmp");
3026 SetCurrentDirectoryA(curdir);
3029 START_TEST(shlexec)
3032 myARGC = winetest_get_mainargs(&myARGV);
3033 if (myARGC >= 3)
3035 doChild(myARGC, myARGV);
3036 /* Skip the tests/failures trace for child processes */
3037 ExitProcess(winetest_get_failures());
3040 init_test();
3042 test_commandline2argv();
3043 test_argify();
3044 test_lpFile_parsed();
3045 test_filename();
3046 test_fileurls();
3047 test_urls();
3048 test_find_executable();
3049 test_lnks();
3050 test_exes();
3051 test_dde();
3052 test_dde_default_app();
3053 test_directory();
3055 cleanup_test();