Add some more cases to the app-id unit tests
[glib.git] / glib / tests / keyfile.c
blob865368b22ef8381480e4ad857ad8a3c29b7176eb
2 #include <glib.h>
3 #include <glib/gstdio.h>
4 #include <locale.h>
5 #include <string.h>
6 #include <stdlib.h>
8 static GKeyFile *
9 load_data (const gchar *data,
10 GKeyFileFlags flags)
12 GKeyFile *keyfile;
13 GError *error = NULL;
15 keyfile = g_key_file_new ();
16 g_key_file_load_from_data (keyfile, data, -1, flags, &error);
17 g_assert_no_error (error);
18 return keyfile;
21 static void
22 check_error (GError **error,
23 GQuark domain,
24 gint code)
26 g_assert_error (*error, domain, code);
27 g_error_free (*error);
28 *error = NULL;
31 static void
32 check_no_error (GError **error)
34 g_assert_no_error (*error);
37 static void
38 check_string_value (GKeyFile *keyfile,
39 const gchar *group,
40 const gchar *key,
41 const gchar *expected)
43 GError *error = NULL;
44 gchar *value;
46 value = g_key_file_get_string (keyfile, group, key, &error);
47 check_no_error (&error);
48 g_assert (value != NULL);
49 g_assert_cmpstr (value, ==, expected);
50 g_free (value);
53 static void
54 check_locale_string_value (GKeyFile *keyfile,
55 const gchar *group,
56 const gchar *key,
57 const gchar *locale,
58 const gchar *expected)
60 GError *error = NULL;
61 gchar *value;
63 value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
64 check_no_error (&error);
65 g_assert (value != NULL);
66 g_assert_cmpstr (value, ==, expected);
67 g_free (value);
70 static void
71 check_string_list_value (GKeyFile *keyfile,
72 const gchar *group,
73 const gchar *key,
74 ...)
76 gint i;
77 gchar *v, **value;
78 va_list args;
79 gsize len;
80 GError *error = NULL;
82 value = g_key_file_get_string_list (keyfile, group, key, &len, &error);
83 check_no_error (&error);
84 g_assert (value != NULL);
86 va_start (args, key);
87 i = 0;
88 v = va_arg (args, gchar*);
89 while (v)
91 g_assert (value[i] != NULL);
92 g_assert_cmpstr (v, ==, value[i]);
93 i++;
94 v = va_arg (args, gchar*);
97 va_end (args);
99 g_strfreev (value);
102 static void
103 check_locale_string_list_value (GKeyFile *keyfile,
104 const gchar *group,
105 const gchar *key,
106 const gchar *locale,
107 ...)
109 gint i;
110 gchar *v, **value;
111 va_list args;
112 gsize len;
113 GError *error = NULL;
115 value = g_key_file_get_locale_string_list (keyfile, group, key, locale, &len, &error);
116 check_no_error (&error);
117 g_assert (value != NULL);
119 va_start (args, locale);
120 i = 0;
121 v = va_arg (args, gchar*);
122 while (v)
124 g_assert (value[i] != NULL);
125 g_assert_cmpstr (v, ==, value[i]);
126 i++;
127 v = va_arg (args, gchar*);
130 va_end (args);
132 g_strfreev (value);
135 static void
136 check_integer_list_value (GKeyFile *keyfile,
137 const gchar *group,
138 const gchar *key,
139 ...)
141 gint i;
142 gint v, *value;
143 va_list args;
144 gsize len;
145 GError *error = NULL;
147 value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
148 check_no_error (&error);
149 g_assert (value != NULL);
151 va_start (args, key);
152 i = 0;
153 v = va_arg (args, gint);
154 while (v != -100)
156 g_assert_cmpint (i, <, len);
157 g_assert_cmpint (value[i], ==, v);
158 i++;
159 v = va_arg (args, gint);
162 va_end (args);
164 g_free (value);
167 static void
168 check_double_list_value (GKeyFile *keyfile,
169 const gchar *group,
170 const gchar *key,
171 ...)
173 gint i;
174 gdouble v, *value;
175 va_list args;
176 gsize len;
177 GError *error = NULL;
179 value = g_key_file_get_double_list (keyfile, group, key, &len, &error);
180 check_no_error (&error);
181 g_assert (value != NULL);
183 va_start (args, key);
184 i = 0;
185 v = va_arg (args, gdouble);
186 while (v != -100)
188 g_assert_cmpint (i, <, len);
189 g_assert_cmpfloat (value[i], ==, v);
190 i++;
191 v = va_arg (args, gdouble);
194 va_end (args);
196 g_free (value);
199 static void
200 check_boolean_list_value (GKeyFile *keyfile,
201 const gchar *group,
202 const gchar *key,
203 ...)
205 gint i;
206 gboolean v, *value;
207 va_list args;
208 gsize len;
209 GError *error = NULL;
211 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
212 check_no_error (&error);
213 g_assert (value != NULL);
215 va_start (args, key);
216 i = 0;
217 v = va_arg (args, gboolean);
218 while (v != -100)
220 g_assert_cmpint (i, <, len);
221 g_assert_cmpint (value[i], ==, v);
222 i++;
223 v = va_arg (args, gboolean);
226 va_end (args);
228 g_free (value);
231 static void
232 check_boolean_value (GKeyFile *keyfile,
233 const gchar *group,
234 const gchar *key,
235 gboolean expected)
237 GError *error = NULL;
238 gboolean value;
240 value = g_key_file_get_boolean (keyfile, group, key, &error);
241 check_no_error (&error);
242 g_assert_cmpint (value, ==, expected);
245 static void
246 check_integer_value (GKeyFile *keyfile,
247 const gchar *group,
248 const gchar *key,
249 gint expected)
251 GError *error = NULL;
252 gint value;
254 value = g_key_file_get_integer (keyfile, group, key, &error);
255 check_no_error (&error);
256 g_assert_cmpint (value, ==, expected);
259 static void
260 check_double_value (GKeyFile *keyfile,
261 const gchar *group,
262 const gchar *key,
263 gdouble expected)
265 GError *error = NULL;
266 gdouble value;
268 value = g_key_file_get_double (keyfile, group, key, &error);
269 check_no_error (&error);
270 g_assert_cmpfloat (value, ==, expected);
273 static void
274 check_name (const gchar *what,
275 const gchar *value,
276 const gchar *expected,
277 gint position)
279 g_assert_cmpstr (value, ==, expected);
282 static void
283 check_length (const gchar *what,
284 gint n_items,
285 gint length,
286 gint expected)
288 g_assert_cmpint (n_items, ==, length);
289 g_assert_cmpint (n_items, ==, expected);
293 /* check that both \n and \r\n are accepted as line ends,
294 * and that stray \r are passed through
296 static void
297 test_line_ends (void)
299 GKeyFile *keyfile;
301 const gchar *data =
302 "[group1]\n"
303 "key1=value1\n"
304 "key2=value2\r\n"
305 "[group2]\r\n"
306 "key3=value3\r\r\n"
307 "key4=value4\n";
309 keyfile = load_data (data, 0);
311 check_string_value (keyfile, "group1", "key1", "value1");
312 check_string_value (keyfile, "group1", "key2", "value2");
313 check_string_value (keyfile, "group2", "key3", "value3\r");
314 check_string_value (keyfile, "group2", "key4", "value4");
316 g_key_file_free (keyfile);
319 /* check handling of whitespace
321 static void
322 test_whitespace (void)
324 GKeyFile *keyfile;
326 const gchar *data =
327 "[group1]\n"
328 "key1 = value1\n"
329 "key2\t=\tvalue2\n"
330 " [ group2 ] \n"
331 "key3 = value3 \n"
332 "key4 = value \t4\n"
333 " key5 = value5\n";
335 keyfile = load_data (data, 0);
337 check_string_value (keyfile, "group1", "key1", "value1");
338 check_string_value (keyfile, "group1", "key2", "value2");
339 check_string_value (keyfile, " group2 ", "key3", "value3 ");
340 check_string_value (keyfile, " group2 ", "key4", "value \t4");
341 check_string_value (keyfile, " group2 ", "key5", "value5");
343 g_key_file_free (keyfile);
346 /* check handling of comments
348 static void
349 test_comments (void)
351 GKeyFile *keyfile;
352 gchar **names;
353 gsize len;
354 GError *error = NULL;
355 gchar *comment;
357 const gchar *data =
358 "# top comment\n"
359 "# top comment, continued\n"
360 "[group1]\n"
361 "key1 = value1\n"
362 "# key comment\n"
363 "# key comment, continued\n"
364 "key2 = value2\n"
365 "# line end check\r\n"
366 "key3 = value3\n"
367 "key4 = value4\n"
368 "# group comment\n"
369 "# group comment, continued\n"
370 "[group2]\n";
372 const gchar *top_comment= " top comment\n top comment, continued\n";
373 const gchar *group_comment= " group comment\n group comment, continued\n";
374 const gchar *key_comment= " key comment\n key comment, continued\n";
376 keyfile = load_data (data, 0);
378 check_string_value (keyfile, "group1", "key1", "value1");
379 check_string_value (keyfile, "group1", "key2", "value2");
380 check_string_value (keyfile, "group1", "key3", "value3");
381 check_string_value (keyfile, "group1", "key4", "value4");
383 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
384 check_no_error (&error);
386 check_length ("keys", g_strv_length (names), len, 4);
387 check_name ("key", names[0], "key1", 0);
388 check_name ("key", names[1], "key2", 1);
389 check_name ("key", names[2], "key3", 2);
390 check_name ("key", names[3], "key4", 3);
392 g_strfreev (names);
394 g_key_file_free (keyfile);
396 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
398 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
399 check_no_error (&error);
401 check_length ("keys", g_strv_length (names), len, 4);
402 check_name ("key", names[0], "key1", 0);
403 check_name ("key", names[1], "key2", 1);
404 check_name ("key", names[2], "key3", 2);
405 check_name ("key", names[3], "key4", 3);
407 g_strfreev (names);
409 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
410 check_no_error (&error);
411 check_name ("top comment", comment, top_comment, 0);
412 g_free (comment);
414 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
415 check_no_error (&error);
416 check_name ("key comment", comment, key_comment, 0);
417 g_free (comment);
419 g_key_file_remove_comment (keyfile, "group1", "key2", &error);
420 check_no_error (&error);
421 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
422 check_no_error (&error);
423 g_assert (comment == NULL);
425 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
426 check_no_error (&error);
427 check_name ("group comment", comment, group_comment, 0);
428 g_free (comment);
430 comment = g_key_file_get_comment (keyfile, "group3", NULL, &error);
431 check_error (&error,
432 G_KEY_FILE_ERROR,
433 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
434 g_assert (comment == NULL);
436 g_key_file_free (keyfile);
440 /* check key and group listing */
441 static void
442 test_listing (void)
444 GKeyFile *keyfile;
445 gchar **names;
446 gsize len;
447 gchar *start;
448 GError *error = NULL;
450 const gchar *data =
451 "[group1]\n"
452 "key1=value1\n"
453 "key2=value2\n"
454 "[group2]\n"
455 "key3=value3\n"
456 "key4=value4\n";
458 keyfile = load_data (data, 0);
460 names = g_key_file_get_groups (keyfile, &len);
461 g_assert (names != NULL);
463 check_length ("groups", g_strv_length (names), len, 2);
464 check_name ("group name", names[0], "group1", 0);
465 check_name ("group name", names[1], "group2", 1);
467 g_strfreev (names);
469 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
470 check_no_error (&error);
472 check_length ("keys", g_strv_length (names), len, 2);
473 check_name ("key", names[0], "key1", 0);
474 check_name ("key", names[1], "key2", 1);
476 g_strfreev (names);
478 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
479 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
481 g_strfreev (names);
483 g_assert (g_key_file_has_group (keyfile, "group1"));
484 g_assert (g_key_file_has_group (keyfile, "group2"));
485 g_assert (!g_key_file_has_group (keyfile, "group10"));
486 g_assert (!g_key_file_has_group (keyfile, "group20"));
488 start = g_key_file_get_start_group (keyfile);
489 g_assert_cmpstr (start, ==, "group1");
490 g_free (start);
492 g_assert (g_key_file_has_key (keyfile, "group1", "key1", &error));
493 check_no_error (&error);
494 g_assert (g_key_file_has_key (keyfile, "group2", "key3", &error));
495 check_no_error (&error);
496 g_assert (!g_key_file_has_key (keyfile, "group2", "no-such-key", NULL));
498 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
499 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
501 g_key_file_free (keyfile);
504 /* check parsing of string values */
505 static void
506 test_string (void)
508 GKeyFile *keyfile;
509 GError *error = NULL;
510 gchar *value;
511 const gchar * const list[3] = {
512 "one",
513 "two;andahalf",
514 "3",
516 const gchar *data =
517 "[valid]\n"
518 "key1=\\s\\n\\t\\r\\\\\n"
519 "key2=\"quoted\"\n"
520 "key3='quoted'\n"
521 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
522 "key5= leading space\n"
523 "key6=trailing space \n"
524 "[invalid]\n"
525 "key1=\\a\\b\\0800xff\n"
526 "key2=blabla\\\n";
528 keyfile = load_data (data, 0);
530 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
531 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
532 check_string_value (keyfile, "valid", "key3", "'quoted'");
533 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
534 check_string_value (keyfile, "valid", "key5", "leading space");
535 check_string_value (keyfile, "valid", "key6", "trailing space ");
537 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
538 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
539 g_free (value);
541 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
542 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
543 g_free (value);
545 g_key_file_set_string (keyfile, "inserted", "key1", "simple");
546 g_key_file_set_string (keyfile, "inserted", "key2", " leading space");
547 g_key_file_set_string (keyfile, "inserted", "key3", "\tleading tab");
548 g_key_file_set_string (keyfile, "inserted", "key4", "new\nline");
549 g_key_file_set_string (keyfile, "inserted", "key5", "carriage\rreturn");
550 g_key_file_set_string (keyfile, "inserted", "key6", "slash\\yay!");
551 g_key_file_set_string_list (keyfile, "inserted", "key7", list, 3);
553 check_string_value (keyfile, "inserted", "key1", "simple");
554 check_string_value (keyfile, "inserted", "key2", " leading space");
555 check_string_value (keyfile, "inserted", "key3", "\tleading tab");
556 check_string_value (keyfile, "inserted", "key4", "new\nline");
557 check_string_value (keyfile, "inserted", "key5", "carriage\rreturn");
558 check_string_value (keyfile, "inserted", "key6", "slash\\yay!");
559 check_string_list_value (keyfile, "inserted", "key7", "one", "two;andahalf", "3", NULL);
561 g_key_file_free (keyfile);
564 /* check parsing of boolean values */
565 static void
566 test_boolean (void)
568 GKeyFile *keyfile;
569 GError *error = NULL;
571 const gchar *data =
572 "[valid]\n"
573 "key1=true\n"
574 "key2=false\n"
575 "key3=1\n"
576 "key4=0\n"
577 "key5= true\n"
578 "key6=true \n"
579 "[invalid]\n"
580 "key1=t\n"
581 "key2=f\n"
582 "key3=yes\n"
583 "key4=no\n";
585 keyfile = load_data (data, 0);
587 check_boolean_value (keyfile, "valid", "key1", TRUE);
588 check_boolean_value (keyfile, "valid", "key2", FALSE);
589 check_boolean_value (keyfile, "valid", "key3", TRUE);
590 check_boolean_value (keyfile, "valid", "key4", FALSE);
591 check_boolean_value (keyfile, "valid", "key5", TRUE);
592 check_boolean_value (keyfile, "valid", "key6", TRUE);
594 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
595 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
597 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
598 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
600 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
601 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
603 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
604 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
606 g_key_file_set_boolean (keyfile, "valid", "key1", FALSE);
607 check_boolean_value (keyfile, "valid", "key1", FALSE);
609 g_key_file_free (keyfile);
612 /* check parsing of integer and double values */
613 static void
614 test_number (void)
616 GKeyFile *keyfile;
617 GError *error = NULL;
618 gdouble dval = 0.0;
620 const gchar *data =
621 "[valid]\n"
622 "key1=0\n"
623 "key2=1\n"
624 "key3=-1\n"
625 "key4=2324431\n"
626 "key5=-2324431\n"
627 "key6=000111\n"
628 "key7= 1\n"
629 "key8=1 \n"
630 "dkey1=000111\n"
631 "dkey2=145.45\n"
632 "dkey3=-3453.7\n"
633 "[invalid]\n"
634 "key1=0xffff\n"
635 "key2=0.5\n"
636 "key3=1e37\n"
637 "key4=ten\n"
638 "key5=\n"
639 "key6=1.0.0\n"
640 "key7=2x2\n"
641 "key8=abc\n";
643 keyfile = load_data (data, 0);
645 check_integer_value (keyfile, "valid", "key1", 0);
646 check_integer_value (keyfile, "valid", "key2", 1);
647 check_integer_value (keyfile, "valid", "key3", -1);
648 check_integer_value (keyfile, "valid", "key4", 2324431);
649 check_integer_value (keyfile, "valid", "key5", -2324431);
650 check_integer_value (keyfile, "valid", "key6", 111);
651 check_integer_value (keyfile, "valid", "key7", 1);
652 check_integer_value (keyfile, "valid", "key8", 1);
653 check_double_value (keyfile, "valid", "dkey1", 111.0);
654 check_double_value (keyfile, "valid", "dkey2", 145.45);
655 check_double_value (keyfile, "valid", "dkey3", -3453.7);
657 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
658 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
660 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
661 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
663 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
664 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
666 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
667 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
669 dval = g_key_file_get_double (keyfile, "invalid", "key5", &error);
670 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
671 g_assert_cmpfloat (dval, ==, 0.0);
673 dval = g_key_file_get_double (keyfile, "invalid", "key6", &error);
674 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
675 g_assert_cmpfloat (dval, ==, 0.0);
677 dval = g_key_file_get_double (keyfile, "invalid", "key7", &error);
678 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
679 g_assert_cmpfloat (dval, ==, 0.0);
681 dval = g_key_file_get_double (keyfile, "invalid", "key8", &error);
682 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
683 g_assert_cmpfloat (dval, ==, 0.0);
685 g_key_file_free (keyfile);
688 /* check handling of translated strings */
689 static void
690 test_locale_string (void)
692 GKeyFile *keyfile;
693 gchar *old_locale;
695 const gchar *data =
696 "[valid]\n"
697 "key1=v1\n"
698 "key1[de]=v1-de\n"
699 "key1[de_DE]=v1-de_DE\n"
700 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
701 "key1[fr]=v1-fr\n"
702 "key1[en] =v1-en\n"
703 "key1[sr@Latn]=v1-sr\n";
705 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
707 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
708 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
709 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
710 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
711 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
712 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
713 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
714 check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
716 g_key_file_free (keyfile);
718 /* now test that translations are thrown away */
720 old_locale = g_strdup (setlocale (LC_ALL, NULL));
721 g_setenv ("LANGUAGE", "de", TRUE);
722 setlocale (LC_ALL, "");
724 keyfile = load_data (data, 0);
726 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
727 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
728 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
729 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
730 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
731 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
732 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
734 g_key_file_free (keyfile);
736 setlocale (LC_ALL, old_locale);
737 g_free (old_locale);
740 static void
741 test_lists (void)
743 GKeyFile *keyfile;
745 const gchar *data =
746 "[valid]\n"
747 "key1=v1;v2\n"
748 "key2=v1;v2;\n"
749 "key3=v1,v2\n"
750 "key4=v1\\;v2\n"
751 "key5=true;false\n"
752 "key6=1;0;-1\n"
753 "key7= 1 ; 0 ; -1 \n"
754 "key8=v1\\,v2\n"
755 "key9=0;1.3456;-76532.456\n";
757 keyfile = load_data (data, 0);
759 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
760 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
761 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
762 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
763 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
764 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
765 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
766 /* maybe these should be valid */
767 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
768 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
770 g_key_file_free (keyfile);
772 /* Now check an alternate separator */
774 keyfile = load_data (data, 0);
775 g_key_file_set_list_separator (keyfile, ',');
777 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
778 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
779 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
781 g_key_file_free (keyfile);
784 static void
785 test_lists_set_get (void)
787 GKeyFile *keyfile;
788 static const char * const strings[] = { "v1", "v2" };
789 static const char * const locale_strings[] = { "v1-l", "v2-l" };
790 static int integers[] = { 1, -1, 2 };
791 static gdouble doubles[] = { 3.14, 2.71 };
793 keyfile = g_key_file_new ();
794 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
795 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
796 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
797 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
799 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
800 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
801 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
802 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
803 g_key_file_free (keyfile);
805 /* and again with a different list separator */
806 keyfile = g_key_file_new ();
807 g_key_file_set_list_separator (keyfile, ',');
808 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
809 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
810 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
811 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
813 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
814 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
815 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
816 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
817 g_key_file_free (keyfile);
820 static void
821 test_group_remove (void)
823 GKeyFile *keyfile;
824 gchar **names;
825 gsize len;
826 GError *error = NULL;
828 const gchar *data =
829 "[group1]\n"
830 "[group2]\n"
831 "key1=bla\n"
832 "key2=bla\n"
833 "[group3]\n"
834 "key1=bla\n"
835 "key2=bla\n";
837 g_test_bug ("165887");
839 keyfile = load_data (data, 0);
841 names = g_key_file_get_groups (keyfile, &len);
842 g_assert (names != NULL);
844 check_length ("groups", g_strv_length (names), len, 3);
845 check_name ("group name", names[0], "group1", 0);
846 check_name ("group name", names[1], "group2", 1);
847 check_name ("group name", names[2], "group3", 2);
849 g_key_file_remove_group (keyfile, "group1", &error);
850 check_no_error (&error);
852 g_strfreev (names);
854 names = g_key_file_get_groups (keyfile, &len);
855 g_assert (names != NULL);
857 check_length ("groups", g_strv_length (names), len, 2);
858 check_name ("group name", names[0], "group2", 0);
859 check_name ("group name", names[1], "group3", 1);
861 g_key_file_remove_group (keyfile, "group2", &error);
862 check_no_error (&error);
864 g_strfreev (names);
866 names = g_key_file_get_groups (keyfile, &len);
867 g_assert (names != NULL);
869 check_length ("groups", g_strv_length (names), len, 1);
870 check_name ("group name", names[0], "group3", 0);
872 g_key_file_remove_group (keyfile, "no such group", &error);
873 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
875 g_strfreev (names);
877 g_key_file_free (keyfile);
880 static void
881 test_key_remove (void)
883 GKeyFile *keyfile;
884 gchar *value;
885 GError *error = NULL;
887 const gchar *data =
888 "[group1]\n"
889 "key1=bla\n"
890 "key2=bla\n";
892 g_test_bug ("165980");
894 keyfile = load_data (data, 0);
896 check_string_value (keyfile, "group1", "key1", "bla");
898 g_key_file_remove_key (keyfile, "group1", "key1", &error);
899 check_no_error (&error);
901 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
902 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
903 g_free (value);
905 g_key_file_remove_key (keyfile, "group1", "key1", &error);
906 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
908 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
909 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
911 g_key_file_free (keyfile);
915 static void
916 test_groups (void)
918 GKeyFile *keyfile;
920 const gchar *data =
921 "[1]\n"
922 "key1=123\n"
923 "[2]\n"
924 "key2=123\n";
926 g_test_bug ("316309");
928 keyfile = load_data (data, 0);
930 check_string_value (keyfile, "1", "key1", "123");
931 check_string_value (keyfile, "2", "key2", "123");
933 g_key_file_free (keyfile);
936 static void
937 test_group_names (void)
939 GKeyFile *keyfile;
940 GError *error = NULL;
941 const gchar *data;
942 gchar *value;
944 /* [ in group name */
945 data = "[a[b]\n"
946 "key1=123\n";
947 keyfile = g_key_file_new ();
948 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
949 g_key_file_free (keyfile);
950 check_error (&error,
951 G_KEY_FILE_ERROR,
952 G_KEY_FILE_ERROR_PARSE);
954 /* ] in group name */
955 data = "[a]b]\n"
956 "key1=123\n";
957 keyfile = g_key_file_new ();
958 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
959 g_key_file_free (keyfile);
960 check_error (&error,
961 G_KEY_FILE_ERROR,
962 G_KEY_FILE_ERROR_PARSE);
964 /* control char in group name */
965 data = "[a\tb]\n"
966 "key1=123\n";
967 keyfile = g_key_file_new ();
968 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
969 g_key_file_free (keyfile);
970 check_error (&error,
971 G_KEY_FILE_ERROR,
972 G_KEY_FILE_ERROR_PARSE);
974 /* empty group name */
975 data = "[]\n"
976 "key1=123\n";
977 keyfile = g_key_file_new ();
978 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
979 g_key_file_free (keyfile);
980 check_error (&error,
981 G_KEY_FILE_ERROR,
982 G_KEY_FILE_ERROR_PARSE);
984 /* Unicode in group name */
985 data = "[\xc2\xbd]\n"
986 "key1=123\n";
987 keyfile = g_key_file_new ();
988 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
989 g_key_file_free (keyfile);
990 check_no_error (&error);
992 keyfile = g_key_file_new ();
993 /*g_key_file_set_string (keyfile, "a[b", "key1", "123");*/
994 value = g_key_file_get_string (keyfile, "a[b", "key1", &error);
995 check_error (&error,
996 G_KEY_FILE_ERROR,
997 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
998 g_assert (value == NULL);
999 g_key_file_free (keyfile);
1001 keyfile = g_key_file_new ();
1002 /*g_key_file_set_string (keyfile, "a]b", "key1", "123");*/
1003 value = g_key_file_get_string (keyfile, "a]b", "key1", &error);
1004 check_error (&error,
1005 G_KEY_FILE_ERROR,
1006 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1007 g_assert (value == NULL);
1008 g_key_file_free (keyfile);
1010 keyfile = g_key_file_new ();
1011 /*g_key_file_set_string (keyfile, "a\tb", "key1", "123");*/
1012 value = g_key_file_get_string (keyfile, "a\tb", "key1", &error);
1013 check_error (&error,
1014 G_KEY_FILE_ERROR,
1015 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1016 g_assert (value == NULL);
1017 g_key_file_free (keyfile);
1019 keyfile = g_key_file_new ();
1020 g_key_file_set_string (keyfile, "\xc2\xbd", "key1", "123");
1021 check_string_value (keyfile, "\xc2\xbd", "key1", "123");
1022 g_key_file_free (keyfile);
1025 static void
1026 test_key_names (void)
1028 GKeyFile *keyfile;
1029 GError *error = NULL;
1030 const gchar *data;
1031 gchar *value;
1033 /* [ in key name */
1034 data = "[a]\n"
1035 "key[=123\n";
1036 keyfile = g_key_file_new ();
1037 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1038 g_key_file_free (keyfile);
1039 check_error (&error,
1040 G_KEY_FILE_ERROR,
1041 G_KEY_FILE_ERROR_PARSE);
1043 /* empty key name */
1044 data = "[a]\n"
1045 " =123\n";
1046 keyfile = g_key_file_new ();
1047 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1048 g_key_file_free (keyfile);
1049 check_error (&error,
1050 G_KEY_FILE_ERROR,
1051 G_KEY_FILE_ERROR_PARSE);
1053 /* empty key name */
1054 data = "[a]\n"
1055 " [de] =123\n";
1056 keyfile = g_key_file_new ();
1057 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1058 g_key_file_free (keyfile);
1059 check_error (&error,
1060 G_KEY_FILE_ERROR,
1061 G_KEY_FILE_ERROR_PARSE);
1063 /* bad locale suffix */
1064 data = "[a]\n"
1065 "foo[@#!&%]=123\n";
1066 keyfile = g_key_file_new ();
1067 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1068 g_key_file_free (keyfile);
1069 check_error (&error,
1070 G_KEY_FILE_ERROR,
1071 G_KEY_FILE_ERROR_PARSE);
1073 /* initial space */
1074 data = "[a]\n"
1075 " foo=123\n";
1076 keyfile = g_key_file_new ();
1077 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1078 check_no_error (&error);
1079 check_string_value (keyfile, "a", "foo", "123");
1080 g_key_file_free (keyfile);
1082 /* final space */
1083 data = "[a]\n"
1084 "foo =123\n";
1085 keyfile = g_key_file_new ();
1086 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1087 check_no_error (&error);
1088 check_string_value (keyfile, "a", "foo", "123");
1089 g_key_file_free (keyfile);
1091 /* inner space */
1092 data = "[a]\n"
1093 "foo bar=123\n";
1094 keyfile = g_key_file_new ();
1095 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1096 check_no_error (&error);
1097 check_string_value (keyfile, "a", "foo bar", "123");
1098 g_key_file_free (keyfile);
1100 /* inner space */
1101 data = "[a]\n"
1102 "foo [de] =123\n";
1103 keyfile = g_key_file_new ();
1104 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1105 check_error (&error,
1106 G_KEY_FILE_ERROR,
1107 G_KEY_FILE_ERROR_PARSE);
1108 g_key_file_free (keyfile);
1110 /* control char in key name */
1111 data = "[a]\n"
1112 "key\tfoo=123\n";
1113 keyfile = g_key_file_new ();
1114 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1115 g_key_file_free (keyfile);
1116 check_no_error (&error);
1118 /* Unicode in key name */
1119 data = "[a]\n"
1120 "\xc2\xbd=123\n";
1121 keyfile = g_key_file_new ();
1122 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1123 g_key_file_free (keyfile);
1124 check_no_error (&error);
1126 keyfile = g_key_file_new ();
1127 g_key_file_set_string (keyfile, "a", "x", "123");
1128 /*g_key_file_set_string (keyfile, "a", "key=", "123");*/
1129 value = g_key_file_get_string (keyfile, "a", "key=", &error);
1130 check_error (&error,
1131 G_KEY_FILE_ERROR,
1132 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1133 g_key_file_free (keyfile);
1135 keyfile = g_key_file_new ();
1136 g_key_file_set_string (keyfile, "a", "x", "123");
1137 /*g_key_file_set_string (keyfile, "a", "key[", "123");*/
1138 value = g_key_file_get_string (keyfile, "a", "key[", &error);
1139 check_error (&error,
1140 G_KEY_FILE_ERROR,
1141 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1142 g_key_file_free (keyfile);
1144 keyfile = g_key_file_new ();
1145 g_key_file_set_string (keyfile, "a", "x", "123");
1146 g_key_file_set_string (keyfile, "a", "key\tfoo", "123");
1147 value = g_key_file_get_string (keyfile, "a", "key\tfoo", &error);
1148 check_no_error (&error);
1149 g_free (value);
1150 g_key_file_free (keyfile);
1152 keyfile = g_key_file_new ();
1153 g_key_file_set_string (keyfile, "a", "x", "123");
1154 /*g_key_file_set_string (keyfile, "a", " key", "123");*/
1155 value = g_key_file_get_string (keyfile, "a", " key", &error);
1156 check_error (&error,
1157 G_KEY_FILE_ERROR,
1158 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1159 g_key_file_free (keyfile);
1161 keyfile = g_key_file_new ();
1162 g_key_file_set_string (keyfile, "a", "x", "123");
1164 /* Unicode key */
1165 g_key_file_set_string (keyfile, "a", "\xc2\xbd", "123");
1166 check_string_value (keyfile, "a", "\xc2\xbd", "123");
1168 /* Keys with / + . (as used by the gnome-vfs mime cache) */
1169 g_key_file_set_string (keyfile, "a", "foo/bar", "/");
1170 check_string_value (keyfile, "a", "foo/bar", "/");
1171 g_key_file_set_string (keyfile, "a", "foo+bar", "+");
1172 check_string_value (keyfile, "a", "foo+bar", "+");
1173 g_key_file_set_string (keyfile, "a", "foo.bar", ".");
1174 check_string_value (keyfile, "a", "foo.bar", ".");
1176 g_key_file_free (keyfile);
1179 static void
1180 test_duplicate_keys (void)
1182 GKeyFile *keyfile;
1183 const gchar *data =
1184 "[1]\n"
1185 "key1=123\n"
1186 "key1=345\n";
1188 keyfile = load_data (data, 0);
1189 check_string_value (keyfile, "1", "key1", "345");
1191 g_key_file_free (keyfile);
1194 static void
1195 test_duplicate_groups (void)
1197 GKeyFile *keyfile;
1198 const gchar *data =
1199 "[Desktop Entry]\n"
1200 "key1=123\n"
1201 "[Desktop Entry]\n"
1202 "key2=123\n";
1204 g_test_bug ("157877");
1206 keyfile = load_data (data, 0);
1207 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1208 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1210 g_key_file_free (keyfile);
1213 static void
1214 test_duplicate_groups2 (void)
1216 GKeyFile *keyfile;
1217 const gchar *data =
1218 "[A]\n"
1219 "foo=bar\n"
1220 "[B]\n"
1221 "foo=baz\n"
1222 "[A]\n"
1223 "foo=bang\n";
1225 g_test_bug ("385910");
1227 keyfile = load_data (data, 0);
1228 check_string_value (keyfile, "A", "foo", "bang");
1229 check_string_value (keyfile, "B", "foo", "baz");
1231 g_key_file_free (keyfile);
1234 static void
1235 test_reload_idempotency (void)
1237 static const gchar *original_data=""
1238 "# Top comment\n"
1239 "\n"
1240 "# First comment\n"
1241 "[first]\n"
1242 "key=value\n"
1243 "# A random comment in the first group\n"
1244 "anotherkey=anothervalue\n"
1245 "# Second comment - one line\n"
1246 "[second]\n"
1247 "# Third comment - two lines\n"
1248 "# Third comment - two lines\n"
1249 "[third]\n"
1250 "blank_line=1\n"
1251 "\n"
1252 "blank_lines=2\n"
1253 "\n\n"
1254 "[fourth]\n"
1255 "[fifth]\n";
1256 GKeyFile *keyfile;
1257 GError *error = NULL;
1258 gchar *data1, *data2;
1259 gsize len1, len2;
1261 g_test_bug ("420686");
1263 /* check that we only insert a single new line between groups */
1264 keyfile = g_key_file_new ();
1265 g_key_file_load_from_data (keyfile,
1266 original_data, strlen(original_data),
1267 G_KEY_FILE_KEEP_COMMENTS,
1268 &error);
1269 check_no_error (&error);
1271 data1 = g_key_file_to_data (keyfile, &len1, &error);
1272 g_assert (data1 != NULL);
1273 g_key_file_free (keyfile);
1275 keyfile = g_key_file_new ();
1276 g_key_file_load_from_data (keyfile,
1277 data1, len1,
1278 G_KEY_FILE_KEEP_COMMENTS,
1279 &error);
1280 check_no_error (&error);
1282 data2 = g_key_file_to_data (keyfile, &len2, &error);
1283 g_assert (data2 != NULL);
1284 g_key_file_free (keyfile);
1286 g_assert_cmpstr (data1, ==, data2);
1288 g_free (data2);
1289 g_free (data1);
1292 static const char int64_data[] =
1293 "[bees]\n"
1294 "a=1\n"
1295 "b=2\n"
1296 "c=123456789123456789\n"
1297 "d=-123456789123456789\n";
1299 static void
1300 test_int64 (void)
1302 GKeyFile *file;
1303 gboolean ok;
1304 guint64 c;
1305 gint64 d;
1306 gchar *value;
1308 g_test_bug ("614864");
1310 file = g_key_file_new ();
1312 ok = g_key_file_load_from_data (file, int64_data, strlen (int64_data),
1313 0, NULL);
1314 g_assert (ok);
1316 c = g_key_file_get_uint64 (file, "bees", "c", NULL);
1317 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1319 d = g_key_file_get_int64 (file, "bees", "d", NULL);
1320 g_assert (d == G_GINT64_CONSTANT (-123456789123456789));
1322 g_key_file_set_uint64 (file, "bees", "c",
1323 G_GUINT64_CONSTANT (987654321987654321));
1324 value = g_key_file_get_value (file, "bees", "c", NULL);
1325 g_assert_cmpstr (value, ==, "987654321987654321");
1326 g_free (value);
1328 g_key_file_set_int64 (file, "bees", "d",
1329 G_GINT64_CONSTANT (-987654321987654321));
1330 value = g_key_file_get_value (file, "bees", "d", NULL);
1331 g_assert_cmpstr (value, ==, "-987654321987654321");
1332 g_free (value);
1334 g_key_file_free (file);
1337 static void
1338 test_load (void)
1340 GKeyFile *file;
1341 GError *error;
1342 gboolean bools[2] = { TRUE, FALSE };
1343 gboolean loaded;
1345 file = g_key_file_new ();
1346 error = NULL;
1347 #ifdef G_OS_UNIX
1348 /* Uses the value of $XDG_DATA_HOME we set in main() */
1349 loaded = g_key_file_load_from_data_dirs (file, "keyfiletest.ini", NULL, 0, &error);
1350 #else
1351 loaded = g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), 0, &error);
1352 #endif
1353 g_assert_no_error (error);
1354 g_assert (loaded);
1356 g_key_file_set_locale_string (file, "test", "key4", "de", "Vierter Schlüssel");
1357 g_key_file_set_boolean_list (file, "test", "key5", bools, 2);
1358 g_key_file_set_integer (file, "test", "key6", 22);
1359 g_key_file_set_double (file, "test", "key7", 2.5);
1360 g_key_file_set_comment (file, "test", "key7", "some float", NULL);
1361 g_key_file_set_comment (file, "test", NULL, "the test group", NULL);
1362 g_key_file_set_comment (file, NULL, NULL, "top comment", NULL);
1364 g_key_file_free (file);
1366 file = g_key_file_new ();
1367 error = NULL;
1368 g_assert (!g_key_file_load_from_data_dirs (file, "keyfile-test.ini", NULL, 0, &error));
1369 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND);
1370 g_error_free (error);
1371 g_key_file_free (file);
1374 static void
1375 test_save (void)
1377 GKeyFile *kf;
1378 GKeyFile *kf2;
1379 static const char data[] =
1380 "[bees]\n"
1381 "a=1\n"
1382 "b=2\n"
1383 "c=123456789123456789\n"
1384 "d=-123456789123456789\n";
1385 gboolean ok;
1386 gchar *file;
1387 guint64 c;
1388 GError *error = NULL;
1389 int fd;
1391 kf = g_key_file_new ();
1392 ok = g_key_file_load_from_data (kf, data, strlen (data), 0, NULL);
1393 g_assert (ok);
1395 file = g_strdup ("key_file_XXXXXX");
1396 fd = g_mkstemp (file);
1397 g_assert (fd != -1);
1398 ok = g_close (fd, &error);
1399 g_assert (ok);
1400 g_assert_no_error (error);
1401 ok = g_key_file_save_to_file (kf, file, &error);
1402 g_assert (ok);
1403 g_assert_no_error (error);
1405 kf2 = g_key_file_new ();
1406 ok = g_key_file_load_from_file (kf2, file, 0, &error);
1407 g_assert (ok);
1408 g_assert_no_error (error);
1410 c = g_key_file_get_uint64 (kf2, "bees", "c", NULL);
1411 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1413 remove (file);
1414 g_free (file);
1415 g_key_file_free (kf);
1416 g_key_file_free (kf2);
1419 static void
1420 test_load_fail (void)
1422 GKeyFile *file;
1423 GError *error;
1425 file = g_key_file_new ();
1426 error = NULL;
1427 g_assert (!g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfile.c", NULL), 0, &error));
1428 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
1429 g_clear_error (&error);
1430 g_assert (!g_key_file_load_from_file (file, "/nosuchfile", 0, &error));
1431 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1432 g_clear_error (&error);
1434 g_key_file_free (file);
1437 static void
1438 test_non_utf8 (void)
1440 GKeyFile *file;
1441 static const char data[] =
1442 "[group]\n"
1443 "a=\230\230\230\n"
1444 "b=a;b;\230\230\230;\n"
1445 "c=a\\\n";
1446 gboolean ok;
1447 GError *error;
1448 gchar *s;
1449 gchar **l;
1451 file = g_key_file_new ();
1453 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1454 g_assert (ok);
1456 error = NULL;
1457 s = g_key_file_get_string (file, "group", "a", &error);
1458 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1459 g_assert (s == NULL);
1461 g_clear_error (&error);
1462 l = g_key_file_get_string_list (file, "group", "b", NULL, &error);
1463 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1464 g_assert (l == NULL);
1466 g_clear_error (&error);
1467 l = g_key_file_get_string_list (file, "group", "c", NULL, &error);
1468 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
1469 g_assert (l == NULL);
1471 g_clear_error (&error);
1473 g_key_file_free (file);
1476 static void
1477 test_page_boundary (void)
1479 GKeyFile *file;
1480 GError *error;
1481 gint i;
1483 #define GROUP "main_section"
1484 #define KEY_PREFIX "fill_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw_"
1485 #define FIRST_KEY 10
1486 #define LAST_KEY 99
1487 #define VALUE 92
1489 g_test_bug ("640695");
1491 file = g_key_file_new ();
1493 error = NULL;
1494 g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "pages.ini", NULL), G_KEY_FILE_NONE, &error);
1495 g_assert_no_error (error);
1497 for (i = FIRST_KEY; i <= LAST_KEY; i++)
1499 gchar *key;
1500 gint val;
1502 key = g_strdup_printf (KEY_PREFIX "%d", i);
1503 val = g_key_file_get_integer (file, GROUP, key, &error);
1504 g_free (key);
1505 g_assert_no_error (error);
1506 g_assert_cmpint (val, ==, VALUE);
1509 g_key_file_free (file);
1512 static void
1513 test_ref (void)
1515 GKeyFile *file;
1516 static const char data[] =
1517 "[group]\n"
1518 "a=1\n";
1519 gboolean ok;
1521 file = g_key_file_new ();
1523 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1524 g_assert (ok);
1525 g_assert (g_key_file_has_key (file, "group", "a", NULL));
1526 g_key_file_ref (file);
1527 g_key_file_free (file);
1528 g_key_file_unref (file);
1531 /* https://bugzilla.gnome.org/show_bug.cgi?id=634232 */
1532 static void
1533 test_replace_value (void)
1535 GKeyFile *keyfile;
1537 keyfile = g_key_file_new();
1538 g_key_file_set_value(keyfile, "grupo1", "chave1", "1234567890");
1539 g_key_file_set_value(keyfile, "grupo1", "chave1", "123123423423423432432423423");
1540 g_key_file_remove_group(keyfile, "grupo1", NULL);
1541 g_free (g_key_file_to_data (keyfile, NULL, NULL));
1542 g_key_file_unref (keyfile);
1545 static void
1546 test_list_separator (void)
1548 GKeyFile *keyfile;
1549 GError *error = NULL;
1551 const gchar *data =
1552 "[test]\n"
1553 "key1=v1,v2\n";
1555 keyfile = g_key_file_new ();
1556 g_key_file_set_list_separator (keyfile, ',');
1557 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1559 check_string_list_value (keyfile, "test", "key1", "v1", "v2", NULL);
1560 g_key_file_unref (keyfile);
1563 static void
1564 test_empty_string (void)
1566 GError *error = NULL;
1567 GKeyFile *kf;
1569 kf = g_key_file_new ();
1571 g_key_file_load_from_data (kf, "", 0, 0, &error);
1572 g_assert_no_error (error);
1574 g_key_file_load_from_data (kf, "", -1, 0, &error);
1575 g_assert_no_error (error);
1577 /* NULL is a fine pointer to use if length is zero */
1578 g_key_file_load_from_data (kf, NULL, 0, 0, &error);
1579 g_assert_no_error (error);
1581 /* should not attempt to access non-NULL pointer if length is zero */
1582 g_key_file_load_from_data (kf, GINT_TO_POINTER (1), 0, 0, &error);
1583 g_assert_no_error (error);
1585 g_key_file_unref (kf);
1588 static void
1589 test_limbo (void)
1591 GKeyFile *file;
1592 static const char data[] =
1593 "a=b\n"
1594 "[group]\n"
1595 "b=c\n";
1596 gboolean ok;
1597 GError *error;
1599 file = g_key_file_new ();
1601 error = NULL;
1602 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1603 g_assert (!ok);
1604 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1605 g_clear_error (&error);
1606 g_key_file_free (file);
1609 static void
1610 test_utf8 (void)
1612 GKeyFile *file;
1613 static const char data[] =
1614 "[group]\n"
1615 "Encoding=non-UTF-8\n";
1616 gboolean ok;
1617 GError *error;
1619 file = g_key_file_new ();
1621 error = NULL;
1622 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1623 g_assert (!ok);
1624 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1625 g_clear_error (&error);
1626 g_key_file_free (file);
1629 static void
1630 test_roundtrip (void)
1632 GKeyFile *kf;
1633 const gchar orig[] =
1634 "[Group1]\n"
1635 "key1=value1\n"
1636 "\n"
1637 "[Group2]\n"
1638 "key1=value1\n";
1639 gsize len;
1640 gchar *data;
1642 kf = load_data (orig, G_KEY_FILE_KEEP_COMMENTS);
1643 g_key_file_set_integer (kf, "Group1", "key2", 0);
1644 g_key_file_remove_key (kf, "Group1", "key2", NULL);
1646 data = g_key_file_to_data (kf, &len, NULL);
1647 g_assert_cmpstr (data, ==, orig);
1649 g_free (data);
1650 g_key_file_free (kf);
1653 static void
1654 test_bytes (void)
1656 const gchar data[] =
1657 "[Group1]\n"
1658 "key1=value1\n"
1659 "\n"
1660 "[Group2]\n"
1661 "key2=value2\n";
1663 GKeyFile *kf = g_key_file_new ();
1664 GBytes *bytes = g_bytes_new (data, strlen (data));
1665 GError *error = NULL;
1667 gchar **names;
1668 gsize len;
1670 g_key_file_load_from_bytes (kf, bytes, 0, &error);
1672 g_assert_no_error (error);
1674 names = g_key_file_get_groups (kf, &len);
1675 g_assert_nonnull (names);
1677 check_length ("groups", g_strv_length (names), len, 2);
1678 check_name ("group name", names[0], "Group1", 0);
1679 check_name ("group name", names[1], "Group2", 1);
1681 check_string_value (kf, "Group1", "key1", "value1");
1682 check_string_value (kf, "Group2", "key2", "value2");
1684 g_strfreev (names);
1685 g_bytes_unref (bytes);
1686 g_key_file_free (kf);
1690 main (int argc, char *argv[])
1692 g_test_init (&argc, &argv, NULL);
1694 #ifdef G_OS_UNIX
1695 g_setenv ("XDG_DATA_HOME", g_test_get_dir (G_TEST_DIST), TRUE);
1696 #endif
1698 g_test_bug_base ("http://bugzilla.gnome.org/");
1700 g_test_add_func ("/keyfile/line-ends", test_line_ends);
1701 g_test_add_func ("/keyfile/whitespace", test_whitespace);
1702 g_test_add_func ("/keyfile/comments", test_comments);
1703 g_test_add_func ("/keyfile/listing", test_listing);
1704 g_test_add_func ("/keyfile/string", test_string);
1705 g_test_add_func ("/keyfile/boolean", test_boolean);
1706 g_test_add_func ("/keyfile/number", test_number);
1707 g_test_add_func ("/keyfile/locale-string", test_locale_string);
1708 g_test_add_func ("/keyfile/lists", test_lists);
1709 g_test_add_func ("/keyfile/lists-set-get", test_lists_set_get);
1710 g_test_add_func ("/keyfile/group-remove", test_group_remove);
1711 g_test_add_func ("/keyfile/key-remove", test_key_remove);
1712 g_test_add_func ("/keyfile/groups", test_groups);
1713 g_test_add_func ("/keyfile/duplicate-keys", test_duplicate_keys);
1714 g_test_add_func ("/keyfile/duplicate-groups", test_duplicate_groups);
1715 g_test_add_func ("/keyfile/duplicate-groups2", test_duplicate_groups2);
1716 g_test_add_func ("/keyfile/group-names", test_group_names);
1717 g_test_add_func ("/keyfile/key-names", test_key_names);
1718 g_test_add_func ("/keyfile/reload", test_reload_idempotency);
1719 g_test_add_func ("/keyfile/int64", test_int64);
1720 g_test_add_func ("/keyfile/load", test_load);
1721 g_test_add_func ("/keyfile/save", test_save);
1722 g_test_add_func ("/keyfile/load-fail", test_load_fail);
1723 g_test_add_func ("/keyfile/non-utf8", test_non_utf8);
1724 g_test_add_func ("/keyfile/page-boundary", test_page_boundary);
1725 g_test_add_func ("/keyfile/ref", test_ref);
1726 g_test_add_func ("/keyfile/replace-value", test_replace_value);
1727 g_test_add_func ("/keyfile/list-separator", test_list_separator);
1728 g_test_add_func ("/keyfile/empty-string", test_empty_string);
1729 g_test_add_func ("/keyfile/limbo", test_limbo);
1730 g_test_add_func ("/keyfile/utf8", test_utf8);
1731 g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
1732 g_test_add_func ("/keyfile/bytes", test_bytes);
1734 return g_test_run ();