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