2 * Tests for file change notification functions
4 * Copyright (c) 2004 Hans Leidekker
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.
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.
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
21 /* TODO: - security attribute changes
22 * - compound filter and multiple notifications
23 * - subtree notifications
24 * - non-documented flags FILE_NOTIFY_CHANGE_LAST_ACCESS and
25 * FILE_NOTIFY_CHANGE_CREATION
31 #include "wine/test.h"
35 static DWORD CALLBACK NotificationThread(LPVOID arg)
37 HANDLE change = (HANDLE) arg;
41 status = WaitForSingleObject(change, 100);
43 if (status == WAIT_OBJECT_0 ) {
44 ret = FindNextChangeNotification(change);
47 ret = FindCloseChangeNotification(change);
48 ok( ret, "FindCloseChangeNotification error: %ld\n",
51 ExitThread((DWORD)ret);
54 static HANDLE StartNotificationThread(LPCSTR path, BOOL subtree, DWORD flags)
56 HANDLE change, thread;
59 change = FindFirstChangeNotificationA(path, subtree, flags);
60 ok(change != INVALID_HANDLE_VALUE, "FindFirstChangeNotification error: %ld\n", GetLastError());
62 thread = CreateThread(NULL, 0, NotificationThread, (LPVOID)change,
64 ok(thread != INVALID_HANDLE_VALUE, "CreateThread error: %ld\n", GetLastError());
69 static DWORD FinishNotificationThread(HANDLE thread)
71 DWORD status, exitcode;
73 status = WaitForSingleObject(thread, 5000);
74 ok(status == WAIT_OBJECT_0, "WaitForSingleObject status %ld error %ld\n", status, GetLastError());
76 ok(GetExitCodeThread(thread, &exitcode), "Could not retrieve thread exit code\n");
81 static void test_FindFirstChangeNotification(void)
83 HANDLE change, file, thread;
84 DWORD attributes, count;
87 char workdir[MAX_PATH], dirname1[MAX_PATH], dirname2[MAX_PATH];
88 char filename1[MAX_PATH], filename2[MAX_PATH];
89 static const char prefix[] = "FCN";
94 change = FindFirstChangeNotificationA("not-a-file", FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
95 ok(change == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
96 "FindFirstChangeNotification error: %ld\n", GetLastError());
98 if (0) /* This documents win2k behavior. It crashes on win98. */
100 change = FindFirstChangeNotificationA(NULL, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
101 ok(change == NULL && GetLastError() == ERROR_PATH_NOT_FOUND,
102 "FindFirstChangeNotification error: %ld\n", GetLastError());
105 ret = FindNextChangeNotification(NULL);
106 ok(!ret && GetLastError() == ERROR_INVALID_HANDLE, "FindNextChangeNotification error: %ld\n",
109 ret = FindCloseChangeNotification(NULL);
110 ok(!ret && GetLastError() == ERROR_INVALID_HANDLE, "FindCloseChangeNotification error: %ld\n",
113 ret = GetTempPathA(MAX_PATH, workdir);
114 ok(ret, "GetTempPathA error: %ld\n", GetLastError());
116 lstrcatA(workdir, "testFileChangeNotification");
118 ret = CreateDirectoryA(workdir, NULL);
119 ok(ret, "CreateDirectoryA error: %ld\n", GetLastError());
121 ret = GetTempFileNameA(workdir, prefix, 0, filename1);
122 ok(ret, "GetTempFileNameA error: %ld\n", GetLastError());
124 file = CreateFileA(filename1, GENERIC_WRITE|GENERIC_READ, 0, NULL, CREATE_ALWAYS,
125 FILE_ATTRIBUTE_NORMAL, 0);
126 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error: %ld\n", GetLastError());
127 ret = CloseHandle(file);
128 ok( ret, "CloseHandle error: %ld\n", GetLastError());
130 /* Try to register notification for a file. win98 and win2k behave differently here */
131 change = FindFirstChangeNotificationA(filename1, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
132 ok(change == INVALID_HANDLE_VALUE && (GetLastError() == ERROR_DIRECTORY ||
133 GetLastError() == ERROR_FILE_NOT_FOUND),
134 "FindFirstChangeNotification error: %ld\n", GetLastError());
136 lstrcpyA(dirname1, filename1);
137 lstrcatA(dirname1, "dir");
139 ret = CreateDirectoryA(dirname1, NULL);
140 ok(ret, "CreateDirectoryA error: %ld\n", GetLastError());
142 /* What if we remove the directory we registered notification for? */
143 thread = StartNotificationThread(dirname1, FALSE, FILE_NOTIFY_CHANGE_DIR_NAME);
144 ret = RemoveDirectoryA(dirname1);
145 ok(ret, "RemoveDirectoryA error: %ld\n", GetLastError());
147 /* win98 and win2k behave differently here */
148 ret = FinishNotificationThread(thread);
149 ok(ret || !ret, "You'll never read this\n");
151 /* functional checks */
153 /* Create a directory */
154 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_DIR_NAME);
155 ret = CreateDirectoryA(dirname1, NULL);
156 ok(ret, "CreateDirectoryA error: %ld\n", GetLastError());
157 ok(FinishNotificationThread(thread), "Missed notification\n");
159 lstrcpyA(dirname2, dirname1);
160 lstrcatA(dirname2, "new");
162 /* Rename a directory */
163 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_DIR_NAME);
164 ret = MoveFileA(dirname1, dirname2);
165 ok(ret, "MoveFileA error: %ld\n", GetLastError());
166 ok(FinishNotificationThread(thread), "Missed notification\n");
168 /* Delete a directory */
169 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_DIR_NAME);
170 ret = RemoveDirectoryA(dirname2);
171 ok(ret, "RemoveDirectoryA error: %ld\n", GetLastError());
172 ok(FinishNotificationThread(thread), "Missed notification\n");
174 lstrcpyA(filename2, filename1);
175 lstrcatA(filename2, "new");
178 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
179 ret = MoveFileA(filename1, filename2);
180 ok(ret, "MoveFileA error: %ld\n", GetLastError());
181 ok(FinishNotificationThread(thread), "Missed notification\n");
184 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
185 ret = DeleteFileA(filename2);
186 ok(ret, "DeleteFileA error: %ld\n", GetLastError());
187 ok(FinishNotificationThread(thread), "Missed notification\n");
190 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
191 file = CreateFileA(filename2, GENERIC_WRITE|GENERIC_READ, 0, NULL, CREATE_ALWAYS,
192 FILE_ATTRIBUTE_NORMAL, 0);
193 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error: %ld\n", GetLastError());
194 ret = CloseHandle(file);
195 ok( ret, "CloseHandle error: %ld\n", GetLastError());
196 ok(FinishNotificationThread(thread), "Missed notification\n");
198 attributes = GetFileAttributesA(filename2);
199 ok(attributes != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA error: %ld\n", GetLastError());
200 attributes &= FILE_ATTRIBUTE_READONLY;
202 /* Change file attributes */
203 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_ATTRIBUTES);
204 ret = SetFileAttributesA(filename2, attributes);
205 ok(ret, "SetFileAttributesA error: %ld\n", GetLastError());
206 ok(FinishNotificationThread(thread), "Missed notification\n");
208 /* Change last write time by writing to a file */
209 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE);
210 file = CreateFileA(filename2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
211 FILE_ATTRIBUTE_NORMAL, 0);
212 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error: %ld\n", GetLastError());
213 ret = WriteFile(file, buffer, sizeof(buffer), &count, NULL);
214 ok(ret && count == sizeof(buffer), "WriteFile error: %ld\n", GetLastError());
215 ret = CloseHandle(file);
216 ok( ret, "CloseHandle error: %ld\n", GetLastError());
217 ok(FinishNotificationThread(thread), "Missed notification\n");
219 /* Change file size by truncating a file */
220 thread = StartNotificationThread(workdir, FALSE, FILE_NOTIFY_CHANGE_SIZE);
221 file = CreateFileA(filename2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
222 FILE_ATTRIBUTE_NORMAL, 0);
223 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error: %ld\n", GetLastError());
224 ret = WriteFile(file, buffer, sizeof(buffer) / 2, &count, NULL);
225 ok(ret && count == sizeof(buffer) / 2, "WriteFileA error: %ld\n", GetLastError());
226 ret = CloseHandle(file);
227 ok( ret, "CloseHandle error: %ld\n", GetLastError());
228 ok(FinishNotificationThread(thread), "Missed notification\n");
232 ret = DeleteFileA(filename2);
233 ok(ret, "DeleteFileA error: %ld\n", GetLastError());
235 ret = RemoveDirectoryA(workdir);
236 ok(ret, "RemoveDirectoryA error: %ld\n", GetLastError());
241 test_FindFirstChangeNotification();