LCOV - code coverage report
Current view: top level - glib/glib/tests - keyfile.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 1010 1010 100.0 %
Date: 2024-04-23 05:16:05 Functions: 53 53 100.0 %
Branches: 16 16 100.0 %

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

Generated by: LCOV version 1.14