• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #undef G_DISABLE_ASSERT
2 
3 #include <glib.h>
4 #include <time.h>
5 #include <locale.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #define TEST_URI_0 	"file:///abc/defgh/ijklmnopqrstuvwxyz"
11 #define TEST_URI_1 	"file:///test/uri/1"
12 #define TEST_URI_2 	"file:///test/uri/2"
13 
14 #define TEST_MIME 	"text/plain"
15 
16 #define TEST_APP_NAME 	"bookmarkfile-test"
17 #define TEST_APP_EXEC 	"bookmarkfile-test %f"
18 
19 static void
test_load_from_data_dirs(void)20 test_load_from_data_dirs (void)
21 {
22   GBookmarkFile *bookmark;
23   gboolean res;
24   gchar *path = NULL;
25   GError *error = NULL;
26 
27   bookmark = g_bookmark_file_new ();
28 
29   res = g_bookmark_file_load_from_data_dirs (bookmark, "no-such-bookmark-file.xbel", &path, &error);
30 
31   g_assert (!res);
32   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
33   g_assert_null (path);
34   g_error_free (error);
35 
36   g_bookmark_file_free (bookmark);
37 }
38 
39 static void
test_to_file(void)40 test_to_file (void)
41 {
42   GBookmarkFile *bookmark;
43   const gchar *filename;
44   gboolean res;
45   GError *error = NULL;
46   gchar *in, *out;
47 
48   bookmark = g_bookmark_file_new ();
49 
50   filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
51   res = g_bookmark_file_load_from_file (bookmark, filename, &error);
52   g_assert (res);
53   g_assert_no_error (error);
54 
55   res = g_bookmark_file_to_file (bookmark, "out.xbel", &error);
56   g_assert (res);
57   g_assert_no_error (error);
58 
59   res = g_file_get_contents (filename, &in, NULL, &error);
60   g_assert (res);
61   g_assert_no_error (error);
62 
63   res = g_file_get_contents ("out.xbel", &out, NULL, &error);
64   g_assert (res);
65   g_assert_no_error (error);
66   remove ("out.xbel");
67 
68   g_assert_cmpstr (in, ==, out);
69   g_free (in);
70   g_free (out);
71 
72   g_bookmark_file_free (bookmark);
73 }
74 
75 static void
test_move_item(void)76 test_move_item (void)
77 {
78   GBookmarkFile *bookmark;
79   const gchar *filename;
80   gboolean res;
81   GError *error = NULL;
82 
83   bookmark = g_bookmark_file_new ();
84 
85   filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
86   res = g_bookmark_file_load_from_file (bookmark, filename, &error);
87   g_assert (res);
88   g_assert_no_error (error);
89 
90   res = g_bookmark_file_move_item (bookmark,
91                                    "file:///home/zefram/Documents/milan-stuttgart.ps",
92                                    "file:///tmp/schedule.ps",
93                                    &error);
94   g_assert (res);
95   g_assert_no_error (error);
96 
97   res = g_bookmark_file_move_item (bookmark,
98                                    "file:///tmp/schedule.ps",
99                                    "file:///tmp/schedule.ps",
100                                    &error);
101   g_assert (res);
102   g_assert_no_error (error);
103 
104   res = g_bookmark_file_move_item (bookmark,
105                                    "file:///no-such-file.xbel",
106                                    "file:///tmp/schedule.ps",
107                                    &error);
108   g_assert (!res);
109   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
110   g_clear_error (&error);
111 
112   res = g_bookmark_file_move_item (bookmark,
113                                    "file:///tmp/schedule.ps",
114                                    NULL,
115                                    &error);
116   g_assert (res);
117   g_assert_no_error (error);
118 
119   g_bookmark_file_free (bookmark);
120 }
121 
122 static void
test_misc(void)123 test_misc (void)
124 {
125   GBookmarkFile *bookmark;
126   const gchar *filename;
127   gboolean res;
128   GError *error = NULL;
129   gchar *s;
130   time_t now, t;
131   gchar *cmd, *exec;
132   guint count;
133 
134   bookmark = g_bookmark_file_new ();
135 
136   filename = g_test_get_filename (G_TEST_DIST, "bookmarks", "valid-01.xbel", NULL);
137   res = g_bookmark_file_load_from_file (bookmark, filename, &error);
138   g_assert (res);
139   g_assert_no_error (error);
140 
141   res = g_bookmark_file_get_icon (bookmark,
142                                    "file:///home/zefram/Documents/milan-stuttgart.ps",
143                                   NULL,
144                                   NULL,
145                                   &error);
146   g_assert (!res);
147   g_assert_no_error (error);
148 
149   res = g_bookmark_file_get_icon (bookmark,
150                                   "file:///tmp/schedule.ps",
151                                   NULL,
152                                   NULL,
153                                   &error);
154   g_assert (!res);
155   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
156   g_clear_error (&error);
157 
158   g_bookmark_file_set_description (bookmark,
159                                    "file:///tmp/schedule0.ps",
160                                    "imaginary schedule");
161   s = g_bookmark_file_get_description (bookmark,
162                                        "file:///tmp/schedule0.ps",
163                                        &error);
164   g_assert_no_error (error);
165   g_assert_cmpstr (s, ==, "imaginary schedule");
166   g_free (s);
167   s = g_bookmark_file_get_mime_type (bookmark,
168                                      "file:///tmp/schedule0.ps",
169                                      &error);
170   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
171   g_assert_null (s);
172   g_clear_error (&error);
173   res = g_bookmark_file_get_is_private (bookmark,
174                                         "file:///tmp/schedule0.ps",
175                                         &error);
176   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_INVALID_VALUE);
177   g_clear_error (&error);
178 
179   g_bookmark_file_set_mime_type (bookmark,
180                                  "file:///tmp/schedule1.ps",
181                                  "image/png");
182   s = g_bookmark_file_get_mime_type (bookmark,
183                                      "file:///tmp/schedule1.ps",
184                                      &error);
185   g_assert_no_error (error);
186   g_assert_cmpstr (s, ==, "image/png");
187   g_free (s);
188 
189   g_bookmark_file_set_is_private (bookmark,
190                                   "file:///tmp/schedule2.ps",
191                                   TRUE);
192   res = g_bookmark_file_get_is_private (bookmark,
193                                         "file:///tmp/schedule2.ps",
194                                         &error);
195   g_assert_no_error (error);
196   g_assert (res);
197 
198   time (&now);
199   g_bookmark_file_set_added (bookmark,
200                              "file:///tmp/schedule3.ps",
201                              (time_t)-1);
202   t = g_bookmark_file_get_added (bookmark,
203                                  "file:///tmp/schedule3.ps",
204                                  &error);
205   g_assert_no_error (error);
206   g_assert (t == now);
207 
208   g_bookmark_file_set_modified (bookmark,
209                                 "file:///tmp/schedule4.ps",
210                                 (time_t)-1);
211   t = g_bookmark_file_get_modified (bookmark,
212                                     "file:///tmp/schedule4.ps",
213                                     &error);
214   g_assert_no_error (error);
215   g_assert (t == now);
216 
217   g_bookmark_file_set_visited (bookmark,
218                                "file:///tmp/schedule5.ps",
219                                (time_t)-1);
220   t = g_bookmark_file_get_visited (bookmark,
221                                    "file:///tmp/schedule5.ps",
222                                    &error);
223   g_assert_no_error (error);
224   g_assert (t == now);
225 
226   g_bookmark_file_set_icon (bookmark,
227                             "file:///tmp/schedule6.ps",
228                             "application-x-postscript",
229                             "image/png");
230   res = g_bookmark_file_get_icon (bookmark,
231                                   "file:///tmp/schedule6.ps",
232                                   &s,
233                                   NULL,
234                                   &error);
235   g_assert_no_error (error);
236   g_assert (res);
237   g_assert_cmpstr (s, ==, "application-x-postscript");
238   g_free (s);
239 
240   g_bookmark_file_set_icon (bookmark,
241                             "file:///tmp/schedule6.ps",
242                             NULL, NULL);
243   res = g_bookmark_file_get_icon (bookmark,
244                                   "file:///tmp/schedule6.ps",
245                                   &s,
246                                   NULL,
247                                   &error);
248   g_assert_no_error (error);
249   g_assert (!res);
250 
251   res = g_bookmark_file_has_application (bookmark,
252                                          "file:///tmp/schedule7.ps",
253                                          "foo",
254                                          &error);
255   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
256   g_assert (!res);
257   g_clear_error (&error);
258 
259   g_bookmark_file_add_application (bookmark,
260                                    "file:///tmp/schedule7.ps",
261                                    NULL, NULL);
262   res = g_bookmark_file_get_app_info (bookmark,
263                                       "file:///tmp/schedule7.ps",
264                                       g_get_application_name (),
265                                       &exec, &count, &t,
266                                       &error);
267   g_assert_no_error (error);
268   g_assert (res);
269   cmd = g_strconcat (g_get_prgname (), " file:///tmp/schedule7.ps", NULL);
270   g_assert_cmpstr (exec, ==, cmd);
271   g_free (cmd);
272   g_free (exec);
273   g_assert_cmpuint (count, ==, 1);
274   g_assert (t == now);
275 
276   g_bookmark_file_free (bookmark);
277 }
278 
279 static gboolean
test_load(GBookmarkFile * bookmark,const gchar * filename)280 test_load (GBookmarkFile *bookmark,
281            const gchar   *filename)
282 {
283   GError *error = NULL;
284   gboolean res;
285 
286   res = g_bookmark_file_load_from_file (bookmark, filename, &error);
287   if (error && g_test_verbose ())
288     g_printerr ("Load error: %s\n", error->message);
289 
290   g_clear_error (&error);
291   return res;
292 }
293 
294 static void
test_query(GBookmarkFile * bookmark)295 test_query (GBookmarkFile *bookmark)
296 {
297   gint size;
298   gchar **uris;
299   gsize uris_len, i;
300   gchar *mime;
301   GError *error;
302 
303   size = g_bookmark_file_get_size (bookmark);
304   uris = g_bookmark_file_get_uris (bookmark, &uris_len);
305 
306   g_assert_cmpint (uris_len, ==, size);
307 
308   for (i = 0; i < uris_len; i++)
309     {
310       g_assert (g_bookmark_file_has_item (bookmark, uris[i]));
311       error = NULL;
312       mime = g_bookmark_file_get_mime_type (bookmark, uris[i], &error);
313       g_assert (mime != NULL);
314       g_assert_no_error (error);
315       g_free (mime);
316     }
317   g_strfreev (uris);
318 
319   g_assert (!g_bookmark_file_has_item (bookmark, "file:///no/such/uri"));
320   error = NULL;
321   mime = g_bookmark_file_get_mime_type (bookmark, "file:///no/such/uri", &error);
322   g_assert (mime == NULL);
323   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
324   g_error_free (error);
325   g_free (mime);
326 }
327 
328 static gboolean
test_modify(GBookmarkFile * bookmark)329 test_modify (GBookmarkFile *bookmark)
330 {
331   gchar *text;
332   guint count;
333   time_t stamp;
334   time_t now;
335   GError *error = NULL;
336   gchar **groups;
337   gsize length;
338   gchar **apps;
339   gchar *icon;
340   gchar *mime;
341 
342   if (g_test_verbose ())
343     g_printerr ("\t=> check global title/description...");
344   g_bookmark_file_set_title (bookmark, NULL, "a file");
345   g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
346 
347   text = g_bookmark_file_get_title (bookmark, NULL, &error);
348   g_assert_no_error (error);
349   g_assert_cmpstr (text, ==, "a file");
350   g_free (text);
351 
352   text = g_bookmark_file_get_description (bookmark, NULL, &error);
353   g_assert_no_error (error);
354   g_assert_cmpstr (text, ==, "a bookmark file");
355   g_free (text);
356   if (g_test_verbose ())
357     g_printerr ("ok\n");
358 
359   if (g_test_verbose ())
360     g_printerr ("\t=> check bookmark title/description...");
361   g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
362   g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
363   g_bookmark_file_set_is_private (bookmark, TEST_URI_0, TRUE);
364   time (&now);
365   g_bookmark_file_set_added (bookmark, TEST_URI_0, now);
366   g_bookmark_file_set_modified (bookmark, TEST_URI_0, now);
367   g_bookmark_file_set_visited (bookmark, TEST_URI_0, now);
368   g_bookmark_file_set_icon (bookmark, TEST_URI_0, "testicon", "image/png");
369 
370   text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
371   g_assert_no_error (error);
372   g_assert_cmpstr (text, ==, "a title");
373   g_free (text);
374   text = g_bookmark_file_get_description (bookmark, TEST_URI_0, &error);
375   g_assert_no_error (error);
376   g_assert_cmpstr (text, ==, "a description");
377   g_free (text);
378   g_assert (g_bookmark_file_get_is_private (bookmark, TEST_URI_0, &error));
379   g_assert_no_error (error);
380   stamp = g_bookmark_file_get_added (bookmark, TEST_URI_0, &error);
381   g_assert_no_error (error);
382   g_assert (stamp == now);
383   stamp = g_bookmark_file_get_modified (bookmark, TEST_URI_0, &error);
384   g_assert_no_error (error);
385   g_assert (stamp == now);
386   stamp = g_bookmark_file_get_visited (bookmark, TEST_URI_0, &error);
387   g_assert_no_error (error);
388   g_assert (stamp == now);
389   g_assert (g_bookmark_file_get_icon (bookmark, TEST_URI_0, &icon, &mime, &error));
390   g_assert_no_error (error);
391   g_assert_cmpstr (icon, ==, "testicon");
392   g_assert_cmpstr (mime, ==, "image/png");
393   g_free (icon);
394   g_free (mime);
395   if (g_test_verbose ())
396     g_printerr ("ok\n");
397 
398   if (g_test_verbose ())
399     g_printerr ("\t=> check non existing bookmark...");
400   g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
401   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
402   g_clear_error (&error);
403   g_bookmark_file_get_is_private (bookmark, TEST_URI_1, &error);
404   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
405   g_clear_error (&error);
406   g_bookmark_file_get_added (bookmark, TEST_URI_1, &error);
407   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
408   g_clear_error (&error);
409   g_bookmark_file_get_modified (bookmark, TEST_URI_1, &error);
410   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
411   g_clear_error (&error);
412   g_bookmark_file_get_visited (bookmark, TEST_URI_1, &error);
413   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
414   g_clear_error (&error);
415   if (g_test_verbose ())
416     g_printerr ("ok\n");
417 
418   if (g_test_verbose ())
419     g_printerr ("\t=> check application...");
420   g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
421   g_assert (!g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
422   g_bookmark_file_add_application (bookmark, TEST_URI_0,
423 				   TEST_APP_NAME,
424 				   TEST_APP_EXEC);
425   g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL));
426   g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
427 		  		&text,
428 				&count,
429 				&stamp,
430 				&error);
431   g_assert_no_error (error);
432   g_assert (count == 1);
433   g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
434   g_free (text);
435   g_assert (g_bookmark_file_remove_application (bookmark, TEST_URI_0, TEST_APP_NAME, &error));
436   g_assert_no_error (error);
437   g_bookmark_file_add_application (bookmark, TEST_URI_0, TEST_APP_NAME, TEST_APP_EXEC);
438   apps = g_bookmark_file_get_applications (bookmark, TEST_URI_0, &length, &error);
439   g_assert_no_error (error);
440   g_assert_cmpint (length, ==, 1);
441   g_assert_cmpstr (apps[0], ==, TEST_APP_NAME);
442   g_strfreev (apps);
443 
444   g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
445 		  		&text,
446 				&count,
447 				&stamp,
448 				&error);
449   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
450   g_clear_error (&error);
451 
452   if (g_test_verbose ())
453     g_printerr ("ok\n");
454 
455   if (g_test_verbose ())
456     g_printerr ("\t=> check groups...");
457   g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
458   g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
459   g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL));
460   g_assert (!g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL));
461   g_assert (g_bookmark_file_remove_group (bookmark, TEST_URI_1, "Test", &error));
462   g_assert_no_error (error);
463   groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, NULL, &error);
464   g_assert_cmpint (g_strv_length (groups), ==, 0);
465   g_strfreev (groups);
466   groups = g_new0 (gchar *, 3);
467   groups[0] = "Group1";
468   groups[1] = "Group2";
469   groups[2] = NULL;
470   g_bookmark_file_set_groups (bookmark, TEST_URI_1, (const gchar **)groups, 2);
471   g_free (groups);
472   groups = g_bookmark_file_get_groups (bookmark, TEST_URI_1, &length, &error);
473   g_assert_cmpint (length, ==, 2);
474   g_strfreev (groups);
475   g_assert_no_error (error);
476 
477   if (g_test_verbose ())
478     g_printerr ("ok\n");
479 
480   if (g_test_verbose ())
481     g_printerr ("\t=> check remove...");
482   g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
483   g_assert_no_error (error);
484   g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
485   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
486   g_clear_error (&error);
487   if (g_test_verbose ())
488     g_printerr ("ok\n");
489 
490   return TRUE;
491 }
492 
493 static void
test_file(gconstpointer d)494 test_file (gconstpointer d)
495 {
496   const gchar *filename = d;
497   GBookmarkFile *bookmark_file;
498   gboolean success;
499   gchar *data;
500   GError *error;
501 
502   bookmark_file = g_bookmark_file_new ();
503   g_assert (bookmark_file != NULL);
504 
505   success = test_load (bookmark_file, filename);
506 
507   if (success)
508     {
509       test_query (bookmark_file);
510       test_modify (bookmark_file);
511 
512       error = NULL;
513       data = g_bookmark_file_to_data (bookmark_file, NULL, &error);
514       g_assert_no_error (error);
515       /* FIXME do some checks on data */
516       g_free (data);
517     }
518 
519   g_bookmark_file_free (bookmark_file);
520 
521   g_assert (success == (strstr (filename, "fail") == NULL));
522 }
523 
524 int
main(int argc,char * argv[])525 main (int argc, char *argv[])
526 {
527   GDir *dir;
528   GError *error;
529   const gchar *name;
530   gchar *path;
531 
532   g_test_init (&argc, &argv, NULL);
533 
534   if (argc > 1)
535     {
536       test_file (argv[1]);
537       return 0;
538     }
539 
540   g_test_add_func ("/bookmarks/load-from-data-dirs", test_load_from_data_dirs);
541   g_test_add_func ("/bookmarks/to-file", test_to_file);
542   g_test_add_func ("/bookmarks/move-item", test_move_item);
543   g_test_add_func ("/bookmarks/misc", test_misc);
544 
545   error = NULL;
546   path = g_test_build_filename (G_TEST_DIST, "bookmarks", NULL);
547   dir = g_dir_open (path, 0, &error);
548   g_free (path);
549   g_assert_no_error (error);
550   while ((name = g_dir_read_name (dir)) != NULL)
551     {
552       if (!g_str_has_suffix (name, ".xbel"))
553         continue;
554 
555       path = g_strdup_printf ("/bookmarks/parse/%s", name);
556       g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "bookmarks", name, NULL),
557                                  test_file, g_free);
558       g_free (path);
559     }
560   g_dir_close (dir);
561 
562   return g_test_run ();
563 }
564