kernel: Add some more tests for FindFirstChangeNotification.
[wine] / dlls / ntdll / tests / change.c
1 /*
2  * File change notification tests
3  *
4  * Copyright 2006 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ntstatus.h>
22 #define WIN32_NO_STATUS
23 #include <windows.h>
24 #include <winnt.h>
25 #include <winternl.h>
26 #include <winerror.h>
27 #include <stdio.h>
28 #include "wine/test.h"
29
30 typedef NTSTATUS (WINAPI *fnNtNotifyChangeDirectoryFile)(
31                           HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,
32                           PIO_STATUS_BLOCK,PVOID,ULONG,ULONG,BOOLEAN);
33 fnNtNotifyChangeDirectoryFile pNtNotifyChangeDirectoryFile;
34
35 static void test_ntncdf(void)
36 {
37     HMODULE hntdll = GetModuleHandle("ntdll");
38     NTSTATUS r;
39     HANDLE hdir, hEvent;
40     char buffer[0x1000];
41     DWORD fflags, filter = 0;
42     IO_STATUS_BLOCK iosb;
43     WCHAR path[MAX_PATH], subdir[MAX_PATH];
44     static const WCHAR szBoo[] = { '\\','b','o','o',0 };
45     static const WCHAR szHoo[] = { '\\','h','o','o',0 };
46
47     pNtNotifyChangeDirectoryFile = (fnNtNotifyChangeDirectoryFile) 
48         GetProcAddress(hntdll, "NtNotifyChangeDirectoryFile");
49     if (!pNtNotifyChangeDirectoryFile)
50         return;
51
52     r = GetTempPathW( MAX_PATH, path );
53     ok( r != 0, "temp path failed\n");
54     if (!r)
55         return;
56
57     lstrcatW( path, szBoo );
58     lstrcpyW( subdir, path );
59     lstrcatW( subdir, szHoo );
60
61     RemoveDirectoryW( subdir );
62     RemoveDirectoryW( path );
63     
64     r = CreateDirectoryW(path, NULL);
65     ok( r == TRUE, "failed to create directory\n");
66
67     r = pNtNotifyChangeDirectoryFile(NULL,NULL,NULL,NULL,NULL,NULL,0,0,0);
68     ok(r==STATUS_ACCESS_VIOLATION, "should return access violation\n");
69
70     fflags = FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED;
71     hdir = CreateFileW(path, GENERIC_READ|SYNCHRONIZE, FILE_SHARE_READ, NULL, 
72                         OPEN_EXISTING, fflags, NULL);
73     ok( hdir != INVALID_HANDLE_VALUE, "failed to open directory\n");
74
75     hEvent = CreateEvent( NULL, 0, 0, NULL );
76
77     r = pNtNotifyChangeDirectoryFile(hdir,NULL,NULL,NULL,&iosb,NULL,0,0,0);
78     ok(r==STATUS_INVALID_PARAMETER, "should return invalid parameter\n");
79
80     r = pNtNotifyChangeDirectoryFile(hdir,hEvent,NULL,NULL,&iosb,NULL,0,0,0);
81     ok(r==STATUS_INVALID_PARAMETER, "should return invalid parameter\n");
82
83     filter = FILE_NOTIFY_CHANGE_FILE_NAME;
84     filter |= FILE_NOTIFY_CHANGE_DIR_NAME;
85     filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
86     filter |= FILE_NOTIFY_CHANGE_SIZE;
87     filter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
88     filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
89     filter |= FILE_NOTIFY_CHANGE_CREATION;
90     filter |= FILE_NOTIFY_CHANGE_SECURITY;
91
92     r = pNtNotifyChangeDirectoryFile(hdir,hEvent,NULL,NULL,&iosb,buffer,sizeof buffer,filter,0);
93     ok(r==STATUS_PENDING, "should return status pending\n");
94
95     r = WaitForSingleObject( hEvent, 0 );
96     ok( r == STATUS_TIMEOUT, "event shouldn't be ready\n" );
97
98     r = CreateDirectoryW( subdir, NULL );
99     ok( r == TRUE, "failed to create directory\n");
100
101     r = WaitForSingleObject( hEvent, 0 );
102     ok( r == WAIT_OBJECT_0, "event wasn't ready\n" );
103
104     r = pNtNotifyChangeDirectoryFile(hdir,0,NULL,NULL,&iosb,NULL,0,filter,0);
105     ok(r==STATUS_PENDING, "should return status pending\n");
106
107     r = WaitForSingleObject( hdir, 0 );
108     ok( r == STATUS_TIMEOUT, "event shouldn't be ready\n" );
109
110     r = RemoveDirectoryW( subdir );
111     ok( r == TRUE, "failed to remove directory\n");
112
113     r = WaitForSingleObject( hdir, 0 );
114     ok( r == WAIT_OBJECT_0, "event wasn't ready\n" );
115
116     r = RemoveDirectoryW( path );
117     ok( r == FALSE, "failed to remove directory\n");
118
119     CloseHandle(hdir);
120
121     r = RemoveDirectoryW( path );
122     ok( r == TRUE, "failed to remove directory\n");
123 }
124
125 START_TEST(change)
126 {
127     test_ntncdf();
128 }