change.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * File change notification tests
 *
 * Copyright 2006 Mike McCormack for CodeWeavers
 *
 * 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 23 24 25 26 27 28 29 30 31 32 33 34
 */

#include <ntstatus.h>
#define WIN32_NO_STATUS
#include <windows.h>
#include <winnt.h>
#include <winternl.h>
#include <winerror.h>
#include <stdio.h>
#include "wine/test.h"

typedef NTSTATUS (WINAPI *fnNtNotifyChangeDirectoryFile)(
                          HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,
                          PIO_STATUS_BLOCK,PVOID,ULONG,ULONG,BOOLEAN);
fnNtNotifyChangeDirectoryFile pNtNotifyChangeDirectoryFile;

35 36 37 38
typedef NTSTATUS (WINAPI *fnNtCancelIoFile)(HANDLE,PIO_STATUS_BLOCK);
fnNtCancelIoFile pNtCancelIoFile;


39 40 41 42 43 44 45 46 47 48
static void test_ntncdf(void)
{
    NTSTATUS r;
    HANDLE hdir, hEvent;
    char buffer[0x1000];
    DWORD fflags, filter = 0;
    IO_STATUS_BLOCK iosb;
    WCHAR path[MAX_PATH], subdir[MAX_PATH];
    static const WCHAR szBoo[] = { '\\','b','o','o',0 };
    static const WCHAR szHoo[] = { '\\','h','o','o',0 };
49
    PFILE_NOTIFY_INFORMATION pfni;
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

    r = GetTempPathW( MAX_PATH, path );
    ok( r != 0, "temp path failed\n");
    if (!r)
        return;

    lstrcatW( path, szBoo );
    lstrcpyW( subdir, path );
    lstrcatW( subdir, szHoo );

    RemoveDirectoryW( subdir );
    RemoveDirectoryW( path );
    
    r = CreateDirectoryW(path, NULL);
    ok( r == TRUE, "failed to create directory\n");

    r = pNtNotifyChangeDirectoryFile(NULL,NULL,NULL,NULL,NULL,NULL,0,0,0);
    ok(r==STATUS_ACCESS_VIOLATION, "should return access violation\n");

    fflags = FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED;
    hdir = CreateFileW(path, GENERIC_READ|SYNCHRONIZE, FILE_SHARE_READ, NULL, 
                        OPEN_EXISTING, fflags, NULL);
    ok( hdir != INVALID_HANDLE_VALUE, "failed to open directory\n");

    hEvent = CreateEvent( NULL, 0, 0, NULL );

    r = pNtNotifyChangeDirectoryFile(hdir,NULL,NULL,NULL,&iosb,NULL,0,0,0);
    ok(r==STATUS_INVALID_PARAMETER, "should return invalid parameter\n");

    r = pNtNotifyChangeDirectoryFile(hdir,hEvent,NULL,NULL,&iosb,NULL,0,0,0);
    ok(r==STATUS_INVALID_PARAMETER, "should return invalid parameter\n");

    filter = FILE_NOTIFY_CHANGE_FILE_NAME;
    filter |= FILE_NOTIFY_CHANGE_DIR_NAME;
    filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
    filter |= FILE_NOTIFY_CHANGE_SIZE;
    filter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
    filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
    filter |= FILE_NOTIFY_CHANGE_CREATION;
    filter |= FILE_NOTIFY_CHANGE_SECURITY;

91
    U(iosb).Status = 1;
92 93 94 95
    iosb.Information = 1;
    r = pNtNotifyChangeDirectoryFile(hdir,hEvent,NULL,NULL,&iosb,buffer,sizeof buffer,-1,0);
    ok(r==STATUS_INVALID_PARAMETER, "should return invalid parameter\n");

96
    ok( U(iosb).Status == 1, "information wrong\n");
97 98
    ok( iosb.Information == 1, "information wrong\n");

99
    U(iosb).Status = 1;
100
    iosb.Information = 0;
101
    r = pNtNotifyChangeDirectoryFile(hdir,hEvent,NULL,NULL,&iosb,buffer,sizeof buffer,filter,0);
102
    ok(r==STATUS_PENDING, "should return status pending\n");
103 104

    r = WaitForSingleObject( hEvent, 0 );
105 106 107 108
    ok( r == STATUS_TIMEOUT, "should timeout\n" );

    r = WaitForSingleObject( hdir, 0 );
    ok( r == STATUS_TIMEOUT, "should timeout\n" );
109 110 111 112

    r = CreateDirectoryW( subdir, NULL );
    ok( r == TRUE, "failed to create directory\n");

113 114 115
    r = WaitForSingleObject( hdir, 0 );
    ok( r == STATUS_TIMEOUT, "should timeout\n" );

116
    r = WaitForSingleObject( hEvent, 0 );
117 118
    ok( r == WAIT_OBJECT_0, "event should be ready\n" );

119
    ok( U(iosb).Status == STATUS_SUCCESS, "information wrong\n");
120
    ok( iosb.Information == 0x12, "information wrong\n");
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135
    pfni = (PFILE_NOTIFY_INFORMATION) buffer;
    ok( pfni->NextEntryOffset == 0, "offset wrong\n" );
    ok( pfni->Action == FILE_ACTION_ADDED, "action wrong\n" );
    ok( pfni->FileNameLength == 6, "len wrong\n" );
    ok( !memcmp(pfni->FileName,&szHoo[1],6), "name wrong\n" );

    r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb,buffer,sizeof buffer,0,0);
    ok(r==STATUS_INVALID_PARAMETER, "should return invalid parameter\n");

    r = pNtNotifyChangeDirectoryFile(hdir,hEvent,NULL,NULL,&iosb,buffer,sizeof buffer,0,0);
    ok(r==STATUS_INVALID_PARAMETER, "should return invalid parameter\n");

    filter = FILE_NOTIFY_CHANGE_SIZE;

136
    U(iosb).Status = 1;
137
    iosb.Information = 1;
138
    r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb,NULL,0,filter,0);
139 140
    ok(r==STATUS_PENDING, "should status pending\n");

141
    ok( U(iosb).Status == 1, "information wrong\n");
142
    ok( iosb.Information == 1, "information wrong\n");
143 144

    r = WaitForSingleObject( hdir, 0 );
145
    ok( r == STATUS_TIMEOUT, "should timeout\n" );
146 147 148 149

    r = RemoveDirectoryW( subdir );
    ok( r == TRUE, "failed to remove directory\n");

150 151 152 153 154 155
    r = WaitForSingleObject( hdir, 100 );
    ok( r == WAIT_OBJECT_0, "should be ready\n" );

    r = WaitForSingleObject( hdir, 100 );
    ok( r == WAIT_OBJECT_0, "should be ready\n" );

156
    ok( U(iosb).Status == STATUS_NOTIFY_ENUM_DIR, "information wrong\n");
157 158 159 160
    ok( iosb.Information == 0, "information wrong\n");

    CloseHandle(hdir);
    CloseHandle(hEvent);
161 162

    r = RemoveDirectoryW( path );
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
    ok( r == TRUE, "failed to remove directory\n");
}


static void test_ntncdf_async(void)
{
    NTSTATUS r;
    HANDLE hdir, hEvent;
    char buffer[0x1000];
    DWORD fflags, filter = 0;
    IO_STATUS_BLOCK iosb, iosb2;
    WCHAR path[MAX_PATH], subdir[MAX_PATH];
    static const WCHAR szBoo[] = { '\\','b','o','o',0 };
    static const WCHAR szHoo[] = { '\\','h','o','o',0 };
    PFILE_NOTIFY_INFORMATION pfni;

    r = GetTempPathW( MAX_PATH, path );
    ok( r != 0, "temp path failed\n");
    if (!r)
        return;

    lstrcatW( path, szBoo );
    lstrcpyW( subdir, path );
    lstrcatW( subdir, szHoo );

    RemoveDirectoryW( subdir );
    RemoveDirectoryW( path );
    
    r = CreateDirectoryW(path, NULL);
    ok( r == TRUE, "failed to create directory\n");

    r = pNtNotifyChangeDirectoryFile(NULL,NULL,NULL,NULL,NULL,NULL,0,0,0);
    ok(r==STATUS_ACCESS_VIOLATION, "should return access violation\n");

    fflags = FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED;
    hdir = CreateFileW(path, GENERIC_READ|SYNCHRONIZE, FILE_SHARE_READ, NULL, 
                        OPEN_EXISTING, fflags, NULL);
    ok( hdir != INVALID_HANDLE_VALUE, "failed to open directory\n");

    hEvent = CreateEvent( NULL, 0, 0, NULL );

    filter = FILE_NOTIFY_CHANGE_FILE_NAME;
    filter |= FILE_NOTIFY_CHANGE_DIR_NAME;
    filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
    filter |= FILE_NOTIFY_CHANGE_SIZE;
    filter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
    filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
    filter |= FILE_NOTIFY_CHANGE_CREATION;
    filter |= FILE_NOTIFY_CHANGE_SECURITY;


214
    U(iosb).Status   = 0x01234567;
215 216 217
    iosb.Information = 0x12345678;
    r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb,buffer,sizeof buffer,filter,0);
    ok(r==STATUS_PENDING, "should status pending\n");
218
    ok(U(iosb).Status == 0x01234567, "status set too soon\n");
219 220 221 222 223 224 225 226
    ok(iosb.Information == 0x12345678, "info set too soon\n");

    r = CreateDirectoryW( subdir, NULL );
    ok( r == TRUE, "failed to create directory\n");

    r = WaitForSingleObject( hdir, 100 );
    ok( r == WAIT_OBJECT_0, "should be ready\n" );

227
    ok(U(iosb).Status == STATUS_SUCCESS, "status not successful\n");
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    ok(iosb.Information == 0x12, "info not set\n");

    pfni = (PFILE_NOTIFY_INFORMATION) buffer;
    ok( pfni->NextEntryOffset == 0, "offset wrong\n" );
    ok( pfni->Action == FILE_ACTION_ADDED, "action wrong\n" );
    ok( pfni->FileNameLength == 6, "len wrong\n" );
    ok( !memcmp(pfni->FileName,&szHoo[1],6), "name wrong\n" );

    r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb,buffer,sizeof buffer,filter,0);
    ok(r==STATUS_PENDING, "should status pending\n");

    r = RemoveDirectoryW( subdir );
    ok( r == TRUE, "failed to remove directory\n");

    r = WaitForSingleObject( hdir, 0 );
    ok( r == WAIT_OBJECT_0, "should be ready\n" );

245
    ok(U(iosb).Status == STATUS_SUCCESS, "status not successful\n");
246 247 248 249 250 251 252 253
    ok(iosb.Information == 0x12, "info not set\n");

    ok( pfni->NextEntryOffset == 0, "offset wrong\n" );
    ok( pfni->Action == FILE_ACTION_REMOVED, "action wrong\n" );
    ok( pfni->FileNameLength == 6, "len wrong\n" );
    ok( !memcmp(pfni->FileName,&szHoo[1],6), "name wrong\n" );

    /* check APCs */
254
    U(iosb).Status = 0;
255 256 257 258 259 260 261 262 263 264 265
    iosb.Information = 0;

    r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb,NULL,0,filter,0);
    ok(r==STATUS_PENDING, "should status pending\n");

    r = CreateDirectoryW( subdir, NULL );
    ok( r == TRUE, "failed to create directory\n");

    r = WaitForSingleObject( hdir, 0 );
    ok( r == WAIT_OBJECT_0, "should be ready\n" );

266
    ok(U(iosb).Status == STATUS_NOTIFY_ENUM_DIR, "status not successful\n");
267 268
    ok(iosb.Information == 0, "info not set\n");

269
    U(iosb).Status = 0;
270 271 272 273 274 275 276 277 278 279 280
    iosb.Information = 0;

    r = pNtNotifyChangeDirectoryFile(hdir,hEvent,NULL,NULL,&iosb,buffer,sizeof buffer,filter,0);
    ok(r==STATUS_PENDING, "should status pending\n");

    r = RemoveDirectoryW( subdir );
    ok( r == TRUE, "failed to remove directory\n");

    r = WaitForSingleObject( hEvent, 0 );
    ok( r == WAIT_OBJECT_0, "should be ready\n" );

281
    ok(U(iosb).Status == STATUS_SUCCESS, "status not successful\n");
282 283 284
    ok(iosb.Information == 0x12, "info not set\n");


285
    U(iosb).Status   = 0x01234567;
286 287 288 289
    iosb.Information = 0x12345678;
    r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb,buffer,sizeof buffer,filter,0);
    ok(r==STATUS_PENDING, "should status pending\n");

290
    U(iosb2).Status   = 0x01234567;
291 292 293 294
    iosb2.Information = 0x12345678;
    r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb2,buffer,sizeof buffer,filter,0);
    ok(r==STATUS_PENDING, "should status pending\n");

295
    ok(U(iosb).Status == 0x01234567, "status set too soon\n");
296 297 298 299 300
    ok(iosb.Information == 0x12345678, "info set too soon\n");

    todo_wine {
    r = pNtCancelIoFile(hdir, &iosb);
    ok( r == STATUS_SUCCESS, "cancel failed\n");
301 302 303

    CloseHandle(hdir);

304 305
    ok(U(iosb).Status == STATUS_SUCCESS, "status wrong\n");
    ok(U(iosb2).Status == STATUS_CANCELLED, "status wrong\n");
306 307 308 309
    }
    ok(iosb.Information == 0, "info wrong\n");
    ok(iosb2.Information == 0, "info wrong\n");

310 311
    r = RemoveDirectoryW( path );
    ok( r == TRUE, "failed to remove directory\n");
312 313

    CloseHandle(hEvent);
314 315 316 317
}

START_TEST(change)
{
318
    HMODULE hntdll = GetModuleHandle("ntdll");
319 320 321 322 323
    if (!hntdll)
    {
        skip("not running on NT, skipping test\n");
        return;
    }
324 325 326 327 328 329

    pNtNotifyChangeDirectoryFile = (fnNtNotifyChangeDirectoryFile) 
        GetProcAddress(hntdll, "NtNotifyChangeDirectoryFile");
    pNtCancelIoFile = (fnNtCancelIoFile)
        GetProcAddress(hntdll, "NtCancelIoFile");

330 331 332
    if (!pNtNotifyChangeDirectoryFile || !pNtCancelIoFile)
    {
        skip("missing functions, skipping test\n");
333
        return;
334
    }
335

336
    test_ntncdf();
337
    test_ntncdf_async();
338
}