From 944944f4741a4bd302595ce3df0855d07c3558d7 Mon Sep 17 00:00:00 2001 From: Juan Lang Date: Wed, 23 Jul 2008 17:41:12 -0700 Subject: [PATCH] Test CryptRetrieveSubjectGUID with a cab file --- dlls/crypt32/tests/Makefile.in | 2 dlls/crypt32/tests/sip.c | 255 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 256 insertions(+), 1 deletions(-) diff --git a/dlls/crypt32/tests/Makefile.in b/dlls/crypt32/tests/Makefile.in index 89d6df5..d99ef3c 100644 --- a/dlls/crypt32/tests/Makefile.in +++ b/dlls/crypt32/tests/Makefile.in @@ -3,7 +3,7 @@ TOPOBJDIR = ../../.. SRCDIR = @srcdir@ VPATH = @srcdir@ TESTDLL = crypt32.dll -IMPORTS = crypt32 advapi32 kernel32 +IMPORTS = crypt32 advapi32 kernel32 cabinet CTESTS = \ base64.c \ diff --git a/dlls/crypt32/tests/sip.c b/dlls/crypt32/tests/sip.c index 09d5767..7f00af0 100644 --- a/dlls/crypt32/tests/sip.c +++ b/dlls/crypt32/tests/sip.c @@ -2,6 +2,8 @@ * Subject Interface Package tests * * Copyright 2006 Paul Vriens + * Copyright 2006 James Hawkins + * Copyright 2008 Juan Lang * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -26,6 +28,7 @@ #include #include #include #include +#include #include "wine/test.h" @@ -129,6 +132,240 @@ static void test_AddRemoveProvider(void) ok ( ret, "CryptSIPRemoveProvider should have succeeded\n"); } +/* make the max size large so there is only one cab file */ +#define MEDIA_SIZE 999999999 +#define FOLDER_THRESHOLD 900000 + +CHAR CURR_DIR[MAX_PATH]; + +/* FCI callbacks */ + +static void *mem_alloc(ULONG cb) +{ + return HeapAlloc(GetProcessHeap(), 0, cb); +} + +static void mem_free(void *memory) +{ + HeapFree(GetProcessHeap(), 0, memory); +} + +static BOOL get_next_cabinet(PCCAB pccab, ULONG cbPrevCab, void *pv) +{ + return TRUE; +} + +static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv) +{ + return 0; +} + +static int file_placed(PCCAB pccab, char *pszFile, long cbFile, + BOOL fContinuation, void *pv) +{ + return 0; +} + +static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv) +{ + HANDLE handle; + DWORD dwAccess = 0; + DWORD dwShareMode = 0; + DWORD dwCreateDisposition = OPEN_EXISTING; + + dwAccess = GENERIC_READ | GENERIC_WRITE; + /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */ + dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; + + if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES) + dwCreateDisposition = OPEN_EXISTING; + else + dwCreateDisposition = CREATE_NEW; + + handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL, + dwCreateDisposition, 0, NULL); + + ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile); + + return (INT_PTR)handle; +} + +static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv) +{ + HANDLE handle = (HANDLE)hf; + DWORD dwRead; + BOOL res; + + res = ReadFile(handle, memory, cb, &dwRead, NULL); + ok(res, "Failed to ReadFile\n"); + + return dwRead; +} + +static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv) +{ + HANDLE handle = (HANDLE)hf; + DWORD dwWritten; + BOOL res; + + res = WriteFile(handle, memory, cb, &dwWritten, NULL); + ok(res, "Failed to WriteFile\n"); + + return dwWritten; +} + +static int fci_close(INT_PTR hf, int *err, void *pv) +{ + HANDLE handle = (HANDLE)hf; + ok(CloseHandle(handle), "Failed to CloseHandle\n"); + + return 0; +} + +static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv) +{ + HANDLE handle = (HANDLE)hf; + DWORD ret; + + ret = SetFilePointer(handle, dist, NULL, seektype); + ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n"); + + return ret; +} + +static int fci_delete(char *pszFile, int *err, void *pv) +{ + BOOL ret = DeleteFileA(pszFile); + ok(ret, "Failed to DeleteFile %s\n", pszFile); + + return 0; +} + +static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv) +{ + LPSTR tempname; + + tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH); + GetTempFileNameA(".", "xx", 0, tempname); + + if (tempname && (strlen(tempname) < (unsigned)cbTempName)) + { + lstrcpyA(pszTempName, tempname); + HeapFree(GetProcessHeap(), 0, tempname); + return TRUE; + } + + HeapFree(GetProcessHeap(), 0, tempname); + + return FALSE; +} + +static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime, + USHORT *pattribs, int *err, void *pv) +{ + BY_HANDLE_FILE_INFORMATION finfo; + FILETIME filetime; + HANDLE handle; + DWORD attrs; + BOOL res; + + handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); + + ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName); + + res = GetFileInformationByHandle(handle, &finfo); + ok(res, "Expected GetFileInformationByHandle to succeed\n"); + + FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime); + FileTimeToDosDateTime(&filetime, pdate, ptime); + + attrs = GetFileAttributes(pszName); + ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n"); + /* fixme: should convert attrs to *pattribs, make sure + * have a test that catches the fact that we don't? + */ + + return (INT_PTR)handle; +} + +static void add_file(HFCI hfci, char *file) +{ + char path[MAX_PATH]; + BOOL res; + + lstrcpyA(path, CURR_DIR); + lstrcatA(path, "\\"); + lstrcatA(path, file); + + res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress, + get_open_info, tcompTYPE_MSZIP); + ok(res, "Expected FCIAddFile to succeed\n"); +} + +static void set_cab_parameters(PCCAB pCabParams) +{ + ZeroMemory(pCabParams, sizeof(CCAB)); + + pCabParams->cb = MEDIA_SIZE; + pCabParams->cbFolderThresh = FOLDER_THRESHOLD; + pCabParams->setID = 0xbeef; + lstrcpyA(pCabParams->szCabPath, CURR_DIR); + lstrcatA(pCabParams->szCabPath, "\\"); + lstrcpyA(pCabParams->szCab, "extract.cab"); +} + +static void createTestFile(const CHAR *name) +{ + HANDLE file; + DWORD written; + + file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); + ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name); + WriteFile(file, name, strlen(name), &written, NULL); + WriteFile(file, "\n", strlen("\n"), &written, NULL); + CloseHandle(file); +} + +static void delete_test_files(void) +{ + DeleteFileA("a.txt"); + DeleteFileA("extract.cab"); +} + +static void create_cab_file(void) +{ + static CHAR a_txt[] = "a.txt"; + CCAB cabParams; + HFCI hfci; + ERF erf; + BOOL res; + int len; + + GetCurrentDirectoryA(MAX_PATH, CURR_DIR); + len = lstrlenA(CURR_DIR); + + if(len && (CURR_DIR[len-1] == '\\')) + CURR_DIR[len-1] = 0; + + set_cab_parameters(&cabParams); + + hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open, + fci_read, fci_write, fci_close, fci_seek, fci_delete, + get_temp_file, &cabParams, NULL); + + ok(hfci != NULL, "Failed to create an FCI context\n"); + + createTestFile(a_txt); + add_file(hfci, a_txt); + + res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress); + ok(res, "Failed to flush the cabinet\n"); + + res = FCIDestroy(hfci); + ok(res, "Failed to destroy the cabinet\n"); +} + static void test_SIPRetrieveSubjectGUID(void) { BOOL ret; @@ -138,8 +375,11 @@ static void test_SIPRetrieveSubjectGUID( static const CHAR regeditExe[] = "regedit.exe"; static const GUID nullSubject = { 0x0, 0x0, 0x0, { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 }}; static const WCHAR deadbeef[] = { 'c',':','\\','d','e','a','d','b','e','e','f','.','d','b','f',0 }; + static const WCHAR extractW[] = { 'e','x','t','r','a','c','t','.', + 'c','a','b',0 }; /* Couldn't find a name for this GUID, it's the one used for 95% of the files */ static const GUID unknownGUID = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; + static const GUID cabGUID = { 0xc689aaba, 0x8e78, 0x11d0, {0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee }}; static CHAR regeditPath[MAX_PATH]; static WCHAR regeditPathW[MAX_PATH]; static CHAR path[MAX_PATH]; @@ -255,6 +495,21 @@ static void test_SIPRetrieveSubjectGUID( /* Clean up */ DeleteFileA(tempfile); + + /* Create a .cab file */ + create_cab_file(); + + SetLastError(0xdeadbeef); + memset(&subject, 1, sizeof(GUID)); + ret = CryptSIPRetrieveSubjectGuid(extractW, NULL, &subject); + todo_wine + ok( ret, "CryptSIPRetrieveSubjectGuid failed: %d (0x%08x)\n", + GetLastError(), GetLastError() ); + todo_wine + ok ( !memcmp(&subject, &cabGUID, sizeof(GUID)), + "Expected GUID %s for cabinet file, not %s\n", show_guid(&cabGUID, guid1), show_guid(&subject, guid2)); + /* Clean up */ + delete_test_files(); } static void test_SIPLoad(void) -- 1.4.1