run.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Copyright 2011 Jacek Caban for CodeWeavers
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #include <stdio.h>
  19. #define COBJMACROS
  20. #define CONST_VTABLE
  21. #define PSAPI_VERSION 1
  22. #include <initguid.h>
  23. #include <windows.h>
  24. #include <psapi.h>
  25. #include <oaidl.h>
  26. #include "wine/test.h"
  27. #define DEFINE_EXPECT(func) \
  28. static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
  29. #define SET_EXPECT(func) \
  30. expect_ ## func = TRUE
  31. #define CLEAR_CALLED(func) \
  32. expect_ ## func = called_ ## func = FALSE
  33. #define CHECK_EXPECT2(func) \
  34. do { \
  35. ok(expect_ ##func, "unexpected call " #func "\n"); \
  36. called_ ## func = TRUE; \
  37. }while(0)
  38. #define CHECK_EXPECT(func) \
  39. do { \
  40. CHECK_EXPECT2(func); \
  41. expect_ ## func = FALSE; \
  42. }while(0)
  43. #define CHECK_CALLED(func) \
  44. do { \
  45. ok(called_ ## func, "expected " #func "\n"); \
  46. expect_ ## func = called_ ## func = FALSE; \
  47. }while(0)
  48. DEFINE_EXPECT(reportSuccess);
  49. #define DISPID_TESTOBJ_OK 10000
  50. #define DISPID_TESTOBJ_TRACE 10001
  51. #define DISPID_TESTOBJ_REPORTSUCCESS 10002
  52. #define DISPID_TESTOBJ_WSCRIPTFULLNAME 10003
  53. #define DISPID_TESTOBJ_WSCRIPTPATH 10004
  54. #define DISPID_TESTOBJ_WSCRIPTSCRIPTNAME 10005
  55. #define DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME 10006
  56. #define TESTOBJ_CLSID "{178fc166-f585-4e24-9c13-4bb7faf80646}"
  57. static const GUID CLSID_TestObj =
  58. {0x178fc166,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}};
  59. static const char *script_name;
  60. static HANDLE wscript_process;
  61. static BSTR a2bstr(const char *str)
  62. {
  63. BSTR ret;
  64. int len;
  65. len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
  66. ret = SysAllocStringLen(NULL, len-1);
  67. MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
  68. return ret;
  69. }
  70. static HRESULT WINAPI Dispatch_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
  71. {
  72. if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IDispatch)) {
  73. *ppv = iface;
  74. return S_OK;
  75. }
  76. *ppv = NULL;
  77. return E_NOINTERFACE;
  78. }
  79. static ULONG WINAPI Dispatch_AddRef(IDispatch *iface)
  80. {
  81. return 2;
  82. }
  83. static ULONG WINAPI Dispatch_Release(IDispatch *iface)
  84. {
  85. return 1;
  86. }
  87. static HRESULT WINAPI Dispatch_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
  88. {
  89. ok(0, "unexpected call\n");
  90. return E_NOTIMPL;
  91. }
  92. static HRESULT WINAPI Dispatch_GetTypeInfo(IDispatch *iface, UINT iTInfo,
  93. LCID lcid, ITypeInfo **ppTInfo)
  94. {
  95. return E_NOTIMPL;
  96. }
  97. static HRESULT WINAPI Dispatch_GetIDsOfNames(IDispatch *iface, REFIID riid,
  98. LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
  99. {
  100. unsigned i;
  101. for(i=0; i<cNames; i++) {
  102. if(!lstrcmpW(rgszNames[i], L"ok")) {
  103. rgDispId[i] = DISPID_TESTOBJ_OK;
  104. }else if(!lstrcmpW(rgszNames[i], L"trace")) {
  105. rgDispId[i] = DISPID_TESTOBJ_TRACE;
  106. }else if(!lstrcmpW(rgszNames[i], L"reportSuccess")) {
  107. rgDispId[i] = DISPID_TESTOBJ_REPORTSUCCESS;
  108. }else if(!lstrcmpW(rgszNames[i], L"wscriptFullName")) {
  109. rgDispId[i] = DISPID_TESTOBJ_WSCRIPTFULLNAME;
  110. }else if(!lstrcmpW(rgszNames[i], L"wscriptPath")) {
  111. rgDispId[i] = DISPID_TESTOBJ_WSCRIPTPATH;
  112. }else if(!lstrcmpW(rgszNames[i], L"wscriptScriptName")) {
  113. rgDispId[i] = DISPID_TESTOBJ_WSCRIPTSCRIPTNAME;
  114. }else if(!lstrcmpW(rgszNames[i], L"wscriptScriptFullName")) {
  115. rgDispId[i] = DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME;
  116. }else {
  117. ok(0, "unexpected name %s\n", wine_dbgstr_w(rgszNames[i]));
  118. return DISP_E_UNKNOWNNAME;
  119. }
  120. }
  121. return S_OK;
  122. }
  123. static HRESULT WINAPI Dispatch_Invoke(IDispatch *iface, DISPID dispIdMember, REFIID riid, LCID lcid,
  124. WORD wFlags, DISPPARAMS *pdp, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
  125. {
  126. switch(dispIdMember) {
  127. case DISPID_TESTOBJ_OK: {
  128. VARIANT *expr, *msg;
  129. ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
  130. ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs);
  131. ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
  132. expr = pdp->rgvarg+1;
  133. if(V_VT(expr) == (VT_VARIANT|VT_BYREF))
  134. expr = V_VARIANTREF(expr);
  135. msg = pdp->rgvarg;
  136. if(V_VT(msg) == (VT_VARIANT|VT_BYREF))
  137. msg = V_VARIANTREF(msg);
  138. ok(V_VT(msg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(msg));
  139. ok(V_VT(expr) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(expr));
  140. ok(V_BOOL(expr), "%s: %s\n", script_name, wine_dbgstr_w(V_BSTR(msg)));
  141. if(pVarResult)
  142. V_VT(pVarResult) = VT_EMPTY;
  143. break;
  144. }
  145. case DISPID_TESTOBJ_TRACE:
  146. ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
  147. ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
  148. ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
  149. ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
  150. trace("%s: %s\n", script_name, wine_dbgstr_w(V_BSTR(pdp->rgvarg)));
  151. if(pVarResult)
  152. V_VT(pVarResult) = VT_EMPTY;
  153. break;
  154. case DISPID_TESTOBJ_REPORTSUCCESS:
  155. CHECK_EXPECT(reportSuccess);
  156. ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
  157. ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
  158. ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
  159. if(pVarResult)
  160. V_VT(pVarResult) = VT_EMPTY;
  161. break;
  162. case DISPID_TESTOBJ_WSCRIPTFULLNAME:
  163. {
  164. WCHAR fullName[MAX_PATH];
  165. DWORD res;
  166. ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
  167. ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
  168. ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
  169. V_VT(pVarResult) = VT_BSTR;
  170. res = GetModuleFileNameExW(wscript_process, NULL, fullName, ARRAY_SIZE(fullName));
  171. if(res == 0)
  172. return E_FAIL;
  173. if(!(V_BSTR(pVarResult) = SysAllocString(fullName)))
  174. return E_OUTOFMEMORY;
  175. break;
  176. }
  177. case DISPID_TESTOBJ_WSCRIPTPATH:
  178. {
  179. WCHAR fullPath[MAX_PATH];
  180. DWORD res;
  181. const WCHAR *pos;
  182. ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
  183. ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
  184. ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
  185. V_VT(pVarResult) = VT_BSTR;
  186. res = GetModuleFileNameExW(wscript_process, NULL, fullPath, ARRAY_SIZE(fullPath));
  187. if(res == 0)
  188. return E_FAIL;
  189. pos = wcsrchr(fullPath, '\\');
  190. if(!(V_BSTR(pVarResult) = SysAllocStringLen(fullPath, pos-fullPath)))
  191. return E_OUTOFMEMORY;
  192. break;
  193. }
  194. case DISPID_TESTOBJ_WSCRIPTSCRIPTNAME:
  195. {
  196. char fullPath[MAX_PATH];
  197. char *pos;
  198. long res;
  199. ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
  200. ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
  201. ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
  202. V_VT(pVarResult) = VT_BSTR;
  203. res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, &pos);
  204. if(!res || res > sizeof(fullPath))
  205. return E_FAIL;
  206. if(!(V_BSTR(pVarResult) = a2bstr(pos)))
  207. return E_OUTOFMEMORY;
  208. break;
  209. }
  210. case DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME:
  211. {
  212. char fullPath[MAX_PATH];
  213. long res;
  214. ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
  215. ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
  216. ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
  217. V_VT(pVarResult) = VT_BSTR;
  218. res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, NULL);
  219. if(!res || res > sizeof(fullPath))
  220. return E_FAIL;
  221. if(!(V_BSTR(pVarResult) = a2bstr(fullPath)))
  222. return E_OUTOFMEMORY;
  223. break;
  224. }
  225. default:
  226. ok(0, "unexpected dispIdMember %d\n", dispIdMember);
  227. return E_NOTIMPL;
  228. }
  229. return S_OK;
  230. }
  231. static IDispatchVtbl testobj_vtbl = {
  232. Dispatch_QueryInterface,
  233. Dispatch_AddRef,
  234. Dispatch_Release,
  235. Dispatch_GetTypeInfoCount,
  236. Dispatch_GetTypeInfo,
  237. Dispatch_GetIDsOfNames,
  238. Dispatch_Invoke
  239. };
  240. static IDispatch testobj = { &testobj_vtbl };
  241. static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
  242. {
  243. if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
  244. *ppv = iface;
  245. return S_OK;
  246. }
  247. *ppv = NULL;
  248. return E_NOINTERFACE;
  249. }
  250. static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
  251. {
  252. return 2;
  253. }
  254. static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
  255. {
  256. return 1;
  257. }
  258. static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
  259. {
  260. ok(!outer, "outer = %p\n", outer);
  261. return IDispatch_QueryInterface(&testobj, riid, ppv);
  262. }
  263. static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
  264. {
  265. return S_OK;
  266. }
  267. static const IClassFactoryVtbl ClassFactoryVtbl = {
  268. ClassFactory_QueryInterface,
  269. ClassFactory_AddRef,
  270. ClassFactory_Release,
  271. ClassFactory_CreateInstance,
  272. ClassFactory_LockServer
  273. };
  274. static IClassFactory testobj_cf = { &ClassFactoryVtbl };
  275. static void run_script_file(const char *file_name, DWORD expected_exit_code)
  276. {
  277. char command[MAX_PATH];
  278. STARTUPINFOA si = {sizeof(si)};
  279. PROCESS_INFORMATION pi;
  280. DWORD exit_code;
  281. BOOL bres;
  282. script_name = file_name;
  283. sprintf(command, "wscript.exe %s arg1 2 ar3", file_name);
  284. SET_EXPECT(reportSuccess);
  285. bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
  286. if(!bres) {
  287. win_skip("script.exe is not available\n");
  288. CLEAR_CALLED(reportSuccess);
  289. return;
  290. }
  291. wscript_process = pi.hProcess;
  292. WaitForSingleObject(pi.hProcess, INFINITE);
  293. bres = GetExitCodeProcess(pi.hProcess, &exit_code);
  294. ok(bres, "GetExitCodeProcess failed: %u\n", GetLastError());
  295. ok(exit_code == expected_exit_code, "exit_code = %u, expected %u\n", exit_code, expected_exit_code);
  296. CloseHandle(pi.hThread);
  297. CloseHandle(pi.hProcess);
  298. CHECK_CALLED(reportSuccess);
  299. }
  300. static void run_script(const char *name, const char *script_data, size_t script_size, DWORD expected_exit_code)
  301. {
  302. char file_name[MAX_PATH];
  303. const char *ext;
  304. HANDLE file;
  305. DWORD size;
  306. BOOL res;
  307. ext = strrchr(name, '.');
  308. ok(ext != NULL, "no script extension\n");
  309. if(!ext)
  310. return;
  311. sprintf(file_name, "test%s", ext);
  312. file = CreateFileA(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  313. FILE_ATTRIBUTE_NORMAL, NULL);
  314. ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %u\n", GetLastError());
  315. if(file == INVALID_HANDLE_VALUE)
  316. return;
  317. res = WriteFile(file, script_data, script_size, &size, NULL);
  318. CloseHandle(file);
  319. ok(res, "Could not write to file: %u\n", GetLastError());
  320. if(!res)
  321. return;
  322. run_script_file(file_name, expected_exit_code);
  323. DeleteFileA(file_name);
  324. }
  325. static void run_simple_script(const char *script, DWORD expected_exit_code)
  326. {
  327. run_script("simple.js", script, strlen(script), expected_exit_code);
  328. }
  329. static BOOL WINAPI test_enum_proc(HMODULE module, LPCSTR type, LPSTR name, LONG_PTR param)
  330. {
  331. const char *script_data;
  332. DWORD script_size;
  333. HRSRC src;
  334. trace("running %s test...\n", name);
  335. src = FindResourceA(NULL, name, type);
  336. ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
  337. if(!src)
  338. return TRUE;
  339. script_data = LoadResource(NULL, src);
  340. script_size = SizeofResource(NULL, src);
  341. while(script_size && !script_data[script_size-1])
  342. script_size--;
  343. run_script(name, script_data, script_size, 0);
  344. return TRUE;
  345. }
  346. static BOOL init_key(const char *key_name, const char *def_value, BOOL init)
  347. {
  348. HKEY hkey;
  349. DWORD res;
  350. if(!init) {
  351. RegDeleteKeyA(HKEY_CLASSES_ROOT, key_name);
  352. return TRUE;
  353. }
  354. res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey);
  355. if(res != ERROR_SUCCESS)
  356. return FALSE;
  357. if(def_value)
  358. res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value));
  359. RegCloseKey(hkey);
  360. return res == ERROR_SUCCESS;
  361. }
  362. static BOOL init_registry(BOOL init)
  363. {
  364. return init_key("Wine.Test\\CLSID", TESTOBJ_CLSID, init);
  365. }
  366. static BOOL register_activex(void)
  367. {
  368. DWORD regid;
  369. HRESULT hres;
  370. if(!init_registry(TRUE)) {
  371. init_registry(FALSE);
  372. return FALSE;
  373. }
  374. hres = CoRegisterClassObject(&CLSID_TestObj, (IUnknown *)&testobj_cf,
  375. CLSCTX_SERVER, REGCLS_MULTIPLEUSE, &regid);
  376. ok(hres == S_OK, "Could not register script engine: %08x\n", hres);
  377. return TRUE;
  378. }
  379. START_TEST(run)
  380. {
  381. char **argv;
  382. int argc;
  383. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  384. if(!register_activex()) {
  385. skip("Could not register ActiveX object.\n");
  386. CoUninitialize();
  387. return;
  388. }
  389. argc = winetest_get_mainargs(&argv);
  390. if(argc > 2) {
  391. run_script_file(argv[2], 0);
  392. }else {
  393. EnumResourceNamesA(NULL, "TESTSCRIPT", test_enum_proc, 0);
  394. run_simple_script("var winetest = new ActiveXObject('Wine.Test');\n"
  395. "winetest.reportSuccess();\n"
  396. "WScript.Quit(3);\n"
  397. "winetest.ok(false, 'not quit?');\n", 3);
  398. }
  399. init_registry(FALSE);
  400. CoUninitialize();
  401. }