#include <stdio.h>
#include <windows.h>
#include <advpub.h>
#include "wine/test.h"
static HMODULE hAdvPack;
static HRESULT (WINAPI *pRunSetupCommand)(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, HANDLE*, DWORD, LPVOID);
static HRESULT (WINAPI *pLaunchINFSection)(HWND, HINSTANCE, LPSTR, INT);
static HRESULT (WINAPI *pLaunchINFSectionEx)(HWND, HINSTANCE, LPSTR, INT);
static char CURR_DIR[MAX_PATH];
static BOOL init_function_pointers(void)
{
hAdvPack = LoadLibraryA("advpack.dll");
if (!hAdvPack)
return FALSE;
pRunSetupCommand = (void *)GetProcAddress(hAdvPack, "RunSetupCommand");
pLaunchINFSection = (void *)GetProcAddress(hAdvPack, "LaunchINFSection");
pLaunchINFSectionEx = (void *)GetProcAddress(hAdvPack, "LaunchINFSectionEx");
if (!pRunSetupCommand || !pLaunchINFSection || !pLaunchINFSectionEx)
return FALSE;
return TRUE;
}
static BOOL is_spapi_err(DWORD err)
{
const DWORD SPAPI_PREFIX = 0x800F0000L;
const DWORD SPAPI_MASK = 0xFFFF0000L;
return (((err & SPAPI_MASK) ^ SPAPI_PREFIX) == 0);
}
static void create_inf_file(LPCSTR filename)
{
DWORD dwNumberOfBytesWritten;
HANDLE hf = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
static const char data[] =
"[Version]\n"
"Signature=\"$Chicago$\"\n"
"AdvancedINF=2.5\n"
"[DefaultInstall]\n"
"CheckAdminRights=1\n";
WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
CloseHandle(hf);
}
static void test_RunSetupCommand(void)
{
HRESULT hr;
HANDLE hexe;
char path[MAX_PATH];
char dir[MAX_PATH];
char systemdir[MAX_PATH];
GetSystemDirectoryA(systemdir, sizeof(systemdir));
hr = pRunSetupCommand(NULL, NULL, "Install", "Dir", "Title", NULL, 0, NULL);
ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
hr = pRunSetupCommand(NULL, "winver.exe", "Install", NULL, "Title", NULL, 0, NULL);
ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
hexe = (HANDLE)0xdeadbeef;
hr = pRunSetupCommand(NULL, "idontexist.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
"Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %ld\n", hr);
ok(hexe == NULL, "Expected hexe to be NULL\n");
ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
hexe = (HANDLE)0xdeadbeef;
hr = pRunSetupCommand(NULL, "winver.exe", "Install", "non\\existent\\directory", "Title", &hexe, 0, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_DIRECTORY),
"Expected HRESULT_FROM_WIN32(ERROR_DIRECTORY), got %ld\n", hr);
ok(hexe == NULL, "Expected hexe to be NULL\n");
ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
hexe = (HANDLE)0xdeadbeef;
hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(is_spapi_err(hr), "Expected a setupapi error, got %ld\n", hr);
ok(hexe == (HANDLE)0xdeadbeef, "Expected hexe to be 0xdeadbeef\n");
ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
hexe = (HANDLE)0xdeadbeef;
hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
ok(hr == S_ASYNCHRONOUS, "Expected S_ASYNCHRONOUS, got %ld\n", hr);
ok(hexe != NULL, "Expected hexe to be non-NULL\n");
ok(TerminateProcess(hexe, 0), "Expected TerminateProcess to succeed\n");
CreateDirectoryA("one", NULL);
create_inf_file("one\\test.inf");
lstrcpyA(path, CURR_DIR);
lstrcatA(path, "\\one\\test.inf");
lstrcpyA(dir, CURR_DIR);
lstrcatA(dir, "\\one");
hr = pRunSetupCommand(NULL, path, "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", hr);
hr = pRunSetupCommand(NULL, path, "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %ld\n", hr);
hr = pRunSetupCommand(NULL, path, "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", hr);
hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", hr);
hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %ld\n", hr);
hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", hr);
hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %ld\n", hr);
hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %ld\n", hr);
hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %ld\n", hr);
DeleteFileA("one\\test.inf");
RemoveDirectoryA("one");
create_inf_file("test.inf");
hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %ld\n", hr);
hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %ld\n", hr);
hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %ld\n", hr);
}
static void test_LaunchINFSection(void)
{
HRESULT hr;
char cmdline[MAX_PATH];
static char file[] = "test.inf,DefaultInstall,4,0";
static char file2[] = "test.inf,,1,0";
if (winetest_interactive)
{
hr = pLaunchINFSection(NULL, NULL, NULL, 0);
ok(hr == 1, "Expected 1, got %ld\n", hr);
}
CreateDirectoryA("one", NULL);
create_inf_file("one\\test.inf");
lstrcpyA(cmdline, CURR_DIR);
lstrcatA(cmdline, "\\");
lstrcatA(cmdline, "one\\test.inf,DefaultInstall,,4");
hr = pLaunchINFSection(NULL, NULL, cmdline, 0);
ok(hr == 0, "Expected 0, got %ld\n", hr);
DeleteFileA("one\\test.inf");
RemoveDirectoryA("one");
create_inf_file("test.inf");
hr = pLaunchINFSection(NULL, NULL, file, 0);
ok(hr == 0, "Expected 0, got %ld\n", hr);
hr = pLaunchINFSection(NULL, NULL, file2, 0);
ok(hr == 0, "Expected 0, got %ld\n", hr);
DeleteFileA("test.inf");
}
static void test_LaunchINFSectionEx(void)
{
HRESULT hr;
char cmdline[MAX_PATH];
create_inf_file("test.inf");
lstrcpyA(cmdline, CURR_DIR);
lstrcatA(cmdline, "\\");
lstrcatA(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
ok(hr == 0, "Expected 0, got %ld\n", hr);
lstrcpyA(cmdline, "\"");
lstrcatA(cmdline, CURR_DIR);
lstrcatA(cmdline, "\\test.inf\",\"DefaultInstall\",\"c:,imacab.cab\",\"4\"");
hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
ok(hr == 0, "Expected 0, got %ld\n", hr);
if (winetest_interactive)
{
lstrcpyA(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %ld\n", hr);
}
DeleteFileA("test.inf");
}
START_TEST(install)
{
DWORD len;
char temp_path[MAX_PATH], prev_path[MAX_PATH];
if (!init_function_pointers())
return;
if (!IsNTAdmin(0, NULL))
{
skip("Most tests need admin rights\n");
return;
}
GetCurrentDirectoryA(MAX_PATH, prev_path);
GetTempPathA(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;
test_RunSetupCommand();
test_LaunchINFSection();
test_LaunchINFSectionEx();
FreeLibrary(hAdvPack);
SetCurrentDirectoryA(prev_path);
}