Remove spaces before '\n' in traces.
[wine] / dlls / shlwapi / tests / ordinal.c
1 /* Unit test suite for SHLWAPI ordinal functions
2  *
3  * Copyright 2004 Jon Griffiths
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdio.h>
21
22 #include "wine/test.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "winuser.h"
26
27 /* Function ptrs for ordinal calls */
28 static HMODULE hShlwapi;
29 static int (WINAPI *pSHSearchMapInt)(const int*,const int*,int,int);
30 static HRESULT (WINAPI *pGetAcceptLanguagesA)(LPSTR,LPDWORD);
31
32 static HANDLE (WINAPI *pSHAllocShared)(LPCVOID,DWORD,DWORD);
33 static LPVOID (WINAPI *pSHLockShared)(HANDLE,DWORD);
34 static BOOL   (WINAPI *pSHUnlockShared)(LPVOID);
35 static BOOL   (WINAPI *pSHFreeShared)(HANDLE,DWORD);
36
37 static void test_GetAcceptLanguagesA(void)
38 {   HRESULT retval;
39     DWORD buffersize, buffersize2, exactsize;
40     char buffer[100];
41
42     if (!pGetAcceptLanguagesA)
43         return;
44
45     buffersize = sizeof(buffer);
46     memset(buffer, 0, sizeof(buffer));
47     SetLastError(ERROR_SUCCESS);
48     retval = pGetAcceptLanguagesA( buffer, &buffersize);
49     trace("GetAcceptLanguagesA: retval %08lx, size %08lx, buffer (%s),"
50         " last error %ld\n", retval, buffersize, buffer, GetLastError());
51     if(retval != S_OK) {
52         trace("GetAcceptLanguagesA: skipping tests\n");
53         return;
54     }
55     ok( (ERROR_NO_IMPERSONATION_TOKEN == GetLastError()) || 
56         (ERROR_CLASS_DOES_NOT_EXIST == GetLastError()) ||
57         (ERROR_PROC_NOT_FOUND == GetLastError()) ||
58         (ERROR_CALL_NOT_IMPLEMENTED == GetLastError()) ||
59         (ERROR_SUCCESS == GetLastError()), "last error set to %ld\n", GetLastError());
60     exactsize = strlen(buffer);
61
62     SetLastError(ERROR_SUCCESS);
63     retval = pGetAcceptLanguagesA( NULL, NULL);
64     ok(retval == E_FAIL,
65        "function result wrong: got %08lx; expected E_FAIL\n", retval);
66     ok(ERROR_SUCCESS == GetLastError(), "last error set to %ld\n", GetLastError());
67
68     buffersize = sizeof(buffer);
69     SetLastError(ERROR_SUCCESS);
70     retval = pGetAcceptLanguagesA( NULL, &buffersize);
71     ok(retval == E_FAIL,
72        "function result wrong: got %08lx; expected E_FAIL\n", retval);
73     ok(buffersize == sizeof(buffer),
74        "buffersize was changed (2nd parameter; not on Win2k)\n");
75     ok(ERROR_SUCCESS == GetLastError(), "last error set to %ld\n", GetLastError());
76
77     SetLastError(ERROR_SUCCESS);
78     retval = pGetAcceptLanguagesA( buffer, NULL);
79     ok(retval == E_FAIL,
80        "function result wrong: got %08lx; expected E_FAIL\n", retval);
81     ok(ERROR_SUCCESS == GetLastError(), "last error set to %ld\n", GetLastError());
82
83     buffersize = 0;
84     memset(buffer, 0, sizeof(buffer));
85     SetLastError(ERROR_SUCCESS);
86     retval = pGetAcceptLanguagesA( buffer, &buffersize);
87     ok(retval == E_FAIL,
88        "function result wrong: got %08lx; expected E_FAIL\n", retval);
89     ok(buffersize == 0,
90        "buffersize wrong(changed) got %08lx; expected 0 (2nd parameter; not on Win2k)\n", buffersize);
91     ok(ERROR_SUCCESS == GetLastError(), "last error set to %ld\n", GetLastError());
92
93     buffersize = buffersize2 = 1;
94     memset(buffer, 0, sizeof(buffer));
95     SetLastError(ERROR_SUCCESS);
96     retval = pGetAcceptLanguagesA( buffer, &buffersize);
97     switch(retval) {
98         case 0L:
99             if(buffersize == exactsize) {
100             ok( (ERROR_SUCCESS == GetLastError()) || (ERROR_CALL_NOT_IMPLEMENTED == GetLastError()) ||
101                 (ERROR_PROC_NOT_FOUND == GetLastError()) || (ERROR_NO_IMPERSONATION_TOKEN == GetLastError()),
102                 "last error wrong: got %08lx; expected ERROR_SUCCESS(NT4)/ERROR_CALL_NOT_IMPLEMENTED(98/ME)/"
103                 "ERROR_PROC_NOT_FOUND(NT4)/ERROR_NO_IMPERSONATION_TOKEN(XP)\n", GetLastError());
104             ok(exactsize == strlen(buffer),
105                  "buffer content (length) wrong: got %08x, expected %08lx\n", strlen(buffer), exactsize);
106             } else if((buffersize +1) == buffersize2) {
107                 ok(ERROR_SUCCESS == GetLastError(),
108                     "last error wrong: got %08lx; expected ERROR_SUCCESS\n", GetLastError());
109                 ok(buffersize == strlen(buffer),
110                     "buffer content (length) wrong: got %08x, expected %08lx\n", strlen(buffer), buffersize);
111             } else
112                 ok( 0, "retval %08lx, size %08lx, buffer (%s), last error %ld\n",
113                     retval, buffersize, buffer, GetLastError());
114             break;
115         case E_INVALIDARG:
116             ok(buffersize == 0,
117                "buffersize wrong: got %08lx, expected 0 (2nd parameter;Win2k)\n", buffersize);
118             ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(),
119                "last error wrong: got %08lx; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
120             ok(buffersize2 == strlen(buffer),
121                "buffer content (length) wrong: got %08x, expected %08lx\n", strlen(buffer), buffersize2);
122             break;
123         default:
124             ok( 0, "retval %08lx, size %08lx, buffer (%s), last error %ld\n",
125                 retval, buffersize, buffer, GetLastError());
126             break;
127     }
128
129     buffersize = buffersize2 = exactsize;
130     memset(buffer, 0, sizeof(buffer));
131     SetLastError(ERROR_SUCCESS);
132     retval = pGetAcceptLanguagesA( buffer, &buffersize);
133     switch(retval) {
134         case 0L:
135             ok(ERROR_SUCCESS == GetLastError(),
136                  "last error wrong: got %08lx; expected ERROR_SUCCESS\n", GetLastError());
137             if((buffersize == exactsize) /* XP */ ||
138                ((buffersize +1)== exactsize) /* 98 */)
139                 ok(buffersize == strlen(buffer),
140                     "buffer content (length) wrong: got %08x, expected %08lx\n", strlen(buffer), buffersize);
141             else
142                 ok( 0, "retval %08lx, size %08lx, buffer (%s), last error %ld\n",
143                     retval, buffersize, buffer, GetLastError());
144             break;
145         case E_INVALIDARG:
146             ok(buffersize == 0,
147                "buffersize wrong: got %08lx, expected 0 (2nd parameter;Win2k)\n", buffersize);
148             ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(),
149                "last error wrong: got %08lx; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
150             ok(buffersize2 == strlen(buffer),
151                "buffer content (length) wrong: got %08x, expected %08lx\n", strlen(buffer), buffersize2);
152             break;
153         default:
154             ok( 0, "retval %08lx, size %08lx, buffer (%s), last error %ld\n",
155                 retval, buffersize, buffer, GetLastError());
156             break;
157     }
158 }
159
160 static void test_SHSearchMapInt(void)
161 {
162   int keys[8], values[8];
163   int i = 0;
164
165   if (!pSHSearchMapInt)
166     return;
167
168   memset(keys, 0, sizeof(keys));
169   memset(values, 0, sizeof(values));
170   keys[0] = 99; values[0] = 101;
171
172   /* NULL key/value lists crash native, so skip testing them */
173
174   /* 1 element */
175   i = pSHSearchMapInt(keys, values, 1, keys[0]);
176   ok(i == values[0], "Len 1, expected %d, got %d\n", values[0], i);
177
178   /* Key doesn't exist */
179   i = pSHSearchMapInt(keys, values, 1, 100);
180   ok(i == -1, "Len 1 - bad key, expected -1, got %d\n", i);
181
182   /* Len = 0 => not found */
183   i = pSHSearchMapInt(keys, values, 0, keys[0]);
184   ok(i == -1, "Len 1 - passed len 0, expected -1, got %d\n", i);
185
186   /* 2 elements, len = 1 */
187   keys[1] = 98; values[1] = 102;
188   i = pSHSearchMapInt(keys, values, 1, keys[1]);
189   ok(i == -1, "Len 1 - array len 2, expected -1, got %d\n", i);
190
191   /* 2 elements, len = 2 */
192   i = pSHSearchMapInt(keys, values, 2, keys[1]);
193   ok(i == values[1], "Len 2, expected %d, got %d\n", values[1], i);
194
195   /* Searches forward */
196   keys[2] = 99; values[2] = 103;
197   i = pSHSearchMapInt(keys, values, 3, keys[0]);
198   ok(i == values[0], "Len 3, expected %d, got %d\n", values[0], i);
199 }
200
201 static void test_alloc_shared(void)
202 {
203     DWORD procid;
204     HANDLE hmem;
205     int val;
206     int* p;
207     BOOL ret;
208
209     procid=GetCurrentProcessId();
210     hmem=pSHAllocShared(NULL,10,procid);
211     ok(hmem!=NULL,"SHAllocShared(NULL...) failed: %ld\n", GetLastError());
212     ret = pSHFreeShared(hmem, procid);
213     ok( ret, "SHFreeShared failed: %ld\n", GetLastError());
214
215     val=0x12345678;
216     hmem=pSHAllocShared(&val,4,procid);
217     ok(hmem!=NULL,"SHAllocShared(NULL...) failed: %ld\n", GetLastError());
218
219     p=(int*)pSHLockShared(hmem,procid);
220     ok(p!=NULL,"SHLockShared failed: %ld\n", GetLastError());
221     if (p!=NULL)
222         ok(*p==val,"Wrong value in shared memory: %d instead of %d\n",*p,val);
223     ret = pSHUnlockShared(p);
224     ok( ret, "SHUnlockShared failed: %ld\n", GetLastError());
225
226     ret = pSHFreeShared(hmem, procid);
227     ok( ret, "SHFreeShared failed: %ld\n", GetLastError());
228 }
229
230 static void test_fdsa(void)
231 {
232     typedef struct
233     {
234         DWORD num_items;       /* Number of elements inserted */
235         void *mem;             /* Ptr to array */
236         DWORD blocks_alloced;  /* Number of elements allocated */
237         BYTE inc;              /* Number of elements to grow by when we need to expand */
238         BYTE block_size;       /* Size in bytes of an element */
239         BYTE flags;            /* Flags */
240     } FDSA_info;
241
242     BOOL (WINAPI *pFDSA_Initialize)(DWORD block_size, DWORD inc, FDSA_info *info, void *mem,
243                                     DWORD init_blocks);
244     BOOL (WINAPI *pFDSA_Destroy)(FDSA_info *info);
245     DWORD (WINAPI *pFDSA_InsertItem)(FDSA_info *info, DWORD where, void *block);
246     BOOL (WINAPI *pFDSA_DeleteItem)(FDSA_info *info, DWORD where);
247
248     FDSA_info info;
249     int block_size = 10, init_blocks = 4, inc = 2;
250     DWORD ret;
251     char *mem;
252
253     pFDSA_Initialize = (void *)GetProcAddress(hShlwapi, (LPSTR)208);
254     pFDSA_Destroy    = (void *)GetProcAddress(hShlwapi, (LPSTR)209);
255     pFDSA_InsertItem = (void *)GetProcAddress(hShlwapi, (LPSTR)210);
256     pFDSA_DeleteItem = (void *)GetProcAddress(hShlwapi, (LPSTR)211);
257
258     mem = HeapAlloc(GetProcessHeap(), 0, block_size * init_blocks);
259     memset(&info, 0, sizeof(info));
260
261     ok(pFDSA_Initialize(block_size, inc, &info, mem, init_blocks), "FDSA_Initialize rets FALSE\n");
262     ok(info.num_items == 0, "num_items = %ld\n", info.num_items);
263     ok(info.mem == mem, "mem = %p\n", info.mem);
264     ok(info.blocks_alloced == init_blocks, "blocks_alloced = %ld\n", info.blocks_alloced);
265     ok(info.inc == inc, "inc = %d\n", info.inc);
266     ok(info.block_size == block_size, "block_size = %d\n", info.block_size);
267     ok(info.flags == 0, "flags = %d\n", info.flags);
268
269     ret = pFDSA_InsertItem(&info, 1234, "1234567890");
270     ok(ret == 0, "ret = %ld\n", ret);
271     ok(info.num_items == 1, "num_items = %ld\n", info.num_items);
272     ok(info.mem == mem, "mem = %p\n", info.mem);
273     ok(info.blocks_alloced == init_blocks, "blocks_alloced = %ld\n", info.blocks_alloced);
274     ok(info.inc == inc, "inc = %d\n", info.inc);
275     ok(info.block_size == block_size, "block_size = %d\n", info.block_size);
276     ok(info.flags == 0, "flags = %d\n", info.flags);
277
278     ret = pFDSA_InsertItem(&info, 1234, "abcdefghij");
279     ok(ret == 1, "ret = %ld\n", ret);
280
281     ret = pFDSA_InsertItem(&info, 1, "klmnopqrst");
282     ok(ret == 1, "ret = %ld\n", ret);
283
284     ret = pFDSA_InsertItem(&info, 0, "uvwxyzABCD");
285     ok(ret == 0, "ret = %ld\n", ret);
286     ok(info.mem == mem, "mem = %p\n", info.mem);
287     ok(info.flags == 0, "flags = %d\n", info.flags);
288
289     /* This next InsertItem will cause shlwapi to allocate its own mem buffer */
290     ret = pFDSA_InsertItem(&info, 0, "EFGHIJKLMN");
291     ok(ret == 0, "ret = %ld\n", ret);
292     ok(info.mem != mem, "mem = %p\n", info.mem);
293     ok(info.blocks_alloced == init_blocks + inc, "blocks_alloced = %ld\n", info.blocks_alloced);
294     ok(info.flags == 0x1, "flags = %d\n", info.flags);
295
296     ok(!memcmp(info.mem, "EFGHIJKLMNuvwxyzABCD1234567890klmnopqrstabcdefghij", 50), "mem %s\n", (char*)info.mem);
297
298     ok(pFDSA_DeleteItem(&info, 2), "rets FALSE\n");
299     ok(info.mem != mem, "mem = %p\n", info.mem);
300     ok(info.blocks_alloced == init_blocks + inc, "blocks_alloced = %ld\n", info.blocks_alloced);
301     ok(info.flags == 0x1, "flags = %d\n", info.flags);
302
303     ok(!memcmp(info.mem, "EFGHIJKLMNuvwxyzABCDklmnopqrstabcdefghij", 40), "mem %s\n", (char*)info.mem);
304
305     ok(pFDSA_DeleteItem(&info, 3), "rets FALSE\n");
306     ok(info.mem != mem, "mem = %p\n", info.mem);
307     ok(info.blocks_alloced == init_blocks + inc, "blocks_alloced = %ld\n", info.blocks_alloced);
308     ok(info.flags == 0x1, "flags = %d\n", info.flags);
309
310     ok(!memcmp(info.mem, "EFGHIJKLMNuvwxyzABCDklmnopqrst", 30), "mem %s\n", (char*)info.mem);
311
312     ok(!pFDSA_DeleteItem(&info, 4), "does not ret FALSE\n");
313
314     /* As shlwapi has allocated memory internally, Destroy will ret FALSE */
315     ok(!pFDSA_Destroy(&info), "FDSA_Destroy does not ret FALSE\n");
316
317
318     /* When Initialize is called with inc = 0, set it to 1 */
319     ok(pFDSA_Initialize(block_size, 0, &info, mem, init_blocks), "FDSA_Initialize rets FALSE\n");
320     ok(info.inc == 1, "inc = %d\n", info.inc);
321
322     /* This time, because shlwapi hasn't had to allocate memory
323        internally, Destroy rets non-zero */
324     ok(pFDSA_Destroy(&info), "FDSA_Destroy rets FALSE\n");
325
326
327     HeapFree(GetProcessHeap(), 0, mem);
328 }
329
330 START_TEST(ordinal)
331 {
332   hShlwapi = LoadLibraryA("shlwapi.dll");
333   ok(hShlwapi != 0, "LoadLibraryA failed\n");
334   if (!hShlwapi)
335     return;
336
337   pGetAcceptLanguagesA = (void*)GetProcAddress(hShlwapi, (LPSTR)14);
338   pSHSearchMapInt = (void*)GetProcAddress(hShlwapi, (LPSTR)198);
339   pSHAllocShared=(void*)GetProcAddress(hShlwapi,(char*)7);
340   pSHLockShared=(void*)GetProcAddress(hShlwapi,(char*)8);
341   pSHUnlockShared=(void*)GetProcAddress(hShlwapi,(char*)9);
342   pSHFreeShared=(void*)GetProcAddress(hShlwapi,(char*)10);
343
344   test_GetAcceptLanguagesA();
345   test_SHSearchMapInt();
346   test_alloc_shared();
347   test_fdsa();
348
349   FreeLibrary(hShlwapi);
350 }