stringtable.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit test suite for StringTable functions
 *
 * Copyright 2005 Steven Edwards for ReactOS
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */
/* 
 * TODO:
 * Add test for StringTableStringFromIdEx
 */

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winreg.h"
#include "setupapi.h"

#include "wine/test.h"


37 38 39 40 41
DECLARE_HANDLE(HSTRING_TABLE);

/* Flags for StringTableAddString and StringTableLookUpString */
#define ST_CASE_SENSITIVE_COMPARE	0x00000001

42
static DWORD    (WINAPI *pStringTableAddString)(HSTRING_TABLE, LPWSTR, DWORD);
43
static DWORD    (WINAPI *pStringTableAddStringEx)(HSTRING_TABLE, LPWSTR, DWORD, LPVOID, DWORD);
44 45 46
static VOID     (WINAPI *pStringTableDestroy)(HSTRING_TABLE);
static HSTRING_TABLE (WINAPI *pStringTableDuplicate)(HSTRING_TABLE hStringTable);
static HSTRING_TABLE (WINAPI *pStringTableInitialize)(VOID);
47
static HSTRING_TABLE (WINAPI *pStringTableInitializeEx)(DWORD, DWORD);
48
static DWORD    (WINAPI *pStringTableLookUpString)(HSTRING_TABLE, LPWSTR, DWORD);
49
static DWORD    (WINAPI *pStringTableLookUpStringEx)(HSTRING_TABLE, LPWSTR, DWORD, LPVOID, DWORD);
50 51 52 53 54 55
static LPWSTR   (WINAPI *pStringTableStringFromId)(HSTRING_TABLE, DWORD);
#if 0
static BOOL     (WINAPI *pStringTableStringFromIdEx)(HSTRING_TABLE, DWORD, LPWSTR, LPDWORD);
static VOID     (WINAPI *pStringTableTrim)(HSTRING_TABLE);
#endif

56
static HMODULE hdll;
57
static WCHAR string[] = {'s','t','r','i','n','g',0};
58 59
static WCHAR String[] = {'S','t','r','i','n','g',0};
static WCHAR foo[] = {'f','o','o',0};
60 61 62

static void load_it_up(void)
{
63
    hdll = GetModuleHandleA("setupapi.dll");
64 65 66 67 68

    pStringTableInitialize = (void*)GetProcAddress(hdll, "StringTableInitialize");
    if (!pStringTableInitialize)
        pStringTableInitialize = (void*)GetProcAddress(hdll, "pSetupStringTableInitialize");

69 70 71 72
    pStringTableInitializeEx = (void*)GetProcAddress(hdll, "StringTableInitializeEx");
    if (!pStringTableInitializeEx)
        pStringTableInitializeEx = (void*)GetProcAddress(hdll, "pSetupStringTableInitializeEx");

73 74 75 76
    pStringTableAddString = (void*)GetProcAddress(hdll, "StringTableAddString");
    if (!pStringTableAddString)
        pStringTableAddString = (void*)GetProcAddress(hdll, "pSetupStringTableAddString");

77 78 79 80
    pStringTableAddStringEx = (void*)GetProcAddress(hdll, "StringTableAddStringEx");
    if (!pStringTableAddStringEx)
        pStringTableAddStringEx = (void*)GetProcAddress(hdll, "pSetupStringTableAddStringEx");

81 82 83 84 85 86 87 88 89 90 91 92
    pStringTableDuplicate = (void*)GetProcAddress(hdll, "StringTableDuplicate");
    if (!pStringTableDuplicate)
        pStringTableDuplicate = (void*)GetProcAddress(hdll, "pSetupStringTableDuplicate");

    pStringTableDestroy = (void*)GetProcAddress(hdll, "StringTableDestroy");
    if (!pStringTableDestroy)
        pStringTableDestroy = (void*)GetProcAddress(hdll, "pSetupStringTableDestroy");

    pStringTableLookUpString = (void*)GetProcAddress(hdll, "StringTableLookUpString");
    if (!pStringTableLookUpString)
        pStringTableLookUpString = (void*)GetProcAddress(hdll, "pSetupStringTableLookUpString");

93 94 95 96
    pStringTableLookUpStringEx = (void*)GetProcAddress(hdll, "StringTableLookUpStringEx");
    if (!pStringTableLookUpStringEx)
        pStringTableLookUpStringEx = (void*)GetProcAddress(hdll, "pSetupStringTableLookUpStringEx");

97 98 99 100 101 102 103
    pStringTableStringFromId = (void*)GetProcAddress(hdll, "StringTableStringFromId");
    if (!pStringTableStringFromId)
        pStringTableStringFromId = (void*)GetProcAddress(hdll, "pSetupStringTableStringFromId");
}

static void test_StringTableAddString(void)
{
104 105 106 107 108
    DWORD retval, hstring, hString, hfoo;
    HANDLE table;

    table = pStringTableInitialize();
    ok(table != NULL, "failed to initialize string table\n");
109

110
    /* case insensitive */
111 112
    hstring=pStringTableAddString(table,string,0);
    ok(hstring!=-1,"Failed to add string to String Table\n");
113
    
114 115
    retval=pStringTableAddString(table,String,0);
    ok(retval!=-1,"Failed to add String to String Table\n");    
116
    ok(hstring==retval,"string handle %x != String handle %x in String Table\n", hstring, retval);        
117
    
118 119
    hfoo=pStringTableAddString(table,foo,0);
    ok(hfoo!=-1,"Failed to add foo to String Table\n");        
120
    ok(hfoo!=hstring,"foo and string share the same ID %x in String Table\n", hfoo);            
121 122
    
    /* case sensitive */    
123
    hString=pStringTableAddString(table,String,ST_CASE_SENSITIVE_COMPARE);
124
    ok(hstring!=hString,"String handle and string share same ID %x in Table\n", hstring);        
125 126

    pStringTableDestroy(table);
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static void test_StringTableAddStringEx(void)
{
    DWORD retval, hstring, hString, hfoo;
    HANDLE table;

    table = pStringTableInitialize();
    ok(table != NULL,"Failed to Initialize String Table\n");

    /* case insensitive */
    hstring = pStringTableAddStringEx(table, string, 0, NULL, 0);
    ok(hstring != ~0u, "Failed to add string to String Table\n");

    retval = pStringTableAddStringEx(table, String, 0, NULL, 0);
    ok(retval != ~0u, "Failed to add String to String Table\n");
    ok(hstring == retval, "string handle %x != String handle %x in String Table\n", hstring, retval);

    hfoo = pStringTableAddStringEx(table, foo, 0, NULL, 0);
    ok(hfoo != ~0u, "Failed to add foo to String Table\n");
    ok(hfoo != hstring, "foo and string share the same ID %x in String Table\n", hfoo);

    /* case sensitive */
    hString = pStringTableAddStringEx(table, String, ST_CASE_SENSITIVE_COMPARE, NULL, 0);
    ok(hstring != hString, "String handle and string share same ID %x in Table\n", hstring);

    pStringTableDestroy(table);
}

156 157
static void test_StringTableDuplicate(void)
{
158 159 160 161 162
    HANDLE table, table2;

    table = pStringTableInitialize();
    ok(table != NULL,"Failed to Initialize String Table\n");

163 164
    table2=pStringTableDuplicate(table);
    ok(table2!=NULL,"Failed to duplicate String Table\n");
165 166 167

    pStringTableDestroy(table);
    pStringTableDestroy(table2);
168 169 170 171
}

static void test_StringTableLookUpString(void)
{   
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    DWORD retval, retval2, hstring, hString, hfoo;
    HANDLE table, table2;

    table = pStringTableInitialize();
    ok(table != NULL,"failed to initialize string table\n");

    hstring = pStringTableAddString(table, string, 0);
    ok(hstring != ~0u, "failed to add 'string' to string table\n");

    hString = pStringTableAddString(table, String, 0);
    ok(hString != ~0u,"failed to add 'String' to string table\n");

    hfoo = pStringTableAddString(table, foo, 0);
    ok(hfoo != ~0u, "failed to add 'foo' to string table\n");

    table2 = pStringTableDuplicate(table);
    ok(table2 != NULL, "Failed to duplicate String Table\n");

190
    /* case insensitive */
191 192
    retval=pStringTableLookUpString(table,string,0);
    ok(retval!=-1,"Failed find string in String Table 1\n");
193
    ok(retval==hstring,
194
        "Lookup for string (%x) does not match previous handle (%x) in String Table 1\n",
195
        retval, hstring);    
196

197 198 199 200 201 202 203 204 205 206 207
    retval=pStringTableLookUpString(table2,string,0);
    ok(retval!=-1,"Failed find string in String Table 2\n");
    
    retval=pStringTableLookUpString(table,String,0);
    ok(retval!=-1,"Failed find String in String Table 1\n");

    retval=pStringTableLookUpString(table2,String,0);
    ok(retval!=-1,"Failed find String in String Table 2\n");    
    
    retval=pStringTableLookUpString(table,foo,0);
    ok(retval!=-1,"Failed find foo in String Table 1\n");    
208
    ok(retval==hfoo,
209
        "Lookup for foo (%x) does not match previous handle (%x) in String Table 1\n",
210
        retval, hfoo);        
211 212 213 214 215 216 217
    
    retval=pStringTableLookUpString(table2,foo,0);
    ok(retval!=-1,"Failed find foo in String Table 2\n");    
    
    /* case sensitive */
    retval=pStringTableLookUpString(table,string,ST_CASE_SENSITIVE_COMPARE);
    retval2=pStringTableLookUpString(table,String,ST_CASE_SENSITIVE_COMPARE);    
218
    ok(retval!=retval2,"Lookup of string equals String in Table 1\n");
219
    ok(retval==hString,
220
        "Lookup for String (%x) does not match previous handle (%x) in String Table 1\n",
221 222 223 224
        retval, hString);

    pStringTableDestroy(table);
    pStringTableDestroy(table2);
225 226
}

227 228
static void test_StringTableLookUpStringEx(void)
{
229 230
    static WCHAR uilevel[] = {'U','I','L','E','V','E','L',0};
    DWORD retval, retval2, hstring, hString, hfoo, data;
231
    HANDLE table, table2;
232
    char buffer[4];
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

    table = pStringTableInitialize();
    ok(table != NULL,"Failed to Initialize String Table\n");

    hstring = pStringTableAddString(table, string, 0);
    ok(hstring != ~0u, "failed to add 'string' to string table\n");

    hString = pStringTableAddString(table, String, 0);
    ok(hString != ~0u,"failed to add 'String' to string table\n");

    hfoo = pStringTableAddString(table, foo, 0);
    ok(hfoo != ~0u, "failed to add 'foo' to string table\n");

    table2 = pStringTableDuplicate(table);
    ok(table2 != NULL, "Failed to duplicate String Table\n");

    /* case insensitive */
250
    retval = pStringTableLookUpStringEx(table, string, 0, NULL, 0);
251 252 253 254 255
    ok(retval != ~0u, "Failed find string in String Table 1\n");
    ok(retval == hstring,
        "Lookup for string (%x) does not match previous handle (%x) in String Table 1\n",
        retval, hstring);

256
    retval = pStringTableLookUpStringEx(table2, string, 0, NULL, 0);
257 258
    ok(retval != ~0u, "Failed find string in String Table 2\n");

259
    retval = pStringTableLookUpStringEx(table, String, 0, NULL, 0);
260 261
    ok(retval != ~0u, "Failed find String in String Table 1\n");

262
    retval = pStringTableLookUpStringEx(table2, String, 0, NULL, 0);
263 264
    ok(retval != ~0u, "Failed find String in String Table 2\n");

265
    retval=pStringTableLookUpStringEx(table, foo, 0, NULL, 0);
266 267 268 269 270
    ok(retval != ~0u, "Failed find foo in String Table 1\n");
    ok(retval == hfoo,
        "Lookup for foo (%x) does not match previous handle (%x) in String Table 1\n",
        retval, hfoo);

271
    retval = pStringTableLookUpStringEx(table2, foo, 0, NULL, 0);
272 273 274
    ok(retval != ~0u, "Failed find foo in String Table 2\n");

    /* case sensitive */
275 276
    retval = pStringTableLookUpStringEx(table, string,ST_CASE_SENSITIVE_COMPARE, NULL, 0);
    retval2 = pStringTableLookUpStringEx(table, String, ST_CASE_SENSITIVE_COMPARE, NULL, 0);
277 278 279 280 281 282
    ok(retval != retval2, "Lookup of string equals String in Table 1\n");
    ok(retval == hString,
        "Lookup for String (%x) does not match previous handle (%x) in String Table 1\n",
        retval, hString);

    pStringTableDestroy(table);
283 284 285 286 287 288 289 290 291

    table = pStringTableInitializeEx(0x1000, 0);
    ok(table != NULL, "failed to initialize string table\n");

    data = 0xaaaaaaaa;
    retval = pStringTableAddStringEx(table, uilevel, 0x5, &data, sizeof(data));
    ok(retval != ~0u, "failed to add 'UILEVEL' to string table\n");

    memset(buffer, 0x55, sizeof(buffer));
292
    retval = pStringTableLookUpStringEx(table, uilevel, ST_CASE_SENSITIVE_COMPARE, buffer, 0);
293 294 295 296
    ok(retval != ~0u, "failed find 'UILEVEL' in string table\n");
    ok(memcmp(buffer, &data, 4), "unexpected data\n");

    memset(buffer, 0x55, sizeof(buffer));
297
    retval = pStringTableLookUpStringEx(table, uilevel, ST_CASE_SENSITIVE_COMPARE, buffer, 2);
298 299 300 301
    ok(retval != ~0u, "failed find 'UILEVEL' in string table\n");
    ok(!memcmp(buffer, &data, 2), "unexpected data\n");

    memset(buffer, 0x55, sizeof(buffer));
302
    retval = pStringTableLookUpStringEx(table, uilevel, ST_CASE_SENSITIVE_COMPARE, buffer, sizeof(buffer));
303 304 305 306
    ok(retval != ~0u, "failed find 'UILEVEL' in string table\n");
    ok(!memcmp(buffer, &data, 4), "unexpected data\n");

    pStringTableDestroy(table);
307
    pStringTableDestroy(table2);
308 309
}

310 311
static void test_StringTableStringFromId(void)
{
312 313
    HANDLE table;
    DWORD hstring;
314
    WCHAR *string2;
315 316
    int result;

317 318 319 320 321 322
    table = pStringTableInitialize();
    ok(table != NULL,"Failed to Initialize String Table\n");

    hstring = pStringTableAddString(table, string, 0);
    ok(hstring != ~0u,"failed to add 'string' to string table\n");

323 324 325 326 327
    /* correct */
    string2=pStringTableStringFromId(table,pStringTableLookUpString(table,string,0));
    ok(string2!=NULL,"Failed to look up string by ID from String Table\n");
    
    result=lstrcmpiW(string, string2);
328
    ok(result==0,"StringID %p does not match requested StringID %p\n",string,string2);
329 330

    pStringTableDestroy(table);
331 332 333 334 335 336 337
}

START_TEST(stringtable)
{
    load_it_up();

    test_StringTableAddString();
338
    test_StringTableAddStringEx();
339 340
    test_StringTableDuplicate();
    test_StringTableLookUpString();
341
    test_StringTableLookUpStringEx();
342 343
    test_StringTableStringFromId();
}