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: CreateFileW.c
8**
9** Purpose: Test the PAL implementation of the CreateFileW function
10**
11**
12**===================================================================*/
13
14#include <palsuite.h>
15
16BOOL Cleanup(void)
17{
18 char FileName[20];
19 int i;
20 BOOL bRet = TRUE; // assume success
21
22 // loop through all accesses, modes, dispositions and flags
23 for (i=0; i<4*8*4*5; ++i) {
24 sprintf_s(FileName, _countof(FileName), "test%03d.txt", i);
25 if (DeleteFileA(FileName) == FALSE) {
26 if (GetLastError() != ERROR_FILE_NOT_FOUND) {
27 bRet = FALSE;
28 }
29 }
30 }
31 return bRet;
32}
33
34
35int __cdecl main(int argc, char *argv[])
36{
37 BOOL bSuccess = TRUE;
38 int nCounter = 0;
39 HANDLE hFile = NULL;
40 WCHAR *lpFileName = NULL;
41 char* pTemp = NULL;
42 char string[40];
43 FILE *outFile = NULL;
44 char results[1024];
45 int i, j, k, l;
46 DWORD dwDesiredAccess[4] = {0, // 0
47 GENERIC_READ, // 1
48 GENERIC_WRITE, // 2
49 GENERIC_READ | GENERIC_WRITE}; // 3
50 DWORD dwShareMode[8] = {0, // 0
51 FILE_SHARE_READ, // 1
52 FILE_SHARE_WRITE, // 2
53 FILE_SHARE_DELETE, // 3
54 FILE_SHARE_READ | FILE_SHARE_WRITE, // 4
55 FILE_SHARE_READ | FILE_SHARE_DELETE, // 5
56 FILE_SHARE_WRITE | FILE_SHARE_DELETE, // 6
57 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE}; // 7
58 LPSECURITY_ATTRIBUTES lpAttr = NULL;
59 DWORD dwCreationDisp[4] = {CREATE_NEW, // 0
60 CREATE_ALWAYS, // 1
61 OPEN_EXISTING, // 2
62 OPEN_ALWAYS}; // 3
63 DWORD dwFlagsAttrib[5] = {FILE_ATTRIBUTE_NORMAL, // 0
64 FILE_FLAG_SEQUENTIAL_SCAN, // 1
65 FILE_FLAG_WRITE_THROUGH, // 2
66 FILE_FLAG_NO_BUFFERING, // 3
67 FILE_FLAG_RANDOM_ACCESS}; // 4
68 HANDLE hTemplate = NULL;
69
70
71 if (0 != PAL_Initialize(argc,argv))
72 {
73 return FAIL;
74 }
75
76 if (!Cleanup()) {
77 Trace("Pre-test Cleanup() failed. LastError=%d\n", GetLastError());
78 return FAIL;
79 }
80
81 /* open the file to read the expected results */
82 outFile = fopen("winoutput", "r");
83 memset (results, 0, 1024);
84
85 fgets(results, 1024, outFile);
86 fclose(outFile);
87
88 nCounter = 0;
89
90 // desired access loop
91 for (i = 0; i < 4; i++)
92 {
93 // share mode loop
94 for (j = 0; j < 8; j++)
95 {
96 // security attributes loop
97 for (k = 0; k < 4; k++)
98 {
99 // creation disp loop
100 for (l = 0; l < 5; l++)
101 {
102 sprintf_s(string, _countof(string), "test%03d.txt", nCounter);
103 lpFileName = convert(string);
104 hFile = CreateFileW(lpFileName,
105 dwDesiredAccess[i],
106 dwShareMode[j],
107 lpAttr,
108 dwCreationDisp[k],
109 dwFlagsAttrib[l],
110 hTemplate);
111 free(lpFileName);
112 if (hFile == INVALID_HANDLE_VALUE)
113 {
114 if (results[nCounter] == '1')
115 {
116 pTemp = convertC(lpFileName);
117 Trace("CreateFile: ERROR: Failed when expected "
118 "to pass %s [%d][%d][%d][%d]\n",
119 pTemp, i, j, k, l);
120 free(pTemp);
121 bSuccess = FALSE;
122 }
123 }
124 else
125 {
126 CloseHandle(hFile);
127 if (results[nCounter] == '0')
128 {
129 pTemp = convertC(lpFileName);
130 Trace("CreateFile: ERROR: Passed when expected "
131 "to fail %s [%d][%d][%d][%d]\n",
132 pTemp, i, j, k, l);
133 free(pTemp);
134 bSuccess = FALSE;
135 }
136 }
137 nCounter ++;
138 }
139 }
140 }
141 }
142
143 if (!Cleanup())
144 {
145 Trace("Post-test Cleanup() failed. LastError=%d\n", GetLastError());
146 return FAIL;
147 }
148
149 int exitCode = bSuccess ? PASS : FAIL;
150 PAL_TerminateEx(exitCode);
151 return exitCode;
152}
153