Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / glib / tests / keyfile.c
blob16f3b788b1e6761b6d28c027dab06a5ec8aeb138
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_locale_value (GKeyFile *keyfile,
72 const gchar *group,
73 const gchar *key,
74 const gchar *locale,
75 const gchar *expected)
77 gchar *value;
79 value = g_key_file_get_locale_for_key (keyfile, group, key, locale);
80 g_assert_cmpstr (value, ==, expected);
81 g_free (value);
84 static void
85 check_string_list_value (GKeyFile *keyfile,
86 const gchar *group,
87 const gchar *key,
88 ...)
90 gint i;
91 gchar *v, **value;
92 va_list args;
93 gsize len;
94 GError *error = NULL;
96 value = g_key_file_get_string_list (keyfile, group, key, &len, &error);
97 check_no_error (&error);
98 g_assert (value != NULL);
100 va_start (args, key);
101 i = 0;
102 v = va_arg (args, gchar*);
103 while (v)
105 g_assert (value[i] != NULL);
106 g_assert_cmpstr (v, ==, value[i]);
107 i++;
108 v = va_arg (args, gchar*);
111 va_end (args);
113 g_strfreev (value);
116 static void
117 check_locale_string_list_value (GKeyFile *keyfile,
118 const gchar *group,
119 const gchar *key,
120 const gchar *locale,
121 ...)
123 gint i;
124 gchar *v, **value;
125 va_list args;
126 gsize len;
127 GError *error = NULL;
129 value = g_key_file_get_locale_string_list (keyfile, group, key, locale, &len, &error);
130 check_no_error (&error);
131 g_assert (value != NULL);
133 va_start (args, locale);
134 i = 0;
135 v = va_arg (args, gchar*);
136 while (v)
138 g_assert (value[i] != NULL);
139 g_assert_cmpstr (v, ==, value[i]);
140 i++;
141 v = va_arg (args, gchar*);
144 va_end (args);
146 g_strfreev (value);
149 static void
150 check_integer_list_value (GKeyFile *keyfile,
151 const gchar *group,
152 const gchar *key,
153 ...)
155 gint i;
156 gint v, *value;
157 va_list args;
158 gsize len;
159 GError *error = NULL;
161 value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
162 check_no_error (&error);
163 g_assert (value != NULL);
165 va_start (args, key);
166 i = 0;
167 v = va_arg (args, gint);
168 while (v != -100)
170 g_assert_cmpint (i, <, len);
171 g_assert_cmpint (value[i], ==, v);
172 i++;
173 v = va_arg (args, gint);
176 va_end (args);
178 g_free (value);
181 static void
182 check_double_list_value (GKeyFile *keyfile,
183 const gchar *group,
184 const gchar *key,
185 ...)
187 gint i;
188 gdouble v, *value;
189 va_list args;
190 gsize len;
191 GError *error = NULL;
193 value = g_key_file_get_double_list (keyfile, group, key, &len, &error);
194 check_no_error (&error);
195 g_assert (value != NULL);
197 va_start (args, key);
198 i = 0;
199 v = va_arg (args, gdouble);
200 while (v != -100)
202 g_assert_cmpint (i, <, len);
203 g_assert_cmpfloat (value[i], ==, v);
204 i++;
205 v = va_arg (args, gdouble);
208 va_end (args);
210 g_free (value);
213 static void
214 check_boolean_list_value (GKeyFile *keyfile,
215 const gchar *group,
216 const gchar *key,
217 ...)
219 gint i;
220 gboolean v, *value;
221 va_list args;
222 gsize len;
223 GError *error = NULL;
225 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
226 check_no_error (&error);
227 g_assert (value != NULL);
229 va_start (args, key);
230 i = 0;
231 v = va_arg (args, gboolean);
232 while (v != -100)
234 g_assert_cmpint (i, <, len);
235 g_assert_cmpint (value[i], ==, v);
236 i++;
237 v = va_arg (args, gboolean);
240 va_end (args);
242 g_free (value);
245 static void
246 check_boolean_value (GKeyFile *keyfile,
247 const gchar *group,
248 const gchar *key,
249 gboolean expected)
251 GError *error = NULL;
252 gboolean value;
254 value = g_key_file_get_boolean (keyfile, group, key, &error);
255 check_no_error (&error);
256 g_assert_cmpint (value, ==, expected);
259 static void
260 check_integer_value (GKeyFile *keyfile,
261 const gchar *group,
262 const gchar *key,
263 gint expected)
265 GError *error = NULL;
266 gint value;
268 value = g_key_file_get_integer (keyfile, group, key, &error);
269 check_no_error (&error);
270 g_assert_cmpint (value, ==, expected);
273 static void
274 check_double_value (GKeyFile *keyfile,
275 const gchar *group,
276 const gchar *key,
277 gdouble expected)
279 GError *error = NULL;
280 gdouble value;
282 value = g_key_file_get_double (keyfile, group, key, &error);
283 check_no_error (&error);
284 g_assert_cmpfloat (value, ==, expected);
287 static void
288 check_name (const gchar *what,
289 const gchar *value,
290 const gchar *expected,
291 gint position)
293 g_assert_cmpstr (value, ==, expected);
296 static void
297 check_length (const gchar *what,
298 gint n_items,
299 gint length,
300 gint expected)
302 g_assert_cmpint (n_items, ==, length);
303 g_assert_cmpint (n_items, ==, expected);
307 /* check that both \n and \r\n are accepted as line ends,
308 * and that stray \r are passed through
310 static void
311 test_line_ends (void)
313 GKeyFile *keyfile;
315 const gchar *data =
316 "[group1]\n"
317 "key1=value1\n"
318 "key2=value2\r\n"
319 "[group2]\r\n"
320 "key3=value3\r\r\n"
321 "key4=value4\n";
323 keyfile = load_data (data, 0);
325 check_string_value (keyfile, "group1", "key1", "value1");
326 check_string_value (keyfile, "group1", "key2", "value2");
327 check_string_value (keyfile, "group2", "key3", "value3\r");
328 check_string_value (keyfile, "group2", "key4", "value4");
330 g_key_file_free (keyfile);
333 /* check handling of whitespace
335 static void
336 test_whitespace (void)
338 GKeyFile *keyfile;
340 const gchar *data =
341 "[group1]\n"
342 "key1 = value1\n"
343 "key2\t=\tvalue2\n"
344 " [ group2 ] \n"
345 "key3 = value3 \n"
346 "key4 = value \t4\n"
347 " key5 = value5\n";
349 keyfile = load_data (data, 0);
351 check_string_value (keyfile, "group1", "key1", "value1");
352 check_string_value (keyfile, "group1", "key2", "value2");
353 check_string_value (keyfile, " group2 ", "key3", "value3 ");
354 check_string_value (keyfile, " group2 ", "key4", "value \t4");
355 check_string_value (keyfile, " group2 ", "key5", "value5");
357 g_key_file_free (keyfile);
360 /* check handling of comments
362 static void
363 test_comments (void)
365 GKeyFile *keyfile;
366 gchar **names;
367 gsize len;
368 GError *error = NULL;
369 gchar *comment;
371 const gchar *data =
372 "# top comment\n"
373 "# top comment, continued\n"
374 "[group1]\n"
375 "key1 = value1\n"
376 "# key comment\n"
377 "# key comment, continued\n"
378 "key2 = value2\n"
379 "# line end check\r\n"
380 "key3 = value3\n"
381 "key4 = value4\n"
382 "# group comment\n"
383 "# group comment, continued\n"
384 "[group2]\n";
386 const gchar *top_comment= " top comment\n top comment, continued\n";
387 const gchar *group_comment= " group comment\n group comment, continued\n";
388 const gchar *key_comment= " key comment\n key comment, continued\n";
390 keyfile = load_data (data, 0);
392 check_string_value (keyfile, "group1", "key1", "value1");
393 check_string_value (keyfile, "group1", "key2", "value2");
394 check_string_value (keyfile, "group1", "key3", "value3");
395 check_string_value (keyfile, "group1", "key4", "value4");
397 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
398 check_no_error (&error);
400 check_length ("keys", g_strv_length (names), len, 4);
401 check_name ("key", names[0], "key1", 0);
402 check_name ("key", names[1], "key2", 1);
403 check_name ("key", names[2], "key3", 2);
404 check_name ("key", names[3], "key4", 3);
406 g_strfreev (names);
408 g_key_file_free (keyfile);
410 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
412 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
413 check_no_error (&error);
415 check_length ("keys", g_strv_length (names), len, 4);
416 check_name ("key", names[0], "key1", 0);
417 check_name ("key", names[1], "key2", 1);
418 check_name ("key", names[2], "key3", 2);
419 check_name ("key", names[3], "key4", 3);
421 g_strfreev (names);
423 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
424 check_no_error (&error);
425 check_name ("top comment", comment, top_comment, 0);
426 g_free (comment);
428 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
429 check_no_error (&error);
430 check_name ("key comment", comment, key_comment, 0);
431 g_free (comment);
433 g_key_file_remove_comment (keyfile, "group1", "key2", &error);
434 check_no_error (&error);
435 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
436 check_no_error (&error);
437 g_assert (comment == NULL);
439 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
440 check_no_error (&error);
441 check_name ("group comment", comment, group_comment, 0);
442 g_free (comment);
444 comment = g_key_file_get_comment (keyfile, "group3", NULL, &error);
445 check_error (&error,
446 G_KEY_FILE_ERROR,
447 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
448 g_assert (comment == NULL);
450 g_key_file_free (keyfile);
454 /* check key and group listing */
455 static void
456 test_listing (void)
458 GKeyFile *keyfile;
459 gchar **names;
460 gsize len;
461 gchar *start;
462 GError *error = NULL;
464 const gchar *data =
465 "[group1]\n"
466 "key1=value1\n"
467 "key2=value2\n"
468 "[group2]\n"
469 "key3=value3\n"
470 "key4=value4\n";
472 keyfile = load_data (data, 0);
474 names = g_key_file_get_groups (keyfile, &len);
475 g_assert (names != NULL);
477 check_length ("groups", g_strv_length (names), len, 2);
478 check_name ("group name", names[0], "group1", 0);
479 check_name ("group name", names[1], "group2", 1);
481 g_strfreev (names);
483 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
484 check_no_error (&error);
486 check_length ("keys", g_strv_length (names), len, 2);
487 check_name ("key", names[0], "key1", 0);
488 check_name ("key", names[1], "key2", 1);
490 g_strfreev (names);
492 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
493 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
495 g_strfreev (names);
497 g_assert (g_key_file_has_group (keyfile, "group1"));
498 g_assert (g_key_file_has_group (keyfile, "group2"));
499 g_assert (!g_key_file_has_group (keyfile, "group10"));
500 g_assert (!g_key_file_has_group (keyfile, "group20"));
502 start = g_key_file_get_start_group (keyfile);
503 g_assert_cmpstr (start, ==, "group1");
504 g_free (start);
506 g_assert (g_key_file_has_key (keyfile, "group1", "key1", &error));
507 check_no_error (&error);
508 g_assert (g_key_file_has_key (keyfile, "group2", "key3", &error));
509 check_no_error (&error);
510 g_assert (!g_key_file_has_key (keyfile, "group2", "no-such-key", NULL));
512 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
513 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
515 g_key_file_free (keyfile);
518 /* check parsing of string values */
519 static void
520 test_string (void)
522 GKeyFile *keyfile;
523 GError *error = NULL;
524 gchar *value;
525 const gchar * const list[3] = {
526 "one",
527 "two;andahalf",
528 "3",
530 const gchar *data =
531 "[valid]\n"
532 "key1=\\s\\n\\t\\r\\\\\n"
533 "key2=\"quoted\"\n"
534 "key3='quoted'\n"
535 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
536 "key5= leading space\n"
537 "key6=trailing space \n"
538 "[invalid]\n"
539 "key1=\\a\\b\\0800xff\n"
540 "key2=blabla\\\n";
542 keyfile = load_data (data, 0);
544 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
545 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
546 check_string_value (keyfile, "valid", "key3", "'quoted'");
547 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
548 check_string_value (keyfile, "valid", "key5", "leading space");
549 check_string_value (keyfile, "valid", "key6", "trailing space ");
551 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
552 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
553 g_free (value);
555 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
556 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
557 g_free (value);
559 g_key_file_set_string (keyfile, "inserted", "key1", "simple");
560 g_key_file_set_string (keyfile, "inserted", "key2", " leading space");
561 g_key_file_set_string (keyfile, "inserted", "key3", "\tleading tab");
562 g_key_file_set_string (keyfile, "inserted", "key4", "new\nline");
563 g_key_file_set_string (keyfile, "inserted", "key5", "carriage\rreturn");
564 g_key_file_set_string (keyfile, "inserted", "key6", "slash\\yay!");
565 g_key_file_set_string_list (keyfile, "inserted", "key7", list, 3);
567 check_string_value (keyfile, "inserted", "key1", "simple");
568 check_string_value (keyfile, "inserted", "key2", " leading space");
569 check_string_value (keyfile, "inserted", "key3", "\tleading tab");
570 check_string_value (keyfile, "inserted", "key4", "new\nline");
571 check_string_value (keyfile, "inserted", "key5", "carriage\rreturn");
572 check_string_value (keyfile, "inserted", "key6", "slash\\yay!");
573 check_string_list_value (keyfile, "inserted", "key7", "one", "two;andahalf", "3", NULL);
575 g_key_file_free (keyfile);
578 /* check parsing of boolean values */
579 static void
580 test_boolean (void)
582 GKeyFile *keyfile;
583 GError *error = NULL;
585 const gchar *data =
586 "[valid]\n"
587 "key1=true\n"
588 "key2=false\n"
589 "key3=1\n"
590 "key4=0\n"
591 "key5= true\n"
592 "key6=true \n"
593 "[invalid]\n"
594 "key1=t\n"
595 "key2=f\n"
596 "key3=yes\n"
597 "key4=no\n";
599 keyfile = load_data (data, 0);
601 check_boolean_value (keyfile, "valid", "key1", TRUE);
602 check_boolean_value (keyfile, "valid", "key2", FALSE);
603 check_boolean_value (keyfile, "valid", "key3", TRUE);
604 check_boolean_value (keyfile, "valid", "key4", FALSE);
605 check_boolean_value (keyfile, "valid", "key5", TRUE);
606 check_boolean_value (keyfile, "valid", "key6", TRUE);
608 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
609 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
611 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
612 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
614 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
615 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
617 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
618 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
620 g_key_file_set_boolean (keyfile, "valid", "key1", FALSE);
621 check_boolean_value (keyfile, "valid", "key1", FALSE);
623 g_key_file_free (keyfile);
626 /* check parsing of integer and double values */
627 static void
628 test_number (void)
630 GKeyFile *keyfile;
631 GError *error = NULL;
632 gdouble dval = 0.0;
634 const gchar *data =
635 "[valid]\n"
636 "key1=0\n"
637 "key2=1\n"
638 "key3=-1\n"
639 "key4=2324431\n"
640 "key5=-2324431\n"
641 "key6=000111\n"
642 "key7= 1\n"
643 "key8=1 \n"
644 "dkey1=000111\n"
645 "dkey2=145.45\n"
646 "dkey3=-3453.7\n"
647 "[invalid]\n"
648 "key1=0xffff\n"
649 "key2=0.5\n"
650 "key3=1e37\n"
651 "key4=ten\n"
652 "key5=\n"
653 "key6=1.0.0\n"
654 "key7=2x2\n"
655 "key8=abc\n";
657 keyfile = load_data (data, 0);
659 check_integer_value (keyfile, "valid", "key1", 0);
660 check_integer_value (keyfile, "valid", "key2", 1);
661 check_integer_value (keyfile, "valid", "key3", -1);
662 check_integer_value (keyfile, "valid", "key4", 2324431);
663 check_integer_value (keyfile, "valid", "key5", -2324431);
664 check_integer_value (keyfile, "valid", "key6", 111);
665 check_integer_value (keyfile, "valid", "key7", 1);
666 check_integer_value (keyfile, "valid", "key8", 1);
667 check_double_value (keyfile, "valid", "dkey1", 111.0);
668 check_double_value (keyfile, "valid", "dkey2", 145.45);
669 check_double_value (keyfile, "valid", "dkey3", -3453.7);
671 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
672 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
674 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
675 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
677 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
678 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
680 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
681 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
683 dval = g_key_file_get_double (keyfile, "invalid", "key5", &error);
684 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
685 g_assert_cmpfloat (dval, ==, 0.0);
687 dval = g_key_file_get_double (keyfile, "invalid", "key6", &error);
688 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
689 g_assert_cmpfloat (dval, ==, 0.0);
691 dval = g_key_file_get_double (keyfile, "invalid", "key7", &error);
692 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
693 g_assert_cmpfloat (dval, ==, 0.0);
695 dval = g_key_file_get_double (keyfile, "invalid", "key8", &error);
696 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
697 g_assert_cmpfloat (dval, ==, 0.0);
699 g_key_file_free (keyfile);
702 /* check handling of translated strings */
703 static void
704 test_locale_string (void)
706 GKeyFile *keyfile;
707 gchar *old_locale;
709 const gchar *data =
710 "[valid]\n"
711 "key1=v1\n"
712 "key1[de]=v1-de\n"
713 "key1[de_DE]=v1-de_DE\n"
714 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
715 "key1[fr]=v1-fr\n"
716 "key1[en] =v1-en\n"
717 "key1[sr@Latn]=v1-sr\n";
719 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
721 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
722 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
723 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
724 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
725 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
726 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
727 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
728 check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
730 g_key_file_free (keyfile);
732 /* now test that translations are thrown away */
734 old_locale = g_strdup (setlocale (LC_ALL, NULL));
735 g_setenv ("LANGUAGE", "de", TRUE);
736 setlocale (LC_ALL, "");
738 keyfile = load_data (data, 0);
740 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
741 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
742 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
743 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
744 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
745 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
746 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
748 g_key_file_free (keyfile);
750 setlocale (LC_ALL, old_locale);
751 g_free (old_locale);
754 static void
755 test_lists (void)
757 GKeyFile *keyfile;
759 const gchar *data =
760 "[valid]\n"
761 "key1=v1;v2\n"
762 "key2=v1;v2;\n"
763 "key3=v1,v2\n"
764 "key4=v1\\;v2\n"
765 "key5=true;false\n"
766 "key6=1;0;-1\n"
767 "key7= 1 ; 0 ; -1 \n"
768 "key8=v1\\,v2\n"
769 "key9=0;1.3456;-76532.456\n";
771 keyfile = load_data (data, 0);
773 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
774 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
775 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
776 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
777 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
778 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
779 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
780 /* maybe these should be valid */
781 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
782 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
784 g_key_file_free (keyfile);
786 /* Now check an alternate separator */
788 keyfile = load_data (data, 0);
789 g_key_file_set_list_separator (keyfile, ',');
791 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
792 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
793 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
795 g_key_file_free (keyfile);
798 static void
799 test_lists_set_get (void)
801 GKeyFile *keyfile;
802 static const char * const strings[] = { "v1", "v2" };
803 static const char * const locale_strings[] = { "v1-l", "v2-l" };
804 static int integers[] = { 1, -1, 2 };
805 static gdouble doubles[] = { 3.14, 2.71 };
807 keyfile = g_key_file_new ();
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);
819 /* and again with a different list separator */
820 keyfile = g_key_file_new ();
821 g_key_file_set_list_separator (keyfile, ',');
822 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
823 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
824 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
825 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
827 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
828 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
829 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
830 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
831 g_key_file_free (keyfile);
834 static void
835 test_group_remove (void)
837 GKeyFile *keyfile;
838 gchar **names;
839 gsize len;
840 GError *error = NULL;
842 const gchar *data =
843 "[group1]\n"
844 "[group2]\n"
845 "key1=bla\n"
846 "key2=bla\n"
847 "[group3]\n"
848 "key1=bla\n"
849 "key2=bla\n";
851 g_test_bug ("165887");
853 keyfile = load_data (data, 0);
855 names = g_key_file_get_groups (keyfile, &len);
856 g_assert (names != NULL);
858 check_length ("groups", g_strv_length (names), len, 3);
859 check_name ("group name", names[0], "group1", 0);
860 check_name ("group name", names[1], "group2", 1);
861 check_name ("group name", names[2], "group3", 2);
863 g_key_file_remove_group (keyfile, "group1", &error);
864 check_no_error (&error);
866 g_strfreev (names);
868 names = g_key_file_get_groups (keyfile, &len);
869 g_assert (names != NULL);
871 check_length ("groups", g_strv_length (names), len, 2);
872 check_name ("group name", names[0], "group2", 0);
873 check_name ("group name", names[1], "group3", 1);
875 g_key_file_remove_group (keyfile, "group2", &error);
876 check_no_error (&error);
878 g_strfreev (names);
880 names = g_key_file_get_groups (keyfile, &len);
881 g_assert (names != NULL);
883 check_length ("groups", g_strv_length (names), len, 1);
884 check_name ("group name", names[0], "group3", 0);
886 g_key_file_remove_group (keyfile, "no such group", &error);
887 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
889 g_strfreev (names);
891 g_key_file_free (keyfile);
894 static void
895 test_key_remove (void)
897 GKeyFile *keyfile;
898 gchar *value;
899 GError *error = NULL;
901 const gchar *data =
902 "[group1]\n"
903 "key1=bla\n"
904 "key2=bla\n";
906 g_test_bug ("165980");
908 keyfile = load_data (data, 0);
910 check_string_value (keyfile, "group1", "key1", "bla");
912 g_key_file_remove_key (keyfile, "group1", "key1", &error);
913 check_no_error (&error);
915 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
916 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
917 g_free (value);
919 g_key_file_remove_key (keyfile, "group1", "key1", &error);
920 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
922 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
923 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
925 g_key_file_free (keyfile);
929 static void
930 test_groups (void)
932 GKeyFile *keyfile;
934 const gchar *data =
935 "[1]\n"
936 "key1=123\n"
937 "[2]\n"
938 "key2=123\n";
940 g_test_bug ("316309");
942 keyfile = load_data (data, 0);
944 check_string_value (keyfile, "1", "key1", "123");
945 check_string_value (keyfile, "2", "key2", "123");
947 g_key_file_free (keyfile);
950 static void
951 test_group_names (void)
953 GKeyFile *keyfile;
954 GError *error = NULL;
955 const gchar *data;
956 gchar *value;
958 /* [ in group name */
959 data = "[a[b]\n"
960 "key1=123\n";
961 keyfile = g_key_file_new ();
962 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
963 g_key_file_free (keyfile);
964 check_error (&error,
965 G_KEY_FILE_ERROR,
966 G_KEY_FILE_ERROR_PARSE);
968 /* ] in group name */
969 data = "[a]b]\n"
970 "key1=123\n";
971 keyfile = g_key_file_new ();
972 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
973 g_key_file_free (keyfile);
974 check_error (&error,
975 G_KEY_FILE_ERROR,
976 G_KEY_FILE_ERROR_PARSE);
978 /* control char in group name */
979 data = "[a\tb]\n"
980 "key1=123\n";
981 keyfile = g_key_file_new ();
982 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
983 g_key_file_free (keyfile);
984 check_error (&error,
985 G_KEY_FILE_ERROR,
986 G_KEY_FILE_ERROR_PARSE);
988 /* empty group name */
989 data = "[]\n"
990 "key1=123\n";
991 keyfile = g_key_file_new ();
992 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
993 g_key_file_free (keyfile);
994 check_error (&error,
995 G_KEY_FILE_ERROR,
996 G_KEY_FILE_ERROR_PARSE);
998 /* Unicode in group name */
999 data = "[\xc2\xbd]\n"
1000 "key1=123\n";
1001 keyfile = g_key_file_new ();
1002 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1003 g_key_file_free (keyfile);
1004 check_no_error (&error);
1006 keyfile = g_key_file_new ();
1007 /*g_key_file_set_string (keyfile, "a[b", "key1", "123");*/
1008 value = g_key_file_get_string (keyfile, "a[b", "key1", &error);
1009 check_error (&error,
1010 G_KEY_FILE_ERROR,
1011 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1012 g_assert (value == NULL);
1013 g_key_file_free (keyfile);
1015 keyfile = g_key_file_new ();
1016 /*g_key_file_set_string (keyfile, "a]b", "key1", "123");*/
1017 value = g_key_file_get_string (keyfile, "a]b", "key1", &error);
1018 check_error (&error,
1019 G_KEY_FILE_ERROR,
1020 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1021 g_assert (value == NULL);
1022 g_key_file_free (keyfile);
1024 keyfile = g_key_file_new ();
1025 /*g_key_file_set_string (keyfile, "a\tb", "key1", "123");*/
1026 value = g_key_file_get_string (keyfile, "a\tb", "key1", &error);
1027 check_error (&error,
1028 G_KEY_FILE_ERROR,
1029 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1030 g_assert (value == NULL);
1031 g_key_file_free (keyfile);
1033 keyfile = g_key_file_new ();
1034 g_key_file_set_string (keyfile, "\xc2\xbd", "key1", "123");
1035 check_string_value (keyfile, "\xc2\xbd", "key1", "123");
1036 g_key_file_free (keyfile);
1039 static void
1040 test_key_names (void)
1042 GKeyFile *keyfile;
1043 GError *error = NULL;
1044 const gchar *data;
1045 gchar *value;
1047 /* [ in key name */
1048 data = "[a]\n"
1049 "key[=123\n";
1050 keyfile = g_key_file_new ();
1051 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1052 g_key_file_free (keyfile);
1053 check_error (&error,
1054 G_KEY_FILE_ERROR,
1055 G_KEY_FILE_ERROR_PARSE);
1057 /* empty key name */
1058 data = "[a]\n"
1059 " =123\n";
1060 keyfile = g_key_file_new ();
1061 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1062 g_key_file_free (keyfile);
1063 check_error (&error,
1064 G_KEY_FILE_ERROR,
1065 G_KEY_FILE_ERROR_PARSE);
1067 /* empty key name */
1068 data = "[a]\n"
1069 " [de] =123\n";
1070 keyfile = g_key_file_new ();
1071 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1072 g_key_file_free (keyfile);
1073 check_error (&error,
1074 G_KEY_FILE_ERROR,
1075 G_KEY_FILE_ERROR_PARSE);
1077 /* bad locale suffix */
1078 data = "[a]\n"
1079 "foo[@#!&%]=123\n";
1080 keyfile = g_key_file_new ();
1081 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1082 g_key_file_free (keyfile);
1083 check_error (&error,
1084 G_KEY_FILE_ERROR,
1085 G_KEY_FILE_ERROR_PARSE);
1087 /* initial space */
1088 data = "[a]\n"
1089 " foo=123\n";
1090 keyfile = g_key_file_new ();
1091 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1092 check_no_error (&error);
1093 check_string_value (keyfile, "a", "foo", "123");
1094 g_key_file_free (keyfile);
1096 /* final space */
1097 data = "[a]\n"
1098 "foo =123\n";
1099 keyfile = g_key_file_new ();
1100 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1101 check_no_error (&error);
1102 check_string_value (keyfile, "a", "foo", "123");
1103 g_key_file_free (keyfile);
1105 /* inner space */
1106 data = "[a]\n"
1107 "foo bar=123\n";
1108 keyfile = g_key_file_new ();
1109 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1110 check_no_error (&error);
1111 check_string_value (keyfile, "a", "foo bar", "123");
1112 g_key_file_free (keyfile);
1114 /* inner space */
1115 data = "[a]\n"
1116 "foo [de] =123\n";
1117 keyfile = g_key_file_new ();
1118 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1119 check_error (&error,
1120 G_KEY_FILE_ERROR,
1121 G_KEY_FILE_ERROR_PARSE);
1122 g_key_file_free (keyfile);
1124 /* control char in key name */
1125 data = "[a]\n"
1126 "key\tfoo=123\n";
1127 keyfile = g_key_file_new ();
1128 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1129 g_key_file_free (keyfile);
1130 check_no_error (&error);
1132 /* Unicode in key name */
1133 data = "[a]\n"
1134 "\xc2\xbd=123\n";
1135 keyfile = g_key_file_new ();
1136 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1137 g_key_file_free (keyfile);
1138 check_no_error (&error);
1140 keyfile = g_key_file_new ();
1141 g_key_file_set_string (keyfile, "a", "x", "123");
1142 /*g_key_file_set_string (keyfile, "a", "key=", "123");*/
1143 value = g_key_file_get_string (keyfile, "a", "key=", &error);
1144 check_error (&error,
1145 G_KEY_FILE_ERROR,
1146 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1147 g_key_file_free (keyfile);
1149 keyfile = g_key_file_new ();
1150 g_key_file_set_string (keyfile, "a", "x", "123");
1151 /*g_key_file_set_string (keyfile, "a", "key[", "123");*/
1152 value = g_key_file_get_string (keyfile, "a", "key[", &error);
1153 check_error (&error,
1154 G_KEY_FILE_ERROR,
1155 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1156 g_key_file_free (keyfile);
1158 keyfile = g_key_file_new ();
1159 g_key_file_set_string (keyfile, "a", "x", "123");
1160 g_key_file_set_string (keyfile, "a", "key\tfoo", "123");
1161 value = g_key_file_get_string (keyfile, "a", "key\tfoo", &error);
1162 check_no_error (&error);
1163 g_free (value);
1164 g_key_file_free (keyfile);
1166 keyfile = g_key_file_new ();
1167 g_key_file_set_string (keyfile, "a", "x", "123");
1168 /*g_key_file_set_string (keyfile, "a", " key", "123");*/
1169 value = g_key_file_get_string (keyfile, "a", " key", &error);
1170 check_error (&error,
1171 G_KEY_FILE_ERROR,
1172 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1173 g_key_file_free (keyfile);
1175 keyfile = g_key_file_new ();
1176 g_key_file_set_string (keyfile, "a", "x", "123");
1178 /* Unicode key */
1179 g_key_file_set_string (keyfile, "a", "\xc2\xbd", "123");
1180 check_string_value (keyfile, "a", "\xc2\xbd", "123");
1182 /* Keys with / + . (as used by the gnome-vfs mime cache) */
1183 g_key_file_set_string (keyfile, "a", "foo/bar", "/");
1184 check_string_value (keyfile, "a", "foo/bar", "/");
1185 g_key_file_set_string (keyfile, "a", "foo+bar", "+");
1186 check_string_value (keyfile, "a", "foo+bar", "+");
1187 g_key_file_set_string (keyfile, "a", "foo.bar", ".");
1188 check_string_value (keyfile, "a", "foo.bar", ".");
1190 g_key_file_free (keyfile);
1193 static void
1194 test_duplicate_keys (void)
1196 GKeyFile *keyfile;
1197 const gchar *data =
1198 "[1]\n"
1199 "key1=123\n"
1200 "key1=345\n";
1202 keyfile = load_data (data, 0);
1203 check_string_value (keyfile, "1", "key1", "345");
1205 g_key_file_free (keyfile);
1208 static void
1209 test_duplicate_groups (void)
1211 GKeyFile *keyfile;
1212 const gchar *data =
1213 "[Desktop Entry]\n"
1214 "key1=123\n"
1215 "[Desktop Entry]\n"
1216 "key2=123\n";
1218 g_test_bug ("157877");
1220 keyfile = load_data (data, 0);
1221 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1222 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1224 g_key_file_free (keyfile);
1227 static void
1228 test_duplicate_groups2 (void)
1230 GKeyFile *keyfile;
1231 const gchar *data =
1232 "[A]\n"
1233 "foo=bar\n"
1234 "[B]\n"
1235 "foo=baz\n"
1236 "[A]\n"
1237 "foo=bang\n";
1239 g_test_bug ("385910");
1241 keyfile = load_data (data, 0);
1242 check_string_value (keyfile, "A", "foo", "bang");
1243 check_string_value (keyfile, "B", "foo", "baz");
1245 g_key_file_free (keyfile);
1248 static void
1249 test_reload_idempotency (void)
1251 static const gchar *original_data=""
1252 "# Top comment\n"
1253 "\n"
1254 "# First comment\n"
1255 "[first]\n"
1256 "key=value\n"
1257 "# A random comment in the first group\n"
1258 "anotherkey=anothervalue\n"
1259 "# Second comment - one line\n"
1260 "[second]\n"
1261 "# Third comment - two lines\n"
1262 "# Third comment - two lines\n"
1263 "[third]\n"
1264 "blank_line=1\n"
1265 "\n"
1266 "blank_lines=2\n"
1267 "\n\n"
1268 "[fourth]\n"
1269 "[fifth]\n";
1270 GKeyFile *keyfile;
1271 GError *error = NULL;
1272 gchar *data1, *data2;
1273 gsize len1, len2;
1275 g_test_bug ("420686");
1277 /* check that we only insert a single new line between groups */
1278 keyfile = g_key_file_new ();
1279 g_key_file_load_from_data (keyfile,
1280 original_data, strlen(original_data),
1281 G_KEY_FILE_KEEP_COMMENTS,
1282 &error);
1283 check_no_error (&error);
1285 data1 = g_key_file_to_data (keyfile, &len1, &error);
1286 g_assert (data1 != NULL);
1287 g_key_file_free (keyfile);
1289 keyfile = g_key_file_new ();
1290 g_key_file_load_from_data (keyfile,
1291 data1, len1,
1292 G_KEY_FILE_KEEP_COMMENTS,
1293 &error);
1294 check_no_error (&error);
1296 data2 = g_key_file_to_data (keyfile, &len2, &error);
1297 g_assert (data2 != NULL);
1298 g_key_file_free (keyfile);
1300 g_assert_cmpstr (data1, ==, data2);
1302 g_free (data2);
1303 g_free (data1);
1306 static const char int64_data[] =
1307 "[bees]\n"
1308 "a=1\n"
1309 "b=2\n"
1310 "c=123456789123456789\n"
1311 "d=-123456789123456789\n";
1313 static void
1314 test_int64 (void)
1316 GKeyFile *file;
1317 gboolean ok;
1318 guint64 c;
1319 gint64 d;
1320 gchar *value;
1322 g_test_bug ("614864");
1324 file = g_key_file_new ();
1326 ok = g_key_file_load_from_data (file, int64_data, strlen (int64_data),
1327 0, NULL);
1328 g_assert (ok);
1330 c = g_key_file_get_uint64 (file, "bees", "c", NULL);
1331 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1333 d = g_key_file_get_int64 (file, "bees", "d", NULL);
1334 g_assert (d == G_GINT64_CONSTANT (-123456789123456789));
1336 g_key_file_set_uint64 (file, "bees", "c",
1337 G_GUINT64_CONSTANT (987654321987654321));
1338 value = g_key_file_get_value (file, "bees", "c", NULL);
1339 g_assert_cmpstr (value, ==, "987654321987654321");
1340 g_free (value);
1342 g_key_file_set_int64 (file, "bees", "d",
1343 G_GINT64_CONSTANT (-987654321987654321));
1344 value = g_key_file_get_value (file, "bees", "d", NULL);
1345 g_assert_cmpstr (value, ==, "-987654321987654321");
1346 g_free (value);
1348 g_key_file_free (file);
1351 static void
1352 test_load (void)
1354 GKeyFile *file;
1355 GError *error;
1356 gboolean bools[2] = { TRUE, FALSE };
1357 gboolean loaded;
1359 file = g_key_file_new ();
1360 error = NULL;
1361 #ifdef G_OS_UNIX
1362 /* Uses the value of $XDG_DATA_HOME we set in main() */
1363 loaded = g_key_file_load_from_data_dirs (file, "keyfiletest.ini", NULL, 0, &error);
1364 #else
1365 loaded = g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), 0, &error);
1366 #endif
1367 g_assert_no_error (error);
1368 g_assert (loaded);
1370 g_key_file_set_locale_string (file, "test", "key4", "de", "Vierter Schlüssel");
1371 g_key_file_set_boolean_list (file, "test", "key5", bools, 2);
1372 g_key_file_set_integer (file, "test", "key6", 22);
1373 g_key_file_set_double (file, "test", "key7", 2.5);
1374 g_key_file_set_comment (file, "test", "key7", "some float", NULL);
1375 g_key_file_set_comment (file, "test", NULL, "the test group", NULL);
1376 g_key_file_set_comment (file, NULL, NULL, "top comment", NULL);
1378 g_key_file_free (file);
1380 file = g_key_file_new ();
1381 error = NULL;
1382 g_assert (!g_key_file_load_from_data_dirs (file, "keyfile-test.ini", NULL, 0, &error));
1383 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND);
1384 g_error_free (error);
1385 g_key_file_free (file);
1388 static void
1389 test_save (void)
1391 GKeyFile *kf;
1392 GKeyFile *kf2;
1393 static const char data[] =
1394 "[bees]\n"
1395 "a=1\n"
1396 "b=2\n"
1397 "c=123456789123456789\n"
1398 "d=-123456789123456789\n";
1399 gboolean ok;
1400 gchar *file;
1401 guint64 c;
1402 GError *error = NULL;
1403 int fd;
1405 kf = g_key_file_new ();
1406 ok = g_key_file_load_from_data (kf, data, strlen (data), 0, NULL);
1407 g_assert (ok);
1409 file = g_strdup ("key_file_XXXXXX");
1410 fd = g_mkstemp (file);
1411 g_assert (fd != -1);
1412 ok = g_close (fd, &error);
1413 g_assert (ok);
1414 g_assert_no_error (error);
1415 ok = g_key_file_save_to_file (kf, file, &error);
1416 g_assert (ok);
1417 g_assert_no_error (error);
1419 kf2 = g_key_file_new ();
1420 ok = g_key_file_load_from_file (kf2, file, 0, &error);
1421 g_assert (ok);
1422 g_assert_no_error (error);
1424 c = g_key_file_get_uint64 (kf2, "bees", "c", NULL);
1425 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1427 remove (file);
1428 g_free (file);
1429 g_key_file_free (kf);
1430 g_key_file_free (kf2);
1433 static void
1434 test_load_fail (void)
1436 GKeyFile *file;
1437 GError *error;
1439 file = g_key_file_new ();
1440 error = NULL;
1441 g_assert (!g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfile.c", NULL), 0, &error));
1442 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
1443 g_clear_error (&error);
1444 g_assert (!g_key_file_load_from_file (file, "/nosuchfile", 0, &error));
1445 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1446 g_clear_error (&error);
1448 g_key_file_free (file);
1451 static void
1452 test_non_utf8 (void)
1454 GKeyFile *file;
1455 static const char data[] =
1456 "[group]\n"
1457 "a=\230\230\230\n"
1458 "b=a;b;\230\230\230;\n"
1459 "c=a\\\n";
1460 gboolean ok;
1461 GError *error;
1462 gchar *s;
1463 gchar **l;
1465 file = g_key_file_new ();
1467 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1468 g_assert (ok);
1470 error = NULL;
1471 s = g_key_file_get_string (file, "group", "a", &error);
1472 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1473 g_assert (s == NULL);
1475 g_clear_error (&error);
1476 l = g_key_file_get_string_list (file, "group", "b", NULL, &error);
1477 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1478 g_assert (l == NULL);
1480 g_clear_error (&error);
1481 l = g_key_file_get_string_list (file, "group", "c", NULL, &error);
1482 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
1483 g_assert (l == NULL);
1485 g_clear_error (&error);
1487 g_key_file_free (file);
1490 static void
1491 test_page_boundary (void)
1493 GKeyFile *file;
1494 GError *error;
1495 gint i;
1497 #define GROUP "main_section"
1498 #define KEY_PREFIX "fill_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw_"
1499 #define FIRST_KEY 10
1500 #define LAST_KEY 99
1501 #define VALUE 92
1503 g_test_bug ("640695");
1505 file = g_key_file_new ();
1507 error = NULL;
1508 g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "pages.ini", NULL), G_KEY_FILE_NONE, &error);
1509 g_assert_no_error (error);
1511 for (i = FIRST_KEY; i <= LAST_KEY; i++)
1513 gchar *key;
1514 gint val;
1516 key = g_strdup_printf (KEY_PREFIX "%d", i);
1517 val = g_key_file_get_integer (file, GROUP, key, &error);
1518 g_free (key);
1519 g_assert_no_error (error);
1520 g_assert_cmpint (val, ==, VALUE);
1523 g_key_file_free (file);
1526 static void
1527 test_ref (void)
1529 GKeyFile *file;
1530 static const char data[] =
1531 "[group]\n"
1532 "a=1\n";
1533 gboolean ok;
1535 file = g_key_file_new ();
1537 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1538 g_assert (ok);
1539 g_assert (g_key_file_has_key (file, "group", "a", NULL));
1540 g_key_file_ref (file);
1541 g_key_file_free (file);
1542 g_key_file_unref (file);
1545 /* https://bugzilla.gnome.org/show_bug.cgi?id=634232 */
1546 static void
1547 test_replace_value (void)
1549 GKeyFile *keyfile;
1551 keyfile = g_key_file_new();
1552 g_key_file_set_value(keyfile, "grupo1", "chave1", "1234567890");
1553 g_key_file_set_value(keyfile, "grupo1", "chave1", "123123423423423432432423423");
1554 g_key_file_remove_group(keyfile, "grupo1", NULL);
1555 g_free (g_key_file_to_data (keyfile, NULL, NULL));
1556 g_key_file_unref (keyfile);
1559 static void
1560 test_list_separator (void)
1562 GKeyFile *keyfile;
1563 GError *error = NULL;
1565 const gchar *data =
1566 "[test]\n"
1567 "key1=v1,v2\n";
1569 keyfile = g_key_file_new ();
1570 g_key_file_set_list_separator (keyfile, ',');
1571 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1573 check_string_list_value (keyfile, "test", "key1", "v1", "v2", NULL);
1574 g_key_file_unref (keyfile);
1577 static void
1578 test_empty_string (void)
1580 GError *error = NULL;
1581 GKeyFile *kf;
1583 kf = g_key_file_new ();
1585 g_key_file_load_from_data (kf, "", 0, 0, &error);
1586 g_assert_no_error (error);
1588 g_key_file_load_from_data (kf, "", -1, 0, &error);
1589 g_assert_no_error (error);
1591 /* NULL is a fine pointer to use if length is zero */
1592 g_key_file_load_from_data (kf, NULL, 0, 0, &error);
1593 g_assert_no_error (error);
1595 /* should not attempt to access non-NULL pointer if length is zero */
1596 g_key_file_load_from_data (kf, GINT_TO_POINTER (1), 0, 0, &error);
1597 g_assert_no_error (error);
1599 g_key_file_unref (kf);
1602 static void
1603 test_limbo (void)
1605 GKeyFile *file;
1606 static const char data[] =
1607 "a=b\n"
1608 "[group]\n"
1609 "b=c\n";
1610 gboolean ok;
1611 GError *error;
1613 file = g_key_file_new ();
1615 error = NULL;
1616 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1617 g_assert (!ok);
1618 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1619 g_clear_error (&error);
1620 g_key_file_free (file);
1623 static void
1624 test_utf8 (void)
1626 GKeyFile *file;
1627 static const char data[] =
1628 "[group]\n"
1629 "Encoding=non-UTF-8\n";
1630 gboolean ok;
1631 GError *error;
1633 file = g_key_file_new ();
1635 error = NULL;
1636 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1637 g_assert (!ok);
1638 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1639 g_clear_error (&error);
1640 g_key_file_free (file);
1643 static void
1644 test_roundtrip (void)
1646 GKeyFile *kf;
1647 const gchar orig[] =
1648 "[Group1]\n"
1649 "key1=value1\n"
1650 "\n"
1651 "[Group2]\n"
1652 "key1=value1\n";
1653 gsize len;
1654 gchar *data;
1656 kf = load_data (orig, G_KEY_FILE_KEEP_COMMENTS);
1657 g_key_file_set_integer (kf, "Group1", "key2", 0);
1658 g_key_file_remove_key (kf, "Group1", "key2", NULL);
1660 data = g_key_file_to_data (kf, &len, NULL);
1661 g_assert_cmpstr (data, ==, orig);
1663 g_free (data);
1664 g_key_file_free (kf);
1667 static void
1668 test_bytes (void)
1670 const gchar data[] =
1671 "[Group1]\n"
1672 "key1=value1\n"
1673 "\n"
1674 "[Group2]\n"
1675 "key2=value2\n";
1677 GKeyFile *kf = g_key_file_new ();
1678 GBytes *bytes = g_bytes_new (data, strlen (data));
1679 GError *error = NULL;
1681 gchar **names;
1682 gsize len;
1684 g_key_file_load_from_bytes (kf, bytes, 0, &error);
1686 g_assert_no_error (error);
1688 names = g_key_file_get_groups (kf, &len);
1689 g_assert_nonnull (names);
1691 check_length ("groups", g_strv_length (names), len, 2);
1692 check_name ("group name", names[0], "Group1", 0);
1693 check_name ("group name", names[1], "Group2", 1);
1695 check_string_value (kf, "Group1", "key1", "value1");
1696 check_string_value (kf, "Group2", "key2", "value2");
1698 g_strfreev (names);
1699 g_bytes_unref (bytes);
1700 g_key_file_free (kf);
1703 static void
1704 test_get_locale (void)
1706 GKeyFile *kf;
1708 kf = g_key_file_new ();
1709 g_key_file_load_from_data (kf,
1710 "[Group]\n"
1711 "x[fr_CA]=a\n"
1712 "x[fr]=b\n"
1713 "x=c\n",
1714 -1, G_KEY_FILE_KEEP_TRANSLATIONS,
1715 NULL);
1717 check_locale_string_value (kf, "Group", "x", "fr_CA", "a");
1718 check_string_locale_value (kf, "Group", "x", "fr_CA", "fr_CA");
1720 check_locale_string_value (kf, "Group", "x", "fr_CH", "b");
1721 check_string_locale_value (kf, "Group", "x", "fr_CH", "fr");
1723 check_locale_string_value (kf, "Group", "x", "eo", "c");
1724 check_string_locale_value (kf, "Group", "x", "eo", NULL);
1726 g_key_file_free (kf);
1730 main (int argc, char *argv[])
1732 g_test_init (&argc, &argv, NULL);
1734 #ifdef G_OS_UNIX
1735 g_setenv ("XDG_DATA_HOME", g_test_get_dir (G_TEST_DIST), TRUE);
1736 #endif
1738 g_test_bug_base ("http://bugzilla.gnome.org/");
1740 g_test_add_func ("/keyfile/line-ends", test_line_ends);
1741 g_test_add_func ("/keyfile/whitespace", test_whitespace);
1742 g_test_add_func ("/keyfile/comments", test_comments);
1743 g_test_add_func ("/keyfile/listing", test_listing);
1744 g_test_add_func ("/keyfile/string", test_string);
1745 g_test_add_func ("/keyfile/boolean", test_boolean);
1746 g_test_add_func ("/keyfile/number", test_number);
1747 g_test_add_func ("/keyfile/locale-string", test_locale_string);
1748 g_test_add_func ("/keyfile/lists", test_lists);
1749 g_test_add_func ("/keyfile/lists-set-get", test_lists_set_get);
1750 g_test_add_func ("/keyfile/group-remove", test_group_remove);
1751 g_test_add_func ("/keyfile/key-remove", test_key_remove);
1752 g_test_add_func ("/keyfile/groups", test_groups);
1753 g_test_add_func ("/keyfile/duplicate-keys", test_duplicate_keys);
1754 g_test_add_func ("/keyfile/duplicate-groups", test_duplicate_groups);
1755 g_test_add_func ("/keyfile/duplicate-groups2", test_duplicate_groups2);
1756 g_test_add_func ("/keyfile/group-names", test_group_names);
1757 g_test_add_func ("/keyfile/key-names", test_key_names);
1758 g_test_add_func ("/keyfile/reload", test_reload_idempotency);
1759 g_test_add_func ("/keyfile/int64", test_int64);
1760 g_test_add_func ("/keyfile/load", test_load);
1761 g_test_add_func ("/keyfile/save", test_save);
1762 g_test_add_func ("/keyfile/load-fail", test_load_fail);
1763 g_test_add_func ("/keyfile/non-utf8", test_non_utf8);
1764 g_test_add_func ("/keyfile/page-boundary", test_page_boundary);
1765 g_test_add_func ("/keyfile/ref", test_ref);
1766 g_test_add_func ("/keyfile/replace-value", test_replace_value);
1767 g_test_add_func ("/keyfile/list-separator", test_list_separator);
1768 g_test_add_func ("/keyfile/empty-string", test_empty_string);
1769 g_test_add_func ("/keyfile/limbo", test_limbo);
1770 g_test_add_func ("/keyfile/utf8", test_utf8);
1771 g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
1772 g_test_add_func ("/keyfile/bytes", test_bytes);
1773 g_test_add_func ("/keyfile/get-locale", test_get_locale);
1775 return g_test_run ();