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: CreateFileA.c
8**
9** Purpose: Test the PAL implementation of the CreateFileA 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;
40 char lpFileName[20];
41 FILE *outFile = NULL;
42 char results[1024];
43 int i, j, k, l;
44 DWORD dwDesiredAccess[4] = {0, // 0
45 GENERIC_READ, // 1
46 GENERIC_WRITE, // 2
47 GENERIC_READ | GENERIC_WRITE}; // 3
48 DWORD dwShareMode[8] = {0, // 0
49 FILE_SHARE_READ, // 1
50 FILE_SHARE_WRITE, // 2
51 FILE_SHARE_DELETE, // 3
52 FILE_SHARE_READ | FILE_SHARE_WRITE, // 4
53 FILE_SHARE_READ | FILE_SHARE_DELETE, // 5
54 FILE_SHARE_WRITE | FILE_SHARE_DELETE, // 6
55 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE}; // 7
56 LPSECURITY_ATTRIBUTES lpAttr = NULL;
57 DWORD dwCreationDisp[4] = {CREATE_NEW, // 0
58 CREATE_ALWAYS, // 1
59 OPEN_EXISTING, // 2
60 OPEN_ALWAYS}; // 3
61 DWORD dwFlagsAttrib[5] = {FILE_ATTRIBUTE_NORMAL, // 0
62 FILE_FLAG_SEQUENTIAL_SCAN, // 1
63 FILE_FLAG_WRITE_THROUGH, // 2
64 FILE_FLAG_NO_BUFFERING, // 3
65 FILE_FLAG_RANDOM_ACCESS}; // 4
66 HANDLE hTemplate = NULL;
67
68
69 if (0 != PAL_Initialize(argc,argv))
70 {
71 return FAIL;
72 }
73
74 if (!Cleanup()) {
75 Trace("Pre-test Cleanup() failed. LastError=%d\n", GetLastError());
76 return FAIL;
77 }
78
79 /* open the file to read the expected results */
80 outFile = fopen("winoutput", "r");
81 memset (results, 0, 1024);
82
83 fgets(results, 1024, outFile);
84 nCounter = (int)strlen(results);
85 fclose(outFile);
86
87 nCounter = 0;
88
89 // desired access loop
90 for (i = 0; i < 4; i++)
91 {
92 // share mode loop
93 for (j = 0; j < 8; j++)
94 {
95 // security attributes loop
96 for (k = 0; k < 4; k++)
97 {
98 // creation disp loop
99 for (l = 0; l < 5; l++)
100 {
101 sprintf_s(lpFileName, _countof(lpFileName), "test%03d.txt", nCounter);
102 hFile = CreateFile(lpFileName,
103 dwDesiredAccess[i],
104 dwShareMode[j],
105 lpAttr,
106 dwCreationDisp[k],
107 dwFlagsAttrib[l],
108 hTemplate);
109 if (hFile == INVALID_HANDLE_VALUE)
110 {
111 if (results[nCounter] == '1')
112 {
113 Trace("CreateFile: ERROR: Failed when expected "
114 "to pass %s [%d][%d][%d][%d]\n",
115 lpFileName, i, j, k, l);
116 bSuccess = FALSE;
117 }
118 }
119 else
120 {
121 CloseHandle(hFile);
122 if (results[nCounter] == '0')
123 {
124 Trace("CreateFile: ERROR: Passed when expected "
125 "to fail %s [%d][%d][%d][%d]\n",
126 lpFileName, i, j, k, l);
127 bSuccess = FALSE;
128 }
129 }
130 nCounter ++;
131 }
132 }
133 }
134 }
135
136 if (!Cleanup())
137 {
138 Trace("Post-test Cleanup() failed. LastError=%d\n", GetLastError());
139 return FAIL;
140 }
141
142 int exitCode = bSuccess ? PASS : FAIL;
143 PAL_TerminateEx(exitCode);
144 return exitCode;
145}
146