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