6 #include <libgnomevfs/gnome-vfs.h>
9 check_dir_exists (gchar *file_uri)
11 GnomeVFSDirectoryHandle *handle;
12 GnomeVFSResult result;
14 result = gnome_vfs_directory_open (&handle,
16 GNOME_VFS_FILE_INFO_DEFAULT);
17 if (result == GNOME_VFS_OK) {
18 gnome_vfs_directory_close (handle);
26 check_dir_count (gchar *file_uri)
28 GnomeVFSResult result;
32 result = gnome_vfs_directory_list_load (&files,
34 GNOME_VFS_FILE_INFO_DEFAULT);
35 if (result != GNOME_VFS_OK)
38 retval = g_list_length (files);
40 gnome_vfs_file_info_list_free (files);
47 check_file_exists (gchar *file_uri)
49 GnomeVFSHandle *handle;
50 GnomeVFSResult result;
52 result = gnome_vfs_open (&handle,
55 if (result == GNOME_VFS_OK) {
56 gnome_vfs_close (handle);
63 check_file_content (gchar *file_uri, gchar *content)
65 GnomeVFSHandle *handle;
66 GnomeVFSResult result;
67 GnomeVFSFileSize readlen;
71 len = strlen (content);
73 result = gnome_vfs_open (&handle,
76 if (result != GNOME_VFS_OK)
80 result = gnome_vfs_read (handle,
84 if (result != GNOME_VFS_OK)
93 MIN (len - idx, readlen)) != 0)
97 gnome_vfs_close (handle);
101 gnome_vfs_close (handle);
105 #define TEST_FAILURE(errstr) \
107 g_print (" " errstr ": %s\n", \
108 gnome_vfs_result_to_string (result)); \
112 #define TEST_SUCCESS g_print (" DONE.\n")
114 #define TEST_RESULT(predicate, error) \
115 if (predicate) TEST_FAILURE (error); \
119 test_vfolder_ops (void)
121 GnomeVFSHandle *handle;
122 GnomeVFSDirectoryHandle *dhandle;
123 GnomeVFSResult result;
124 gchar *uri, *realuri, *content;
125 GnomeVFSFileSize writelen;
127 uri = "test-vfolder:///";
128 g_print ("Opening %s...\n", uri);
129 result = gnome_vfs_directory_open (&dhandle,
131 GNOME_VFS_FILE_INFO_DEFAULT);
132 TEST_RESULT (result != GNOME_VFS_OK, "ERROR OPENNING");
140 * Create a new directory and delete it
142 /* Simple directory create */
143 uri = "test-vfolder:///MyTestFolder1";
144 g_print ("Creating new directory...");
145 result = gnome_vfs_make_directory (uri, GNOME_VFS_PERM_USER_ALL);
146 TEST_RESULT (result != GNOME_VFS_OK || !check_dir_exists (uri),
147 "ERROR CREATING DIR");
149 /* Simple directory delete */
150 uri = "test-vfolder:///MyTestFolder1";
151 g_print ("Deleting new empty directory...");
152 result = gnome_vfs_remove_directory (uri);
153 TEST_RESULT (result != GNOME_VFS_OK || check_dir_exists (uri),
154 "ERROR DELETING DIR");
159 * Create a new directory, add a file to it, check file exists,
160 * check we can't delete a non-empty dir, delete file, then delete
163 /* Simple Create 2 */
164 uri = "test-vfolder:///MyTestFolder2/";
165 g_print ("Creating new directory...");
166 result = gnome_vfs_make_directory (uri, GNOME_VFS_PERM_USER_ALL);
167 TEST_RESULT (result != GNOME_VFS_OK || !check_dir_exists (uri),
168 "ERROR CREATING DIR");
170 /* Create Empty File */
171 uri = "test-vfolder:///MyTestFolder2/a_fake_file.desktop";
172 g_print ("Creating new file...");
173 result = gnome_vfs_create (&handle,
175 GNOME_VFS_OPEN_READ | GNOME_VFS_OPEN_WRITE,
177 GNOME_VFS_PERM_USER_ALL);
178 TEST_RESULT (result != GNOME_VFS_OK ||
179 !check_file_exists (uri) ||
180 !check_file_content (uri, ""),
181 "ERROR CREATING FILE");
183 /* Try removing dir (should fail) */
184 uri = "test-vfolder:///MyTestFolder2";
185 g_print ("Deleting new non-empty directory...");
186 result = gnome_vfs_remove_directory (uri);
187 TEST_RESULT (result != GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY ||
188 !check_dir_exists (uri),
189 "ERROR DELETING DIR");
192 uri = "test-vfolder:///MyTestFolder2/a_fake_file.desktop";
193 g_print ("Deleting new file...");
194 result = gnome_vfs_unlink (uri);
195 TEST_RESULT (result != GNOME_VFS_OK || check_file_exists (uri),
196 "ERROR DELETING FILE");
198 /* Try removing dir */
199 uri = "test-vfolder:///MyTestFolder2/";
200 g_print ("Deleting new empty directory...");
201 result = gnome_vfs_remove_directory (uri);
202 TEST_RESULT (result != GNOME_VFS_OK || check_dir_exists (uri),
203 "ERROR DELETING DIR");
208 * Check creating an existing directory fails. Only run during first
211 /* Try to create existing empty dir (should fail) */
212 uri = "test-vfolder:///EmptyFolder";
213 if (check_dir_exists (uri)) {
214 g_print ("Creating existing empty directory...");
215 result = gnome_vfs_make_directory (uri,
216 GNOME_VFS_PERM_USER_ALL);
217 TEST_RESULT (result != GNOME_VFS_ERROR_FILE_EXISTS,
218 "ABLE TO CREATE EXISTING DIR");
220 /* Try to delete existing empty dir */
221 g_print ("Deleting existing empty directory...");
222 result = gnome_vfs_remove_directory (uri);
223 TEST_RESULT (result != GNOME_VFS_OK || check_dir_exists (uri),
224 "ERROR DELETING DIR");
229 * Check we can't delete an existing hidden directory, check we can't
230 * add a file to an existing hidden directory, check we can create the
231 * existing hidden directory, create a file in now non-hidden dir,
232 * delete created file and check dir is still visible, delete dir.
234 /* First, see if its hidden */
235 uri = "test-vfolder:///EmptyHiddenFolder";
236 g_print ("Checking if hidden directory is visible ...");
237 TEST_RESULT (check_dir_exists (uri),
238 "ABLE TO SEE HIDDEN DIR");
240 /* Try to delete existing empty hidden dir (should fail) */
241 uri = "test-vfolder:///EmptyHiddenFolder";
242 g_print ("Deleting existing empty hidden directory...");
243 result = gnome_vfs_remove_directory (uri);
244 TEST_RESULT (result == GNOME_VFS_OK || check_dir_exists (uri),
245 "ABLE TO DELETE HIDDEN DIR");
247 /* Try to add file to existing empty hidden dir (should fail) */
248 uri = "test-vfolder:///EmptyHiddenFolder/a_fake_file.desktop";
249 g_print ("Creating file in existing empty hidden directory...");
250 result = gnome_vfs_create (&handle,
252 GNOME_VFS_OPEN_WRITE,
254 GNOME_VFS_PERM_USER_ALL);
255 TEST_RESULT (result == GNOME_VFS_OK || check_dir_exists (uri),
256 "ABLE TO CREATE FILE IN HIDDEN DIR");
258 /* Try to create existing empty hidden dir */
259 uri = "test-vfolder:///EmptyHiddenFolder";
260 g_print ("Deleting existing empty hidden directory...");
261 result = gnome_vfs_make_directory (uri, GNOME_VFS_PERM_USER_ALL);
262 TEST_RESULT (result != GNOME_VFS_OK || !check_dir_exists (uri),
263 "ERROR OVERRIDING HIDDEN DIR");
265 /* Try to add file to existing empty (now) non-hidden dir */
266 uri = "test-vfolder:///EmptyHiddenFolder/a_fake_file.desktop";
267 g_print ("Creating file in existing empty hidden directory...");
268 result = gnome_vfs_create (&handle,
270 GNOME_VFS_OPEN_WRITE,
272 GNOME_VFS_PERM_USER_ALL);
273 TEST_RESULT (result != GNOME_VFS_OK || !check_file_exists (uri),
274 "ERROR CREATING FILE IN DIR");
276 /* Try to delete existing empty hidden dir */
277 uri = "test-vfolder:///EmptyHiddenFolder";
278 g_print ("Deleting existing empty hidden directory...");
279 result = gnome_vfs_remove_directory (uri);
280 TEST_RESULT (result == GNOME_VFS_OK || !check_dir_exists (uri),
281 "ABLE TO DELETE NON-EMPTY FOLDER");
283 /* Try to delete the file we created */
284 uri = "test-vfolder:///EmptyHiddenFolder/a_fake_file.desktop";
285 g_print ("Deleting created file...");
286 result = gnome_vfs_unlink (uri);
287 TEST_RESULT (result != GNOME_VFS_OK || check_file_exists (uri),
288 "ERROR DELETING FILE");
290 /* Try to delete the directory */
291 uri = "test-vfolder:///EmptyHiddenFolder";
292 g_print ("Deleting overridden directory...");
293 result = gnome_vfs_remove_directory (uri);
294 TEST_RESULT (result != GNOME_VFS_OK || check_dir_exists (uri),
295 "ERROR DELETING DIR");
299 * Add file to writedir with keywords, check presence in non-hidden
300 * folder, check presence in previsouly hidden folder, delete file,
301 * check it is not present in non-hidden folder, and check that the
302 * hidden folder is back to being not visible
304 /* Create file with keywords, check visibility */
305 realuri = g_build_filename (getenv ("GNOME_VFS_VFOLDER_WRITEDIR"),
307 "my_keyworded_file.desktop",
309 g_print ("Creating file in writedir to check keyword inclusion...");
310 result = gnome_vfs_create (&handle,
312 GNOME_VFS_OPEN_WRITE,
314 GNOME_VFS_PERM_USER_ALL);
315 TEST_RESULT (result != GNOME_VFS_OK, "ERROR CREATING FILE IN WRITEDIR");
317 content = "Categories=FakeCategory1;FakeCategory2;FakeCategory3\n\n";
318 g_print ("Writing content...");
319 result = gnome_vfs_write (handle,
323 TEST_RESULT (result != GNOME_VFS_OK, "ERROR WRITING FILE IN WRITEDIR");
325 g_print ("Closing file...");
326 result = gnome_vfs_close (handle);
327 TEST_RESULT (result != GNOME_VFS_OK, "ERROR CLOSING FILE");
329 uri = "test-vfolder:///KeywordFolder/my_keyworded_file.desktop";
330 g_print ("Checking file presence in /KeywordFolder/...");
331 TEST_RESULT (!check_file_content (uri, content),
332 "KEYWORDED FILE NOT PRESENT");
334 uri = "test-vfolder:///EmptyHiddenFolder";
335 g_print ("Checking /EmptyHiddenFolder/ is now visible...");
336 TEST_RESULT (!check_dir_exists (uri),
337 "HIDDEN DIRECTORY NOT VISIBLE");
339 uri = "test-vfolder:///EmptyHiddenFolder/my_keyworded_file.desktop";
340 g_print ("Checking file presence in /EmptyHiddenFolder/...");
341 TEST_RESULT (!check_file_content (uri, content),
342 "KEYWORDED FILE NOT PRESENT IN HIDDEN DIRECTORY");
344 g_print ("Deleting new file from writedir...");
345 result = gnome_vfs_unlink (realuri);
346 TEST_RESULT (result != GNOME_VFS_OK || !check_file_exists (realuri),
347 "ERROR DELETING FILE");
349 uri = "test-vfolder:///KeywordFolder/my_keyworded_file.desktop";
350 g_print ("Checking file is missing from /KeywordFolder/...");
351 TEST_RESULT (check_file_exists (uri),
352 "KEYWORDED FILE STILL PRESENT");
354 uri = "test-vfolder:///EmptyHiddenFolder";
355 g_print ("Checking /EmptyHiddenFolder/ is hidden again...");
356 TEST_RESULT (check_dir_exists (uri),
357 "HIDDEN DIRECTORY STILL VISIBLE");
364 * Create file and move to a different directory.
366 uri = "test-vfolder:///MyTestFolder1";
367 g_print ("Creating src directory...");
368 result = gnome_vfs_make_directory (uri, GNOME_VFS_PERM_USER_ALL);
369 TEST_RESULT (result != GNOME_VFS_OK || !check_dir_exists (uri),
370 "ERROR CREATING DIR");
372 uri = "test-vfolder:///MyTestFolder1/a_fake_file.desktop";
373 g_print ("Creating src file...");
374 result = gnome_vfs_create (&handle,
376 GNOME_VFS_OPEN_WRITE,
378 GNOME_VFS_PERM_USER_ALL);
379 TEST_RESULT (result != GNOME_VFS_OK || !check_file_exists (uri),
380 "ERROR CREATING FILE");
382 uri = "test-vfolder:///MyTestFolder2";
383 g_print ("Creating dest directory...");
384 result = gnome_vfs_make_directory (uri, GNOME_VFS_PERM_USER_ALL);
385 TEST_RESULT (result != GNOME_VFS_OK || !check_dir_exists (uri),
386 "ERROR CREATING DIR");
388 g_print ("Moving dir...");
389 result = gnome_vfs_move ("test-vfolder:///MyTestFolder1",
390 "test-vfolder:///MyTestFolder2/MyButt",
392 TEST_RESULT (result != GNOME_VFS_OK ||
393 !check_dir_exists ("test-vfolder:///MyTestFolder2/MyButt") ||
394 check_dir_exists ("test-vfolder:///MyTestFolder1"),
397 g_print ("Moving file...");
398 result = gnome_vfs_move ("test-vfolder:///MyTestFolder2/MyButt/a_fake_file.desktop",
399 "test-vfolder:///MyTestFolder2",
401 TEST_RESULT (result != GNOME_VFS_OK ||
402 !check_file_exists ("test-vfolder:///MyTestFolder2/a_fake_file.desktop") ||
403 check_file_exists ("test-vfolder:///MyTestFolder2/MyButt/a_fake_file.desktop"),
404 "ERROR MOVING FILE");
406 uri = "test-vfolder:///MyTestFolder2/a_fake_file.desktop";
407 g_print ("Deleting dest file...");
408 result = gnome_vfs_unlink (uri);
409 TEST_RESULT (result != GNOME_VFS_OK || check_file_exists (uri),
410 "ERROR DELETING FILE");
412 /* Leave these around for future tests */
414 uri = "test-vfolder:///MyTestFolder1";
415 g_print ("Deleting src directory...");
416 result = gnome_vfs_remove_directory (uri);
417 TEST_RESULT (result != GNOME_VFS_OK || check_dir_exists (uri),
418 "ERROR DELETING DIR");
420 uri = "test-vfolder:///MyTestFolder2";
421 g_print ("Deleting dest directory...");
422 result = gnome_vfs_remove_directory (uri);
423 TEST_RESULT (result != GNOME_VFS_OK || check_dir_exists (uri),
424 "ERROR DELETING DIR");
429 * Create file and rename.
431 uri = "test-vfolder:///MyTestFolder1/a_file_to_rename.desktop";
432 g_print ("Creating file...");
433 result = gnome_vfs_create (&handle,
435 GNOME_VFS_OPEN_WRITE,
437 GNOME_VFS_PERM_USER_ALL);
438 TEST_RESULT (result != GNOME_VFS_OK || check_file_exists (uri),
439 "ERROR CREATING FILE");
442 GnomeVFSFileInfo info;
445 info.name = "a_renamed_file.desktop";
447 g_print ("Renaming file...");
448 result = gnome_vfs_set_file_info (uri,
450 GNOME_VFS_SET_FILE_INFO_NAME);
452 "test-vfolder:///MyTestFolder1/a_renamed_file.desktop";
453 TEST_RESULT (result != GNOME_VFS_OK ||
454 check_file_exists (uri) ||
455 !check_file_exists (desturi),
456 "ERROR RENAMING FILE");
461 * Move new directory inside another directory.
466 * Move existing directory with children inside a new directory.
471 // delete existing dir
474 // create file with keywords, check appearance in vfolders
476 // edit existing file
478 // move existing file
479 // set filename new file
480 // set filename existing file
482 // delete existing file
484 // monitor file creation
486 // monitor file delete
487 // monitor dir creation
488 // monitor dir delete
494 main (int argc, char **argv)
496 gint iterations = 10;
497 gchar *cwd, cwdbuf [2048], *path;
499 putenv ("GNOME_VFS_MODULE_PATH=" GNOME_VFS_MODULE_PATH);
500 putenv ("GNOME_VFS_MODULE_CONFIG_PATH=" GNOME_VFS_MODULE_CONFIG_PATH);
502 cwd = getcwd (cwdbuf, sizeof (cwdbuf));
504 putenv (g_strconcat ("GNOME_VFS_VFOLDER_INFODIR=", cwd, NULL));
506 path = g_build_filename (cwd, "test-vfolder-tmp", NULL);
507 putenv (g_strconcat ("GNOME_VFS_VFOLDER_WRITEDIR=", path, NULL));
513 iterations = atoi (argv [1]);
516 if (test_vfolder_ops () != 0) {
517 g_print ("\nFAILURE!!\n");
518 gnome_vfs_shutdown ();
522 } while (--iterations);
524 gnome_vfs_shutdown ();
526 g_print ("\nSUCCESS!\n");