[KMTEST]
[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
33 /* PUBLIC FUNCTIONS ***********************************************************/
34
35 VOID NtoskrnlIoMdlTest(HANDLE KeyHandle)
36 {
37 PMDL Mdl;
38 PIRP Irp;
39 PVOID VirtualAddress;
40 ULONG MdlSize = 2*4096+184; // 2 pages and some random value
41
42 StartTest();
43
44 // Try to alloc 2Gb MDL
45 Mdl = IoAllocateMdl(NULL, 2048UL*0x100000, FALSE, FALSE, NULL);
46
47 ok(Mdl == NULL,
48 "IoAllocateMdl should fail allocation of 2Gb or more, but got Mdl=0x%X",
49 (UINT32)Mdl);
50
51 if (Mdl)
52 IoFreeMdl(Mdl);
53
54 // Now create a valid MDL
55 VirtualAddress = ExAllocatePool(NonPagedPool, MdlSize);
56 Mdl = IoAllocateMdl(VirtualAddress, MdlSize, FALSE, FALSE, NULL);
57 ok(Mdl != NULL, "Mdl allocation failed");
58 // Check fields of the allocated struct
59 ok(Mdl->Next == NULL, "Mdl->Next should be NULL, but is 0x%X",
60 (UINT32)Mdl->Next);
61 ok(Mdl->ByteCount == MdlSize,
62 "Mdl->ByteCount should be equal to MdlSize, but is 0x%X",
63 (UINT32)Mdl->ByteCount);
64 // TODO: Check other fields of MDL struct
65
66 IoFreeMdl(Mdl);
67 // Allocate now with pointer to an Irp
68 Irp = IoAllocateIrp(1, FALSE);
69 ok(Irp != NULL, "IRP allocation failed");
70 Mdl = IoAllocateMdl(VirtualAddress, MdlSize, FALSE, FALSE, Irp);
71 ok(Mdl != NULL, "Mdl allocation failed");
72 ok(Irp->MdlAddress == Mdl, "Irp->MdlAddress should be 0x%X, but is 0x%X",
73 (UINT32)Mdl, (UINT32)Irp->MdlAddress);
74
75 IoFreeMdl(Mdl);
76
77 // TODO: Check a case when SecondaryBuffer == TRUE
78
79 IoFreeIrp(Irp);
80 ExFreePool(VirtualAddress);
81
82 FinishTest(KeyHandle, L"IoMdlTest");
83 }
84
85 VOID NtoskrnlIoIrpTest(HANDLE KeyHandle)
86 {
87 USHORT size;
88 IRP *iorp;
89
90 StartTest();
91
92 // 1st test
93 size = sizeof(IRP) + 5 * sizeof(IO_STACK_LOCATION);
94 iorp = ExAllocatePool(NonPagedPool, size);
95
96 if (NULL != iorp)
97 {
98 IoInitializeIrp(iorp, size, 5);
99
100 ok(6 == iorp->Type, "Irp type should be 6, but got %d\n", iorp->Type);
101 ok(iorp->Size == size, "Irp size should be %d, but got %d\n",
102 iorp->Size, size);
103 ok(5 == iorp->StackCount, "Irp StackCount should be 5, but got %d\n",
104 iorp->StackCount);
105 ok(6 == iorp->CurrentLocation, "Irp CurrentLocation should be 6, but got %d\n",
106 iorp->CurrentLocation);
107 ok(IsListEmpty(&iorp->ThreadListEntry), "IRP thread list is not empty\n");
108 ok ((PIO_STACK_LOCATION)(iorp + 1) + 5 ==
109 iorp->Tail.Overlay.CurrentStackLocation,
110 "CurrentStackLocation mismatch\n");
111
112 ExFreePool(iorp);
113 }
114
115 // 2nd test
116 size = sizeof(IRP) + 2 * sizeof(IO_STACK_LOCATION);
117 iorp = IoAllocateIrp(2, FALSE);
118
119 if (NULL != iorp)
120 {
121 ok(6 == iorp->Type, "Irp type should be 6, but got %d\n", iorp->Type);
122 ok(iorp->Size >= size,
123 "Irp size should be more or equal to %d, but got %d\n",
124 iorp->Size, size);
125 ok(2 == iorp->StackCount, "Irp StackCount should be 2, but got %d\n",
126 iorp->StackCount);
127 ok(3 == iorp->CurrentLocation, "Irp CurrentLocation should be 3, but got %d\n",
128 iorp->CurrentLocation);
129 ok(IsListEmpty(&iorp->ThreadListEntry), "IRP thread list is not empty\n");
130 ok ((PIO_STACK_LOCATION)(iorp + 1) + 2 ==
131 iorp->Tail.Overlay.CurrentStackLocation,
132 "CurrentStackLocation mismatch\n");
133 ok((IRP_ALLOCATED_FIXED_SIZE & iorp->AllocationFlags),
134 "IRP Allocation flags lack fixed size attribute\n");
135 ok(!(IRP_LOOKASIDE_ALLOCATION & iorp->AllocationFlags),
136 "IRP Allocation flags should not have lookaside allocation\n");
137
138 IoFreeIrp(iorp);
139 }
140
141 // 3rd test
142 size = sizeof(IRP) + 2 * sizeof(IO_STACK_LOCATION);
143 iorp = IoAllocateIrp(2, TRUE);
144
145 if (NULL != iorp)
146 {
147 ok(6 == iorp->Type, "Irp type should be 6, but got %d\n", iorp->Type);
148 ok(iorp->Size >= size,
149 "Irp size should be more or equal to %d, but got %d\n",
150 iorp->Size, size);
151 ok(2 == iorp->StackCount, "Irp StackCount should be 2, but got %d\n",
152 iorp->StackCount);
153 ok(3 == iorp->CurrentLocation, "Irp CurrentLocation should be 3, but got %d\n",
154 iorp->CurrentLocation);
155 ok(IsListEmpty(&iorp->ThreadListEntry), "IRP thread list is not empty\n");
156 ok ((PIO_STACK_LOCATION)(iorp + 1) + 2 ==
157 iorp->Tail.Overlay.CurrentStackLocation,
158 "CurrentStackLocation mismatch\n");
159 ok((IRP_ALLOCATED_FIXED_SIZE & iorp->AllocationFlags),
160 "IRP Allocation flags lack fixed size attribute\n");
161 ok((IRP_LOOKASIDE_ALLOCATION & iorp->AllocationFlags),
162 "IRP Allocation flags lack lookaside allocation\n");
163
164 IoFreeIrp(iorp);
165 }
166
167 FinishTest(KeyHandle, L"IoIrpTest");
168 }