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