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: DeleteFileA.c
8**
9** Purpose: Tests the PAL implementation of the DeleteFileA function.
10**
11**
12**===================================================================*/
13
14// delete an existing file
15// delete a non-existant file
16// delete an open file
17// delete files using wild cards
18// delete a hidden file
19// delete a file without proper permissions
20//
21
22#define PAL_STDCPP_COMPAT
23#include <palsuite.h>
24#undef PAL_STDCPP_COMPAT
25
26#include <unistd.h>
27#include <sys/stat.h>
28
29
30int __cdecl main(int argc, char *argv[])
31{
32 FILE *tempFile = NULL;
33 BOOL bRc = FALSE;
34
35
36 if (0 != PAL_Initialize(argc,argv))
37 {
38 return FAIL;
39 }
40
41 //
42 // create a test file
43 //
44 tempFile = fopen("testFile01.txt", "w");
45 if (tempFile == NULL)
46 {
47 Fail ("DeleteFileA: ERROR: Couldn't create \"DeleteFileA's"
48 " testFile01.txt\"\n");
49 }
50
51 fprintf(tempFile, "DeleteFileA test file.\n");
52 if (fclose(tempFile) != 0)
53 {
54 Fail ("DeleteFileA: ERROR: Couldn't close \"DeleteFileA's"
55 " testFile01.txt\"\n");
56 }
57
58 //
59 // delete a symlink to an existing file
60 //
61 if (symlink("testFile01.txt", "testFile01_symlink") != 0)
62 {
63 Fail("DeleteFileA: ERROR: Failed to create a symlink to testFile01.txt.\n");
64 }
65
66 bRc = DeleteFileA("testFile01_symlink");
67 if (bRc != TRUE)
68 {
69 Fail ("DeleteFileA: ERROR: Couldn't delete symlink!\n Error is %d\n", GetLastError());
70 }
71
72 struct stat statBuffer;
73 if (lstat("testFile01.txt", &statBuffer) != 0)
74 {
75 Fail("DeleteFileA: ERROR: Deleting a symlink deleted the file it was pointing to.\n");
76 }
77
78 if (lstat("testFile01_symlink", &statBuffer) == 0)
79 {
80 Fail("DeleteFileA: ERROR: Failed to delete a symlink.\n");
81 }
82
83 //
84 // deleting an existing file
85 //
86 bRc = DeleteFileA("testFile01.txt");
87 if (bRc != TRUE)
88 {
89 Fail ("DeleteFileA: ERROR: Couldn't delete DeleteFileA's"
90 " \"testFile01.txt\"\n"
91 " Error is %d\n", GetLastError());
92 }
93
94
95 //
96 // deleting a non-existant file : should fail
97 //
98
99 bRc = DeleteFileA("testFile02.txt");
100 if (bRc != FALSE)
101 {
102 Fail ("DeleteFileA: ERROR: Was able to delete the non-existant"
103 " file \"testFile02.txt\"\n");
104 }
105
106
107
108
109 //
110 // deleting an open file
111 //
112 tempFile = fopen("testFile03.txt", "w");
113 if (tempFile == NULL)
114 {
115 Fail("DeleteFileA: ERROR: Couldn't create \"DeleteFileA's"
116 " testFile03.txt\"\n");
117 }
118
119 fprintf(tempFile, "DeleteFileA test file.\n");
120 if (fclose(tempFile) != 0)
121 {
122 Fail ("DeleteFileA: ERROR: Couldn't close \"DeleteFileA's"
123 " testFile03.txt\"\n");
124 }
125
126 bRc = DeleteFileA("testFile03.txt");
127 if (bRc != TRUE)
128 {
129 Fail("DeleteFileA: ERROR: Couldn't delete DeleteFileA's"
130 " \"testFile03.txt\"\n"
131 " Error is %d\n", GetLastError());
132 }
133 bRc = DeleteFileA("testFile03.txt");
134
135
136
137
138 //
139 // delete using wild cards
140 //
141
142 // create the test file
143 tempFile = fopen("testFile04.txt", "w");
144 if (tempFile == NULL)
145 {
146 Fail("DeleteFileA: ERROR: Couldn't create DeleteFileA's"
147 " \"testFile04.txt\"\n");
148 }
149 fprintf(tempFile, "DeleteFileA test file.\n");
150 if (fclose(tempFile) != 0)
151 {
152 Fail ("DeleteFileA: ERROR: Couldn't close \"DeleteFileA's"
153 " testFile04.txt\"\n");
154 }
155
156 // delete using '?'
157 bRc = DeleteFileA("testFile0?.txt");
158 if (bRc == TRUE)
159 {
160 Fail("DeleteFileA: ERROR: Was able to delete using the"
161 " \'?\' wildcard\n");
162 }
163
164 // delete using '*'
165 bRc = DeleteFileA("testFile*.txt");
166 if (bRc == TRUE)
167 {
168 Fail("DeleteFileA: ERROR: Was able to delete using the"
169 " \'*\' wildcard\n");
170 }
171
172 bRc = DeleteFileA("testFile04.txt");
173 if (bRc != TRUE)
174 {
175 Fail ("DeleteFileA: ERROR: Couldn't delete DeleteFileA's"
176 " \"testFile04.txt\"\n"
177 " Error is %d\n", GetLastError());
178 }
179
180 PAL_Terminate();
181 return PASS;
182}
183