9537c6ca5a20110a5d6d5eccdfae043aac1750a5
[reactos.git] / rostests / kmtests / ntos_io / IoFilesystem.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite File System test
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <kmt_test.h>
9
10 /* FIXME: Test this stuff on non-FAT volumes */
11
12 static
13 VOID
14 TestSharedCacheMap(VOID)
15 {
16 NTSTATUS Status;
17 struct
18 {
19 PCWSTR ParentPath;
20 PCWSTR RelativePath;
21 } Tests[] =
22 {
23 { 0, L"\\SystemRoot\\system32\\drivers\\etc\\hosts" },
24 { L"\\SystemRoot", L"system32\\drivers\\etc\\hosts" },
25 { L"\\SystemRoot\\system32", L"drivers\\etc\\hosts" },
26 { L"\\SystemRoot\\system32\\drivers", L"etc\\hosts" },
27 { L"\\SystemRoot\\system32\\drivers\\etc", L"hosts" },
28 };
29 OBJECT_ATTRIBUTES ObjectAttributes;
30 IO_STATUS_BLOCK IoStatus;
31 UNICODE_STRING ParentPath;
32 UNICODE_STRING RelativePath;
33 HANDLE ParentHandle[RTL_NUMBER_OF(Tests)] = { NULL };
34 HANDLE FileHandle[RTL_NUMBER_OF(Tests)] = { NULL };
35 PFILE_OBJECT FileObject[RTL_NUMBER_OF(Tests)] = { NULL };
36 PFILE_OBJECT SystemRootObject = NULL;
37 UCHAR Buffer[32];
38 HANDLE EventHandle;
39 LARGE_INTEGER FileOffset;
40 ULONG i;
41
42 /* We need an event for ZwReadFile */
43 InitializeObjectAttributes(&ObjectAttributes,
44 NULL,
45 OBJ_KERNEL_HANDLE,
46 NULL,
47 NULL);
48 Status = ZwCreateEvent(&EventHandle,
49 SYNCHRONIZE,
50 &ObjectAttributes,
51 NotificationEvent,
52 FALSE);
53 if (skip(NT_SUCCESS(Status), "No event\n"))
54 goto Cleanup;
55
56 /* Open all test files and get their FILE_OBJECT pointers */
57 for (i = 0; i < RTL_NUMBER_OF(Tests); i++)
58 {
59 if (Tests[i].ParentPath)
60 {
61 RtlInitUnicodeString(&ParentPath, Tests[i].ParentPath);
62 InitializeObjectAttributes(&ObjectAttributes,
63 &ParentPath,
64 OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
65 NULL,
66 NULL);
67 Status = ZwOpenFile(&ParentHandle[i],
68 GENERIC_READ,
69 &ObjectAttributes,
70 &IoStatus,
71 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
72 0);
73 ok_eq_hex(Status, STATUS_SUCCESS);
74 if (skip(NT_SUCCESS(Status), "No parent handle %lu\n", i))
75 goto Cleanup;
76 }
77
78 RtlInitUnicodeString(&RelativePath, Tests[i].RelativePath);
79 InitializeObjectAttributes(&ObjectAttributes,
80 &RelativePath,
81 OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
82 ParentHandle[i],
83 NULL);
84 Status = ZwOpenFile(&FileHandle[i],
85 FILE_ALL_ACCESS,
86 &ObjectAttributes,
87 &IoStatus,
88 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
89 0);
90 ok_eq_hex(Status, STATUS_SUCCESS);
91 if (skip(NT_SUCCESS(Status), "No file handle %lu\n", i))
92 goto Cleanup;
93
94 Status = ObReferenceObjectByHandle(FileHandle[i],
95 FILE_ALL_ACCESS,
96 *IoFileObjectType,
97 KernelMode,
98 (PVOID*)&FileObject[i],
99 NULL);
100 ok_eq_hex(Status, STATUS_SUCCESS);
101 if (skip(NT_SUCCESS(Status), "No file object %lu\n", i))
102 goto Cleanup;
103 }
104
105 /* Also get a file object for the SystemRoot directory */
106 Status = ObReferenceObjectByHandle(ParentHandle[1],
107 GENERIC_READ,
108 *IoFileObjectType,
109 KernelMode,
110 (PVOID*)&SystemRootObject,
111 NULL);
112 ok_eq_hex(Status, STATUS_SUCCESS);
113 if (skip(NT_SUCCESS(Status), "No SystemRoot object\n"))
114 goto Cleanup;
115
116 /* Before read, caching is not initialized */
117 ok_eq_pointer(SystemRootObject->SectionObjectPointer, NULL);
118 for (i = 0; i < RTL_NUMBER_OF(Tests); i++)
119 {
120 ok(FileObject[i]->SectionObjectPointer != NULL, "FileObject[%lu]->SectionObjectPointer = NULL\n", i);
121 ok(FileObject[i]->SectionObjectPointer == FileObject[0]->SectionObjectPointer,
122 "FileObject[%lu]->SectionObjectPointer = %p, expected %p\n",
123 i, FileObject[i]->SectionObjectPointer, FileObject[0]->SectionObjectPointer);
124 }
125 if (!skip(FileObject[0]->SectionObjectPointer != NULL, "No section object pointers\n"))
126 ok_eq_pointer(FileObject[0]->SectionObjectPointer->SharedCacheMap, NULL);
127
128 /* Perform a read on one handle to initialize caching */
129 FileOffset.QuadPart = 0;
130 Status = ZwReadFile(FileHandle[0],
131 EventHandle,
132 NULL,
133 NULL,
134 &IoStatus,
135 Buffer,
136 sizeof(Buffer),
137 &FileOffset,
138 NULL);
139 if (Status == STATUS_PENDING)
140 {
141 Status = ZwWaitForSingleObject(EventHandle, FALSE, NULL);
142 ok_eq_hex(Status, STATUS_SUCCESS);
143 Status = IoStatus.Status;
144 }
145 ok_eq_hex(Status, STATUS_SUCCESS);
146
147 /* Now we see a SharedCacheMap for the file */
148 ok_eq_pointer(SystemRootObject->SectionObjectPointer, NULL);
149 for (i = 0; i < RTL_NUMBER_OF(Tests); i++)
150 {
151 ok(FileObject[i]->SectionObjectPointer != NULL, "FileObject[%lu]->SectionObjectPointer = NULL\n", i);
152 ok(FileObject[i]->SectionObjectPointer == FileObject[0]->SectionObjectPointer,
153 "FileObject[%lu]->SectionObjectPointer = %p, expected %p\n",
154 i, FileObject[i]->SectionObjectPointer, FileObject[0]->SectionObjectPointer);
155 }
156 if (!skip(FileObject[0]->SectionObjectPointer != NULL, "No section object pointers\n"))
157 ok(FileObject[0]->SectionObjectPointer->SharedCacheMap != NULL, "SharedCacheMap is NULL\n");
158
159 Cleanup:
160 if (SystemRootObject)
161 ObDereferenceObject(SystemRootObject);
162 if (EventHandle)
163 ObCloseHandle(EventHandle, KernelMode);
164 for (i = 0; i < RTL_NUMBER_OF(Tests); i++)
165 {
166 if (FileObject[i])
167 ObDereferenceObject(FileObject[i]);
168 if (FileHandle[i])
169 ObCloseHandle(FileHandle[i], KernelMode);
170 if (ParentHandle[i])
171 ObCloseHandle(ParentHandle[i], KernelMode);
172 }
173 }
174
175 START_TEST(IoFilesystem)
176 {
177 TestSharedCacheMap();
178 }