1 /*
2  * PROJECT:     ReactOS API Tests
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Test for RtlValidateUnicodeString
5  * COPYRIGHT:   Copyright 2020 Hermes Belusca-Maito
6  */
7 
8 #include "precomp.h"
9 
10 START_TEST(RtlValidateUnicodeString)
11 {
12     NTSTATUS Status;
13     UNICODE_STRING String;
14     UNICODE_STRING ValidString = RTL_CONSTANT_STRING(L"My Wild String!");
15 
16     /* Start with a valid Unicode string */
17     String = ValidString;
18 
19     /* Non-zero flags are unsupported! */
20     Status = RtlValidateUnicodeString(0, &String);
21     ok(Status == STATUS_SUCCESS, "Status = 0x%lx\n", Status);
22     Status = RtlValidateUnicodeString(1, &String);
23     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
24     Status = RtlValidateUnicodeString(0xdeadbeef, &String);
25     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
26 
27     /* Empty string is allowed. Test also the flags. */
28     RtlInitEmptyUnicodeString(&String, NULL, 0);
29     Status = RtlValidateUnicodeString(0, &String);
30     ok(Status == STATUS_SUCCESS, "Status = 0x%lx\n", Status);
31     Status = RtlValidateUnicodeString(1, &String);
32     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
33     Status = RtlValidateUnicodeString(0xdeadbeef, &String);
34     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
35 
36     // With a non-NULL but empty buffer, and zero lengths.
37     RtlInitEmptyUnicodeString(&String, L"", 0);
38     Status = RtlValidateUnicodeString(0, &String);
39     ok(Status == STATUS_SUCCESS, "Status = 0x%lx\n", Status);
40     Status = RtlValidateUnicodeString(1, &String);
41     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
42     Status = RtlValidateUnicodeString(0xdeadbeef, &String);
43     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
44 
45     // With a non-NULL but empty buffer, and zero Length, non-zero MaximumLength.
46     RtlInitEmptyUnicodeString(&String, L"", sizeof(WCHAR));
47     String.Length = 0;
48     Status = RtlValidateUnicodeString(0, &String);
49     ok(Status == STATUS_SUCCESS, "Status = 0x%lx\n", Status);
50     Status = RtlValidateUnicodeString(1, &String);
51     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
52     Status = RtlValidateUnicodeString(0xdeadbeef, &String);
53     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
54 
55     /* NULL pointer is also allowed! Test also the flags. */
56     Status = RtlValidateUnicodeString(0, NULL);
57     ok(Status == STATUS_SUCCESS, "Status = 0x%lx\n", Status);
58     Status = RtlValidateUnicodeString(1, NULL);
59     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
60     Status = RtlValidateUnicodeString(0xdeadbeef, NULL);
61     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
62 
63 
64     /*
65      * Now test invalid strings.
66      */
67 
68     // NULL buffer but non-zero lengths.
69     String = ValidString;
70     String.Buffer = NULL;
71     Status = RtlValidateUnicodeString(0, &String);
72     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
73 
74     // NULL buffer, zero Length, non-zero MaximumLength.
75     String = ValidString;
76     String.Buffer = NULL;
77     String.Length = 0;
78     Status = RtlValidateUnicodeString(0, &String);
79     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
80 
81     // NULL buffer, non-zero Length, zero MaximumLength
82     // (tests also the case Length > MaximumLength that must fail).
83     String = ValidString;
84     String.Buffer = NULL;
85     String.MaximumLength = 0;
86     Status = RtlValidateUnicodeString(0, &String);
87     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
88 
89     // Non-NULL buffer, non-zero Length, zero MaximumLength.
90     String = ValidString;
91     String.MaximumLength = 0;
92     Status = RtlValidateUnicodeString(0, &String);
93     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
94 
95     /* Non-NULL buffer, odd lengths */
96 
97     String = ValidString;
98     String.Length--; // Length was already >= 2 so it remains > 0.
99     Status = RtlValidateUnicodeString(0, &String);
100     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
101 
102     String.MaximumLength--; // MaximumLength was already >= 2 so it remains > 0.
103     Status = RtlValidateUnicodeString(0, &String);
104     ok(Status == STATUS_INVALID_PARAMETER, "Status = 0x%lx\n", Status);
105 }
106