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 19 #include <stdio.h> 20 21 #define COBJMACROS 22 #define CONST_VTABLE 23 24 #define PSAPI_VERSION 1 25 #include <initguid.h> 26 #include <windows.h> 27 #include <psapi.h> 28 #include <oaidl.h> 29 30 #include "wine/test.h" 31 32 #define DEFINE_EXPECT(func) \ 33 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE 34 35 #define SET_EXPECT(func) \ 36 expect_ ## func = TRUE 37 38 #define CLEAR_CALLED(func) \ 39 expect_ ## func = called_ ## func = FALSE 40 41 #define CHECK_EXPECT2(func) \ 42 do { \ 43 ok(expect_ ##func, "unexpected call " #func "\n"); \ 44 called_ ## func = TRUE; \ 45 }while(0) 46 47 #define CHECK_EXPECT(func) \ 48 do { \ 49 CHECK_EXPECT2(func); \ 50 expect_ ## func = FALSE; \ 51 }while(0) 52 53 #define CHECK_CALLED(func) \ 54 do { \ 55 ok(called_ ## func, "expected " #func "\n"); \ 56 expect_ ## func = called_ ## func = FALSE; \ 57 }while(0) 58 59 DEFINE_EXPECT(reportSuccess); 60 61 #define DISPID_TESTOBJ_OK 10000 62 #define DISPID_TESTOBJ_TRACE 10001 63 #define DISPID_TESTOBJ_REPORTSUCCESS 10002 64 #define DISPID_TESTOBJ_WSCRIPTFULLNAME 10003 65 #define DISPID_TESTOBJ_WSCRIPTPATH 10004 66 #define DISPID_TESTOBJ_WSCRIPTSCRIPTNAME 10005 67 #define DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME 10006 68 69 #define TESTOBJ_CLSID "{178fc166-f585-4e24-9c13-4bb7faf80646}" 70 71 static const GUID CLSID_TestObj = 72 {0x178fc166,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}}; 73 74 static const char *script_name; 75 static HANDLE wscript_process; 76 77 static int strcmp_wa(LPCWSTR strw, const char *stra) 78 { 79 WCHAR buf[512]; 80 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, ARRAY_SIZE(buf)); 81 return lstrcmpW(strw, buf); 82 } 83 84 static const WCHAR* mystrrchr(const WCHAR *str, WCHAR ch) 85 { 86 const WCHAR *pos = NULL, *current = str; 87 while(*current != 0) { 88 if(*current == ch) 89 pos = current; 90 ++current; 91 } 92 return pos; 93 } 94 95 static BSTR a2bstr(const char *str) 96 { 97 BSTR ret; 98 int len; 99 100 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); 101 ret = SysAllocStringLen(NULL, len-1); 102 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); 103 104 return ret; 105 } 106 107 static HRESULT WINAPI Dispatch_QueryInterface(IDispatch *iface, REFIID riid, void **ppv) 108 { 109 if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IDispatch)) { 110 *ppv = iface; 111 return S_OK; 112 } 113 114 *ppv = NULL; 115 return E_NOINTERFACE; 116 } 117 118 static ULONG WINAPI Dispatch_AddRef(IDispatch *iface) 119 { 120 return 2; 121 } 122 123 static ULONG WINAPI Dispatch_Release(IDispatch *iface) 124 { 125 return 1; 126 } 127 128 static HRESULT WINAPI Dispatch_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo) 129 { 130 ok(0, "unexpected call\n"); 131 return E_NOTIMPL; 132 } 133 134 static HRESULT WINAPI Dispatch_GetTypeInfo(IDispatch *iface, UINT iTInfo, 135 LCID lcid, ITypeInfo **ppTInfo) 136 { 137 ok(0, "unexpected call\n"); 138 return E_NOTIMPL; 139 } 140 141 static HRESULT WINAPI Dispatch_GetIDsOfNames(IDispatch *iface, REFIID riid, 142 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) 143 { 144 unsigned i; 145 146 for(i=0; i<cNames; i++) { 147 if(!strcmp_wa(rgszNames[i], "ok")) { 148 rgDispId[i] = DISPID_TESTOBJ_OK; 149 }else if(!strcmp_wa(rgszNames[i], "trace")) { 150 rgDispId[i] = DISPID_TESTOBJ_TRACE; 151 }else if(!strcmp_wa(rgszNames[i], "reportSuccess")) { 152 rgDispId[i] = DISPID_TESTOBJ_REPORTSUCCESS; 153 }else if(!strcmp_wa(rgszNames[i], "wscriptFullName")) { 154 rgDispId[i] = DISPID_TESTOBJ_WSCRIPTFULLNAME; 155 }else if(!strcmp_wa(rgszNames[i], "wscriptPath")) { 156 rgDispId[i] = DISPID_TESTOBJ_WSCRIPTPATH; 157 }else if(!strcmp_wa(rgszNames[i], "wscriptScriptName")) { 158 rgDispId[i] = DISPID_TESTOBJ_WSCRIPTSCRIPTNAME; 159 }else if(!strcmp_wa(rgszNames[i], "wscriptScriptFullName")) { 160 rgDispId[i] = DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME; 161 }else { 162 ok(0, "unexpected name %s\n", wine_dbgstr_w(rgszNames[i])); 163 return DISP_E_UNKNOWNNAME; 164 } 165 } 166 167 return S_OK; 168 } 169 170 static HRESULT WINAPI Dispatch_Invoke(IDispatch *iface, DISPID dispIdMember, REFIID riid, LCID lcid, 171 WORD wFlags, DISPPARAMS *pdp, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) 172 { 173 switch(dispIdMember) { 174 case DISPID_TESTOBJ_OK: { 175 VARIANT *expr, *msg; 176 177 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags); 178 ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs); 179 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs); 180 181 expr = pdp->rgvarg+1; 182 if(V_VT(expr) == (VT_VARIANT|VT_BYREF)) 183 expr = V_VARIANTREF(expr); 184 185 msg = pdp->rgvarg; 186 if(V_VT(msg) == (VT_VARIANT|VT_BYREF)) 187 msg = V_VARIANTREF(msg); 188 189 ok(V_VT(msg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(msg)); 190 ok(V_VT(expr) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(expr)); 191 ok(V_BOOL(expr), "%s: %s\n", script_name, wine_dbgstr_w(V_BSTR(msg))); 192 if(pVarResult) 193 V_VT(pVarResult) = VT_EMPTY; 194 break; 195 } 196 case DISPID_TESTOBJ_TRACE: 197 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags); 198 ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs); 199 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs); 200 ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg)); 201 trace("%s: %s\n", script_name, wine_dbgstr_w(V_BSTR(pdp->rgvarg))); 202 if(pVarResult) 203 V_VT(pVarResult) = VT_EMPTY; 204 break; 205 case DISPID_TESTOBJ_REPORTSUCCESS: 206 CHECK_EXPECT(reportSuccess); 207 208 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags); 209 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs); 210 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs); 211 if(pVarResult) 212 V_VT(pVarResult) = VT_EMPTY; 213 break; 214 case DISPID_TESTOBJ_WSCRIPTFULLNAME: 215 { 216 WCHAR fullName[MAX_PATH]; 217 DWORD res; 218 219 ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags); 220 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs); 221 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs); 222 V_VT(pVarResult) = VT_BSTR; 223 res = GetModuleFileNameExW(wscript_process, NULL, fullName, ARRAY_SIZE(fullName)); 224 if(res == 0) 225 return E_FAIL; 226 if(!(V_BSTR(pVarResult) = SysAllocString(fullName))) 227 return E_OUTOFMEMORY; 228 break; 229 } 230 case DISPID_TESTOBJ_WSCRIPTPATH: 231 { 232 WCHAR fullPath[MAX_PATH]; 233 DWORD res; 234 const WCHAR *pos; 235 236 ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags); 237 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs); 238 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs); 239 V_VT(pVarResult) = VT_BSTR; 240 res = GetModuleFileNameExW(wscript_process, NULL, fullPath, ARRAY_SIZE(fullPath)); 241 if(res == 0) 242 return E_FAIL; 243 pos = mystrrchr(fullPath, '\\'); 244 if(!(V_BSTR(pVarResult) = SysAllocStringLen(fullPath, pos-fullPath))) 245 return E_OUTOFMEMORY; 246 break; 247 } 248 case DISPID_TESTOBJ_WSCRIPTSCRIPTNAME: 249 { 250 char fullPath[MAX_PATH]; 251 char *pos; 252 long res; 253 254 ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags); 255 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs); 256 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs); 257 V_VT(pVarResult) = VT_BSTR; 258 res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, &pos); 259 if(!res || res > sizeof(fullPath)) 260 return E_FAIL; 261 if(!(V_BSTR(pVarResult) = a2bstr(pos))) 262 return E_OUTOFMEMORY; 263 break; 264 } 265 case DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME: 266 { 267 char fullPath[MAX_PATH]; 268 long res; 269 270 ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags); 271 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs); 272 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs); 273 V_VT(pVarResult) = VT_BSTR; 274 res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, NULL); 275 if(!res || res > sizeof(fullPath)) 276 return E_FAIL; 277 if(!(V_BSTR(pVarResult) = a2bstr(fullPath))) 278 return E_OUTOFMEMORY; 279 break; 280 } 281 default: 282 ok(0, "unexpected dispIdMember %d\n", dispIdMember); 283 return E_NOTIMPL; 284 } 285 286 return S_OK; 287 } 288 289 static IDispatchVtbl testobj_vtbl = { 290 Dispatch_QueryInterface, 291 Dispatch_AddRef, 292 Dispatch_Release, 293 Dispatch_GetTypeInfoCount, 294 Dispatch_GetTypeInfo, 295 Dispatch_GetIDsOfNames, 296 Dispatch_Invoke 297 }; 298 299 static IDispatch testobj = { &testobj_vtbl }; 300 301 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv) 302 { 303 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) { 304 *ppv = iface; 305 return S_OK; 306 } 307 308 *ppv = NULL; 309 return E_NOINTERFACE; 310 } 311 312 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface) 313 { 314 return 2; 315 } 316 317 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface) 318 { 319 return 1; 320 } 321 322 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv) 323 { 324 ok(!outer, "outer = %p\n", outer); 325 return IDispatch_QueryInterface(&testobj, riid, ppv); 326 } 327 328 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock) 329 { 330 return S_OK; 331 } 332 333 static const IClassFactoryVtbl ClassFactoryVtbl = { 334 ClassFactory_QueryInterface, 335 ClassFactory_AddRef, 336 ClassFactory_Release, 337 ClassFactory_CreateInstance, 338 ClassFactory_LockServer 339 }; 340 341 static IClassFactory testobj_cf = { &ClassFactoryVtbl }; 342 343 static void run_script_file(const char *file_name, DWORD expected_exit_code) 344 { 345 char command[MAX_PATH]; 346 STARTUPINFOA si = {sizeof(si)}; 347 PROCESS_INFORMATION pi; 348 DWORD exit_code; 349 BOOL bres; 350 351 script_name = file_name; 352 sprintf(command, "wscript.exe %s arg1 2 ar3", file_name); 353 354 SET_EXPECT(reportSuccess); 355 356 bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); 357 if(!bres) { 358 win_skip("script.exe is not available\n"); 359 CLEAR_CALLED(reportSuccess); 360 return; 361 } 362 363 wscript_process = pi.hProcess; 364 WaitForSingleObject(pi.hProcess, INFINITE); 365 366 bres = GetExitCodeProcess(pi.hProcess, &exit_code); 367 ok(bres, "GetExitCodeProcess failed: %u\n", GetLastError()); 368 ok(exit_code == expected_exit_code, "exit_code = %u, expected %u\n", exit_code, expected_exit_code); 369 370 CloseHandle(pi.hThread); 371 CloseHandle(pi.hProcess); 372 373 CHECK_CALLED(reportSuccess); 374 } 375 376 static void run_script(const char *name, const char *script_data, size_t script_size, DWORD expected_exit_code) 377 { 378 char file_name[MAX_PATH]; 379 const char *ext; 380 HANDLE file; 381 DWORD size; 382 BOOL res; 383 384 ext = strrchr(name, '.'); 385 ok(ext != NULL, "no script extension\n"); 386 if(!ext) 387 return; 388 389 sprintf(file_name, "test%s", ext); 390 391 file = CreateFileA(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 392 FILE_ATTRIBUTE_NORMAL, NULL); 393 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %u\n", GetLastError()); 394 if(file == INVALID_HANDLE_VALUE) 395 return; 396 397 res = WriteFile(file, script_data, script_size, &size, NULL); 398 CloseHandle(file); 399 ok(res, "Could not write to file: %u\n", GetLastError()); 400 if(!res) 401 return; 402 403 run_script_file(file_name, expected_exit_code); 404 405 DeleteFileA(file_name); 406 } 407 408 static void run_simple_script(const char *script, DWORD expected_exit_code) 409 { 410 run_script("simple.js", script, strlen(script), expected_exit_code); 411 } 412 413 static BOOL WINAPI test_enum_proc(HMODULE module, LPCSTR type, LPSTR name, LONG_PTR param) 414 { 415 const char *script_data; 416 DWORD script_size; 417 HRSRC src; 418 419 trace("running %s test...\n", name); 420 421 src = FindResourceA(NULL, name, type); 422 ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError()); 423 if(!src) 424 return TRUE; 425 426 script_data = LoadResource(NULL, src); 427 script_size = SizeofResource(NULL, src); 428 while(script_size && !script_data[script_size-1]) 429 script_size--; 430 431 run_script(name, script_data, script_size, 0); 432 return TRUE; 433 } 434 435 static BOOL init_key(const char *key_name, const char *def_value, BOOL init) 436 { 437 HKEY hkey; 438 DWORD res; 439 440 if(!init) { 441 RegDeleteKeyA(HKEY_CLASSES_ROOT, key_name); 442 return TRUE; 443 } 444 445 res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey); 446 if(res != ERROR_SUCCESS) 447 return FALSE; 448 449 if(def_value) 450 res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value)); 451 452 RegCloseKey(hkey); 453 return res == ERROR_SUCCESS; 454 } 455 456 static BOOL init_registry(BOOL init) 457 { 458 return init_key("Wine.Test\\CLSID", TESTOBJ_CLSID, init); 459 } 460 461 static BOOL register_activex(void) 462 { 463 DWORD regid; 464 HRESULT hres; 465 466 if(!init_registry(TRUE)) { 467 init_registry(FALSE); 468 return FALSE; 469 } 470 471 hres = CoRegisterClassObject(&CLSID_TestObj, (IUnknown *)&testobj_cf, 472 CLSCTX_SERVER, REGCLS_MULTIPLEUSE, ®id); 473 ok(hres == S_OK, "Could not register script engine: %08x\n", hres); 474 return TRUE; 475 } 476 477 START_TEST(run) 478 { 479 char **argv; 480 int argc; 481 482 CoInitializeEx(NULL, COINIT_MULTITHREADED); 483 484 if(!register_activex()) { 485 skip("Could not register ActiveX object.\n"); 486 CoUninitialize(); 487 return; 488 } 489 490 argc = winetest_get_mainargs(&argv); 491 if(argc > 2) { 492 run_script_file(argv[2], 0); 493 }else { 494 EnumResourceNamesA(NULL, "TESTSCRIPT", test_enum_proc, 0); 495 496 run_simple_script("var winetest = new ActiveXObject('Wine.Test');\n" 497 "winetest.reportSuccess();\n" 498 "WScript.Quit(3);\n" 499 "winetest.ok(false, 'not quit?');\n", 3); 500 } 501 502 init_registry(FALSE); 503 CoUninitialize(); 504 } 505