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