ReactOS  0.4.15-dev-3173-g40ee59d
RtlpApplyLengthFunction.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS API Tests
3  * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE: Test for RtlpApplyLengthFunction
5  * COPYRIGHT: Copyright 2021 Mark Jansen <mark.jansen@reactos.org>
6  */
7 
8 #include "precomp.h"
9 #include <ntstrsafe.h>
10 
11 
13 NTAPI
15  IN ULONG Type,
16  IN PVOID UnicodeStringOrUnicodeStringBuffer,
17  IN NTSTATUS(NTAPI*LengthFunction)(ULONG, PUNICODE_STRING, PULONG));
18 
19 
21 {
22  ok_int(*Length, 0);
23  /* Show that this is ignored when an error is returned */
24  *Length = 3;
26 }
27 
29 {
30  ok_int(*Length, 0);
31  *Length = 4;
32  return STATUS_SUCCESS;
33 }
34 
36 {
37  ok_int(*Length, 0);
38  *Length = 4;
39  return (NTSTATUS)1;
40 }
41 
43 {
44  /* Use Buffer as length, to show that the function does not interpret the contents at all */
45  *Length = (ULONG)(ULONG_PTR)String->Buffer;
46  return STATUS_SUCCESS;
47 }
48 
49 
51 {
53  /* Show that RtlpApplyLengthFunction does not interpret anything in the UNICODE_STRING */
54  UNICODE_STRING String = { 1, 2, (PWSTR)3 };
56  WCHAR StaticBuffer[10] = { 0 };
57  ULONG n;
58 
60  ok_int(String.Length, 1);
62 
64  ok_int(String.Length, 1);
66 
67  /* Show that no flag is accepted */
68  for (n = 0; n < 32; ++n)
69  {
71  ok_int(String.Length, 1);
73  }
74 
76  ok_int(String.Length, 1);
78 
79  /* Still Length 1 when the function returns an error */
81  ok_int(String.Length, 1);
83 
85  ok_int(String.Length, 8); /* Value returned from LengthFunction is multiplied by sizeof(WCHAR) */
87 
88  String.Length = 1;
90  ok_int(String.Length, 8);
91  ok_hex(Status, STATUS_SUCCESS); /* Returns STATUS_SUCCESS regardless of success code from the function */
92 
93  /* Show max length */
95  String.Length = 2;
97  ok_int(String.Length, UNICODE_STRING_MAX_CHARS * sizeof(WCHAR));
99 
100  String.Buffer = (PWCHAR)(UNICODE_STRING_MAX_CHARS + 1);
101  String.Length = 2;
103  ok_int(String.Length, 2); /* Unchanged */
105 
106  /* Now try it with the RTL_UNICODE_STRING_BUFFER, this works fine on 2k3 but not on Win10!! */
107  RtlInitBuffer(&Buffer.ByteBuffer, (PUCHAR)StaticBuffer, sizeof(StaticBuffer));
108  /* In this case the Buffer is modified, so we should have a valid UNICODE_STRING! */
109  Buffer.String.Length = 5;
110  Buffer.String.MaximumLength = Buffer.ByteBuffer.StaticSize;
111  Buffer.String.Buffer = (PWSTR)Buffer.ByteBuffer.Buffer;
112  wcscpy(StaticBuffer, L"123456789");
113 
114  /* Show that no flag is accepted */
115  for (n = 0; n < 32; ++n)
116  {
118  ok_int(Buffer.String.Length, 5);
120  ok_wstr(StaticBuffer, L"123456789");
121  }
122 
123  /* Still Length 1 when the function returns an error */
125  ok_int(Buffer.String.Length, 5);
127  ok_wstr(StaticBuffer, L"123456789");
128 
130  ok_int(Buffer.String.Length, 8); /* Value returned from LengthFunction is multiplied by sizeof(WCHAR) */
132  ok_wstr(StaticBuffer, L"1234"); /* Buffer is truncated */
133  ok_wstr(StaticBuffer + 5, L"6789"); /* Rest is not overwritten*/
134 
135  Buffer.String.Length = 1;
136  wcscpy(StaticBuffer, L"123456789");
138  ok_int(Buffer.String.Length, 8);
139  ok_hex(Status, STATUS_SUCCESS); /* Returns STATUS_SUCCESS regardless of success code from the function */
140  ok_wstr(StaticBuffer, L"1234"); /* Buffer is truncated */
141  ok_wstr(StaticBuffer + 5, L"6789"); /* Rest is not overwritten*/
142 }
143 
#define IN
Definition: typedefs.h:39
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
NTSTATUS NTAPI LengthFunctionFail(ULONG Unknown, PUNICODE_STRING String, PULONG Length)
uint16_t * PWSTR
Definition: typedefs.h:56
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
GLdouble n
Definition: glext.h:7729
uint16_t * PWCHAR
Definition: typedefs.h:56
#define ok_hex(expression, result)
Definition: atltest.h:94
uint32_t ULONG_PTR
Definition: typedefs.h:65
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2430
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define ok_int(expression, result)
Definition: atltest.h:134
Definition: bufpool.h:45
#define STATUS_NAME_TOO_LONG
Definition: ntstatus.h:498
Status
Definition: gdiplustypes.h:24
#define STATUS_INVALID_ACCOUNT_NAME
Definition: ntstatus.h:334
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define UNICODE_STRING_MAX_CHARS
NTSTATUS NTAPI RtlpApplyLengthFunction(IN ULONG Flags, IN ULONG Type, IN PVOID UnicodeStringOrUnicodeStringBuffer, IN NTSTATUS(NTAPI *LengthFunction)(ULONG, PUNICODE_STRING, PULONG))
Definition: path.c:446
NTSTATUS NTAPI LengthFunctionOk(ULONG Unknown, PUNICODE_STRING String, PULONG Length)
__wchar_t WCHAR
Definition: xmlstorage.h:180
START_TEST(RtlpApplyLengthFunction)
Type
Definition: Type.h:6
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
#define NTSTATUS
Definition: precomp.h:20
#define ok_wstr(x, y)
Definition: atltest.h:130
NTSTATUS NTAPI LengthFunctionReturn1(ULONG Unknown, PUNICODE_STRING String, PULONG Length)
unsigned int * PULONG
Definition: retypes.h:1
#define NULL
Definition: types.h:112
NTSTATUS NTAPI LengthFunctionCopyLen(ULONG Unknown, PUNICODE_STRING String, PULONG Length)
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_SUCCESS
Definition: shellext.h:65