2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
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.
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.
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
25 #define WIN32_NO_STATUS
34 #include "wine/test.h"
36 typedef VOID (WINAPI *fnBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
37 typedef VOID (WINAPI *fnBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
38 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
39 POBJECTS_AND_NAME_A pObjName,
40 SE_OBJECT_TYPE ObjectType,
42 LPSTR InheritedObjectTypeName,
44 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
45 POBJECTS_AND_SID pObjSid,
47 GUID* pInheritedObjectGuid,
49 typedef LPSTR (WINAPI *fnGetTrusteeNameA)( PTRUSTEEA pTrustee );
50 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
51 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( LPCSTR str, PSID pSid );
52 typedef BOOL (WINAPI *fnGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
53 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
54 typedef DWORD (WINAPI *fnRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
55 typedef BOOL (WINAPI *fnCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
57 typedef NTSTATUS (WINAPI *fnLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
58 typedef NTSTATUS (WINAPI *fnLsaClose)(LSA_HANDLE);
59 typedef NTSTATUS (WINAPI *fnLsaFreeMemory)(PVOID);
60 typedef NTSTATUS (WINAPI *fnLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
64 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA;
65 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA;
66 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA;
67 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA;
68 fnGetTrusteeNameA pGetTrusteeNameA;
69 fnConvertSidToStringSidA pConvertSidToStringSidA;
70 fnConvertStringSidToSidA pConvertStringSidToSidA;
71 fnGetFileSecurityA pGetFileSecurityA;
72 fnRtlAdjustPrivilege pRtlAdjustPrivilege;
73 fnCreateWellKnownSid pCreateWellKnownSid;
74 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy;
76 fnLsaFreeMemory pLsaFreeMemory;
77 fnLsaOpenPolicy pLsaOpenPolicy;
81 SID_IDENTIFIER_AUTHORITY auth;
85 static void init(void)
87 hmod = GetModuleHandle("advapi32.dll");
90 static void test_str_sid(const char *str_sid)
95 if (pConvertStringSidToSidA(str_sid, &psid))
97 if (pConvertSidToStringSidA(psid, &temp))
99 trace(" %s: %s\n", str_sid, temp);
106 if (GetLastError() != ERROR_INVALID_SID)
107 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
109 trace(" %s: couldn't be converted\n", str_sid);
113 static void test_sid(void)
115 struct sidRef refs[] = {
116 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
117 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
118 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
119 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
120 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
121 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
123 const char noSubAuthStr[] = "S-1-5";
129 pConvertSidToStringSidA = (fnConvertSidToStringSidA)
130 GetProcAddress( hmod, "ConvertSidToStringSidA" );
131 if( !pConvertSidToStringSidA )
133 pConvertStringSidToSidA = (fnConvertStringSidToSidA)
134 GetProcAddress( hmod, "ConvertStringSidToSidA" );
135 if( !pConvertStringSidToSidA )
138 r = pConvertStringSidToSidA( NULL, NULL );
139 ok( !r, "expected failure with NULL parameters\n" );
140 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
142 ok( GetLastError() == ERROR_INVALID_PARAMETER,
143 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
146 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
147 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
148 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
151 r = pConvertStringSidToSidA( NULL, &str );
152 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
153 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
156 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
158 "expected failure with no sub authorities\n" );
159 ok( GetLastError() == ERROR_INVALID_SID,
160 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
163 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
167 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
169 ok( r, "failed to allocate sid\n" );
170 r = pConvertSidToStringSidA( psid, &str );
171 ok( r, "failed to convert sid\n" );
174 ok( !strcmp( str, refs[i].refStr ),
175 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
181 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
182 ok( r, "failed to parse sid string\n" );
185 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
186 sizeof(refs[i].auth) ),
187 "string sid %s didn't parse to expected value\n"
188 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
190 MAKEWORD( pisid->IdentifierAuthority.Value[1],
191 pisid->IdentifierAuthority.Value[0] ),
192 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
193 pisid->IdentifierAuthority.Value[4] ),
194 MAKEWORD( pisid->IdentifierAuthority.Value[3],
195 pisid->IdentifierAuthority.Value[2] ) ),
196 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
197 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
198 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
203 trace("String SIDs:\n");
244 static void test_trustee(void)
246 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
247 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
249 OBJECTS_AND_NAME_ oan;
253 char szObjectTypeName[] = "ObjectTypeName";
254 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
255 char szTrusteeName[] = "szTrusteeName";
256 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
258 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
260 pBuildTrusteeWithSidA = (fnBuildTrusteeWithSidA)
261 GetProcAddress( hmod, "BuildTrusteeWithSidA" );
262 pBuildTrusteeWithNameA = (fnBuildTrusteeWithNameA)
263 GetProcAddress( hmod, "BuildTrusteeWithNameA" );
264 pBuildTrusteeWithObjectsAndNameA = (fnBuildTrusteeWithObjectsAndNameA)
265 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
266 pBuildTrusteeWithObjectsAndSidA = (fnBuildTrusteeWithObjectsAndSidA)
267 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
268 pGetTrusteeNameA = (fnGetTrusteeNameA)
269 GetProcAddress (hmod, "GetTrusteeNameA" );
270 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
271 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
275 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
277 trace( "failed to init SID\n" );
281 /* test BuildTrusteeWithSidA */
282 memset( &trustee, 0xff, sizeof trustee );
283 pBuildTrusteeWithSidA( &trustee, psid );
285 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
286 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
287 "MultipleTrusteeOperation wrong\n");
288 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
289 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
290 ok( trustee.ptstrName == (LPSTR) psid, "ptstrName wrong\n" );
292 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
293 memset( &trustee, 0xff, sizeof trustee );
294 memset( &oas, 0xff, sizeof(oas) );
295 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
296 &InheritedObjectType, psid);
298 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
299 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
300 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
301 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
302 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
304 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
305 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
306 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
307 ok(oas.pSid == psid, "pSid wrong\n");
309 /* test GetTrusteeNameA */
310 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
312 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
313 memset( &trustee, 0xff, sizeof trustee );
314 memset( &oas, 0xff, sizeof(oas) );
315 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
316 &InheritedObjectType, psid);
318 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
319 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
320 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
321 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
322 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
324 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
325 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
326 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
327 ok(oas.pSid == psid, "pSid wrong\n");
331 /* test BuildTrusteeWithNameA */
332 memset( &trustee, 0xff, sizeof trustee );
333 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
335 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
336 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
337 "MultipleTrusteeOperation wrong\n");
338 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
339 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
340 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
342 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
343 memset( &trustee, 0xff, sizeof trustee );
344 memset( &oan, 0xff, sizeof(oan) );
345 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
346 szInheritedObjectTypeName, szTrusteeName);
348 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
349 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
350 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
351 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
352 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
354 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
355 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
356 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
357 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
359 /* test GetTrusteeNameA */
360 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
362 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
363 memset( &trustee, 0xff, sizeof trustee );
364 memset( &oan, 0xff, sizeof(oan) );
365 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
366 szInheritedObjectTypeName, szTrusteeName);
368 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
369 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
370 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
371 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
372 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
374 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
375 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
376 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
377 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
379 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
380 memset( &trustee, 0xff, sizeof trustee );
381 memset( &oan, 0xff, sizeof(oan) );
382 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
383 NULL, szTrusteeName);
385 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
386 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
387 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
388 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
389 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
391 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
392 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
393 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
394 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
397 /* If the first isn't defined, assume none is */
398 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
399 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
400 #define SE_CREATE_TOKEN_PRIVILEGE 2L
401 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
402 #define SE_LOCK_MEMORY_PRIVILEGE 4L
403 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
404 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
405 #define SE_TCB_PRIVILEGE 7L
406 #define SE_SECURITY_PRIVILEGE 8L
407 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
408 #define SE_LOAD_DRIVER_PRIVILEGE 10L
409 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
410 #define SE_SYSTEMTIME_PRIVILEGE 12L
411 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
412 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
413 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
414 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
415 #define SE_BACKUP_PRIVILEGE 17L
416 #define SE_RESTORE_PRIVILEGE 18L
417 #define SE_SHUTDOWN_PRIVILEGE 19L
418 #define SE_DEBUG_PRIVILEGE 20L
419 #define SE_AUDIT_PRIVILEGE 21L
420 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
421 #define SE_CHANGE_NOTIFY_PRIVILLEGE 23L
422 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
423 #define SE_UNDOCK_PRIVILEGE 25L
424 #define SE_SYNC_AGENT_PRIVILEGE 26L
425 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
426 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
427 #define SE_IMPERSONATE_PRIVILEGE 29L
428 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
429 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
430 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
432 static void test_allocateLuid(void)
434 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
438 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
439 if (!pAllocateLocallyUniqueId) return;
441 ret = pAllocateLocallyUniqueId(&luid1);
442 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
446 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
447 ret = pAllocateLocallyUniqueId(&luid2);
449 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
450 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
451 "AllocateLocallyUniqueId returned a well-known LUID\n");
452 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
453 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
454 ret = pAllocateLocallyUniqueId(NULL);
455 ok( !ret && GetLastError() == ERROR_NOACCESS,
456 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
460 static void test_lookupPrivilegeName(void)
462 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
463 char buf[MAX_PATH]; /* arbitrary, seems long enough */
464 DWORD cchName = sizeof(buf);
465 LUID luid = { 0, 0 };
469 /* check whether it's available first */
470 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
471 if (!pLookupPrivilegeNameA) return;
472 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
473 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
474 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
477 /* check with a short buffer */
479 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
480 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
481 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
482 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
484 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
485 "LookupPrivilegeNameA returned an incorrect required length for\n"
486 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
487 lstrlenA("SeCreateTokenPrivilege") + 1);
488 /* check a known value and its returned length on success */
489 cchName = sizeof(buf);
490 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
491 cchName == strlen("SeCreateTokenPrivilege"),
492 "LookupPrivilegeNameA returned an incorrect output length for\n"
493 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
494 (int)strlen("SeCreateTokenPrivilege"));
495 /* check known values */
496 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i < SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
499 cchName = sizeof(buf);
500 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
501 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
502 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
504 /* check a bogus LUID */
505 luid.LowPart = 0xdeadbeef;
506 cchName = sizeof(buf);
507 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
508 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
509 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
511 /* check on a bogus system */
512 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
513 cchName = sizeof(buf);
514 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
515 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
516 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE: %d\n",
526 static void test_lookupPrivilegeValue(void)
528 static const struct NameToLUID privs[] = {
529 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
530 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
531 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
532 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
533 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
534 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
535 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
536 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
537 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
538 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
539 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
540 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
541 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
542 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
543 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
544 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
545 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
546 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
547 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
548 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
549 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
550 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILLEGE },
551 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
552 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
553 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
554 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
555 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
556 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
557 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
559 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
564 /* check whether it's available first */
565 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
566 if (!pLookupPrivilegeValueA) return;
567 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
568 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
571 /* check a bogus system name */
572 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
573 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
574 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE: %d\n",
576 /* check a NULL string */
577 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
578 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
579 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
581 /* check a bogus privilege name */
582 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
583 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
584 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
586 /* check case insensitive */
587 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
589 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
591 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
593 /* Not all privileges are implemented on all Windows versions, so
594 * don't worry if the call fails
596 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
598 ok(luid.LowPart == privs[i].lowPart,
599 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
605 static void test_luid(void)
608 test_lookupPrivilegeName();
609 test_lookupPrivilegeValue();
612 static void test_FileSecurity(void)
614 char directory[MAX_PATH];
615 DWORD retval, outSize;
619 pGetFileSecurityA = (fnGetFileSecurityA)
620 GetProcAddress( hmod, "GetFileSecurityA" );
621 if( !pGetFileSecurityA )
624 retval = GetTempPathA(sizeof(directory), directory);
626 trace("GetTempPathA failed\n");
630 strcpy(directory, "\\Should not exist");
632 SetLastError(NO_ERROR);
633 result = pGetFileSecurityA( directory,OWNER_SECURITY_INFORMATION,buffer,0x40,&outSize);
634 ok(!result, "GetFileSecurityA should fail for not existing directories/files\n");
635 ok( (GetLastError() == ERROR_FILE_NOT_FOUND ) ||
636 (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) ,
637 "last error ERROR_FILE_NOT_FOUND / ERROR_CALL_NOT_IMPLEMENTED (98) "
638 "expected, got %d\n", GetLastError());
641 static void test_AccessCheck(void)
643 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
645 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
646 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
647 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
648 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
654 PRIVILEGE_SET *PrivSet;
659 NtDllModule = GetModuleHandle("ntdll.dll");
663 trace("not running on NT, skipping test\n");
666 pRtlAdjustPrivilege = (fnRtlAdjustPrivilege)
667 GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
668 if (!pRtlAdjustPrivilege) return;
670 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
671 res = InitializeAcl(Acl, 256, ACL_REVISION);
672 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
674 trace("ACLs not implemented - skipping tests\n");
677 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
679 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
680 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
682 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
683 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
684 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
686 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
687 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
688 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
690 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
691 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
693 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_ALL_ACCESS, AdminSid);
694 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
696 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
698 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
699 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
701 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
702 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
704 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
705 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
707 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
708 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
710 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
711 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
712 PrivSet->PrivilegeCount = 16;
714 ImpersonateSelf(SecurityImpersonation);
716 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
718 ret = OpenThreadToken(GetCurrentThread(),
719 TOKEN_QUERY, TRUE, &Token);
720 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
722 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
723 PrivSet, &PrivSetLen, &Access, &AccessStatus);
724 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
725 ok(AccessStatus && (Access == KEY_READ),
726 "AccessCheck failed to grant access with error %d\n",
729 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
730 PrivSet, &PrivSetLen, &Access, &AccessStatus);
731 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
733 "AccessCheck failed to grant any access with error %d\n",
735 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
738 PrivSet->PrivilegeCount = 16;
739 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
740 PrivSet, &PrivSetLen, &Access, &AccessStatus);
741 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
742 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
745 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
749 PrivSet->PrivilegeCount = 16;
750 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
751 PrivSet, &PrivSetLen, &Access, &AccessStatus);
752 ok(ret && AccessStatus && GetLastError() == 0,
753 "AccessCheck should have succeeded, error %d\n",
755 ok(Access == ACCESS_SYSTEM_SECURITY,
756 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
760 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
766 FreeSid(EveryoneSid);
771 HeapFree(GetProcessHeap(), 0, Acl);
772 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
773 HeapFree(GetProcessHeap(), 0, PrivSet);
776 /* test GetTokenInformation for the various attributes */
777 static void test_token_attr(void)
781 TOKEN_PRIVILEGES *Privileges;
782 TOKEN_GROUPS *Groups;
788 /* cygwin-like use case */
789 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
790 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
794 DWORD bufsize = sizeof(buf);
795 ret = GetTokenInformation(Token, TokenUser,(void*)buf, bufsize, &bufsize);
796 todo_wine ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
800 if(!pConvertSidToStringSidA)
803 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &Token);
804 GLE = GetLastError();
805 ok(ret || (GLE == ERROR_CALL_NOT_IMPLEMENTED),
806 "OpenProcessToken failed with error %d\n", GLE);
807 if(!ret && (GLE == ERROR_CALL_NOT_IMPLEMENTED))
809 trace("OpenProcessToken() not implemented, skipping test_token_attr()\n");
814 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
815 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
816 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
817 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
818 trace("TokenGroups:\n");
819 for (i = 0; i < Groups->GroupCount; i++)
821 DWORD NameLength = 255;
823 DWORD DomainLength = 255;
825 SID_NAME_USE SidNameUse;
826 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
829 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
830 ok(ret, "LookupAccountSid(%s) failed with error %d\n", SidString, GetLastError());
831 trace("\t%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
832 LocalFree(SidString);
834 HeapFree(GetProcessHeap(), 0, Groups);
837 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
838 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
839 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
840 User = HeapAlloc(GetProcessHeap(), 0, Size);
841 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
843 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
845 pConvertSidToStringSidA(User->User.Sid, &SidString);
846 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
847 LocalFree(SidString);
848 HeapFree(GetProcessHeap(), 0, User);
851 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
852 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
853 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
854 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
855 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
857 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
858 trace("TokenPrivileges:\n");
859 for (i = 0; i < Privileges->PrivilegeCount; i++)
862 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
863 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
864 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
868 typedef union _MAX_SID
871 char max[SECURITY_MAX_SID_SIZE];
874 static void test_sid_str(PSID * sid)
877 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
878 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
881 char account[MAX_PATH], domain[MAX_PATH];
883 DWORD acc_size = MAX_PATH;
884 DWORD dom_size = MAX_PATH;
885 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
886 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
887 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
889 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
890 else if (GetLastError() == ERROR_NONE_MAPPED)
891 trace(" %s couldn't be mapped\n", str_sid);
896 static void test_LookupAccountSid(void)
898 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
899 CHAR accountA[MAX_PATH], domainA[MAX_PATH];
900 DWORD acc_sizeA, dom_sizeA;
901 DWORD real_acc_sizeA, real_dom_sizeA;
902 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
903 DWORD acc_sizeW, dom_sizeW;
904 DWORD real_acc_sizeW, real_dom_sizeW;
905 PSID pUsersSid = NULL;
913 /* native windows crashes if account size, domain size, or name use is NULL */
915 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
916 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
917 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
918 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
920 /* not running on NT so give up */
921 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
924 real_acc_sizeA = MAX_PATH;
925 real_dom_sizeA = MAX_PATH;
926 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
927 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
929 /* try NULL account */
930 acc_sizeA = MAX_PATH;
931 dom_sizeA = MAX_PATH;
932 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
933 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
935 /* try NULL domain */
936 acc_sizeA = MAX_PATH;
937 dom_sizeA = MAX_PATH;
938 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
939 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
941 /* try a small account buffer */
943 dom_sizeA = MAX_PATH;
945 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
946 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
947 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
948 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
950 /* try a 0 sized account buffer */
952 dom_sizeA = MAX_PATH;
954 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
955 /* this can fail or succeed depending on OS version but the size will always be returned */
956 ok(acc_sizeA == real_acc_sizeA + 1,
957 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
958 real_acc_sizeA + 1, acc_sizeA);
960 /* try a 0 sized account buffer */
962 dom_sizeA = MAX_PATH;
963 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
964 /* this can fail or succeed depending on OS version but the size will always be returned */
965 ok(acc_sizeA == real_acc_sizeA + 1,
966 "LookupAccountSid() Expected acc_size = %u, got %u\n",
967 real_acc_sizeA + 1, acc_sizeA);
969 /* try a small domain buffer */
971 acc_sizeA = MAX_PATH;
973 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
974 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
975 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
976 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
978 /* try a 0 sized domain buffer */
980 acc_sizeA = MAX_PATH;
982 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
983 /* this can fail or succeed depending on OS version but the size will always be returned */
984 ok(dom_sizeA == real_dom_sizeA + 1,
985 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
986 real_dom_sizeA + 1, dom_sizeA);
988 /* try a 0 sized domain buffer */
990 acc_sizeA = MAX_PATH;
991 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
992 /* this can fail or succeed depending on OS version but the size will always be returned */
993 ok(dom_sizeA == real_dom_sizeA + 1,
994 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
995 real_dom_sizeA + 1, dom_sizeA);
997 real_acc_sizeW = MAX_PATH;
998 real_dom_sizeW = MAX_PATH;
999 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1000 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1002 /* native windows crashes if domainW or accountW is NULL */
1004 /* try a small account buffer */
1006 dom_sizeW = MAX_PATH;
1008 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1009 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1010 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1011 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1013 /* try a 0 sized account buffer */
1015 dom_sizeW = MAX_PATH;
1017 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1018 /* this can fail or succeed depending on OS version but the size will always be returned */
1019 ok(acc_sizeW == real_acc_sizeW + 1,
1020 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1021 real_acc_sizeW + 1, acc_sizeW);
1023 /* try a 0 sized account buffer */
1025 dom_sizeW = MAX_PATH;
1026 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1027 /* this can fail or succeed depending on OS version but the size will always be returned */
1028 ok(acc_sizeW == real_acc_sizeW + 1,
1029 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1030 real_acc_sizeW + 1, acc_sizeW);
1032 /* try a small domain buffer */
1034 acc_sizeW = MAX_PATH;
1036 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1037 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1038 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1039 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1041 /* try a 0 sized domain buffer */
1043 acc_sizeW = MAX_PATH;
1045 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1046 /* this can fail or succeed depending on OS version but the size will always be returned */
1047 ok(dom_sizeW == real_dom_sizeW + 1,
1048 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1049 real_dom_sizeW + 1, dom_sizeW);
1051 /* try a 0 sized domain buffer */
1053 acc_sizeW = MAX_PATH;
1054 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1055 /* this can fail or succeed depending on OS version but the size will always be returned */
1056 ok(dom_sizeW == real_dom_sizeW + 1,
1057 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1058 real_dom_sizeW + 1, dom_sizeW);
1062 pCreateWellKnownSid = (fnCreateWellKnownSid)GetProcAddress( hmod, "CreateWellKnownSid" );
1064 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1066 trace("Well Known SIDs:\n");
1067 for (i = 0; i <= 60; i++)
1069 size = SECURITY_MAX_SID_SIZE;
1070 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1072 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1074 acc_sizeA = MAX_PATH;
1075 dom_sizeA = MAX_PATH;
1076 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1077 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1078 LocalFree(str_sidA);
1083 if (GetLastError() != ERROR_INVALID_PARAMETER)
1084 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1086 trace(" %d: not supported\n", i);
1090 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1091 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1092 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1093 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1095 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1099 LSA_OBJECT_ATTRIBUTES object_attributes;
1101 ZeroMemory(&object_attributes, sizeof(object_attributes));
1102 object_attributes.Length = sizeof(object_attributes);
1104 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1105 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1106 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1108 /* try a more restricted access mask if necessary */
1109 if (status == STATUS_ACCESS_DENIED) {
1110 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1111 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1112 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1115 if (status == STATUS_SUCCESS)
1117 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1118 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1119 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1120 if (status == STATUS_SUCCESS)
1122 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1123 if (info->DomainSid)
1125 int count = *GetSidSubAuthorityCount(info->DomainSid);
1126 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1127 test_sid_str((PSID)&max_sid.sid);
1128 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1129 max_sid.sid.SubAuthorityCount = count + 1;
1130 test_sid_str((PSID)&max_sid.sid);
1131 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1132 test_sid_str((PSID)&max_sid.sid);
1133 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1134 test_sid_str((PSID)&max_sid.sid);
1135 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1136 test_sid_str((PSID)&max_sid.sid);
1137 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1138 test_sid_str((PSID)&max_sid.sid);
1139 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1140 test_sid_str((PSID)&max_sid.sid);
1141 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1142 test_sid_str((PSID)&max_sid.sid);
1143 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1144 test_sid_str((PSID)&max_sid.sid);
1145 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1146 test_sid_str((PSID)&max_sid.sid);
1147 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1148 test_sid_str((PSID)&max_sid.sid);
1149 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1150 test_sid_str((PSID)&max_sid.sid);
1151 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1152 test_sid_str((PSID)&max_sid.sid);
1153 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1154 test_sid_str((PSID)&max_sid.sid);
1157 pLsaFreeMemory((LPVOID)info);
1160 status = pLsaClose(handle);
1161 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1167 START_TEST(security)
1174 test_FileSecurity();
1177 test_LookupAccountSid();