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: WriteFile.c (test 4)
8**
9** Purpose: Tests the PAL implementation of the WriteFile function.
10** Performs multiple writes to a file at different locations
11** then verifies the results with GetFileSize.
12**
13** dependency:
14** CreateFile.
15** GetFileSize.
16** FlushFileBuffers
17** SetFilePointer.
18** CloseHandle.
19** DeleteFile.
20**
21**
22**
23**===================================================================*/
24
25
26#include <palsuite.h>
27
28BOOL CleanUp(HANDLE hFile, const char * fileName)
29{
30 BOOL bRc = TRUE;
31 if (CloseHandle(hFile) != TRUE)
32 {
33 bRc = FALSE;
34 Trace("WriteFile: ERROR -> Unable to close file \"%s\","
35 " error: %ld.\n", fileName, GetLastError());
36 }
37 if (!DeleteFileA(fileName))
38 {
39 bRc = FALSE;
40 Trace("WriteFile: ERROR -> Unable to delete file \"%s\","
41 " error: %ld.\n", fileName, GetLastError());
42 }
43 return bRc;
44}
45
46int __cdecl main(int argc, char *argv[])
47{
48 const char* szStringTest = "1234567890";
49 const char* szWritableFile = "writeable.txt";
50 HANDLE hFile = NULL;
51 DWORD dwBytesWritten;
52
53 if (0 != PAL_Initialize(argc,argv))
54 {
55 return FAIL;
56 }
57
58 /* create the test file */
59 hFile = CreateFile(szWritableFile,
60 GENERIC_WRITE,
61 FILE_SHARE_WRITE,
62 NULL,
63 CREATE_ALWAYS,
64 FILE_ATTRIBUTE_NORMAL,
65 NULL);
66
67 if(hFile == INVALID_HANDLE_VALUE)
68 {
69 Fail("WriteFile: ERROR -> Unable to create file \"%s\".\n",
70 szWritableFile);
71 }
72
73
74 /* test wtriting to the file */
75 if( WriteFile(hFile, /* HANDLE handle to file */
76 szStringTest, /* data buffer */
77 strlen(szStringTest), /* number of bytes to write */
78 &dwBytesWritten, /* number of bytes written */
79 NULL) /* overlapped buffer */
80 ==0)
81 {
82 Trace("WriteFile: ERROR -> Unable to write to file error: %ld \n",
83 GetLastError());
84 CleanUp(hFile,szWritableFile);
85 Fail("");
86 }
87
88 if(!FlushFileBuffers(hFile))
89 { Trace("WriteFile: ERROR -> Call to FlushFile Buffers failed "
90 "error %ld \n",GetLastError());
91 CleanUp(hFile,szWritableFile);
92 Fail("");
93 }
94
95 /* check the file size */
96 if(GetFileSize(hFile, NULL)!=strlen(szStringTest))
97 {
98 Trace("WriteFile: ERROR -> writing %u chars to empty file "
99 "caused its size to become %u\n",strlen(szStringTest),
100 GetFileSize(hFile, NULL));
101 CleanUp(hFile,szWritableFile);
102 Fail("");
103 }
104
105 /* test writing to the file at position 5. */
106 SetFilePointer(
107 hFile, /* handle to file */
108 0x5, /* bytes to move pointer */
109 NULL, /* bytes to move pointer */
110 FILE_BEGIN /* starting point */
111 );
112
113
114 if( WriteFile(hFile, /* HANDLE handle to file */
115 szStringTest, /* data buffer */
116 strlen(szStringTest), /* number of bytes to write */
117 &dwBytesWritten, /* number of bytes written */
118 NULL) /* overlapped buffer */
119 ==0)
120 {
121 Trace("WriteFile: ERROR -> Unable to write to file after "
122 " moiving the file poiner to 5 error: %ld \n",
123 GetLastError());
124 CleanUp(hFile,szWritableFile);
125 Fail("");
126 }
127
128
129 if(!FlushFileBuffers(hFile))
130 {
131 Trace("WriteFile: ERROR -> Call to FlushFile Buffers failed "
132 "error %ld \n",GetLastError());
133 CleanUp(hFile,szWritableFile);
134 Fail("");
135 }
136
137 /* Check the file size */
138 if(GetFileSize(hFile, NULL)!=(strlen(szStringTest)+5))
139 {
140 Trace("WriteFile: ERROR -> writing %u chars to the file after "
141 "sitting the file pointer to 5 resulted in wrong file size; "
142 "Expected %u resulted %u.",strlen(szStringTest),
143 (strlen(szStringTest)+5),GetFileSize(hFile, NULL));
144 CleanUp(hFile,szWritableFile);
145 Fail("");
146 }
147
148 if (!CleanUp(hFile,szWritableFile))
149 {
150 Fail("");
151 }
152
153 PAL_Terminate();
154 return PASS;
155}
156