1 /*
2  * PROJECT:     apphelp_apitest
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Tests for shim-database api's
5  * COPYRIGHT:   Copyright 2012 Detlef Riekenberg
6  *              Copyright 2013 Mislav Blažević
7  *              Copyright 2015-2019 Mark Jansen (mark.jansen@reactos.org)
8  */
9 
10 #include <ntstatus.h>
11 #define WIN32_NO_STATUS
12 #include <windows.h>
13 #include <shlwapi.h>
14 #include <winnt.h>
15 #ifdef __REACTOS__
16 #include <ntndk.h>
17 #else
18 #include <winternl.h>
19 #endif
20 
21 #include <winerror.h>
22 #include <stdio.h>
23 #include <initguid.h>
24 
25 #include <pseh/pseh2.h>
26 
27 #include "wine/test.h"
28 
29 #include "apphelp_apitest.h"
30 
31 
32 #define DOS_PATH 0
33 #define HID_DATABASE_FULLPATH 2
34 
35 #define SDB_DATABASE_MAIN_SHIM 0x80030000
36 
37 
38 #define TAGID_NULL 0x0
39 #define TAGID_ROOT 0x0
40 #define _TAGID_ROOT 12
41 
42 
43 #define TAG_TYPE_MASK 0xF000
44 
45 #define TAG_TYPE_NULL 0x1000
46 #define TAG_TYPE_BYTE 0x2000
47 #define TAG_TYPE_WORD 0x3000
48 #define TAG_TYPE_DWORD 0x4000
49 #define TAG_TYPE_QWORD 0x5000
50 #define TAG_TYPE_STRINGREF 0x6000
51 #define TAG_TYPE_LIST 0x7000
52 #define TAG_TYPE_STRING 0x8000
53 #define TAG_TYPE_BINARY 0x9000
54 #define TAG_NULL 0x0
55 
56 #define TAG_INCLUDE (0x1 | TAG_TYPE_NULL)
57 
58 #define TAG_MATCH_MODE (0x1 | TAG_TYPE_WORD)
59 
60 #define TAG_SIZE (0x1 | TAG_TYPE_DWORD)
61 #define TAG_CHECKSUM (0x3 | TAG_TYPE_DWORD)
62 #define TAG_MODULE_TYPE (0x6 | TAG_TYPE_DWORD)
63 #define TAG_VERFILEOS (0x9 | TAG_TYPE_DWORD)
64 #define TAG_VERFILETYPE (0xA | TAG_TYPE_DWORD)
65 #define TAG_PE_CHECKSUM (0xB | TAG_TYPE_DWORD)
66 #define TAG_PROBLEMSEVERITY (0x10 | TAG_TYPE_DWORD)
67 #define TAG_HTMLHELPID (0x15 | TAG_TYPE_DWORD)
68 #define TAG_FLAGS (0x17 | TAG_TYPE_DWORD)
69 #define TAG_LAYER_TAGID (0x1A | TAG_TYPE_DWORD)
70 #define TAG_LINKER_VERSION (0x1C | TAG_TYPE_DWORD)
71 #define TAG_LINK_DATE (0x1D | TAG_TYPE_DWORD)
72 #define TAG_UPTO_LINK_DATE (0x1E | TAG_TYPE_DWORD)
73 #define TAG_APP_NAME_RC_ID (0x24 | TAG_TYPE_DWORD)
74 #define TAG_VENDOR_NAME_RC_ID (0x25 | TAG_TYPE_DWORD)
75 #define TAG_SUMMARY_MSG_RC_ID (0x26 | TAG_TYPE_DWORD)
76 #define TAG_OS_PLATFORM (0x23 | TAG_TYPE_DWORD)
77 
78 #define TAG_TIME (0x1 | TAG_TYPE_QWORD)
79 #define TAG_BIN_FILE_VERSION (0x2 | TAG_TYPE_QWORD)
80 #define TAG_BIN_PRODUCT_VERSION (0x3 | TAG_TYPE_QWORD)
81 #define TAG_UPTO_BIN_PRODUCT_VERSION (0x6 | TAG_TYPE_QWORD)
82 #define TAG_UPTO_BIN_FILE_VERSION (0xD | TAG_TYPE_QWORD)
83 #define TAG_FLAG_LUA (0x10 | TAG_TYPE_QWORD)
84 
85 #define TAG_DATABASE (0x1 | TAG_TYPE_LIST)
86 #define TAG_INEXCLUD (0x3 | TAG_TYPE_LIST)
87 #define TAG_EXE (0x7 | TAG_TYPE_LIST)
88 #define TAG_MATCHING_FILE (0x8 | TAG_TYPE_LIST)
89 #define TAG_SHIM_REF (0x9| TAG_TYPE_LIST)
90 #define TAG_LAYER (0xB | TAG_TYPE_LIST)
91 #define TAG_APPHELP (0xD | TAG_TYPE_LIST)
92 #define TAG_LINK (0xE | TAG_TYPE_LIST)
93 #define TAG_DATA (0xF | TAG_TYPE_LIST)
94 #define TAG_STRINGTABLE (0x801 | TAG_TYPE_LIST)
95 
96 #define TAG_STRINGTABLE_ITEM (0x801 | TAG_TYPE_STRING)
97 
98 #define TAG_NAME (0x1 | TAG_TYPE_STRINGREF)
99 #define TAG_MODULE (0x3 | TAG_TYPE_STRINGREF)
100 #define TAG_VENDOR (0x5 | TAG_TYPE_STRINGREF)
101 #define TAG_APP_NAME (0x6 | TAG_TYPE_STRINGREF)
102 #define TAG_COMMAND_LINE (0x8 | TAG_TYPE_STRINGREF)
103 #define TAG_COMPANY_NAME (0x9 | TAG_TYPE_STRINGREF)
104 #define TAG_WILDCARD_NAME (0xB | TAG_TYPE_STRINGREF)
105 #define TAG_PRODUCT_NAME (0x10 | TAG_TYPE_STRINGREF)
106 #define TAG_PRODUCT_VERSION (0x11 | TAG_TYPE_STRINGREF)
107 #define TAG_FILE_DESCRIPTION (0x12 | TAG_TYPE_STRINGREF)
108 #define TAG_FILE_VERSION (0x13 | TAG_TYPE_STRINGREF)
109 #define TAG_ORIGINAL_FILENAME (0x14 | TAG_TYPE_STRINGREF)
110 #define TAG_INTERNAL_NAME (0x15 | TAG_TYPE_STRINGREF)
111 #define TAG_LEGAL_COPYRIGHT (0x16 | TAG_TYPE_STRINGREF)
112 #define TAG_APPHELP_DETAILS (0x18 | TAG_TYPE_STRINGREF)
113 #define TAG_LINK_URL (0x19 | TAG_TYPE_STRINGREF)
114 #define TAG_APPHELP_TITLE (0x1B | TAG_TYPE_STRINGREF)
115 
116 #define TAG_COMPILER_VERSION (0x22 | TAG_TYPE_STRINGREF)
117 
118 #define TAG_GENERAL (0x2 | TAG_TYPE_NULL)
119 
120 #define TAG_EXE_ID (0x4 | TAG_TYPE_BINARY)
121 #define TAG_DATA_BITS (0x5 | TAG_TYPE_BINARY)
122 #define TAG_DATABASE_ID (0x7 | TAG_TYPE_BINARY)
123 
124 
125 typedef struct _DB_INFORMATION
126 {
127     DWORD dwSomething;
128     DWORD dwMajor;
129     DWORD dwMinor;
130     LPCWSTR Description;
131     GUID Id;
132     /* Win10+ has an extra field here */
133 } DB_INFORMATION, *PDB_INFORMATION;
134 
135 
136 static HMODULE hdll;
137 static LPCWSTR (WINAPI *pSdbTagToString)(TAG);
138 static PDB (WINAPI *pSdbOpenDatabase)(LPCWSTR, PATH_TYPE);
139 static PDB (WINAPI *pSdbCreateDatabase)(LPCWSTR, PATH_TYPE);
140 static BOOL (WINAPI *pSdbGetDatabaseVersion)(LPCWSTR, PDWORD, PDWORD);
141 static void (WINAPI *pSdbCloseDatabase)(PDB);
142 static void (WINAPI *pSdbCloseDatabaseWrite)(PDB);
143 static TAG (WINAPI *pSdbGetTagFromTagID)(PDB, TAGID);
144 static BOOL (WINAPI *pSdbWriteNULLTag)(PDB, TAG);
145 static BOOL (WINAPI *pSdbWriteWORDTag)(PDB, TAG, WORD);
146 static BOOL (WINAPI *pSdbWriteDWORDTag)(PDB, TAG, DWORD);
147 static BOOL (WINAPI *pSdbWriteQWORDTag)(PDB, TAG, QWORD);
148 static BOOL (WINAPI *pSdbWriteBinaryTagFromFile)(PDB, TAG, LPCWSTR);
149 static BOOL (WINAPI *pSdbWriteStringTag)(PDB, TAG, LPCWSTR);
150 static BOOL (WINAPI *pSdbWriteStringRefTag)(PDB, TAG, TAGID);
151 static TAGID (WINAPI *pSdbBeginWriteListTag)(PDB, TAG);
152 static BOOL (WINAPI *pSdbEndWriteListTag)(PDB, TAGID);
153 static TAGID (WINAPI *pSdbFindFirstTag)(PDB, TAGID, TAG);
154 static TAGID (WINAPI *pSdbFindNextTag)(PDB, TAGID, TAGID);
155 static TAGID (WINAPI *pSdbFindFirstNamedTag)(PDB pdb, TAGID root, TAGID find, TAGID nametag, LPCWSTR find_name);
156 static WORD (WINAPI *pSdbReadWORDTag)(PDB, TAGID, WORD);
157 static DWORD (WINAPI *pSdbReadDWORDTag)(PDB, TAGID, DWORD);
158 static QWORD (WINAPI *pSdbReadQWORDTag)(PDB, TAGID, QWORD);
159 static BOOL (WINAPI *pSdbReadBinaryTag)(PDB, TAGID, PBYTE, DWORD);
160 static BOOL (WINAPI *pSdbReadStringTag)(PDB, TAGID, LPWSTR, DWORD);
161 static DWORD (WINAPI *pSdbGetTagDataSize)(PDB, TAGID);
162 static PVOID (WINAPI *pSdbGetBinaryTagData)(PDB, TAGID);
163 static LPWSTR (WINAPI *pSdbGetStringTagPtr)(PDB, TAGID);
164 static TAGID (WINAPI *pSdbGetFirstChild)(PDB, TAGID);
165 static TAGID (WINAPI *pSdbGetNextChild)(PDB, TAGID, TAGID);
166 static BOOL (WINAPI *pSdbGetDatabaseID)(PDB, GUID*);
167 static BOOL (WINAPI *pSdbGUIDToString)(CONST GUID *, PCWSTR, SIZE_T);
168 static HSDB (WINAPI *pSdbInitDatabase)(DWORD, LPCWSTR);
169 static void (WINAPI *pSdbReleaseDatabase)(HSDB);
170 static BOOL (WINAPI *pSdbGetMatchingExe)(HSDB hsdb, LPCWSTR path, LPCWSTR module_name, LPCWSTR env, DWORD flags, PSDBQUERYRESULT_VISTA result);
171 static BOOL (WINAPI *pSdbTagRefToTagID)(HSDB hSDB, TAGREF trWhich, PDB *ppdb, TAGID *ptiWhich);
172 static BOOL (WINAPI *pSdbTagIDToTagRef)(HSDB hSDB, PDB pdb, TAGID tiWhich, TAGREF *ptrWhich);
173 static TAGREF (WINAPI *pSdbGetLayerTagRef)(HSDB hsdb, LPCWSTR layerName);
174 static LONGLONG (WINAPI* pSdbMakeIndexKeyFromString)(LPCWSTR);
175 static DWORD (WINAPI* pSdbQueryData)(HSDB hsdb, TAGREF trWhich, LPCWSTR lpszDataName, LPDWORD lpdwDataType, LPVOID lpBuffer, LPDWORD lpcbBufferSize);
176 static DWORD (WINAPI* pSdbQueryDataEx)(HSDB hsdb, TAGREF trWhich, LPCWSTR lpszDataName, LPDWORD lpdwDataType, LPVOID lpBuffer, LPDWORD lpcbBufferSize, TAGREF *ptrData);
177 static DWORD (WINAPI* pSdbQueryDataExTagID)(PDB pdb, TAGID tiExe, LPCWSTR lpszDataName, LPDWORD lpdwDataType, LPVOID lpBuffer, LPDWORD lpcbBufferSize, TAGID *ptiData);
178 static BOOL (WINAPI* pSdbGetDatabaseInformation)(PDB pdb, PDB_INFORMATION information);
179 static VOID (WINAPI* pSdbFreeDatabaseInformation)(PDB_INFORMATION information);
180 
181 DEFINE_GUID(GUID_DATABASE_TEST, 0xe39b0eb0, 0x55db, 0x450b, 0x9b, 0xd4, 0xd2, 0x0c, 0x94, 0x84, 0x26, 0x0f);
182 DEFINE_GUID(GUID_MAIN_DATABASE, 0x11111111, 0x1111, 0x1111, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11);
183 
184 
185 static void Write(HANDLE file, LPCVOID buffer, DWORD size)
186 {
187     DWORD dwWritten = 0;
188     WriteFile(file, buffer, size, &dwWritten, NULL);
189 }
190 
191 static void test_GetDatabaseInformationEmpty(PDB pdb)
192 {
193     PDB_INFORMATION pInfo;
194     BOOL fResult;
195 
196     if (!pSdbGetDatabaseInformation || !pSdbFreeDatabaseInformation)
197     {
198         skip("GetDatabaseInformation or SdbFreeDatabaseInformation not found\n");
199         return;
200     }
201 
202     pInfo = (PDB_INFORMATION)malloc(sizeof(*pInfo) * 4);
203     memset(pInfo, 0xDE, sizeof(*pInfo) * 2);
204 
205     fResult = pSdbGetDatabaseInformation(pdb, pInfo);
206     ok(fResult, "SdbGetDatabaseInformation failed\n");
207     if (fResult)
208     {
209         ok_int(pInfo->dwSomething, 0);
210         ok(IsEqualGUID(GUID_NULL, pInfo->Id), "expected guid to be empty(%s)\n", wine_dbgstr_guid(&pInfo->Id));
211         ok(pInfo->Description == NULL, "Expected pInfo->Description to be NULL, was %s\n", wine_dbgstr_w(pInfo->Description));
212 
213         /* Struct is slightly bigger on some Win10, and the DB version nr is different on all */
214         if (g_WinVersion >= WINVER_WIN10)
215         {
216             ok(pInfo->dwMajor == 3, "Expected pInfo->dwMajor to be 3, was: %d\n", pInfo->dwMajor);
217             ok(pInfo->dwMinor == 0, "Expected pInfo->dwMinor to be 0, was: %d\n", pInfo->dwMinor);
218 
219             ok(pInfo[1].dwSomething == 0 || pInfo[1].dwSomething == 0xdededede, "Something amiss: 0x%x\n", pInfo[1].dwSomething);
220             ok(pInfo[1].dwMajor == 0xdededede, "Cookie2 corrupt: 0x%x\n", pInfo[1].dwMajor);
221         }
222         else
223         {
224             ok(pInfo->dwMajor == 2, "Expected pInfo->dwMajor to be 2, was: %d\n", pInfo->dwMajor);
225             if (g_WinVersion >= _WIN32_WINNT_VISTA)
226             {
227                 ok(pInfo->dwMinor == 1, "Expected pInfo->dwMinor to be 1, was: %d\n", pInfo->dwMinor);
228             }
229             else
230             {
231                 SYSTEMTIME si = {0};
232                 GetSystemTime(&si);
233                 DWORD dwExpect = ((DWORD)si.wYear - 2000) * 10000 + si.wMonth * 100 + si.wDay;
234                 ok(pInfo->dwMinor == dwExpect, "Expected pInfo->dwMinor to be %d, was: %d\n", dwExpect, pInfo->dwMinor);
235             }
236 
237             ok(pInfo[1].dwSomething == 0xdededede, "Cookie1 corrupt: 0x%x\n", pInfo[1].dwSomething);
238             ok(pInfo[1].dwMajor == 0xdededede, "Cookie2 corrupt: 0x%x\n", pInfo[1].dwMajor);
239         }
240 
241     }
242     free(pInfo);
243 }
244 
245 
246 static void test_Sdb(void)
247 {
248     static const WCHAR temp[] = L"temp";
249     static const WCHAR path1[] = L"temp.sdb";
250     static const WCHAR path2[] = L"temp2.bin";
251     static const WCHAR tag_size_string[] = L"SIZE";
252     static const WCHAR tag_flag_lua_string[] = L"FLAG_LUA";
253     static const WCHAR invalid_tag[] = L"InvalidTag";
254     static const TAG tags[5] = {
255         TAG_SIZE, TAG_FLAG_LUA, TAG_NAME,
256         TAG_STRINGTABLE, TAG_STRINGTABLE_ITEM
257     };
258     WCHAR buffer[6] = { 0 };
259     PDB pdb;
260     QWORD qword;
261     DWORD dword;
262     WORD word;
263     BOOL ret;
264     HANDLE file; /* temp file created for testing purpose */
265     TAG tag;
266     TAGID tagid, ptagid, stringref = 6;
267     LPCWSTR string;
268     PBYTE binary;
269 
270     pdb = pSdbCreateDatabase(path1, DOS_PATH);
271     ok(pdb != NULL, "failed to create database\n");
272     if (pdb != NULL)
273     {
274         ret = pSdbWriteDWORDTag(pdb, tags[0], 0xDEADBEEF);
275         ok(ret, "failed to write DWORD tag\n");
276         ret = pSdbWriteQWORDTag(pdb, tags[1], 0xDEADBEEFBABE);
277         ok(ret, "failed to write QWORD tag\n");
278         ret = pSdbWriteStringRefTag(pdb, tags[2], stringref);
279         ok(ret, "failed to write stringref tag\n");
280         tagid = pSdbBeginWriteListTag(pdb, tags[3]);
281         ok(tagid != TAGID_NULL, "unexpected NULL tagid\n");
282         ret = pSdbWriteStringTag(pdb, tags[4], temp);
283         ok(ret, "failed to write string tag\n");
284         ret = pSdbWriteNULLTag(pdb, TAG_GENERAL);
285         ok(ret, "failed to write NULL tag\n");
286         ret = pSdbWriteWORDTag(pdb, TAG_MATCH_MODE, 0xACE);
287         ok(ret, "failed to write WORD tag\n");
288         ret = pSdbEndWriteListTag(pdb, tagid);
289         ok(ret, "failed to update list size\n");
290         /* [Err ][SdbCloseDatabase    ] Failed to close the file. */
291         pSdbCloseDatabaseWrite(pdb);
292     }
293 
294     /* [Err ][SdbGetDatabaseID    ] Failed to get root tag */
295     pdb = pSdbOpenDatabase(path1, DOS_PATH);
296     ok(pdb != NULL, "unexpected NULL handle\n");
297 
298     if (pdb)
299     {
300         tagid = pSdbGetFirstChild(pdb, TAGID_ROOT);
301         ok(tagid == _TAGID_ROOT, "unexpected tagid %u, expected %u\n", tagid, _TAGID_ROOT);
302 
303         tag = pSdbGetTagFromTagID(pdb, tagid);
304         ok(tag == TAG_SIZE, "unexpected tag 0x%x, expected 0x%x\n", tag, TAG_SIZE);
305 
306         string = pSdbTagToString(tag);
307         ok(lstrcmpW(string, tag_size_string) == 0, "unexpected string %s, expected %s\n",
308             wine_dbgstr_w(string), wine_dbgstr_w(tag_size_string));
309 
310         dword = pSdbReadDWORDTag(pdb, tagid, 0);
311         ok(dword == 0xDEADBEEF, "unexpected value %u, expected 0xDEADBEEF\n", dword);
312 
313         tagid = pSdbGetNextChild(pdb, TAGID_ROOT, tagid);
314         ok(tagid == _TAGID_ROOT + sizeof(TAG) + sizeof(DWORD), "unexpected tagid %u, expected %u\n",
315             tagid, _TAGID_ROOT + sizeof(TAG) + sizeof(DWORD));
316 
317         tag = pSdbGetTagFromTagID(pdb, tagid);
318         ok(tag == TAG_FLAG_LUA, "unexpected tag 0x%x, expected 0x%x\n", tag, TAG_FLAG_LUA);
319 
320         string = pSdbTagToString(tag);
321         if (g_WinVersion >= WINVER_VISTA)
322         {
323             ok(lstrcmpW(string, tag_flag_lua_string) == 0, "unexpected string %s, expected %s\n",
324                 wine_dbgstr_w(string), wine_dbgstr_w(tag_flag_lua_string));
325         }
326         else
327         {
328             ok(lstrcmpW(string, invalid_tag) == 0, "unexpected string %s, expected %s\n",
329                 wine_dbgstr_w(string), wine_dbgstr_w(invalid_tag));
330         }
331 
332         qword = pSdbReadQWORDTag(pdb, tagid, 0);
333         ok(qword == 0xDEADBEEFBABE, "unexpected value 0x%I64x, expected 0xDEADBEEFBABE\n", qword);
334 
335         tagid = pSdbGetNextChild(pdb, TAGID_ROOT, tagid);
336         string = pSdbGetStringTagPtr(pdb, tagid);
337         ok(string && (lstrcmpW(string, temp) == 0), "unexpected string %s, expected %s\n",
338             wine_dbgstr_w(string), wine_dbgstr_w(temp));
339 
340         ptagid = pSdbGetNextChild(pdb, TAGID_ROOT, tagid);
341         tagid = pSdbGetFirstChild(pdb, ptagid);
342 
343         string = pSdbGetStringTagPtr(pdb, tagid);
344         ok(string && (lstrcmpW(string, temp) == 0), "unexpected string %s, expected %s\n",
345             wine_dbgstr_w(string), wine_dbgstr_w(temp));
346 
347         ok(pSdbReadStringTag(pdb, tagid, buffer, 6), "failed to write string to buffer\n");
348         /* [Err ][SdbpReadTagData     ] Buffer too small. Avail: 6, Need: 10. */
349         ok(!pSdbReadStringTag(pdb, tagid, buffer, 3), "string was written to buffer, but failure was expected");
350         ok(pSdbGetTagDataSize(pdb, tagid) == 5 * sizeof(WCHAR), "string has unexpected size\n");
351 
352         tagid = pSdbGetNextChild(pdb, ptagid, tagid);
353         tag = pSdbGetTagFromTagID(pdb, tagid);
354         ok(tag == TAG_GENERAL, "unexpected tag 0x%x, expected 0x%x\n", tag, TAG_GENERAL);
355         ok(pSdbGetTagDataSize(pdb, tagid) == 0, "null tag with size > 0\n");
356 
357         tagid = pSdbGetNextChild(pdb, ptagid, tagid);
358         word = pSdbReadWORDTag(pdb, tagid, 0);
359         ok(word == 0xACE, "unexpected value 0x%x, expected 0x%x\n", word, 0xACE);
360 
361         test_GetDatabaseInformationEmpty(pdb);
362 
363         pSdbCloseDatabase(pdb);
364     }
365     DeleteFileW(path1);
366 
367     file = CreateFileW(path2, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
368     ok(file != INVALID_HANDLE_VALUE, "failed to open file\n");
369     Write(file, &qword, 8);
370     CloseHandle(file);
371 
372     pdb = pSdbCreateDatabase(path1, DOS_PATH);
373     ok(pdb != NULL, "unexpected NULL handle\n");
374 
375     if (pdb)
376     {
377         ret = pSdbWriteBinaryTagFromFile(pdb, TAG_DATA_BITS, path2);
378         ok(ret, "failed to write tag from binary file\n");
379         pSdbCloseDatabaseWrite(pdb);     /* [Err ][SdbCloseDatabase    ] Failed to close the file. */
380         DeleteFileW(path2);
381 
382         /* FIXME: doesnt work on win10?! */
383         pdb = pSdbOpenDatabase(path1, DOS_PATH);
384         if (g_WinVersion < WINVER_WIN10)
385         {
386             /* ERROR,SdbOpenDatabaseEx,845,Failed to open SDB - File size too large or small. */
387             ok(pdb != NULL, "unexpected NULL handle\n");
388         }
389         if (pdb)
390         {
391             binary = (PBYTE)pSdbGetBinaryTagData(pdb, _TAGID_ROOT);
392             ok(memcmp(binary, &qword, 8) == 0, "binary data is corrupt\n");
393             ret = pSdbReadBinaryTag(pdb, _TAGID_ROOT, (PBYTE)buffer, 12);
394             ok(ret, "failed to read binary tag\n");
395             ok(memcmp(buffer, &qword, 8) == 0, "binary data is corrupt\n");
396             pSdbCloseDatabase(pdb);
397         }
398     }
399     DeleteFileW(path1);
400 }
401 
402 /*
403  - Show that a stringtable is automatically generated,
404  - Show that entries in the stringtable are re-used,
405  - validate multiple lists (for the length)
406  */
407 static void test_write_ex(void)
408 {
409     WCHAR path1[] = {'t','e','s','t','.','s','d','b',0};
410     WCHAR test1[] = {'T','E','S','T',0};
411     WCHAR test2[] = {'t','e','s','t',0};
412     PDB pdb;
413     TAGID tagdb, tagstr;
414     TAG tag;
415     DWORD size;
416     BOOL ret;
417     LPWSTR ptr;
418 
419     /* Write a small database */
420     pdb = pSdbCreateDatabase(path1, DOS_PATH);
421     ok(pdb != NULL, "Expected a valid database\n");
422     if (!pdb)
423         return;
424     tagdb = pSdbBeginWriteListTag(pdb, TAG_DATABASE);
425     ok(tagdb == 12, "Expected tag to be 12, was %u\n", tagdb);
426     ret = pSdbWriteStringTag(pdb, TAG_NAME, test1);
427     ret = pSdbWriteStringTag(pdb, TAG_NAME, test2);
428     ok(ret, "Expected SdbWriteStringTag to succeed\n");
429     ret = pSdbEndWriteListTag(pdb, tagdb);
430     ok(ret, "Expected SdbEndWriteListTag to succeed\n");
431 
432     tagdb = pSdbBeginWriteListTag(pdb, TAG_DATABASE);
433     ok(tagdb == 30, "Expected tag to be 24, was %u\n", tagdb);
434     ret = pSdbWriteStringTag(pdb, TAG_NAME, test1);
435     ret = pSdbWriteStringTag(pdb, TAG_NAME, test2);
436     ok(ret, "Expected SdbWriteStringTag to succeed\n");
437     ret = pSdbEndWriteListTag(pdb, tagdb);
438     ok(ret, "Expected SdbEndWriteListTag to succeed\n");
439 
440     pSdbCloseDatabaseWrite(pdb);
441 
442     /* Now validate it's contents */
443     pdb = pSdbOpenDatabase(path1, DOS_PATH);
444     ok(pdb != NULL, "Expected a valid database\n");
445     if (!pdb)
446         return;
447 
448     tagdb = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
449     ok(tagdb == 12, "Expected tag to be 12, was %u\n", tagdb);
450     size = pSdbGetTagDataSize(pdb, tagdb);
451     ok(size == 12, "Expected size to be 12, was %u\n", size);
452 
453     tagstr = pSdbFindFirstTag(pdb, tagdb, TAG_NAME);
454     ok(tagstr == 18, "Expected string tag to be 18, was %u\n", tagstr);
455     tag = pSdbGetTagFromTagID(pdb, tagstr);
456     ok(tag == TAG_NAME, "Expected tag to be TAG_NAME, was 0x%x\n", (DWORD)tag);
457     size = pSdbGetTagDataSize(pdb, tagstr);
458     ok(size == 4, "Expected size to be 4, was 0x%x\n", size);
459 
460     tagstr = pSdbFindNextTag(pdb, tagdb, tagstr);
461     ok(tagstr == 24, "Expected string tag to be 24, was %u\n", tagstr);
462     tag = pSdbGetTagFromTagID(pdb, tagstr);
463     ok(tag == TAG_NAME, "Expected tag to be TAG_NAME, was 0x%x\n", (DWORD)tag);
464     size = pSdbGetTagDataSize(pdb, tagstr);
465     ok(size == 4, "Expected size to be 4, was 0x%x\n", size);
466 
467     tagdb = pSdbFindNextTag(pdb, TAGID_ROOT, tagdb);
468     ok(tagdb == 30, "Expected tag to be 30, was %u\n", tagdb);
469     size = pSdbGetTagDataSize(pdb, tagdb);
470     ok(size == 12, "Expected size to be 12, was %u\n", size);
471 
472     tagstr = pSdbFindFirstTag(pdb, tagdb, TAG_NAME);
473     ok(tagstr == 36, "Expected string tag to be 36, was %u\n", tagstr);
474     tag = pSdbGetTagFromTagID(pdb, tagstr);
475     ok(tag == TAG_NAME, "Expected tag to be TAG_NAME, was 0x%x\n", (DWORD)tag);
476     size = pSdbGetTagDataSize(pdb, tagstr);
477     ok(size == 4, "Expected size to be 4, was %u\n", size);
478 
479     tagstr = pSdbFindNextTag(pdb, tagdb, tagstr);
480     ok(tagstr == 42, "Expected string tag to be 42, was %u\n", tagstr);
481     tag = pSdbGetTagFromTagID(pdb, tagstr);
482     ok(tag == TAG_NAME, "Expected tag to be TAG_NAME, was 0x%x\n", (DWORD)tag);
483     size = pSdbGetTagDataSize(pdb, tagstr);
484     ok(size == 4, "Expected size to be 4, was 0x%x\n", size);
485 
486     tagdb = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_STRINGTABLE);
487     ok(tagdb == 48, "Expected tag to be 48, was %u\n", tagdb);
488     size = pSdbGetTagDataSize(pdb, tagdb);
489     ok(size == 32, "Expected size to be 32, was %u\n", size);
490 
491     tagstr = pSdbGetFirstChild(pdb, tagdb);
492     ok(tagstr == 54, "Expected string tag to be 54, was %u\n", tagstr);
493     tag = pSdbGetTagFromTagID(pdb, tagstr);
494     ok(tag == TAG_STRINGTABLE_ITEM, "Expected tag to be TAG_STRINGTABLE_ITEM, was 0x%x\n", (DWORD)tag);
495     size = pSdbGetTagDataSize(pdb, tagstr);
496     ok(size == 10, "Expected size to be 10, was %u\n", size);
497     ptr = pSdbGetStringTagPtr(pdb, tagstr);
498     ok(ptr != NULL, "Expected a valid pointer\n");
499     if (ptr)
500         ok(!wcscmp(ptr, test1), "Expected ptr to be %s, was %s\n", wine_dbgstr_w(test1), wine_dbgstr_w(ptr));
501 
502     tagstr = pSdbGetNextChild(pdb, tagdb, tagstr);
503     ok(tagstr == 70, "Expected string tag to be 70, was %u\n", tagstr);
504     tag = pSdbGetTagFromTagID(pdb, tagstr);
505     ok(tag == TAG_STRINGTABLE_ITEM, "Expected tag to be TAG_STRINGTABLE_ITEM, was 0x%x\n", (DWORD)tag);
506     size = pSdbGetTagDataSize(pdb, tagstr);
507     ok(size == 10, "Expected size to be 10, was %u\n", size);
508     ptr = pSdbGetStringTagPtr(pdb, tagstr);
509     ok(ptr != NULL, "Expected a valid pointer\n");
510     if (ptr)
511         ok(!wcscmp(ptr, test2), "Expected ptr to be %s, was %s\n", wine_dbgstr_w(test2), wine_dbgstr_w(ptr));
512 
513     pSdbCloseDatabase(pdb);
514 }
515 
516 
517 static void write_db_strings(const WCHAR* name, const WCHAR* data[], size_t count)
518 {
519     PDB pdb;
520     size_t n;
521     BOOL ret;
522 
523     pdb = pSdbCreateDatabase(name, DOS_PATH);
524     ok(pdb != NULL, "Failed to create db for case %u\n", count);
525     for (n = 0; n < count; ++n)
526     {
527         ret = pSdbWriteStringTag(pdb, TAG_NAME, data[n]);
528         ok(ret, "Failed to write string %u/%u\n", n, count);
529     }
530     pSdbCloseDatabaseWrite(pdb);
531 }
532 
533 static void test_stringtable()
534 {
535     static const WCHAR path1[] = {'t','e','s','t','.','s','d','b',0};
536     static const WCHAR test1[] = {'t','e','s','t','1',0};
537     static const WCHAR test2[] = {'T','e','s','t','1',0};
538     static const WCHAR test3[] = {'T','E','s','t','1',0};
539     static const WCHAR test4[] = {'T','E','S','T','1',0};
540     static const WCHAR test5[] = {'T','E','S','T','2',0};
541     static const WCHAR lipsum[] = {'L','o','r','e','m',' ','i','p','s','u','m',' ','d','o','l','o','r',' ','s','i','t',' ','a','m','e','t',',',' ','c','o','n','s','e','c','t','e','t','u','r',' ','a','d','i','p','i','s','c','i','n','g',' ','e','l','i','t','.',' ','N','u','l','l','a',' ','a','n','t','e',' ','r','i','s','u','s',',',' ','m','a','l','e','s','u','a','d','a',' ','s','e','d',' ','i','a','c','u','l','i','s',' ','l','u','c','t','u','s',',',' ','o','r','n','a','r','e',' ','p','u','l','v','i','n','a','r',' ','v','e','l','i','t','.',' ','L','o','r','e','m',' ','i','p','s','u','m',' ','d','o','l','o','r',' ','s','i','t',' ','a','m','e','t',',',' ','c','o','n','s','e','c','t','e','t','u','r',' ','a','d','i','p','i','s','c','i','n','g',' ','e','l','i','t','.',' ','I','n','t','e','g','e','r',' ','q','u','i','s',' ','f','e','l','i','s',' ','u','t',' ','l','e','o',' ','e','l','e','i','f','e','n','d',' ','u','l','t','r','i','c','e','s',' ','f','i','n','i','b','u','s',' ','e','u',' ','d','o','l','o','r','.',' ','I','n',' ','b','i','b','e','n','d','u','m',',',' ','e','r','o','s',' ','e','u',' ','f','a','u','c','i','b','u','s',' ','c','o','n','s','e','q','u','a','t',',',' ','n','i','s','i',' ','m','a','g','n','a',' ','v','e','n','e','n','a','t','i','s',' ','j','u','s','t','o',',',' ','a','t',' ','t','r','i','s','t','i','q','u','e',' ','m','e','t','u','s',' ','d','o','l','o','r',' ','u','t',' ','r','i','s','u','s','.',' ','N','u','n','c',' ','e','u',' ','o','d','i','o',' ','d','i','g','n','i','s','s','i','m',',',' ','o','r','n','a','r','e',' ','a','n','t','e',' ','g','r','a','v','i','d','a',',',' ','l','o','b','o','r','t','i','s',' ','e','r','o','s','.',' ','C','r','a','s',' ','s','e','m',' ','e','x',',',' ','c','o','n','s','e','c','t','e','t','u','r',' ','p','u','l','v','i','n','a','r',' ','t','i','n','c','i','d','u','n','t',' ','e','u',',',' ','c','o','n','g','u','e',' ','a',' ','e','r','o','s','.',' ','C','u','r','a','b','i','t','u','r',' ','e','r','o','s',' ','e','r','a','t',',',' ','p','e','l','l','e','n','t','e','s','q','u','e',' ','e','t',' ','n','i','b','h',' ','q','u','i','s',',',' ','i','n','t','e','r','d','u','m',' ','t','e','m','p','o','r',' ','o','d','i','o','.',' ','E','t','i','a','m',' ','s','a','p','i','e','n',' ','s','a','p','i','e','n',',',' ','a','l','i','q','u','a','m',' ','u','t',' ','a','l','i','q','u','a','m',' ','a','t',',',' ','s','a','g','i','t','t','i','s',' ','e','u',' ','m','a','g','n','a','.',' ','M','a','e','c','e','n','a','s',' ','m','a','g','n','a',' ','m','a','g','n','a',',',' ','s','u','s','c','i','p','i','t',' ','u','t',' ','l','o','r','e','m',' ','u','t',',',' ','v','a','r','i','u','s',' ','p','r','e','t','i','u','m',' ','f','e','l','i','s','.',' ','I','n','t','e','g','e','r',' ','t','i','n','c','i','d','u','n','t',',',' ','m','e','t','u','s',' ','v','e','l',' ','s','o','l','l','i','c','i','t','u','d','i','n',' ','f','i','n','i','b','u','s',',',' ','f','e','l','i','s',' ','e','r','a','t',' ','m','o','l','e','s','t','i','e',' ','u','r','n','a',',',' ','a',' ','c','o','n','d','i','m','e','n','t','u','m',' ','a','u','g','u','e',' ','a','r','c','u',' ','v','i','t','a','e',' ','r','i','s','u','s','.',' ','E','t','i','a','m',' ','i','d',' ','s','a','g','i','t','t','i','s',' ','q','u','a','m','.',' ','M','o','r','b','i',' ','a',' ','u','l','t','r','i','c','i','e','s',' ','n','u','n','c','.',' ','P','h','a','s','e','l','l','u','s',' ','e','r','o','s',' ','r','i','s','u','s',',',' ','c','u','r','s','u','s',' ','u','l','l','a','m','c','o','r','p','e','r',' ','m','a','s','s','a',' ','s','e','d',',',' ','d','i','g','n','i','s','s','i','m',' ','c','o','n','s','e','q','u','a','t',' ','l','i','g','u','l','a','.',' ','A','l','i','q','u','a','m',' ','t','u','r','p','i','s',' ','a','r','c','u',',',' ','a','c','c','u','m','s','a','n',' ','q','u','i','s',' ','s','a','p','i','e','n',' ','v','i','t','a','e',',',' ','l','a','c','i','n','i','a',' ','e','u','i','s','m','o','d',' ','n','i','s','l','.',' ','M','a','u','r','i','s',' ','i','d',' ','f','e','l','i','s',' ','s','e','m','.',0};
542     /* Last char changed from '.' to '!' */
543     static const WCHAR lipsum2[] = {'L','o','r','e','m',' ','i','p','s','u','m',' ','d','o','l','o','r',' ','s','i','t',' ','a','m','e','t',',',' ','c','o','n','s','e','c','t','e','t','u','r',' ','a','d','i','p','i','s','c','i','n','g',' ','e','l','i','t','.',' ','N','u','l','l','a',' ','a','n','t','e',' ','r','i','s','u','s',',',' ','m','a','l','e','s','u','a','d','a',' ','s','e','d',' ','i','a','c','u','l','i','s',' ','l','u','c','t','u','s',',',' ','o','r','n','a','r','e',' ','p','u','l','v','i','n','a','r',' ','v','e','l','i','t','.',' ','L','o','r','e','m',' ','i','p','s','u','m',' ','d','o','l','o','r',' ','s','i','t',' ','a','m','e','t',',',' ','c','o','n','s','e','c','t','e','t','u','r',' ','a','d','i','p','i','s','c','i','n','g',' ','e','l','i','t','.',' ','I','n','t','e','g','e','r',' ','q','u','i','s',' ','f','e','l','i','s',' ','u','t',' ','l','e','o',' ','e','l','e','i','f','e','n','d',' ','u','l','t','r','i','c','e','s',' ','f','i','n','i','b','u','s',' ','e','u',' ','d','o','l','o','r','.',' ','I','n',' ','b','i','b','e','n','d','u','m',',',' ','e','r','o','s',' ','e','u',' ','f','a','u','c','i','b','u','s',' ','c','o','n','s','e','q','u','a','t',',',' ','n','i','s','i',' ','m','a','g','n','a',' ','v','e','n','e','n','a','t','i','s',' ','j','u','s','t','o',',',' ','a','t',' ','t','r','i','s','t','i','q','u','e',' ','m','e','t','u','s',' ','d','o','l','o','r',' ','u','t',' ','r','i','s','u','s','.',' ','N','u','n','c',' ','e','u',' ','o','d','i','o',' ','d','i','g','n','i','s','s','i','m',',',' ','o','r','n','a','r','e',' ','a','n','t','e',' ','g','r','a','v','i','d','a',',',' ','l','o','b','o','r','t','i','s',' ','e','r','o','s','.',' ','C','r','a','s',' ','s','e','m',' ','e','x',',',' ','c','o','n','s','e','c','t','e','t','u','r',' ','p','u','l','v','i','n','a','r',' ','t','i','n','c','i','d','u','n','t',' ','e','u',',',' ','c','o','n','g','u','e',' ','a',' ','e','r','o','s','.',' ','C','u','r','a','b','i','t','u','r',' ','e','r','o','s',' ','e','r','a','t',',',' ','p','e','l','l','e','n','t','e','s','q','u','e',' ','e','t',' ','n','i','b','h',' ','q','u','i','s',',',' ','i','n','t','e','r','d','u','m',' ','t','e','m','p','o','r',' ','o','d','i','o','.',' ','E','t','i','a','m',' ','s','a','p','i','e','n',' ','s','a','p','i','e','n',',',' ','a','l','i','q','u','a','m',' ','u','t',' ','a','l','i','q','u','a','m',' ','a','t',',',' ','s','a','g','i','t','t','i','s',' ','e','u',' ','m','a','g','n','a','.',' ','M','a','e','c','e','n','a','s',' ','m','a','g','n','a',' ','m','a','g','n','a',',',' ','s','u','s','c','i','p','i','t',' ','u','t',' ','l','o','r','e','m',' ','u','t',',',' ','v','a','r','i','u','s',' ','p','r','e','t','i','u','m',' ','f','e','l','i','s','.',' ','I','n','t','e','g','e','r',' ','t','i','n','c','i','d','u','n','t',',',' ','m','e','t','u','s',' ','v','e','l',' ','s','o','l','l','i','c','i','t','u','d','i','n',' ','f','i','n','i','b','u','s',',',' ','f','e','l','i','s',' ','e','r','a','t',' ','m','o','l','e','s','t','i','e',' ','u','r','n','a',',',' ','a',' ','c','o','n','d','i','m','e','n','t','u','m',' ','a','u','g','u','e',' ','a','r','c','u',' ','v','i','t','a','e',' ','r','i','s','u','s','.',' ','E','t','i','a','m',' ','i','d',' ','s','a','g','i','t','t','i','s',' ','q','u','a','m','.',' ','M','o','r','b','i',' ','a',' ','u','l','t','r','i','c','i','e','s',' ','n','u','n','c','.',' ','P','h','a','s','e','l','l','u','s',' ','e','r','o','s',' ','r','i','s','u','s',',',' ','c','u','r','s','u','s',' ','u','l','l','a','m','c','o','r','p','e','r',' ','m','a','s','s','a',' ','s','e','d',',',' ','d','i','g','n','i','s','s','i','m',' ','c','o','n','s','e','q','u','a','t',' ','l','i','g','u','l','a','.',' ','A','l','i','q','u','a','m',' ','t','u','r','p','i','s',' ','a','r','c','u',',',' ','a','c','c','u','m','s','a','n',' ','q','u','i','s',' ','s','a','p','i','e','n',' ','v','i','t','a','e',',',' ','l','a','c','i','n','i','a',' ','e','u','i','s','m','o','d',' ','n','i','s','l','.',' ','M','a','u','r','i','s',' ','i','d',' ','f','e','l','i','s',' ','s','e','m','!',0};
544     static const WCHAR empty[] = {0};
545     static const WCHAR* all[] = { test1, test2, test3, test4, test5, lipsum, lipsum2, empty };
546     static const TAGID expected_str[] = { 0xc, 0x12, 0x18, 0x1e, 0x24, 0x2a, 0x30, 0x36 };
547     static const TAGID expected_tab[] = { 6, 0x18, 0x2a, 0x3c, 0x4e, 0x60, 0x846, 0x102c };
548     DWORD n, j;
549 
550     for (n = 0; n < (sizeof(all) / sizeof(all[0])); ++n)
551     {
552         PDB pdb;
553         TAGID tagstr, table, expected_table;
554 
555         write_db_strings(path1, all, n+1);
556 
557         pdb = pSdbOpenDatabase(path1, DOS_PATH);
558         ok(pdb != NULL, "Expected a valid database\n");
559         if (!pdb)
560         {
561             DeleteFileW(path1);
562             continue;
563         }
564         tagstr = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_NAME);
565         for (j = 0; j <= n; ++j)
566         {
567             ok(tagstr == expected_str[j], "Expected tagstr to be 0x%x, was 0x%x for %u/%u\n", expected_str[j], tagstr, j, n);
568             if (tagstr)
569             {
570                 LPWSTR data;
571                 DWORD size;
572                 TAG tag = pSdbGetTagFromTagID(pdb, tagstr);
573                 ok(tag == TAG_NAME, "Expected tag to be TAG_NAME, was 0x%x for %u/%u\n", tag, j, n);
574                 size = pSdbGetTagDataSize(pdb, tagstr);
575                 ok(size == 4, "Expected datasize to be 4, was %u for %u/%u\n", size, j, n);
576                 data = pSdbGetStringTagPtr(pdb, tagstr);
577                 ok(data && !wcsicmp(data, all[j]), "Expected data to be %s was %s for %u/%u\n", wine_dbgstr_w(all[j]), wine_dbgstr_w(data), j, n);
578             }
579             tagstr = pSdbFindNextTag(pdb, TAGID_ROOT, tagstr);
580         }
581         ok(tagstr == TAGID_NULL, "Expected to be at the end for %u\n", n);
582 
583 
584         table = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_STRINGTABLE);
585         expected_table = 0xc + (n+1)*6;
586         ok(table == expected_table, "Expected to find a stringtable at 0x%x instead of 0x%x for %u\n", expected_table, table, n);
587         if (table)
588         {
589             tagstr = pSdbFindFirstTag(pdb, table, TAG_STRINGTABLE_ITEM);
590             for (j = 0; j <= n; ++j)
591             {
592                 ok(tagstr == (expected_tab[j] + expected_table), "Expected tagstr to be 0x%x, was 0x%x for %u/%u\n", (expected_tab[j] + expected_table), tagstr, j, n);
593                 if (tagstr)
594                 {
595                     LPWSTR data;
596                     DWORD size, expected_size;
597                     TAG tag = pSdbGetTagFromTagID(pdb, tagstr);
598                     ok(tag == TAG_STRINGTABLE_ITEM, "Expected tag to be TAG_NAME, was 0x%x for %u/%u\n", tag, j, n);
599                     size = pSdbGetTagDataSize(pdb, tagstr);
600                     expected_size = (lstrlenW(all[j])+1) * 2;
601                     ok(size == expected_size, "Expected datasize to be %u, was %u for %u/%u\n", expected_size, size, j, n);
602                     data = pSdbGetStringTagPtr(pdb, tagstr);
603                     ok(data && !wcsicmp(data, all[j]), "Expected data to be %s was %s for %u/%u\n", wine_dbgstr_w(all[j]), wine_dbgstr_w(data), j, n);
604                 }
605                 tagstr = pSdbFindNextTag(pdb, TAGID_ROOT, tagstr);
606             }
607             ok(tagstr == TAGID_NULL, "Expected to be at the end for %u\n", n);
608         }
609 
610         pSdbCloseDatabase(pdb);
611         DeleteFileW(path1);
612     }
613 }
614 
615 static void match_strw_attr_imp(PDB pdb, TAGID parent, TAG find, const WCHAR* compare)
616 {
617     TAGID attr = pSdbFindFirstTag(pdb, parent, find);
618     winetest_ok(attr != TAG_NULL, "Could not find: %x\n", find);
619     if (attr != TAG_NULL)
620     {
621         LPWSTR name = pSdbGetStringTagPtr(pdb, attr);
622         winetest_ok(name != NULL, "Could not convert attr to str.\n");
623         if (name)
624         {
625             winetest_ok(wcscmp(name, compare) == 0, "Expected tagid %x to be %s, was %s\n", attr, wine_dbgstr_w(compare), wine_dbgstr_w(name));
626         }
627     }
628 }
629 
630 static void match_dw_attr_imp(PDB pdb, TAGID parent, TAG find, DWORD compare)
631 {
632     TAGID attr = pSdbFindFirstTag(pdb, parent, find);
633     winetest_ok(attr != TAG_NULL, "Could not find: %x\n", find);
634     if (attr != TAG_NULL)
635     {
636         DWORD val = pSdbReadDWORDTag(pdb, attr, 0x1234567);
637         winetest_ok(val == compare, "Expected tagid %x to be 0x%x, was 0x%x\n", attr, compare, val);
638     }
639 }
640 
641 static void match_qw_attr_imp(PDB pdb, TAGID parent, TAG find, QWORD compare)
642 {
643     TAGID attr = pSdbFindFirstTag(pdb, parent, find);
644     winetest_ok(attr != TAG_NULL, "Could not find: %x\n", find);
645     if (attr != TAG_NULL)
646     {
647         QWORD val = pSdbReadQWORDTag(pdb, attr, 0x123456789abcdef);
648         winetest_ok(val == compare, "Expected tagid %x to be 0x%I64x, was 0x%I64x\n", attr, compare, val);
649     }
650 }
651 
652 static void match_guid_attr_imp(PDB pdb, TAGID parent, TAG find, const GUID* compare)
653 {
654     TAGID attr = pSdbFindFirstTag(pdb, parent, find);
655     winetest_ok(attr != TAG_NULL, "Could not find: %x\n", find);
656     if (attr != TAG_NULL)
657     {
658         GUID guid = { 0 };
659         BOOL result = pSdbReadBinaryTag(pdb, attr, (PBYTE)&guid, sizeof(guid));
660         winetest_ok(result, "expected pSdbReadBinaryTag not to fail.\n");
661         winetest_ok(IsEqualGUID(guid, *compare), "expected guids to be equal(%s:%s)\n", wine_dbgstr_guid(&guid), wine_dbgstr_guid(compare));
662     }
663 }
664 
665 #define match_strw_attr  (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : match_strw_attr_imp
666 #define match_dw_attr  (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : match_dw_attr_imp
667 #define match_qw_attr  (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : match_qw_attr_imp
668 #define match_guid_attr  (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : match_guid_attr_imp
669 
670 
671 //The application name cannot contain any of the following characters:
672 // \ / < > : * ? |  "
673 
674 static void check_db_properties(PDB pdb, TAGID root)
675 {
676     TAGID iter = pSdbFindFirstTag(pdb, root, TAG_DATABASE_ID);
677     ok(iter != TAGID_NULL, "expected a result, got TAGID_NULL\n");
678     if (iter != TAGID_NULL)
679     {
680         GUID guid = { 0 }, guid2 = { 0 };
681         BOOL result = pSdbReadBinaryTag(pdb, iter, (PBYTE)&guid, sizeof(guid));
682         ok(result, "expected SdbReadBinaryTag not to fail.\n");
683         if (result)
684         {
685             WCHAR guid_wstr[50];
686             result = pSdbGUIDToString(&guid, guid_wstr, 50);
687             ok(result, "expected SdbGUIDToString not to fail.\n");
688             if (result)
689             {
690                 char guid_str[50];
691                 WideCharToMultiByte(CP_ACP, 0, guid_wstr, -1, guid_str, sizeof(guid_str), NULL, NULL);
692                 ok_str(guid_str, "{e39b0eb0-55db-450b-9bd4-d20c9484260f}");
693             }
694             ok(pSdbGetDatabaseID(pdb, &guid2), "expected SdbGetDatabaseID not to fail.\n");
695             ok(IsEqualGUID(guid, guid2), "expected guids to be equal(%s:%s)\n", wine_dbgstr_guid(&guid), wine_dbgstr_guid(&guid2));
696         }
697     }
698     match_qw_attr(pdb, root, TAG_TIME, 0x1d1b91a02c0d63e);
699     match_strw_attr(pdb, root, TAG_COMPILER_VERSION, L"2.1.0.3");
700     match_strw_attr(pdb, root, TAG_NAME, L"apphelp_test1");
701     match_dw_attr(pdb, root, TAG_OS_PLATFORM, 1);
702 }
703 
704 static void check_db_layer(PDB pdb, TAGID layer)
705 {
706     TAGID shimref, inexclude, is_include;
707     ok(layer != TAGID_NULL, "Expected a valid layer, got NULL\n");
708     if (!layer)
709         return;
710 
711     match_strw_attr(pdb, layer, TAG_NAME, L"TestNewMode");
712     shimref = pSdbFindFirstTag(pdb, layer, TAG_SHIM_REF);
713     ok(shimref != TAGID_NULL, "Expected a valid shim ref, got NULL\n");
714     if (!shimref)
715         return;
716 
717     match_strw_attr(pdb, shimref, TAG_NAME, L"VirtualRegistry");
718     match_strw_attr(pdb, shimref, TAG_COMMAND_LINE, L"ThemeActive");
719     inexclude = pSdbFindFirstTag(pdb, shimref, TAG_INEXCLUD);
720     ok(inexclude != TAGID_NULL, "Expected a valid in/exclude ref, got NULL\n");
721     if (!inexclude)
722         return;
723 
724     is_include = pSdbFindFirstTag(pdb, inexclude, TAG_INCLUDE);
725     ok(is_include == TAGID_NULL, "Expected a NULL include ref, but got one anyway.\n");
726     match_strw_attr(pdb, inexclude, TAG_MODULE, L"exclude.dll");
727 
728     inexclude = pSdbFindNextTag(pdb, shimref, inexclude);
729     ok(inexclude != TAGID_NULL, "Expected a valid in/exclude ref, got NULL\n");
730     if (!inexclude)
731         return;
732 
733     is_include = pSdbFindFirstTag(pdb, inexclude, TAG_INCLUDE);
734     ok(is_include != TAGID_NULL, "Expected a valid include ref, got NULL\n");
735     match_strw_attr(pdb, inexclude, TAG_MODULE, L"include.dll");
736 }
737 
738 static void check_matching_file(PDB pdb, TAGID exe, TAGID matching_file, int num)
739 {
740     ok(matching_file != TAGID_NULL, "Expected to find atleast 1 matching file.\n");
741     if (matching_file == TAGID_NULL)
742         return;
743 
744     ok(num < 4, "Too many matches, expected only 4!\n");
745     if (num >= 4)
746         return;
747 
748 
749     match_strw_attr(pdb, matching_file, TAG_NAME, L"*");
750     match_strw_attr(pdb, matching_file, TAG_COMPANY_NAME, L"CompanyName");
751     match_strw_attr(pdb, matching_file, TAG_PRODUCT_NAME, L"ProductName");
752     match_strw_attr(pdb, matching_file, TAG_PRODUCT_VERSION, L"1.0.0.1");
753     match_strw_attr(pdb, matching_file, TAG_FILE_VERSION, L"1.0.0.0");
754 
755     if (num == 0 || num == 3)
756     {
757         match_qw_attr(pdb, matching_file, TAG_UPTO_BIN_PRODUCT_VERSION, 0x1000000000001);
758         match_qw_attr(pdb, matching_file, TAG_UPTO_BIN_FILE_VERSION, 0x1000000000000);
759     }
760     if (num == 1 || num == 3)
761     {
762         match_dw_attr(pdb, matching_file, TAG_PE_CHECKSUM, 0xbaad);
763     }
764     if (num != 0)
765     {
766         match_qw_attr(pdb, matching_file, TAG_BIN_PRODUCT_VERSION, 0x1000000000001);
767         match_qw_attr(pdb, matching_file, TAG_BIN_FILE_VERSION, 0x1000000000000);
768     }
769     if (num == 3)
770     {
771         match_dw_attr(pdb, matching_file, TAG_SIZE, 0x800);
772         match_dw_attr(pdb, matching_file, TAG_CHECKSUM, 0x178bd629);
773         match_strw_attr(pdb, matching_file, TAG_FILE_DESCRIPTION, L"FileDescription");
774         match_dw_attr(pdb, matching_file, TAG_MODULE_TYPE, 3);
775         match_dw_attr(pdb, matching_file, TAG_VERFILEOS, 4);
776         match_dw_attr(pdb, matching_file, TAG_VERFILETYPE, 1);
777         match_dw_attr(pdb, matching_file, TAG_LINKER_VERSION, 0x40002);
778         match_strw_attr(pdb, matching_file, TAG_ORIGINAL_FILENAME, L"OriginalFilename");
779         match_strw_attr(pdb, matching_file, TAG_INTERNAL_NAME, L"InternalName");
780         match_strw_attr(pdb, matching_file, TAG_LEGAL_COPYRIGHT, L"LegalCopyright");
781         match_dw_attr(pdb, matching_file, TAG_LINK_DATE, 0x12345);
782         match_dw_attr(pdb, matching_file, TAG_UPTO_LINK_DATE, 0x12345);
783     }
784     if (num > 3)
785     {
786         ok(0, "unknown case: %d\n", num);
787     }
788     matching_file = pSdbFindNextTag(pdb, exe, matching_file);
789     if (num == 2)
790     {
791         ok(matching_file != TAGID_NULL, "Did expect a secondary match on %d\n", num);
792         match_strw_attr(pdb, matching_file, TAG_NAME, L"test_checkfile.txt");
793         match_dw_attr(pdb, matching_file, TAG_SIZE, 0x4);
794         match_dw_attr(pdb, matching_file, TAG_CHECKSUM, 0xb0b0b0b0);
795     }
796     else
797     {
798         ok(matching_file == TAGID_NULL, "Did not expect a secondary match on %d\n", num);
799     }
800 }
801 
802 // "C:\WINDOWS\system32\pcaui.exe" /g {bf39e0e6-c61c-4a22-8802-3ea8ad00b655} /x {4e50c93f-b863-4dfa-bae2-d80ef4ce5c89} /a "apphelp_name_allow" /v "apphelp_vendor_allow" /s "Allow it!" /b 1 /f 0 /k 0 /e "C:\Users\Mark\AppData\Local\Temp\apphelp_test\test_allow.exe" /u "http://reactos.org/allow" /c
803 // "C:\WINDOWS\system32\pcaui.exe" /g {fa150915-1244-4169-a4ba-fc098c442840} /x {156720e1-ef98-4d04-965a-d85de05e6d9f} /a "apphelp_name_disallow" /v "apphelp_vendor_disallow" /s "Not allowed!" /b 2 /f 0 /k 0 /e "C:\Users\Mark\AppData\Local\Temp\apphelp_test\test_disallow.exe" /u "http://reactos.org/disallow" /c
804 
805 static void check_matching_apphelp(PDB pdb, TAGID apphelp, int num)
806 {
807     if (num == 0)
808     {
809 /*
810 [Window Title]
811 Program Compatibility Assistant
812 
813 [Main Instruction]
814 This program has known compatibility issues
815 
816 [Expanded Information]
817 Allow it!
818 
819 [^] Hide details  [ ] Don't show this message again  [Check for solutions online] [Run program] [Cancel]
820 */
821         match_dw_attr(pdb, apphelp, TAG_FLAGS, 1);
822         match_dw_attr(pdb, apphelp, TAG_PROBLEMSEVERITY, 1);
823         match_dw_attr(pdb, apphelp, TAG_HTMLHELPID, 1);
824         match_dw_attr(pdb, apphelp, TAG_APP_NAME_RC_ID, 0x6f0072);
825         match_dw_attr(pdb, apphelp, TAG_VENDOR_NAME_RC_ID, 0x720067);
826         match_dw_attr(pdb, apphelp, TAG_SUMMARY_MSG_RC_ID, 0);
827     }
828     else
829     {
830 /*
831 [Window Title]
832 Program Compatibility Assistant
833 
834 [Main Instruction]
835 This program is blocked due to compatibility issues
836 
837 [Expanded Information]
838 Not allowed!
839 
840 [^] Hide details  [Check for solutions online] [Cancel]
841 */
842         match_dw_attr(pdb, apphelp, TAG_FLAGS, 1);
843         match_dw_attr(pdb, apphelp, TAG_PROBLEMSEVERITY, 2);
844         match_dw_attr(pdb, apphelp, TAG_HTMLHELPID, 2);
845         match_dw_attr(pdb, apphelp, TAG_APP_NAME_RC_ID, 0x320020);
846         match_dw_attr(pdb, apphelp, TAG_VENDOR_NAME_RC_ID, 0x38002e);
847         match_dw_attr(pdb, apphelp, TAG_SUMMARY_MSG_RC_ID, 0);
848     }
849     apphelp = pSdbFindNextTag(pdb, apphelp, apphelp);
850     ok(apphelp == TAGID_NULL, "Did not expect a secondary match on %d\n", num);
851 }
852 
853 static void check_matching_layer(PDB pdb, TAGID layer, int num)
854 {
855     if (num == 2)
856     {
857         match_dw_attr(pdb, layer, TAG_LAYER_TAGID, 0x18e);
858         match_strw_attr(pdb, layer, TAG_NAME, L"TestNewMode");
859     }
860     else
861     {
862         TAGID layer_tagid = pSdbFindFirstTag(pdb, layer, TAG_LAYER_TAGID);
863         ok(layer_tagid == TAGID_NULL, "expected not to find a layer tagid, got %x\n", layer_tagid);
864         match_strw_attr(pdb, layer, TAG_NAME, L"WinSrv03");
865     }
866 }
867 
868 static struct
869 {
870     const WCHAR* name;
871     const WCHAR* app_name;
872     const WCHAR* vendor;
873     GUID exe_id;
874     const WCHAR* extra_file;
875     DWORD dwLayerCount;
876     TAGREF atrExes_0;
877     DWORD adwExeFlags_0;
878     TAGREF atrLayers_0;
879     TAGREF trApphelp;
880     const char* env_var;
881 } test_exedata[5] = {
882     {
883         L"test_allow.exe",
884         L"apphelp_name_allow",
885         L"apphelp_vendor_allow",
886         { 0x4e50c93f, 0xb863, 0x4dfa, { 0xba, 0xe2, 0xd8, 0x0e, 0xf4, 0xce, 0x5c, 0x89 } },
887         NULL,
888         0,
889         0x1c6,
890         0x1000,
891         0,
892         0x1c6,
893         NULL,
894     },
895     {
896         L"test_disallow.exe",
897         L"apphelp_name_disallow",
898         L"apphelp_vendor_disallow",
899         { 0x156720e1, 0xef98, 0x4d04, { 0x96, 0x5a, 0xd8, 0x5d, 0xe0, 0x5e, 0x6d, 0x9f } },
900         NULL,
901         0,
902         0x256,
903         0x3000,
904         0,
905         0x256,
906         NULL,
907     },
908     {
909         L"test_new.exe",
910         L"fixnew_name",
911         L"fixnew_vendor",
912         { 0xce70ef69, 0xa21d, 0x408b, { 0x84, 0x5b, 0xf9, 0x9e, 0xac, 0x06, 0x09, 0xe7 } },
913         L"test_checkfile.txt",
914         1,
915         0x2ec,
916         0,
917         0x18e,
918         0,
919         NULL,
920     },
921     {
922         L"test_w2k3.exe",
923         L"fix_name",
924         L"fix_vendor",
925         { 0xb4ead144, 0xf640, 0x4e4b, { 0x94, 0xc4, 0x0c, 0x7f, 0xa8, 0x66, 0x23, 0xb0 } },
926         NULL,
927         0,
928         0x37c,
929         0,
930         0,
931         0,
932         NULL,
933     },
934     {
935         L"test_unknown_file.exe",
936         L"apphelp_name_allow",
937         L"apphelp_vendor_allow",
938         { 0x00000000, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
939         NULL,
940         1,
941         0,
942         0,
943         0x18e,
944         0,
945         "TestNewMode",
946     },
947 };
948 
949 static void check_db_exes(PDB pdb, TAGID root)
950 {
951     int num = 0;
952     TAGID exe = pSdbFindFirstTag(pdb, root, TAG_EXE);
953     TAGID altExe = pSdbFindFirstNamedTag(pdb, root, TAG_EXE, TAG_NAME, L"test_allow.exe");
954     ok_hex(altExe, (int)exe);
955     while (exe != TAGID_NULL)
956     {
957         TAGID apphelp, layer;
958         ok(num < 4, "Too many matches, expected only 4!\n");
959         if (num >= 4)
960             break;
961         match_strw_attr(pdb, exe, TAG_NAME, test_exedata[num].name);
962         match_strw_attr(pdb, exe, TAG_APP_NAME, test_exedata[num].app_name);
963         match_strw_attr(pdb, exe, TAG_VENDOR, test_exedata[num].vendor);
964         match_guid_attr(pdb, exe, TAG_EXE_ID, &test_exedata[num].exe_id);
965         check_matching_file(pdb, exe, pSdbFindFirstTag(pdb, exe, TAG_MATCHING_FILE), num);
966         apphelp = pSdbFindFirstTag(pdb, exe, TAG_APPHELP);
967         if (num == 0 || num == 1)
968         {
969             ok(apphelp != TAGID_NULL, "Expected to find a valid apphelp match on %d.\n", num);
970             if (apphelp)
971                 check_matching_apphelp(pdb, apphelp, num);
972         }
973         else
974         {
975             ok(apphelp == TAGID_NULL, "Did not expect an apphelp match on %d\n", num);
976         }
977         layer = pSdbFindFirstTag(pdb, exe, TAG_LAYER);
978         if (num == 2 || num == 3)
979         {
980             ok(layer != TAGID_NULL, "Expected to find a valid layer match on %d.\n", num);
981             if (layer)
982                 check_matching_layer(pdb, layer, num);
983         }
984         else
985         {
986             ok(layer == TAGID_NULL, "Did not expect a layer match on %d\n", num);
987         }
988         ++num;
989         exe = pSdbFindNextTag(pdb, root, exe);
990     }
991     ok(num == 4, "Expected to find 4 exe tags, found: %d\n", num);
992 }
993 
994 static struct
995 {
996     DWORD htmlhelpid;
997     const WCHAR* link;
998     const WCHAR* apphelp_title;
999     const WCHAR* apphelp_details;
1000 } test_layerdata[2] = {
1001     {
1002         2,
1003         L"http://reactos.org/disallow",
1004         L"apphelp_name_disallow",
1005         L"Not allowed!",
1006     },
1007     {
1008         1,
1009         L"http://reactos.org/allow",
1010         L"apphelp_name_allow",
1011         L"Allow it!",
1012     },
1013 };
1014 
1015 static void check_db_apphelp(PDB pdb, TAGID root)
1016 {
1017     int num = 0;
1018     TAGID apphelp = pSdbFindFirstTag(pdb, root, TAG_APPHELP);
1019     while (apphelp != TAGID_NULL)
1020     {
1021         TAGID link;
1022         ok(num < 2, "Too many matches, expected only 4!\n");
1023         if (num >= 2)
1024             break;
1025         match_dw_attr(pdb, apphelp, TAG_HTMLHELPID, test_layerdata[num].htmlhelpid);
1026         link = pSdbFindFirstTag(pdb, apphelp, TAG_LINK);
1027         ok(link != TAGID_NULL, "expected to find a link tag\n");
1028         if (link != TAGID_NULL)
1029         {
1030             match_strw_attr(pdb, link, TAG_LINK_URL, test_layerdata[num].link);
1031         }
1032         match_strw_attr(pdb, apphelp, TAG_APPHELP_TITLE, test_layerdata[num].apphelp_title);
1033         match_strw_attr(pdb, apphelp, TAG_APPHELP_DETAILS, test_layerdata[num].apphelp_details);
1034         apphelp = pSdbFindNextTag(pdb, root, apphelp);
1035         num++;
1036     }
1037     ok(num == 2, "Expected to find 2 layer tags, found: %d\n", num);
1038 }
1039 
1040 static void test_GetDatabaseInformation(PDB pdb)
1041 {
1042     PDB_INFORMATION pInfo;
1043     BOOL fResult;
1044 
1045     if (!pSdbGetDatabaseInformation || !pSdbFreeDatabaseInformation)
1046     {
1047         skip("GetDatabaseInformation or SdbFreeDatabaseInformation not found\n");
1048         return;
1049     }
1050 
1051     _SEH2_TRY
1052     {
1053         pSdbFreeDatabaseInformation(NULL);
1054     }
1055     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1056     {
1057         ok(0, "SdbFreeDatabaseInformation did not handle a NULL pointer very gracefully.\n");
1058     }
1059     _SEH2_END;
1060 
1061 
1062     pInfo = (PDB_INFORMATION)malloc(sizeof(*pInfo) * 4);
1063     memset(pInfo, 0xDE, sizeof(*pInfo) * 2);
1064 
1065     fResult = pSdbGetDatabaseInformation(pdb, pInfo);
1066     ok(fResult, "SdbGetDatabaseInformation failed\n");
1067     if (fResult)
1068     {
1069         ok_int(pInfo->dwSomething, 1);
1070         ok(IsEqualGUID(GUID_DATABASE_TEST, pInfo->Id), "expected guids to be equal(%s:%s)\n",
1071            wine_dbgstr_guid(&GUID_DATABASE_TEST), wine_dbgstr_guid(&pInfo->Id));
1072         ok(wcscmp(pInfo->Description, L"apphelp_test1") == 0,
1073            "Expected pInfo->Description to be 'apphelp_test1', was %s\n", wine_dbgstr_w(pInfo->Description));
1074 
1075         /* Struct is slightly bigger on some Win10, and the DB version nr is different on all */
1076         if (g_WinVersion >= WINVER_WIN10)
1077         {
1078             ok(pInfo->dwMajor == 3, "Expected pInfo->dwMajor to be 3, was: %d\n", pInfo->dwMajor);
1079             ok(pInfo->dwMinor == 0, "Expected pInfo->dwMinor to be 0, was: %d\n", pInfo->dwMinor);
1080 
1081             ok(pInfo[1].dwSomething == 4 || pInfo[1].dwSomething == 0xdededede, "Something amiss: 0x%x\n", pInfo[1].dwSomething);
1082             ok(pInfo[1].dwMajor == 0xdededede, "Cookie2 corrupt: 0x%x\n", pInfo[1].dwMajor);
1083         }
1084         else
1085         {
1086             ok(pInfo->dwMajor == 2, "Expected pInfo->dwMajor to be 2, was: %d\n", pInfo->dwMajor);
1087             ok(pInfo->dwMinor == 1, "Expected pInfo->dwMinor to be 1, was: %d\n", pInfo->dwMinor);
1088 
1089             ok(pInfo[1].dwSomething == 0xdededede, "Cookie1 corrupt: 0x%x\n", pInfo[1].dwSomething);
1090             ok(pInfo[1].dwMajor == 0xdededede, "Cookie2 corrupt: 0x%x\n", pInfo[1].dwMajor);
1091         }
1092 
1093     }
1094     free(pInfo);
1095 }
1096 
1097 static void test_CheckDatabaseManually(void)
1098 {
1099     static const WCHAR path[] = {'t','e','s','t','_','d','b','.','s','d','b',0};
1100     TAGID root;
1101     PDB pdb;
1102     BOOL ret;
1103     DWORD ver_hi, ver_lo;
1104 
1105     test_create_db(L"test_db.sdb", g_WinVersion >= WINVER_WIN10);
1106 
1107     /* both ver_hi and ver_lo cannot be null, it'll crash. */
1108     ver_hi = ver_lo = 0x12345678;
1109     ret = pSdbGetDatabaseVersion(path, &ver_hi, &ver_lo);
1110     ok(ret, "Expected SdbGetDatabaseVersion to succeed\n");
1111     if (g_WinVersion >= WINVER_WIN10)
1112     {
1113         ok(ver_hi == 3, "Expected ver_hi to be 3, was: %d\n", ver_hi);
1114         ok(ver_lo == 0, "Expected ver_lo to be 0, was: %d\n", ver_lo);
1115     }
1116     else
1117     {
1118         ok(ver_hi == 2, "Expected ver_hi to be 2, was: %d\n", ver_hi);
1119         ok(ver_lo == 1, "Expected ver_lo to be 1, was: %d\n", ver_lo);
1120     }
1121 
1122     ver_hi = ver_lo = 0x12345678;
1123     ret = pSdbGetDatabaseVersion(NULL, &ver_hi, &ver_lo);
1124     if (g_WinVersion >= WINVER_WIN10)
1125     {
1126         ok(!ret, "Expected SdbGetDatabaseVersion to fail\n");
1127         ok(ver_hi == 0, "Expected ver_hi to be 0, was: 0x%x\n", ver_hi);
1128         ok(ver_lo == 0, "Expected ver_lo to be 0, was: 0x%x\n", ver_lo);
1129     }
1130     else
1131     {
1132         ok(ret, "Expected SdbGetDatabaseVersion to succeed\n");
1133         ok(ver_hi == 0x12345678, "Expected ver_hi to be 0x12345678, was: 0x%x\n", ver_hi);
1134         ok(ver_lo == 0x12345678, "Expected ver_lo to be 0x12345678, was: 0x%x\n", ver_lo);
1135     }
1136 
1137     ver_hi = ver_lo = 0x12345678;
1138     ret = pSdbGetDatabaseVersion(path + 1, &ver_hi, &ver_lo);
1139     if (g_WinVersion >= WINVER_WIN10)
1140     {
1141         ok(!ret, "Expected SdbGetDatabaseVersion to fail\n");
1142         ok(ver_hi == 0, "Expected ver_hi to be 0, was: 0x%x\n", ver_hi);
1143         ok(ver_lo == 0, "Expected ver_lo to be 0, was: 0x%x\n", ver_lo);
1144     }
1145     else
1146     {
1147         ok(ret, "Expected SdbGetDatabaseVersion to succeed\n");
1148         ok(ver_hi == 0x12345678, "Expected ver_hi to be 0x12345678, was: 0x%x\n", ver_hi);
1149         ok(ver_lo == 0x12345678, "Expected ver_lo to be 0x12345678, was: 0x%x\n", ver_lo);
1150     }
1151 
1152     pdb = pSdbOpenDatabase(path, DOS_PATH);
1153     ok(pdb != NULL, "unexpected NULL handle\n");
1154 
1155     root = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
1156     ok(root != TAGID_NULL, "expected to find a root tag\n");
1157     if (root != TAGID_NULL)
1158     {
1159         TAGID tagLayer = pSdbFindFirstTag(pdb, root, TAG_LAYER);
1160         TAGID tagAlt = pSdbFindFirstNamedTag(pdb, root, TAG_LAYER, TAG_NAME, L"TestNewMode");
1161         TAGID tagAlt2 = pSdbFindFirstNamedTag(pdb, root, TAG_LAYER, TAG_NAME, L"TESTNEWMODE");
1162         TAGID tagAlt3 = pSdbFindFirstNamedTag(pdb, root, TAG_LAYER, TAG_NAME, L"testnewmode");
1163         ok_hex(tagLayer, (int)tagAlt);
1164         ok_hex(tagLayer, (int)tagAlt2);
1165         ok_hex(tagLayer, (int)tagAlt3);
1166         check_db_properties(pdb, root);
1167         check_db_layer(pdb, tagLayer);
1168         check_db_exes(pdb, root);
1169         check_db_apphelp(pdb, root);
1170     }
1171     test_GetDatabaseInformation(pdb);
1172 
1173     pSdbCloseDatabase(pdb);
1174     DeleteFileA("test_db.sdb");
1175 }
1176 
1177 static void test_is_testdb(PDB pdb)
1178 {
1179     if (pdb)
1180     {
1181         GUID guid;
1182         memset(&guid, 0, sizeof(guid));
1183         ok(pSdbGetDatabaseID(pdb, &guid), "expected SdbGetDatabaseID not to fail.\n");
1184         ok(IsEqualGUID(guid, GUID_DATABASE_TEST), "Expected SdbGetDatabaseID to return the test db GUID, was: %s\n", wine_dbgstr_guid(&guid));
1185     }
1186     else
1187     {
1188         skip("Not checking DB GUID, received a null pdb\n");
1189     }
1190 }
1191 
1192 static BOOL IsUserAdmin()
1193 {
1194     BOOL Result;
1195     SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY };
1196     PSID AdministratorsGroup;
1197 
1198     Result = AllocateAndInitializeSid(&NtAuthority, 2,
1199                                       SECURITY_BUILTIN_DOMAIN_RID,
1200                                       DOMAIN_ALIAS_RID_ADMINS,
1201                                       0, 0, 0, 0, 0, 0,
1202                                       &AdministratorsGroup);
1203     if (Result)
1204     {
1205         if (!CheckTokenMembership( NULL, AdministratorsGroup, &Result))
1206             Result = FALSE;
1207         FreeSid(AdministratorsGroup);
1208     }
1209 
1210     return Result;
1211 }
1212 
1213 
1214 template<typename SDBQUERYRESULT_T>
1215 static void check_adwExeFlags(DWORD adwExeFlags_0, SDBQUERYRESULT_T& query, const char* file, int line, size_t cur)
1216 {
1217     ok_(file, line)(query.adwExeFlags[0] == adwExeFlags_0, "Expected adwExeFlags[0] to be 0x%x, was: 0x%x for %d\n", adwExeFlags_0, query.adwExeFlags[0], cur);
1218     for (size_t n = 1; n < _countof(query.atrExes); ++n)
1219         ok_(file, line)(query.adwExeFlags[n] == 0, "Expected adwExeFlags[%d] to be 0, was: %x for %d\n", n, query.adwExeFlags[0], cur);
1220 }
1221 
1222 template<>
1223 void check_adwExeFlags(DWORD, SDBQUERYRESULT_2k3&, const char*, int, size_t)
1224 {
1225 }
1226 
1227 
1228 template<typename SDBQUERYRESULT_T>
1229 static void test_mode_generic(const WCHAR* workdir, HSDB hsdb, size_t cur)
1230 {
1231     WCHAR exename[MAX_PATH], testfile[MAX_PATH];
1232     BOOL ret;
1233     SDBQUERYRESULT_T query;
1234     PDB pdb;
1235     TAGID tagid;
1236     TAGREF trApphelp;
1237     DWORD expect_flags = 0, adwExeFlags_0, exe_count;
1238     UNICODE_STRING exenameNT;
1239 
1240     memset(&query, 0xab, sizeof(query));
1241 
1242     swprintf(exename, L"%s\\%s", workdir, test_exedata[cur].name);
1243     if (test_exedata[cur].extra_file)
1244         swprintf(testfile, L"%s\\%s", workdir, test_exedata[cur].extra_file);
1245     test_create_exe(exename, 0);
1246 
1247     if (test_exedata[cur].extra_file)
1248     {
1249         /* First we try without the file at all. */
1250         DeleteFileW(testfile);
1251         ret = pSdbGetMatchingExe(hsdb, exename, NULL, NULL, 0, (SDBQUERYRESULT_VISTA*)&query);
1252         ok(ret == 0, "SdbGetMatchingExe should have failed for %d.\n", cur);
1253         /* Now re-try with the correct file */
1254         test_create_file(testfile, "aaaa", 4);
1255     }
1256 
1257 #if 0
1258     // Results seem to be cached based on filename, until we can invalidate this, do not test the same filename twice!
1259     DeleteFileW(exename);
1260     // skip exports
1261     test_create_exe(exename, 1);
1262     ret = pSdbGetMatchingExe(hsdb, exenameW, NULL, NULL, 0, &query);
1263     ok(ret == 0, "SdbGetMatchingExe should have failed for %d.\n", cur);
1264 
1265     DeleteFileW(exename);
1266     test_create_exe(exename, 0);
1267 #endif
1268 
1269     if (test_exedata[cur].env_var)
1270     {
1271         SetEnvironmentVariableA("__COMPAT_LAYER", test_exedata[cur].env_var);
1272     }
1273 
1274     ret = pSdbGetMatchingExe(hsdb, exename, NULL, NULL, 0, (SDBQUERYRESULT_VISTA*)&query);
1275     ok(ret, "SdbGetMatchingExe should not fail for %d.\n", cur);
1276 
1277     exe_count = (test_exedata[cur].env_var == NULL) ? 1 : 0;
1278 
1279     ok(query.dwExeCount == exe_count, "Expected dwExeCount to be %d, was %d for %d\n", exe_count, query.dwExeCount, cur);
1280     ok(query.dwLayerCount == test_exedata[cur].dwLayerCount, "Expected dwLayerCount to be %d, was %d for %d\n", test_exedata[cur].dwLayerCount, query.dwLayerCount, cur);
1281     ok(query.dwCustomSDBMap == 1, "Expected dwCustomSDBMap to be 1, was %d for %d\n", query.dwCustomSDBMap, cur);
1282     ok(query.dwLayerFlags == 0, "Expected dwLayerFlags to be 0, was 0x%x for %d\n", query.dwLayerFlags, cur);
1283     trApphelp = (g_WinVersion < WINVER_WIN10) ? 0 : test_exedata[cur].trApphelp;
1284     ok(query.trApphelp == trApphelp, "Expected trApphelp to be 0x%x, was 0x%x for %d\n", trApphelp, query.trApphelp, cur);
1285 
1286     if (g_WinVersion < WINVER_WIN7)
1287         expect_flags = 0;
1288     else if (g_WinVersion < WINVER_WIN8)
1289         expect_flags = 1;
1290     else if (g_WinVersion < WINVER_WIN10)
1291         expect_flags = 0x101;
1292     else
1293     {
1294         expect_flags = 0x121;   /* for 2 and 3, this becomes 101 when not elevated. */
1295         if ((cur == 2 || cur == 3) && !IsUserAdmin())
1296             expect_flags &= ~0x20;
1297     }
1298 
1299     if (test_exedata[cur].env_var)
1300         expect_flags &= ~0x100;
1301 
1302     ok(query.dwFlags == expect_flags, "Expected dwFlags to be 0x%x, was 0x%x for %d\n", expect_flags, query.dwFlags, cur);
1303 
1304     ok(query.atrExes[0] == test_exedata[cur].atrExes_0, "Expected atrExes[0] to be 0x%x, was: 0x%x for %d\n", test_exedata[cur].atrExes_0, query.atrExes[0], cur);
1305     for (size_t n = 1; n < _countof(query.atrExes); ++n)
1306         ok(query.atrExes[n] == 0, "Expected atrExes[%d] to be 0, was: %x for %d\n", n, query.atrExes[n], cur);
1307 
1308     adwExeFlags_0 = (g_WinVersion < WINVER_WIN10) ? 0 : test_exedata[cur].adwExeFlags_0;
1309     check_adwExeFlags(adwExeFlags_0, query, __FILE__, __LINE__, cur);
1310 
1311     ok(query.atrLayers[0] == test_exedata[cur].atrLayers_0, "Expected atrLayers[0] to be 0x%x, was: %x for %d\n", test_exedata[cur].atrLayers_0, query.atrLayers[0], cur);
1312     for (size_t n = 1; n < _countof(query.atrLayers); ++n)
1313         ok(query.atrLayers[n] == 0, "Expected atrLayers[%d] to be 0, was: %x for %d\n", n, query.atrLayers[0], cur);
1314 
1315     if (g_WinVersion >= WINVER_VISTA)
1316         ok(IsEqualGUID(query.rgGuidDB[0], GUID_DATABASE_TEST), "Expected rgGuidDB[0] to be the test db GUID, was: %s for %d\n", wine_dbgstr_guid(&query.rgGuidDB[0]), cur);
1317     else
1318         ok(IsEqualGUID(query.rgGuidDB[0], GUID_MAIN_DATABASE), "Expected rgGuidDB[0] to be the main db GUID, was: %s for %d\n", wine_dbgstr_guid(&query.rgGuidDB[0]), cur);
1319     for (size_t n = 1; n < _countof(query.rgGuidDB); ++n)
1320         ok(IsEqualGUID(query.rgGuidDB[n], GUID_NULL), "Expected rgGuidDB[%d] to be GUID_NULL, was: %s for %d\n", n, wine_dbgstr_guid(&query.rgGuidDB[n]), cur);
1321 
1322     if (query.atrExes[0])
1323     {
1324         pdb = (PDB)0x12345678;
1325         tagid = 0x76543210;
1326         ret = pSdbTagRefToTagID(hsdb, query.atrExes[0], &pdb, &tagid);
1327         ok(ret, "SdbTagRefToTagID failed for %d.\n", cur);
1328         ok(pdb != NULL && pdb != (PDB)0x12345678, "SdbTagRefToTagID failed to return a pdb for %d.\n", cur);
1329         ok(tagid != 0 && tagid != 0x76543210, "SdbTagRefToTagID failed to return a tagid for %d.\n", cur);
1330 
1331         if (pdb && pdb != (PDB)0x12345678)
1332         {
1333             TAGREF tr = 0x12345678;
1334             TAG tag = pSdbGetTagFromTagID(pdb, tagid);
1335             test_is_testdb(pdb);
1336             ok(tag == TAG_EXE, "Expected tag to be TAG_EXE, was 0x%x for %d.\n", tag, cur);
1337             match_strw_attr(pdb, tagid, TAG_NAME, test_exedata[cur].name);
1338 
1339             /* And back again */
1340             ret = pSdbTagIDToTagRef(hsdb, pdb, tagid, &tr);
1341             ok(ret, "SdbTagIDToTagRef failed for %d.\n", cur);
1342             ok(tr == query.atrExes[0], "Expected tr to be 0x%x, was 0x%x for %d.\n", query.atrExes[0], tr, cur);
1343         }
1344         else
1345         {
1346             skip("Skipping a bunch of tests because of an invalid pointer\n");
1347         }
1348     }
1349 
1350     if (test_exedata[cur].atrLayers_0)
1351     {
1352         pdb = (PDB)0x12345678;
1353         tagid = 0x76543210;
1354         ret = pSdbTagRefToTagID(hsdb, query.atrLayers[0], &pdb, &tagid);
1355         ok(ret, "SdbTagRefToTagID failed for %d.\n", cur);
1356         ok(pdb != NULL && pdb != (PDB)0x12345678, "SdbTagRefToTagID failed to return a pdb for %d.\n", cur);
1357         ok(tagid != 0 && tagid != 0x76543210, "SdbTagRefToTagID failed to return a tagid for %d.\n", cur);
1358 
1359         if (pdb && pdb != (PDB)0x12345678)
1360         {
1361             TAGREF tr = 0x12345678;
1362             TAG tag = pSdbGetTagFromTagID(pdb, tagid);
1363             test_is_testdb(pdb);
1364             ok(tag == TAG_LAYER, "Expected tag to be TAG_LAYER, was 0x%x for %d.\n", tag, cur);
1365             match_strw_attr(pdb, tagid, TAG_NAME, L"TestNewMode");
1366 
1367             /* And back again */
1368             ret = pSdbTagIDToTagRef(hsdb, pdb, tagid, &tr);
1369             ok(ret, "SdbTagIDToTagRef failed for %d.\n", cur);
1370             ok(tr == test_exedata[cur].atrLayers_0, "Expected tr to be 0x%x, was 0x%x for %d.\n", test_exedata[cur].atrLayers_0, tr, cur);
1371         }
1372         else
1373         {
1374             skip("Skipping a bunch of tests because of an invalid pointer\n");
1375         }
1376     }
1377 
1378     pdb = (PDB)0x12345678;
1379     tagid = 0x76543210;
1380     ret = pSdbTagRefToTagID(hsdb, 0, &pdb, &tagid);
1381     ok(pdb != NULL && pdb != (PDB)0x12345678, "Expected pdb to be set to a valid pdb, was: %p\n", pdb);
1382     ok(tagid == 0, "Expected tagid to be set to 0, was: 0x%x\n", tagid);
1383 
1384 
1385 
1386     if (RtlDosPathNameToNtPathName_U(exename, &exenameNT, NULL, NULL))
1387     {
1388         /*
1389         ERROR,AslPathGetLongFileNameLongpath,110,Long path conversion failed 123 [c0000001]
1390         ERROR,AslPathBuildSignatureLongpath,1086,AslPathGetLongFileNameLongpath failed for \??\C:\Users\MARK~1.DEV\AppData\Local\Temp\apphelp_test\test_allow.exe [c0000001]
1391         */
1392         ret = pSdbGetMatchingExe(hsdb, exenameNT.Buffer, NULL, NULL, 0, (SDBQUERYRESULT_VISTA*)&query);
1393         ok(!ret, "SdbGetMatchingExe should not succeed for %d.\n", cur);
1394 
1395         RtlFreeUnicodeString(&exenameNT);
1396     }
1397 
1398     if (test_exedata[cur].extra_file)
1399         DeleteFileW(testfile);
1400     DeleteFileW(exename);
1401 
1402     if (test_exedata[cur].env_var)
1403     {
1404         SetEnvironmentVariableA("__COMPAT_LAYER", NULL);
1405     }
1406 }
1407 
1408 template<typename SDBQUERYRESULT_T>
1409 static void test_MatchApplications(void)
1410 {
1411     WCHAR workdir[MAX_PATH], dbpath[MAX_PATH];
1412     BOOL ret;
1413     HSDB hsdb;
1414 
1415     ret = GetTempPathW(_countof(workdir), workdir);
1416     ok(ret, "GetTempPathW error: %d\n", GetLastError());
1417     wcscat(workdir, L"apphelp_test");
1418 
1419     ret = CreateDirectoryW(workdir, NULL);
1420     ok(ret, "CreateDirectoryW error: %d\n", GetLastError());
1421 
1422     /* SdbInitDatabase needs an nt-path */
1423     swprintf(dbpath, L"\\??\\%s\\test.sdb", workdir);
1424 
1425     test_create_db(dbpath + 4, g_WinVersion >= WINVER_WIN10);
1426 
1427     hsdb = pSdbInitDatabase(HID_DATABASE_FULLPATH, dbpath);
1428 
1429     ok(hsdb != NULL, "Expected a valid database handle\n");
1430 
1431     if (!hsdb)
1432     {
1433         skip("SdbInitDatabase not implemented?\n");
1434     }
1435     else
1436     {
1437         /* now that our enviroment is setup, let's go ahead and run the actual tests.. */
1438         size_t n;
1439         for (n = 0; n < _countof(test_exedata); ++n)
1440             test_mode_generic<SDBQUERYRESULT_T>(workdir, hsdb, n);
1441         pSdbReleaseDatabase(hsdb);
1442     }
1443 
1444     DeleteFileW(dbpath + 4);
1445 
1446     ret = RemoveDirectoryW(workdir);
1447     ok(ret, "RemoveDirectoryW error: %d\n", GetLastError());
1448 }
1449 
1450 static BOOL write_raw_file(const WCHAR* FileName, const void* Data, DWORD Size)
1451 {
1452     BOOL Success;
1453     DWORD dwWritten;
1454     HANDLE Handle = CreateFileW(FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1455 
1456     if (Handle == INVALID_HANDLE_VALUE)
1457     {
1458         skip("Failed to create temp file %ls, error %u\n", FileName, GetLastError());
1459         return FALSE;
1460     }
1461     Success = WriteFile(Handle, Data, Size, &dwWritten, NULL);
1462     ok(Success == TRUE, "WriteFile failed with %u\n", GetLastError());
1463     ok(dwWritten == Size, "WriteFile wrote %u bytes instead of %u\n", dwWritten, Size);
1464     CloseHandle(Handle);
1465     return Success && (dwWritten == Size);
1466 }
1467 
1468 static bool extract_resource(const WCHAR* Filename, LPCWSTR ResourceName)
1469 {
1470     HMODULE hMod = GetModuleHandleW(NULL);
1471     HRSRC hRsrc = FindResourceW(hMod, ResourceName, MAKEINTRESOURCEW(RT_RCDATA));
1472     ok(!!hRsrc, "Unable to find %s\n", wine_dbgstr_w(ResourceName));
1473     if (!hRsrc)
1474         return false;
1475 
1476     HGLOBAL hGlobal = LoadResource(hMod, hRsrc);
1477     DWORD Size = SizeofResource(hMod, hRsrc);
1478     LPVOID pData = LockResource(hGlobal);
1479 
1480     ok(Size && !!pData, "Unable to load %s\n", wine_dbgstr_w(ResourceName));
1481     if (!Size || !pData)
1482         return false;
1483 
1484     BOOL Written = write_raw_file(Filename, pData, Size);
1485     UnlockResource(pData);
1486     return Written;
1487 }
1488 
1489 template<typename SDBQUERYRESULT_T>
1490 static void test_match_ex(const WCHAR* workdir, HSDB hsdb)
1491 {
1492     WCHAR exename[MAX_PATH];
1493     PWCHAR Vendor, AppName, TestName;
1494     SDBQUERYRESULT_T query;
1495     TAGID dbtag, exetag, tagid;
1496     BOOL ret, Succeed;
1497     PDB pdb;
1498 
1499     memset(&query, 0xab, sizeof(query));
1500 
1501     ret = pSdbTagRefToTagID(hsdb, 0, &pdb, &tagid);
1502     ok(pdb != NULL && pdb != (PDB)0x12345678, "Expected pdb to be set to a valid pdb, was: %p\n", pdb);
1503 
1504     dbtag = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
1505     ok(dbtag != TAGID_NULL, "Expected to get a valid TAG_DATABASE\n");
1506 
1507     for (exetag = pSdbFindFirstTag(pdb, dbtag, TAG_EXE); exetag; exetag = pSdbFindNextTag(pdb, dbtag, exetag))
1508     {
1509         tagid = pSdbFindFirstTag(pdb, exetag, TAG_VENDOR);
1510         Vendor = pSdbGetStringTagPtr(pdb, tagid);
1511         if (!Vendor)
1512             continue;
1513         Succeed = !wcsicmp(Vendor, L"Succeed");
1514         if (!Succeed && wcsicmp(Vendor, L"Fail"))
1515             continue;
1516         tagid = pSdbFindFirstTag(pdb, exetag, TAG_APP_NAME);
1517         AppName = pSdbGetStringTagPtr(pdb, tagid);
1518         if (!AppName)
1519             continue;
1520 
1521         tagid = pSdbFindFirstTag(pdb, exetag, TAG_NAME);
1522         TestName = pSdbGetStringTagPtr(pdb, tagid);
1523         if (!TestName)
1524             continue;
1525 
1526         swprintf(exename, L"%s\\%s", workdir, AppName);
1527         test_create_exe(exename, 0);
1528 
1529         ret = pSdbGetMatchingExe(hsdb, exename, NULL, NULL, 0, (SDBQUERYRESULT_VISTA*)&query);
1530         DWORD exe_count = Succeed ? 1 : 0;
1531 
1532         if (Succeed && !ret && g_WinVersion == _WIN32_WINNT_WS03)
1533         {
1534             skip("As long as we do not have indexes, we will hit a bug in W2k3\n");
1535 #if 0
1536 [Info][SdbGetIndex         ] index 0x7007(0x600b) was not found in the index table
1537 [Info][SdbGetIndex         ] index 0x7007(0x6001) was not found in the index table
1538 [Info][SdbpSearchDB        ] Searching database with no index.
1539 [Err ][SdbpSearchDB        ] No DATABASE tag found.
1540 #endif
1541         }
1542         else
1543         {
1544             if (Succeed)
1545                 ok(ret, "SdbGetMatchingExe should not fail for %s.\n", wine_dbgstr_w(TestName));
1546             else
1547                 ok(!ret, "SdbGetMatchingExe should not succeed for %s.\n", wine_dbgstr_w(TestName));
1548 
1549             ok(query.dwExeCount == exe_count, "Expected dwExeCount to be %d, was %d for %s\n", exe_count, query.dwExeCount, wine_dbgstr_w(TestName));
1550         }
1551         DeleteFileW(exename);
1552     }
1553 }
1554 
1555 
1556 template<typename SDBQUERYRESULT_T>
1557 static void test_MatchApplicationsEx(void)
1558 {
1559     WCHAR workdir[MAX_PATH], dbpath[MAX_PATH];
1560     BOOL ret;
1561     HSDB hsdb;
1562 
1563     ret = GetTempPathW(_countof(workdir), workdir);
1564     ok(ret, "GetTempPathW error: %d\n", GetLastError());
1565     lstrcatW(workdir, L"apphelp_test");
1566 
1567     ret = CreateDirectoryW(workdir, NULL);
1568     ok(ret, "CreateDirectoryW error: %d\n", GetLastError());
1569 
1570     /* SdbInitDatabase needs an nt-path */
1571     swprintf(dbpath, L"\\??\\%s\\test.sdb", workdir);
1572 
1573     if (extract_resource(dbpath + 4, MAKEINTRESOURCEW(101)))
1574     {
1575         hsdb = pSdbInitDatabase(HID_DATABASE_FULLPATH, dbpath);
1576 
1577         ok(hsdb != NULL, "Expected a valid database handle\n");
1578 
1579         if (!hsdb)
1580         {
1581             skip("SdbInitDatabase not implemented?\n");
1582         }
1583         else
1584         {
1585             /* now that our enviroment is setup, let's go ahead and run the actual tests.. */
1586             test_match_ex<SDBQUERYRESULT_T>(workdir, hsdb);
1587             pSdbReleaseDatabase(hsdb);
1588         }
1589     }
1590     else
1591     {
1592         ok(0, "Unable to extract database\n");
1593     }
1594 
1595     DeleteFileW(dbpath + 4);
1596 
1597     ret = RemoveDirectoryW(workdir);
1598     ok(ret, "RemoveDirectoryW error: %d\n", GetLastError());
1599 }
1600 
1601 
1602 
1603 
1604 static void test_TagRef(void)
1605 {
1606     WCHAR tmpdir[MAX_PATH], dbpath[MAX_PATH];
1607     BOOL ret;
1608     HSDB hsdb;
1609     PDB pdb;
1610     TAGID db;
1611     DWORD size;
1612     TAGREF tr;
1613 
1614     ret = GetTempPathW(_countof(tmpdir), tmpdir);
1615     ok(ret, "GetTempPathA error: %d\n", GetLastError());
1616 
1617     /* SdbInitDatabase needs an nt-path */
1618     swprintf(dbpath, L"\\??\\%stest.sdb", tmpdir);
1619 
1620     test_create_db(dbpath + 4, g_WinVersion >= WINVER_WIN10);
1621 
1622     hsdb = pSdbInitDatabase(HID_DATABASE_FULLPATH, dbpath);
1623 
1624     /* HSDB is the only arg that can't be null */
1625     ret = pSdbTagRefToTagID(hsdb, 0, NULL, NULL);
1626     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1627 
1628     size = test_get_db_size();
1629 
1630     pdb = (PDB)&db;
1631     db = 12345;
1632     ret = pSdbTagRefToTagID(hsdb, size - 1, &pdb, &db);
1633     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1634     ok(pdb != NULL, "Expected a result, got: %p\n", pdb);
1635     ok(db == (size - 1), "Expected %u, got: %u\n", size - 1, db);
1636 
1637     /* Convert it back. */
1638     tr = 0x12345678;
1639     ret = pSdbTagIDToTagRef(hsdb, pdb, db, &tr);
1640     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1641     ok(tr == (size - 1), "Expected %u, got: %u\n", size - 1, tr);
1642 
1643     pdb = (PDB)&db;
1644     db = 12345;
1645     ret = pSdbTagRefToTagID(hsdb, size, &pdb, &db);
1646     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1647     ok(pdb != NULL, "Expected a result, got: %p\n", pdb);
1648     ok(db == size, "Expected %u, got: %u\n", size, db);
1649 
1650     tr = 0x12345678;
1651     ret = pSdbTagIDToTagRef(hsdb, pdb, db, &tr);
1652     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1653     ok(tr == size, "Expected %u, got: %u\n", size, tr);
1654 
1655     pdb = (PDB)&db;
1656     db = 12345;
1657     ret = pSdbTagRefToTagID(hsdb, size + 1, &pdb, &db);
1658     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1659     ok(pdb != NULL, "Expected a result, got: %p\n", pdb);
1660     ok(db == (size + 1), "Expected %u, got: %u\n", size + 1, db);
1661 
1662     tr = 0x12345678;
1663     ret = pSdbTagIDToTagRef(hsdb, pdb, db, &tr);
1664     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1665     ok(tr == (size + 1), "Expected %u, got: %u\n", (size + 1), tr);
1666 
1667     pdb = (PDB)&db;
1668     db = 12345;
1669     ret = pSdbTagRefToTagID(hsdb, 0x0fffffff, &pdb, &db);
1670     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1671     ok(pdb != NULL, "Expected a result, got: %p\n", pdb);
1672     ok(db == 0x0fffffff, "Expected %u, got: %u\n", 0x0fffffff, db);
1673 
1674     tr = 0x12345678;
1675     ret = pSdbTagIDToTagRef(hsdb, pdb, db, &tr);
1676     ok(ret == TRUE, "Expected ret to be TRUE, was: %d\n", ret);
1677     ok(tr == 0x0fffffff, "Expected %u, got: %u\n", 0x0fffffff, tr);
1678 
1679     pdb = (PDB)&db;
1680     db = 12345;
1681     ret = pSdbTagRefToTagID(hsdb, 0x10000000, &pdb, &db);
1682     ok(ret == FALSE, "Expected ret to be FALSE, was: %d\n", ret);
1683     ok(pdb == NULL, "Expected no result, got: %p\n", pdb);
1684     ok(db == 0, "Expected no result, got: 0x%x\n", db);
1685 
1686     tr = 0x12345678;
1687     ret = pSdbTagIDToTagRef(hsdb, pdb, 0x10000000, &tr);
1688     ok(ret == FALSE, "Expected ret to be TRUE, was: %d\n", ret);
1689     ok(tr == 0, "Expected %u, got: %u\n", 0, tr);
1690 
1691     pdb = NULL;
1692     db = TAGID_NULL;
1693     ret = pSdbTagRefToTagID(hsdb, TAGID_ROOT, &pdb, NULL);
1694     ok(ret != FALSE, "Expected ret to be TRUE, was: %d\n", ret);
1695     ok(pdb != NULL, "Expected pdb to be valid\n");
1696 
1697     if (pdb == NULL)
1698     {
1699         skip("Cannot run tests without pdb\n");
1700     }
1701     else
1702     {
1703         db = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
1704         if (db != TAGID_NULL)
1705         {
1706             TAGID child;
1707             child = pSdbGetFirstChild(pdb, db);
1708             while (child != TAGID_NULL)
1709             {
1710                 PDB pdb_res;
1711                 TAGID tagid_res;
1712                 /* We are using a TAGID as a TAGREF here. */
1713                 ret = pSdbTagRefToTagID(hsdb, child, &pdb_res, &tagid_res);
1714                 ok(ret, "Expected SdbTagRefToTagID to succeed\n");
1715 
1716                 /* For simple cases (primary DB) TAGREF == TAGID */
1717                 tr = 0x12345678;
1718                 ret = pSdbTagIDToTagRef(hsdb, pdb_res, tagid_res, &tr);
1719                 ok(ret, "Expected SdbTagIDToTagRef to succeed\n");
1720                 ok_hex(tr, (int)tagid_res);
1721 
1722                 child = pSdbGetNextChild(pdb, db, child);
1723             }
1724         }
1725         else
1726         {
1727             skip("Cannot run tests without valid db tag\n");
1728         }
1729     }
1730 
1731     /* Get a tagref for our own layer */
1732     tr = pSdbGetLayerTagRef(hsdb, L"TestNewMode");
1733     ok_hex(tr, 0x18e);
1734 
1735     /* We cannot find a tagref from the main database. */
1736     tr = pSdbGetLayerTagRef(hsdb, L"256Color");
1737     ok_hex(tr, 0);
1738 
1739     pSdbReleaseDatabase(hsdb);
1740 
1741     DeleteFileW(dbpath + 4);
1742 }
1743 
1744 
1745 static void test_DataTags(HSDB hsdb)
1746 {
1747     PDB pdb = NULL;
1748     TAGID db = TAGID_NULL, layer, exe;
1749     TAGREF trData;
1750     BYTE Buffer[1024];
1751     DWORD dwBufferSize, dwDataType, dwRet;
1752     TAGID tiData;
1753 
1754     BOOL ret = pSdbTagRefToTagID(hsdb, TAGID_ROOT, &pdb, NULL);
1755 
1756     ok(ret != FALSE, "Expected ret to be TRUE, was: %d\n", ret);
1757     ok(pdb != NULL, "Expected pdb to be valid\n");
1758 
1759     if (pdb == NULL)
1760     {
1761         skip("Cannot run tests without pdb\n");
1762         return;
1763     }
1764 
1765     db = pSdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
1766     ok(db != NULL, "Expected db to be valid\n");
1767     if (db == TAGID_NULL)
1768     {
1769         skip("Cannot run tests without db\n");
1770         return;
1771     }
1772 
1773     layer = pSdbFindFirstNamedTag(pdb, db, TAG_LAYER, TAG_NAME, L"DATA_LAYER");
1774     ok(layer != NULL, "Expected layer to be valid\n");
1775     if (layer == TAGID_NULL)
1776     {
1777         skip("Cannot run tests without layer\n");
1778         return;
1779     }
1780 
1781     memset(Buffer, 0xaa, sizeof(Buffer));
1782     dwBufferSize = sizeof(Buffer);
1783     dwDataType = 0x12345;
1784     tiData = 0x111111;
1785     dwRet = pSdbQueryDataExTagID(pdb, layer, L"TESTDATA1", &dwDataType, Buffer, &dwBufferSize, &tiData);
1786     ok_hex(dwRet, ERROR_SUCCESS);
1787     ok_hex(dwDataType, REG_DWORD);
1788     ok_hex(dwBufferSize, sizeof(DWORD));
1789     ok_hex(*(DWORD*)Buffer, 3333);
1790     ok(tiData != NULL && tiData != 0x111111, "Expected tiData, got NULL\n");
1791     ok_hex(pSdbGetTagFromTagID(pdb, tiData), TAG_DATA);
1792 
1793     memset(Buffer, 0xaa, sizeof(Buffer));
1794     dwBufferSize = sizeof(Buffer);
1795     dwRet = pSdbQueryDataExTagID(pdb, layer, L"TESTDATA1", NULL, Buffer, &dwBufferSize, NULL);
1796     ok_hex(dwRet, ERROR_SUCCESS);
1797     ok_hex(dwBufferSize, sizeof(DWORD));
1798     ok_hex(*(DWORD*)Buffer, 3333);
1799 
1800     if (g_WinVersion > _WIN32_WINNT_WS03)
1801     {
1802         memset(Buffer, 0xaa, sizeof(Buffer));
1803         dwBufferSize = sizeof(Buffer);
1804         dwRet = pSdbQueryDataExTagID(pdb, layer, L"TESTDATA1", NULL, Buffer, NULL, NULL);
1805         ok_hex(dwRet, ERROR_INSUFFICIENT_BUFFER);
1806         ok_hex(*(DWORD*)Buffer, (int)0xaaaaaaaa);
1807     }
1808 
1809     memset(Buffer, 0xaa, sizeof(Buffer));
1810     dwBufferSize = 1;
1811     dwRet = pSdbQueryDataExTagID(pdb, layer, L"TESTDATA1", NULL, Buffer, &dwBufferSize, NULL);
1812     ok_hex(dwRet, ERROR_INSUFFICIENT_BUFFER);
1813     ok_hex(dwBufferSize, sizeof(DWORD));
1814     ok_hex(*(DWORD*)Buffer, (int)0xaaaaaaaa);
1815 
1816     memset(Buffer, 0xaa, sizeof(Buffer));
1817     dwBufferSize = sizeof(Buffer);
1818     dwRet = pSdbQueryDataExTagID(pdb, layer, L"TESTDATA1", NULL, NULL, &dwBufferSize, NULL);
1819     ok_hex(dwRet, ERROR_INSUFFICIENT_BUFFER);
1820     ok_hex(dwBufferSize, sizeof(DWORD));
1821     ok_hex(*(DWORD*)Buffer, (int)0xaaaaaaaa);
1822 
1823     memset(Buffer, 0xaa, sizeof(Buffer));
1824     dwBufferSize = sizeof(Buffer);
1825     dwRet = pSdbQueryDataExTagID(pdb, TAGID_NULL, L"TESTDATA1", NULL, Buffer, &dwBufferSize, NULL);
1826     ok_hex(dwRet, ERROR_NOT_FOUND);
1827     ok_hex(dwBufferSize, sizeof(Buffer));
1828     ok_hex(*(DWORD*)Buffer, (int)0xaaaaaaaa);
1829 
1830     memset(Buffer, 0xaa, sizeof(Buffer));
1831     dwBufferSize = sizeof(Buffer);
1832     dwDataType = 0x12345;
1833     tiData = 0x111111;
1834     dwRet = pSdbQueryDataExTagID(pdb, layer, L"TESTDATA2", &dwDataType, Buffer, &dwBufferSize, &tiData);
1835     ok_hex(dwRet, ERROR_SUCCESS);
1836     ok_hex(dwDataType, REG_QWORD);
1837     ok_hex(dwBufferSize, sizeof(QWORD));
1838     ok(*(QWORD*)Buffer == 4294967295ull, "unexpected value 0x%I64x, expected 4294967295\n", *(QWORD*)Buffer);
1839     ok(tiData != NULL && tiData != 0x111111, "Expected tiData, got NULL\n");
1840     ok_hex(pSdbGetTagFromTagID(pdb, tiData), TAG_DATA);
1841 
1842     /* Not case sensitive */
1843     memset(Buffer, 0xaa, sizeof(Buffer));
1844     dwBufferSize = sizeof(Buffer);
1845     dwDataType = 0x12345;
1846     tiData = 0x111111;
1847     dwRet = pSdbQueryDataExTagID(pdb, layer, L"TESTDATA3", &dwDataType, Buffer, &dwBufferSize, &tiData);
1848     ok_hex(dwRet, ERROR_SUCCESS);
1849     ok_hex(dwDataType, REG_SZ);
1850     ok_hex(dwBufferSize, (int)((wcslen(L"Test string")+1) * sizeof(WCHAR)));
1851     Buffer[_countof(Buffer)-1] = L'\0';
1852     ok_wstr(((WCHAR*)Buffer), L"Test string");
1853     ok(tiData != NULL && tiData != 0x111111, "Expected tiData, got NULL\n");
1854     ok_hex(pSdbGetTagFromTagID(pdb, tiData), TAG_DATA);
1855 
1856     /* Show that SdbQueryDataEx behaves the same */
1857     memset(Buffer, 0xaa, sizeof(Buffer));
1858     dwBufferSize = sizeof(Buffer);
1859     dwDataType = 0x12345;
1860     trData = 0x111111;
1861     dwRet = pSdbQueryDataEx(hsdb, layer, L"TESTDATA1", &dwDataType, Buffer, &dwBufferSize, &trData);
1862     ok_hex(dwRet, ERROR_SUCCESS);
1863     ok_hex(dwDataType, REG_DWORD);
1864     ok_hex(dwBufferSize, sizeof(DWORD));
1865     ok_hex(*(DWORD*)Buffer, 3333);
1866     ok(trData != NULL && trData != 0x111111, "Expected trData, got NULL\n");
1867 
1868     /* And SdbQueryData as well */
1869     memset(Buffer, 0xaa, sizeof(Buffer));
1870     dwBufferSize = sizeof(Buffer);
1871     dwDataType = 0x12345;
1872     dwRet = pSdbQueryData(hsdb, layer, L"TESTDATA1", &dwDataType, Buffer, &dwBufferSize);
1873     ok_hex(dwRet, ERROR_SUCCESS);
1874     ok_hex(dwDataType, REG_DWORD);
1875     ok_hex(dwBufferSize, sizeof(DWORD));
1876     ok_hex(*(DWORD*)Buffer, 3333);
1877 
1878     exe = pSdbFindFirstNamedTag(pdb, db, TAG_EXE, TAG_NAME, L"test_match0.exe");
1879     ok(exe != NULL, "Expected exe to be valid\n");
1880     if (exe == TAGID_NULL)
1881     {
1882         skip("Cannot run tests without exe\n");
1883         return;
1884     }
1885 
1886     memset(Buffer, 0xaa, sizeof(Buffer));
1887     dwBufferSize = sizeof(Buffer);
1888     dwDataType = 0x12345;
1889     tiData = 0x111111;
1890     dwRet = pSdbQueryDataExTagID(pdb, exe, L"TESTDATA1", &dwDataType, Buffer, &dwBufferSize, &tiData);
1891     ok_hex(dwRet, ERROR_NOT_FOUND);
1892     ok_hex(dwDataType, 0x12345);
1893     ok_hex(dwBufferSize, sizeof(Buffer));
1894     ok_hex(*(DWORD*)Buffer, (int)0xaaaaaaaa);
1895     ok(tiData == 0x111111, "Expected 0x111111, got 0x%x\n", tiData);
1896 
1897     /* Show that SdbQueryDataEx behaves the same */
1898     memset(Buffer, 0xaa, sizeof(Buffer));
1899     dwBufferSize = sizeof(Buffer);
1900     dwDataType = 0x12345;
1901     trData = 0x111111;
1902     dwRet = pSdbQueryDataEx(hsdb, exe, L"TESTDATA1", &dwDataType, Buffer, &dwBufferSize, &trData);
1903     ok_hex(dwRet, ERROR_NOT_FOUND);
1904     ok_hex(dwDataType, 0x12345);
1905     ok_hex(dwBufferSize, sizeof(Buffer));
1906     ok_hex(*(DWORD*)Buffer, (int)0xaaaaaaaa);
1907     if (g_WinVersion == _WIN32_WINNT_WS03)
1908         ok(trData == 0, "Expected 0, got 0x%x\n", trData);
1909     else
1910         ok(trData == 0x111111, "Expected 0x111111, got 0x%x\n", trData);
1911 
1912     /* And SdbQueryData as well */
1913     memset(Buffer, 0xaa, sizeof(Buffer));
1914     dwBufferSize = sizeof(Buffer);
1915     dwDataType = 0x12345;
1916     dwRet = pSdbQueryData(hsdb, exe, L"TESTDATA1", &dwDataType, Buffer, &dwBufferSize);
1917     ok_hex(dwRet, ERROR_NOT_FOUND);
1918     ok_hex(dwDataType, 0x12345);
1919     ok_hex(dwBufferSize, sizeof(Buffer));
1920     ok_hex(*(DWORD*)Buffer, (int)0xaaaaaaaa);
1921 }
1922 
1923 
1924 static void test_Data(void)
1925 {
1926     WCHAR workdir[MAX_PATH], dbpath[MAX_PATH];
1927     BOOL ret;
1928     HSDB hsdb;
1929 
1930     ret = GetTempPathW(_countof(workdir), workdir);
1931     ok(ret, "GetTempPathW error: %d\n", GetLastError());
1932     lstrcatW(workdir, L"apphelp_test");
1933 
1934     ret = CreateDirectoryW(workdir, NULL);
1935     ok(ret, "CreateDirectoryW error: %d\n", GetLastError());
1936 
1937     /* SdbInitDatabase needs an nt-path */
1938     swprintf(dbpath, L"\\??\\%s\\test.sdb", workdir);
1939 
1940     if (extract_resource(dbpath + 4, MAKEINTRESOURCEW(101)))
1941     {
1942         hsdb = pSdbInitDatabase(HID_DATABASE_FULLPATH, dbpath);
1943 
1944         ok(hsdb != NULL, "Expected a valid database handle\n");
1945 
1946         if (!hsdb)
1947         {
1948             skip("SdbInitDatabase not implemented?\n");
1949         }
1950         else
1951         {
1952             test_DataTags(hsdb);
1953             pSdbReleaseDatabase(hsdb);
1954         }
1955     }
1956     else
1957     {
1958         ok(0, "Unable to extract database\n");
1959     }
1960 
1961     DeleteFileW(dbpath + 4);
1962 
1963     ret = RemoveDirectoryW(workdir);
1964     ok(ret, "RemoveDirectoryW error: %d\n", GetLastError());
1965 }
1966 
1967 
1968 static void expect_indexA_imp(const char* text, LONGLONG expected)
1969 {
1970     static WCHAR wide_string[100] = { 0 };
1971     LONGLONG result;
1972     MultiByteToWideChar(CP_ACP, 0, text, -1, wide_string, 100);
1973 
1974     result = pSdbMakeIndexKeyFromString(wide_string);
1975     winetest_ok(result == expected, "Expected %s to result in %s, was: %s\n", text, wine_dbgstr_longlong(expected), wine_dbgstr_longlong(result));
1976 }
1977 
1978 #define expect_indexA  (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : expect_indexA_imp
1979 
1980 static void test_IndexKeyFromString(void)
1981 {
1982 #if 0
1983     static WCHAR tmp[] = { 0xabba, 0xbcde, 0x2020, 0x20, 0x4444, 0 };
1984     static WCHAR tmp2[] = { 0xabba, 0xbcde, 0x20, 0x4444, 0 };
1985     static WCHAR tmp3[] = { 0x20, 0xbcde, 0x4041, 0x4444, 0 };
1986     static WCHAR tmp4[] = { 0x20, 0xbcde, 0x4041, 0x4444, 0x4444, 0 };
1987     static WCHAR tmp5[] = { 0x2020, 0xbcde, 0x4041, 0x4444, 0x4444, 0 };
1988     static WCHAR tmp6 [] = { 0x20, 0xbcde, 0x4041, 0x4444, 0x4444, 0x4444, 0};
1989     static WCHAR tmp7 [] = { 0xbcde, 0x4041, 0x4444, 0x4444, 0x4444, 0x4444, 0x4444, 0x4444, 0x4444, 0};
1990     static WCHAR tmp8 [] = { 0xbc00, 0x4041, 0x4444, 0x4444, 0x4444, 0x4444, 0x4444, 0x4444, 0x4444, 0};
1991 #endif
1992 
1993 #if 0
1994     /* This crashes. */
1995     pSdbMakeIndexKeyFromString(NULL);
1996 #endif
1997 
1998     expect_indexA("", 0x0000000000000000);
1999     expect_indexA("a", 0x4100000000000000);
2000     expect_indexA("aa", 0x4141000000000000);
2001     expect_indexA("aaa", 0x4141410000000000);
2002     expect_indexA("aaaa", 0x4141414100000000);
2003     expect_indexA("aaaaa", 0x4141414141000000);
2004     expect_indexA("aaaaaa", 0x4141414141410000);
2005     expect_indexA("aaaaaaa", 0x4141414141414100);
2006     expect_indexA("aaaaaaaa", 0x4141414141414141);
2007     expect_indexA("aaa aaaaa", 0x4141412041414141);
2008     /* Does not change */
2009     expect_indexA("aaaaaaaaa", 0x4141414141414141);
2010     expect_indexA("aaaaaaaab", 0x4141414141414141);
2011     expect_indexA("aaaaaaaac", 0x4141414141414141);
2012     expect_indexA("aaaaaaaaF", 0x4141414141414141);
2013     /* Upcase */
2014     expect_indexA("AAAAAAAA", 0x4141414141414141);
2015     expect_indexA("ABABABAB", 0x4142414241424142);
2016     expect_indexA("ABABABABZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ", 0x4142414241424142);
2017 
2018 #if 0
2019     /* These fail, but is that because the codepoints are too weird, or because the func is not correct? */
2020     result = pSdbMakeIndexKeyFromString(tmp);
2021     ok(result == 0xbaabdebc20200000, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp),
2022         wine_dbgstr_longlong(0xbaabdebc20200000), wine_dbgstr_longlong(result));
2023 
2024     result = pSdbMakeIndexKeyFromString(tmp2);
2025     ok(result == 0xbaabdebc00000000, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp2),
2026         wine_dbgstr_longlong(0xbaabdebc00000000), wine_dbgstr_longlong(result));
2027 
2028     result = pSdbMakeIndexKeyFromString(tmp3);
2029     ok(result == 0x20debc4140000000, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp3),
2030         wine_dbgstr_longlong(0x20debc4140000000), wine_dbgstr_longlong(result));
2031 
2032     result = pSdbMakeIndexKeyFromString(tmp4);
2033     ok(result == 0x20debc4140000000, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp4),
2034         wine_dbgstr_longlong(0x20debc4140000000), wine_dbgstr_longlong(result));
2035 
2036     result = pSdbMakeIndexKeyFromString(tmp5);
2037     ok(result == 0x2020debc41400000, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp5),
2038         wine_dbgstr_longlong(0x2020debc41400000), wine_dbgstr_longlong(result));
2039 
2040     result = pSdbMakeIndexKeyFromString(tmp6);
2041     ok(result == 0x20debc4140444400, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp6),
2042         wine_dbgstr_longlong(0x20debc4140444400), wine_dbgstr_longlong(result));
2043 
2044     result = pSdbMakeIndexKeyFromString(tmp7);
2045     ok(result == 0xdebc414044444444, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp7),
2046         wine_dbgstr_longlong(0xdebc414044444444), wine_dbgstr_longlong(result));
2047 
2048     result = pSdbMakeIndexKeyFromString(tmp8);
2049     ok(result == 0xbc414044444444, "Expected %s to result in %s, was: %s\n", wine_dbgstr_w(tmp8),
2050         wine_dbgstr_longlong(0xbc414044444444), wine_dbgstr_longlong(result));
2051 #endif
2052 }
2053 
2054 static int validate_SDBQUERYRESULT_size()
2055 {
2056     unsigned char buffer[SDBQUERYRESULT_EXPECTED_SIZE_VISTA * 2];
2057     WCHAR path[MAX_PATH];
2058     size_t n;
2059 
2060     memset(buffer, 0xab, sizeof(buffer));
2061 
2062     GetModuleFileNameW(NULL, path, MAX_PATH);
2063     pSdbGetMatchingExe(NULL, path, NULL, NULL, 0, (SDBQUERYRESULT_VISTA*)buffer);
2064     if (buffer[0] == 0xab)
2065     {
2066         trace("SdbGetMatchingExe didnt do anything, cannot determine SDBQUERYRESULT size\n");
2067         return 0;
2068     }
2069 
2070     if (buffer[SDBQUERYRESULT_EXPECTED_SIZE_2k3] == 0xab && buffer[SDBQUERYRESULT_EXPECTED_SIZE_2k3-1] != 0xab)
2071     {
2072         return 1;
2073     }
2074 
2075     if (buffer[SDBQUERYRESULT_EXPECTED_SIZE_VISTA] == 0xab && buffer[SDBQUERYRESULT_EXPECTED_SIZE_VISTA-1] != 0xab)
2076     {
2077         return 2;
2078     }
2079 
2080     for (n = 0; n < _countof(buffer); ++n)
2081     {
2082         if (buffer[n] != 0xab)
2083         {
2084             trace("Unknown size: %i\n", n);
2085             break;
2086         }
2087     }
2088 
2089     return 0;
2090 }
2091 
2092 
2093 START_TEST(db)
2094 {
2095     //SetEnvironmentVariableA("SHIM_DEBUG_LEVEL", "4");
2096     //SetEnvironmentVariableA("SHIMENG_DEBUG_LEVEL", "4");
2097     //SetEnvironmentVariableA("DEBUGCHANNEL", "+apphelp");
2098 
2099     silence_debug_output();
2100     hdll = LoadLibraryA("apphelp.dll");
2101 
2102     /* We detect the apphelp version that is loaded, instead of the os we are running on.
2103        This allows for easier testing multiple versions of the dll */
2104     g_WinVersion = get_module_version(hdll);
2105     trace("Apphelp version: 0x%x\n", g_WinVersion);
2106 
2107     *(void**)&pSdbTagToString = (void *)GetProcAddress(hdll, "SdbTagToString");
2108     *(void**)&pSdbOpenDatabase = (void *)GetProcAddress(hdll, "SdbOpenDatabase");
2109     *(void**)&pSdbCreateDatabase = (void *)GetProcAddress(hdll, "SdbCreateDatabase");
2110     *(void**)&pSdbGetDatabaseVersion = (void *)GetProcAddress(hdll, "SdbGetDatabaseVersion");
2111     *(void**)&pSdbCloseDatabase = (void *)GetProcAddress(hdll, "SdbCloseDatabase");
2112     *(void**)&pSdbCloseDatabaseWrite = (void *)GetProcAddress(hdll, "SdbCloseDatabaseWrite");
2113     *(void**)&pSdbGetTagFromTagID = (void *)GetProcAddress(hdll, "SdbGetTagFromTagID");
2114     *(void**)&pSdbWriteNULLTag = (void *)GetProcAddress(hdll, "SdbWriteNULLTag");
2115     *(void**)&pSdbWriteWORDTag = (void *)GetProcAddress(hdll, "SdbWriteWORDTag");
2116     *(void**)&pSdbWriteDWORDTag = (void *)GetProcAddress(hdll, "SdbWriteDWORDTag");
2117     *(void**)&pSdbWriteQWORDTag = (void *)GetProcAddress(hdll, "SdbWriteQWORDTag");
2118     *(void**)&pSdbWriteBinaryTagFromFile = (void *)GetProcAddress(hdll, "SdbWriteBinaryTagFromFile");
2119     *(void**)&pSdbWriteStringTag = (void *)GetProcAddress(hdll, "SdbWriteStringTag");
2120     *(void**)&pSdbWriteStringRefTag = (void *)GetProcAddress(hdll, "SdbWriteStringRefTag");
2121     *(void**)&pSdbBeginWriteListTag = (void *)GetProcAddress(hdll, "SdbBeginWriteListTag");
2122     *(void**)&pSdbEndWriteListTag = (void *)GetProcAddress(hdll, "SdbEndWriteListTag");
2123     *(void**)&pSdbFindFirstTag = (void *)GetProcAddress(hdll, "SdbFindFirstTag");
2124     *(void**)&pSdbFindNextTag = (void *)GetProcAddress(hdll, "SdbFindNextTag");
2125     *(void**)&pSdbFindFirstNamedTag = (void *)GetProcAddress(hdll, "SdbFindFirstNamedTag");
2126     *(void**)&pSdbReadWORDTag = (void *)GetProcAddress(hdll, "SdbReadWORDTag");
2127     *(void**)&pSdbReadDWORDTag = (void *)GetProcAddress(hdll, "SdbReadDWORDTag");
2128     *(void**)&pSdbReadQWORDTag = (void *)GetProcAddress(hdll, "SdbReadQWORDTag");
2129     *(void**)&pSdbReadBinaryTag = (void *)GetProcAddress(hdll, "SdbReadBinaryTag");
2130     *(void**)&pSdbReadStringTag = (void *)GetProcAddress(hdll, "SdbReadStringTag");
2131     *(void**)&pSdbGetTagDataSize = (void *)GetProcAddress(hdll, "SdbGetTagDataSize");
2132     *(void**)&pSdbGetBinaryTagData = (void *)GetProcAddress(hdll, "SdbGetBinaryTagData");
2133     *(void**)&pSdbGetStringTagPtr = (void *)GetProcAddress(hdll, "SdbGetStringTagPtr");
2134     *(void**)&pSdbGetFirstChild = (void *)GetProcAddress(hdll, "SdbGetFirstChild");
2135     *(void**)&pSdbGetNextChild = (void *)GetProcAddress(hdll, "SdbGetNextChild");
2136     *(void**)&pSdbGetDatabaseID = (void *)GetProcAddress(hdll, "SdbGetDatabaseID");
2137     *(void**)&pSdbGUIDToString = (void *)GetProcAddress(hdll, "SdbGUIDToString");
2138     *(void**)&pSdbInitDatabase = (void *)GetProcAddress(hdll, "SdbInitDatabase");
2139     *(void**)&pSdbReleaseDatabase = (void *)GetProcAddress(hdll, "SdbReleaseDatabase");
2140     *(void**)&pSdbGetMatchingExe = (void *)GetProcAddress(hdll, "SdbGetMatchingExe");
2141     *(void**)&pSdbTagRefToTagID = (void *)GetProcAddress(hdll, "SdbTagRefToTagID");
2142     *(void**)&pSdbTagIDToTagRef = (void *)GetProcAddress(hdll, "SdbTagIDToTagRef");
2143     *(void**)&pSdbMakeIndexKeyFromString = (void *)GetProcAddress(hdll, "SdbMakeIndexKeyFromString");
2144     *(void**)&pSdbQueryData = (void *)GetProcAddress(hdll, "SdbQueryData");
2145     *(void**)&pSdbQueryDataEx = (void *)GetProcAddress(hdll, "SdbQueryDataEx");
2146     *(void**)&pSdbQueryDataExTagID = (void *)GetProcAddress(hdll, "SdbQueryDataExTagID");
2147     *(void**)&pSdbGetLayerTagRef = (void *)GetProcAddress(hdll, "SdbGetLayerTagRef");
2148     *(void**)&pSdbGetDatabaseInformation = (void *)GetProcAddress(hdll, "SdbGetDatabaseInformation");
2149     *(void**)&pSdbFreeDatabaseInformation = (void *)GetProcAddress(hdll, "SdbFreeDatabaseInformation");
2150 
2151     test_Sdb();
2152     test_write_ex();
2153     test_stringtable();
2154     test_CheckDatabaseManually();
2155     switch (validate_SDBQUERYRESULT_size())
2156     {
2157     case 1:
2158         test_MatchApplications<SDBQUERYRESULT_2k3>();
2159         test_MatchApplicationsEx<SDBQUERYRESULT_2k3>();
2160         break;
2161     case 2:
2162         test_MatchApplications<SDBQUERYRESULT_VISTA>();
2163         test_MatchApplicationsEx<SDBQUERYRESULT_VISTA>();
2164         break;
2165     default:
2166         skip("Skipping tests with SDBQUERYRESULT due to a wrong size reported\n");
2167         break;
2168     }
2169     test_TagRef();
2170     test_Data();
2171     skip("test_SecondaryDB()\n");
2172     test_IndexKeyFromString();
2173 }
2174