jscript: Fix parse_double_literal implementation.
[wine] / dlls / jscript / tests / jscript.c
1 /*
2  * Copyright 2008 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 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <initguid.h>
23 #include <ole2.h>
24 #include <activscp.h>
25 #include <objsafe.h>
26 #include <dispex.h>
27
28 #include "wine/test.h"
29
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31
32 static const CLSID CLSID_JScript =
33     {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
34
35 #define DEFINE_EXPECT(func) \
36     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
37
38 #define SET_EXPECT(func) \
39     expect_ ## func = TRUE
40
41 #define SET_CALLED(func) \
42     called_ ## func = TRUE
43
44 #define CHECK_EXPECT2(func) \
45     do { \
46         ok(expect_ ##func, "unexpected call " #func "\n"); \
47         called_ ## func = TRUE; \
48     }while(0)
49
50 #define CHECK_EXPECT(func) \
51     do { \
52         CHECK_EXPECT2(func); \
53         expect_ ## func = FALSE; \
54     }while(0)
55
56 #define CHECK_CALLED(func) \
57     do { \
58         ok(called_ ## func, "expected " #func "\n"); \
59         expect_ ## func = called_ ## func = FALSE; \
60     }while(0)
61
62 DEFINE_EXPECT(GetLCID);
63 DEFINE_EXPECT(OnStateChange_STARTED);
64 DEFINE_EXPECT(OnStateChange_CONNECTED);
65 DEFINE_EXPECT(OnStateChange_DISCONNECTED);
66 DEFINE_EXPECT(OnStateChange_CLOSED);
67 DEFINE_EXPECT(OnStateChange_INITIALIZED);
68 DEFINE_EXPECT(OnEnterScript);
69 DEFINE_EXPECT(OnLeaveScript);
70
71 #define test_state(s,ss) _test_state(__LINE__,s,ss)
72 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
73 {
74     SCRIPTSTATE state = -1;
75     HRESULT hres;
76
77     hres = IActiveScript_GetScriptState(script, &state);
78     ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
79     ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
80 }
81
82 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
83 {
84     *ppv = NULL;
85
86     if(IsEqualGUID(&IID_IUnknown, riid))
87         *ppv = iface;
88     else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
89         *ppv = iface;
90     else
91         return E_NOINTERFACE;
92
93     IUnknown_AddRef((IUnknown*)*ppv);
94     return S_OK;
95 }
96
97 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
98 {
99     return 2;
100 }
101
102 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
103 {
104     return 1;
105 }
106
107 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
108 {
109     CHECK_EXPECT(GetLCID);
110     return E_NOTIMPL;
111 }
112
113 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
114         DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
115 {
116     ok(0, "unexpected call\n");
117     return E_NOTIMPL;
118 }
119
120 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
121 {
122     ok(0, "unexpected call\n");
123     return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
127         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
128 {
129     ok(0, "unexpected call\n");
130     return E_NOTIMPL;
131 }
132
133 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
134 {
135     switch(ssScriptState) {
136     case SCRIPTSTATE_STARTED:
137         CHECK_EXPECT(OnStateChange_STARTED);
138         return S_OK;
139     case SCRIPTSTATE_CONNECTED:
140         CHECK_EXPECT(OnStateChange_CONNECTED);
141         return S_OK;
142     case SCRIPTSTATE_DISCONNECTED:
143         CHECK_EXPECT(OnStateChange_DISCONNECTED);
144         return S_OK;
145     case SCRIPTSTATE_CLOSED:
146         CHECK_EXPECT(OnStateChange_CLOSED);
147         return S_OK;
148     case SCRIPTSTATE_INITIALIZED:
149         CHECK_EXPECT(OnStateChange_INITIALIZED);
150         return S_OK;
151     default:
152         ok(0, "unexpected call %d\n", ssScriptState);
153     }
154
155     return E_NOTIMPL;
156 }
157
158 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
159 {
160     ok(0, "unexpected call\n");
161     return E_NOTIMPL;
162 }
163
164 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
165 {
166     CHECK_EXPECT(OnEnterScript);
167     return S_OK;
168 }
169
170 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
171 {
172     CHECK_EXPECT(OnLeaveScript);
173     return S_OK;
174 }
175
176 #undef ACTSCPSITE_THIS
177
178 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
179     ActiveScriptSite_QueryInterface,
180     ActiveScriptSite_AddRef,
181     ActiveScriptSite_Release,
182     ActiveScriptSite_GetLCID,
183     ActiveScriptSite_GetItemInfo,
184     ActiveScriptSite_GetDocVersionString,
185     ActiveScriptSite_OnScriptTerminate,
186     ActiveScriptSite_OnStateChange,
187     ActiveScriptSite_OnScriptError,
188     ActiveScriptSite_OnEnterScript,
189     ActiveScriptSite_OnLeaveScript
190 };
191
192 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
193
194 static void test_script_dispatch(IActiveScript *script, BOOL initialized)
195 {
196     IDispatchEx *dispex;
197     IDispatch *disp;
198     HRESULT hres;
199
200     disp = (void*)0xdeadbeef;
201     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
202     if(!initialized) {
203         ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
204         ok(!disp, "disp != NULL\n");
205         return;
206     }
207
208     ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
209     if(FAILED(hres))
210         return;
211
212     ok(disp != NULL, "disp == NULL\n");
213     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
214     IDispatch_Release(disp);
215     ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
216
217     IDispatchEx_Release(dispex);
218 }
219
220 static void test_safety(IUnknown *unk)
221 {
222     IObjectSafety *safety;
223     DWORD supported, enabled;
224     HRESULT hres;
225
226     hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
227     ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
228     if(FAILED(hres))
229         return;
230
231     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
232     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
233     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
234     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
235
236     supported = enabled = 0xdeadbeef;
237     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
238     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
239     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
240        "supported=%x\n", supported);
241     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
242
243     supported = enabled = 0xdeadbeef;
244     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
245     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
246     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
247        "supported=%x\n", supported);
248     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
249
250     supported = enabled = 0xdeadbeef;
251     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
252     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
253     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
254        "supported=%x\n", supported);
255     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
256
257     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
258             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
259                 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
260             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
261     ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
262
263     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
264             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
265             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
266     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
267
268     supported = enabled = 0xdeadbeef;
269     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
270     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
271     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
272        "supported=%x\n", supported);
273     ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
274        "enabled=%x\n", enabled);
275
276     IObjectSafety_Release(safety);
277 }
278
279 static void test_jscript(void)
280 {
281     IActiveScriptParse *parse;
282     IActiveScript *script;
283     IUnknown *unk;
284     ULONG ref;
285     HRESULT hres;
286
287     hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
288             &IID_IUnknown, (void**)&unk);
289     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
290     if(FAILED(hres))
291         return;
292
293     hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
294     ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
295
296     hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
297     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
298     if (FAILED(hres))
299     {
300         IActiveScript_Release(script);
301         return;
302     }
303
304     test_state(script, SCRIPTSTATE_UNINITIALIZED);
305     test_safety(unk);
306
307     hres = IActiveScriptParse64_InitNew(parse);
308     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
309
310     hres = IActiveScriptParse64_InitNew(parse);
311     ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
312
313     hres = IActiveScript_SetScriptSite(script, NULL);
314     ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
315
316     test_state(script, SCRIPTSTATE_UNINITIALIZED);
317     test_script_dispatch(script, FALSE);
318
319     SET_EXPECT(GetLCID);
320     SET_EXPECT(OnStateChange_INITIALIZED);
321     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
322     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
323     CHECK_CALLED(GetLCID);
324     CHECK_CALLED(OnStateChange_INITIALIZED);
325
326     test_state(script, SCRIPTSTATE_INITIALIZED);
327
328     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
329     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
330
331     test_script_dispatch(script, TRUE);
332
333     SET_EXPECT(OnStateChange_STARTED);
334     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
335     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
336     CHECK_CALLED(OnStateChange_STARTED);
337
338     test_state(script, SCRIPTSTATE_STARTED);
339
340     SET_EXPECT(OnStateChange_CLOSED);
341     hres = IActiveScript_Close(script);
342     ok(hres == S_OK, "Close failed: %08x\n", hres);
343     CHECK_CALLED(OnStateChange_CLOSED);
344
345     test_state(script, SCRIPTSTATE_CLOSED);
346     test_script_dispatch(script, FALSE);
347
348     IUnknown_Release(parse);
349     IActiveScript_Release(script);
350
351     ref = IUnknown_Release(unk);
352     ok(!ref, "ref = %d\n", ref);
353 }
354
355 static void test_jscript2(void)
356 {
357     IActiveScriptParse *parse;
358     IActiveScript *script;
359     IUnknown *unk;
360     ULONG ref;
361     HRESULT hres;
362
363     hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
364             &IID_IUnknown, (void**)&unk);
365     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
366     if(FAILED(hres))
367         return;
368
369     hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
370     ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
371
372     hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
373     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
374     if (FAILED(hres))
375     {
376         IActiveScript_Release(script);
377         return;
378     }
379
380     test_state(script, SCRIPTSTATE_UNINITIALIZED);
381
382     SET_EXPECT(GetLCID);
383     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
384     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
385     CHECK_CALLED(GetLCID);
386
387     test_state(script, SCRIPTSTATE_UNINITIALIZED);
388
389     SET_EXPECT(OnStateChange_INITIALIZED);
390     hres = IActiveScriptParse64_InitNew(parse);
391     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
392     CHECK_CALLED(OnStateChange_INITIALIZED);
393
394     hres = IActiveScriptParse64_InitNew(parse);
395     ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
396
397     SET_EXPECT(OnStateChange_CONNECTED);
398     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
399     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
400     CHECK_CALLED(OnStateChange_CONNECTED);
401
402     test_state(script, SCRIPTSTATE_CONNECTED);
403
404     SET_EXPECT(OnStateChange_DISCONNECTED);
405     SET_EXPECT(OnStateChange_INITIALIZED);
406     SET_EXPECT(OnStateChange_CLOSED);
407     hres = IActiveScript_Close(script);
408     ok(hres == S_OK, "Close failed: %08x\n", hres);
409     CHECK_CALLED(OnStateChange_DISCONNECTED);
410     CHECK_CALLED(OnStateChange_INITIALIZED);
411     CHECK_CALLED(OnStateChange_CLOSED);
412
413     test_state(script, SCRIPTSTATE_CLOSED);
414     test_script_dispatch(script, FALSE);
415
416     IUnknown_Release(parse);
417     IActiveScript_Release(script);
418
419     ref = IUnknown_Release(unk);
420     ok(!ref, "ref = %d\n", ref);
421 }
422
423 START_TEST(jscript)
424 {
425     CoInitialize(NULL);
426
427     test_jscript();
428     test_jscript2();
429
430     CoUninitialize();
431 }