986098edc7dee64ba335b47f98b15813f878c439
[reactos.git] / rostests / drivers / kmtest / ntos_io.c
1 /*
2 * NTOSKRNL Io Regressions KM-Test
3 * ReactOS Kernel Mode Regression Testing framework
4 *
5 * Copyright 2006 Aleksey Bragin <aleksey@reactos.org>
6 * Copyright 2008 Etersoft (Alexander Morozov)
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; see the file COPYING.LIB.
20 * If not, write to the Free Software Foundation,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 /* INCLUDES *******************************************************************/
25
26 #include <ddk/ntddk.h>
27 #include "kmtest.h"
28
29 #define NDEBUG
30 #include "debug.h"
31
32 VOID NtoskrnlIoDeviceInterface();
33
34
35 /* PUBLIC FUNCTIONS ***********************************************************/
36
37 VOID NtoskrnlIoMdlTest()
38 {
39 PMDL Mdl;
40 PIRP Irp;
41 PVOID VirtualAddress;
42 ULONG MdlSize = 2*4096+184; // 2 pages and some random value
43
44 StartTest();
45
46 // Try to alloc 2Gb MDL
47 Mdl = IoAllocateMdl(NULL, 2048UL*0x100000, FALSE, FALSE, NULL);
48
49 ok(Mdl == NULL,
50 "IoAllocateMdl should fail allocation of 2Gb or more, but got Mdl=0x%X",
51 (UINT32)Mdl);
52
53 if (Mdl)
54 IoFreeMdl(Mdl);
55
56 // Now create a valid MDL
57 VirtualAddress = ExAllocatePool(NonPagedPool, MdlSize);
58 Mdl = IoAllocateMdl(VirtualAddress, MdlSize, FALSE, FALSE, NULL);
59 ok(Mdl != NULL, "Mdl allocation failed");
60 // Check fields of the allocated struct
61 ok(Mdl->Next == NULL, "Mdl->Next should be NULL, but is 0x%X",
62 (UINT32)Mdl->Next);
63 ok(Mdl->ByteCount == MdlSize,
64 "Mdl->ByteCount should be equal to MdlSize, but is 0x%X",
65 (UINT32)Mdl->ByteCount);
66 // TODO: Check other fields of MDL struct
67
68 IoFreeMdl(Mdl);
69 // Allocate now with pointer to an Irp
70 Irp = IoAllocateIrp(1, FALSE);
71 ok(Irp != NULL, "IRP allocation failed");
72 Mdl = IoAllocateMdl(VirtualAddress, MdlSize, FALSE, FALSE, Irp);
73 ok(Mdl != NULL, "Mdl allocation failed");
74 ok(Irp->MdlAddress == Mdl, "Irp->MdlAddress should be 0x%X, but is 0x%X",
75 (UINT32)Mdl, (UINT32)Irp->MdlAddress);
76
77 IoFreeMdl(Mdl);
78
79 // TODO: Check a case when SecondaryBuffer == TRUE
80
81 IoFreeIrp(Irp);
82 ExFreePool(VirtualAddress);
83
84 FinishTest("NTOSKRNL Io Mdl");
85 }
86
87 VOID NtoskrnlIoIrpTest()
88 {
89 USHORT size;
90 IRP *iorp;
91
92 StartTest();
93
94 // 1st test
95 size = sizeof(IRP) + 5 * sizeof(IO_STACK_LOCATION);
96 iorp = ExAllocatePool(NonPagedPool, size);
97
98 if (NULL != iorp)
99 {
100 IoInitializeIrp(iorp, size, 5);
101
102 ok(6 == iorp->Type, "Irp type should be 6, but got %d\n", iorp->Type);
103 ok(iorp->Size == size, "Irp size should be %d, but got %d\n",
104 iorp->Size, size);
105 ok(5 == iorp->StackCount, "Irp StackCount should be 5, but got %d\n",
106 iorp->StackCount);
107 ok(6 == iorp->CurrentLocation, "Irp CurrentLocation should be 6, but got %d\n",
108 iorp->CurrentLocation);
109 ok(IsListEmpty(&iorp->ThreadListEntry), "IRP thread list is not empty\n");
110 ok ((PIO_STACK_LOCATION)(iorp + 1) + 5 ==
111 iorp->Tail.Overlay.CurrentStackLocation,
112 "CurrentStackLocation mismatch\n");
113
114 ExFreePool(iorp);
115 }
116
117 // 2nd test
118 size = sizeof(IRP) + 2 * sizeof(IO_STACK_LOCATION);
119 iorp = IoAllocateIrp(2, FALSE);
120
121 if (NULL != iorp)
122 {
123 ok(6 == iorp->Type, "Irp type should be 6, but got %d\n", iorp->Type);
124 ok(iorp->Size >= size,
125 "Irp size should be more or equal to %d, but got %d\n",
126 iorp->Size, size);
127 ok(2 == iorp->StackCount, "Irp StackCount should be 2, but got %d\n",
128 iorp->StackCount);
129 ok(3 == iorp->CurrentLocation, "Irp CurrentLocation should be 3, but got %d\n",
130 iorp->CurrentLocation);
131 ok(IsListEmpty(&iorp->ThreadListEntry), "IRP thread list is not empty\n");
132 ok ((PIO_STACK_LOCATION)(iorp + 1) + 2 ==
133 iorp->Tail.Overlay.CurrentStackLocation,
134 "CurrentStackLocation mismatch\n");
135 ok((IRP_ALLOCATED_FIXED_SIZE & iorp->AllocationFlags),
136 "IRP Allocation flags lack fixed size attribute\n");
137 ok(!(IRP_LOOKASIDE_ALLOCATION & iorp->AllocationFlags),
138 "IRP Allocation flags should not have lookaside allocation\n");
139
140 IoFreeIrp(iorp);
141 }
142
143 // 3rd test
144 size = sizeof(IRP) + 2 * sizeof(IO_STACK_LOCATION);
145 iorp = IoAllocateIrp(2, TRUE);
146
147 if (NULL != iorp)
148 {
149 ok(6 == iorp->Type, "Irp type should be 6, but got %d\n", iorp->Type);
150 ok(iorp->Size >= size,
151 "Irp size should be more or equal to %d, but got %d\n",
152 iorp->Size, size);
153 ok(2 == iorp->StackCount, "Irp StackCount should be 2, but got %d\n",
154 iorp->StackCount);
155 ok(3 == iorp->CurrentLocation, "Irp CurrentLocation should be 3, but got %d\n",
156 iorp->CurrentLocation);
157 ok(IsListEmpty(&iorp->ThreadListEntry), "IRP thread list is not empty\n");
158 ok ((PIO_STACK_LOCATION)(iorp + 1) + 2 ==
159 iorp->Tail.Overlay.CurrentStackLocation,
160 "CurrentStackLocation mismatch\n");
161 ok((IRP_ALLOCATED_FIXED_SIZE & iorp->AllocationFlags),
162 "IRP Allocation flags lack fixed size attribute\n");
163 ok((IRP_LOOKASIDE_ALLOCATION & iorp->AllocationFlags),
164 "IRP Allocation flags lack lookaside allocation\n");
165
166 IoFreeIrp(iorp);
167 }
168
169 FinishTest("NTOSKRNL Io Irp");
170 }
171
172 VOID NtoskrnlIoTests()
173 {
174 NtoskrnlIoMdlTest();
175 NtoskrnlIoDeviceInterface();
176 NtoskrnlIoIrpTest();
177 }