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 4)
8**
9** Purpose: Tests the PAL implementation of the ReadFile function.
10** Creates a file and writes a small string to it, attempt
11** to read many more characters that exist. The returned
12** number of chars should be the amount written originally
13** not the number requested.
14**
15**
16**===================================================================*/
17#include <palsuite.h>
18
19int __cdecl main(int argc, char *argv[])
20{
21 HANDLE hFile = NULL;
22 DWORD dwBytesWritten;
23 BOOL bRc = FALSE;
24 char szBuffer[256];
25 DWORD dwBytesRead = 0;
26 int szRequestSize = 256;
27 char testFile[] = "testfile.tmp";
28 char testString[] = "people stop and stare";
29 DWORD res = 0;
30
31 /* Initialize the PAL.
32 */
33 if (0 != PAL_Initialize(argc,argv))
34 {
35 return FAIL;
36 }
37
38 /* Initialize the buffer.
39 */
40 memset(szBuffer, 0, 256);
41
42 /* Create a file to test with.
43 */
44 hFile = CreateFile(testFile,
45 GENERIC_WRITE|GENERIC_READ,
46 FILE_SHARE_WRITE|FILE_SHARE_READ,
47 NULL,
48 CREATE_ALWAYS,
49 FILE_ATTRIBUTE_NORMAL,
50 NULL);
51
52 if(hFile == INVALID_HANDLE_VALUE)
53 {
54 Fail("ERROR:%u: Unable to create file \"%s\".\n",
55 GetLastError(),
56 testFile);
57 }
58
59 /* Write to the File handle.
60 */
61 bRc = WriteFile(hFile,
62 testString,
63 strlen(testString),
64 &dwBytesWritten,
65 NULL);
66
67 if (bRc == FALSE)
68 {
69 Trace("ERROR:%u: Unable to write to file handle "
70 "hFile=0x%lx\n",
71 GetLastError(),
72 hFile);
73 if (!CloseHandle(hFile))
74 {
75 Trace("ERROR:%u%: Unable to close handle 0x%lx.\n",
76 GetLastError(),
77 hFile);
78 }
79 Fail("");
80 }
81
82 /* Set the file pointer to beginning of file.
83 */
84 res = SetFilePointer(hFile, (LONG)NULL, NULL, FILE_BEGIN);
85
86 if( (res == INVALID_SET_FILE_POINTER) &&
87 (GetLastError() != NO_ERROR))
88 {
89 Trace("ERROR:%u: Unable to set file pointer to the beginning of file.",
90 GetLastError());
91
92 if (!CloseHandle(hFile))
93 {
94 Trace("ERROR:%u%: Unable to close handle 0x%lx.\n",
95 GetLastError(),
96 hFile);
97 }
98 Fail("");
99 }
100
101
102 /* Attempt to read 256 characters from a file
103 * that does not contain that many.
104 */
105 bRc = ReadFile(hFile,
106 szBuffer,
107 szRequestSize,
108 &dwBytesRead,
109 NULL);
110
111 if (bRc == FALSE)
112 {
113 Trace("ERROR:%u: Unable to read from file handle 0x%lx.\n",
114 GetLastError(),
115 hFile);
116 if (!CloseHandle(hFile))
117 {
118 Trace("ERROR:%u%: Unable to close handle 0x%lx.\n",
119 GetLastError(),
120 hFile);
121 }
122 Fail("");
123 }
124
125 /* Confirm the number of bytes read with that requested.
126 */
127 if (dwBytesRead != strlen(testString))
128 {
129 Trace("ERROR: The number of bytes read \"%d\" is not equal to the "
130 "number originally written \"%d\" to the file.\n",
131 dwBytesRead,
132 strlen(testString));
133 if (!CloseHandle(hFile))
134 {
135 Trace("ERROR:%u%: Unable to close handle 0x%lx.\n",
136 GetLastError(),
137 hFile);
138 }
139 Fail("");
140 }
141
142 /* Terminate the PAL.
143 */
144 PAL_Terminate();
145 return PASS;
146}
147
148