Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / shlwapi / tests / ordinal.c
blob0d98ec8d8ce90905d4dfc7a4953984ec17396aa5
1 /* Unit test suite for SHLWAPI ordinal functions
3 * Copyright 2004 Jon Griffiths
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdio.h>
22 #include "wine/test.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "winuser.h"
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);
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);
37 static void test_GetAcceptLanguagesA(void)
38 { HRESULT retval;
39 DWORD buffersize, buffersize2, exactsize;
40 char buffer[100];
42 if (!pGetAcceptLanguagesA)
43 return;
45 buffersize = sizeof(buffer);
46 memset(buffer, 0, sizeof(buffer));
47 SetLastError(ERROR_SUCCESS);
48 retval = pGetAcceptLanguagesA( buffer, &buffersize);
49 trace("GetAcceptLanguagesA: retval %08x, size %08x, buffer (%s),"
50 " last error %u\n", retval, buffersize, buffer, GetLastError());
51 if(retval != S_OK) {
52 trace("GetAcceptLanguagesA: skipping tests\n");
53 return;
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 %u\n", GetLastError());
60 exactsize = strlen(buffer);
62 SetLastError(ERROR_SUCCESS);
63 retval = pGetAcceptLanguagesA( NULL, NULL);
64 ok(retval == E_FAIL,
65 "function result wrong: got %08x; expected E_FAIL\n", retval);
66 ok(ERROR_SUCCESS == GetLastError(), "last error set to %u\n", GetLastError());
68 buffersize = sizeof(buffer);
69 SetLastError(ERROR_SUCCESS);
70 retval = pGetAcceptLanguagesA( NULL, &buffersize);
71 ok(retval == E_FAIL,
72 "function result wrong: got %08x; 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 %u\n", GetLastError());
77 SetLastError(ERROR_SUCCESS);
78 retval = pGetAcceptLanguagesA( buffer, NULL);
79 ok(retval == E_FAIL,
80 "function result wrong: got %08x; expected E_FAIL\n", retval);
81 ok(ERROR_SUCCESS == GetLastError(), "last error set to %u\n", GetLastError());
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 %08x; expected E_FAIL\n", retval);
89 ok(buffersize == 0,
90 "buffersize wrong(changed) got %08x; expected 0 (2nd parameter; not on Win2k)\n", buffersize);
91 ok(ERROR_SUCCESS == GetLastError(), "last error set to %u\n", GetLastError());
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 %u; 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 %08x\n", lstrlenA(buffer), exactsize);
106 } else if((buffersize +1) == buffersize2) {
107 ok(ERROR_SUCCESS == GetLastError(),
108 "last error wrong: got %u; expected ERROR_SUCCESS\n", GetLastError());
109 ok(buffersize == strlen(buffer),
110 "buffer content (length) wrong: got %08x, expected %08x\n", lstrlenA(buffer), buffersize);
111 } else
112 ok( 0, "retval %08x, size %08x, buffer (%s), last error %u\n",
113 retval, buffersize, buffer, GetLastError());
114 break;
115 case E_INVALIDARG:
116 ok(buffersize == 0,
117 "buffersize wrong: got %08x, expected 0 (2nd parameter;Win2k)\n", buffersize);
118 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(),
119 "last error wrong: got %u; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
120 ok(buffersize2 == strlen(buffer),
121 "buffer content (length) wrong: got %08x, expected %08x\n", lstrlenA(buffer), buffersize2);
122 break;
123 default:
124 ok( 0, "retval %08x, size %08x, buffer (%s), last error %u\n",
125 retval, buffersize, buffer, GetLastError());
126 break;
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 %u; 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 %08x\n", lstrlenA(buffer), buffersize);
141 else
142 ok( 0, "retval %08x, size %08x, buffer (%s), last error %u\n",
143 retval, buffersize, buffer, GetLastError());
144 break;
145 case E_INVALIDARG:
146 ok(buffersize == 0,
147 "buffersize wrong: got %08x, expected 0 (2nd parameter;Win2k)\n", buffersize);
148 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(),
149 "last error wrong: got %u; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
150 ok(buffersize2 == strlen(buffer),
151 "buffer content (length) wrong: got %08x, expected %08x\n", lstrlenA(buffer), buffersize2);
152 break;
153 default:
154 ok( 0, "retval %08x, size %08x, buffer (%s), last error %u\n",
155 retval, buffersize, buffer, GetLastError());
156 break;
160 static void test_SHSearchMapInt(void)
162 int keys[8], values[8];
163 int i = 0;
165 if (!pSHSearchMapInt)
166 return;
168 memset(keys, 0, sizeof(keys));
169 memset(values, 0, sizeof(values));
170 keys[0] = 99; values[0] = 101;
172 /* NULL key/value lists crash native, so skip testing them */
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);
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);
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);
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);
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);
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);
201 static void test_alloc_shared(void)
203 DWORD procid;
204 HANDLE hmem;
205 int val;
206 int* p;
207 BOOL ret;
209 procid=GetCurrentProcessId();
210 hmem=pSHAllocShared(NULL,10,procid);
211 ok(hmem!=NULL,"SHAllocShared(NULL...) failed: %u\n", GetLastError());
212 ret = pSHFreeShared(hmem, procid);
213 ok( ret, "SHFreeShared failed: %u\n", GetLastError());
215 val=0x12345678;
216 hmem=pSHAllocShared(&val,4,procid);
217 ok(hmem!=NULL,"SHAllocShared(NULL...) failed: %u\n", GetLastError());
219 p=(int*)pSHLockShared(hmem,procid);
220 ok(p!=NULL,"SHLockShared failed: %u\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: %u\n", GetLastError());
226 ret = pSHFreeShared(hmem, procid);
227 ok( ret, "SHFreeShared failed: %u\n", GetLastError());
230 static void test_fdsa(void)
232 typedef struct
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;
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, const void *block);
246 BOOL (WINAPI *pFDSA_DeleteItem)(FDSA_info *info, DWORD where);
248 FDSA_info info;
249 int block_size = 10, init_blocks = 4, inc = 2;
250 DWORD ret;
251 char *mem;
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);
258 mem = HeapAlloc(GetProcessHeap(), 0, block_size * init_blocks);
259 memset(&info, 0, sizeof(info));
261 ok(pFDSA_Initialize(block_size, inc, &info, mem, init_blocks), "FDSA_Initialize rets FALSE\n");
262 ok(info.num_items == 0, "num_items = %d\n", info.num_items);
263 ok(info.mem == mem, "mem = %p\n", info.mem);
264 ok(info.blocks_alloced == init_blocks, "blocks_alloced = %d\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);
269 ret = pFDSA_InsertItem(&info, 1234, "1234567890");
270 ok(ret == 0, "ret = %d\n", ret);
271 ok(info.num_items == 1, "num_items = %d\n", info.num_items);
272 ok(info.mem == mem, "mem = %p\n", info.mem);
273 ok(info.blocks_alloced == init_blocks, "blocks_alloced = %d\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);
278 ret = pFDSA_InsertItem(&info, 1234, "abcdefghij");
279 ok(ret == 1, "ret = %d\n", ret);
281 ret = pFDSA_InsertItem(&info, 1, "klmnopqrst");
282 ok(ret == 1, "ret = %d\n", ret);
284 ret = pFDSA_InsertItem(&info, 0, "uvwxyzABCD");
285 ok(ret == 0, "ret = %d\n", ret);
286 ok(info.mem == mem, "mem = %p\n", info.mem);
287 ok(info.flags == 0, "flags = %d\n", info.flags);
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 = %d\n", ret);
292 ok(info.mem != mem, "mem = %p\n", info.mem);
293 ok(info.blocks_alloced == init_blocks + inc, "blocks_alloced = %d\n", info.blocks_alloced);
294 ok(info.flags == 0x1, "flags = %d\n", info.flags);
296 ok(!memcmp(info.mem, "EFGHIJKLMNuvwxyzABCD1234567890klmnopqrstabcdefghij", 50), "mem %s\n", (char*)info.mem);
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 = %d\n", info.blocks_alloced);
301 ok(info.flags == 0x1, "flags = %d\n", info.flags);
303 ok(!memcmp(info.mem, "EFGHIJKLMNuvwxyzABCDklmnopqrstabcdefghij", 40), "mem %s\n", (char*)info.mem);
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 = %d\n", info.blocks_alloced);
308 ok(info.flags == 0x1, "flags = %d\n", info.flags);
310 ok(!memcmp(info.mem, "EFGHIJKLMNuvwxyzABCDklmnopqrst", 30), "mem %s\n", (char*)info.mem);
312 ok(!pFDSA_DeleteItem(&info, 4), "does not ret FALSE\n");
314 /* As shlwapi has allocated memory internally, Destroy will ret FALSE */
315 ok(!pFDSA_Destroy(&info), "FDSA_Destroy does not ret FALSE\n");
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);
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");
327 HeapFree(GetProcessHeap(), 0, mem);
331 typedef struct SHELL_USER_SID {
332 SID_IDENTIFIER_AUTHORITY sidAuthority;
333 DWORD dwUserGroupID;
334 DWORD dwUserID;
335 } SHELL_USER_SID, *PSHELL_USER_SID;
336 typedef struct SHELL_USER_PERMISSION {
337 SHELL_USER_SID susID;
338 DWORD dwAccessType;
339 BOOL fInherit;
340 DWORD dwAccessMask;
341 DWORD dwInheritMask;
342 DWORD dwInheritAccessMask;
343 } SHELL_USER_PERMISSION, *PSHELL_USER_PERMISSION;
344 static void test_GetShellSecurityDescriptor(void)
346 SHELL_USER_PERMISSION supCurrentUserFull = {
347 { {SECURITY_NULL_SID_AUTHORITY}, 0, 0 },
348 ACCESS_ALLOWED_ACE_TYPE, FALSE,
349 GENERIC_ALL, 0, 0 };
350 #define MY_INHERITANCE 0xBE /* invalid value to proof behavior */
351 SHELL_USER_PERMISSION supEveryoneDenied = {
352 { {SECURITY_WORLD_SID_AUTHORITY}, SECURITY_WORLD_RID, 0 },
353 ACCESS_DENIED_ACE_TYPE, TRUE,
354 GENERIC_WRITE, MY_INHERITANCE | 0xDEADBA00, GENERIC_READ };
355 PSHELL_USER_PERMISSION rgsup[2] = {
356 &supCurrentUserFull, &supEveryoneDenied,
358 SECURITY_DESCRIPTOR* psd;
359 SECURITY_DESCRIPTOR* (WINAPI*pGetShellSecurityDescriptor)(PSHELL_USER_PERMISSION*,int);
361 pGetShellSecurityDescriptor=(void*)GetProcAddress(hShlwapi,(char*)475);
363 psd = pGetShellSecurityDescriptor(NULL, 2);
364 ok(psd==NULL, "GetShellSecurityDescriptor should fail\n");
365 psd = pGetShellSecurityDescriptor(rgsup, 0);
366 ok(psd==NULL, "GetShellSecurityDescriptor should fail\n");
368 psd = pGetShellSecurityDescriptor(rgsup, 2);
369 ok(psd!=NULL, "GetShellSecurityDescriptor failed\n");
370 if (psd!=NULL)
372 BOOL bHasDacl = FALSE, bDefaulted;
373 PACL pAcl;
374 DWORD dwRev;
375 SECURITY_DESCRIPTOR_CONTROL control;
377 ok(IsValidSecurityDescriptor(psd), "returned value is not valid SD\n");
379 ok(GetSecurityDescriptorControl(psd, &control, &dwRev),
380 "GetSecurityDescriptorControl failed with error %u\n", GetLastError());
381 ok(0 == (control & SE_SELF_RELATIVE), "SD should be absolute\n");
383 ok(GetSecurityDescriptorDacl(psd, &bHasDacl, &pAcl, &bDefaulted),
384 "GetSecurityDescriptorDacl failed with error %u\n", GetLastError());
386 ok(bHasDacl, "SD has no DACL\n");
387 if (bHasDacl)
389 ok(!bDefaulted, "DACL should not be defaulted\n");
391 ok(pAcl != NULL, "NULL DACL!\n");
392 if (pAcl != NULL)
394 ACL_SIZE_INFORMATION asiSize;
396 ok(IsValidAcl(pAcl), "DACL is not valid\n");
398 ok(GetAclInformation(pAcl, &asiSize, sizeof(asiSize), AclSizeInformation),
399 "GetAclInformation failed with error %u\n", GetLastError());
401 ok(asiSize.AceCount == 3, "Incorrect number of ACEs: %d entries\n", asiSize.AceCount);
402 if (asiSize.AceCount == 3)
404 ACCESS_ALLOWED_ACE *paaa; /* will use for DENIED too */
406 ok(GetAce(pAcl, 0, (LPVOID*)&paaa), "GetAce failed with error %u\n", GetLastError());
407 ok(paaa->Header.AceType == ACCESS_ALLOWED_ACE_TYPE,
408 "Invalid ACE type %d\n", paaa->Header.AceType);
409 ok(paaa->Header.AceFlags == 0, "Invalid ACE flags %x\n", paaa->Header.AceFlags);
410 ok(paaa->Mask == GENERIC_ALL, "Invalid ACE mask %x\n", paaa->Mask);
412 ok(GetAce(pAcl, 1, (LPVOID*)&paaa), "GetAce failed with error %u\n", GetLastError());
413 ok(paaa->Header.AceType == ACCESS_DENIED_ACE_TYPE,
414 "Invalid ACE type %d\n", paaa->Header.AceType);
415 /* first one of two ACEs generated from inheritable entry - without inheritance */
416 ok(paaa->Header.AceFlags == 0, "Invalid ACE flags %x\n", paaa->Header.AceFlags);
417 ok(paaa->Mask == GENERIC_WRITE, "Invalid ACE mask %x\n", paaa->Mask);
419 ok(GetAce(pAcl, 2, (LPVOID*)&paaa), "GetAce failed with error %u\n", GetLastError());
420 ok(paaa->Header.AceType == ACCESS_DENIED_ACE_TYPE,
421 "Invalid ACE type %d\n", paaa->Header.AceType);
422 /* second ACE - with inheritance */
423 ok(paaa->Header.AceFlags == MY_INHERITANCE,
424 "Invalid ACE flags %x\n", paaa->Header.AceFlags);
425 ok(paaa->Mask == GENERIC_READ, "Invalid ACE mask %x\n", paaa->Mask);
430 LocalFree(psd);
434 START_TEST(ordinal)
436 hShlwapi = LoadLibraryA("shlwapi.dll");
437 ok(hShlwapi != 0, "LoadLibraryA failed\n");
438 if (!hShlwapi)
439 return;
441 pGetAcceptLanguagesA = (void*)GetProcAddress(hShlwapi, (LPSTR)14);
442 pSHSearchMapInt = (void*)GetProcAddress(hShlwapi, (LPSTR)198);
443 pSHAllocShared=(void*)GetProcAddress(hShlwapi,(char*)7);
444 pSHLockShared=(void*)GetProcAddress(hShlwapi,(char*)8);
445 pSHUnlockShared=(void*)GetProcAddress(hShlwapi,(char*)9);
446 pSHFreeShared=(void*)GetProcAddress(hShlwapi,(char*)10);
448 test_GetAcceptLanguagesA();
449 test_SHSearchMapInt();
450 test_alloc_shared();
451 test_fdsa();
452 test_GetShellSecurityDescriptor();
454 FreeLibrary(hShlwapi);