1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Test for NtOpenThreadToken[Ex]
5  * PROGRAMMER:      Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include "precomp.h"
9 
START_TEST(NtOpenThreadToken)10 START_TEST(NtOpenThreadToken)
11 {
12     NTSTATUS Status;
13     HANDLE TokenHandle;
14     BOOLEAN OpenAsSelf;
15     ULONG HandleAttributes[] = { 0, OBJ_KERNEL_HANDLE };
16     ULONG i;
17 
18     TokenHandle = (HANDLE)0x55555555;
19     Status = NtOpenThreadToken(NtCurrentThread(),
20                                TOKEN_READ,
21                                TRUE,
22                                &TokenHandle);
23     ok(Status == STATUS_NO_TOKEN, "Status = %lx\n", Status);
24     ok(TokenHandle == (HANDLE)0x55555555 /* 2003 */ ||
25        TokenHandle == NULL /* Win7 */, "TokenHandle = %p\n", TokenHandle);
26 
27     Status = RtlImpersonateSelf(SecurityImpersonation);
28     ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
29     for (OpenAsSelf = FALSE; OpenAsSelf <= TRUE; OpenAsSelf++)
30     {
31         Status = NtOpenThreadToken(NtCurrentThread(),
32                                    TOKEN_READ,
33                                    OpenAsSelf,
34                                    &TokenHandle);
35         ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
36         if (NT_SUCCESS(Status))
37         {
38             ok((LONG_PTR)TokenHandle > 0, "TokenHandle = %p\n", TokenHandle);
39             Status = NtClose(TokenHandle);
40             ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
41         }
42 
43         for (i = 0; i < RTL_NUMBER_OF(HandleAttributes); i++)
44         {
45             Status = NtOpenThreadTokenEx(NtCurrentThread(),
46                                          TOKEN_READ,
47                                          OpenAsSelf,
48                                          HandleAttributes[i],
49                                          &TokenHandle);
50             ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
51             if (NT_SUCCESS(Status))
52             {
53                 ok((LONG_PTR)TokenHandle > 0, "TokenHandle = %p\n", TokenHandle);
54                 Status = NtClose(TokenHandle);
55                 ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
56             }
57         }
58     }
59 
60     TokenHandle = NULL;
61     Status = NtSetInformationThread(NtCurrentThread(),
62                                     ThreadImpersonationToken,
63                                     &TokenHandle,
64                                     sizeof(TokenHandle));
65     ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
66 }
67