winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / jscript / tests / activex.c
blob636dd81e19d00ab63ef6a72588b348d14f1ece08
1 /*
2 * Copyright 2009 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdio.h>
21 #define COBJMACROS
22 #define CONST_VTABLE
24 #include <ole2.h>
25 #include <dispex.h>
26 #include <activscp.h>
27 #include <objsafe.h>
28 #include <urlmon.h>
29 #include <mshtmhst.h>
31 #include "wine/test.h"
33 #ifdef _WIN64
35 #define IActiveScriptParse_QueryInterface IActiveScriptParse64_QueryInterface
36 #define IActiveScriptParse_Release IActiveScriptParse64_Release
37 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
38 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
39 #define IActiveScriptParseProcedure2_Release \
40 IActiveScriptParseProcedure2_64_Release
41 #define IActiveScriptParseProcedure2_ParseProcedureText \
42 IActiveScriptParseProcedure2_64_ParseProcedureText
44 #else
46 #define IActiveScriptParse_QueryInterface IActiveScriptParse32_QueryInterface
47 #define IActiveScriptParse_Release IActiveScriptParse32_Release
48 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
49 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
50 #define IActiveScriptParseProcedure2_Release \
51 IActiveScriptParseProcedure2_32_Release
52 #define IActiveScriptParseProcedure2_ParseProcedureText \
53 IActiveScriptParseProcedure2_32_ParseProcedureText
55 #endif
57 static const CLSID CLSID_JScript =
58 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
60 #define DEFINE_EXPECT(func) \
61 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
63 #define SET_EXPECT(func) \
64 expect_ ## func = TRUE
66 #define CHECK_EXPECT2(func) \
67 do { \
68 ok(expect_ ##func, "unexpected call " #func "\n"); \
69 called_ ## func = TRUE; \
70 }while(0)
72 #define CHECK_EXPECT(func) \
73 do { \
74 CHECK_EXPECT2(func); \
75 expect_ ## func = FALSE; \
76 }while(0)
78 #define CHECK_CALLED(func) \
79 do { \
80 ok(called_ ## func, "expected " #func "\n"); \
81 expect_ ## func = called_ ## func = FALSE; \
82 }while(0)
84 DEFINE_EXPECT(CreateInstance);
85 DEFINE_EXPECT(ProcessUrlAction);
86 DEFINE_EXPECT(QueryCustomPolicy);
87 DEFINE_EXPECT(reportSuccess);
88 DEFINE_EXPECT(Host_QS_SecMgr);
89 DEFINE_EXPECT(Caller_QS_SecMgr);
90 DEFINE_EXPECT(QI_IObjectWithSite);
91 DEFINE_EXPECT(SetSite);
93 static const WCHAR testW[] = {'t','e','s','t',0};
95 static HRESULT QS_SecMgr_hres;
96 static HRESULT ProcessUrlAction_hres;
97 static DWORD ProcessUrlAction_policy;
98 static HRESULT CreateInstance_hres;
99 static HRESULT QueryCustomPolicy_hres;
100 static DWORD QueryCustomPolicy_psize;
101 static DWORD QueryCustomPolicy_policy;
102 static HRESULT QI_IDispatch_hres;
103 static HRESULT SetSite_hres;
104 static BOOL AllowIServiceProvider;
106 #define TESTOBJ_CLSID "{178fc163-f585-4e24-9c13-4bb7faf80646}"
108 static const GUID CLSID_TestObj =
109 {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}};
111 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
112 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
113 {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
115 #define DISPID_TEST_REPORTSUCCESS 0x1000
117 #define DISPID_GLOBAL_OK 0x2000
119 static BSTR a2bstr(const char *str)
121 BSTR ret;
122 int len;
124 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
125 ret = SysAllocStringLen(NULL, len-1);
126 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
128 return ret;
131 static int strcmp_wa(LPCWSTR strw, const char *stra)
133 CHAR buf[512];
134 WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), 0, 0);
135 return lstrcmpA(buf, stra);
138 static HRESULT WINAPI ObjectWithSite_QueryInterface(IObjectWithSite *iface, REFIID riid, void **ppv)
140 ok(0, "unexpected call\n");
141 return E_NOTIMPL;
144 static ULONG WINAPI ObjectWithSite_AddRef(IObjectWithSite *iface)
146 return 2;
149 static ULONG WINAPI ObjectWithSite_Release(IObjectWithSite *iface)
151 return 1;
154 static HRESULT WINAPI ObjectWithSite_SetSite(IObjectWithSite *iface, IUnknown *pUnkSite)
156 IServiceProvider *sp;
157 HRESULT hres;
160 CHECK_EXPECT(SetSite);
161 ok(pUnkSite != NULL, "pUnkSite == NULL\n");
163 hres = IUnknown_QueryInterface(pUnkSite, &IID_IServiceProvider, (void**)&sp);
164 ok(hres == S_OK, "Could not get IServiceProvider iface: %08x\n", hres);
165 IServiceProvider_Release(sp);
167 return SetSite_hres;
170 static HRESULT WINAPI ObjectWithSite_GetSite(IObjectWithSite *iface, REFIID riid, void **ppvSite)
172 ok(0, "unexpected call\n");
173 return E_NOTIMPL;
176 static const IObjectWithSiteVtbl ObjectWithSiteVtbl = {
177 ObjectWithSite_QueryInterface,
178 ObjectWithSite_AddRef,
179 ObjectWithSite_Release,
180 ObjectWithSite_SetSite,
181 ObjectWithSite_GetSite
184 static IObjectWithSite ObjectWithSite = { &ObjectWithSiteVtbl };
186 static IObjectWithSite *object_with_site;
188 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
190 *ppv = NULL;
192 if(IsEqualGUID(riid, &IID_IUnknown)) {
193 *ppv = iface;
194 }else if(IsEqualGUID(riid, &IID_IDispatch) || IsEqualGUID(riid, &IID_IDispatchEx)) {
195 if(FAILED(QI_IDispatch_hres))
196 return QI_IDispatch_hres;
197 *ppv = iface;
198 }else if(IsEqualGUID(&IID_IObjectWithSite, riid)) {
199 CHECK_EXPECT(QI_IObjectWithSite);
200 *ppv = object_with_site;
201 }else if(IsEqualGUID(&IID_IObjectSafety, riid)) {
202 ok(0, "Unexpected IID_IObjectSafety query\n");
205 return *ppv ? S_OK : E_NOINTERFACE;
208 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
210 return 2;
213 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
215 return 1;
218 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
220 ok(0, "unexpected call\n");
221 return E_NOTIMPL;
224 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
225 LCID lcid, ITypeInfo **ppTInfo)
227 ok(0, "unexpected call\n");
228 return E_NOTIMPL;
231 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
232 LPOLESTR *rgszNames, UINT cNames,
233 LCID lcid, DISPID *rgDispId)
235 ok(0, "unexpected call\n");
236 return E_NOTIMPL;
239 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
240 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
241 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
243 ok(0, "unexpected call\n");
244 return E_NOTIMPL;
247 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
249 ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName), grfdex);
250 return E_NOTIMPL;
253 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
255 ok(0, "unexpected call\n");
256 return E_NOTIMPL;
259 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
261 ok(0, "unexpected call\n");
262 return E_NOTIMPL;
265 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
267 ok(0, "unexpected call\n");
268 return E_NOTIMPL;
271 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
273 ok(0, "unexpected call\n");
274 return E_NOTIMPL;
277 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
279 ok(0, "unexpected call\n");
280 return E_NOTIMPL;
283 static HRESULT WINAPI Test_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
285 if(!strcmp_wa(bstrName, "reportSuccess")) {
286 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
287 *pid = DISPID_TEST_REPORTSUCCESS;
288 return S_OK;
291 ok(0, "unexpected name %s\n", wine_dbgstr_w(bstrName));
292 return E_NOTIMPL;
295 static HRESULT WINAPI Test_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
296 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
298 switch(id) {
299 case DISPID_TEST_REPORTSUCCESS:
300 CHECK_EXPECT(reportSuccess);
302 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
303 ok(pdp != NULL, "pdp == NULL\n");
304 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
305 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
306 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
307 ok(!pvarRes, "pvarRes != NULL\n");
308 ok(pei != NULL, "pei == NULL\n");
309 break;
311 default:
312 ok(0, "unexpected call\n");
313 return E_NOTIMPL;
316 return S_OK;
319 static IDispatchExVtbl testObjVtbl = {
320 DispatchEx_QueryInterface,
321 DispatchEx_AddRef,
322 DispatchEx_Release,
323 DispatchEx_GetTypeInfoCount,
324 DispatchEx_GetTypeInfo,
325 DispatchEx_GetIDsOfNames,
326 DispatchEx_Invoke,
327 Test_GetDispID,
328 Test_InvokeEx,
329 DispatchEx_DeleteMemberByName,
330 DispatchEx_DeleteMemberByDispID,
331 DispatchEx_GetMemberProperties,
332 DispatchEx_GetMemberName,
333 DispatchEx_GetNextDispID,
334 DispatchEx_GetNameSpaceParent
337 static IDispatchEx testObj = { &testObjVtbl };
339 static HRESULT WINAPI Global_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
341 if(!strcmp_wa(bstrName, "ok")) {
342 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
343 *pid = DISPID_GLOBAL_OK;
344 return S_OK;
347 ok(0, "unexpected name %s\n", wine_dbgstr_w(bstrName));
348 return E_NOTIMPL;
351 static HRESULT WINAPI Global_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
352 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
354 switch(id) {
355 case DISPID_GLOBAL_OK:
356 ok(wFlags == INVOKE_FUNC || wFlags == (INVOKE_FUNC|INVOKE_PROPERTYGET), "wFlags = %x\n", wFlags);
357 ok(pdp != NULL, "pdp == NULL\n");
358 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
359 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
360 ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs);
361 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
362 ok(pei != NULL, "pei == NULL\n");
364 ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
365 ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(pdp->rgvarg));
366 ok(V_BOOL(pdp->rgvarg+1), "%s\n", wine_dbgstr_w(V_BSTR(pdp->rgvarg)));
367 break;
369 default:
370 ok(0, "unexpected call\n");
371 return E_NOTIMPL;
374 return S_OK;
377 static IDispatchExVtbl globalObjVtbl = {
378 DispatchEx_QueryInterface,
379 DispatchEx_AddRef,
380 DispatchEx_Release,
381 DispatchEx_GetTypeInfoCount,
382 DispatchEx_GetTypeInfo,
383 DispatchEx_GetIDsOfNames,
384 DispatchEx_Invoke,
385 Global_GetDispID,
386 Global_InvokeEx,
387 DispatchEx_DeleteMemberByName,
388 DispatchEx_DeleteMemberByDispID,
389 DispatchEx_GetMemberProperties,
390 DispatchEx_GetMemberName,
391 DispatchEx_GetNextDispID,
392 DispatchEx_GetNameSpaceParent
395 static IDispatchEx globalObj = { &globalObjVtbl };
397 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
399 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
400 *ppv = iface;
401 return S_OK;
404 /* TODO: IClassFactoryEx */
405 *ppv = NULL;
406 return E_NOINTERFACE;
409 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
411 return 2;
414 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
416 return 1;
419 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
421 CHECK_EXPECT(CreateInstance);
423 ok(!outer, "outer = %p\n", outer);
424 ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", wine_dbgstr_guid(riid));
426 if(SUCCEEDED(CreateInstance_hres))
427 *ppv = &testObj;
428 return CreateInstance_hres;
431 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
433 ok(0, "unexpected call\n");
434 return S_OK;
437 static const IClassFactoryVtbl ClassFactoryVtbl = {
438 ClassFactory_QueryInterface,
439 ClassFactory_AddRef,
440 ClassFactory_Release,
441 ClassFactory_CreateInstance,
442 ClassFactory_LockServer
445 static IClassFactory activex_cf = { &ClassFactoryVtbl };
447 static HRESULT WINAPI InternetHostSecurityManager_QueryInterface(IInternetHostSecurityManager *iface, REFIID riid, void **ppv)
449 ok(0, "unexpected call\n");
450 return E_NOINTERFACE;
453 static ULONG WINAPI InternetHostSecurityManager_AddRef(IInternetHostSecurityManager *iface)
455 return 2;
458 static ULONG WINAPI InternetHostSecurityManager_Release(IInternetHostSecurityManager *iface)
460 return 1;
463 static HRESULT WINAPI InternetHostSecurityManager_GetSecurityId(IInternetHostSecurityManager *iface, BYTE *pbSecurityId,
464 DWORD *pcbSecurityId, DWORD_PTR dwReserved)
466 ok(0, "unexpected call\n");
467 return E_NOTIMPL;
470 static HRESULT WINAPI InternetHostSecurityManager_ProcessUrlAction(IInternetHostSecurityManager *iface, DWORD dwAction,
471 BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwFlags, DWORD dwReserved)
473 CHECK_EXPECT(ProcessUrlAction);
475 ok(dwAction == URLACTION_ACTIVEX_RUN, "dwAction = %x\n", dwAction);
476 ok(pPolicy != NULL, "pPolicy == NULL\n");
477 ok(cbPolicy == sizeof(DWORD), "cbPolicy = %d\n", cbPolicy);
478 ok(pContext != NULL, "pContext == NULL\n");
479 ok(cbContext == sizeof(GUID), "cbContext = %d\n", cbContext);
480 ok(IsEqualGUID(pContext, &CLSID_TestObj), "pContext = %s\n", wine_dbgstr_guid((const IID*)pContext));
481 ok(!dwFlags, "dwFlags = %x\n", dwFlags);
482 ok(!dwReserved, "dwReserved = %x\n", dwReserved);
484 if(SUCCEEDED(ProcessUrlAction_hres))
485 *(DWORD*)pPolicy = ProcessUrlAction_policy;
486 return ProcessUrlAction_hres;
489 static HRESULT WINAPI InternetHostSecurityManager_QueryCustomPolicy(IInternetHostSecurityManager *iface, REFGUID guidKey,
490 BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwReserved)
492 const struct CONFIRMSAFETY *cs = (const struct CONFIRMSAFETY*)pContext;
493 DWORD *ret;
495 CHECK_EXPECT(QueryCustomPolicy);
497 ok(IsEqualGUID(&GUID_CUSTOM_CONFIRMOBJECTSAFETY, guidKey), "guidKey = %s\n", wine_dbgstr_guid(guidKey));
499 ok(ppPolicy != NULL, "ppPolicy == NULL\n");
500 ok(pcbPolicy != NULL, "pcbPolicy == NULL\n");
501 ok(pContext != NULL, "pContext == NULL\n");
502 ok(cbContext == sizeof(struct CONFIRMSAFETY), "cbContext = %d\n", cbContext);
503 ok(!dwReserved, "dwReserved = %x\n", dwReserved);
505 /* TODO: CLSID */
506 ok(cs->pUnk != NULL, "cs->pUnk == NULL\n");
507 ok(!cs->dwFlags, "dwFlags = %x\n", cs->dwFlags);
509 if(FAILED(QueryCustomPolicy_hres))
510 return QueryCustomPolicy_hres;
512 ret = CoTaskMemAlloc(QueryCustomPolicy_psize);
513 *ppPolicy = (BYTE*)ret;
514 *pcbPolicy = QueryCustomPolicy_psize;
515 memset(ret, 0, QueryCustomPolicy_psize);
516 if(QueryCustomPolicy_psize >= sizeof(DWORD))
517 *ret = QueryCustomPolicy_policy;
519 return QueryCustomPolicy_hres;
522 static const IInternetHostSecurityManagerVtbl InternetHostSecurityManagerVtbl = {
523 InternetHostSecurityManager_QueryInterface,
524 InternetHostSecurityManager_AddRef,
525 InternetHostSecurityManager_Release,
526 InternetHostSecurityManager_GetSecurityId,
527 InternetHostSecurityManager_ProcessUrlAction,
528 InternetHostSecurityManager_QueryCustomPolicy
531 static IInternetHostSecurityManager InternetHostSecurityManager = { &InternetHostSecurityManagerVtbl };
533 static IServiceProvider ServiceProvider;
535 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
537 ok(0, "unexpected call\n");
538 return E_NOINTERFACE;
541 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
543 return 2;
546 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
548 return 1;
551 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
552 REFGUID guidService, REFIID riid, void **ppv)
554 if(IsEqualGUID(&SID_GetCaller, guidService))
555 return E_NOINTERFACE;
557 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
558 if(iface == &ServiceProvider)
559 CHECK_EXPECT(Host_QS_SecMgr);
560 else
561 CHECK_EXPECT(Caller_QS_SecMgr);
562 ok(IsEqualGUID(&IID_IInternetHostSecurityManager, riid), "unexpected riid %s\n", wine_dbgstr_guid(riid));
563 if(SUCCEEDED(QS_SecMgr_hres))
564 *ppv = &InternetHostSecurityManager;
565 return QS_SecMgr_hres;
568 ok(0, "unexpected service %s\n", wine_dbgstr_guid(guidService));
569 return E_NOINTERFACE;
572 static IServiceProviderVtbl ServiceProviderVtbl = {
573 ServiceProvider_QueryInterface,
574 ServiceProvider_AddRef,
575 ServiceProvider_Release,
576 ServiceProvider_QueryService
579 static IServiceProvider ServiceProvider = { &ServiceProviderVtbl };
580 static IServiceProvider caller_sp = { &ServiceProviderVtbl };
582 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
584 if(IsEqualGUID(&IID_IUnknown, riid)) {
585 *ppv = iface;
586 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
587 *ppv = iface;
588 }else if(IsEqualGUID(&IID_IServiceProvider, riid) && AllowIServiceProvider) {
589 *ppv = &ServiceProvider;
590 }else {
591 *ppv = NULL;
592 return E_NOINTERFACE;
595 IUnknown_AddRef((IUnknown*)*ppv);
596 return S_OK;
599 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
601 return 2;
604 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
606 return 1;
609 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
611 *plcid = GetUserDefaultLCID();
612 return S_OK;
615 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
616 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
618 ok(dwReturnMask == SCRIPTINFO_IUNKNOWN, "unexpected dwReturnMask %x\n", dwReturnMask);
619 ok(!ppti, "ppti != NULL\n");
620 ok(!strcmp_wa(pstrName, "test"), "pstrName = %s\n", wine_dbgstr_w(pstrName));
622 *ppiunkItem = (IUnknown*)&globalObj;
623 return S_OK;
626 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
628 return E_NOTIMPL;
631 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
632 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
634 return E_NOTIMPL;
637 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
639 return E_NOTIMPL;
642 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
644 return E_NOTIMPL;
647 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
649 return E_NOTIMPL;
652 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
654 return E_NOTIMPL;
657 #undef ACTSCPSITE_THIS
659 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
660 ActiveScriptSite_QueryInterface,
661 ActiveScriptSite_AddRef,
662 ActiveScriptSite_Release,
663 ActiveScriptSite_GetLCID,
664 ActiveScriptSite_GetItemInfo,
665 ActiveScriptSite_GetDocVersionString,
666 ActiveScriptSite_OnScriptTerminate,
667 ActiveScriptSite_OnStateChange,
668 ActiveScriptSite_OnScriptError,
669 ActiveScriptSite_OnEnterScript,
670 ActiveScriptSite_OnLeaveScript
673 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
675 static void set_safety_options(IUnknown *unk, BOOL use_sec_mgr)
677 IObjectSafety *safety;
678 DWORD supported, enabled, options_all, options_set;
679 HRESULT hres;
681 hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
682 ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
683 if(FAILED(hres))
684 return;
686 options_all = INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER;
687 if(use_sec_mgr)
688 options_set = options_all;
689 else
690 options_set = INTERFACE_USES_DISPEX;
692 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, options_all, options_set);
693 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
695 supported = enabled = 0xdeadbeef;
696 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
697 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
698 ok(supported == options_all, "supported=%x, expected %x\n", supported, options_all);
699 ok(enabled == options_set, "enabled=%x, expected %x\n", enabled, options_set);
701 IObjectSafety_Release(safety);
704 #define parse_script_a(p,s) _parse_script_a(__LINE__,p,s)
705 static void _parse_script_a(unsigned line, IActiveScriptParse *parser, const char *script)
707 BSTR str;
708 HRESULT hres;
710 str = a2bstr(script);
711 hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
712 SysFreeString(str);
713 ok_(__FILE__,line)(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
716 static IActiveScriptParse *create_script(BOOL skip_tests, BOOL use_sec_mgr)
718 IActiveScriptParse *parser;
719 IActiveScript *script;
720 HRESULT hres;
722 QS_SecMgr_hres = S_OK;
723 ProcessUrlAction_hres = S_OK;
724 ProcessUrlAction_policy = URLPOLICY_ALLOW;
725 CreateInstance_hres = S_OK;
726 QueryCustomPolicy_hres = S_OK;
727 QueryCustomPolicy_psize = sizeof(DWORD);
728 QueryCustomPolicy_policy = URLPOLICY_ALLOW;
729 QI_IDispatch_hres = S_OK;
730 SetSite_hres = S_OK;
731 AllowIServiceProvider = TRUE;
733 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
734 &IID_IActiveScript, (void**)&script);
735 if(!skip_tests)
736 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
737 if(FAILED(hres))
738 return NULL;
740 if(!skip_tests)
741 set_safety_options((IUnknown*)script, use_sec_mgr);
743 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parser);
744 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
746 hres = IActiveScriptParse_InitNew(parser);
747 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
749 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
750 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
752 hres = IActiveScript_AddNamedItem(script, testW,
753 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
754 ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
756 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
757 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
759 IActiveScript_Release(script);
761 if(!skip_tests) {
762 parse_script_a(parser,
763 "function testException(func, type, number) {\n"
764 " try {\n"
765 " func();\n"
766 " }catch(e) {\n"
767 " ok(e.name === type, 'e.name = ' + e.name + ', expected ' + type)\n"
768 " ok(e.number === number, 'e.number = ' + e.number + ', expected ' + number);\n"
769 " return;\n"
770 " }\n"
771 " ok(false, 'exception expected');\n"
772 "}");
775 return parser;
778 static IDispatchEx *parse_procedure_a(IActiveScriptParse *parser, const char *src)
780 IActiveScriptParseProcedure2 *parse_proc;
781 IDispatchEx *dispex;
782 IDispatch *disp;
783 BSTR str;
784 HRESULT hres;
786 hres = IActiveScriptParse_QueryInterface(parser, &IID_IActiveScriptParseProcedure2, (void**)&parse_proc);
787 ok(hres == S_OK, "Could not get IActiveScriptParseProcedure2: %08x\n", hres);
789 str = a2bstr(src);
790 hres = IActiveScriptParseProcedure2_ParseProcedureText(parse_proc, str, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, &disp);
791 SysFreeString(str);
792 IActiveScriptParseProcedure2_Release(parse_proc);
793 ok(hres == S_OK, "ParseProcedureText failed: %08x\n", hres);
794 ok(disp != NULL, "disp == NULL\n");
796 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
797 IDispatch_Release(disp);
798 ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
800 return dispex;
803 #define call_procedure(p,c) _call_procedure(__LINE__,p,c)
804 static void _call_procedure(unsigned line, IDispatchEx *proc, IServiceProvider *caller)
806 DISPPARAMS dp = {NULL,NULL,0,0};
807 EXCEPINFO ei = {0};
808 HRESULT hres;
810 hres = IDispatchEx_InvokeEx(proc, DISPID_VALUE, 0, DISPATCH_METHOD, &dp, NULL, &ei, caller);
811 ok_(__FILE__,line)(hres == S_OK, "InvokeEx failed: %08x\n", hres);
815 static void test_ActiveXObject(void)
817 IActiveScriptParse *parser;
818 IDispatchEx *proc;
820 parser = create_script(FALSE, TRUE);
822 SET_EXPECT(Host_QS_SecMgr);
823 SET_EXPECT(ProcessUrlAction);
824 SET_EXPECT(CreateInstance);
825 SET_EXPECT(QueryCustomPolicy);
826 SET_EXPECT(QI_IObjectWithSite);
827 SET_EXPECT(reportSuccess);
828 parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
829 CHECK_CALLED(Host_QS_SecMgr);
830 CHECK_CALLED(ProcessUrlAction);
831 CHECK_CALLED(CreateInstance);
832 CHECK_CALLED(QueryCustomPolicy);
833 CHECK_CALLED(QI_IObjectWithSite);
834 CHECK_CALLED(reportSuccess);
836 proc = parse_procedure_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
838 SET_EXPECT(ProcessUrlAction);
839 SET_EXPECT(CreateInstance);
840 SET_EXPECT(QueryCustomPolicy);
841 SET_EXPECT(QI_IObjectWithSite);
842 SET_EXPECT(reportSuccess);
843 call_procedure(proc, NULL);
844 CHECK_CALLED(ProcessUrlAction);
845 CHECK_CALLED(CreateInstance);
846 CHECK_CALLED(QueryCustomPolicy);
847 CHECK_CALLED(QI_IObjectWithSite);
848 CHECK_CALLED(reportSuccess);
850 SET_EXPECT(ProcessUrlAction);
851 SET_EXPECT(CreateInstance);
852 SET_EXPECT(QueryCustomPolicy);
853 SET_EXPECT(QI_IObjectWithSite);
854 SET_EXPECT(reportSuccess);
855 call_procedure(proc, &caller_sp);
856 CHECK_CALLED(ProcessUrlAction);
857 CHECK_CALLED(CreateInstance);
858 CHECK_CALLED(QueryCustomPolicy);
859 CHECK_CALLED(QI_IObjectWithSite);
860 CHECK_CALLED(reportSuccess);
862 IDispatchEx_Release(proc);
863 IActiveScriptParse_Release(parser);
865 parser = create_script(FALSE, TRUE);
866 proc = parse_procedure_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
868 SET_EXPECT(Host_QS_SecMgr);
869 SET_EXPECT(ProcessUrlAction);
870 SET_EXPECT(CreateInstance);
871 SET_EXPECT(QueryCustomPolicy);
872 SET_EXPECT(QI_IObjectWithSite);
873 SET_EXPECT(reportSuccess);
874 call_procedure(proc, &caller_sp);
875 CHECK_CALLED(Host_QS_SecMgr);
876 CHECK_CALLED(ProcessUrlAction);
877 CHECK_CALLED(CreateInstance);
878 CHECK_CALLED(QueryCustomPolicy);
879 CHECK_CALLED(QI_IObjectWithSite);
880 CHECK_CALLED(reportSuccess);
882 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.TestABC'); }, 'Error', -2146827859);");
884 IDispatchEx_Release(proc);
885 IActiveScriptParse_Release(parser);
887 parser = create_script(FALSE, TRUE);
888 QS_SecMgr_hres = E_NOINTERFACE;
890 SET_EXPECT(Host_QS_SecMgr);
891 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
892 CHECK_CALLED(Host_QS_SecMgr);
894 IActiveScriptParse_Release(parser);
896 parser = create_script(FALSE, TRUE);
897 ProcessUrlAction_hres = E_FAIL;
899 SET_EXPECT(Host_QS_SecMgr);
900 SET_EXPECT(ProcessUrlAction);
901 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
902 CHECK_CALLED(Host_QS_SecMgr);
903 CHECK_CALLED(ProcessUrlAction);
905 IActiveScriptParse_Release(parser);
907 parser = create_script(FALSE, TRUE);
908 ProcessUrlAction_policy = URLPOLICY_DISALLOW;
910 SET_EXPECT(Host_QS_SecMgr);
911 SET_EXPECT(ProcessUrlAction);
912 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
913 CHECK_CALLED(Host_QS_SecMgr);
914 CHECK_CALLED(ProcessUrlAction);
916 IActiveScriptParse_Release(parser);
918 parser = create_script(FALSE, TRUE);
919 CreateInstance_hres = E_FAIL;
921 SET_EXPECT(Host_QS_SecMgr);
922 SET_EXPECT(ProcessUrlAction);
923 SET_EXPECT(CreateInstance);
924 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
925 CHECK_CALLED(Host_QS_SecMgr);
926 CHECK_CALLED(ProcessUrlAction);
927 CHECK_CALLED(CreateInstance);
929 IActiveScriptParse_Release(parser);
931 parser = create_script(FALSE, TRUE);
932 QueryCustomPolicy_hres = E_FAIL;
934 SET_EXPECT(Host_QS_SecMgr);
935 SET_EXPECT(ProcessUrlAction);
936 SET_EXPECT(CreateInstance);
937 SET_EXPECT(QueryCustomPolicy);
938 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
939 CHECK_CALLED(Host_QS_SecMgr);
940 CHECK_CALLED(ProcessUrlAction);
941 CHECK_CALLED(CreateInstance);
942 CHECK_CALLED(QueryCustomPolicy);
944 IActiveScriptParse_Release(parser);
946 parser = create_script(FALSE, TRUE);
947 QueryCustomPolicy_psize = 6;
949 SET_EXPECT(Host_QS_SecMgr);
950 SET_EXPECT(ProcessUrlAction);
951 SET_EXPECT(CreateInstance);
952 SET_EXPECT(QueryCustomPolicy);
953 SET_EXPECT(QI_IObjectWithSite);
954 SET_EXPECT(reportSuccess);
955 parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
956 CHECK_CALLED(Host_QS_SecMgr);
957 CHECK_CALLED(ProcessUrlAction);
958 CHECK_CALLED(CreateInstance);
959 CHECK_CALLED(QueryCustomPolicy);
960 CHECK_CALLED(QI_IObjectWithSite);
961 CHECK_CALLED(reportSuccess);
963 IActiveScriptParse_Release(parser);
965 parser = create_script(FALSE, TRUE);
966 QueryCustomPolicy_policy = URLPOLICY_DISALLOW;
968 SET_EXPECT(Host_QS_SecMgr);
969 SET_EXPECT(ProcessUrlAction);
970 SET_EXPECT(CreateInstance);
971 SET_EXPECT(QueryCustomPolicy);
972 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
973 CHECK_CALLED(Host_QS_SecMgr);
974 CHECK_CALLED(ProcessUrlAction);
975 CHECK_CALLED(CreateInstance);
976 CHECK_CALLED(QueryCustomPolicy);
978 QueryCustomPolicy_psize = 6;
980 SET_EXPECT(ProcessUrlAction);
981 SET_EXPECT(CreateInstance);
982 SET_EXPECT(QueryCustomPolicy);
983 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
984 CHECK_CALLED(ProcessUrlAction);
985 CHECK_CALLED(CreateInstance);
986 CHECK_CALLED(QueryCustomPolicy);
988 QueryCustomPolicy_policy = URLPOLICY_ALLOW;
989 QueryCustomPolicy_psize = 3;
991 SET_EXPECT(ProcessUrlAction);
992 SET_EXPECT(CreateInstance);
993 SET_EXPECT(QueryCustomPolicy);
994 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
995 CHECK_CALLED(ProcessUrlAction);
996 CHECK_CALLED(CreateInstance);
997 CHECK_CALLED(QueryCustomPolicy);
999 IActiveScriptParse_Release(parser);
1001 parser = create_script(FALSE, FALSE);
1003 SET_EXPECT(CreateInstance);
1004 SET_EXPECT(QI_IObjectWithSite);
1005 SET_EXPECT(reportSuccess);
1006 parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
1007 CHECK_CALLED(CreateInstance);
1008 CHECK_CALLED(QI_IObjectWithSite);
1009 CHECK_CALLED(reportSuccess);
1011 IActiveScriptParse_Release(parser);
1013 parser = create_script(FALSE, TRUE);
1014 object_with_site = &ObjectWithSite;
1016 SET_EXPECT(Host_QS_SecMgr);
1017 SET_EXPECT(ProcessUrlAction);
1018 SET_EXPECT(CreateInstance);
1019 SET_EXPECT(QueryCustomPolicy);
1020 SET_EXPECT(QI_IObjectWithSite);
1021 SET_EXPECT(SetSite);
1022 SET_EXPECT(reportSuccess);
1023 parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
1024 CHECK_CALLED(Host_QS_SecMgr);
1025 CHECK_CALLED(ProcessUrlAction);
1026 CHECK_CALLED(CreateInstance);
1027 CHECK_CALLED(QueryCustomPolicy);
1028 CHECK_CALLED(QI_IObjectWithSite);
1029 CHECK_CALLED(SetSite);
1030 CHECK_CALLED(reportSuccess);
1032 SetSite_hres = E_FAIL;
1033 SET_EXPECT(ProcessUrlAction);
1034 SET_EXPECT(CreateInstance);
1035 SET_EXPECT(QueryCustomPolicy);
1036 SET_EXPECT(QI_IObjectWithSite);
1037 SET_EXPECT(SetSite);
1038 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
1039 CHECK_CALLED(ProcessUrlAction);
1040 CHECK_CALLED(CreateInstance);
1041 CHECK_CALLED(QueryCustomPolicy);
1042 CHECK_CALLED(QI_IObjectWithSite);
1043 CHECK_CALLED(SetSite);
1045 IActiveScriptParse_Release(parser);
1047 /* No IServiceProvider Interface */
1048 parser = create_script(FALSE, FALSE);
1049 object_with_site = &ObjectWithSite;
1050 AllowIServiceProvider = FALSE;
1052 SET_EXPECT(CreateInstance);
1053 SET_EXPECT(QI_IObjectWithSite);
1054 SET_EXPECT(reportSuccess);
1055 SET_EXPECT(SetSite);
1056 parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
1057 CHECK_CALLED(CreateInstance);
1058 CHECK_CALLED(QI_IObjectWithSite);
1059 CHECK_CALLED(reportSuccess);
1060 CHECK_CALLED(SetSite);
1062 IActiveScriptParse_Release(parser);
1064 parser = create_script(FALSE, TRUE);
1065 object_with_site = &ObjectWithSite;
1066 AllowIServiceProvider = FALSE;
1068 parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
1070 IActiveScriptParse_Release(parser);
1073 static BOOL init_key(const char *key_name, const char *def_value, BOOL init)
1075 HKEY hkey;
1076 DWORD res;
1078 if(!init) {
1079 RegDeleteKeyA(HKEY_CLASSES_ROOT, key_name);
1080 return TRUE;
1083 res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey);
1084 if(res != ERROR_SUCCESS)
1085 return FALSE;
1087 if(def_value)
1088 res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value));
1090 RegCloseKey(hkey);
1092 return res == ERROR_SUCCESS;
1095 static BOOL init_registry(BOOL init)
1097 return init_key("Wine.Test\\CLSID", TESTOBJ_CLSID, init);
1100 static BOOL register_activex(void)
1102 DWORD regid;
1103 HRESULT hres;
1105 if(!init_registry(TRUE)) {
1106 init_registry(FALSE);
1107 return FALSE;
1110 hres = CoRegisterClassObject(&CLSID_TestObj, (IUnknown *)&activex_cf,
1111 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
1112 ok(hres == S_OK, "Could not register script engine: %08x\n", hres);
1114 return TRUE;
1117 static BOOL check_jscript(void)
1119 IActiveScriptProperty *script_prop;
1120 IActiveScriptParse *parser;
1121 BSTR str;
1122 HRESULT hres;
1124 parser = create_script(TRUE, TRUE);
1125 if(!parser)
1126 return FALSE;
1128 str = a2bstr("if(!('localeCompare' in String.prototype)) throw 1;");
1129 hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
1130 SysFreeString(str);
1132 if(hres == S_OK)
1133 hres = IActiveScriptParse_QueryInterface(parser, &IID_IActiveScriptProperty, (void**)&script_prop);
1134 IActiveScriptParse_Release(parser);
1135 if(hres == S_OK)
1136 IActiveScriptProperty_Release(script_prop);
1138 return hres == S_OK;
1141 START_TEST(activex)
1143 CoInitialize(NULL);
1145 if(check_jscript()) {
1146 if(register_activex()) {
1147 test_ActiveXObject();
1148 init_registry(FALSE);
1149 }else {
1150 skip("Could not register ActiveX object\n");
1152 }else {
1153 win_skip("Broken engine, probably too old\n");
1156 CoUninitialize();