directory.c 20.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit test suite for directory functions.
 *
 * Copyright 2002 Dmitry Timoshkov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22
#include <stdarg.h>

23
#include "wine/test.h"
24
#include "windef.h"
25
#include "winbase.h"
26
#include "winerror.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "winternl.h"

static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);

static void init(void)
{
    HMODULE hntdll = GetModuleHandleA("ntdll.dll");
    pNtQueryObject = (void *)GetProcAddress(hntdll, "NtQueryObject");
}

#define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,__LINE__)
static void test_granted_access(HANDLE handle, ACCESS_MASK access, int line)
{
    OBJECT_BASIC_INFORMATION obj_info;
    NTSTATUS status;

    status = pNtQueryObject(handle, ObjectBasicInformation, &obj_info,
                            sizeof(obj_info), NULL);
    ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
    ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
        "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
}
49 50 51 52 53 54 55 56 57

/* If you change something in these tests, please do the same
 * for GetSystemDirectory tests.
 */
static void test_GetWindowsDirectoryA(void)
{
    UINT len, len_with_null;
    char buf[MAX_PATH];

58
    len_with_null = GetWindowsDirectoryA(NULL, 0);
59
    ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
60

61 62
    lstrcpyA(buf, "foo");
    len_with_null = GetWindowsDirectoryA(buf, 1);
63
    ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
64 65 66

    lstrcpyA(buf, "foo");
    len = GetWindowsDirectoryA(buf, len_with_null - 1);
67 68
    ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
    ok(len == len_with_null, "GetWindowsDirectoryW returned %d, expected %d\n",
69
       len, len_with_null);
70 71 72

    lstrcpyA(buf, "foo");
    len = GetWindowsDirectoryA(buf, len_with_null);
73 74 75
    ok(lstrcmpA(buf, "foo") != 0, "should touch the buffer\n");
    ok(len == strlen(buf), "returned length should be equal to the length of string\n");
    ok(len == len_with_null-1, "GetWindowsDirectoryA returned %d, expected %d\n",
76
       len, len_with_null-1);
77 78 79 80 81 82 83 84
}

static void test_GetWindowsDirectoryW(void)
{
    UINT len, len_with_null;
    WCHAR buf[MAX_PATH];
    static const WCHAR fooW[] = {'f','o','o',0};

85
    len_with_null = GetWindowsDirectoryW(NULL, 0);
86 87 88
    if (len_with_null == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip("GetWindowsDirectoryW is not implemented\n");
89
        return;
90
    }
91
    ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
92

93
    lstrcpyW(buf, fooW);
94
    len = GetWindowsDirectoryW(buf, 1);
95 96
    ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
    ok(len == len_with_null, "GetWindowsDirectoryW returned %d, expected %d\n",
97
       len, len_with_null);
98 99 100

    lstrcpyW(buf, fooW);
    len = GetWindowsDirectoryW(buf, len_with_null - 1);
101 102
    ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
    ok(len == len_with_null, "GetWindowsDirectoryW returned %d, expected %d\n",
103
       len, len_with_null);
104 105 106

    lstrcpyW(buf, fooW);
    len = GetWindowsDirectoryW(buf, len_with_null);
107 108 109
    ok(lstrcmpW(buf, fooW) != 0, "should touch the buffer\n");
    ok(len == lstrlenW(buf), "returned length should be equal to the length of string\n");
    ok(len == len_with_null-1, "GetWindowsDirectoryW returned %d, expected %d\n",
110
       len, len_with_null-1);
111 112 113 114 115 116 117 118 119 120 121
}


/* If you change something in these tests, please do the same
 * for GetWindowsDirectory tests.
 */
static void test_GetSystemDirectoryA(void)
{
    UINT len, len_with_null;
    char buf[MAX_PATH];

122
    len_with_null = GetSystemDirectoryA(NULL, 0);
123
    ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
124

125
    lstrcpyA(buf, "foo");
126
    len = GetSystemDirectoryA(buf, 1);
127 128
    ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
    ok(len == len_with_null, "GetSystemDirectoryA returned %d, expected %d\n",
129
       len, len_with_null);
130 131 132

    lstrcpyA(buf, "foo");
    len = GetSystemDirectoryA(buf, len_with_null - 1);
133 134
    ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
    ok(len == len_with_null, "GetSystemDirectoryA returned %d, expected %d\n",
135
       len, len_with_null);
136 137 138

    lstrcpyA(buf, "foo");
    len = GetSystemDirectoryA(buf, len_with_null);
139 140 141
    ok(lstrcmpA(buf, "foo") != 0, "should touch the buffer\n");
    ok(len == strlen(buf), "returned length should be equal to the length of string\n");
    ok(len == len_with_null-1, "GetSystemDirectoryW returned %d, expected %d\n",
142
       len, len_with_null-1);
143 144 145 146 147 148 149 150
}

static void test_GetSystemDirectoryW(void)
{
    UINT len, len_with_null;
    WCHAR buf[MAX_PATH];
    static const WCHAR fooW[] = {'f','o','o',0};

151
    len_with_null = GetSystemDirectoryW(NULL, 0);
152 153 154
    if (len_with_null == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip("GetSystemDirectoryW is not available\n");
155
        return;
156
    }
157
    ok(len_with_null <= MAX_PATH, "should fit into MAX_PATH\n");
158

159
    lstrcpyW(buf, fooW);
160
    len = GetSystemDirectoryW(buf, 1);
161 162
    ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
    ok(len == len_with_null, "GetSystemDirectoryW returned %d, expected %d\n",
163
       len, len_with_null);
164 165 166

    lstrcpyW(buf, fooW);
    len = GetSystemDirectoryW(buf, len_with_null - 1);
167 168
    ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
    ok(len == len_with_null, "GetSystemDirectoryW returned %d, expected %d\n",
169
       len, len_with_null);
170 171 172

    lstrcpyW(buf, fooW);
    len = GetSystemDirectoryW(buf, len_with_null);
173 174 175
    ok(lstrcmpW(buf, fooW) != 0, "should touch the buffer\n");
    ok(len == lstrlenW(buf), "returned length should be equal to the length of string\n");
    ok(len == len_with_null-1, "GetSystemDirectoryW returned %d, expected %d\n",
176
       len, len_with_null-1);
177 178
}

179 180 181
static void test_CreateDirectoryA(void)
{
    char tmpdir[MAX_PATH];
182
    WCHAR curdir[MAX_PATH];
183 184 185
    BOOL ret;

    ret = CreateDirectoryA(NULL, NULL);
186 187
    ok(ret == FALSE && (GetLastError() == ERROR_PATH_NOT_FOUND ||
                        GetLastError() == ERROR_INVALID_PARAMETER),
188
       "CreateDirectoryA(NULL): ret=%d err=%d\n", ret, GetLastError());
189 190

    ret = CreateDirectoryA("", NULL);
191 192
    ok(ret == FALSE && (GetLastError() == ERROR_BAD_PATHNAME ||
                        GetLastError() == ERROR_PATH_NOT_FOUND),
193
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
194 195

    ret = GetSystemDirectoryA(tmpdir, MAX_PATH);
196
    ok(ret < MAX_PATH, "System directory should fit into MAX_PATH\n");
197

198
    GetCurrentDirectoryW(MAX_PATH, curdir);
199
    ret = SetCurrentDirectoryA(tmpdir);
200
    ok(ret == TRUE, "could not chdir to the System directory\n");
201 202

    ret = CreateDirectoryA(".", NULL);
203
    ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
204
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
205

206 207

    ret = CreateDirectoryA("..", NULL);
208
    ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
209
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
210 211 212 213

    GetTempPathA(MAX_PATH, tmpdir);
    tmpdir[3] = 0; /* truncate the path */
    ret = CreateDirectoryA(tmpdir, NULL);
214 215
    ok(ret == FALSE && (GetLastError() == ERROR_ALREADY_EXISTS ||
                        GetLastError() == ERROR_ACCESS_DENIED),
216
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
217 218 219 220

    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me");
    ret = CreateDirectoryA(tmpdir, NULL);
221
    ok(ret == TRUE,       "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
222 223

    ret = CreateDirectoryA(tmpdir, NULL);
224
    ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
225
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
226 227

    ret = RemoveDirectoryA(tmpdir);
228
    ok(ret == TRUE,
229
       "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
230

231

232 233
    lstrcatA(tmpdir, "?");
    ret = CreateDirectoryA(tmpdir, NULL);
234 235
    ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
			GetLastError() == ERROR_PATH_NOT_FOUND),
236
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
237
    RemoveDirectoryA(tmpdir);
238 239 240

    tmpdir[lstrlenA(tmpdir) - 1] = '*';
    ret = CreateDirectoryA(tmpdir, NULL);
241 242
    ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
			GetLastError() == ERROR_PATH_NOT_FOUND),
243
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
244 245
    RemoveDirectoryA(tmpdir);

246 247 248 249
    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me/Please Remove Me");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND, 
250
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
251 252 253 254 255 256 257 258 259
    RemoveDirectoryA(tmpdir);

    /* Test behavior with a trailing dot.
     * The directory should be created without the dot.
     */
    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me.");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE,
260
       "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
261 262 263 264

    lstrcatA(tmpdir, "/Please Remove Me");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE,
265
       "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
266 267
    ret = RemoveDirectoryA(tmpdir);
    ok(ret == TRUE,
268
       "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
269 270 271 272 273

    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me");
    ret = RemoveDirectoryA(tmpdir);
    ok(ret == TRUE,
274
       "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
275 276 277 278 279 280 281 282

    /* Test behavior with two trailing dots.
     * The directory should be created without the trailing dots.
     */
    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me..");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE,
283
       "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
284 285 286 287 288

    lstrcatA(tmpdir, "/Please Remove Me");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE || /* On Win98 */
       (ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND), /* On NT! */
289
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
290 291 292 293
    if (ret == TRUE)
    {
        ret = RemoveDirectoryA(tmpdir);
        ok(ret == TRUE,
294
           "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
295 296 297 298 299 300
    }

    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me");
    ret = RemoveDirectoryA(tmpdir);
    ok(ret == TRUE,
301
       "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
302 303 304 305 306 307 308 309

    /* Test behavior with a trailing space.
     * The directory should be created without the trailing space.
     */
    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me ");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE,
310
       "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
311 312 313 314 315

    lstrcatA(tmpdir, "/Please Remove Me");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE || /* On Win98 */
       (ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND), /* On NT! */
316
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
317 318 319 320
    if (ret == TRUE)
    {
        ret = RemoveDirectoryA(tmpdir);
        ok(ret == TRUE,
321
           "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
322 323 324 325 326 327
    }

    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me");
    ret = RemoveDirectoryA(tmpdir);
    ok(ret == TRUE,
328
       "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
329 330 331 332 333 334 335 336

    /* Test behavior with a trailing space.
     * The directory should be created without the trailing spaces.
     */
    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me  ");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE,
337
       "CreateDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
338 339 340 341 342

    lstrcatA(tmpdir, "/Please Remove Me");
    ret = CreateDirectoryA(tmpdir, NULL);
    ok(ret == TRUE || /* On Win98 */
       (ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND), /* On NT! */
343
       "CreateDirectoryA(%s): ret=%d err=%d\n", tmpdir, ret, GetLastError());
344 345 346 347
    if (ret == TRUE)
    {
        ret = RemoveDirectoryA(tmpdir);
        ok(ret == TRUE,
348
           "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
349 350 351 352
    }

    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me");
353
    ret = RemoveDirectoryA(tmpdir);
354
    ok(ret == TRUE,
355
       "RemoveDirectoryA(%s) failed err=%d\n", tmpdir, GetLastError());
356
    SetCurrentDirectoryW(curdir);
357 358 359 360 361 362 363 364 365
}

static void test_CreateDirectoryW(void)
{
    WCHAR tmpdir[MAX_PATH];
    BOOL ret;
    static const WCHAR empty_strW[] = { 0 };
    static const WCHAR tmp_dir_name[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
    static const WCHAR dotW[] = {'.',0};
366
    static const WCHAR slashW[] = {'/',0};
367
    static const WCHAR dotdotW[] = {'.','.',0};
368
    static const WCHAR questionW[] = {'?',0};
369
    WCHAR curdir[MAX_PATH];
370 371

    ret = CreateDirectoryW(NULL, NULL);
372 373 374
    if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip("CreateDirectoryW is not available\n");
375
        return;
376
    }
377 378
    ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
       "should not create NULL path ret %u err %u\n", ret, GetLastError());
379 380

    ret = CreateDirectoryW(empty_strW, NULL);
381 382
    ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
       "should not create empty path ret %u err %u\n", ret, GetLastError());
383 384

    ret = GetSystemDirectoryW(tmpdir, MAX_PATH);
385
    ok(ret < MAX_PATH, "System directory should fit into MAX_PATH\n");
386

387
    GetCurrentDirectoryW(MAX_PATH, curdir);
388
    ret = SetCurrentDirectoryW(tmpdir);
389
    ok(ret == TRUE, "could not chdir to the System directory ret %u err %u\n", ret, GetLastError());
390 391

    ret = CreateDirectoryW(dotW, NULL);
392 393
    ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
       "should not create existing path ret %u err %u\n", ret, GetLastError());
394 395

    ret = CreateDirectoryW(dotdotW, NULL);
396 397
    ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
       "should not create existing path ret %u err %u\n", ret, GetLastError());
398 399 400 401

    GetTempPathW(MAX_PATH, tmpdir);
    tmpdir[3] = 0; /* truncate the path */
    ret = CreateDirectoryW(tmpdir, NULL);
402
    ok(ret == FALSE && (GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_ALREADY_EXISTS),
403
       "should deny access to the drive root ret %u err %u\n", ret, GetLastError());
404 405 406 407

    GetTempPathW(MAX_PATH, tmpdir);
    lstrcatW(tmpdir, tmp_dir_name);
    ret = CreateDirectoryW(tmpdir, NULL);
408
    ok(ret == TRUE, "CreateDirectoryW should always succeed\n");
409 410

    ret = CreateDirectoryW(tmpdir, NULL);
411 412
    ok(ret == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
       "should not create existing path ret %u err %u\n", ret, GetLastError());
413 414

    ret = RemoveDirectoryW(tmpdir);
415
    ok(ret == TRUE, "RemoveDirectoryW should always succeed\n");
416

417 418 419
    lstrcatW(tmpdir, questionW);
    ret = CreateDirectoryW(tmpdir, NULL);
    ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
420
       "CreateDirectoryW with ? wildcard name should fail with error 183, ret=%s error=%d\n",
421 422
       ret ? " True" : "False", GetLastError());
    ret = RemoveDirectoryW(tmpdir);
423
    ok(ret == FALSE, "RemoveDirectoryW should have failed\n");
424 425 426 427

    tmpdir[lstrlenW(tmpdir) - 1] = '*';
    ret = CreateDirectoryW(tmpdir, NULL);
    ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
428
       "CreateDirectoryW with * wildcard name should fail with error 183, ret=%s error=%d\n",
429 430
       ret ? " True" : "False", GetLastError());
    ret = RemoveDirectoryW(tmpdir);
431
    ok(ret == FALSE, "RemoveDirectoryW should have failed\n");
432 433 434 435 436 437
    
    GetTempPathW(MAX_PATH, tmpdir);
    lstrcatW(tmpdir, tmp_dir_name);
    lstrcatW(tmpdir, slashW);
    lstrcatW(tmpdir, tmp_dir_name);
    ret = CreateDirectoryW(tmpdir, NULL);
438 439 440
    ok(ret == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
      "CreateDirectoryW with multiple nonexistent directories in path should fail ret %u err %u\n",
       ret, GetLastError());
441
    ret = RemoveDirectoryW(tmpdir);
442
    ok(ret == FALSE, "RemoveDirectoryW should have failed\n");
443 444

    SetCurrentDirectoryW(curdir);
445 446 447 448
}

static void test_RemoveDirectoryA(void)
{
449
    char curdir[MAX_PATH];
450 451 452 453 454 455
    char tmpdir[MAX_PATH];
    BOOL ret;

    GetTempPathA(MAX_PATH, tmpdir);
    lstrcatA(tmpdir, "Please Remove Me");
    ret = CreateDirectoryA(tmpdir, NULL);
456
    ok(ret == TRUE, "CreateDirectoryA should always succeed\n");
457

458 459 460 461 462 463 464 465 466 467 468 469
    GetCurrentDirectoryA(MAX_PATH, curdir);
    ok(SetCurrentDirectoryA(tmpdir), "SetCurrentDirectoryA failed\n");

    SetLastError(0xdeadbeef);
    ok(!RemoveDirectoryA(tmpdir), "RemoveDirectoryA succeeded\n");
    ok(GetLastError() == ERROR_SHARING_VIOLATION,
       "Expected ERROR_SHARING_VIOLATION, got %u\n", GetLastError());

    TEST_GRANTED_ACCESS(NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.Handle,
                        FILE_TRAVERSE | SYNCHRONIZE);

    SetCurrentDirectoryA(curdir);
470
    ret = RemoveDirectoryA(tmpdir);
471
    ok(ret == TRUE, "RemoveDirectoryA should always succeed\n");
472

473 474
    lstrcatA(tmpdir, "?");
    ret = RemoveDirectoryA(tmpdir);
475 476
    ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
			GetLastError() == ERROR_PATH_NOT_FOUND),
477
       "RemoveDirectoryA with ? wildcard name should fail, ret=%s error=%d\n",
478 479 480 481
       ret ? " True" : "False", GetLastError());

    tmpdir[lstrlenA(tmpdir) - 1] = '*';
    ret = RemoveDirectoryA(tmpdir);
482 483
    ok(ret == FALSE && (GetLastError() == ERROR_INVALID_NAME ||
			GetLastError() == ERROR_PATH_NOT_FOUND),
484
       "RemoveDirectoryA with * wildcard name should fail, ret=%s error=%d\n",
485
       ret ? " True" : "False", GetLastError());
486 487 488 489
}

static void test_RemoveDirectoryW(void)
{
490
    WCHAR curdir[MAX_PATH];
491 492 493 494 495 496 497 498
    WCHAR tmpdir[MAX_PATH];
    BOOL ret;
    static const WCHAR tmp_dir_name[] = {'P','l','e','a','s','e',' ','R','e','m','o','v','e',' ','M','e',0};
    static const WCHAR questionW[] = {'?',0};

    GetTempPathW(MAX_PATH, tmpdir);
    lstrcatW(tmpdir, tmp_dir_name);
    ret = CreateDirectoryW(tmpdir, NULL);
499 500 501 502 503
    if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip("CreateDirectoryW is not available\n");
        return;
    }
504

505
    ok(ret == TRUE, "CreateDirectoryW should always succeed\n");
506

507 508 509 510 511 512 513 514 515 516 517 518
    GetCurrentDirectoryW(MAX_PATH, curdir);
    ok(SetCurrentDirectoryW(tmpdir), "SetCurrentDirectoryW failed\n");

    SetLastError(0xdeadbeef);
    ok(!RemoveDirectoryW(tmpdir), "RemoveDirectoryW succeeded\n");
    ok(GetLastError() == ERROR_SHARING_VIOLATION,
       "Expected ERROR_SHARING_VIOLATION, got %u\n", GetLastError());

    TEST_GRANTED_ACCESS(NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.Handle,
                        FILE_TRAVERSE | SYNCHRONIZE);

    SetCurrentDirectoryW(curdir);
519
    ret = RemoveDirectoryW(tmpdir);
520
    ok(ret == TRUE, "RemoveDirectoryW should always succeed\n");
521

522 523 524
    lstrcatW(tmpdir, questionW);
    ret = RemoveDirectoryW(tmpdir);
    ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
525
       "RemoveDirectoryW with wildcard should fail with error 183, ret=%s error=%d\n",
526
       ret ? " True" : "False", GetLastError());
527

528 529 530
    tmpdir[lstrlenW(tmpdir) - 1] = '*';
    ret = RemoveDirectoryW(tmpdir);
    ok(ret == FALSE && GetLastError() == ERROR_INVALID_NAME,
531
       "RemoveDirectoryW with * wildcard name should fail with error 183, ret=%s error=%d\n",
532
       ret ? " True" : "False", GetLastError());
533 534
}

535 536 537 538
static void test_SetCurrentDirectoryA(void)
{
    SetLastError(0);
    ok( !SetCurrentDirectoryA( "\\some_dummy_dir" ), "SetCurrentDirectoryA succeeded\n" );
539
    ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %d\n", GetLastError() );
540
    ok( !SetCurrentDirectoryA( "\\some_dummy\\subdir" ), "SetCurrentDirectoryA succeeded\n" );
541
    ok( GetLastError() == ERROR_PATH_NOT_FOUND, "wrong error %d\n", GetLastError() );
542 543
}

544 545
START_TEST(directory)
{
546 547
    init();

548 549
    test_GetWindowsDirectoryA();
    test_GetWindowsDirectoryW();
550

551 552
    test_GetSystemDirectoryA();
    test_GetSystemDirectoryW();
553 554 555

    test_CreateDirectoryA();
    test_CreateDirectoryW();
556 557 558

    test_RemoveDirectoryA();
    test_RemoveDirectoryW();
559 560

    test_SetCurrentDirectoryA();
561
}