1 /*
2  * PROJECT:         ReactOS kernel-mode tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Kernel-Mode Test Suite NtCreateSection test user-mode part
5  * PROGRAMMER:      Pierre Schweitzer <pierre@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 
START_TEST(NtCreateSection)10 START_TEST(NtCreateSection)
11 {
12     PVOID Buffer;
13     SIZE_T FileSize;
14     NTSTATUS Status;
15     LARGE_INTEGER MaxFileSize;
16     HANDLE Handle, SectionHandle;
17     IO_STATUS_BLOCK IoStatusBlock;
18     OBJECT_ATTRIBUTES ObjectAttributes;
19     UNICODE_STRING InitOnCreate = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-NtCreateSection\\InitOnCreate");
20     UNICODE_STRING InitOnRW = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-NtCreateSection\\InitOnRW");
21     UNICODE_STRING InvalidInit = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-NtCreateSection\\InvalidInit");
22     DWORD Error;
23 
24     Error = KmtLoadAndOpenDriver(L"NtCreateSection", FALSE);
25     ok_eq_int(Error, ERROR_SUCCESS);
26     if (Error)
27         return;
28 
29     /* Test 0 */
30     InitializeObjectAttributes(&ObjectAttributes, &InvalidInit, OBJ_CASE_INSENSITIVE, NULL, NULL);
31     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
32                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
33     ok_eq_hex(Status, STATUS_SUCCESS);
34 
35     MaxFileSize.QuadPart = 512;
36     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
37                              PAGE_READWRITE, SEC_COMMIT, Handle);
38     ok_eq_hex(Status, STATUS_INVALID_FILE_FOR_SECTION);
39     if (NT_SUCCESS(Status)) NtClose(SectionHandle);
40     Status = NtClose(Handle);
41     ok_eq_hex(Status, STATUS_SUCCESS);
42 
43     /* Test 1 */
44     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
45     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
46                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
47     ok_eq_hex(Status, STATUS_SUCCESS);
48 
49     MaxFileSize.QuadPart = 512;
50     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
51                              PAGE_READWRITE, SEC_COMMIT, Handle);
52     ok_eq_hex(Status, STATUS_SUCCESS);
53 
54     Buffer = NULL;
55     FileSize = 0;
56     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
57                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
58     ok_eq_hex(Status, STATUS_SUCCESS);
59 
60     KmtStartSeh();
61     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
62     memset(Buffer, 0xBA, 512);
63     KmtEndSeh(STATUS_SUCCESS);
64 
65     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
66     ok_eq_hex(Status, STATUS_SUCCESS);
67     Status = NtClose(SectionHandle);
68     ok_eq_hex(Status, STATUS_SUCCESS);
69     Status = NtClose(Handle);
70     ok_eq_hex(Status, STATUS_SUCCESS);
71 
72     /* Test 2 */
73     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
74     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
75                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
76     ok_eq_hex(Status, STATUS_SUCCESS);
77 
78     MaxFileSize.QuadPart = 4096;
79     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
80                              PAGE_READWRITE, SEC_COMMIT, Handle);
81     ok_eq_hex(Status, STATUS_SUCCESS);
82 
83     Buffer = NULL;
84     FileSize = 0;
85     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
86                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
87     ok_eq_hex(Status, STATUS_SUCCESS);
88 
89     KmtStartSeh();
90     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
91     memset(Buffer, 0xBA, 4096);
92     KmtEndSeh(STATUS_SUCCESS);
93 
94     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
95     ok_eq_hex(Status, STATUS_SUCCESS);
96     Status = NtClose(SectionHandle);
97     ok_eq_hex(Status, STATUS_SUCCESS);
98     Status = NtClose(Handle);
99     ok_eq_hex(Status, STATUS_SUCCESS);
100 
101     /* Test 3 */
102     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
103     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
104                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
105     ok_eq_hex(Status, STATUS_SUCCESS);
106 
107     MaxFileSize.QuadPart = 512;
108     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
109                              PAGE_READWRITE, SEC_COMMIT, Handle);
110     ok_eq_hex(Status, STATUS_SUCCESS);
111 
112     Buffer = NULL;
113     FileSize = 0;
114     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
115                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
116     ok_eq_hex(Status, STATUS_SUCCESS);
117 
118     KmtStartSeh();
119     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
120     memset(Buffer, 0xBA, 512);
121     KmtEndSeh(STATUS_SUCCESS);
122 
123     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
124     ok_eq_hex(Status, STATUS_SUCCESS);
125     Status = NtClose(SectionHandle);
126     ok_eq_hex(Status, STATUS_SUCCESS);
127     Status = NtClose(Handle);
128     ok_eq_hex(Status, STATUS_SUCCESS);
129 
130     /* Test 4 */
131     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
132     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
133                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
134     ok_eq_hex(Status, STATUS_SUCCESS);
135 
136     MaxFileSize.QuadPart = 4096;
137     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
138                              PAGE_READWRITE, SEC_COMMIT, Handle);
139     ok_eq_hex(Status, STATUS_SUCCESS);
140 
141     Buffer = NULL;
142     FileSize = 0;
143     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
144                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
145     ok_eq_hex(Status, STATUS_SUCCESS);
146 
147     KmtStartSeh();
148     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
149     memset(Buffer, 0xBA, 4096);
150     KmtEndSeh(STATUS_SUCCESS);
151 
152     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
153     ok_eq_hex(Status, STATUS_SUCCESS);
154     Status = NtClose(SectionHandle);
155     ok_eq_hex(Status, STATUS_SUCCESS);
156     Status = NtClose(Handle);
157     ok_eq_hex(Status, STATUS_SUCCESS);
158 
159     /* Test 10 */
160     InitializeObjectAttributes(&ObjectAttributes, &InvalidInit, OBJ_CASE_INSENSITIVE, NULL, NULL);
161     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
162                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
163     ok_eq_hex(Status, STATUS_SUCCESS);
164 
165     MaxFileSize.QuadPart = 512;
166     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
167                              PAGE_READWRITE, SEC_COMMIT, Handle);
168     ok_eq_hex(Status, STATUS_INVALID_FILE_FOR_SECTION);
169     if (NT_SUCCESS(Status)) NtClose(SectionHandle);
170     Status = NtClose(Handle);
171     ok_eq_hex(Status, STATUS_SUCCESS);
172 
173     /* Test 11 */
174     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
175     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
176                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
177     ok_eq_hex(Status, STATUS_SUCCESS);
178 
179     MaxFileSize.QuadPart = 512;
180     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
181                              PAGE_READWRITE, SEC_COMMIT, Handle);
182     ok_eq_hex(Status, STATUS_SUCCESS);
183 
184     Buffer = NULL;
185     FileSize = 0;
186     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
187                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
188     ok_eq_hex(Status, STATUS_SUCCESS);
189 
190     KmtStartSeh();
191     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
192     memset(Buffer, 0xBA, 512);
193     KmtEndSeh(STATUS_SUCCESS);
194 
195     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
196     ok_eq_hex(Status, STATUS_SUCCESS);
197     Status = NtClose(SectionHandle);
198     ok_eq_hex(Status, STATUS_SUCCESS);
199     Status = NtClose(Handle);
200     ok_eq_hex(Status, STATUS_SUCCESS);
201 
202     /* Test 12 */
203     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
204     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
205                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
206     ok_eq_hex(Status, STATUS_SUCCESS);
207 
208     MaxFileSize.QuadPart = 4096;
209     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
210                              PAGE_READWRITE, SEC_COMMIT, Handle);
211     ok_eq_hex(Status, STATUS_SUCCESS);
212 
213     Buffer = NULL;
214     FileSize = 0;
215     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
216                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
217     ok_eq_hex(Status, STATUS_SUCCESS);
218 
219     KmtStartSeh();
220     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
221     memset(Buffer, 0xBA, 4096);
222     KmtEndSeh(STATUS_SUCCESS);
223 
224     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
225     ok_eq_hex(Status, STATUS_SUCCESS);
226     Status = NtClose(SectionHandle);
227     ok_eq_hex(Status, STATUS_SUCCESS);
228     Status = NtClose(Handle);
229     ok_eq_hex(Status, STATUS_SUCCESS);
230 
231     /* Test 13 */
232     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
233     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
234                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
235     ok_eq_hex(Status, STATUS_SUCCESS);
236 
237     MaxFileSize.QuadPart = 512;
238     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
239                              PAGE_READWRITE, SEC_COMMIT, Handle);
240     ok_eq_hex(Status, STATUS_SUCCESS);
241 
242     Buffer = NULL;
243     FileSize = 0;
244     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
245                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
246     ok_eq_hex(Status, STATUS_SUCCESS);
247 
248     KmtStartSeh();
249     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
250     memset(Buffer, 0xBA, 512);
251     KmtEndSeh(STATUS_SUCCESS);
252 
253     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
254     ok_eq_hex(Status, STATUS_SUCCESS);
255     Status = NtClose(SectionHandle);
256     ok_eq_hex(Status, STATUS_SUCCESS);
257     Status = NtClose(Handle);
258     ok_eq_hex(Status, STATUS_SUCCESS);
259 
260     /* Test 14 */
261     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
262     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
263                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
264     ok_eq_hex(Status, STATUS_SUCCESS);
265 
266     MaxFileSize.QuadPart = 4096;
267     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
268                              PAGE_READWRITE, SEC_COMMIT, Handle);
269     ok_eq_hex(Status, STATUS_SUCCESS);
270 
271     Buffer = NULL;
272     FileSize = 0;
273     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
274                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
275     ok_eq_hex(Status, STATUS_SUCCESS);
276 
277     KmtStartSeh();
278     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
279     memset(Buffer, 0xBA, 4096);
280     KmtEndSeh(STATUS_SUCCESS);
281 
282     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
283     ok_eq_hex(Status, STATUS_SUCCESS);
284     Status = NtClose(SectionHandle);
285     ok_eq_hex(Status, STATUS_SUCCESS);
286     Status = NtClose(Handle);
287     ok_eq_hex(Status, STATUS_SUCCESS);
288 
289     KmtCloseDriver();
290     KmtUnloadDriver();
291 }
292