[NTDLL_APITEST] Add ProcessBasePriority and ProcessRaisePriority classes unit tests...
authorGeorge Bișoc <fraizeraust99@gmail.com>
Sat, 6 Jun 2020 15:47:38 +0000 (17:47 +0200)
committerGitHub <noreply@github.com>
Sat, 6 Jun 2020 15:47:38 +0000 (17:47 +0200)
ProcessRaisePriority expects a buffer input information argument of KPRIORITY type definition as shown in this documentation (see the PROCESS_INFORMATION_CLASS structure members list of classes).
https://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FProcess%2FNtSetInformationProcess.html

modules/rostests/apitests/ntdll/NtSetInformationProcess.c

index b5fcc18..0b805b3 100644 (file)
@@ -7,6 +7,147 @@
 
 #include "precomp.h"
 
+static
+void
+Test_ProcBasePriorityClass(void)
+{
+    NTSTATUS Status;
+
+    /*
+     * Assign a priority of HIGH_PRIORITY (see pstypes.h).
+     * The function will fail with a status of STATUS_PRIVILEGE_NOT_HELD
+     * as the process who executed the caller doesn't have the requested
+     * privileges to perform the operation.
+    */
+    KPRIORITY BasePriority = HIGH_PRIORITY;
+
+    /* Everything is NULL */
+    Status = NtSetInformationProcess(NULL,
+                                     ProcessBasePriority,
+                                     NULL,
+                                     0);
+    ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
+
+    /* Set the base priority to an invalid process handle */
+    Status = NtSetInformationProcess(NULL,
+                                     ProcessBasePriority,
+                                     &BasePriority,
+                                     sizeof(KPRIORITY));
+    ok_hex(Status, STATUS_INVALID_HANDLE);
+
+    /* Don't input anything to the caller but the length information is valid */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessBasePriority,
+                                     NULL,
+                                     sizeof(KPRIORITY));
+    ok_hex(Status, STATUS_ACCESS_VIOLATION);
+
+    /* Give the base priority input to the caller but length is invalid */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessBasePriority,
+                                     &BasePriority,
+                                     0);
+    ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
+
+    /* The input buffer is misaligned and length information invalid */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessBasePriority,
+                                     (PVOID)1,
+                                     0);
+    ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
+
+    /* The input buffer is misaligned */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessBasePriority,
+                                     (PVOID)1,
+                                     sizeof(KPRIORITY));
+    ok_hex(Status, STATUS_DATATYPE_MISALIGNMENT);
+
+    /* The input buffer is misaligned (try with an alignment of 2) */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessBasePriority,
+                                     (PVOID)2,
+                                     sizeof(KPRIORITY));
+    ok_hex(Status, STATUS_DATATYPE_MISALIGNMENT);
+
+    /* Set the base priority but we have lack privileges to do so */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessBasePriority,
+                                     &BasePriority,
+                                     sizeof(KPRIORITY));
+    ok_hex(Status, STATUS_PRIVILEGE_NOT_HELD);
+
+    /*
+     * Assign a random priority value this time and
+     * set the base priority to the current process.
+    */
+    BasePriority = 8;
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessBasePriority,
+                                     &BasePriority,
+                                     sizeof(KPRIORITY));
+    ok_hex(Status, STATUS_SUCCESS);
+}
+
+static
+void
+Test_ProcRaisePriorityClass(void)
+{
+    NTSTATUS Status;
+
+    /* Raise the priority as much as possible */
+    ULONG RaisePriority = MAXIMUM_PRIORITY;
+
+    /* Everything is NULL */
+    Status = NtSetInformationProcess(NULL,
+                                     ProcessRaisePriority,
+                                     NULL,
+                                     0);
+    ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
+
+    /* A invalid handle to process is given */
+    Status = NtSetInformationProcess(NULL,
+                                     ProcessRaisePriority,
+                                     &RaisePriority,
+                                     sizeof(ULONG));
+    ok_hex(Status, STATUS_INVALID_HANDLE);
+
+    /* The input buffer is misaligned and length information invalid */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessRaisePriority,
+                                     (PVOID)1,
+                                     0);
+    ok_hex(Status, STATUS_INFO_LENGTH_MISMATCH);
+
+    /* A NULL buffer has been accessed */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessRaisePriority,
+                                     NULL,
+                                     sizeof(ULONG));
+    ok_hex(Status, STATUS_ACCESS_VIOLATION);
+
+    /* The input buffer is misaligned */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessRaisePriority,
+                                     (PVOID)1,
+                                     sizeof(ULONG));
+    ok_hex(Status, STATUS_DATATYPE_MISALIGNMENT);
+
+    /* The input buffer is misaligned -- try with an alignment size of 2 */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessRaisePriority,
+                                     (PVOID)2,
+                                     sizeof(ULONG));
+    ok_hex(Status, STATUS_DATATYPE_MISALIGNMENT);
+
+    /* Raise the priority of the given current process */
+    Status = NtSetInformationProcess(NtCurrentProcess(),
+                                     ProcessRaisePriority,
+                                     &RaisePriority,
+                                     sizeof(ULONG));
+    ok_hex(Status, STATUS_SUCCESS);
+}
+
 static
 void
 Test_ProcForegroundBackgroundClass(void)
@@ -77,4 +218,6 @@ Test_ProcForegroundBackgroundClass(void)
 START_TEST(NtSetInformationProcess)
 {
     Test_ProcForegroundBackgroundClass();
+    Test_ProcBasePriorityClass();
+    Test_ProcRaisePriorityClass();
 }