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