ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / modules / vfolder / test-vfolder.c
1
2 #include <glib.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <libgnomevfs/gnome-vfs.h>
7
8 static gboolean
9 check_dir_exists (gchar *file_uri)
10 {
11         GnomeVFSDirectoryHandle *handle;
12         GnomeVFSResult result;
13
14         result = gnome_vfs_directory_open (&handle,
15                                            file_uri,
16                                            GNOME_VFS_FILE_INFO_DEFAULT);
17         if (result == GNOME_VFS_OK) {
18                 gnome_vfs_directory_close (handle);
19                 return TRUE;
20         } else 
21                 return FALSE;
22 }
23
24 #if 0
25 static gint
26 check_dir_count (gchar *file_uri)
27 {
28         GnomeVFSResult result;
29         GList *files;
30         gint retval;
31
32         result = gnome_vfs_directory_list_load (&files,
33                                                 file_uri,
34                                                 GNOME_VFS_FILE_INFO_DEFAULT);
35         if (result != GNOME_VFS_OK)
36                 return FALSE;
37
38         retval = g_list_length (files);
39
40         gnome_vfs_file_info_list_free (files);
41
42         return retval;
43 }
44 #endif
45
46 static gboolean
47 check_file_exists (gchar *file_uri)
48 {
49         GnomeVFSHandle *handle;
50         GnomeVFSResult result;
51
52         result = gnome_vfs_open (&handle,
53                                  file_uri,
54                                  GNOME_VFS_OPEN_READ);
55         if (result == GNOME_VFS_OK) {
56                 gnome_vfs_close (handle);
57                 return TRUE;
58         } else 
59                 return FALSE;
60 }
61
62 static gboolean
63 check_file_content (gchar *file_uri, gchar *content)
64 {
65         GnomeVFSHandle *handle;
66         GnomeVFSResult result;
67         GnomeVFSFileSize readlen;
68         gchar readbuf [2048];
69         gint idx = 0, len;
70
71         len = strlen (content);
72
73         result = gnome_vfs_open (&handle,
74                                  file_uri,
75                                  GNOME_VFS_OPEN_READ);
76         if (result != GNOME_VFS_OK)
77                 return FALSE;
78
79         while (idx < len) {
80                 result = gnome_vfs_read (handle, 
81                                          readbuf, 
82                                          sizeof (readbuf),
83                                          &readlen);
84                 if (result != GNOME_VFS_OK) 
85                         goto ERROR;
86
87                 idx += readlen;
88                 if (idx > len)
89                         goto ERROR;
90
91                 if (memcmp (readbuf, 
92                             &content [idx], 
93                             MIN (len - idx, readlen)) != 0)
94                         goto ERROR;
95         }
96         
97         gnome_vfs_close (handle);
98         return TRUE;
99
100  ERROR:
101         gnome_vfs_close (handle);
102         return FALSE;
103 }
104
105 #define TEST_FAILURE(errstr)                                   \
106         do {                                                   \
107                 g_print (" " errstr ": %s\n",                  \
108                          gnome_vfs_result_to_string (result)); \
109                 return 1;                                      \
110         } while (0)
111
112 #define TEST_SUCCESS g_print (" DONE.\n")
113
114 #define TEST_RESULT(predicate, error)        \
115         if (predicate) TEST_FAILURE (error); \
116         else TEST_SUCCESS
117
118 static gint 
119 test_vfolder_ops (void)
120 {
121         GnomeVFSHandle *handle;
122         GnomeVFSDirectoryHandle *dhandle;
123         GnomeVFSResult result;
124         gchar *uri, *realuri, *content;
125         GnomeVFSFileSize writelen;
126
127         uri = "test-vfolder:///";
128         g_print ("Opening %s...\n", uri);
129         result = gnome_vfs_directory_open (&dhandle,
130                                            uri,
131                                            GNOME_VFS_FILE_INFO_DEFAULT);
132         TEST_RESULT (result != GNOME_VFS_OK, "ERROR OPENNING");
133
134         /* 
135          * Directory Tests 
136          */
137
138         /* 
139          * TEST 1: 
140          * Create a new directory and delete it 
141          */
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");
148
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");
155
156
157         /* 
158          * TEST 2: 
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
161          * directory.  
162          */
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");
169
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, 
174                                    uri, 
175                                    GNOME_VFS_OPEN_READ | GNOME_VFS_OPEN_WRITE, 
176                                    FALSE,
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");
182
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");
190
191         /* Delete file */
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");
197
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");
204
205         
206         /* 
207          * TEST 3:
208          * Check creating an existing directory fails.  Only run during first 
209          * iteration.
210          */
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");
219
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");
225         }
226
227         /* 
228          * TEST 4: 
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.
233          */
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");
239
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");
246
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, 
251                                    uri, 
252                                    GNOME_VFS_OPEN_WRITE, 
253                                    FALSE,
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");
257
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");
264
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, 
269                                    uri, 
270                                    GNOME_VFS_OPEN_WRITE, 
271                                    FALSE,
272                                    GNOME_VFS_PERM_USER_ALL);
273         TEST_RESULT (result != GNOME_VFS_OK || !check_file_exists (uri),
274                      "ERROR CREATING FILE IN DIR");
275
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");
282
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");
289
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");
296
297         /* 
298          * TEST 5: 
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 
303          */
304         /* Create file with keywords, check visibility */
305         realuri = g_build_filename (getenv ("GNOME_VFS_VFOLDER_WRITEDIR"),
306                                     "test-vfolder",
307                                     "my_keyworded_file.desktop",
308                                     NULL);
309         g_print ("Creating file in writedir to check keyword inclusion...");
310         result = gnome_vfs_create (&handle, 
311                                    realuri, 
312                                    GNOME_VFS_OPEN_WRITE, 
313                                    FALSE,
314                                    GNOME_VFS_PERM_USER_ALL);
315         TEST_RESULT (result != GNOME_VFS_OK, "ERROR CREATING FILE IN WRITEDIR");
316
317         content = "Categories=FakeCategory1;FakeCategory2;FakeCategory3\n\n";
318         g_print ("Writing content...");
319         result = gnome_vfs_write (handle,
320                                   content,
321                                   strlen (content),
322                                   &writelen);
323         TEST_RESULT (result != GNOME_VFS_OK, "ERROR WRITING FILE IN WRITEDIR");
324
325         g_print ("Closing file...");
326         result = gnome_vfs_close (handle);
327         TEST_RESULT (result != GNOME_VFS_OK, "ERROR CLOSING FILE");
328
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");
333
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");
338
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");
343
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");
348
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");
353
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");
358
359         g_free (realuri);
360
361
362         /* 
363          * TEST 6:
364          * Create file and move to a different directory.
365          */
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");
371
372         uri = "test-vfolder:///MyTestFolder1/a_fake_file.desktop";
373         g_print ("Creating src file...");
374         result = gnome_vfs_create (&handle, 
375                                    uri, 
376                                    GNOME_VFS_OPEN_WRITE, 
377                                    FALSE,
378                                    GNOME_VFS_PERM_USER_ALL);
379         TEST_RESULT (result != GNOME_VFS_OK || !check_file_exists (uri), 
380                      "ERROR CREATING FILE");
381
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");
387         
388         g_print ("Moving dir...");
389         result = gnome_vfs_move ("test-vfolder:///MyTestFolder1",
390                                  "test-vfolder:///MyTestFolder2/MyButt",
391                                  TRUE);
392         TEST_RESULT (result != GNOME_VFS_OK || 
393                      !check_dir_exists ("test-vfolder:///MyTestFolder2/MyButt") ||
394                      check_dir_exists ("test-vfolder:///MyTestFolder1"),
395                      "ERROR MOVING DIR");
396
397         g_print ("Moving file...");
398         result = gnome_vfs_move ("test-vfolder:///MyTestFolder2/MyButt/a_fake_file.desktop",
399                                  "test-vfolder:///MyTestFolder2",
400                                  TRUE);
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");
405
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");
411
412         /* Leave these around for future tests */
413         /*
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");
419
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");
425         */
426
427         /* 
428          * TEST 7:
429          * Create file and rename.
430          */
431         uri = "test-vfolder:///MyTestFolder1/a_file_to_rename.desktop";
432         g_print ("Creating file...");
433         result = gnome_vfs_create (&handle, 
434                                    uri, 
435                                    GNOME_VFS_OPEN_WRITE, 
436                                    FALSE,
437                                    GNOME_VFS_PERM_USER_ALL);
438         TEST_RESULT (result != GNOME_VFS_OK || check_file_exists (uri), 
439                      "ERROR CREATING FILE");
440
441         {
442                 GnomeVFSFileInfo info;
443                 gchar *desturi;
444
445                 info.name = "a_renamed_file.desktop";
446
447                 g_print ("Renaming file...");
448                 result = gnome_vfs_set_file_info (uri,
449                                                   &info,
450                                                   GNOME_VFS_SET_FILE_INFO_NAME);
451                 desturi = 
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");
457         }
458
459         /* 
460          * TEST 8:
461          * Move new directory inside another directory.
462          */
463
464         /* 
465          * TEST 9:
466          * Move existing directory with children inside a new directory.
467          */
468
469         // create dir
470         // delete new dir
471         // delete existing dir
472
473         // create file
474         // create file with keywords, check appearance in vfolders
475         // edit new file
476         // edit existing file
477         // move new file
478         // move existing file
479         // set filename new file
480         // set filename existing file
481         // delete new file
482         // delete existing file
483         
484         // monitor file creation
485         // monitor file edit
486         // monitor file delete
487         // monitor dir creation
488         // monitor dir delete   
489         
490         return 0;
491 }
492
493 int 
494 main (int argc, char **argv)
495 {
496         gint iterations = 10;
497         gchar *cwd, cwdbuf [2048], *path;
498
499         putenv ("GNOME_VFS_MODULE_PATH=" GNOME_VFS_MODULE_PATH);
500         putenv ("GNOME_VFS_MODULE_CONFIG_PATH=" GNOME_VFS_MODULE_CONFIG_PATH);
501
502         cwd = getcwd (cwdbuf, sizeof (cwdbuf));
503
504         putenv (g_strconcat ("GNOME_VFS_VFOLDER_INFODIR=", cwd, NULL));
505
506         path = g_build_filename (cwd, "test-vfolder-tmp", NULL);
507         putenv (g_strconcat ("GNOME_VFS_VFOLDER_WRITEDIR=", path, NULL));
508         g_free (path);
509
510         gnome_vfs_init ();
511
512         if (argc > 1)
513                 iterations = atoi (argv [1]);
514
515         do {
516                 if (test_vfolder_ops () != 0) {
517                         g_print ("\nFAILURE!!\n");
518                         gnome_vfs_shutdown ();
519                         return 1;
520                 }
521                 g_print ("\n\n");
522         } while (--iterations);
523
524         gnome_vfs_shutdown ();
525
526         g_print ("\nSUCCESS!\n");
527         
528         return 0;
529 }