1
2 #include <glib.h>
3 #include <glib/gstdio.h>
4 #include <locale.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 static GKeyFile *
load_data(const gchar * data,GKeyFileFlags flags)9 load_data (const gchar *data,
10 GKeyFileFlags flags)
11 {
12 GKeyFile *keyfile;
13 GError *error = NULL;
14
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;
19 }
20
21 static void
check_error(GError ** error,GQuark domain,gint code)22 check_error (GError **error,
23 GQuark domain,
24 gint code)
25 {
26 g_assert_error (*error, domain, code);
27 g_error_free (*error);
28 *error = NULL;
29 }
30
31 static void
check_no_error(GError ** error)32 check_no_error (GError **error)
33 {
34 g_assert_no_error (*error);
35 }
36
37 static void
check_string_value(GKeyFile * keyfile,const gchar * group,const gchar * key,const gchar * expected)38 check_string_value (GKeyFile *keyfile,
39 const gchar *group,
40 const gchar *key,
41 const gchar *expected)
42 {
43 GError *error = NULL;
44 gchar *value;
45
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);
51 }
52
53 static void
check_locale_string_value(GKeyFile * keyfile,const gchar * group,const gchar * key,const gchar * locale,const gchar * expected)54 check_locale_string_value (GKeyFile *keyfile,
55 const gchar *group,
56 const gchar *key,
57 const gchar *locale,
58 const gchar *expected)
59 {
60 GError *error = NULL;
61 gchar *value;
62
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);
68 }
69
70 static void
check_string_locale_value(GKeyFile * keyfile,const gchar * group,const gchar * key,const gchar * locale,const gchar * expected)71 check_string_locale_value (GKeyFile *keyfile,
72 const gchar *group,
73 const gchar *key,
74 const gchar *locale,
75 const gchar *expected)
76 {
77 gchar *value;
78
79 value = g_key_file_get_locale_for_key (keyfile, group, key, locale);
80 g_assert_cmpstr (value, ==, expected);
81 g_free (value);
82 }
83
84 static void
check_string_list_value(GKeyFile * keyfile,const gchar * group,const gchar * key,...)85 check_string_list_value (GKeyFile *keyfile,
86 const gchar *group,
87 const gchar *key,
88 ...)
89 {
90 gint i;
91 gchar *v, **value;
92 va_list args;
93 gsize len;
94 GError *error = NULL;
95
96 value = g_key_file_get_string_list (keyfile, group, key, &len, &error);
97 check_no_error (&error);
98 g_assert (value != NULL);
99
100 va_start (args, key);
101 i = 0;
102 v = va_arg (args, gchar*);
103 while (v)
104 {
105 g_assert (value[i] != NULL);
106 g_assert_cmpstr (v, ==, value[i]);
107 i++;
108 v = va_arg (args, gchar*);
109 }
110
111 va_end (args);
112
113 g_strfreev (value);
114 }
115
116 static void
check_locale_string_list_value(GKeyFile * keyfile,const gchar * group,const gchar * key,const gchar * locale,...)117 check_locale_string_list_value (GKeyFile *keyfile,
118 const gchar *group,
119 const gchar *key,
120 const gchar *locale,
121 ...)
122 {
123 gint i;
124 gchar *v, **value;
125 va_list args;
126 gsize len;
127 GError *error = NULL;
128
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);
132
133 va_start (args, locale);
134 i = 0;
135 v = va_arg (args, gchar*);
136 while (v)
137 {
138 g_assert (value[i] != NULL);
139 g_assert_cmpstr (v, ==, value[i]);
140 i++;
141 v = va_arg (args, gchar*);
142 }
143
144 va_end (args);
145
146 g_strfreev (value);
147 }
148
149 static void
check_integer_list_value(GKeyFile * keyfile,const gchar * group,const gchar * key,...)150 check_integer_list_value (GKeyFile *keyfile,
151 const gchar *group,
152 const gchar *key,
153 ...)
154 {
155 gint i;
156 gint v, *value;
157 va_list args;
158 gsize len;
159 GError *error = NULL;
160
161 value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
162 check_no_error (&error);
163 g_assert (value != NULL);
164
165 va_start (args, key);
166 i = 0;
167 v = va_arg (args, gint);
168 while (v != -100)
169 {
170 g_assert_cmpint (i, <, len);
171 g_assert_cmpint (value[i], ==, v);
172 i++;
173 v = va_arg (args, gint);
174 }
175
176 va_end (args);
177
178 g_free (value);
179 }
180
181 static void
check_double_list_value(GKeyFile * keyfile,const gchar * group,const gchar * key,...)182 check_double_list_value (GKeyFile *keyfile,
183 const gchar *group,
184 const gchar *key,
185 ...)
186 {
187 gint i;
188 gdouble v, *value;
189 va_list args;
190 gsize len;
191 GError *error = NULL;
192
193 value = g_key_file_get_double_list (keyfile, group, key, &len, &error);
194 check_no_error (&error);
195 g_assert (value != NULL);
196
197 va_start (args, key);
198 i = 0;
199 v = va_arg (args, gdouble);
200 while (v != -100)
201 {
202 g_assert_cmpint (i, <, len);
203 g_assert_cmpfloat (value[i], ==, v);
204 i++;
205 v = va_arg (args, gdouble);
206 }
207
208 va_end (args);
209
210 g_free (value);
211 }
212
213 static void
check_boolean_list_value(GKeyFile * keyfile,const gchar * group,const gchar * key,...)214 check_boolean_list_value (GKeyFile *keyfile,
215 const gchar *group,
216 const gchar *key,
217 ...)
218 {
219 gint i;
220 gboolean v, *value;
221 va_list args;
222 gsize len;
223 GError *error = NULL;
224
225 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
226 check_no_error (&error);
227 g_assert (value != NULL);
228
229 va_start (args, key);
230 i = 0;
231 v = va_arg (args, gboolean);
232 while (v != -100)
233 {
234 g_assert_cmpint (i, <, len);
235 g_assert_cmpint (value[i], ==, v);
236 i++;
237 v = va_arg (args, gboolean);
238 }
239
240 va_end (args);
241
242 g_free (value);
243 }
244
245 static void
check_boolean_value(GKeyFile * keyfile,const gchar * group,const gchar * key,gboolean expected)246 check_boolean_value (GKeyFile *keyfile,
247 const gchar *group,
248 const gchar *key,
249 gboolean expected)
250 {
251 GError *error = NULL;
252 gboolean value;
253
254 value = g_key_file_get_boolean (keyfile, group, key, &error);
255 check_no_error (&error);
256 g_assert_cmpint (value, ==, expected);
257 }
258
259 static void
check_integer_value(GKeyFile * keyfile,const gchar * group,const gchar * key,gint expected)260 check_integer_value (GKeyFile *keyfile,
261 const gchar *group,
262 const gchar *key,
263 gint expected)
264 {
265 GError *error = NULL;
266 gint value;
267
268 value = g_key_file_get_integer (keyfile, group, key, &error);
269 check_no_error (&error);
270 g_assert_cmpint (value, ==, expected);
271 }
272
273 static void
check_double_value(GKeyFile * keyfile,const gchar * group,const gchar * key,gdouble expected)274 check_double_value (GKeyFile *keyfile,
275 const gchar *group,
276 const gchar *key,
277 gdouble expected)
278 {
279 GError *error = NULL;
280 gdouble value;
281
282 value = g_key_file_get_double (keyfile, group, key, &error);
283 check_no_error (&error);
284 g_assert_cmpfloat (value, ==, expected);
285 }
286
287 static void
check_name(const gchar * what,const gchar * value,const gchar * expected,gint position)288 check_name (const gchar *what,
289 const gchar *value,
290 const gchar *expected,
291 gint position)
292 {
293 g_assert_cmpstr (value, ==, expected);
294 }
295
296 static void
check_length(const gchar * what,gint n_items,gint length,gint expected)297 check_length (const gchar *what,
298 gint n_items,
299 gint length,
300 gint expected)
301 {
302 g_assert_cmpint (n_items, ==, length);
303 g_assert_cmpint (n_items, ==, expected);
304 }
305
306
307 /* check that both \n and \r\n are accepted as line ends,
308 * and that stray \r are passed through
309 */
310 static void
test_line_ends(void)311 test_line_ends (void)
312 {
313 GKeyFile *keyfile;
314
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";
322
323 keyfile = load_data (data, 0);
324
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");
329
330 g_key_file_free (keyfile);
331 }
332
333 /* check handling of whitespace
334 */
335 static void
test_whitespace(void)336 test_whitespace (void)
337 {
338 GKeyFile *keyfile;
339
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";
348
349 keyfile = load_data (data, 0);
350
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");
356
357 g_key_file_free (keyfile);
358 }
359
360 /* check handling of comments
361 */
362 static void
test_comments(void)363 test_comments (void)
364 {
365 GKeyFile *keyfile;
366 gchar **names;
367 gsize len;
368 GError *error = NULL;
369 gchar *comment;
370
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 "# single line comment\n"
382 "key4 = value4\n"
383 "# group comment\n"
384 "# group comment, continued\n"
385 "[group2]\n";
386
387 const gchar *top_comment = " top comment\n top comment, continued";
388 const gchar *group_comment = " group comment\n group comment, continued";
389 const gchar *key_comment = " key comment\n key comment, continued";
390 const gchar *key4_comment = " single line comment";
391
392 keyfile = load_data (data, 0);
393
394 check_string_value (keyfile, "group1", "key1", "value1");
395 check_string_value (keyfile, "group1", "key2", "value2");
396 check_string_value (keyfile, "group1", "key3", "value3");
397 check_string_value (keyfile, "group1", "key4", "value4");
398
399 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
400 check_no_error (&error);
401
402 check_length ("keys", g_strv_length (names), len, 4);
403 check_name ("key", names[0], "key1", 0);
404 check_name ("key", names[1], "key2", 1);
405 check_name ("key", names[2], "key3", 2);
406 check_name ("key", names[3], "key4", 3);
407
408 g_strfreev (names);
409
410 g_key_file_free (keyfile);
411
412 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
413
414 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
415 check_no_error (&error);
416
417 check_length ("keys", g_strv_length (names), len, 4);
418 check_name ("key", names[0], "key1", 0);
419 check_name ("key", names[1], "key2", 1);
420 check_name ("key", names[2], "key3", 2);
421 check_name ("key", names[3], "key4", 3);
422
423 g_strfreev (names);
424
425 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
426 check_no_error (&error);
427 check_name ("top comment", comment, top_comment, 0);
428 g_free (comment);
429
430 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
431 check_no_error (&error);
432 check_name ("key comment", comment, key_comment, 0);
433 g_free (comment);
434
435 g_key_file_remove_comment (keyfile, "group1", "key2", &error);
436 check_no_error (&error);
437 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
438 check_no_error (&error);
439 g_assert (comment == NULL);
440
441 comment = g_key_file_get_comment (keyfile, "group1", "key4", &error);
442 check_no_error (&error);
443 check_name ("key comment", comment, key4_comment, 0);
444 g_free (comment);
445
446 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
447 check_no_error (&error);
448 check_name ("group comment", comment, group_comment, 0);
449 g_free (comment);
450
451 comment = g_key_file_get_comment (keyfile, "group3", NULL, &error);
452 check_error (&error,
453 G_KEY_FILE_ERROR,
454 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
455 g_assert (comment == NULL);
456
457 g_key_file_free (keyfile);
458 }
459
460
461 /* check key and group listing */
462 static void
test_listing(void)463 test_listing (void)
464 {
465 GKeyFile *keyfile;
466 gchar **names;
467 gsize len;
468 gchar *start;
469 GError *error = NULL;
470
471 const gchar *data =
472 "[group1]\n"
473 "key1=value1\n"
474 "key2=value2\n"
475 "[group2]\n"
476 "key3=value3\n"
477 "key4=value4\n";
478
479 keyfile = load_data (data, 0);
480
481 names = g_key_file_get_groups (keyfile, &len);
482 g_assert (names != NULL);
483
484 check_length ("groups", g_strv_length (names), len, 2);
485 check_name ("group name", names[0], "group1", 0);
486 check_name ("group name", names[1], "group2", 1);
487
488 g_strfreev (names);
489
490 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
491 check_no_error (&error);
492
493 check_length ("keys", g_strv_length (names), len, 2);
494 check_name ("key", names[0], "key1", 0);
495 check_name ("key", names[1], "key2", 1);
496
497 g_strfreev (names);
498
499 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
500 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
501
502 g_strfreev (names);
503
504 g_assert (g_key_file_has_group (keyfile, "group1"));
505 g_assert (g_key_file_has_group (keyfile, "group2"));
506 g_assert (!g_key_file_has_group (keyfile, "group10"));
507 g_assert (!g_key_file_has_group (keyfile, "group20"));
508
509 start = g_key_file_get_start_group (keyfile);
510 g_assert_cmpstr (start, ==, "group1");
511 g_free (start);
512
513 g_assert (g_key_file_has_key (keyfile, "group1", "key1", &error));
514 check_no_error (&error);
515 g_assert (g_key_file_has_key (keyfile, "group2", "key3", &error));
516 check_no_error (&error);
517 g_assert (!g_key_file_has_key (keyfile, "group2", "no-such-key", NULL));
518
519 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
520 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
521
522 g_key_file_free (keyfile);
523 }
524
525 /* check parsing of string values */
526 static void
test_string(void)527 test_string (void)
528 {
529 GKeyFile *keyfile;
530 GError *error = NULL;
531 gchar *value;
532 const gchar * const list[3] = {
533 "one",
534 "two;andahalf",
535 "3",
536 };
537 const gchar *data =
538 "[valid]\n"
539 "key1=\\s\\n\\t\\r\\\\\n"
540 "key2=\"quoted\"\n"
541 "key3='quoted'\n"
542 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
543 "key5= leading space\n"
544 "key6=trailing space \n"
545 "[invalid]\n"
546 "key1=\\a\\b\\0800xff\n"
547 "key2=blabla\\\n";
548
549 keyfile = load_data (data, 0);
550
551 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
552 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
553 check_string_value (keyfile, "valid", "key3", "'quoted'");
554 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
555 check_string_value (keyfile, "valid", "key5", "leading space");
556 check_string_value (keyfile, "valid", "key6", "trailing space ");
557
558 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
559 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
560 g_free (value);
561
562 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
563 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
564 g_free (value);
565
566 g_key_file_set_string (keyfile, "inserted", "key1", "simple");
567 g_key_file_set_string (keyfile, "inserted", "key2", " leading space");
568 g_key_file_set_string (keyfile, "inserted", "key3", "\tleading tab");
569 g_key_file_set_string (keyfile, "inserted", "key4", "new\nline");
570 g_key_file_set_string (keyfile, "inserted", "key5", "carriage\rreturn");
571 g_key_file_set_string (keyfile, "inserted", "key6", "slash\\yay!");
572 g_key_file_set_string_list (keyfile, "inserted", "key7", list, 3);
573
574 check_string_value (keyfile, "inserted", "key1", "simple");
575 check_string_value (keyfile, "inserted", "key2", " leading space");
576 check_string_value (keyfile, "inserted", "key3", "\tleading tab");
577 check_string_value (keyfile, "inserted", "key4", "new\nline");
578 check_string_value (keyfile, "inserted", "key5", "carriage\rreturn");
579 check_string_value (keyfile, "inserted", "key6", "slash\\yay!");
580 check_string_list_value (keyfile, "inserted", "key7", "one", "two;andahalf", "3", NULL);
581
582 g_key_file_free (keyfile);
583 }
584
585 /* check parsing of boolean values */
586 static void
test_boolean(void)587 test_boolean (void)
588 {
589 GKeyFile *keyfile;
590 GError *error = NULL;
591
592 const gchar *data =
593 "[valid]\n"
594 "key1=true\n"
595 "key2=false\n"
596 "key3=1\n"
597 "key4=0\n"
598 "key5= true\n"
599 "key6=true \n"
600 "[invalid]\n"
601 "key1=t\n"
602 "key2=f\n"
603 "key3=yes\n"
604 "key4=no\n";
605
606 keyfile = load_data (data, 0);
607
608 check_boolean_value (keyfile, "valid", "key1", TRUE);
609 check_boolean_value (keyfile, "valid", "key2", FALSE);
610 check_boolean_value (keyfile, "valid", "key3", TRUE);
611 check_boolean_value (keyfile, "valid", "key4", FALSE);
612 check_boolean_value (keyfile, "valid", "key5", TRUE);
613 check_boolean_value (keyfile, "valid", "key6", TRUE);
614
615 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
616 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
617
618 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
619 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
620
621 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
622 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
623
624 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
625 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
626
627 g_key_file_set_boolean (keyfile, "valid", "key1", FALSE);
628 check_boolean_value (keyfile, "valid", "key1", FALSE);
629
630 g_key_file_free (keyfile);
631 }
632
633 /* check parsing of integer and double values */
634 static void
test_number(void)635 test_number (void)
636 {
637 GKeyFile *keyfile;
638 GError *error = NULL;
639 gdouble dval = 0.0;
640
641 const gchar *data =
642 "[valid]\n"
643 "key1=0\n"
644 "key2=1\n"
645 "key3=-1\n"
646 "key4=2324431\n"
647 "key5=-2324431\n"
648 "key6=000111\n"
649 "key7= 1\n"
650 "key8=1 \n"
651 "dkey1=000111\n"
652 "dkey2=145.45\n"
653 "dkey3=-3453.7\n"
654 "[invalid]\n"
655 "key1=0xffff\n"
656 "key2=0.5\n"
657 "key3=1e37\n"
658 "key4=ten\n"
659 "key5=\n"
660 "key6=1.0.0\n"
661 "key7=2x2\n"
662 "key8=abc\n";
663
664 keyfile = load_data (data, 0);
665
666 check_integer_value (keyfile, "valid", "key1", 0);
667 check_integer_value (keyfile, "valid", "key2", 1);
668 check_integer_value (keyfile, "valid", "key3", -1);
669 check_integer_value (keyfile, "valid", "key4", 2324431);
670 check_integer_value (keyfile, "valid", "key5", -2324431);
671 check_integer_value (keyfile, "valid", "key6", 111);
672 check_integer_value (keyfile, "valid", "key7", 1);
673 check_integer_value (keyfile, "valid", "key8", 1);
674 check_double_value (keyfile, "valid", "dkey1", 111.0);
675 check_double_value (keyfile, "valid", "dkey2", 145.45);
676 check_double_value (keyfile, "valid", "dkey3", -3453.7);
677
678 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
679 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
680
681 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
682 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
683
684 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
685 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
686
687 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
688 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
689
690 dval = g_key_file_get_double (keyfile, "invalid", "key5", &error);
691 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
692 g_assert_cmpfloat (dval, ==, 0.0);
693
694 dval = g_key_file_get_double (keyfile, "invalid", "key6", &error);
695 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
696 g_assert_cmpfloat (dval, ==, 0.0);
697
698 dval = g_key_file_get_double (keyfile, "invalid", "key7", &error);
699 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
700 g_assert_cmpfloat (dval, ==, 0.0);
701
702 dval = g_key_file_get_double (keyfile, "invalid", "key8", &error);
703 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
704 g_assert_cmpfloat (dval, ==, 0.0);
705
706 g_key_file_free (keyfile);
707 }
708
709 /* check handling of translated strings */
710 static void
test_locale_string(void)711 test_locale_string (void)
712 {
713 GKeyFile *keyfile;
714 gchar *old_locale;
715
716 const gchar *data =
717 "[valid]\n"
718 "key1=v1\n"
719 "key1[de]=v1-de\n"
720 "key1[de_DE]=v1-de_DE\n"
721 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
722 "key1[fr]=v1-fr\n"
723 "key1[en] =v1-en\n"
724 "key1[sr@Latn]=v1-sr\n";
725
726 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
727
728 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
729 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
730 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
731 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
732 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
733 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
734 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
735 check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
736
737 g_key_file_free (keyfile);
738
739 /* now test that translations are thrown away */
740
741 old_locale = g_strdup (setlocale (LC_ALL, NULL));
742 g_setenv ("LANGUAGE", "de", TRUE);
743 setlocale (LC_ALL, "");
744
745 keyfile = load_data (data, 0);
746
747 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
748 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
749 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
750 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
751 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
752 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
753 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
754
755 g_key_file_free (keyfile);
756
757 setlocale (LC_ALL, old_locale);
758 g_free (old_locale);
759 }
760
761 static void
test_locale_string_multiple_loads(void)762 test_locale_string_multiple_loads (void)
763 {
764 GKeyFile *keyfile = NULL;
765 GError *local_error = NULL;
766 gchar *old_locale = NULL;
767 guint i;
768 const gchar *data =
769 "[valid]\n"
770 "key1=v1\n"
771 "key1[de]=v1-de\n"
772 "key1[de_DE]=v1-de_DE\n"
773 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
774 "key1[fr]=v1-fr\n"
775 "key1[en] =v1-en\n"
776 "key1[sr@Latn]=v1-sr\n";
777
778 g_test_summary ("Check that loading with translations multiple times works");
779 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2361");
780
781 old_locale = g_strdup (setlocale (LC_ALL, NULL));
782 g_setenv ("LANGUAGE", "de", TRUE);
783 setlocale (LC_ALL, "");
784
785 keyfile = g_key_file_new ();
786
787 for (i = 0; i < 3; i++)
788 {
789 g_key_file_load_from_data (keyfile, data, -1, G_KEY_FILE_NONE, &local_error);
790 g_assert_no_error (local_error);
791
792 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
793 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
794 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
795 }
796
797 g_key_file_free (keyfile);
798
799 setlocale (LC_ALL, old_locale);
800 g_free (old_locale);
801 }
802
803 static void
test_lists(void)804 test_lists (void)
805 {
806 GKeyFile *keyfile;
807
808 const gchar *data =
809 "[valid]\n"
810 "key1=v1;v2\n"
811 "key2=v1;v2;\n"
812 "key3=v1,v2\n"
813 "key4=v1\\;v2\n"
814 "key5=true;false\n"
815 "key6=1;0;-1\n"
816 "key7= 1 ; 0 ; -1 \n"
817 "key8=v1\\,v2\n"
818 "key9=0;1.3456;-76532.456\n";
819
820 keyfile = load_data (data, 0);
821
822 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
823 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
824 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
825 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
826 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
827 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
828 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
829 /* maybe these should be valid */
830 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
831 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
832
833 g_key_file_free (keyfile);
834
835 /* Now check an alternate separator */
836
837 keyfile = load_data (data, 0);
838 g_key_file_set_list_separator (keyfile, ',');
839
840 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
841 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
842 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
843
844 g_key_file_free (keyfile);
845 }
846
847 static void
test_lists_set_get(void)848 test_lists_set_get (void)
849 {
850 GKeyFile *keyfile;
851 static const char * const strings[] = { "v1", "v2" };
852 static const char * const locale_strings[] = { "v1-l", "v2-l" };
853 static int integers[] = { 1, -1, 2 };
854 static gdouble doubles[] = { 3.14, 2.71 };
855
856 keyfile = g_key_file_new ();
857 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
858 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
859 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
860 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
861
862 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
863 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
864 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
865 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
866 g_key_file_free (keyfile);
867
868 /* and again with a different list separator */
869 keyfile = g_key_file_new ();
870 g_key_file_set_list_separator (keyfile, ',');
871 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
872 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
873 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
874 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
875
876 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
877 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
878 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
879 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
880 g_key_file_free (keyfile);
881 }
882
883 static void
test_group_remove(void)884 test_group_remove (void)
885 {
886 GKeyFile *keyfile;
887 gchar **names;
888 gsize len;
889 GError *error = NULL;
890
891 const gchar *data =
892 "[group1]\n"
893 "[group2]\n"
894 "key1=bla\n"
895 "key2=bla\n"
896 "[group3]\n"
897 "key1=bla\n"
898 "key2=bla\n";
899
900 g_test_bug ("165887");
901
902 keyfile = load_data (data, 0);
903
904 names = g_key_file_get_groups (keyfile, &len);
905 g_assert (names != NULL);
906
907 check_length ("groups", g_strv_length (names), len, 3);
908 check_name ("group name", names[0], "group1", 0);
909 check_name ("group name", names[1], "group2", 1);
910 check_name ("group name", names[2], "group3", 2);
911
912 g_key_file_remove_group (keyfile, "group1", &error);
913 check_no_error (&error);
914
915 g_strfreev (names);
916
917 names = g_key_file_get_groups (keyfile, &len);
918 g_assert (names != NULL);
919
920 check_length ("groups", g_strv_length (names), len, 2);
921 check_name ("group name", names[0], "group2", 0);
922 check_name ("group name", names[1], "group3", 1);
923
924 g_key_file_remove_group (keyfile, "group2", &error);
925 check_no_error (&error);
926
927 g_strfreev (names);
928
929 names = g_key_file_get_groups (keyfile, &len);
930 g_assert (names != NULL);
931
932 check_length ("groups", g_strv_length (names), len, 1);
933 check_name ("group name", names[0], "group3", 0);
934
935 g_key_file_remove_group (keyfile, "no such group", &error);
936 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
937
938 g_strfreev (names);
939
940 g_key_file_free (keyfile);
941 }
942
943 static void
test_key_remove(void)944 test_key_remove (void)
945 {
946 GKeyFile *keyfile;
947 gchar *value;
948 GError *error = NULL;
949
950 const gchar *data =
951 "[group1]\n"
952 "key1=bla\n"
953 "key2=bla\n";
954
955 g_test_bug ("165980");
956
957 keyfile = load_data (data, 0);
958
959 check_string_value (keyfile, "group1", "key1", "bla");
960
961 g_key_file_remove_key (keyfile, "group1", "key1", &error);
962 check_no_error (&error);
963
964 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
965 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
966 g_free (value);
967
968 g_key_file_remove_key (keyfile, "group1", "key1", &error);
969 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
970
971 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
972 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
973
974 g_key_file_free (keyfile);
975 }
976
977
978 static void
test_groups(void)979 test_groups (void)
980 {
981 GKeyFile *keyfile;
982
983 const gchar *data =
984 "[1]\n"
985 "key1=123\n"
986 "[2]\n"
987 "key2=123\n";
988
989 g_test_bug ("316309");
990
991 keyfile = load_data (data, 0);
992
993 check_string_value (keyfile, "1", "key1", "123");
994 check_string_value (keyfile, "2", "key2", "123");
995
996 g_key_file_free (keyfile);
997 }
998
999 static void
test_group_names(void)1000 test_group_names (void)
1001 {
1002 GKeyFile *keyfile;
1003 GError *error = NULL;
1004 const gchar *data;
1005 gchar *value;
1006
1007 /* [ in group name */
1008 data = "[a[b]\n"
1009 "key1=123\n";
1010 keyfile = g_key_file_new ();
1011 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1012 g_key_file_free (keyfile);
1013 check_error (&error,
1014 G_KEY_FILE_ERROR,
1015 G_KEY_FILE_ERROR_PARSE);
1016
1017 /* ] in group name */
1018 data = "[a]b]\n"
1019 "key1=123\n";
1020 keyfile = g_key_file_new ();
1021 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1022 g_key_file_free (keyfile);
1023 check_error (&error,
1024 G_KEY_FILE_ERROR,
1025 G_KEY_FILE_ERROR_PARSE);
1026
1027 /* control char in group name */
1028 data = "[a\tb]\n"
1029 "key1=123\n";
1030 keyfile = g_key_file_new ();
1031 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1032 g_key_file_free (keyfile);
1033 check_error (&error,
1034 G_KEY_FILE_ERROR,
1035 G_KEY_FILE_ERROR_PARSE);
1036
1037 /* empty group name */
1038 data = "[]\n"
1039 "key1=123\n";
1040 keyfile = g_key_file_new ();
1041 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1042 g_key_file_free (keyfile);
1043 check_error (&error,
1044 G_KEY_FILE_ERROR,
1045 G_KEY_FILE_ERROR_PARSE);
1046
1047 /* Unicode in group name */
1048 data = "[\xc2\xbd]\n"
1049 "key1=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_no_error (&error);
1054
1055 keyfile = g_key_file_new ();
1056 /*g_key_file_set_string (keyfile, "a[b", "key1", "123");*/
1057 value = g_key_file_get_string (keyfile, "a[b", "key1", &error);
1058 check_error (&error,
1059 G_KEY_FILE_ERROR,
1060 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1061 g_assert (value == NULL);
1062 g_key_file_free (keyfile);
1063
1064 keyfile = g_key_file_new ();
1065 /*g_key_file_set_string (keyfile, "a]b", "key1", "123");*/
1066 value = g_key_file_get_string (keyfile, "a]b", "key1", &error);
1067 check_error (&error,
1068 G_KEY_FILE_ERROR,
1069 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1070 g_assert (value == NULL);
1071 g_key_file_free (keyfile);
1072
1073 keyfile = g_key_file_new ();
1074 /*g_key_file_set_string (keyfile, "a\tb", "key1", "123");*/
1075 value = g_key_file_get_string (keyfile, "a\tb", "key1", &error);
1076 check_error (&error,
1077 G_KEY_FILE_ERROR,
1078 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1079 g_assert (value == NULL);
1080 g_key_file_free (keyfile);
1081
1082 keyfile = g_key_file_new ();
1083 g_key_file_set_string (keyfile, "\xc2\xbd", "key1", "123");
1084 check_string_value (keyfile, "\xc2\xbd", "key1", "123");
1085 g_key_file_free (keyfile);
1086 }
1087
1088 static void
test_key_names(void)1089 test_key_names (void)
1090 {
1091 GKeyFile *keyfile;
1092 GError *error = NULL;
1093 const gchar *data;
1094 gchar *value;
1095
1096 /* [ in key name */
1097 data = "[a]\n"
1098 "key[=123\n";
1099 keyfile = g_key_file_new ();
1100 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1101 g_key_file_free (keyfile);
1102 check_error (&error,
1103 G_KEY_FILE_ERROR,
1104 G_KEY_FILE_ERROR_PARSE);
1105
1106 /* empty key name */
1107 data = "[a]\n"
1108 " =123\n";
1109 keyfile = g_key_file_new ();
1110 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1111 g_key_file_free (keyfile);
1112 check_error (&error,
1113 G_KEY_FILE_ERROR,
1114 G_KEY_FILE_ERROR_PARSE);
1115
1116 /* empty key name */
1117 data = "[a]\n"
1118 " [de] =123\n";
1119 keyfile = g_key_file_new ();
1120 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1121 g_key_file_free (keyfile);
1122 check_error (&error,
1123 G_KEY_FILE_ERROR,
1124 G_KEY_FILE_ERROR_PARSE);
1125
1126 /* bad locale suffix */
1127 data = "[a]\n"
1128 "foo[@#!&%]=123\n";
1129 keyfile = g_key_file_new ();
1130 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1131 g_key_file_free (keyfile);
1132 check_error (&error,
1133 G_KEY_FILE_ERROR,
1134 G_KEY_FILE_ERROR_PARSE);
1135
1136 /* initial space */
1137 data = "[a]\n"
1138 " foo=123\n";
1139 keyfile = g_key_file_new ();
1140 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1141 check_no_error (&error);
1142 check_string_value (keyfile, "a", "foo", "123");
1143 g_key_file_free (keyfile);
1144
1145 /* final space */
1146 data = "[a]\n"
1147 "foo =123\n";
1148 keyfile = g_key_file_new ();
1149 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1150 check_no_error (&error);
1151 check_string_value (keyfile, "a", "foo", "123");
1152 g_key_file_free (keyfile);
1153
1154 /* inner space */
1155 data = "[a]\n"
1156 "foo bar=123\n";
1157 keyfile = g_key_file_new ();
1158 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1159 check_no_error (&error);
1160 check_string_value (keyfile, "a", "foo bar", "123");
1161 g_key_file_free (keyfile);
1162
1163 /* inner space */
1164 data = "[a]\n"
1165 "foo [de] =123\n";
1166 keyfile = g_key_file_new ();
1167 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1168 check_error (&error,
1169 G_KEY_FILE_ERROR,
1170 G_KEY_FILE_ERROR_PARSE);
1171 g_key_file_free (keyfile);
1172
1173 /* control char in key name */
1174 data = "[a]\n"
1175 "key\tfoo=123\n";
1176 keyfile = g_key_file_new ();
1177 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1178 g_key_file_free (keyfile);
1179 check_no_error (&error);
1180
1181 /* Unicode in key name */
1182 data = "[a]\n"
1183 "\xc2\xbd=123\n";
1184 keyfile = g_key_file_new ();
1185 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1186 g_key_file_free (keyfile);
1187 check_no_error (&error);
1188
1189 keyfile = g_key_file_new ();
1190 g_key_file_set_string (keyfile, "a", "x", "123");
1191 /*g_key_file_set_string (keyfile, "a", "key=", "123");*/
1192 value = g_key_file_get_string (keyfile, "a", "key=", &error);
1193 check_error (&error,
1194 G_KEY_FILE_ERROR,
1195 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1196 g_assert_null (value);
1197 g_key_file_free (keyfile);
1198
1199 keyfile = g_key_file_new ();
1200 g_key_file_set_string (keyfile, "a", "x", "123");
1201 /*g_key_file_set_string (keyfile, "a", "key[", "123");*/
1202 value = g_key_file_get_string (keyfile, "a", "key[", &error);
1203 check_error (&error,
1204 G_KEY_FILE_ERROR,
1205 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1206 g_assert_null (value);
1207 g_key_file_free (keyfile);
1208
1209 keyfile = g_key_file_new ();
1210 g_key_file_set_string (keyfile, "a", "x", "123");
1211 g_key_file_set_string (keyfile, "a", "key\tfoo", "123");
1212 value = g_key_file_get_string (keyfile, "a", "key\tfoo", &error);
1213 check_no_error (&error);
1214 g_free (value);
1215 g_key_file_free (keyfile);
1216
1217 keyfile = g_key_file_new ();
1218 g_key_file_set_string (keyfile, "a", "x", "123");
1219 /*g_key_file_set_string (keyfile, "a", " key", "123");*/
1220 value = g_key_file_get_string (keyfile, "a", " key", &error);
1221 check_error (&error,
1222 G_KEY_FILE_ERROR,
1223 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1224 g_assert_null (value);
1225 g_key_file_free (keyfile);
1226
1227 keyfile = g_key_file_new ();
1228 g_key_file_set_string (keyfile, "a", "x", "123");
1229
1230 /* Unicode key */
1231 g_key_file_set_string (keyfile, "a", "\xc2\xbd", "123");
1232 check_string_value (keyfile, "a", "\xc2\xbd", "123");
1233
1234 /* Keys with / + . (as used by the gnome-vfs mime cache) */
1235 g_key_file_set_string (keyfile, "a", "foo/bar", "/");
1236 check_string_value (keyfile, "a", "foo/bar", "/");
1237 g_key_file_set_string (keyfile, "a", "foo+bar", "+");
1238 check_string_value (keyfile, "a", "foo+bar", "+");
1239 g_key_file_set_string (keyfile, "a", "foo.bar", ".");
1240 check_string_value (keyfile, "a", "foo.bar", ".");
1241
1242 g_key_file_free (keyfile);
1243 }
1244
1245 static void
test_duplicate_keys(void)1246 test_duplicate_keys (void)
1247 {
1248 GKeyFile *keyfile;
1249 const gchar *data =
1250 "[1]\n"
1251 "key1=123\n"
1252 "key1=345\n";
1253
1254 keyfile = load_data (data, 0);
1255 check_string_value (keyfile, "1", "key1", "345");
1256
1257 g_key_file_free (keyfile);
1258 }
1259
1260 static void
test_duplicate_groups(void)1261 test_duplicate_groups (void)
1262 {
1263 GKeyFile *keyfile;
1264 const gchar *data =
1265 "[Desktop Entry]\n"
1266 "key1=123\n"
1267 "[Desktop Entry]\n"
1268 "key2=123\n";
1269
1270 g_test_bug ("157877");
1271
1272 keyfile = load_data (data, 0);
1273 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1274 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1275
1276 g_key_file_free (keyfile);
1277 }
1278
1279 static void
test_duplicate_groups2(void)1280 test_duplicate_groups2 (void)
1281 {
1282 GKeyFile *keyfile;
1283 const gchar *data =
1284 "[A]\n"
1285 "foo=bar\n"
1286 "[B]\n"
1287 "foo=baz\n"
1288 "[A]\n"
1289 "foo=bang\n";
1290
1291 g_test_bug ("385910");
1292
1293 keyfile = load_data (data, 0);
1294 check_string_value (keyfile, "A", "foo", "bang");
1295 check_string_value (keyfile, "B", "foo", "baz");
1296
1297 g_key_file_free (keyfile);
1298 }
1299
1300 static void
test_reload_idempotency(void)1301 test_reload_idempotency (void)
1302 {
1303 static const gchar *original_data=""
1304 "# Top comment\n"
1305 "\n"
1306 "# First comment\n"
1307 "[first]\n"
1308 "key=value\n"
1309 "# A random comment in the first group\n"
1310 "anotherkey=anothervalue\n"
1311 "# Second comment - one line\n"
1312 "[second]\n"
1313 "# Third comment - two lines\n"
1314 "# Third comment - two lines\n"
1315 "[third]\n"
1316 "blank_line=1\n"
1317 "\n"
1318 "blank_lines=2\n"
1319 "\n\n"
1320 "[fourth]\n"
1321 "[fifth]\n";
1322 GKeyFile *keyfile;
1323 GError *error = NULL;
1324 gchar *data1, *data2;
1325 gsize len1, len2;
1326
1327 g_test_bug ("420686");
1328
1329 /* check that we only insert a single new line between groups */
1330 keyfile = g_key_file_new ();
1331 g_key_file_load_from_data (keyfile,
1332 original_data, strlen(original_data),
1333 G_KEY_FILE_KEEP_COMMENTS,
1334 &error);
1335 check_no_error (&error);
1336
1337 data1 = g_key_file_to_data (keyfile, &len1, &error);
1338 g_assert (data1 != NULL);
1339 g_key_file_free (keyfile);
1340
1341 keyfile = g_key_file_new ();
1342 g_key_file_load_from_data (keyfile,
1343 data1, len1,
1344 G_KEY_FILE_KEEP_COMMENTS,
1345 &error);
1346 check_no_error (&error);
1347
1348 data2 = g_key_file_to_data (keyfile, &len2, &error);
1349 g_assert (data2 != NULL);
1350 g_key_file_free (keyfile);
1351
1352 g_assert_cmpstr (data1, ==, data2);
1353
1354 g_free (data2);
1355 g_free (data1);
1356 }
1357
1358 static const char int64_data[] =
1359 "[bees]\n"
1360 "a=1\n"
1361 "b=2\n"
1362 "c=123456789123456789\n"
1363 "d=-123456789123456789\n";
1364
1365 static void
test_int64(void)1366 test_int64 (void)
1367 {
1368 GKeyFile *file;
1369 gboolean ok;
1370 guint64 c;
1371 gint64 d;
1372 gchar *value;
1373
1374 g_test_bug ("614864");
1375
1376 file = g_key_file_new ();
1377
1378 ok = g_key_file_load_from_data (file, int64_data, strlen (int64_data),
1379 0, NULL);
1380 g_assert (ok);
1381
1382 c = g_key_file_get_uint64 (file, "bees", "c", NULL);
1383 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1384
1385 d = g_key_file_get_int64 (file, "bees", "d", NULL);
1386 g_assert (d == G_GINT64_CONSTANT (-123456789123456789));
1387
1388 g_key_file_set_uint64 (file, "bees", "c",
1389 G_GUINT64_CONSTANT (987654321987654321));
1390 value = g_key_file_get_value (file, "bees", "c", NULL);
1391 g_assert_cmpstr (value, ==, "987654321987654321");
1392 g_free (value);
1393
1394 g_key_file_set_int64 (file, "bees", "d",
1395 G_GINT64_CONSTANT (-987654321987654321));
1396 value = g_key_file_get_value (file, "bees", "d", NULL);
1397 g_assert_cmpstr (value, ==, "-987654321987654321");
1398 g_free (value);
1399
1400 g_key_file_free (file);
1401 }
1402
1403 static void
test_load(void)1404 test_load (void)
1405 {
1406 GKeyFile *file;
1407 GError *error;
1408 gboolean bools[2] = { TRUE, FALSE };
1409 gboolean loaded;
1410
1411 file = g_key_file_new ();
1412 error = NULL;
1413 #ifdef G_OS_UNIX
1414 /* Uses the value of $XDG_DATA_HOME we set in main() */
1415 loaded = g_key_file_load_from_data_dirs (file, "keyfiletest.ini", NULL, 0, &error);
1416 #else
1417 loaded = g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), 0, &error);
1418 #endif
1419 g_assert_no_error (error);
1420 g_assert (loaded);
1421
1422 g_key_file_set_locale_string (file, "test", "key4", "de", "Vierter Schlüssel");
1423 g_key_file_set_boolean_list (file, "test", "key5", bools, 2);
1424 g_key_file_set_integer (file, "test", "key6", 22);
1425 g_key_file_set_double (file, "test", "key7", 2.5);
1426 g_key_file_set_comment (file, "test", "key7", "some float", NULL);
1427 g_key_file_set_comment (file, "test", NULL, "the test group", NULL);
1428 g_key_file_set_comment (file, NULL, NULL, "top comment", NULL);
1429
1430 g_key_file_free (file);
1431
1432 file = g_key_file_new ();
1433 error = NULL;
1434 g_assert (!g_key_file_load_from_data_dirs (file, "keyfile-test.ini", NULL, 0, &error));
1435 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND);
1436 g_error_free (error);
1437 g_key_file_free (file);
1438 }
1439
1440 static void
test_save(void)1441 test_save (void)
1442 {
1443 GKeyFile *kf;
1444 GKeyFile *kf2;
1445 static const char data[] =
1446 "[bees]\n"
1447 "a=1\n"
1448 "b=2\n"
1449 "c=123456789123456789\n"
1450 "d=-123456789123456789\n";
1451 gboolean ok;
1452 gchar *file;
1453 guint64 c;
1454 GError *error = NULL;
1455 int fd;
1456
1457 kf = g_key_file_new ();
1458 ok = g_key_file_load_from_data (kf, data, strlen (data), 0, NULL);
1459 g_assert (ok);
1460
1461 file = g_strdup ("key_file_XXXXXX");
1462 fd = g_mkstemp (file);
1463 g_assert (fd != -1);
1464 ok = g_close (fd, &error);
1465 g_assert (ok);
1466 g_assert_no_error (error);
1467 ok = g_key_file_save_to_file (kf, file, &error);
1468 g_assert (ok);
1469 g_assert_no_error (error);
1470
1471 kf2 = g_key_file_new ();
1472 ok = g_key_file_load_from_file (kf2, file, 0, &error);
1473 g_assert (ok);
1474 g_assert_no_error (error);
1475
1476 c = g_key_file_get_uint64 (kf2, "bees", "c", NULL);
1477 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1478
1479 remove (file);
1480 g_free (file);
1481 g_key_file_free (kf);
1482 g_key_file_free (kf2);
1483 }
1484
1485 static void
test_load_fail(void)1486 test_load_fail (void)
1487 {
1488 GKeyFile *file;
1489 GError *error;
1490
1491 file = g_key_file_new ();
1492 error = NULL;
1493 g_assert (!g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfile.c", NULL), 0, &error));
1494 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
1495 g_clear_error (&error);
1496 g_assert (!g_key_file_load_from_file (file, "/nosuchfile", 0, &error));
1497 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1498 g_clear_error (&error);
1499
1500 g_key_file_free (file);
1501 }
1502
1503 static void
test_non_utf8(void)1504 test_non_utf8 (void)
1505 {
1506 GKeyFile *file;
1507 static const char data[] =
1508 "[group]\n"
1509 "a=\230\230\230\n"
1510 "b=a;b;\230\230\230;\n"
1511 "c=a\\\n";
1512 gboolean ok;
1513 GError *error;
1514 gchar *s;
1515 gchar **l;
1516
1517 file = g_key_file_new ();
1518
1519 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1520 g_assert (ok);
1521
1522 error = NULL;
1523 s = g_key_file_get_string (file, "group", "a", &error);
1524 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1525 g_assert (s == NULL);
1526
1527 g_clear_error (&error);
1528 l = g_key_file_get_string_list (file, "group", "b", NULL, &error);
1529 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1530 g_assert (l == NULL);
1531
1532 g_clear_error (&error);
1533 l = g_key_file_get_string_list (file, "group", "c", NULL, &error);
1534 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
1535 g_assert (l == NULL);
1536
1537 g_clear_error (&error);
1538
1539 g_key_file_free (file);
1540 }
1541
1542 static void
test_page_boundary(void)1543 test_page_boundary (void)
1544 {
1545 GKeyFile *file;
1546 GError *error;
1547 gint i;
1548
1549 #define GROUP "main_section"
1550 #define KEY_PREFIX "fill_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw_"
1551 #define FIRST_KEY 10
1552 #define LAST_KEY 99
1553 #define VALUE 92
1554
1555 g_test_bug ("640695");
1556
1557 file = g_key_file_new ();
1558
1559 error = NULL;
1560 g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "pages.ini", NULL), G_KEY_FILE_NONE, &error);
1561 g_assert_no_error (error);
1562
1563 for (i = FIRST_KEY; i <= LAST_KEY; i++)
1564 {
1565 gchar *key;
1566 gint val;
1567
1568 key = g_strdup_printf (KEY_PREFIX "%d", i);
1569 val = g_key_file_get_integer (file, GROUP, key, &error);
1570 g_free (key);
1571 g_assert_no_error (error);
1572 g_assert_cmpint (val, ==, VALUE);
1573 }
1574
1575 g_key_file_free (file);
1576 }
1577
1578 static void
test_ref(void)1579 test_ref (void)
1580 {
1581 GKeyFile *file;
1582 static const char data[] =
1583 "[group]\n"
1584 "a=1\n";
1585 gboolean ok;
1586
1587 file = g_key_file_new ();
1588
1589 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1590 g_assert (ok);
1591 g_assert (g_key_file_has_key (file, "group", "a", NULL));
1592 g_key_file_ref (file);
1593 g_key_file_free (file);
1594 g_key_file_unref (file);
1595 }
1596
1597 /* https://bugzilla.gnome.org/show_bug.cgi?id=634232 */
1598 static void
test_replace_value(void)1599 test_replace_value (void)
1600 {
1601 GKeyFile *keyfile;
1602
1603 keyfile = g_key_file_new();
1604 g_key_file_set_value(keyfile, "grupo1", "chave1", "1234567890");
1605 g_key_file_set_value(keyfile, "grupo1", "chave1", "123123423423423432432423423");
1606 g_key_file_remove_group(keyfile, "grupo1", NULL);
1607 g_free (g_key_file_to_data (keyfile, NULL, NULL));
1608 g_key_file_unref (keyfile);
1609 }
1610
1611 static void
test_list_separator(void)1612 test_list_separator (void)
1613 {
1614 GKeyFile *keyfile;
1615 GError *error = NULL;
1616
1617 const gchar *data =
1618 "[test]\n"
1619 "key1=v1,v2\n";
1620
1621 keyfile = g_key_file_new ();
1622 g_key_file_set_list_separator (keyfile, ',');
1623 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1624
1625 check_string_list_value (keyfile, "test", "key1", "v1", "v2", NULL);
1626 g_key_file_unref (keyfile);
1627 }
1628
1629 static void
test_empty_string(void)1630 test_empty_string (void)
1631 {
1632 GError *error = NULL;
1633 GKeyFile *kf;
1634
1635 kf = g_key_file_new ();
1636
1637 g_key_file_load_from_data (kf, "", 0, 0, &error);
1638 g_assert_no_error (error);
1639
1640 g_key_file_load_from_data (kf, "", -1, 0, &error);
1641 g_assert_no_error (error);
1642
1643 /* NULL is a fine pointer to use if length is zero */
1644 g_key_file_load_from_data (kf, NULL, 0, 0, &error);
1645 g_assert_no_error (error);
1646
1647 /* should not attempt to access non-NULL pointer if length is zero */
1648 g_key_file_load_from_data (kf, GINT_TO_POINTER (1), 0, 0, &error);
1649 g_assert_no_error (error);
1650
1651 g_key_file_unref (kf);
1652 }
1653
1654 static void
test_limbo(void)1655 test_limbo (void)
1656 {
1657 GKeyFile *file;
1658 static const char data[] =
1659 "a=b\n"
1660 "[group]\n"
1661 "b=c\n";
1662 gboolean ok;
1663 GError *error;
1664
1665 file = g_key_file_new ();
1666
1667 error = NULL;
1668 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1669 g_assert (!ok);
1670 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1671 g_clear_error (&error);
1672 g_key_file_free (file);
1673 }
1674
1675 static void
test_utf8(void)1676 test_utf8 (void)
1677 {
1678 GKeyFile *file;
1679 static const char data[] =
1680 "[group]\n"
1681 "Encoding=non-UTF-8\n";
1682 gboolean ok;
1683 GError *error;
1684
1685 file = g_key_file_new ();
1686
1687 error = NULL;
1688 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1689 g_assert (!ok);
1690 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1691 g_clear_error (&error);
1692 g_key_file_free (file);
1693 }
1694
1695 static void
test_roundtrip(void)1696 test_roundtrip (void)
1697 {
1698 GKeyFile *kf;
1699 const gchar orig[] =
1700 "[Group1]\n"
1701 "key1=value1\n"
1702 "\n"
1703 "[Group2]\n"
1704 "key1=value1\n";
1705 gsize len;
1706 gchar *data;
1707
1708 kf = load_data (orig, G_KEY_FILE_KEEP_COMMENTS);
1709 g_key_file_set_integer (kf, "Group1", "key2", 0);
1710 g_key_file_remove_key (kf, "Group1", "key2", NULL);
1711
1712 data = g_key_file_to_data (kf, &len, NULL);
1713 g_assert_cmpstr (data, ==, orig);
1714
1715 g_free (data);
1716 g_key_file_free (kf);
1717 }
1718
1719 static void
test_bytes(void)1720 test_bytes (void)
1721 {
1722 const gchar data[] =
1723 "[Group1]\n"
1724 "key1=value1\n"
1725 "\n"
1726 "[Group2]\n"
1727 "key2=value2\n";
1728
1729 GKeyFile *kf = g_key_file_new ();
1730 GBytes *bytes = g_bytes_new (data, strlen (data));
1731 GError *error = NULL;
1732
1733 gchar **names;
1734 gsize len;
1735
1736 g_key_file_load_from_bytes (kf, bytes, 0, &error);
1737
1738 g_assert_no_error (error);
1739
1740 names = g_key_file_get_groups (kf, &len);
1741 g_assert_nonnull (names);
1742
1743 check_length ("groups", g_strv_length (names), len, 2);
1744 check_name ("group name", names[0], "Group1", 0);
1745 check_name ("group name", names[1], "Group2", 1);
1746
1747 check_string_value (kf, "Group1", "key1", "value1");
1748 check_string_value (kf, "Group2", "key2", "value2");
1749
1750 g_strfreev (names);
1751 g_bytes_unref (bytes);
1752 g_key_file_free (kf);
1753 }
1754
1755 static void
test_get_locale(void)1756 test_get_locale (void)
1757 {
1758 GKeyFile *kf;
1759
1760 kf = g_key_file_new ();
1761 g_key_file_load_from_data (kf,
1762 "[Group]\n"
1763 "x[fr_CA]=a\n"
1764 "x[fr]=b\n"
1765 "x=c\n",
1766 -1, G_KEY_FILE_KEEP_TRANSLATIONS,
1767 NULL);
1768
1769 check_locale_string_value (kf, "Group", "x", "fr_CA", "a");
1770 check_string_locale_value (kf, "Group", "x", "fr_CA", "fr_CA");
1771
1772 check_locale_string_value (kf, "Group", "x", "fr_CH", "b");
1773 check_string_locale_value (kf, "Group", "x", "fr_CH", "fr");
1774
1775 check_locale_string_value (kf, "Group", "x", "eo", "c");
1776 check_string_locale_value (kf, "Group", "x", "eo", NULL);
1777
1778 g_key_file_free (kf);
1779 }
1780
1781 static void
test_free_when_not_last_ref(void)1782 test_free_when_not_last_ref (void)
1783 {
1784 GKeyFile *kf;
1785 GError *error = NULL;
1786 const gchar *data =
1787 "[Group]\n"
1788 "Key=Value\n";
1789
1790 kf = load_data (data, G_KEY_FILE_NONE);
1791 /* Add a second ref */
1792 g_key_file_ref (kf);
1793
1794 /* Quick coherence check */
1795 g_assert_true (g_key_file_has_group (kf, "Group"));
1796 g_assert_true (g_key_file_has_key (kf, "Group", "Key", &error));
1797 g_assert_no_error (error);
1798
1799 /* Should clear all keys and groups, and remove one ref */
1800 g_key_file_free (kf);
1801
1802 /* kf should still work */
1803 g_assert_false (g_key_file_has_group (kf, "Group"));
1804 g_assert_false (g_key_file_has_key (kf, "Group", "Key", &error));
1805 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1806 g_clear_error (&error);
1807
1808 g_key_file_load_from_data (kf, data, -1, G_KEY_FILE_NONE, &error);
1809 g_assert_no_error (error);
1810
1811 g_assert_true (g_key_file_has_group (kf, "Group"));
1812 g_assert_true (g_key_file_has_key (kf, "Group", "Key", &error));
1813
1814 g_key_file_unref (kf);
1815 }
1816
1817 int
main(int argc,char * argv[])1818 main (int argc, char *argv[])
1819 {
1820 g_test_init (&argc, &argv, NULL);
1821
1822 #ifdef G_OS_UNIX
1823 g_setenv ("XDG_DATA_HOME", g_test_get_dir (G_TEST_DIST), TRUE);
1824 #endif
1825
1826 g_test_bug_base ("http://bugzilla.gnome.org/");
1827
1828 g_test_add_func ("/keyfile/line-ends", test_line_ends);
1829 g_test_add_func ("/keyfile/whitespace", test_whitespace);
1830 g_test_add_func ("/keyfile/comments", test_comments);
1831 g_test_add_func ("/keyfile/listing", test_listing);
1832 g_test_add_func ("/keyfile/string", test_string);
1833 g_test_add_func ("/keyfile/boolean", test_boolean);
1834 g_test_add_func ("/keyfile/number", test_number);
1835 g_test_add_func ("/keyfile/locale-string", test_locale_string);
1836 g_test_add_func ("/keyfile/locale-string/multiple-loads", test_locale_string_multiple_loads);
1837 g_test_add_func ("/keyfile/lists", test_lists);
1838 g_test_add_func ("/keyfile/lists-set-get", test_lists_set_get);
1839 g_test_add_func ("/keyfile/group-remove", test_group_remove);
1840 g_test_add_func ("/keyfile/key-remove", test_key_remove);
1841 g_test_add_func ("/keyfile/groups", test_groups);
1842 g_test_add_func ("/keyfile/duplicate-keys", test_duplicate_keys);
1843 g_test_add_func ("/keyfile/duplicate-groups", test_duplicate_groups);
1844 g_test_add_func ("/keyfile/duplicate-groups2", test_duplicate_groups2);
1845 g_test_add_func ("/keyfile/group-names", test_group_names);
1846 g_test_add_func ("/keyfile/key-names", test_key_names);
1847 g_test_add_func ("/keyfile/reload", test_reload_idempotency);
1848 g_test_add_func ("/keyfile/int64", test_int64);
1849 g_test_add_func ("/keyfile/load", test_load);
1850 g_test_add_func ("/keyfile/save", test_save);
1851 g_test_add_func ("/keyfile/load-fail", test_load_fail);
1852 g_test_add_func ("/keyfile/non-utf8", test_non_utf8);
1853 g_test_add_func ("/keyfile/page-boundary", test_page_boundary);
1854 g_test_add_func ("/keyfile/ref", test_ref);
1855 g_test_add_func ("/keyfile/replace-value", test_replace_value);
1856 g_test_add_func ("/keyfile/list-separator", test_list_separator);
1857 g_test_add_func ("/keyfile/empty-string", test_empty_string);
1858 g_test_add_func ("/keyfile/limbo", test_limbo);
1859 g_test_add_func ("/keyfile/utf8", test_utf8);
1860 g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
1861 g_test_add_func ("/keyfile/bytes", test_bytes);
1862 g_test_add_func ("/keyfile/get-locale", test_get_locale);
1863 g_test_add_func ("/keyfile/free-when-not-last-ref", test_free_when_not_last_ref);
1864
1865 return g_test_run ();
1866 }
1867