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