run.c 14 KB
Newer Older
Jacek Caban's avatar
Jacek Caban committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2011 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdio.h>

#define COBJMACROS
#define CONST_VTABLE

24
#define PSAPI_VERSION 1
Jacek Caban's avatar
Jacek Caban committed
25 26
#include <initguid.h>
#include <windows.h>
27
#include <psapi.h>
Jacek Caban's avatar
Jacek Caban committed
28 29 30 31 32 33 34 35 36 37
#include <oaidl.h>

#include "wine/test.h"

#define DEFINE_EXPECT(func) \
    static BOOL expect_ ## func = FALSE, called_ ## func = FALSE

#define SET_EXPECT(func) \
    expect_ ## func = TRUE

38 39
#define CLEAR_CALLED(func) \
    expect_ ## func = called_ ## func = FALSE
Jacek Caban's avatar
Jacek Caban committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#define CHECK_EXPECT2(func) \
    do { \
        ok(expect_ ##func, "unexpected call " #func "\n"); \
        called_ ## func = TRUE; \
    }while(0)

#define CHECK_EXPECT(func) \
    do { \
        CHECK_EXPECT2(func); \
        expect_ ## func = FALSE; \
    }while(0)

#define CHECK_CALLED(func) \
    do { \
        ok(called_ ## func, "expected " #func "\n"); \
        expect_ ## func = called_ ## func = FALSE; \
    }while(0)

DEFINE_EXPECT(reportSuccess);

61 62 63 64
#define DISPID_TESTOBJ_OK                        10000
#define DISPID_TESTOBJ_TRACE                     10001
#define DISPID_TESTOBJ_REPORTSUCCESS             10002
#define DISPID_TESTOBJ_WSCRIPTFULLNAME           10003
65
#define DISPID_TESTOBJ_WSCRIPTPATH               10004
66
#define DISPID_TESTOBJ_WSCRIPTSCRIPTNAME         10005
67
#define DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME     10006
Jacek Caban's avatar
Jacek Caban committed
68 69 70 71 72 73 74

#define TESTOBJ_CLSID "{178fc166-f585-4e24-9c13-4bb7faf80646}"

static const GUID CLSID_TestObj =
    {0x178fc166,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}};

static const char *script_name;
75
static HANDLE wscript_process;
Jacek Caban's avatar
Jacek Caban committed
76

77 78 79 80 81 82 83 84 85 86 87 88
static BSTR a2bstr(const char *str)
{
    BSTR ret;
    int len;

    len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
    ret = SysAllocStringLen(NULL, len-1);
    MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);

    return ret;
}

Jacek Caban's avatar
Jacek Caban committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static HRESULT WINAPI Dispatch_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
{
    if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IDispatch)) {
        *ppv = iface;
	return S_OK;
    }

    *ppv = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI Dispatch_AddRef(IDispatch *iface)
{
    return 2;
}

static ULONG WINAPI Dispatch_Release(IDispatch *iface)
{
    return 1;
}

static HRESULT WINAPI Dispatch_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
{
    ok(0, "unexpected call\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI Dispatch_GetTypeInfo(IDispatch *iface, UINT iTInfo,
	LCID lcid, ITypeInfo **ppTInfo)
{
    return E_NOTIMPL;
}

static HRESULT WINAPI Dispatch_GetIDsOfNames(IDispatch *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    unsigned i;

    for(i=0; i<cNames; i++) {
128
        if(!lstrcmpW(rgszNames[i], L"ok")) {
Jacek Caban's avatar
Jacek Caban committed
129
            rgDispId[i] = DISPID_TESTOBJ_OK;
130
        }else if(!lstrcmpW(rgszNames[i], L"trace")) {
Jacek Caban's avatar
Jacek Caban committed
131
            rgDispId[i] = DISPID_TESTOBJ_TRACE;
132
        }else if(!lstrcmpW(rgszNames[i], L"reportSuccess")) {
Jacek Caban's avatar
Jacek Caban committed
133
            rgDispId[i] = DISPID_TESTOBJ_REPORTSUCCESS;
134
        }else if(!lstrcmpW(rgszNames[i], L"wscriptFullName")) {
135
            rgDispId[i] = DISPID_TESTOBJ_WSCRIPTFULLNAME;
136
        }else if(!lstrcmpW(rgszNames[i], L"wscriptPath")) {
137
            rgDispId[i] = DISPID_TESTOBJ_WSCRIPTPATH;
138
        }else if(!lstrcmpW(rgszNames[i], L"wscriptScriptName")) {
139
            rgDispId[i] = DISPID_TESTOBJ_WSCRIPTSCRIPTNAME;
140
        }else if(!lstrcmpW(rgszNames[i], L"wscriptScriptFullName")) {
141
            rgDispId[i] = DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME;
Jacek Caban's avatar
Jacek Caban committed
142 143 144 145 146 147 148 149 150 151 152 153 154
        }else {
            ok(0, "unexpected name %s\n", wine_dbgstr_w(rgszNames[i]));
            return DISP_E_UNKNOWNNAME;
        }
    }

    return S_OK;
}

static HRESULT WINAPI Dispatch_Invoke(IDispatch *iface, DISPID dispIdMember, REFIID riid, LCID lcid,
				      WORD wFlags, DISPPARAMS *pdp, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    switch(dispIdMember) {
155 156 157
    case DISPID_TESTOBJ_OK: {
        VARIANT *expr, *msg;

Jacek Caban's avatar
Jacek Caban committed
158 159 160
        ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
        ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs);
        ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
161 162 163 164 165 166 167 168 169 170 171 172

        expr = pdp->rgvarg+1;
        if(V_VT(expr) == (VT_VARIANT|VT_BYREF))
            expr = V_VARIANTREF(expr);

        msg = pdp->rgvarg;
        if(V_VT(msg) == (VT_VARIANT|VT_BYREF))
            msg = V_VARIANTREF(msg);

        ok(V_VT(msg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(msg));
        ok(V_VT(expr) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(expr));
        ok(V_BOOL(expr), "%s: %s\n", script_name, wine_dbgstr_w(V_BSTR(msg)));
Jacek Caban's avatar
Jacek Caban committed
173 174 175
        if(pVarResult)
            V_VT(pVarResult) = VT_EMPTY;
        break;
176
    }
Jacek Caban's avatar
Jacek Caban committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    case DISPID_TESTOBJ_TRACE:
        ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
        ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
        ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
        ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
        trace("%s: %s\n", script_name, wine_dbgstr_w(V_BSTR(pdp->rgvarg)));
        if(pVarResult)
            V_VT(pVarResult) = VT_EMPTY;
        break;
    case DISPID_TESTOBJ_REPORTSUCCESS:
        CHECK_EXPECT(reportSuccess);

        ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
        ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
        ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
        if(pVarResult)
            V_VT(pVarResult) = VT_EMPTY;
        break;
195 196 197 198 199 200 201 202 203
    case DISPID_TESTOBJ_WSCRIPTFULLNAME:
    {
        WCHAR fullName[MAX_PATH];
        DWORD res;

        ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
        ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
        ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
        V_VT(pVarResult) = VT_BSTR;
204
        res = GetModuleFileNameExW(wscript_process, NULL, fullName, ARRAY_SIZE(fullName));
205 206 207 208 209 210
        if(res == 0)
            return E_FAIL;
        if(!(V_BSTR(pVarResult) = SysAllocString(fullName)))
            return E_OUTOFMEMORY;
        break;
    }
211 212 213 214 215 216 217 218 219 220
    case DISPID_TESTOBJ_WSCRIPTPATH:
    {
        WCHAR fullPath[MAX_PATH];
        DWORD res;
        const WCHAR *pos;

        ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
        ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
        ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
        V_VT(pVarResult) = VT_BSTR;
221
        res = GetModuleFileNameExW(wscript_process, NULL, fullPath, ARRAY_SIZE(fullPath));
222 223
        if(res == 0)
            return E_FAIL;
224
        pos = wcsrchr(fullPath, '\\');
225 226 227 228
        if(!(V_BSTR(pVarResult) = SysAllocStringLen(fullPath, pos-fullPath)))
            return E_OUTOFMEMORY;
        break;
    }
229 230 231 232 233 234 235 236 237 238
    case DISPID_TESTOBJ_WSCRIPTSCRIPTNAME:
    {
        char fullPath[MAX_PATH];
        char *pos;
        long res;

        ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
        ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
        ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
        V_VT(pVarResult) = VT_BSTR;
239 240
        res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, &pos);
        if(!res || res > sizeof(fullPath))
241
            return E_FAIL;
242
        if(!(V_BSTR(pVarResult) = a2bstr(pos)))
243 244 245
            return E_OUTOFMEMORY;
        break;
    }
246 247 248 249 250 251 252 253 254
    case DISPID_TESTOBJ_WSCRIPTSCRIPTFULLNAME:
    {
        char fullPath[MAX_PATH];
        long res;

        ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
        ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
        ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
        V_VT(pVarResult) = VT_BSTR;
255 256
        res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, NULL);
        if(!res || res > sizeof(fullPath))
257
            return E_FAIL;
258
        if(!(V_BSTR(pVarResult) = a2bstr(fullPath)))
259 260 261
            return E_OUTOFMEMORY;
        break;
    }
Jacek Caban's avatar
Jacek Caban committed
262
    default:
263
        ok(0, "unexpected dispIdMember %ld\n", dispIdMember);
Jacek Caban's avatar
Jacek Caban committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        return E_NOTIMPL;
    }

    return S_OK;
}

static IDispatchVtbl testobj_vtbl = {
    Dispatch_QueryInterface,
    Dispatch_AddRef,
    Dispatch_Release,
    Dispatch_GetTypeInfoCount,
    Dispatch_GetTypeInfo,
    Dispatch_GetIDsOfNames,
    Dispatch_Invoke
};

static IDispatch testobj = { &testobj_vtbl };

static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
{
    if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
        *ppv = iface;
        return S_OK;
    }

    *ppv = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
{
    return 2;
}

static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
{
    return 1;
}

static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
{
    ok(!outer, "outer = %p\n", outer);
    return IDispatch_QueryInterface(&testobj, riid, ppv);
}

static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
{
    return S_OK;
}

static const IClassFactoryVtbl ClassFactoryVtbl = {
    ClassFactory_QueryInterface,
    ClassFactory_AddRef,
    ClassFactory_Release,
    ClassFactory_CreateInstance,
    ClassFactory_LockServer
};

static IClassFactory testobj_cf = { &ClassFactoryVtbl };

324
static void run_script_file(const char *file_name, DWORD expected_exit_code)
Jacek Caban's avatar
Jacek Caban committed
325 326 327 328
{
    char command[MAX_PATH];
    STARTUPINFOA si = {sizeof(si)};
    PROCESS_INFORMATION pi;
329
    DWORD exit_code;
Jacek Caban's avatar
Jacek Caban committed
330 331 332
    BOOL bres;

    script_name = file_name;
333
    sprintf(command, "wscript.exe %s arg1 2 ar3", file_name);
Jacek Caban's avatar
Jacek Caban committed
334 335 336 337 338 339

    SET_EXPECT(reportSuccess);

    bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    if(!bres) {
        win_skip("script.exe is not available\n");
340
        CLEAR_CALLED(reportSuccess);
Jacek Caban's avatar
Jacek Caban committed
341 342 343
        return;
    }

344
    wscript_process = pi.hProcess;
Jacek Caban's avatar
Jacek Caban committed
345
    WaitForSingleObject(pi.hProcess, INFINITE);
346 347

    bres = GetExitCodeProcess(pi.hProcess, &exit_code);
348 349
    ok(bres, "GetExitCodeProcess failed: %lu\n", GetLastError());
    ok(exit_code == expected_exit_code, "exit_code = %lu, expected %lu\n", exit_code, expected_exit_code);
350

Jacek Caban's avatar
Jacek Caban committed
351 352 353 354 355 356
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

    CHECK_CALLED(reportSuccess);
}

357
static void run_script(const char *name, const char *script_data, size_t script_size, DWORD expected_exit_code)
Jacek Caban's avatar
Jacek Caban committed
358 359
{
    char file_name[MAX_PATH];
360
    const char *ext;
Jacek Caban's avatar
Jacek Caban committed
361
    HANDLE file;
362
    DWORD size;
Jacek Caban's avatar
Jacek Caban committed
363 364 365
    BOOL res;

    ext = strrchr(name, '.');
366
    ok(ext != NULL, "no script extension\n");
Jacek Caban's avatar
Jacek Caban committed
367
    if(!ext)
368
      return;
Jacek Caban's avatar
Jacek Caban committed
369 370 371 372 373

    sprintf(file_name, "test%s", ext);

    file = CreateFileA(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL, NULL);
374
    ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %lu\n", GetLastError());
Jacek Caban's avatar
Jacek Caban committed
375
    if(file == INVALID_HANDLE_VALUE)
376
        return;
Jacek Caban's avatar
Jacek Caban committed
377 378 379

    res = WriteFile(file, script_data, script_size, &size, NULL);
    CloseHandle(file);
380
    ok(res, "Could not write to file: %lu\n", GetLastError());
Jacek Caban's avatar
Jacek Caban committed
381
    if(!res)
382
        return;
Jacek Caban's avatar
Jacek Caban committed
383

384
    run_script_file(file_name, expected_exit_code);
Jacek Caban's avatar
Jacek Caban committed
385 386

    DeleteFileA(file_name);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
}

static void run_simple_script(const char *script, DWORD expected_exit_code)
{
    run_script("simple.js", script, strlen(script), expected_exit_code);
}

static BOOL WINAPI test_enum_proc(HMODULE module, LPCSTR type, LPSTR name, LONG_PTR param)
{
    const char *script_data;
    DWORD script_size;
    HRSRC src;

    trace("running %s test...\n", name);

    src = FindResourceA(NULL, name, type);
403
    ok(src != NULL, "Could not find resource %s: %lu\n", name, GetLastError());
404 405 406 407 408 409 410 411 412
    if(!src)
        return TRUE;

    script_data = LoadResource(NULL, src);
    script_size = SizeofResource(NULL, src);
    while(script_size && !script_data[script_size-1])
        script_size--;

    run_script(name, script_data, script_size, 0);
Jacek Caban's avatar
Jacek Caban committed
413 414 415 416 417 418 419 420 421
    return TRUE;
}

static BOOL init_key(const char *key_name, const char *def_value, BOOL init)
{
    HKEY hkey;
    DWORD res;

    if(!init) {
422
        RegDeleteKeyA(HKEY_CLASSES_ROOT, key_name);
Jacek Caban's avatar
Jacek Caban committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        return TRUE;
    }

    res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey);
    if(res != ERROR_SUCCESS)
        return FALSE;

    if(def_value)
        res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value));

    RegCloseKey(hkey);
    return res == ERROR_SUCCESS;
}

static BOOL init_registry(BOOL init)
{
    return init_key("Wine.Test\\CLSID", TESTOBJ_CLSID, init);
}

static BOOL register_activex(void)
{
    DWORD regid;
    HRESULT hres;

    if(!init_registry(TRUE)) {
        init_registry(FALSE);
        return FALSE;
    }

    hres = CoRegisterClassObject(&CLSID_TestObj, (IUnknown *)&testobj_cf,
            CLSCTX_SERVER, REGCLS_MULTIPLEUSE, &regid);
454
    ok(hres == S_OK, "Could not register script engine: %08lx\n", hres);
Jacek Caban's avatar
Jacek Caban committed
455 456 457 458 459 460 461 462 463
    return TRUE;
}

START_TEST(run)
{
    char **argv;
    int argc;

    CoInitializeEx(NULL, COINIT_MULTITHREADED);
464 465 466 467 468 469

    if(!register_activex()) {
        skip("Could not register ActiveX object.\n");
        CoUninitialize();
        return;
    }
Jacek Caban's avatar
Jacek Caban committed
470 471

    argc = winetest_get_mainargs(&argv);
472 473 474
    if(argc > 2) {
        run_script_file(argv[2], 0);
    }else {
Jacek Caban's avatar
Jacek Caban committed
475 476
        EnumResourceNamesA(NULL, "TESTSCRIPT", test_enum_proc, 0);

477 478 479 480 481 482
        run_simple_script("var winetest = new ActiveXObject('Wine.Test');\n"
                           "winetest.reportSuccess();\n"
                           "WScript.Quit(3);\n"
                           "winetest.ok(false, 'not quit?');\n", 3);
}

Jacek Caban's avatar
Jacek Caban committed
483 484 485
    init_registry(FALSE);
    CoUninitialize();
}