ReactOS  0.4.14-dev-376-gaedba84
NtWriteFile.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS API tests
3  * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE: Test for NtWriteFile
5  * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include "precomp.h"
9 
10 static
11 BOOL
13 {
14 #ifdef _WIN64
15  return TRUE;
16 #else
19 
22  &IsWow64,
23  sizeof(IsWow64),
24  NULL);
25  if (NT_SUCCESS(Status))
26  {
27  return IsWow64 != 0;
28  }
29 
30  return FALSE;
31 #endif
32 }
33 
34 static
35 ULONG
37 {
38  return Is64BitSystem() ? 48 : 28;
39 }
40 
41 static
42 ULONG
44 {
45  BOOL Ret;
47 
48  /* FIXME: Would be better to actually open systemroot */
50  ok(Ret != FALSE, "GetDiskFreeSpaceW failed: %lx\n", GetLastError());
51  if (!Ret)
52  {
53  SectorSize = 4096; /* On failure, assume max size */
54  }
55 
56  return SectorSize;
57 }
58 
60 {
63  UNICODE_STRING FileName = RTL_CONSTANT_STRING(L"\\SystemRoot\\ntdll-apitest-NtWriteFile-test.bin");
64  PVOID Buffer;
69  FILE_DISPOSITION_INFORMATION DispositionInfo;
70  ULONG TooLargeDataSize = (MAXUSHORT + 1 - SizeOfMdl()) / sizeof(ULONG_PTR) * PAGE_SIZE; // 0x3FF9000 on x86
71  ULONG LargeMdlMaxDataSize = TooLargeDataSize - PAGE_SIZE;
72 
73  trace("System is %d bits, Size of MDL: %lu\n", Is64BitSystem() ? 64 : 32, SizeOfMdl());
74  trace("Max MDL data size: 0x%lx bytes\n", LargeMdlMaxDataSize);
75 
76  ByteOffset.QuadPart = 0;
77 
78  Buffer = NULL;
79  BufferSize = TooLargeDataSize;
81  &Buffer,
82  0,
83  &BufferSize,
86  if (!NT_SUCCESS(Status))
87  {
88  skip("Failed to allocate memory, status %lx\n", Status);
89  return;
90  }
91 
93  &FileName,
95  NULL,
96  NULL);
100  &IoStatus,
101  NULL,
102  0,
103  0,
107  NULL,
108  0);
110 
111  /* non-cached, max size -- succeeds */
113  NULL,
114  NULL,
115  NULL,
116  &IoStatus,
117  Buffer,
118  LargeMdlMaxDataSize - PAGE_SIZE,
119  &ByteOffset,
120  NULL);
122 
123  /* non-cached, max size -- succeeds */
125  NULL,
126  NULL,
127  NULL,
128  &IoStatus,
129  Buffer,
130  LargeMdlMaxDataSize,
131  &ByteOffset,
132  NULL);
134 
135  /* non-cached, too large -- fails to allocate MDL
136  * Note: this returns STATUS_SUCCESS on Win7 -- higher MDL size limit */
138  NULL,
139  NULL,
140  NULL,
141  &IoStatus,
142  Buffer,
143  LargeMdlMaxDataSize + PAGE_SIZE,
144  &ByteOffset,
145  NULL);
147 
148  /* non-cached, unaligned -- fails with invalid parameter */
150  NULL,
151  NULL,
152  NULL,
153  &IoStatus,
154  Buffer,
155  LargeMdlMaxDataSize + 1,
156  &ByteOffset,
157  NULL);
159 
160  DispositionInfo.DeleteFile = TRUE;
162  &IoStatus,
163  &DispositionInfo,
164  sizeof(DispositionInfo),
169 
173  &IoStatus,
174  NULL,
175  0,
176  0,
179  NULL,
180  0);
182 
183  /* cached: succeeds with arbitrary length */
185  NULL,
186  NULL,
187  NULL,
188  &IoStatus,
189  Buffer,
190  LargeMdlMaxDataSize,
191  &ByteOffset,
192  NULL);
194 
196  NULL,
197  NULL,
198  NULL,
199  &IoStatus,
200  Buffer,
201  LargeMdlMaxDataSize + 1,
202  &ByteOffset,
203  NULL);
205 
207  NULL,
208  NULL,
209  NULL,
210  &IoStatus,
211  Buffer,
212  TooLargeDataSize,
213  &ByteOffset,
214  NULL);
216 
217  DispositionInfo.DeleteFile = TRUE;
219  &IoStatus,
220  &DispositionInfo,
221  sizeof(DispositionInfo),
226 
228  &Buffer,
229  &BufferSize,
230  MEM_RELEASE);
232 
233  /* Now, testing aligned/non aligned writes */
234 
236  trace("Sector is %ld bytes\n", BufferSize);
237 
239  &Buffer,
240  0,
241  &BufferSize,
243  PAGE_READONLY);
244  if (!NT_SUCCESS(Status))
245  {
246  skip("Failed to allocate memory, status %lx\n", Status);
247  return;
248  }
249 
253  &IoStatus,
254  NULL,
255  0,
256  0,
261  NULL,
262  0);
264 
265  /* non-cached, broken length -- fails with invalid parameter */
266  ByteOffset.QuadPart = 0;
268  NULL,
269  NULL,
270  NULL,
271  &IoStatus,
272  Buffer,
273  4,
274  &ByteOffset,
275  NULL);
277 
278  /* non-cached, broken offset -- fails with invalid parameter */
279  ByteOffset.QuadPart = 4;
281  NULL,
282  NULL,
283  NULL,
284  &IoStatus,
285  Buffer,
286  BufferSize,
287  &ByteOffset,
288  NULL);
290 
291  /* non-cached, good length and offset -- succeeds */
292  ByteOffset.QuadPart = 0;
294  NULL,
295  NULL,
296  NULL,
297  &IoStatus,
298  Buffer,
299  BufferSize,
300  &ByteOffset,
301  NULL);
303 
304  DispositionInfo.DeleteFile = TRUE;
306  &IoStatus,
307  &DispositionInfo,
308  sizeof(DispositionInfo),
313 
315  &Buffer,
316  &BufferSize,
317  MEM_RELEASE);
319 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define TRUE
Definition: types.h:120
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
NTSTATUS NTAPI NtSetInformationFile(HANDLE hFile, PIO_STATUS_BLOCK io, PVOID ptr, ULONG len, FILE_INFORMATION_CLASS FileInformationClass)
IN PLARGE_INTEGER IN ULONG IN BOOLEAN IN ULONG IN BOOLEAN OUT PIO_STATUS_BLOCK IoStatus
Definition: fatprocs.h:2650
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define MEM_COMMIT
Definition: nt_native.h:1313
#define ok_hex(expression, result)
Definition: atltest.h:94
static BOOL Is64BitSystem(VOID)
Definition: NtWriteFile.c:12
uint32_t ULONG_PTR
Definition: typedefs.h:63
HANDLE FileHandle
Definition: stats.c:38
#define FILE_NO_INTERMEDIATE_BUFFERING
Definition: from_kernel.h:28
unsigned int BOOL
Definition: ntddk_ex.h:94
#define MEM_RESERVE
Definition: nt_native.h:1314
#define FILE_WRITE_THROUGH
Definition: from_kernel.h:26
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
#define FILE_WRITE_DATA
Definition: nt_native.h:631
#define NtCurrentProcess()
Definition: nt_native.h:1657
BOOL WINAPI GetDiskFreeSpaceW(IN LPCWSTR lpRootPathName, OUT LPDWORD lpSectorsPerCluster, OUT LPDWORD lpBytesPerSector, OUT LPDWORD lpNumberOfFreeClusters, OUT LPDWORD lpTotalNumberOfClusters)
Definition: disk.c:173
#define trace
Definition: atltest.h:70
NTSTATUS NTAPI NtCreateFile(OUT PHANDLE FileHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK IoStatusBlock, IN PLARGE_INTEGER AllocationSize OPTIONAL, IN ULONG FileAttributes, IN ULONG ShareAccess, IN ULONG CreateDisposition, IN ULONG CreateOptions, IN PVOID EaBuffer OPTIONAL, IN ULONG EaLength)
static ULONG SizeOfSector(VOID)
Definition: NtWriteFile.c:43
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
START_TEST(NtWriteFile)
Definition: NtWriteFile.c:59
#define BufferSize
Definition: classpnp.h:419
static ULONG SizeOfMdl(VOID)
Definition: NtWriteFile.c:36
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static const WCHAR L[]
Definition: oid.c:1250
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define SYNCHRONIZE
Definition: nt_native.h:61
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4367
#define ok(value,...)
Definition: atltest.h:57
#define PAGE_READONLY
Definition: compat.h:127
#define MAXUSHORT
Definition: typedefs.h:81
#define skip(...)
Definition: atltest.h:64
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define MEM_RELEASE
Definition: nt_native.h:1316
BOOL IsWow64(VOID)
Definition: user_lib.cpp:579
unsigned int ULONG
Definition: retypes.h:1
#define FILE_SUPERSEDE
Definition: from_kernel.h:53
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
_In_ ULONG SectorSize
Definition: halfuncs.h:291
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define DELETE
Definition: nt_native.h:57
IN PDCB IN PCCB IN VBO IN OUT PULONG OUT PDIRENT OUT PBCB OUT PVBO ByteOffset
Definition: fatprocs.h:716
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5090