install.c 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit tests for advpack.dll install functions
 *
 * Copyright (C) 2006 James Hawkins
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25
 */

#include <stdio.h>
#include <windows.h>
#include <advpub.h>
#include "wine/test.h"

26
static HMODULE hAdvPack;
27 28
/* function pointers */
static HRESULT (WINAPI *pRunSetupCommand)(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, HANDLE*, DWORD, LPVOID);
29 30 31 32
static HRESULT (WINAPI *pLaunchINFSection)(HWND, HINSTANCE, LPSTR, INT);
static HRESULT (WINAPI *pLaunchINFSectionEx)(HWND, HINSTANCE, LPSTR, INT);

static char CURR_DIR[MAX_PATH];
33

34
static BOOL init_function_pointers(void)
35
{
36
    hAdvPack = LoadLibraryA("advpack.dll");
37 38 39 40
    if (!hAdvPack)
        return FALSE;

    pRunSetupCommand = (void *)GetProcAddress(hAdvPack, "RunSetupCommand");
41 42
    pLaunchINFSection = (void *)GetProcAddress(hAdvPack, "LaunchINFSection");
    pLaunchINFSectionEx = (void *)GetProcAddress(hAdvPack, "LaunchINFSectionEx");
43

44
    if (!pRunSetupCommand || !pLaunchINFSection || !pLaunchINFSectionEx)
45 46 47 48 49
        return FALSE;

    return TRUE;
}

50 51 52 53 54 55 56 57
static BOOL is_spapi_err(DWORD err)
{
    const DWORD SPAPI_PREFIX = 0x800F0000L;
    const DWORD SPAPI_MASK = 0xFFFF0000L;

    return (((err & SPAPI_MASK) ^ SPAPI_PREFIX) == 0);
}

58
static void create_inf_file(LPCSTR filename)
59 60 61 62 63
{
    DWORD dwNumberOfBytesWritten;
    HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
                           CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

64 65 66 67 68 69
    static const char data[] =
        "[Version]\n"
        "Signature=\"$Chicago$\"\n"
        "AdvancedINF=2.5\n"
        "[DefaultInstall]\n"
        "CheckAdminRights=1\n";
70

71
    WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
72 73 74
    CloseHandle(hf);
}

75
static void test_RunSetupCommand(void)
76 77 78
{
    HRESULT hr;
    HANDLE hexe;
79 80
    char path[MAX_PATH];
    char dir[MAX_PATH];
81 82 83
    char systemdir[MAX_PATH];

    GetSystemDirectoryA(systemdir, sizeof(systemdir));
84 85 86

    /* try an invalid cmd name */
    hr = pRunSetupCommand(NULL, NULL, "Install", "Dir", "Title", NULL, 0, NULL);
87
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
88 89 90

    /* try an invalid directory */
    hr = pRunSetupCommand(NULL, "winver.exe", "Install", NULL, "Title", NULL, 0, NULL);
91
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
92

93
    /* try to run a nonexistent exe */
94
    hexe = (HANDLE)0xdeadbeef;
95
    hr = pRunSetupCommand(NULL, "idontexist.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
96
    ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
97
       "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
98 99 100 101
    ok(hexe == NULL, "Expcted hexe to be NULL\n");
    ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");

    /* try a bad directory */
102
    hexe = (HANDLE)0xdeadbeef;
103 104
    hr = pRunSetupCommand(NULL, "winver.exe", "Install", "non\\existent\\directory", "Title", &hexe, 0, NULL);
    ok(hr == HRESULT_FROM_WIN32(ERROR_DIRECTORY),
105
       "Expected HRESULT_FROM_WIN32(ERROR_DIRECTORY), got %d\n", hr);
106 107 108 109
    ok(hexe == NULL, "Expcted hexe to be NULL\n");
    ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");

    /* try to run an exe with the RSC_FLAG_INF flag */
110
    hexe = (HANDLE)0xdeadbeef;
111
    hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
112 113 114
    ok(is_spapi_err(hr) ||
       hr == E_FAIL, /* win9x */
       "Expected a setupapi error or E_FAIL, got %d\n", hr);
115
    ok(hexe == (HANDLE)0xdeadbeef, "Expected hexe to be 0xdeadbeef\n");
116 117 118
    ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");

    /* run winver.exe */
119
    hexe = (HANDLE)0xdeadbeef;
120
    hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
121
    ok(hr == S_ASYNCHRONOUS, "Expected S_ASYNCHRONOUS, got %d\n", hr);
122 123
    ok(hexe != NULL, "Expected hexe to be non-NULL\n");
    ok(TerminateProcess(hexe, 0), "Expected TerminateProcess to succeed\n");
124 125 126 127 128 129 130 131 132

    CreateDirectoryA("one", NULL);
    create_inf_file("one\\test.inf");

    /* try a full path to the INF, with working dir provided */
    lstrcpy(path, CURR_DIR);
    lstrcat(path, "\\one\\test.inf");
    lstrcpy(dir, CURR_DIR);
    lstrcat(dir, "\\one");
133
    hr = pRunSetupCommand(NULL, path, "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
134
    ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
135 136

    /* try a full path to the INF, NULL working dir */
137
    hr = pRunSetupCommand(NULL, path, "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
138
    ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
139
       "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
140 141

    /* try a full path to the INF, empty working dir */
142
    hr = pRunSetupCommand(NULL, path, "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
143
    ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
144 145

    /* try a relative path to the INF, with working dir provided */
146
    hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
147 148 149
    ok(hr == ERROR_SUCCESS ||
       hr == E_FAIL, /* win9x */
       "Expected ERROR_SUCCESS, got %d\n", hr);
150 151

    /* try a relative path to the INF, NULL working dir */
152
    hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
153
    ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
154
       "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
155 156

    /* try a relative path to the INF, empty working dir */
157
    hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
158 159 160
    ok(hr == ERROR_SUCCESS ||
       hr == E_FAIL, /* win9x */
       "Expected ERROR_SUCCESS or E_FAIL, got %d\n", hr);
161 162

    /* try only the INF filename, with working dir provided */
163
    hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
164 165 166
    ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
       hr == E_FAIL, /* win9x */
       "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
167 168

    /* try only the INF filename, NULL working dir */
169
    hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
170
    ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
171
       "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
172 173

    /* try only the INF filename, empty working dir */
174
    hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
175 176 177
    ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
       hr == E_FAIL, /* win9x */
       "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
178 179 180 181 182 183 184

    DeleteFileA("one\\test.inf");
    RemoveDirectoryA("one");

    create_inf_file("test.inf");

    /* try INF file in the current directory, working directory provided */
185
    hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
186 187 188
    ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
       hr == E_FAIL, /* win9x */
       "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
189 190

    /* try INF file in the current directory, NULL working directory */
191
    hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
192
    ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
193
       "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
194 195

    /* try INF file in the current directory, empty working directory */
196
    hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
197 198 199
    ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
       hr == E_FAIL, /* win9x */
       "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
200 201
}

202
static void test_LaunchINFSection(void)
203 204 205
{
    HRESULT hr;
    char cmdline[MAX_PATH];
206
    static char file[] = "test.inf,DefaultInstall,4,0";
207

208 209 210 211 212 213 214 215 216
    /* The 'No UI' flag seems to have no effect whatsoever on Windows.
     * So only do this test in interactive mode.
     */
    if (winetest_interactive)
    {
        /* try an invalid cmdline */
        hr = pLaunchINFSection(NULL, NULL, NULL, 0);
        ok(hr == 1, "Expected 1, got %d\n", hr);
    }
217 218 219 220 221 222 223 224 225

    CreateDirectoryA("one", NULL);
    create_inf_file("one\\test.inf");

    /* try a full path to the INF */
    lstrcpy(cmdline, CURR_DIR);
    lstrcat(cmdline, "\\");
    lstrcat(cmdline, "one\\test.inf,DefaultInstall,,4");
    hr = pLaunchINFSection(NULL, NULL, cmdline, 0);
226
    ok(hr == 0, "Expected 0, got %d\n", hr);
227 228 229 230 231 232 233

    DeleteFileA("one\\test.inf");
    RemoveDirectoryA("one");

    create_inf_file("test.inf");

    /* try just the INF filename */
234
    hr = pLaunchINFSection(NULL, NULL, file, 0);
235
    ok(hr == 0, "Expected 0, got %d\n", hr);
236 237

    DeleteFileA("test.inf");
238 239
}

240
static void test_LaunchINFSectionEx(void)
241 242 243 244 245 246
{
    HRESULT hr;
    char cmdline[MAX_PATH];

    create_inf_file("test.inf");

Paul Vriens's avatar
Paul Vriens committed
247
    /* try an invalid CAB filename with an absolute INF name */
248
    lstrcpy(cmdline, CURR_DIR);
Paul Vriens's avatar
Paul Vriens committed
249 250
    lstrcat(cmdline, "\\");
    lstrcat(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
251 252 253
    hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
    ok(hr == 0, "Expected 0, got %d\n", hr);

254 255 256 257 258 259 260 261 262 263
    /* The 'No UI' flag seems to have no effect whatsoever on Windows.
     * So only do this test in interactive mode.
     */
    if (winetest_interactive)
    {
        /* try an invalid CAB filename with a relative INF name */
        lstrcpy(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
        hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
        ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
    }
Paul Vriens's avatar
Paul Vriens committed
264

265 266 267
    DeleteFileA("test.inf");
}

268 269
START_TEST(install)
{
270 271 272
    DWORD len;
    char temp_path[MAX_PATH], prev_path[MAX_PATH];

273 274 275
    if (!init_function_pointers())
        return;

276 277 278 279 280 281 282 283 284
    GetCurrentDirectoryA(MAX_PATH, prev_path);
    GetTempPath(MAX_PATH, temp_path);
    SetCurrentDirectoryA(temp_path);

    lstrcpyA(CURR_DIR, temp_path);
    len = lstrlenA(CURR_DIR);

    if(len && (CURR_DIR[len - 1] == '\\'))
        CURR_DIR[len - 1] = 0;
285

286
    test_RunSetupCommand();
287
    test_LaunchINFSection();
288
    test_LaunchINFSectionEx();
289 290

    FreeLibrary(hAdvPack);
291
    SetCurrentDirectoryA(prev_path);
292
}