summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/RemoveDirectoryW/test1/RemoveDirectoryW.c
blob: ae1dd0fb977f9514583c4b35c7bfa49cc04ebc41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  RemoveDirectoryW.c
**
** Purpose: Tests the PAL implementation of the RemoveDirectoryW function.
**
**
**===================================================================*/


#include <palsuite.h>


int __cdecl main(int argc, char *argv[])
{
    BOOL bRc = FALSE;
    char szDirName[252];
    DWORD curDirLen;
    WCHAR *szwTemp = NULL;
    WCHAR *szwTemp2 = NULL;
    WCHAR szwCurrentDir[MAX_PATH];
    WCHAR szwSubDir[MAX_PATH];

    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }

    /*
     * remove a NULL directory 
     */
    bRc = RemoveDirectoryW(NULL);
    if (bRc != FALSE)
    {
        Fail("RemoveDirectoryW: Failed since it was able to remove a"
            " NULL directory name\n");
    }

    /* 
     * remove a directory that does not exist 
     */
    szwTemp = convert("test_directory");
    bRc = RemoveDirectoryW(szwTemp);
    if (bRc != FALSE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed since it was able to remove"
            " the non-existant directory \"test_directory\"\n");
    }

    /* 
     * remove a directory that exists 
     */
    bRc = CreateDirectoryW(szwTemp, NULL);
    if (bRc != TRUE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to create the directory "
            "\"test_directory\" when it exists already.\n");
    }
    bRc = RemoveDirectoryW(szwTemp);
    if (bRc == FALSE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to remove the directory "
            "\"test_directory\" (error code %d)\n",
            GetLastError());
    }
    /* Make sure the directory was removed */
    if( -1 != GetFileAttributesW(szwTemp) )
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Able to get the attributes of "
             "the removed directory\n");
    }
    free(szwTemp);

    /* 
     * remove long directory names (245 characters) 
     */
    curDirLen = GetCurrentDirectoryA(0, NULL) + 1;
    memset(szDirName, 0, 252);
    memset(szDirName, 'a', 245 - curDirLen);
    szwTemp = convert(szDirName);
    bRc = CreateDirectoryW(szwTemp, NULL);
    if (bRc == FALSE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to create a directory name "
            "245 chars long\n");
    }
    bRc = RemoveDirectoryW(szwTemp);
    if (bRc == FALSE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to remove a 245 char "
            "long directory\n");
    }

    /* Make sure the directory was removed */
    if( -1 != GetFileAttributesW(szwTemp) )
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Able to get the attributes of "
             "the removed directory\n");
    }
    free(szwTemp);

    /* 
     * directories with dots 
     */
    memset(szDirName, 0, 252);
    sprintf(szDirName, ".dotDirectory");
    szwTemp = convert(szDirName);
    bRc = CreateDirectoryW(szwTemp, NULL);
    if (bRc == FALSE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to create \"%s\"\n", szDirName);
    }
    bRc = RemoveDirectoryW(szwTemp);
    if (bRc == FALSE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to remove \"%s\"\n", szDirName);
    }

    /* Make sure the directory was removed */
    if( -1 != GetFileAttributesW(szwTemp) )
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Able to get the attributes of "
             "the removed directory\n");
    }
    free(szwTemp);

    /* 
     * Try calling RemoveDirectory with a file name
     */
    memset(szDirName, 0, 252);
    sprintf(szDirName, "removedirectoryw.c");
    szwTemp = convert(szDirName);

    bRc = RemoveDirectoryW(szwTemp);
    free(szwTemp);
    if (bRc != FALSE)
    {
        Fail("RemoveDirectoryW: should have failed when "
             "called with a valid file name" );
    }

    /* 
     * remove a non empty directory 
     *
     * To test that, we'll first create non_empty_dir, we'll
     * set the current dir to non_empty_dir in which we'll
     * create sub_dir. We'll go back to the root of non_empty_dir
     * and we'll try to delete it (it shouldn't work).
     * After that we'll cleanup sub_dir and non_empty_dir 
     */

    /* Get the current directory so it is easy to get back
       to it later */
    if( 0 == GetCurrentDirectoryW(MAX_PATH, szwCurrentDir) )
    {
        Fail("RemoveDirectoryW: Failed to get current directory "
            "with GetCurrentDirectoryW.\n");
    }

    /* Create non_empty_dir */
    szwTemp = convert("non_empty_dir");
    bRc = CreateDirectoryW(szwTemp, NULL);
    if (bRc != TRUE)
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to create the directory "
             "\"non_empty_dir\" when it exists already.\n");
    }

    if( 0 == SetCurrentDirectoryW(szwTemp) )
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to set current directory to "
            "\"non_empty_dir\" with SetCurrentDirectoryW.\n");
    }

    /* Get the directory full path so it is easy to get back
       to it later */
    if( 0 == GetCurrentDirectoryW(MAX_PATH, szwSubDir) )
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to get current directory "
            "with GetCurrentDirectoryW.\n");
    }

    /* Create sub_dir */
    szwTemp2 = convert("sub_dir");
    bRc = CreateDirectoryW(szwTemp2, NULL);
    if (bRc != TRUE)
    {
        free(szwTemp);
        free(szwTemp2);
        Fail("RemoveDirectoryW: Failed to create the directory "
            "\"sub_dir\" when it exists already.\n");
    }

    /* Set the current dir to the parent of non_empty_dir/sub_dir */
    if( 0 == SetCurrentDirectoryW(szwCurrentDir) )
    {
        free(szwTemp);
        free(szwTemp2);
        Fail("RemoveDirectoryW: Failed to set current directory to "
            "\"non_empty_dir\" with SetCurrentDirectoryW.\n");
    }

    /* Try to remove non_empty_dir (shouldn't work) */
    bRc = RemoveDirectoryW(szwTemp);
    if (bRc == TRUE)
    {
        free(szwTemp);
        free(szwTemp2);
        Fail("RemoveDirectoryW: shouldn't have been able to remove "
             "the non empty directory \"non_empty_dir\"\n");
    }

    /* Go back to non_empty_dir and remove sub_dir */
    if( 0 == SetCurrentDirectoryW(szwSubDir) )
    {
        free(szwTemp);
        free(szwTemp2);
        Fail("RemoveDirectoryW: Failed to set current directory to "
            "\"non_empty_dir\" with SetCurrentDirectoryW.\n");
    }

    bRc = RemoveDirectoryW(szwTemp2);
    if (bRc == FALSE)
    {
        free(szwTemp);
        free(szwTemp2);
        Fail("RemoveDirectoryW: unable to remove "
             "directory \"sub_dir\"(error code %d)\n",
             GetLastError());
    }
    /* Make sure the directory was removed */
    if( -1 != GetFileAttributesW(szwTemp2) )
    {
        Fail("RemoveDirectoryW: Able to get the attributes of "
             "the removed directory\n");
    }
    free(szwTemp2);

    /* Go back to parent of non_empty_dir and remove non_empty_dir */
    if( 0 == SetCurrentDirectoryW(szwCurrentDir) )
    {
        free(szwTemp);
        Fail("RemoveDirectoryW: Failed to set current directory to "
            "\"..\non_empty_dir\" with SetCurrentDirectoryW.\n");
    }
    bRc = RemoveDirectoryW(szwTemp);
    if (bRc == FALSE)
    {
        free(szwTemp);    
        Fail("RemoveDirectoryW: unable to remove "
             "the directory \"non_empty_dir\"(error code %d)\n",
             GetLastError());
    }
    /* Make sure the directory was removed */
    if( -1 != GetFileAttributesW(szwTemp) )
    {
        Fail("RemoveDirectoryW: Able to get the attributes of "
             "the removed directory\n");
    }
    free(szwTemp); 


    PAL_Terminate();  
    return PASS;
}