Added a bunch of test cases for atoms.
[wine] / dlls / kernel / tests / change.c
1 /*
2  * Tests for file change notification functions
3  *
4  * Copyright (c) 2004 Hans Leidekker
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 /* 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
26  */
27
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #include "wine/test.h"
32 #include <windef.h>
33 #include <winbase.h>
34
35 static DWORD CALLBACK NotificationThread(LPVOID arg)
36 {
37     HANDLE change = (HANDLE) arg;
38     BOOL ret = FALSE;
39     DWORD status;
40
41     status = WaitForSingleObject(change, 100);
42
43     if (status == WAIT_OBJECT_0 ) {
44         ret = FindNextChangeNotification(change);
45     }
46
47     ret = FindCloseChangeNotification(change);
48     ok( ret, "FindCloseChangeNotification error: %ld\n",
49        GetLastError());
50
51     ExitThread((DWORD)ret);
52 }
53
54 static HANDLE StartNotificationThread(LPCSTR path, BOOL subtree, DWORD flags)
55 {
56     HANDLE change, thread;
57     DWORD threadId;
58
59     change = FindFirstChangeNotificationA(path, subtree, flags);
60     ok(change != INVALID_HANDLE_VALUE, "FindFirstChangeNotification error: %ld\n", GetLastError());
61
62     thread = CreateThread(NULL, 0, NotificationThread, (LPVOID)change,
63                           0, &threadId);
64     ok(thread != INVALID_HANDLE_VALUE, "CreateThread error: %ld\n", GetLastError());
65
66     return thread;
67 }
68
69 static DWORD FinishNotificationThread(HANDLE thread)
70 {
71     DWORD status, exitcode;
72
73     status = WaitForSingleObject(thread, 5000);
74     ok(status == WAIT_OBJECT_0, "WaitForSingleObject status %ld error %ld\n", status, GetLastError());
75
76     ok(GetExitCodeThread(thread, &exitcode), "Could not retrieve thread exit code\n");
77
78     return exitcode;
79 }
80
81 static void test_FindFirstChangeNotification(void)
82 {
83     HANDLE change, file, thread;
84     DWORD attributes, count;
85     BOOL ret;
86
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";
90     char buffer[2048];
91
92     /* pathetic checks */
93
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());
97
98     if (0) /* This documents win2k behavior. It crashes on win98. */
99     { 
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());
103     }
104
105     ret = FindNextChangeNotification(NULL);
106     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE, "FindNextChangeNotification error: %ld\n",
107        GetLastError());
108
109     ret = FindCloseChangeNotification(NULL);
110     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE, "FindCloseChangeNotification error: %ld\n",
111        GetLastError());
112
113     ret = GetTempPathA(MAX_PATH, workdir);
114     ok(ret, "GetTempPathA error: %ld\n", GetLastError());
115
116     lstrcatA(workdir, "testFileChangeNotification");
117
118     ret = CreateDirectoryA(workdir, NULL);
119     ok(ret, "CreateDirectoryA error: %ld\n", GetLastError());
120
121     ret = GetTempFileNameA(workdir, prefix, 0, filename1);
122     ok(ret, "GetTempFileNameA error: %ld\n", GetLastError());
123
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());
129
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());
135
136     lstrcpyA(dirname1, filename1);
137     lstrcatA(dirname1, "dir");
138
139     ret = CreateDirectoryA(dirname1, NULL);
140     ok(ret, "CreateDirectoryA error: %ld\n", GetLastError());
141
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());
146
147     /* win98 and win2k behave differently here */
148     ret = FinishNotificationThread(thread);
149     ok(ret || !ret, "You'll never read this\n");
150
151     /* functional checks */
152
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");
158
159     lstrcpyA(dirname2, dirname1);
160     lstrcatA(dirname2, "new");
161
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");
167
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");
173
174     lstrcpyA(filename2, filename1);
175     lstrcatA(filename2, "new");
176
177     /* Rename a file */
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");
182
183     /* Delete a file */
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");
188
189     /* Create a file */
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");
197
198     attributes = GetFileAttributesA(filename2);
199     ok(attributes != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA error: %ld\n", GetLastError());
200     attributes &= FILE_ATTRIBUTE_READONLY;
201
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");
207
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");
218
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");
229
230     /* clean up */
231     
232     ret = DeleteFileA(filename2);
233     ok(ret, "DeleteFileA error: %ld\n", GetLastError());
234
235     ret = RemoveDirectoryA(workdir);
236     ok(ret, "RemoveDirectoryA error: %ld\n", GetLastError());
237 }
238
239 START_TEST(change)
240 {
241     test_FindFirstChangeNotification();
242 }