1 /*
2  * PROJECT:         ReactOS kernel-mode tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Kernel-Mode Test Suite CcCopyRead test user-mode part
5  * PROGRAMMER:      Pierre Schweitzer <pierre@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 
10 START_TEST(CcCopyRead)
11 {
12     HANDLE Handle;
13     NTSTATUS Status;
14     LARGE_INTEGER ByteOffset;
15     IO_STATUS_BLOCK IoStatusBlock;
16     OBJECT_ATTRIBUTES ObjectAttributes;
17     PVOID Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, 1024);
18     UNICODE_STRING BigAlignmentTest = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-CcCopyRead\\BigAlignmentTest");
19     UNICODE_STRING SmallAlignmentTest = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-CcCopyRead\\SmallAlignmentTest");
20     UNICODE_STRING ReallySmallAlignmentTest = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-CcCopyRead\\ReallySmallAlignmentTest");
21     UNICODE_STRING FileBig = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-CcCopyRead\\FileBig");
22     UNICODE_STRING BehaviourTestFile = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-CcCopyRead\\BehaviourTestFile");
23 
24     KmtLoadDriver(L"CcCopyRead", FALSE);
25     KmtOpenDriver();
26 
27     InitializeObjectAttributes(&ObjectAttributes, &SmallAlignmentTest, OBJ_CASE_INSENSITIVE, NULL, NULL);
28     Status = NtOpenFile(&Handle, FILE_ALL_ACCESS, &ObjectAttributes, &IoStatusBlock, 0, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
29     ok_eq_hex(Status, STATUS_SUCCESS);
30 
31     ByteOffset.QuadPart = 3;
32     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 3, &ByteOffset, NULL);
33     ok_eq_hex(Status, STATUS_SUCCESS);
34     ok_eq_hex(((USHORT *)Buffer)[0], 0xBABA);
35 
36     ByteOffset.QuadPart = 514;
37     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 514, &ByteOffset, NULL);
38     ok_eq_hex(Status, STATUS_SUCCESS);
39     ok_eq_hex(((USHORT *)Buffer)[242], 0xBABA);
40     ok_eq_hex(((USHORT *)Buffer)[243], 0xFFFF);
41 
42     ByteOffset.QuadPart = 1000;
43     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 2, &ByteOffset, NULL);
44     ok_eq_hex(Status, STATUS_SUCCESS);
45     ok_eq_hex(((USHORT *)Buffer)[0], 0xFFFF);
46     ok_eq_hex(((USHORT *)Buffer)[1], 0xBABA);
47 
48     NtClose(Handle);
49 
50     InitializeObjectAttributes(&ObjectAttributes, &BigAlignmentTest, OBJ_CASE_INSENSITIVE, NULL, NULL);
51     Status = NtOpenFile(&Handle, FILE_ALL_ACCESS, &ObjectAttributes, &IoStatusBlock, 0, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
52     ok_eq_hex(Status, STATUS_SUCCESS);
53 
54     ByteOffset.QuadPart = 3;
55     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 3, &ByteOffset, NULL);
56     ok_eq_hex(Status, STATUS_SUCCESS);
57     ok_eq_hex(((USHORT *)Buffer)[0], 0xBABA);
58 
59     ByteOffset.QuadPart = 514;
60     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 514, &ByteOffset, NULL);
61     ok_eq_hex(Status, STATUS_SUCCESS);
62     ok_eq_hex(((USHORT *)Buffer)[242], 0xBABA);
63     ok_eq_hex(((USHORT *)Buffer)[243], 0xFFFF);
64 
65     ByteOffset.QuadPart = 300000;
66     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 10, &ByteOffset, NULL);
67     ok_eq_hex(Status, STATUS_SUCCESS);
68     ok_eq_hex(((USHORT *)Buffer)[0], 0xBABA);
69 
70     ByteOffset.QuadPart = 999990;
71     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 10, &ByteOffset, NULL);
72     ok_eq_hex(Status, STATUS_SUCCESS);
73     ok_eq_hex(((USHORT *)Buffer)[0], 0xBABA);
74 
75     ByteOffset.QuadPart = 1000;
76     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 2, &ByteOffset, NULL);
77     ok_eq_hex(Status, STATUS_SUCCESS);
78     ok_eq_hex(((USHORT *)Buffer)[0], 0xFFFF);
79     ok_eq_hex(((USHORT *)Buffer)[1], 0xBABA);
80 
81     NtClose(Handle);
82 
83     InitializeObjectAttributes(&ObjectAttributes, &ReallySmallAlignmentTest, OBJ_CASE_INSENSITIVE, NULL, NULL);
84     Status = NtOpenFile(&Handle, FILE_ALL_ACCESS, &ObjectAttributes, &IoStatusBlock, 0, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
85     ok_eq_hex(Status, STATUS_SUCCESS);
86 
87     ByteOffset.QuadPart = 1;
88     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 61, &ByteOffset, NULL);
89     ok_eq_hex(Status, STATUS_SUCCESS);
90     ok_eq_hex(((USHORT *)Buffer)[0], 0xBABA);
91 
92     NtClose(Handle);
93 
94     InitializeObjectAttributes(&ObjectAttributes, &FileBig, OBJ_CASE_INSENSITIVE, NULL, NULL);
95     Status = NtOpenFile(&Handle, FILE_ALL_ACCESS, &ObjectAttributes, &IoStatusBlock, 0, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
96     ok_eq_hex(Status, STATUS_SUCCESS);
97 
98     ByteOffset.QuadPart = 0;
99     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 1024, &ByteOffset, NULL);
100     ok_eq_hex(Status, STATUS_SUCCESS);
101     ok_eq_hex(((USHORT *)Buffer)[0], 0xBABA);
102 
103     NtClose(Handle);
104 
105     InitializeObjectAttributes(&ObjectAttributes, &BehaviourTestFile, OBJ_CASE_INSENSITIVE, NULL, NULL);
106     Status = NtOpenFile(&Handle, FILE_ALL_ACCESS, &ObjectAttributes, &IoStatusBlock, 0, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
107     ok_eq_hex(Status, STATUS_SUCCESS);
108 
109     ByteOffset.QuadPart = 0;
110     Status = NtReadFile(Handle, NULL, NULL, NULL, &IoStatusBlock, Buffer, 1024, &ByteOffset, NULL);
111 
112     NtClose(Handle);
113 
114     RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
115     KmtCloseDriver();
116     KmtUnloadDriver();
117 }
118