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