tests: Assorted spelling fixes.
[wine] / dlls / jscript / tests / activex.c
1 /*
2  * Copyright 2009 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 #include <ole2.h>
25 #include <dispex.h>
26 #include <activscp.h>
27 #include <objsafe.h>
28 #include <urlmon.h>
29 #include <mshtmhst.h>
30
31 #include "wine/test.h"
32
33 static const CLSID CLSID_JScript =
34     {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
35
36 #define DEFINE_EXPECT(func) \
37     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
38
39 #define SET_EXPECT(func) \
40     expect_ ## func = TRUE
41
42 #define SET_CALLED(func) \
43     called_ ## func = TRUE
44
45 #define CHECK_EXPECT2(func) \
46     do { \
47         ok(expect_ ##func, "unexpected call " #func "\n"); \
48         called_ ## func = TRUE; \
49     }while(0)
50
51 #define CHECK_EXPECT(func) \
52     do { \
53         CHECK_EXPECT2(func); \
54         expect_ ## func = FALSE; \
55     }while(0)
56
57 #define CHECK_CALLED(func) \
58     do { \
59         ok(called_ ## func, "expected " #func "\n"); \
60         expect_ ## func = called_ ## func = FALSE; \
61     }while(0)
62
63 DEFINE_EXPECT(CreateInstance);
64 DEFINE_EXPECT(ProcessUrlAction);
65 DEFINE_EXPECT(QueryCustomPolicy);
66 DEFINE_EXPECT(reportSuccess);
67 DEFINE_EXPECT(Host_QS_SecMgr);
68 DEFINE_EXPECT(Caller_QS_SecMgr);
69 DEFINE_EXPECT(QI_IObjectWithSite);
70 DEFINE_EXPECT(SetSite);
71
72 static const WCHAR testW[] = {'t','e','s','t',0};
73
74 static HRESULT QS_SecMgr_hres;
75 static HRESULT ProcessUrlAction_hres;
76 static DWORD ProcessUrlAction_policy;
77 static HRESULT CreateInstance_hres;
78 static HRESULT QueryCustomPolicy_hres;
79 static DWORD QueryCustomPolicy_psize;
80 static DWORD QueryCustomPolicy_policy;
81 static HRESULT QI_IDispatch_hres;
82 static HRESULT SetSite_hres;
83
84 #define TESTOBJ_CLSID "{178fc163-f585-4e24-9c13-4bb7faf80646}"
85
86 static const GUID CLSID_TestObj =
87     {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}};
88
89 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
90 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
91     {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
92
93 #define DISPID_TEST_REPORTSUCCESS    0x1000
94
95 #define DISPID_GLOBAL_OK             0x2000
96
97 static const char *debugstr_guid(REFIID riid)
98 {
99     static char buf[50];
100
101     sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
102             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
103             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
104             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
105
106     return buf;
107 }
108
109 static BSTR a2bstr(const char *str)
110 {
111     BSTR ret;
112     int len;
113
114     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
115     ret = SysAllocStringLen(NULL, len-1);
116     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
117
118     return ret;
119 }
120
121 static int strcmp_wa(LPCWSTR strw, const char *stra)
122 {
123     CHAR buf[512];
124     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), 0, 0);
125     return lstrcmpA(buf, stra);
126 }
127
128 static HRESULT WINAPI ObjectWithSite_QueryInterface(IObjectWithSite *iface, REFIID riid, void **ppv)
129 {
130     ok(0, "unexpected call\n");
131     return E_NOTIMPL;
132 }
133
134 static ULONG WINAPI ObjectWithSite_AddRef(IObjectWithSite *iface)
135 {
136     return 2;
137 }
138
139 static ULONG WINAPI ObjectWithSite_Release(IObjectWithSite *iface)
140 {
141     return 1;
142 }
143
144 static HRESULT WINAPI ObjectWithSite_SetSite(IObjectWithSite *iface, IUnknown *pUnkSite)
145 {
146     IServiceProvider *sp;
147     HRESULT hres;
148
149
150     CHECK_EXPECT(SetSite);
151     ok(pUnkSite != NULL, "pUnkSite == NULL\n");
152
153     hres = IUnknown_QueryInterface(pUnkSite, &IID_IServiceProvider, (void**)&sp);
154     ok(hres == S_OK, "Could not get IServiceProvider iface: %08x\n", hres);
155     IServiceProvider_Release(sp);
156
157     return SetSite_hres;
158 }
159
160 static HRESULT WINAPI ObjectWithSite_GetSite(IObjectWithSite *iface, REFIID riid, void **ppvSite)
161 {
162     ok(0, "unexpected call\n");
163     return E_NOTIMPL;
164 }
165
166 static const IObjectWithSiteVtbl ObjectWithSiteVtbl = {
167     ObjectWithSite_QueryInterface,
168     ObjectWithSite_AddRef,
169     ObjectWithSite_Release,
170     ObjectWithSite_SetSite,
171     ObjectWithSite_GetSite
172 };
173
174 static IObjectWithSite ObjectWithSite = { &ObjectWithSiteVtbl };
175
176 static IObjectWithSite *object_with_site;
177
178 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
179 {
180     *ppv = NULL;
181
182     if(IsEqualGUID(riid, &IID_IUnknown)) {
183        *ppv = iface;
184     }else if(IsEqualGUID(riid, &IID_IDispatch) || IsEqualGUID(riid, &IID_IDispatchEx)) {
185         if(FAILED(QI_IDispatch_hres))
186             return QI_IDispatch_hres;
187         *ppv = iface;
188     }else if(IsEqualGUID(&IID_IObjectWithSite, riid)) {
189         CHECK_EXPECT(QI_IObjectWithSite);
190         *ppv = object_with_site;
191     }else if(IsEqualGUID(&IID_IObjectSafety, riid)) {
192         ok(0, "Unexpected IID_IObjectSafety query\n");
193     }
194
195     return *ppv ? S_OK : E_NOINTERFACE;
196 }
197
198 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
199 {
200     return 2;
201 }
202
203 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
204 {
205     return 1;
206 }
207
208 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
209 {
210     ok(0, "unexpected call\n");
211     return E_NOTIMPL;
212 }
213
214 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
215                                               LCID lcid, ITypeInfo **ppTInfo)
216 {
217     ok(0, "unexpected call\n");
218     return E_NOTIMPL;
219 }
220
221 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
222                                                 LPOLESTR *rgszNames, UINT cNames,
223                                                 LCID lcid, DISPID *rgDispId)
224 {
225     ok(0, "unexpected call\n");
226     return E_NOTIMPL;
227 }
228
229 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
230                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
231                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
232 {
233     ok(0, "unexpected call\n");
234     return E_NOTIMPL;
235 }
236
237 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
238 {
239     ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName), grfdex);
240     return E_NOTIMPL;
241 }
242
243 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
244 {
245     ok(0, "unexpected call\n");
246     return E_NOTIMPL;
247 }
248
249 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
250 {
251     ok(0, "unexpected call\n");
252     return E_NOTIMPL;
253 }
254
255 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
256 {
257     ok(0, "unexpected call\n");
258     return E_NOTIMPL;
259 }
260
261 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
262 {
263     ok(0, "unexpected call\n");
264     return E_NOTIMPL;
265 }
266
267 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
268 {
269     ok(0, "unexpected call\n");
270     return E_NOTIMPL;
271 }
272
273 static HRESULT WINAPI Test_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
274 {
275     if(!strcmp_wa(bstrName, "reportSuccess")) {
276         ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
277         *pid = DISPID_TEST_REPORTSUCCESS;
278         return S_OK;
279     }
280
281     ok(0, "unexpected name %s\n", wine_dbgstr_w(bstrName));
282     return E_NOTIMPL;
283 }
284
285 static HRESULT WINAPI Test_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
286         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
287 {
288     switch(id) {
289     case DISPID_TEST_REPORTSUCCESS:
290         CHECK_EXPECT(reportSuccess);
291
292         ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
293         ok(pdp != NULL, "pdp == NULL\n");
294         ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
295         ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
296         ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
297         ok(!pvarRes, "pvarRes != NULL\n");
298         ok(pei != NULL, "pei == NULL\n");
299         break;
300
301     default:
302         ok(0, "unexpected call\n");
303         return E_NOTIMPL;
304     }
305
306     return S_OK;
307 }
308
309 static IDispatchExVtbl testObjVtbl = {
310     DispatchEx_QueryInterface,
311     DispatchEx_AddRef,
312     DispatchEx_Release,
313     DispatchEx_GetTypeInfoCount,
314     DispatchEx_GetTypeInfo,
315     DispatchEx_GetIDsOfNames,
316     DispatchEx_Invoke,
317     Test_GetDispID,
318     Test_InvokeEx,
319     DispatchEx_DeleteMemberByName,
320     DispatchEx_DeleteMemberByDispID,
321     DispatchEx_GetMemberProperties,
322     DispatchEx_GetMemberName,
323     DispatchEx_GetNextDispID,
324     DispatchEx_GetNameSpaceParent
325 };
326
327 static IDispatchEx testObj = { &testObjVtbl };
328
329 static HRESULT WINAPI Global_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
330 {
331     if(!strcmp_wa(bstrName, "ok")) {
332         ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
333         *pid = DISPID_GLOBAL_OK;
334         return S_OK;
335     }
336
337     ok(0, "unexpected name %s\n", wine_dbgstr_w(bstrName));
338     return E_NOTIMPL;
339 }
340
341 static HRESULT WINAPI Global_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
342         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
343 {
344     switch(id) {
345     case DISPID_GLOBAL_OK:
346         ok(wFlags == INVOKE_FUNC || wFlags == (INVOKE_FUNC|INVOKE_PROPERTYGET), "wFlags = %x\n", wFlags);
347         ok(pdp != NULL, "pdp == NULL\n");
348         ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
349         ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
350         ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs);
351         ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
352         ok(pei != NULL, "pei == NULL\n");
353
354         ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
355         ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(pdp->rgvarg));
356         ok(V_BOOL(pdp->rgvarg+1), "%s\n", wine_dbgstr_w(V_BSTR(pdp->rgvarg)));
357         break;
358
359     default:
360         ok(0, "unexpected call\n");
361         return E_NOTIMPL;
362     }
363
364     return S_OK;
365 }
366
367 static IDispatchExVtbl globalObjVtbl = {
368     DispatchEx_QueryInterface,
369     DispatchEx_AddRef,
370     DispatchEx_Release,
371     DispatchEx_GetTypeInfoCount,
372     DispatchEx_GetTypeInfo,
373     DispatchEx_GetIDsOfNames,
374     DispatchEx_Invoke,
375     Global_GetDispID,
376     Global_InvokeEx,
377     DispatchEx_DeleteMemberByName,
378     DispatchEx_DeleteMemberByDispID,
379     DispatchEx_GetMemberProperties,
380     DispatchEx_GetMemberName,
381     DispatchEx_GetNextDispID,
382     DispatchEx_GetNameSpaceParent
383 };
384
385 static IDispatchEx globalObj = { &globalObjVtbl };
386
387 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
388 {
389     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
390         *ppv = iface;
391         return S_OK;
392     }
393
394     /* TODO: IClassFactoryEx */
395     *ppv = NULL;
396     return E_NOINTERFACE;
397 }
398
399 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
400 {
401     return 2;
402 }
403
404 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
405 {
406     return 1;
407 }
408
409 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
410 {
411     CHECK_EXPECT(CreateInstance);
412
413     ok(!outer, "outer = %p\n", outer);
414     ok(IsEqualGUID(&IID_IUnknown, riid), "unexpected riid %s\n", debugstr_guid(riid));
415
416     if(SUCCEEDED(CreateInstance_hres))
417         *ppv = &testObj;
418     return CreateInstance_hres;
419 }
420
421 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
422 {
423     ok(0, "unexpected call\n");
424     return S_OK;
425 }
426
427 static const IClassFactoryVtbl ClassFactoryVtbl = {
428     ClassFactory_QueryInterface,
429     ClassFactory_AddRef,
430     ClassFactory_Release,
431     ClassFactory_CreateInstance,
432     ClassFactory_LockServer
433 };
434
435 static IClassFactory activex_cf = { &ClassFactoryVtbl };
436
437 static HRESULT WINAPI InternetHostSecurityManager_QueryInterface(IInternetHostSecurityManager *iface, REFIID riid, void **ppv)
438 {
439     ok(0, "unexpected call\n");
440     return E_NOINTERFACE;
441 }
442
443 static ULONG WINAPI InternetHostSecurityManager_AddRef(IInternetHostSecurityManager *iface)
444 {
445     return 2;
446 }
447
448 static ULONG WINAPI InternetHostSecurityManager_Release(IInternetHostSecurityManager *iface)
449 {
450     return 1;
451 }
452
453 static HRESULT WINAPI InternetHostSecurityManager_GetSecurityId(IInternetHostSecurityManager *iface,  BYTE *pbSecurityId,
454         DWORD *pcbSecurityId, DWORD_PTR dwReserved)
455 {
456     ok(0, "unexpected call\n");
457     return E_NOTIMPL;
458 }
459
460 static HRESULT WINAPI InternetHostSecurityManager_ProcessUrlAction(IInternetHostSecurityManager *iface, DWORD dwAction,
461         BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwFlags, DWORD dwReserved)
462 {
463     CHECK_EXPECT(ProcessUrlAction);
464
465     ok(dwAction == URLACTION_ACTIVEX_RUN, "dwAction = %x\n", dwAction);
466     ok(pPolicy != NULL, "pPolicy == NULL\n");
467     ok(cbPolicy == sizeof(DWORD), "cbPolicy = %d\n", cbPolicy);
468     ok(pContext != NULL, "pContext == NULL\n");
469     ok(cbContext == sizeof(GUID), "cbContext = %d\n", cbContext);
470     ok(IsEqualGUID(pContext, &CLSID_TestObj), "pContext = %s\n", debugstr_guid((const IID*)pContext));
471     ok(!dwFlags, "dwFlags = %x\n", dwFlags);
472     ok(!dwReserved, "dwReserved = %x\n", dwReserved);
473
474     if(SUCCEEDED(ProcessUrlAction_hres))
475         *(DWORD*)pPolicy = ProcessUrlAction_policy;
476     return ProcessUrlAction_hres;
477 }
478
479 static HRESULT WINAPI InternetHostSecurityManager_QueryCustomPolicy(IInternetHostSecurityManager *iface, REFGUID guidKey,
480         BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwReserved)
481 {
482     const struct CONFIRMSAFETY *cs = (const struct CONFIRMSAFETY*)pContext;
483     DWORD *ret;
484
485     CHECK_EXPECT(QueryCustomPolicy);
486
487     ok(IsEqualGUID(&GUID_CUSTOM_CONFIRMOBJECTSAFETY, guidKey), "guidKey = %s\n", debugstr_guid(guidKey));
488
489     ok(ppPolicy != NULL, "ppPolicy == NULL\n");
490     ok(pcbPolicy != NULL, "pcbPolicy == NULL\n");
491     ok(pContext != NULL, "pContext == NULL\n");
492     ok(cbContext == sizeof(struct CONFIRMSAFETY), "cbContext = %d\n", cbContext);
493     ok(!dwReserved, "dwReserved = %x\n", dwReserved);
494
495     /* TODO: CLSID */
496     ok(cs->pUnk != NULL, "cs->pUnk == NULL\n");
497     ok(!cs->dwFlags, "dwFlags = %x\n", cs->dwFlags);
498
499     if(FAILED(QueryCustomPolicy_hres))
500         return QueryCustomPolicy_hres;
501
502     ret = CoTaskMemAlloc(QueryCustomPolicy_psize);
503     *ppPolicy = (BYTE*)ret;
504     *pcbPolicy = QueryCustomPolicy_psize;
505     memset(ret, 0, QueryCustomPolicy_psize);
506     if(QueryCustomPolicy_psize >= sizeof(DWORD))
507         *ret = QueryCustomPolicy_policy;
508
509     return QueryCustomPolicy_hres;
510 }
511
512 static const IInternetHostSecurityManagerVtbl InternetHostSecurityManagerVtbl = {
513     InternetHostSecurityManager_QueryInterface,
514     InternetHostSecurityManager_AddRef,
515     InternetHostSecurityManager_Release,
516     InternetHostSecurityManager_GetSecurityId,
517     InternetHostSecurityManager_ProcessUrlAction,
518     InternetHostSecurityManager_QueryCustomPolicy
519 };
520
521 static IInternetHostSecurityManager InternetHostSecurityManager = { &InternetHostSecurityManagerVtbl };
522
523 static IServiceProvider ServiceProvider;
524
525 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
526 {
527     ok(0, "unexpected call\n");
528     return E_NOINTERFACE;
529 }
530
531 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
532 {
533     return 2;
534 }
535
536 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
537 {
538     return 1;
539 }
540
541 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
542         REFGUID guidService, REFIID riid, void **ppv)
543 {
544     if(IsEqualGUID(&SID_GetCaller, guidService))
545         return E_NOINTERFACE;
546
547     if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
548         if(iface == &ServiceProvider)
549             CHECK_EXPECT(Host_QS_SecMgr);
550         else
551             CHECK_EXPECT(Caller_QS_SecMgr);
552         ok(IsEqualGUID(&IID_IInternetHostSecurityManager, riid), "unexpected riid %s\n", debugstr_guid(riid));
553         if(SUCCEEDED(QS_SecMgr_hres))
554             *ppv = &InternetHostSecurityManager;
555         return QS_SecMgr_hres;
556     }
557
558     ok(0, "unexpected service %s\n", debugstr_guid(guidService));
559     return E_NOINTERFACE;
560 }
561
562 static IServiceProviderVtbl ServiceProviderVtbl = {
563     ServiceProvider_QueryInterface,
564     ServiceProvider_AddRef,
565     ServiceProvider_Release,
566     ServiceProvider_QueryService
567 };
568
569 static IServiceProvider ServiceProvider = { &ServiceProviderVtbl };
570 static IServiceProvider caller_sp = { &ServiceProviderVtbl };
571
572 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
573 {
574     if(IsEqualGUID(&IID_IUnknown, riid)) {
575         *ppv = iface;
576     }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
577         *ppv = iface;
578     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
579         *ppv = &ServiceProvider;
580     }else {
581         *ppv = NULL;
582         return E_NOINTERFACE;
583     }
584
585     IUnknown_AddRef((IUnknown*)*ppv);
586     return S_OK;
587 }
588
589 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
590 {
591     return 2;
592 }
593
594 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
595 {
596     return 1;
597 }
598
599 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
600 {
601     *plcid = GetUserDefaultLCID();
602     return S_OK;
603 }
604
605 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
606         DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
607 {
608     ok(dwReturnMask == SCRIPTINFO_IUNKNOWN, "unexpected dwReturnMask %x\n", dwReturnMask);
609     ok(!ppti, "ppti != NULL\n");
610     ok(!strcmp_wa(pstrName, "test"), "pstrName = %s\n", wine_dbgstr_w(pstrName));
611
612     *ppiunkItem = (IUnknown*)&globalObj;
613     return S_OK;
614 }
615
616 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
617 {
618     return E_NOTIMPL;
619 }
620
621 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
622         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
623 {
624     return E_NOTIMPL;
625 }
626
627 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
628 {
629     return E_NOTIMPL;
630 }
631
632 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
633 {
634     return E_NOTIMPL;
635 }
636
637 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
638 {
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
643 {
644     return E_NOTIMPL;
645 }
646
647 #undef ACTSCPSITE_THIS
648
649 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
650     ActiveScriptSite_QueryInterface,
651     ActiveScriptSite_AddRef,
652     ActiveScriptSite_Release,
653     ActiveScriptSite_GetLCID,
654     ActiveScriptSite_GetItemInfo,
655     ActiveScriptSite_GetDocVersionString,
656     ActiveScriptSite_OnScriptTerminate,
657     ActiveScriptSite_OnStateChange,
658     ActiveScriptSite_OnScriptError,
659     ActiveScriptSite_OnEnterScript,
660     ActiveScriptSite_OnLeaveScript
661 };
662
663 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
664
665 static void set_safety_options(IUnknown *unk, BOOL use_sec_mgr)
666 {
667     IObjectSafety *safety;
668     DWORD supported, enabled, options_all, options_set;
669     HRESULT hres;
670
671     hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
672     ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
673     if(FAILED(hres))
674         return;
675
676     options_all = INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER;
677     if(use_sec_mgr)
678         options_set = options_all;
679     else
680         options_set = INTERFACE_USES_DISPEX;
681
682     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, options_all, options_set);
683     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
684
685     supported = enabled = 0xdeadbeef;
686     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
687     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
688     ok(supported == options_all, "supported=%x, expected %x\n", supported, options_all);
689     ok(enabled == options_set, "enabled=%x, expected %x\n", enabled, options_set);
690
691     IObjectSafety_Release(safety);
692 }
693
694 #define parse_script_a(p,s) _parse_script_a(__LINE__,p,s)
695 static void _parse_script_a(unsigned line, IActiveScriptParse *parser, const char *script)
696 {
697     BSTR str;
698     HRESULT hres;
699
700     str = a2bstr(script);
701     hres = IActiveScriptParse64_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
702     SysFreeString(str);
703     ok_(__FILE__,line)(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
704 }
705
706 static IActiveScriptParse *create_script(BOOL skip_tests, BOOL use_sec_mgr)
707 {
708     IActiveScriptParse *parser;
709     IActiveScript *script;
710     HRESULT hres;
711
712     QS_SecMgr_hres = S_OK;
713     ProcessUrlAction_hres = S_OK;
714     ProcessUrlAction_policy = URLPOLICY_ALLOW;
715     CreateInstance_hres = S_OK;
716     QueryCustomPolicy_hres = S_OK;
717     QueryCustomPolicy_psize = sizeof(DWORD);
718     QueryCustomPolicy_policy = URLPOLICY_ALLOW;
719     QI_IDispatch_hres = S_OK;
720     SetSite_hres = S_OK;
721
722     hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
723             &IID_IActiveScript, (void**)&script);
724     if(!skip_tests)
725         ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
726     if(FAILED(hres))
727         return NULL;
728
729     if(!skip_tests)
730         set_safety_options((IUnknown*)script, use_sec_mgr);
731
732     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parser);
733     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
734
735     hres = IActiveScriptParse64_InitNew(parser);
736     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
737
738     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
739     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
740
741     hres = IActiveScript_AddNamedItem(script, testW,
742             SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
743     ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
744
745     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
746     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
747
748     IActiveScript_Release(script);
749
750     if(!skip_tests) {
751         parse_script_a(parser,
752                 "function testException(func, type, number) {\n"
753                 "    try {\n"
754                 "        func();\n"
755                 "    }catch(e) {\n"
756                 "        ok(e.name === type, 'e.name = ' + e.name + ', expected ' + type)\n"
757                 "        ok(e.number === number, 'e.number = ' + e.number + ', expected ' + number);\n"
758                 "        return;\n"
759                 "    }\n"
760                 "    ok(false, 'exception expected');\n"
761                 "}");
762     }
763
764     return parser;
765 }
766
767 static IDispatchEx *parse_procedure_a(IActiveScriptParse *parser, const char *src)
768 {
769     IActiveScriptParseProcedure2 *parse_proc;
770     IDispatchEx *dispex;
771     IDispatch *disp;
772     BSTR str;
773     HRESULT hres;
774
775     hres = IUnknown_QueryInterface(parser, &IID_IActiveScriptParseProcedure2, (void**)&parse_proc);
776     ok(hres == S_OK, "Could not get IActiveScriptParseProcedure2: %08x\n", hres);
777
778     str = a2bstr(src);
779     hres = IActiveScriptParseProcedure2_64_ParseProcedureText(parse_proc, str, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, &disp);
780     SysFreeString(str);
781     IUnknown_Release(parse_proc);
782     ok(hres == S_OK, "ParseProcedureText failed: %08x\n", hres);
783     ok(disp != NULL, "disp == NULL\n");
784
785     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
786     IDispatch_Release(dispex);
787     ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
788
789     return dispex;
790 }
791
792 #define call_procedure(p,c) _call_procedure(__LINE__,p,c)
793 static void _call_procedure(unsigned line, IDispatchEx *proc, IServiceProvider *caller)
794 {
795     DISPPARAMS dp = {NULL,NULL,0,0};
796     EXCEPINFO ei = {0};
797     HRESULT hres;
798
799     hres = IDispatchEx_InvokeEx(proc, DISPID_VALUE, 0, DISPATCH_METHOD, &dp, NULL, &ei, caller);
800     ok_(__FILE__,line)(hres == S_OK, "InvokeEx failed: %08x\n", hres);
801
802 }
803
804 static void test_ActiveXObject(void)
805 {
806     IActiveScriptParse *parser;
807     IDispatchEx *proc;
808
809     parser = create_script(FALSE, TRUE);
810
811     SET_EXPECT(Host_QS_SecMgr);
812     SET_EXPECT(ProcessUrlAction);
813     SET_EXPECT(CreateInstance);
814     SET_EXPECT(QueryCustomPolicy);
815     SET_EXPECT(QI_IObjectWithSite);
816     SET_EXPECT(reportSuccess);
817     parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
818     CHECK_CALLED(Host_QS_SecMgr);
819     CHECK_CALLED(ProcessUrlAction);
820     CHECK_CALLED(CreateInstance);
821     CHECK_CALLED(QueryCustomPolicy);
822     CHECK_CALLED(QI_IObjectWithSite);
823     CHECK_CALLED(reportSuccess);
824
825     proc = parse_procedure_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
826
827     SET_EXPECT(ProcessUrlAction);
828     SET_EXPECT(CreateInstance);
829     SET_EXPECT(QueryCustomPolicy);
830     SET_EXPECT(QI_IObjectWithSite);
831     SET_EXPECT(reportSuccess);
832     call_procedure(proc, NULL);
833     CHECK_CALLED(ProcessUrlAction);
834     CHECK_CALLED(CreateInstance);
835     CHECK_CALLED(QueryCustomPolicy);
836     CHECK_CALLED(QI_IObjectWithSite);
837     CHECK_CALLED(reportSuccess);
838
839     SET_EXPECT(ProcessUrlAction);
840     SET_EXPECT(CreateInstance);
841     SET_EXPECT(QueryCustomPolicy);
842     SET_EXPECT(QI_IObjectWithSite);
843     SET_EXPECT(reportSuccess);
844     call_procedure(proc, &caller_sp);
845     CHECK_CALLED(ProcessUrlAction);
846     CHECK_CALLED(CreateInstance);
847     CHECK_CALLED(QueryCustomPolicy);
848     CHECK_CALLED(QI_IObjectWithSite);
849     CHECK_CALLED(reportSuccess);
850
851     IDispatchEx_Release(proc);
852     IUnknown_Release(parser);
853
854     parser = create_script(FALSE, TRUE);
855     proc = parse_procedure_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
856
857     SET_EXPECT(Host_QS_SecMgr);
858     SET_EXPECT(ProcessUrlAction);
859     SET_EXPECT(CreateInstance);
860     SET_EXPECT(QueryCustomPolicy);
861     SET_EXPECT(QI_IObjectWithSite);
862     SET_EXPECT(reportSuccess);
863     call_procedure(proc, &caller_sp);
864     CHECK_CALLED(Host_QS_SecMgr);
865     CHECK_CALLED(ProcessUrlAction);
866     CHECK_CALLED(CreateInstance);
867     CHECK_CALLED(QueryCustomPolicy);
868     CHECK_CALLED(QI_IObjectWithSite);
869     CHECK_CALLED(reportSuccess);
870
871     parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.TestABC'); }, 'Error', -2146827859);");
872
873     IDispatchEx_Release(proc);
874     IUnknown_Release(parser);
875
876     parser = create_script(FALSE, TRUE);
877     QS_SecMgr_hres = E_NOINTERFACE;
878
879     SET_EXPECT(Host_QS_SecMgr);
880     parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
881     CHECK_CALLED(Host_QS_SecMgr);
882
883     IUnknown_Release(parser);
884
885     parser = create_script(FALSE, TRUE);
886     ProcessUrlAction_hres = E_FAIL;
887
888     SET_EXPECT(Host_QS_SecMgr);
889     SET_EXPECT(ProcessUrlAction);
890     parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
891     CHECK_CALLED(Host_QS_SecMgr);
892     CHECK_CALLED(ProcessUrlAction);
893
894     IUnknown_Release(parser);
895
896     parser = create_script(FALSE, TRUE);
897     ProcessUrlAction_policy = URLPOLICY_DISALLOW;
898
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);
904
905     IUnknown_Release(parser);
906
907     parser = create_script(FALSE, TRUE);
908     CreateInstance_hres = E_FAIL;
909
910     SET_EXPECT(Host_QS_SecMgr);
911     SET_EXPECT(ProcessUrlAction);
912     SET_EXPECT(CreateInstance);
913     parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
914     CHECK_CALLED(Host_QS_SecMgr);
915     CHECK_CALLED(ProcessUrlAction);
916     CHECK_CALLED(CreateInstance);
917
918     IUnknown_Release(parser);
919
920     parser = create_script(FALSE, TRUE);
921     QueryCustomPolicy_hres = E_FAIL;
922
923     SET_EXPECT(Host_QS_SecMgr);
924     SET_EXPECT(ProcessUrlAction);
925     SET_EXPECT(CreateInstance);
926     SET_EXPECT(QueryCustomPolicy);
927     parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
928     CHECK_CALLED(Host_QS_SecMgr);
929     CHECK_CALLED(ProcessUrlAction);
930     CHECK_CALLED(CreateInstance);
931     CHECK_CALLED(QueryCustomPolicy);
932
933     IUnknown_Release(parser);
934
935     parser = create_script(FALSE, TRUE);
936     QueryCustomPolicy_psize = 6;
937
938     SET_EXPECT(Host_QS_SecMgr);
939     SET_EXPECT(ProcessUrlAction);
940     SET_EXPECT(CreateInstance);
941     SET_EXPECT(QueryCustomPolicy);
942     SET_EXPECT(QI_IObjectWithSite);
943     SET_EXPECT(reportSuccess);
944     parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
945     CHECK_CALLED(Host_QS_SecMgr);
946     CHECK_CALLED(ProcessUrlAction);
947     CHECK_CALLED(CreateInstance);
948     CHECK_CALLED(QueryCustomPolicy);
949     CHECK_CALLED(QI_IObjectWithSite);
950     CHECK_CALLED(reportSuccess);
951
952     IUnknown_Release(parser);
953
954     parser = create_script(FALSE, TRUE);
955     QueryCustomPolicy_policy = URLPOLICY_DISALLOW;
956
957     SET_EXPECT(Host_QS_SecMgr);
958     SET_EXPECT(ProcessUrlAction);
959     SET_EXPECT(CreateInstance);
960     SET_EXPECT(QueryCustomPolicy);
961     parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
962     CHECK_CALLED(Host_QS_SecMgr);
963     CHECK_CALLED(ProcessUrlAction);
964     CHECK_CALLED(CreateInstance);
965     CHECK_CALLED(QueryCustomPolicy);
966
967     QueryCustomPolicy_psize = 6;
968
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(ProcessUrlAction);
974     CHECK_CALLED(CreateInstance);
975     CHECK_CALLED(QueryCustomPolicy);
976
977     QueryCustomPolicy_policy = URLPOLICY_ALLOW;
978     QueryCustomPolicy_psize = 3;
979
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);
987
988     IUnknown_Release(parser);
989
990     parser = create_script(FALSE, FALSE);
991
992     SET_EXPECT(CreateInstance);
993     SET_EXPECT(QI_IObjectWithSite);
994     SET_EXPECT(reportSuccess);
995     parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
996     CHECK_CALLED(CreateInstance);
997     CHECK_CALLED(QI_IObjectWithSite);
998     CHECK_CALLED(reportSuccess);
999
1000     IUnknown_Release(parser);
1001
1002     parser = create_script(FALSE, TRUE);
1003     object_with_site = &ObjectWithSite;
1004
1005     SET_EXPECT(Host_QS_SecMgr);
1006     SET_EXPECT(ProcessUrlAction);
1007     SET_EXPECT(CreateInstance);
1008     SET_EXPECT(QueryCustomPolicy);
1009     SET_EXPECT(QI_IObjectWithSite);
1010     SET_EXPECT(SetSite);
1011     SET_EXPECT(reportSuccess);
1012     parse_script_a(parser, "(new ActiveXObject('Wine.Test')).reportSuccess();");
1013     CHECK_CALLED(Host_QS_SecMgr);
1014     CHECK_CALLED(ProcessUrlAction);
1015     CHECK_CALLED(CreateInstance);
1016     CHECK_CALLED(QueryCustomPolicy);
1017     CHECK_CALLED(QI_IObjectWithSite);
1018     CHECK_CALLED(SetSite);
1019     CHECK_CALLED(reportSuccess);
1020
1021     SetSite_hres = E_FAIL;
1022     SET_EXPECT(ProcessUrlAction);
1023     SET_EXPECT(CreateInstance);
1024     SET_EXPECT(QueryCustomPolicy);
1025     SET_EXPECT(QI_IObjectWithSite);
1026     SET_EXPECT(SetSite);
1027     parse_script_a(parser, "testException(function() { new ActiveXObject('Wine.Test'); }, 'Error', -2146827859);");
1028     CHECK_CALLED(ProcessUrlAction);
1029     CHECK_CALLED(CreateInstance);
1030     CHECK_CALLED(QueryCustomPolicy);
1031     CHECK_CALLED(QI_IObjectWithSite);
1032     CHECK_CALLED(SetSite);
1033
1034     IUnknown_Release(parser);
1035 }
1036
1037 static BOOL init_key(const char *key_name, const char *def_value, BOOL init)
1038 {
1039     HKEY hkey;
1040     DWORD res;
1041
1042     if(!init) {
1043         RegDeleteKey(HKEY_CLASSES_ROOT, key_name);
1044         return TRUE;
1045     }
1046
1047     res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey);
1048     if(res != ERROR_SUCCESS)
1049         return FALSE;
1050
1051     if(def_value)
1052         res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value));
1053
1054     RegCloseKey(hkey);
1055
1056     return res == ERROR_SUCCESS;
1057 }
1058
1059 static BOOL init_registry(BOOL init)
1060 {
1061     return init_key("Wine.Test\\CLSID", TESTOBJ_CLSID, init);
1062 }
1063
1064 static BOOL register_activex(void)
1065 {
1066     DWORD regid;
1067     HRESULT hres;
1068
1069     if(!init_registry(TRUE)) {
1070         init_registry(FALSE);
1071         return FALSE;
1072     }
1073
1074     hres = CoRegisterClassObject(&CLSID_TestObj, (IUnknown *)&activex_cf,
1075                                  CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
1076     ok(hres == S_OK, "Could not register script engine: %08x\n", hres);
1077
1078     return TRUE;
1079 }
1080
1081 static BOOL check_jscript(void)
1082 {
1083     IActiveScriptProperty *script_prop;
1084     IActiveScriptParse *parser;
1085     BSTR str;
1086     HRESULT hres;
1087
1088     parser = create_script(TRUE, TRUE);
1089     if(!parser)
1090         return FALSE;
1091
1092     str = a2bstr("if(!('localeCompare' in String.prototype)) throw 1;");
1093     hres = IActiveScriptParse64_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
1094     SysFreeString(str);
1095
1096     if(hres == S_OK)
1097         hres = IUnknown_QueryInterface(parser, &IID_IActiveScriptProperty, (void**)&script_prop);
1098     IUnknown_Release(parser);
1099     if(hres == S_OK)
1100         IActiveScriptProperty_Release(script_prop);
1101
1102     return hres == S_OK;
1103 }
1104
1105 START_TEST(activex)
1106 {
1107     CoInitialize(NULL);
1108
1109     if(check_jscript()) {
1110         register_activex();
1111
1112         test_ActiveXObject();
1113
1114         init_registry(FALSE);
1115     }else {
1116         win_skip("Broken engine, probably too old\n");
1117     }
1118
1119     CoUninitialize();
1120 }