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.
5 /*=====================================================================
7 ** Source: ReadFile.c (test 2)
9 ** Purpose: Tests the PAL implementation of the ReadFile function.
10 ** Creates a test file and performs an array of read tests.
12 ** Assumes successful:
19 **===================================================================*/
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";
30 //Previously number of tests was 6, now 4 refer VSW 312690
33 const int PAGESIZE = 4096;
37 BOOL validateResults(const char* szString, // string read
38 DWORD dwByteCount, // amount requested
39 DWORD dwBytesRead) // amount read
41 // were the correct number of bytes read?
42 if (dwBytesRead > dwByteCount)
44 Trace("bytes read > bytes asked for\n");
47 if (dwBytesRead != strlen(szString))
49 Trace("bytes read != length of read string\n");
57 if (memcmp(szString, szStringTest, dwBytesRead) != 0)
59 Trace("read = %s string = %s", szString, szStringTest);
66 BOOL readTest(DWORD dwByteCount, char cResult)
73 hFile = CreateFile(szReadableFile,
78 FILE_ATTRIBUTE_NORMAL,
80 if(hFile == INVALID_HANDLE_VALUE)
82 Trace("ReadFile: ERROR -> Unable to open file \"%s\".\n",
87 memset(readBuffer, 0, PAGESIZE);
89 bRc = ReadFile(hFile, readBuffer, dwByteCount, &dwBytesRead, NULL);
93 // if it failed, was it supposed to fail?
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());
107 // if it passed, was it supposed to pass?
110 Trace("cresult = 0\n");
115 return (validateResults(readBuffer, dwByteCount, dwBytesRead));
123 int __cdecl main(int argc, char *argv[])
126 const int BUFFER_SIZE = 2 * PAGESIZE;
128 DWORD dwByteCount[] = { 0,
130 strlen(szStringTest),
132 // Commented out two negative test cases : Refer VSW 312690
138 char szResults[] = "1111"; // Was "111100": Refer VSW 312690
141 DWORD dwBytesWritten = 0;
143 if (0 != PAL_Initialize(argc,argv))
148 /* aloocate read-write memery for readBuffer */
149 if (!(readBuffer = (char*) VirtualAlloc(NULL, BUFFER_SIZE, MEM_COMMIT, PAGE_READWRITE)))
151 Fail("VirtualAlloc failed: GetLastError returns %d\n", GetLastError());
155 /* write protect the second page of readBuffer */
156 if (!VirtualProtect(&readBuffer[PAGESIZE], PAGESIZE, PAGE_NOACCESS, &oldProt))
158 Fail("VirtualProtect failed: GetLastError returns %d\n", GetLastError());
162 // create the test file
163 hFile = CreateFile(szReadableFile,
168 FILE_ATTRIBUTE_NORMAL,
171 if(hFile == INVALID_HANDLE_VALUE)
173 Fail("ReadFile: ERROR -> Unable to create file \"%s\" (%d).\n",
174 szReadableFile, GetLastError());
177 bRc = WriteFile(hFile, szStringTest, strlen(szStringTest), &dwBytesWritten, NULL);
181 for (i = 0; i< NOOFTESTS; i++)
183 bRc = readTest(dwByteCount[i], szResults[i]);
186 Fail("ReadFile: ERROR -> Failed on test[%d]\n", i);
190 VirtualFree(readBuffer, BUFFER_SIZE, MEM_RELEASE);