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