1 /*
2  * PROJECT:         ReactOS API tests
3  * LICENSE:         GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:         Tests for the NtSetInformationThread API
5  * COPYRIGHT:       Copyright 2020 George Bișoc <george.bisoc@reactos.org>
6  */
7 
8 #include "precomp.h"
9 
10 static
11 void
12 Test_ThreadPriorityClass(void)
13 {
14     NTSTATUS Status;
15     KPRIORITY *Priority;
16 
17     Priority = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(KPRIORITY));
18     if (Priority == NULL)
19     {
20         skip("Failed to allocate memory for thread priority variable!\n");
21         return;
22     }
23 
24     /* Assign a priority */
25     *Priority = 11;
26 
27     /* Everything is NULL */
28     Status = NtSetInformationThread(NULL,
29                                     ThreadPriority,
30                                     NULL,
31                                     0);
32     ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
33 
34     /* Give an invalid thread handle */
35     Status = NtSetInformationThread(NULL,
36                                     ThreadPriority,
37                                     Priority,
38                                     sizeof(KPRIORITY));
39     ok_hex(Status, STATUS_INVALID_HANDLE);
40 
41     /* Don't set any priority to the thread */
42     Status = NtSetInformationThread(GetCurrentThread(),
43                                     ThreadPriority,
44                                     NULL,
45                                     sizeof(KPRIORITY));
46     ok_hex(Status, STATUS_ACCESS_VIOLATION);
47 
48     /* The information length is incorrect */
49     Status = NtSetInformationThread(GetCurrentThread(),
50                                     ThreadPriority,
51                                     Priority,
52                                     0);
53     ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
54 
55     /* The buffer is misaligned and information length is incorrect */
56     Status = NtSetInformationThread(GetCurrentThread(),
57                                     ThreadPriority,
58                                     (PVOID)1,
59                                     0);
60     ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
61 
62     /* The buffer is misaligned */
63     Status = NtSetInformationThread(GetCurrentThread(),
64                                     ThreadPriority,
65                                     (PVOID)1,
66                                     sizeof(KPRIORITY));
67     ok_hex(Status, STATUS_DATATYPE_MISALIGNMENT);
68 
69     /* The buffer is misaligned -- try with an alignment size of 2 */
70     Status = NtSetInformationThread(GetCurrentThread(),
71                                     ThreadPriority,
72                                     (PVOID)2,
73                                     sizeof(KPRIORITY));
74     ok_hex(Status, STATUS_DATATYPE_MISALIGNMENT);
75 
76     /* Set the priority to the current thread */
77     Status = NtSetInformationThread(GetCurrentThread(),
78                                     ThreadPriority,
79                                     Priority,
80                                     sizeof(KPRIORITY));
81     ok_hex(Status, STATUS_SUCCESS);
82 
83     /*
84      * Set the priority as LOW_REALTIME_PRIORITY,
85      * we don't have privileges to do so.
86     */
87     *Priority = LOW_REALTIME_PRIORITY;
88     Status = NtSetInformationThread(GetCurrentThread(),
89                                     ThreadPriority,
90                                     Priority,
91                                     sizeof(KPRIORITY));
92     ok_hex(Status, STATUS_PRIVILEGE_NOT_HELD);
93 
94     HeapFree(GetProcessHeap(), 0, Priority);
95 }
96 
97 START_TEST(NtSetInformationThread)
98 {
99     Test_ThreadPriorityClass();
100 }
101