1 /*
2  * PROJECT:         ReactOS kernel-mode tests
3  * LICENSE:         LGPLv2+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Kernel-Mode Test Suite Io Regressions KM-Test (Mdl)
5  * PROGRAMMER:      Aleksey Bragin <aleksey@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 
10 #define NDEBUG
11 #include <debug.h>
12 
13 START_TEST(IoMdl)
14 {
15     PMDL Mdl;
16     PIRP Irp;
17     PVOID VirtualAddress;
18     ULONG MdlSize = 2*4096+184; // 2 pages and some random value
19     ULONG TooLargeMdlSize = (0x10000 - sizeof(MDL)) / sizeof(ULONG_PTR) * PAGE_SIZE;
20 
21     // Try to alloc 2Gb MDL
22     Mdl = IoAllocateMdl(NULL, 2048UL*0x100000, FALSE, FALSE, NULL);
23 
24     ok(Mdl == NULL,
25       "IoAllocateMdl should fail allocation of 2Gb or more, but got Mdl=0x%X\n",
26       (UINT_PTR)Mdl);
27 
28     if (Mdl)
29         IoFreeMdl(Mdl);
30 
31     // Now create a valid MDL
32     VirtualAddress = ExAllocatePool(NonPagedPool, MdlSize);
33     Mdl = IoAllocateMdl(VirtualAddress, MdlSize, FALSE, FALSE, NULL);
34     ok(Mdl != NULL, "Mdl allocation failed\n");
35     // Check fields of the allocated struct
36     ok(Mdl->Next == NULL, "Mdl->Next should be NULL, but is 0x%X\n",
37         (UINT_PTR)Mdl->Next);
38     ok(Mdl->ByteCount == MdlSize,
39         "Mdl->ByteCount should be equal to MdlSize, but is 0x%X\n",
40         (UINT_PTR)Mdl->ByteCount);
41     // TODO: Check other fields of MDL struct
42 
43     IoFreeMdl(Mdl);
44 
45     // Test maximum size for an MDL
46     Mdl = IoAllocateMdl(NULL, TooLargeMdlSize, FALSE, FALSE, NULL);
47     ok(Mdl == NULL, "Mdl allocation for %lu bytes succeeded\n", TooLargeMdlSize);
48     if (Mdl) IoFreeMdl(Mdl);
49 
50     Mdl = IoAllocateMdl(NULL, TooLargeMdlSize - PAGE_SIZE + 1, FALSE, FALSE, NULL);
51     ok(Mdl == NULL, "Mdl allocation for %lu bytes succeeded\n", TooLargeMdlSize - PAGE_SIZE + 1);
52     if (Mdl) IoFreeMdl(Mdl);
53 
54     Mdl = IoAllocateMdl(NULL, TooLargeMdlSize - PAGE_SIZE, FALSE, FALSE, NULL);
55     ok(Mdl != NULL, "Mdl allocation for %lu bytes failed\n", TooLargeMdlSize - PAGE_SIZE);
56     if (Mdl) IoFreeMdl(Mdl);
57 
58     Mdl = IoAllocateMdl(NULL, TooLargeMdlSize / 2, FALSE, FALSE, NULL);
59     ok(Mdl != NULL, "Mdl allocation for %lu bytes failed\n", TooLargeMdlSize / 2);
60     if (Mdl) IoFreeMdl(Mdl);
61 
62     // Allocate now with pointer to an Irp
63     Irp = IoAllocateIrp(1, FALSE);
64     ok(Irp != NULL, "IRP allocation failed\n");
65     Mdl = IoAllocateMdl(VirtualAddress, MdlSize, FALSE, FALSE, Irp);
66     ok(Mdl != NULL, "Mdl allocation failed\n");
67     ok(Irp->MdlAddress == Mdl, "Irp->MdlAddress should be 0x%X, but is 0x%X\n",
68         (UINT_PTR)Mdl, (UINT_PTR)Irp->MdlAddress);
69 
70     IoFreeMdl(Mdl);
71 
72     // TODO: Check a case when SecondaryBuffer == TRUE
73 
74     IoFreeIrp(Irp);
75     ExFreePool(VirtualAddress);
76 }
77