1 /*
2  * Unit test suite for StringTable functions
3  *
4  * Copyright 2005 Steven Edwards for ReactOS
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 /*
21  * TODO:
22  * Add test for StringTableStringFromIdEx
23  */
24 
25 #include <stdarg.h>
26 
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "winnls.h"
33 #include "setupapi.h"
34 
35 #include "wine/test.h"
36 
37 
38 //DECLARE_HANDLE(HSTRING_TABLE);
39 
40 /* Flags for StringTableAddString and StringTableLookUpString */
41 #define ST_CASE_SENSITIVE_COMPARE	0x00000001
42 
43 static DWORD    (WINAPI *pStringTableAddString)(HSTRING_TABLE, LPWSTR, DWORD);
44 static DWORD    (WINAPI *pStringTableAddStringEx)(HSTRING_TABLE, LPWSTR, DWORD, LPVOID, DWORD);
45 static VOID     (WINAPI *pStringTableDestroy)(HSTRING_TABLE);
46 static HSTRING_TABLE (WINAPI *pStringTableDuplicate)(HSTRING_TABLE hStringTable);
47 static HSTRING_TABLE (WINAPI *pStringTableInitialize)(VOID);
48 static HSTRING_TABLE (WINAPI *pStringTableInitializeEx)(DWORD, DWORD);
49 static DWORD    (WINAPI *pStringTableLookUpString)(HSTRING_TABLE, LPWSTR, DWORD);
50 static DWORD    (WINAPI *pStringTableLookUpStringEx)(HSTRING_TABLE, LPWSTR, DWORD, LPVOID, DWORD);
51 static LPWSTR   (WINAPI *pStringTableStringFromId)(HSTRING_TABLE, DWORD);
52 static BOOL     (WINAPI *pStringTableGetExtraData)(HSTRING_TABLE, ULONG, void*, ULONG);
53 
54 static WCHAR string[] = {'s','t','r','i','n','g',0};
55 static WCHAR String[] = {'S','t','r','i','n','g',0};
56 static WCHAR foo[] = {'f','o','o',0};
57 
load_it_up(void)58 static void load_it_up(void)
59 {
60     HMODULE hdll = GetModuleHandleA("setupapi.dll");
61 
62 #define X(f) if (!(p##f = (void*)GetProcAddress(hdll, #f))) \
63                  p##f = (void*)GetProcAddress(hdll, "pSetup"#f);
64     X(StringTableInitialize);
65     X(StringTableInitializeEx);
66     X(StringTableAddString);
67     X(StringTableAddStringEx);
68     X(StringTableDuplicate);
69     X(StringTableDestroy);
70     X(StringTableLookUpString);
71     X(StringTableLookUpStringEx);
72     X(StringTableStringFromId);
73     X(StringTableGetExtraData);
74 #undef X
75 }
76 
test_StringTableAddString(void)77 static void test_StringTableAddString(void)
78 {
79     DWORD retval, hstring, hString, hfoo;
80     HSTRING_TABLE table;
81 
82     table = pStringTableInitialize();
83     ok(table != NULL, "failed to initialize string table\n");
84 
85     /* case insensitive */
86     hstring=pStringTableAddString(table,string,0);
87     ok(hstring!=-1,"Failed to add string to String Table\n");
88 
89     retval=pStringTableAddString(table,String,0);
90     ok(retval!=-1,"Failed to add String to String Table\n");
91     ok(hstring==retval,"string handle %x != String handle %x in String Table\n", hstring, retval);
92 
93     hfoo=pStringTableAddString(table,foo,0);
94     ok(hfoo!=-1,"Failed to add foo to String Table\n");
95     ok(hfoo!=hstring,"foo and string share the same ID %x in String Table\n", hfoo);
96 
97     /* case sensitive */
98     hString=pStringTableAddString(table,String,ST_CASE_SENSITIVE_COMPARE);
99     ok(hstring!=hString,"String handle and string share same ID %x in Table\n", hstring);
100 
101     pStringTableDestroy(table);
102 }
103 
test_StringTableAddStringEx(void)104 static void test_StringTableAddStringEx(void)
105 {
106     DWORD retval, hstring, hString, hfoo, extra;
107     HANDLE table;
108     BOOL ret;
109 
110     table = pStringTableInitialize();
111     ok(table != NULL,"Failed to Initialize String Table\n");
112 
113     /* case insensitive */
114     hstring = pStringTableAddStringEx(table, string, 0, NULL, 0);
115     ok(hstring != -1, "Failed to add string to String Table\n");
116 
117     retval = pStringTableAddStringEx(table, String, 0, NULL, 0);
118     ok(retval != -1, "Failed to add String to String Table\n");
119     ok(hstring == retval, "string handle %x != String handle %x in String Table\n", hstring, retval);
120 
121     hfoo = pStringTableAddStringEx(table, foo, 0, NULL, 0);
122     ok(hfoo != -1, "Failed to add foo to String Table\n");
123     ok(hfoo != hstring, "foo and string share the same ID %x in String Table\n", hfoo);
124 
125     /* case sensitive */
126     hString = pStringTableAddStringEx(table, String, ST_CASE_SENSITIVE_COMPARE, NULL, 0);
127     ok(hstring != hString, "String handle and string share same ID %x in Table\n", hstring);
128 
129     pStringTableDestroy(table);
130 
131     /* set same string twice but with different extra */
132     table = pStringTableInitializeEx(4, 0);
133     ok(table != NULL, "Failed to Initialize String Table\n");
134 
135     extra = 10;
136     hstring = pStringTableAddStringEx(table, string, 0, &extra, 4);
137     ok(hstring != -1, "failed to add string, %d\n", hstring);
138 
139     extra = 0;
140     ret = pStringTableGetExtraData(table, hstring, &extra, 4);
141     ok(ret && extra == 10, "got %d, extra %d\n", ret, extra);
142 
143     extra = 11;
144     hstring = pStringTableAddStringEx(table, string, 0, &extra, 4);
145     ok(hstring != -1, "failed to add string, %d\n", hstring);
146 
147     extra = 0;
148     ret = pStringTableGetExtraData(table, hstring, &extra, 4);
149     ok(ret && extra == 10, "got %d, extra %d\n", ret, extra);
150 
151     pStringTableDestroy(table);
152 }
153 
test_StringTableDuplicate(void)154 static void test_StringTableDuplicate(void)
155 {
156     HSTRING_TABLE table, table2;
157 
158     table = pStringTableInitialize();
159     ok(table != NULL,"Failed to Initialize String Table\n");
160 
161     table2=pStringTableDuplicate(table);
162     ok(table2!=NULL,"Failed to duplicate String Table\n");
163 
164     pStringTableDestroy(table);
165     pStringTableDestroy(table2);
166 }
167 
test_StringTableLookUpString(void)168 static void test_StringTableLookUpString(void)
169 {
170     DWORD retval, retval2, hstring, hString, hfoo;
171     HSTRING_TABLE table, table2;
172 
173     table = pStringTableInitialize();
174     ok(table != NULL,"failed to initialize string table\n");
175 
176     hstring = pStringTableAddString(table, string, 0);
177     ok(hstring != ~0u, "failed to add 'string' to string table\n");
178 
179     hString = pStringTableAddString(table, String, 0);
180     ok(hString != ~0u,"failed to add 'String' to string table\n");
181 
182     hfoo = pStringTableAddString(table, foo, 0);
183     ok(hfoo != ~0u, "failed to add 'foo' to string table\n");
184 
185     table2 = pStringTableDuplicate(table);
186     ok(table2 != NULL, "Failed to duplicate String Table\n");
187 
188     /* case insensitive */
189     retval=pStringTableLookUpString(table,string,0);
190     ok(retval!=-1,"Failed find string in String Table 1\n");
191     ok(retval==hstring,
192         "Lookup for string (%x) does not match previous handle (%x) in String Table 1\n",
193         retval, hstring);
194 
195     retval=pStringTableLookUpString(table2,string,0);
196     ok(retval!=-1,"Failed find string in String Table 2\n");
197 
198     retval=pStringTableLookUpString(table,String,0);
199     ok(retval!=-1,"Failed find String in String Table 1\n");
200 
201     retval=pStringTableLookUpString(table2,String,0);
202     ok(retval!=-1,"Failed find String in String Table 2\n");
203 
204     retval=pStringTableLookUpString(table,foo,0);
205     ok(retval!=-1,"Failed find foo in String Table 1\n");
206     ok(retval==hfoo,
207         "Lookup for foo (%x) does not match previous handle (%x) in String Table 1\n",
208         retval, hfoo);
209 
210     retval=pStringTableLookUpString(table2,foo,0);
211     ok(retval!=-1,"Failed find foo in String Table 2\n");
212 
213     /* case sensitive */
214     retval=pStringTableLookUpString(table,string,ST_CASE_SENSITIVE_COMPARE);
215     retval2=pStringTableLookUpString(table,String,ST_CASE_SENSITIVE_COMPARE);
216     ok(retval!=retval2,"Lookup of string equals String in Table 1\n");
217     ok(retval==hString,
218         "Lookup for String (%x) does not match previous handle (%x) in String Table 1\n",
219         retval, hString);
220 
221     pStringTableDestroy(table);
222     pStringTableDestroy(table2);
223 }
224 
test_StringTableLookUpStringEx(void)225 static void test_StringTableLookUpStringEx(void)
226 {
227     static WCHAR uilevel[] = {'U','I','L','E','V','E','L',0};
228     DWORD retval, retval2, hstring, hString, hfoo, data;
229     HSTRING_TABLE table, table2;
230     char buffer[4];
231 
232     table = pStringTableInitialize();
233     ok(table != NULL,"Failed to Initialize String Table\n");
234 
235     hstring = pStringTableAddString(table, string, 0);
236     ok(hstring != ~0u, "failed to add 'string' to string table\n");
237 
238     hString = pStringTableAddString(table, String, 0);
239     ok(hString != ~0u,"failed to add 'String' to string table\n");
240 
241     hfoo = pStringTableAddString(table, foo, 0);
242     ok(hfoo != ~0u, "failed to add 'foo' to string table\n");
243 
244     table2 = pStringTableDuplicate(table);
245     ok(table2 != NULL, "Failed to duplicate String Table\n");
246 
247     /* case insensitive */
248     retval = pStringTableLookUpStringEx(table, string, 0, NULL, 0);
249     ok(retval != ~0u, "Failed find string in String Table 1\n");
250     ok(retval == hstring,
251         "Lookup for string (%x) does not match previous handle (%x) in String Table 1\n",
252         retval, hstring);
253 
254     retval = pStringTableLookUpStringEx(table2, string, 0, NULL, 0);
255     ok(retval != ~0u, "Failed find string in String Table 2\n");
256 
257     retval = pStringTableLookUpStringEx(table, String, 0, NULL, 0);
258     ok(retval != ~0u, "Failed find String in String Table 1\n");
259 
260     retval = pStringTableLookUpStringEx(table2, String, 0, NULL, 0);
261     ok(retval != ~0u, "Failed find String in String Table 2\n");
262 
263     retval=pStringTableLookUpStringEx(table, foo, 0, NULL, 0);
264     ok(retval != ~0u, "Failed find foo in String Table 1\n");
265     ok(retval == hfoo,
266         "Lookup for foo (%x) does not match previous handle (%x) in String Table 1\n",
267         retval, hfoo);
268 
269     retval = pStringTableLookUpStringEx(table2, foo, 0, NULL, 0);
270     ok(retval != ~0u, "Failed find foo in String Table 2\n");
271 
272     /* case sensitive */
273     retval = pStringTableLookUpStringEx(table, string,ST_CASE_SENSITIVE_COMPARE, NULL, 0);
274     retval2 = pStringTableLookUpStringEx(table, String, ST_CASE_SENSITIVE_COMPARE, NULL, 0);
275     ok(retval != retval2, "Lookup of string equals String in Table 1\n");
276     ok(retval == hString,
277         "Lookup for String (%x) does not match previous handle (%x) in String Table 1\n",
278         retval, hString);
279 
280     pStringTableDestroy(table);
281 
282     table = pStringTableInitializeEx(0x1000, 0);
283     ok(table != NULL, "failed to initialize string table\n");
284 
285     data = 0xaaaaaaaa;
286     retval = pStringTableAddStringEx(table, uilevel, 0x5, &data, sizeof(data));
287     ok(retval != ~0u, "failed to add 'UILEVEL' to string table\n");
288 
289     memset(buffer, 0x55, sizeof(buffer));
290     retval = pStringTableLookUpStringEx(table, uilevel, ST_CASE_SENSITIVE_COMPARE, buffer, 0);
291     ok(retval != ~0u, "failed find 'UILEVEL' in string table\n");
292     ok(memcmp(buffer, &data, 4), "unexpected data\n");
293 
294     memset(buffer, 0x55, sizeof(buffer));
295     retval = pStringTableLookUpStringEx(table, uilevel, ST_CASE_SENSITIVE_COMPARE, buffer, 2);
296     ok(retval != ~0u, "failed find 'UILEVEL' in string table\n");
297     ok(!memcmp(buffer, &data, 2), "unexpected data\n");
298 
299     memset(buffer, 0x55, sizeof(buffer));
300     retval = pStringTableLookUpStringEx(table, uilevel, ST_CASE_SENSITIVE_COMPARE, buffer, sizeof(buffer));
301     ok(retval != ~0u, "failed find 'UILEVEL' in string table\n");
302     ok(!memcmp(buffer, &data, 4), "unexpected data\n");
303 
304     pStringTableDestroy(table);
305     pStringTableDestroy(table2);
306 }
307 
test_StringTableStringFromId(void)308 static void test_StringTableStringFromId(void)
309 {
310     HSTRING_TABLE table;
311     WCHAR *string2;
312     DWORD id, id2;
313 
314     table = pStringTableInitialize();
315     ok(table != NULL, "Failed to Initialize String Table\n");
316 
317     id = pStringTableAddString(table, string, 0);
318     ok(id != -1, "failed to add 'string' to string table\n");
319 
320     /* correct */
321     id2 = pStringTableLookUpString(table, string, 0);
322     ok(id2 == id, "got %d and %d\n", id2, id);
323 
324     string2 = pStringTableStringFromId(table, id2);
325     ok(string2 != NULL, "failed to lookup string %d\n", id2);
326     ok(!lstrcmpiW(string, string2), "got %s, expected %s\n", wine_dbgstr_w(string2), wine_dbgstr_w(string));
327 
328     pStringTableDestroy(table);
329 }
330 
331 struct stringtable {
332     char     *data;
333     ULONG     nextoffset;
334     ULONG     allocated;
335     DWORD_PTR unk[2];
336     ULONG     max_extra_size;
337     LCID      lcid;
338 };
339 
test_stringtable_layout(void)340 static void test_stringtable_layout(void)
341 {
342     struct stringtable *ptr;
343     HSTRING_TABLE table;
344 
345     table = pStringTableInitialize();
346     ok(table != NULL,"failed to initialize string table\n");
347 
348     ptr = (struct stringtable*)table;
349     ok(ptr->data != NULL, "got %p\n", ptr->data);
350     /* first data offset is right after bucket area */
351     ok(ptr->nextoffset == 509*sizeof(DWORD), "got %d\n", ptr->nextoffset);
352     ok(ptr->allocated != 0, "got %d\n", ptr->allocated);
353 todo_wine {
354     ok(ptr->unk[0] != 0, "got %lx\n", ptr->unk[0]);
355     ok(ptr->unk[1] != 0, "got %lx\n", ptr->unk[1]);
356 }
357     ok(ptr->max_extra_size == 0, "got %d\n", ptr->max_extra_size);
358     ok(ptr->lcid == GetThreadLocale(), "got %x, thread lcid %x\n", ptr->lcid, GetThreadLocale());
359 
360     pStringTableDestroy(table);
361 }
362 
START_TEST(stringtable)363 START_TEST(stringtable)
364 {
365     load_it_up();
366 
367     test_StringTableAddString();
368     test_StringTableAddStringEx();
369     test_StringTableDuplicate();
370     test_StringTableLookUpString();
371     test_StringTableLookUpStringEx();
372     test_StringTableStringFromId();
373     test_stringtable_layout();
374 }
375