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 2)
8**
9** Purpose: Tests the PAL implementation of the WriteFile function.
10** Creates a number of files and writes different amounts of
11** data and verifies the writes.
12**
13**
14**===================================================================*/
15
16
17#include <palsuite.h>
18
19
20char* writeBuffer;
21const char* szWritableFile = "Writeable.txt";
22const char* szResultsFile = "Results.txt";
23const int PAGESIZE = 4096;
24
25BOOL writeTest(DWORD dwByteCount, DWORD dwBytesWrittenResult, BOOL bResult)
26{
27 HANDLE hFile = NULL;
28 DWORD dwBytesWritten;
29 BOOL bRc = FALSE;
30
31 /* create the test file */
32 DeleteFile(szWritableFile);
33 hFile = CreateFile(szWritableFile, GENERIC_WRITE, FILE_SHARE_WRITE,
34 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
35
36 if(hFile == INVALID_HANDLE_VALUE)
37 {
38 Trace("WriteFile: ERROR -> Unable to create file \"%s\".\n",
39 szWritableFile);
40 return FALSE;
41 }
42
43 bRc = WriteFile(hFile, writeBuffer, dwByteCount, &dwBytesWritten, NULL);
44 CloseHandle(hFile);
45
46 if ((bRc != bResult) || (dwBytesWrittenResult != dwBytesWritten))
47 {
48 Trace("WriteFile returned BOOL:%d and dwWritten:%d what we do expect is"
49 " BOOL:%d and dwWritten:%d\n", bRc, dwBytesWritten, bResult,
50 dwBytesWrittenResult);
51 return FALSE;
52 }
53
54 return TRUE;
55}
56
57int __cdecl main(int argc, char *argv[])
58{
59 const char * testString = "The quick fox jumped over the lazy dog's back.";
60 const int testStringLen = strlen(testString);
61
62 DWORD dwByteCount[4] = {-1, 10, testStringLen, 0};
63 DWORD dwByteWritten[4] = {0, 10, testStringLen, 0};
64 BOOL bResults[] = {FALSE, TRUE, TRUE, TRUE};
65
66 const int BUFFER_SIZE = 2 * PAGESIZE;
67 int j;
68 BOOL bRc = FALSE;
69 DWORD oldProt;
70
71 if (0 != PAL_Initialize(argc,argv))
72 {
73 return FAIL;
74 }
75
76 /* allocate read-write memery for writeBuffer */
77 if (!(writeBuffer = (char*) VirtualAlloc(NULL, BUFFER_SIZE, MEM_COMMIT,
78 PAGE_READWRITE)))
79 {
80 Fail("VirtualAlloc failed: GetLastError returns %d\n", GetLastError());
81 return FAIL;
82 }
83
84 memset((void*) writeBuffer, '.', BUFFER_SIZE);
85 strcpy(writeBuffer, testString);
86
87 /* write protect the second page of writeBuffer */
88 if (!VirtualProtect(&writeBuffer[PAGESIZE], PAGESIZE, PAGE_NOACCESS, &oldProt))
89 {
90 Fail("VirtualProtect failed: GetLastError returns %d\n", GetLastError());
91 return FAIL;
92 }
93
94 for (j = 0; j< 4; j++)
95 {
96 bRc = writeTest(dwByteCount[j], dwByteWritten[j], bResults[j]);
97 if (bRc != TRUE)
98 {
99 Fail("WriteFile: ERROR -> Failed on test[%d]\n", j);
100 }
101 }
102
103 PAL_Terminate();
104 return PASS;
105}
106