1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*===================================================================== |
6 | ** |
7 | ** Source: ReadFile.c (test 2) |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the ReadFile function. |
10 | ** Creates a test file and performs an array of read tests. |
11 | ** |
12 | ** Assumes successful: |
13 | ** CreateFile |
14 | ** CloseHandle |
15 | ** WriteFile |
16 | ** GetLastError |
17 | ** |
18 | ** |
19 | **===================================================================*/ |
20 | |
21 | |
22 | #include <palsuite.h> |
23 | |
24 | |
25 | const char* szStringTest = "The quick fox jumped over the lazy dog's back.\0" ; |
26 | const char* szEmptyString = "" ; |
27 | const char* szReadableFile = "Readable.txt" ; |
28 | const char* szResultsFile = "Results.txt" ; |
29 | |
30 | //Previously number of tests was 6, now 4 refer VSW 312690 |
31 | #define NOOFTESTS 4 |
32 | |
33 | const int PAGESIZE = 4096; |
34 | |
35 | char *readBuffer; |
36 | |
37 | BOOL validateResults(const char* szString, // string read |
38 | DWORD dwByteCount, // amount requested |
39 | DWORD dwBytesRead) // amount read |
40 | { |
41 | // were the correct number of bytes read? |
42 | if (dwBytesRead > dwByteCount) |
43 | { |
44 | Trace("bytes read > bytes asked for\n" ); |
45 | return FALSE; |
46 | } |
47 | if (dwBytesRead != strlen(szString)) |
48 | { |
49 | Trace("bytes read != length of read string\n" ); |
50 | return FALSE; |
51 | } |
52 | |
53 | // |
54 | // compare results |
55 | // |
56 | |
57 | if (memcmp(szString, szStringTest, dwBytesRead) != 0) |
58 | { |
59 | Trace("read = %s string = %s" , szString, szStringTest); |
60 | return FALSE; |
61 | } |
62 | |
63 | return TRUE; |
64 | } |
65 | |
66 | BOOL readTest(DWORD dwByteCount, char cResult) |
67 | { |
68 | HANDLE hFile = NULL; |
69 | DWORD dwBytesRead; |
70 | BOOL bRc = FALSE; |
71 | |
72 | // open the test file |
73 | hFile = CreateFile(szReadableFile, |
74 | GENERIC_READ, |
75 | FILE_SHARE_READ, |
76 | NULL, |
77 | OPEN_EXISTING, |
78 | FILE_ATTRIBUTE_NORMAL, |
79 | NULL); |
80 | if(hFile == INVALID_HANDLE_VALUE) |
81 | { |
82 | Trace("ReadFile: ERROR -> Unable to open file \"%s\".\n" , |
83 | szReadableFile); |
84 | return FALSE; |
85 | } |
86 | |
87 | memset(readBuffer, 0, PAGESIZE); |
88 | |
89 | bRc = ReadFile(hFile, readBuffer, dwByteCount, &dwBytesRead, NULL); |
90 | |
91 | if (bRc == FALSE) |
92 | { |
93 | // if it failed, was it supposed to fail? |
94 | if (cResult == '1') |
95 | { |
96 | Trace("\nbRc = %d\n" , bRc); |
97 | Trace("readBuffer = [%s] dwByteCount = %d dwBytesRead = %d\n" , readBuffer, dwByteCount, dwBytesRead); |
98 | Trace("cresult = 1\n" ); |
99 | Trace("getlasterror = %d\n" , GetLastError()); |
100 | CloseHandle(hFile); |
101 | return FALSE; |
102 | } |
103 | } |
104 | else |
105 | { |
106 | CloseHandle(hFile); |
107 | // if it passed, was it supposed to pass? |
108 | if (cResult == '0') |
109 | { |
110 | Trace("cresult = 0\n" ); |
111 | return FALSE; |
112 | } |
113 | else |
114 | { |
115 | return (validateResults(readBuffer, dwByteCount, dwBytesRead)); |
116 | } |
117 | } |
118 | |
119 | CloseHandle(hFile); |
120 | return TRUE; |
121 | } |
122 | |
123 | int __cdecl main(int argc, char *argv[]) |
124 | { |
125 | HANDLE hFile = NULL; |
126 | const int BUFFER_SIZE = 2 * PAGESIZE; |
127 | |
128 | DWORD dwByteCount[] = { 0, |
129 | 10, |
130 | strlen(szStringTest), |
131 | PAGESIZE |
132 | // Commented out two negative test cases : Refer VSW 312690 |
133 | // 2 * PAGESIZE, |
134 | // -1 |
135 | }; |
136 | |
137 | DWORD oldProt; |
138 | char szResults[] = "1111" ; // Was "111100": Refer VSW 312690 |
139 | int i; |
140 | BOOL bRc = FALSE; |
141 | DWORD dwBytesWritten = 0; |
142 | |
143 | if (0 != PAL_Initialize(argc,argv)) |
144 | { |
145 | return FAIL; |
146 | } |
147 | |
148 | /* allocate read-write memery for readBuffer */ |
149 | if (!(readBuffer = (char*) VirtualAlloc(NULL, BUFFER_SIZE, MEM_COMMIT, PAGE_READWRITE))) |
150 | { |
151 | Fail("VirtualAlloc failed: GetLastError returns %d\n" , GetLastError()); |
152 | return FAIL; |
153 | } |
154 | |
155 | /* write protect the second page of readBuffer */ |
156 | if (!VirtualProtect(&readBuffer[PAGESIZE], PAGESIZE, PAGE_NOACCESS, &oldProt)) |
157 | { |
158 | Fail("VirtualProtect failed: GetLastError returns %d\n" , GetLastError()); |
159 | return FAIL; |
160 | } |
161 | |
162 | // create the test file |
163 | hFile = CreateFile(szReadableFile, |
164 | GENERIC_WRITE, |
165 | FILE_SHARE_WRITE, |
166 | NULL, |
167 | CREATE_ALWAYS, |
168 | FILE_ATTRIBUTE_NORMAL, |
169 | NULL); |
170 | |
171 | if(hFile == INVALID_HANDLE_VALUE) |
172 | { |
173 | Fail("ReadFile: ERROR -> Unable to create file \"%s\" (%d).\n" , |
174 | szReadableFile, GetLastError()); |
175 | } |
176 | |
177 | bRc = WriteFile(hFile, szStringTest, strlen(szStringTest), &dwBytesWritten, NULL); |
178 | CloseHandle(hFile); |
179 | |
180 | |
181 | for (i = 0; i< NOOFTESTS; i++) |
182 | { |
183 | bRc = readTest(dwByteCount[i], szResults[i]); |
184 | if (bRc != TRUE) |
185 | { |
186 | Fail("ReadFile: ERROR -> Failed on test[%d]\n" , i); |
187 | } |
188 | } |
189 | |
190 | VirtualFree(readBuffer, BUFFER_SIZE, MEM_RELEASE); |
191 | PAL_Terminate(); |
192 | return PASS; |
193 | } |
194 | |
195 | |