os/ossrv/glib/tsrc/BC/tests/keyfile-test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/keyfile-test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1077 @@
     1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
     1.5 +#include <glib.h>
     1.6 +#include <locale.h>
     1.7 +#include <string.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <stdio.h>
    1.10 +
    1.11 +#ifdef SYMBIAN
    1.12 +#include "mrt2_glib2_test.h"
    1.13 +#endif /*SYMBIAN*/
    1.14 +
    1.15 +
    1.16 +static GKeyFile *
    1.17 +load_data (const gchar   *data, 
    1.18 +	   GKeyFileFlags  flags)
    1.19 +{
    1.20 +  GKeyFile *keyfile;
    1.21 +  GError *error = NULL;
    1.22 +
    1.23 +  keyfile = g_key_file_new ();
    1.24 +  g_key_file_load_from_data (keyfile, data, -1, flags, &error);
    1.25 +  if (error)
    1.26 +    {
    1.27 +      g_print ("Could not load data: %s\n", error->message);
    1.28 +      
    1.29 +      g_assert(FALSE && "keyfile-test failed");
    1.30 +      
    1.31 +      #ifdef SYMBIAN
    1.32 +  	  testResultXml("keyfile-test");
    1.33 +  	  #endif /* EMULATOR */
    1.34 +  	  
    1.35 +      exit (1);
    1.36 +    }
    1.37 +  
    1.38 +  return keyfile;
    1.39 +}
    1.40 +
    1.41 +static void
    1.42 +check_error (GError **error,
    1.43 +	     GQuark   domain,
    1.44 +	     gint     code)
    1.45 +{
    1.46 +  if (*error == NULL)
    1.47 +    {
    1.48 +      g_print ("Missing an error\n");
    1.49 +            
    1.50 +      g_assert(FALSE && "keyfile-test failed");
    1.51 +      
    1.52 +      #ifdef SYMBIAN
    1.53 +  	  testResultXml("keyfile-test");
    1.54 +  	  #endif /* EMULATOR */
    1.55 +  	  
    1.56 +      exit (1);
    1.57 +    }
    1.58 +  
    1.59 +  if ((*error)->domain != domain)
    1.60 +    {
    1.61 +      g_print ("Wrong error domain: got %s, expected %s\n",
    1.62 +	       g_quark_to_string ((*error)->domain),
    1.63 +	       g_quark_to_string (domain));      
    1.64 +
    1.65 +      g_assert(FALSE && "keyfile-test failed");
    1.66 +      
    1.67 +      #ifdef SYMBIAN
    1.68 +  	  testResultXml("keyfile-test");
    1.69 +  	  #endif /* EMULATOR */ 	  
    1.70 +
    1.71 +      exit (1);
    1.72 +    }
    1.73 +  
    1.74 +  if ((*error)->code != code)
    1.75 +    {
    1.76 +      g_print ("Wrong error code: got %d, expected %d\n",
    1.77 +	       (*error)->code, code);
    1.78 +	        
    1.79 +      g_assert(FALSE && "keyfile-test failed");
    1.80 +      
    1.81 +      #ifdef SYMBIAN
    1.82 +  	  testResultXml("keyfile-test");
    1.83 +  	  #endif /* EMULATOR */
    1.84 +  	  
    1.85 +      exit (1);
    1.86 +    }
    1.87 +
    1.88 +  g_error_free (*error);
    1.89 +  *error = NULL;
    1.90 +}
    1.91 +
    1.92 +static void 
    1.93 +check_no_error (GError **error)
    1.94 +{
    1.95 +  if (*error != NULL)
    1.96 +    {
    1.97 +      g_print ("Unexpected error: (%s, %d) %s\n",
    1.98 +	       g_quark_to_string ((*error)->domain),
    1.99 +	       (*error)->code, (*error)->message);	        
   1.100 +
   1.101 +      g_assert(FALSE && "keyfile-test failed");
   1.102 +      
   1.103 +      #ifdef SYMBIAN
   1.104 +  	  testResultXml("keyfile-test");
   1.105 +  	  #endif /* EMULATOR */
   1.106 +  	  
   1.107 +      exit (1);
   1.108 +    }
   1.109 +}
   1.110 +
   1.111 +static void
   1.112 +check_string_value (GKeyFile    *keyfile,
   1.113 +		    const gchar *group,
   1.114 +		    const gchar *key,
   1.115 +		    const gchar *expected) 
   1.116 +{
   1.117 +  GError *error = NULL;
   1.118 +  gchar *value;
   1.119 +
   1.120 +  value = g_key_file_get_string (keyfile, group, key, &error);
   1.121 +  check_no_error (&error);
   1.122 +  g_assert (value != NULL);
   1.123 +
   1.124 +  if (strcmp (value, expected) != 0)
   1.125 +    {
   1.126 +      g_print ("Group %s key %s: "
   1.127 +	       "expected string value '%s', actual value '%s'\n",
   1.128 +	       group, key, expected, value); 
   1.129 +	  	        
   1.130 +      g_assert(FALSE && "keyfile-test failed");
   1.131 +      
   1.132 +      #ifdef SYMBIAN
   1.133 +  	  testResultXml("keyfile-test");
   1.134 +  	  #endif /* EMULATOR */
   1.135 +  	      
   1.136 +      exit (1);
   1.137 +    }
   1.138 +
   1.139 +  g_free (value);
   1.140 +}
   1.141 +
   1.142 +static void
   1.143 +check_locale_string_value (GKeyFile    *keyfile,
   1.144 +			   const gchar *group,
   1.145 +			   const gchar *key,
   1.146 +			   const gchar *locale,
   1.147 +			   const gchar *expected) 
   1.148 +{
   1.149 +  GError *error = NULL;
   1.150 +  gchar *value;
   1.151 +
   1.152 +  value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
   1.153 +  check_no_error (&error);
   1.154 +  g_assert (value != NULL);
   1.155 +
   1.156 +  if (strcmp (value, expected) != 0)
   1.157 +    {
   1.158 +      g_print ("Group %s key %s locale %s: "
   1.159 +	       "expected string value '%s', actual value '%s'\n",
   1.160 +	       group, key, locale, expected, value);  
   1.161 +	       
   1.162 +	  	        
   1.163 +      g_assert(FALSE && "keyfile-test failed");
   1.164 +      
   1.165 +      #ifdef SYMBIAN
   1.166 +  	  testResultXml("keyfile-test");
   1.167 +  	  #endif /* EMULATOR */
   1.168 +  	     
   1.169 +      exit (1);
   1.170 +    }
   1.171 +
   1.172 +  g_free (value);
   1.173 +}
   1.174 +
   1.175 +static void
   1.176 +check_string_list_value (GKeyFile    *keyfile,
   1.177 +			 const gchar *group,
   1.178 +			 const gchar *key,
   1.179 +			 ...)
   1.180 +{
   1.181 +  gint i;
   1.182 +  gchar *v, **value;
   1.183 +  va_list args;
   1.184 +  gsize len;
   1.185 +  GError *error = NULL;
   1.186 +
   1.187 +  value = g_key_file_get_string_list (keyfile, group, key, &len, &error);
   1.188 +  check_no_error (&error);
   1.189 +  g_assert (value != NULL);
   1.190 +  
   1.191 +  va_start (args, key);
   1.192 +  i = 0;
   1.193 +  v = va_arg (args, gchar*);
   1.194 +  while (v)
   1.195 +    {
   1.196 +      if (value[i] == NULL)
   1.197 +	{
   1.198 +	  g_print ("Group %s key %s: list too short (%d)\n", 
   1.199 +		   group, key, i);      
   1.200 +	  	        
   1.201 +      g_assert(FALSE && "keyfile-test failed");
   1.202 +      
   1.203 +      #ifdef SYMBIAN
   1.204 +  	  testResultXml("keyfile-test");
   1.205 +  	  #endif /* EMULATOR */
   1.206 +  	  
   1.207 +	  exit (1);
   1.208 +	}
   1.209 +      if (strcmp (v, value[i]) != 0)
   1.210 +	{
   1.211 +	  g_print ("Group %s key %s: mismatch at %d, expected %s, got %s\n", 
   1.212 +		   group, key, i, v, value[i]);      
   1.213 +		   
   1.214 +	  	        
   1.215 +      g_assert(FALSE && "keyfile-test failed");
   1.216 +      
   1.217 +      #ifdef SYMBIAN
   1.218 +  	  testResultXml("keyfile-test");
   1.219 +  	  #endif /* EMULATOR */
   1.220 +  	  
   1.221 +	  exit (1);
   1.222 +	}
   1.223 +
   1.224 +      i++;
   1.225 +      v = va_arg (args, gchar*);
   1.226 +    }
   1.227 +
   1.228 +  va_end (args);
   1.229 +  
   1.230 +  g_strfreev (value);
   1.231 +}
   1.232 +
   1.233 +static void
   1.234 +check_integer_list_value (GKeyFile    *keyfile,
   1.235 +			  const gchar *group,
   1.236 +			  const gchar *key,
   1.237 +			  ...)
   1.238 +{
   1.239 +  gint i;
   1.240 +  gint v, *value;
   1.241 +  va_list args;
   1.242 +  gsize len;
   1.243 +  GError *error = NULL;
   1.244 +
   1.245 +  value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
   1.246 +  check_no_error (&error);
   1.247 +  g_assert (value != NULL);
   1.248 +  
   1.249 +  va_start (args, key);
   1.250 +  i = 0;
   1.251 +  v = va_arg (args, gint);
   1.252 +  while (v != -100)
   1.253 +    {
   1.254 +      if (i == len)
   1.255 +	{
   1.256 +	  g_print ("Group %s key %s: list too short (%d)\n", 
   1.257 +		   group, key, i);      
   1.258 +	  	        
   1.259 +      g_assert(FALSE && "keyfile-test failed");
   1.260 +      
   1.261 +      #ifdef SYMBIAN
   1.262 +  	  testResultXml("keyfile-test");
   1.263 +  	  #endif /* EMULATOR */
   1.264 +  	  
   1.265 +	  exit (1);
   1.266 +	}
   1.267 +      if (value[i] != v)
   1.268 +	{
   1.269 +	  g_print ("Group %s key %s: mismatch at %d, expected %d, got %d\n", 
   1.270 +		   group, key, i, v, value[i]);      
   1.271 +	  	        
   1.272 +      g_assert(FALSE && "keyfile-test failed");
   1.273 +      
   1.274 +      #ifdef SYMBIAN
   1.275 +  	  testResultXml("keyfile-test");
   1.276 +  	  #endif /* EMULATOR */
   1.277 +  	  
   1.278 +	  exit (1);
   1.279 +	}
   1.280 +
   1.281 +      i++;
   1.282 +      v = va_arg (args, gint);
   1.283 +    }
   1.284 +
   1.285 +  va_end (args);
   1.286 +  
   1.287 +  g_free (value);
   1.288 +}
   1.289 +
   1.290 +static void
   1.291 +check_boolean_list_value (GKeyFile    *keyfile,
   1.292 +			  const gchar *group,
   1.293 +			  const gchar *key,
   1.294 +			  ...)
   1.295 +{
   1.296 +  gint i;
   1.297 +  gboolean v, *value;
   1.298 +  va_list args;
   1.299 +  gsize len;
   1.300 +  GError *error = NULL;
   1.301 +
   1.302 +  value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
   1.303 +  check_no_error (&error);
   1.304 +  g_assert (value != NULL);
   1.305 +  
   1.306 +  va_start (args, key);
   1.307 +  i = 0;
   1.308 +  v = va_arg (args, gboolean);
   1.309 +  while (v != -100)
   1.310 +    {
   1.311 +      if (i == len)
   1.312 +	{
   1.313 +	  g_print ("Group %s key %s: list too short (%d)\n", 
   1.314 +		   group, key, i);      
   1.315 +	  	        
   1.316 +      g_assert(FALSE && "keyfile-test failed");
   1.317 +      
   1.318 +      #ifdef SYMBIAN
   1.319 +  	  testResultXml("keyfile-test");
   1.320 +  	  #endif /* EMULATOR */
   1.321 +  	  
   1.322 +	  exit (1);
   1.323 +	}
   1.324 +      if (value[i] != v)
   1.325 +	{
   1.326 +	  g_print ("Group %s key %s: mismatch at %d, expected %d, got %d\n", 
   1.327 +		   group, key, i, v, value[i]);      
   1.328 +	  	        
   1.329 +      g_assert(FALSE && "keyfile-test failed");
   1.330 +      
   1.331 +      #ifdef SYMBIAN
   1.332 +  	  testResultXml("keyfile-test");
   1.333 +  	  #endif /* EMULATOR */
   1.334 +  	  
   1.335 +	  exit (1);
   1.336 +	}
   1.337 +
   1.338 +      i++;
   1.339 +      v = va_arg (args, gboolean);
   1.340 +    }
   1.341 +
   1.342 +  va_end (args);
   1.343 +  
   1.344 +  g_free (value);
   1.345 +}
   1.346 +
   1.347 +static void
   1.348 +check_boolean_value (GKeyFile    *keyfile,
   1.349 +		     const gchar *group,
   1.350 +		     const gchar *key,
   1.351 +		     gboolean     expected) 
   1.352 +{
   1.353 +  GError *error = NULL;
   1.354 +  gboolean value;
   1.355 +
   1.356 +  value = g_key_file_get_boolean (keyfile, group, key, &error);
   1.357 +  check_no_error (&error);
   1.358 +
   1.359 +  if (value != expected)
   1.360 +    {
   1.361 +      g_print ("Group %s key %s: "
   1.362 +	       "expected boolean value '%s', actual value '%s'\n",
   1.363 +	       group, key, 
   1.364 +	       expected ? "true" : "false", 
   1.365 +	       value ? "true" : "false");      
   1.366 +	  	        
   1.367 +      g_assert(FALSE && "keyfile-test failed");
   1.368 +      
   1.369 +      #ifdef SYMBIAN
   1.370 +  	  testResultXml("keyfile-test");
   1.371 +  	  #endif /* EMULATOR */
   1.372 +  	  
   1.373 +      exit (1);
   1.374 +    }
   1.375 +}
   1.376 +
   1.377 +static void
   1.378 +check_integer_value (GKeyFile    *keyfile,
   1.379 +		     const gchar *group,
   1.380 +		     const gchar *key,
   1.381 +		     gint         expected) 
   1.382 +{
   1.383 +  GError *error = NULL;
   1.384 +  gint value;
   1.385 +
   1.386 +  value = g_key_file_get_integer (keyfile, group, key, &error);
   1.387 +  check_no_error (&error);
   1.388 +
   1.389 +  if (value != expected)
   1.390 +    {
   1.391 +      g_print ("Group %s key %s: "
   1.392 +	       "expected integer value %d, actual value %d\n",
   1.393 +	       group, key, expected, value);      
   1.394 +	  	        
   1.395 +      g_assert(FALSE && "keyfile-test failed");
   1.396 +      
   1.397 +      #ifdef SYMBIAN
   1.398 +  	  testResultXml("keyfile-test");
   1.399 +  	  #endif /* EMULATOR */
   1.400 +  	  
   1.401 +      exit (1);
   1.402 +    }
   1.403 +}
   1.404 +
   1.405 +static void
   1.406 +check_name (const gchar *what,
   1.407 +	    const gchar *value,
   1.408 +	    const gchar *expected,
   1.409 +	    gint         position)
   1.410 +{
   1.411 +  if (!value || strcmp (expected, value) != 0)
   1.412 +    {
   1.413 +      g_print ("Wrong %s returned: got '%s' at %d, expected '%s'\n",
   1.414 +	       what, value, position, expected);
   1.415 +	  	        
   1.416 +      g_assert(FALSE && "keyfile-test failed");
   1.417 +      
   1.418 +      #ifdef SYMBIAN
   1.419 +  	  testResultXml("keyfile-test");
   1.420 +  	  #endif /* EMULATOR */
   1.421 +  	  
   1.422 +      exit (1);
   1.423 +    }
   1.424 +}
   1.425 +
   1.426 +static void
   1.427 +check_length (const gchar *what,
   1.428 +	      gint         n_items,
   1.429 +	      gint         length,
   1.430 +	      gint         expected)
   1.431 +{
   1.432 +  if (n_items != length || length != expected)
   1.433 +    {
   1.434 +      g_print ("Wrong number of %s returned: got %d items, length %d, expected %d\n",
   1.435 +	       what, n_items, length, expected);
   1.436 +      	        
   1.437 +      g_assert(FALSE && "keyfile-test failed");
   1.438 +      
   1.439 +      #ifdef SYMBIAN
   1.440 +  	  testResultXml("keyfile-test");
   1.441 +  	  #endif /* EMULATOR */
   1.442 +  	  
   1.443 +      exit (1);
   1.444 +    }
   1.445 +}
   1.446 +
   1.447 +
   1.448 +/* check that both \n and \r\n are accepted as line ends,
   1.449 + * and that stray \r are passed through
   1.450 + */
   1.451 +static void
   1.452 +test_line_ends (void)
   1.453 +{
   1.454 +  GKeyFile *keyfile;
   1.455 +
   1.456 +  const gchar *data = 
   1.457 +    "[group1]\n"
   1.458 +    "key1=value1\n"
   1.459 +    "key2=value2\r\n"
   1.460 +    "[group2]\r\n"
   1.461 +    "key3=value3\r\r\n"
   1.462 +    "key4=value4\n";
   1.463 +
   1.464 +  keyfile = load_data (data, 0);
   1.465 +
   1.466 +  check_string_value (keyfile, "group1", "key1", "value1");
   1.467 +  check_string_value (keyfile, "group1", "key2", "value2");
   1.468 +  check_string_value (keyfile, "group2", "key3", "value3\r");
   1.469 +  check_string_value (keyfile, "group2", "key4", "value4");
   1.470 +
   1.471 +  g_key_file_free (keyfile);
   1.472 +}
   1.473 +
   1.474 +/* check handling of whitespace 
   1.475 + */
   1.476 +static void
   1.477 +test_whitespace (void)
   1.478 +{
   1.479 +  GKeyFile *keyfile;
   1.480 +
   1.481 +  const gchar *data = 
   1.482 +    "[group1]\n"
   1.483 +    "key1 = value1\n"
   1.484 +    "key2\t=\tvalue2\n"
   1.485 +    " [ group2 ] \n"
   1.486 +    "key3  =  value3  \n"
   1.487 +    "key4  =  value \t4\n"
   1.488 +    "  key5  =  value5\n";
   1.489 +  
   1.490 +  keyfile = load_data (data, 0);
   1.491 +
   1.492 +  check_string_value (keyfile, "group1", "key1", "value1");
   1.493 +  check_string_value (keyfile, "group1", "key2", "value2");
   1.494 +  check_string_value (keyfile, " group2 ", "key3", "value3  ");
   1.495 +  check_string_value (keyfile, " group2 ", "key4", "value \t4");
   1.496 +  check_string_value (keyfile, " group2 ", "key5", "value5");
   1.497 +
   1.498 +  g_key_file_free (keyfile);
   1.499 +}
   1.500 +
   1.501 +/* check handling of comments
   1.502 + */
   1.503 +static void
   1.504 +test_comments (void)
   1.505 +{
   1.506 +  GKeyFile *keyfile;
   1.507 +  gchar **names;
   1.508 +  gsize len;
   1.509 +  GError *error = NULL;
   1.510 +  gchar *comment;
   1.511 +
   1.512 +  const gchar *data = 
   1.513 +    "# top comment\n"
   1.514 +    "# top comment, continued\n"
   1.515 +    "[group1]\n"
   1.516 +    "key1 = value1\n"
   1.517 +    "# key comment\n"
   1.518 +    "# key comment, continued\n"
   1.519 +    "key2 = value2\n"
   1.520 +    "# line end check\r\n"
   1.521 +    "key3 = value3\n"
   1.522 +    "key4 = value4\n"
   1.523 +    "# group comment\n"
   1.524 +    "# group comment, continued\n"
   1.525 +    "[group2]\n";
   1.526 +
   1.527 +  const gchar *top_comment= " top comment\n top comment, continued\n";
   1.528 +  const gchar *group_comment= " group comment\n group comment, continued\n";
   1.529 +  const gchar *key_comment= " key comment\n key comment, continued\n";
   1.530 +  
   1.531 +  keyfile = load_data (data, 0);
   1.532 +
   1.533 +  check_string_value (keyfile, "group1", "key1", "value1");
   1.534 +  check_string_value (keyfile, "group1", "key2", "value2");
   1.535 +  check_string_value (keyfile, "group1", "key3", "value3");
   1.536 +  check_string_value (keyfile, "group1", "key4", "value4");
   1.537 +
   1.538 +  names = g_key_file_get_keys (keyfile, "group1", &len, &error);
   1.539 +  check_no_error (&error);
   1.540 +
   1.541 +  check_length ("keys", g_strv_length (names), len, 4);
   1.542 +  check_name ("key", names[0], "key1", 0);
   1.543 +  check_name ("key", names[1], "key2", 1);
   1.544 +  check_name ("key", names[2], "key3", 2);
   1.545 +  check_name ("key", names[3], "key4", 3);
   1.546 +
   1.547 +  g_strfreev (names);
   1.548 +
   1.549 +  g_key_file_free (keyfile);
   1.550 +
   1.551 +  keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
   1.552 +
   1.553 +  names = g_key_file_get_keys (keyfile, "group1", &len, &error);
   1.554 +  check_no_error (&error);
   1.555 +
   1.556 +  check_length ("keys", g_strv_length (names), len, 4);
   1.557 +  check_name ("key", names[0], "key1", 0);
   1.558 +  check_name ("key", names[1], "key2", 1);
   1.559 +  check_name ("key", names[2], "key3", 2);
   1.560 +  check_name ("key", names[3], "key4", 3);
   1.561 +
   1.562 +  g_strfreev (names);
   1.563 +
   1.564 +  comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
   1.565 +  check_no_error (&error);
   1.566 +  check_name ("top comment", comment, top_comment, 0);
   1.567 +  g_free (comment);
   1.568 +
   1.569 +  comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
   1.570 +  check_no_error (&error);
   1.571 +  check_name ("key comment", comment, key_comment, 0);
   1.572 +  g_free (comment);
   1.573 +
   1.574 +  comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
   1.575 +  check_no_error (&error);
   1.576 +  check_name ("group comment", comment, group_comment, 0);
   1.577 +  g_free (comment);
   1.578 +
   1.579 +  g_key_file_free (keyfile);
   1.580 +}
   1.581 +
   1.582 +
   1.583 +/* check key and group listing */
   1.584 +static void
   1.585 +test_listing (void)
   1.586 +{
   1.587 +  GKeyFile *keyfile;
   1.588 +  gchar **names;
   1.589 +  gsize len;
   1.590 +  gchar *start;
   1.591 +  GError *error = NULL;
   1.592 +
   1.593 +  const gchar *data = 
   1.594 +    "[group1]\n"
   1.595 +    "key1=value1\n"
   1.596 +    "key2=value2\n"
   1.597 +    "[group2]\n"
   1.598 +    "key3=value3\n"
   1.599 +    "key4=value4\n";
   1.600 +  
   1.601 +  keyfile = load_data (data, 0);
   1.602 +
   1.603 +  names = g_key_file_get_groups (keyfile, &len);
   1.604 +  if (names == NULL)
   1.605 +    {
   1.606 +      g_print ("Error listing groups\n");
   1.607 +            	        
   1.608 +      g_assert(FALSE && "keyfile-test failed");
   1.609 +      
   1.610 +      #ifdef SYMBIAN
   1.611 +  	  testResultXml("keyfile-test");
   1.612 +  	  #endif /* EMULATOR */
   1.613 +  	  
   1.614 +      exit (1);
   1.615 +    }
   1.616 +
   1.617 +  check_length ("groups", g_strv_length (names), len, 2);
   1.618 +  check_name ("group name", names[0], "group1", 0);
   1.619 +  check_name ("group name", names[1], "group2", 1);
   1.620 +  
   1.621 +  g_strfreev (names);
   1.622 +  
   1.623 +  names = g_key_file_get_keys (keyfile, "group1", &len, &error);
   1.624 +  check_no_error (&error);
   1.625 +
   1.626 +  check_length ("keys", g_strv_length (names), len, 2);
   1.627 +  check_name ("key", names[0], "key1", 0);
   1.628 +  check_name ("key", names[1], "key2", 1);
   1.629 +
   1.630 +  g_strfreev (names);
   1.631 +
   1.632 +  names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
   1.633 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
   1.634 +
   1.635 +  g_strfreev (names);
   1.636 +
   1.637 +  if (!g_key_file_has_group (keyfile, "group1") ||
   1.638 +      !g_key_file_has_group (keyfile, "group2") ||
   1.639 +      g_key_file_has_group (keyfile, "group10") ||
   1.640 +      g_key_file_has_group (keyfile, "group2 "))      
   1.641 +    {
   1.642 +      g_print ("Group finding trouble\n");
   1.643 +      
   1.644 +      	        
   1.645 +      g_assert(FALSE && "keyfile-test failed");
   1.646 +      
   1.647 +      #ifdef SYMBIAN
   1.648 +  	  testResultXml("keyfile-test");
   1.649 +  	  #endif /* EMULATOR */
   1.650 +  	  
   1.651 +      exit (1);      
   1.652 +    }
   1.653 +
   1.654 +  start = g_key_file_get_start_group (keyfile);
   1.655 +  if (!start || strcmp (start, "group1") != 0)
   1.656 +    {
   1.657 +      g_print ("Start group finding trouble\n");
   1.658 +      
   1.659 +      	        
   1.660 +      g_assert(FALSE && "keyfile-test failed");
   1.661 +      
   1.662 +      #ifdef SYMBIAN
   1.663 +  	  testResultXml("keyfile-test");
   1.664 +  	  #endif /* EMULATOR */
   1.665 +  	  
   1.666 +      exit (1);
   1.667 +    }
   1.668 +  g_free (start);
   1.669 +
   1.670 +  if (!g_key_file_has_key (keyfile, "group1", "key1", &error) ||
   1.671 +      !g_key_file_has_key (keyfile, "group2", "key3", &error) ||
   1.672 +      g_key_file_has_key (keyfile, "group2", "no-such-key", &error))
   1.673 +    {
   1.674 +      g_print ("Key finding trouble\n");
   1.675 +      
   1.676 +      	        
   1.677 +      g_assert(FALSE && "keyfile-test failed");
   1.678 +      
   1.679 +      #ifdef SYMBIAN
   1.680 +  	  testResultXml("keyfile-test");
   1.681 +  	  #endif /* EMULATOR */
   1.682 +  	  
   1.683 +
   1.684 +      exit (1);      
   1.685 +    }
   1.686 +  check_no_error (&error);
   1.687 +  
   1.688 +  g_key_file_has_key (keyfile, "no-such-group", "key", &error);
   1.689 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
   1.690 +
   1.691 +  g_key_file_free (keyfile);
   1.692 +}
   1.693 +
   1.694 +/* check parsing of string values */
   1.695 +static void
   1.696 +test_string (void)
   1.697 +{
   1.698 +  GKeyFile *keyfile;
   1.699 +  GError *error = NULL;
   1.700 +  gchar *value;
   1.701 +
   1.702 +  const gchar *data = 
   1.703 +    "[valid]\n"
   1.704 +    "key1=\\s\\n\\t\\r\\\\\n"
   1.705 +    "key2=\"quoted\"\n"
   1.706 +    "key3='quoted'\n"
   1.707 +    "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
   1.708 +    "[invalid]\n"
   1.709 +    "key1=\\a\\b\\0800xff\n"
   1.710 +    "key2=blabla\\\n";
   1.711 +  
   1.712 +  keyfile = load_data (data, 0);
   1.713 +
   1.714 +  check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
   1.715 +  check_string_value (keyfile, "valid", "key2", "\"quoted\"");
   1.716 +  check_string_value (keyfile, "valid", "key3", "'quoted'");  
   1.717 +  check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");  
   1.718 +  
   1.719 +  value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
   1.720 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.721 +  g_free (value);
   1.722 +
   1.723 +  value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
   1.724 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.725 +  g_free (value);
   1.726 +  
   1.727 +  g_key_file_free (keyfile);
   1.728 +}
   1.729 +
   1.730 +/* check parsing of boolean values */
   1.731 +static void
   1.732 +test_boolean (void)
   1.733 +{
   1.734 +  GKeyFile *keyfile;
   1.735 +  GError *error = NULL;
   1.736 +
   1.737 +  const gchar *data = 
   1.738 +    "[valid]\n"
   1.739 +    "key1=true\n"
   1.740 +    "key2=false\n"
   1.741 +    "key3=1\n"
   1.742 +    "key4=0\n"
   1.743 +    "[invalid]\n"
   1.744 +    "key1=t\n"
   1.745 +    "key2=f\n"
   1.746 +    "key3=yes\n"
   1.747 +    "key4=no\n";
   1.748 +  
   1.749 +  keyfile = load_data (data, 0);
   1.750 +
   1.751 +  check_boolean_value (keyfile, "valid", "key1", TRUE);
   1.752 +  check_boolean_value (keyfile, "valid", "key2", FALSE);
   1.753 +  check_boolean_value (keyfile, "valid", "key3", TRUE);
   1.754 +  check_boolean_value (keyfile, "valid", "key4", FALSE);
   1.755 +
   1.756 +  g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
   1.757 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.758 +
   1.759 +  g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
   1.760 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.761 +
   1.762 +  g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
   1.763 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.764 +
   1.765 +  g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
   1.766 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.767 +
   1.768 +  g_key_file_free (keyfile);
   1.769 +}
   1.770 +
   1.771 +/* check parsing of integer values */
   1.772 +static void
   1.773 +test_integer (void)
   1.774 +{
   1.775 +  GKeyFile *keyfile;
   1.776 +  GError *error = NULL;
   1.777 +
   1.778 +  const gchar *data = 
   1.779 +    "[valid]\n"
   1.780 +    "key1=0\n"
   1.781 +    "key2=1\n"
   1.782 +    "key3=-1\n"
   1.783 +    "key4=2324431\n"
   1.784 +    "key5=-2324431\n"
   1.785 +    "key6=000111\n"
   1.786 +    "[invalid]\n"
   1.787 +    "key1=0xffff\n"
   1.788 +    "key2=0.5\n"
   1.789 +    "key3=1e37\n"
   1.790 +    "key4=ten\n";
   1.791 +  
   1.792 +  keyfile = load_data (data, 0);
   1.793 +
   1.794 +  check_integer_value (keyfile, "valid", "key1", 0);
   1.795 +  check_integer_value (keyfile, "valid", "key2", 1);
   1.796 +  check_integer_value (keyfile, "valid", "key3", -1);
   1.797 +  check_integer_value (keyfile, "valid", "key4", 2324431);
   1.798 +  check_integer_value (keyfile, "valid", "key5", -2324431);
   1.799 +  check_integer_value (keyfile, "valid", "key6", 111);
   1.800 +
   1.801 +  g_key_file_get_integer (keyfile, "invalid", "key1", &error);
   1.802 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.803 +
   1.804 +  g_key_file_get_integer (keyfile, "invalid", "key2", &error);
   1.805 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.806 +
   1.807 +  g_key_file_get_integer (keyfile, "invalid", "key3", &error);
   1.808 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.809 +
   1.810 +  g_key_file_get_integer (keyfile, "invalid", "key4", &error);
   1.811 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
   1.812 +
   1.813 +  g_key_file_free (keyfile);
   1.814 +}
   1.815 +
   1.816 +/* check handling of translated strings */
   1.817 +static void
   1.818 +test_locale_string (void)
   1.819 +{
   1.820 +  GKeyFile *keyfile;
   1.821 +  GError *error = NULL;
   1.822 +  gchar *value;
   1.823 +
   1.824 +  const gchar *data = 
   1.825 +    "[valid]\n"
   1.826 +    "key1=v1\n"
   1.827 +    "key1[de]=v1-de\n"
   1.828 +    "key1[de_DE]=v1-de_DE\n"
   1.829 +    "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
   1.830 +    "key1[fr]=v1-fr\n"
   1.831 +    "key1[en] =v1-en\n"
   1.832 +    "key1[sr@Latn]=v1-sr\n";
   1.833 +  
   1.834 +  keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
   1.835 +
   1.836 +  check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
   1.837 +  check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
   1.838 +  check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
   1.839 +  check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
   1.840 +  check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
   1.841 +  check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
   1.842 +  check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
   1.843 +  check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
   1.844 +  
   1.845 +  g_key_file_free (keyfile);
   1.846 +
   1.847 +  /* now test that translations are thrown away */
   1.848 +
   1.849 +  g_setenv ("LANGUAGE", "de", TRUE);
   1.850 +  setlocale (LC_ALL, "");
   1.851 +
   1.852 +  keyfile = load_data (data, 0);
   1.853 +
   1.854 +  check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
   1.855 +  check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
   1.856 +  check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
   1.857 +  check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
   1.858 +  check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
   1.859 +  check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
   1.860 +  check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
   1.861 +
   1.862 +  g_key_file_free (keyfile);  
   1.863 +}
   1.864 +
   1.865 +static void
   1.866 +test_lists (void)
   1.867 +{
   1.868 +  GKeyFile *keyfile;
   1.869 +
   1.870 +  const gchar *data = 
   1.871 +    "[valid]\n"
   1.872 +    "key1=v1;v2\n"
   1.873 +    "key2=v1;v2;\n"
   1.874 +    "key3=v1,v2\n"
   1.875 +    "key4=v1\\;v2\n"
   1.876 +    "key5=true;false\n"
   1.877 +    "key6=1;0;-1\n"
   1.878 +    "key7= 1 ; 0 ; -1 \n"
   1.879 +    "key8=v1\\,v2\n";
   1.880 +  
   1.881 +  keyfile = load_data (data, 0);
   1.882 +
   1.883 +  check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
   1.884 +  check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
   1.885 +  check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
   1.886 +  check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
   1.887 +  check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
   1.888 +  check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
   1.889 +  /* maybe these should be valid */
   1.890 +  /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
   1.891 +  /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
   1.892 +
   1.893 +  g_key_file_free (keyfile);  
   1.894 +
   1.895 +  /* Now check an alternate separator */
   1.896 +
   1.897 +  keyfile = load_data (data, 0);
   1.898 +  g_key_file_set_list_separator (keyfile, ',');
   1.899 +
   1.900 +  check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
   1.901 +  check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
   1.902 +  check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
   1.903 +
   1.904 +  g_key_file_free (keyfile);  
   1.905 +}
   1.906 +
   1.907 +/* http://bugzilla.gnome.org/show_bug.cgi?id=165887 */
   1.908 +static void 
   1.909 +test_group_remove (void)
   1.910 +{
   1.911 +  GKeyFile *keyfile;
   1.912 +  gchar **names;
   1.913 +  gsize len;
   1.914 +  GError *error = NULL;
   1.915 +
   1.916 +  const gchar *data = 
   1.917 +    "[group1]\n"
   1.918 +    "[group2]\n"
   1.919 +    "key1=bla\n"
   1.920 +    "key2=bla\n"
   1.921 +    "[group3]\n"
   1.922 +    "key1=bla\n"
   1.923 +    "key2=bla\n";
   1.924 +  
   1.925 +  keyfile = load_data (data, 0);
   1.926 +  
   1.927 +  names = g_key_file_get_groups (keyfile, &len);
   1.928 +  if (names == NULL)
   1.929 +    {
   1.930 +      g_print ("Error listing groups\n");
   1.931 +      	        
   1.932 +      g_assert(FALSE && "keyfile-test failed");
   1.933 +      
   1.934 +      #ifdef SYMBIAN
   1.935 +  	  testResultXml("keyfile-test");
   1.936 +  	  #endif /* EMULATOR */
   1.937 +  	  
   1.938 +      exit (1);
   1.939 +    }
   1.940 +
   1.941 +  check_length ("groups", g_strv_length (names), len, 3);
   1.942 +  check_name ("group name", names[0], "group1", 0);
   1.943 +  check_name ("group name", names[1], "group2", 1);
   1.944 +  check_name ("group name", names[2], "group3", 2);
   1.945 +
   1.946 +  g_key_file_remove_group (keyfile, "group1", &error);
   1.947 +  check_no_error (&error);
   1.948 +  
   1.949 +  g_strfreev (names);
   1.950 +
   1.951 +  names = g_key_file_get_groups (keyfile, &len);
   1.952 +  if (names == NULL)
   1.953 +    {
   1.954 +      g_print ("Error listing groups\n");
   1.955 +      	        
   1.956 +      g_assert(FALSE && "keyfile-test failed");
   1.957 +      
   1.958 +      #ifdef SYMBIAN
   1.959 +  	  testResultXml("keyfile-test");
   1.960 +  	  #endif /* EMULATOR */
   1.961 +  	  
   1.962 +      exit (1);
   1.963 +    }
   1.964 +
   1.965 +  check_length ("groups", g_strv_length (names), len, 2);
   1.966 +  check_name ("group name", names[0], "group2", 0);
   1.967 +  check_name ("group name", names[1], "group3", 1);
   1.968 +
   1.969 +  g_key_file_remove_group (keyfile, "group2", &error);
   1.970 +  check_no_error (&error);
   1.971 +  
   1.972 +  g_strfreev (names);
   1.973 +
   1.974 +  names = g_key_file_get_groups (keyfile, &len);
   1.975 +  if (names == NULL)
   1.976 +    {
   1.977 +      g_print ("Error listing groups\n");
   1.978 +      	        
   1.979 +      g_assert(FALSE && "keyfile-test failed");
   1.980 +      
   1.981 +      #ifdef SYMBIAN
   1.982 +  	  testResultXml("keyfile-test");
   1.983 +  	  #endif /* EMULATOR */
   1.984 +  	  
   1.985 +      exit (1);
   1.986 +    }
   1.987 +
   1.988 +  check_length ("groups", g_strv_length (names), len, 1);
   1.989 +  check_name ("group name", names[0], "group3", 0);
   1.990 +
   1.991 +  g_key_file_remove_group (keyfile, "no such group", &error);
   1.992 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
   1.993 +
   1.994 +  g_strfreev (names);
   1.995 +
   1.996 +  g_key_file_free (keyfile);
   1.997 +}
   1.998 +
   1.999 +/* http://bugzilla.gnome.org/show_bug.cgi?id=165980 */
  1.1000 +static void 
  1.1001 +test_key_remove (void)
  1.1002 +{
  1.1003 +  GKeyFile *keyfile;
  1.1004 +  gchar *value;
  1.1005 +  GError *error = NULL;
  1.1006 +
  1.1007 +  const gchar *data = 
  1.1008 +    "[group1]\n"
  1.1009 +    "key1=bla\n"
  1.1010 +    "key2=bla\n";
  1.1011 +  
  1.1012 +  keyfile = load_data (data, 0);
  1.1013 +  
  1.1014 +  check_string_value (keyfile, "group1", "key1", "bla");
  1.1015 +
  1.1016 +  g_key_file_remove_key (keyfile, "group1", "key1", &error);
  1.1017 +  check_no_error (&error);
  1.1018 +
  1.1019 +  value = g_key_file_get_string (keyfile, "group1", "key1", &error);
  1.1020 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
  1.1021 +  g_free (value);
  1.1022 +  
  1.1023 +  g_key_file_remove_key (keyfile, "group1", "key1", &error);
  1.1024 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
  1.1025 +
  1.1026 +  g_key_file_remove_key (keyfile, "no such group", "key1", &error);
  1.1027 +  check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
  1.1028 +
  1.1029 +  g_key_file_free (keyfile);
  1.1030 +}
  1.1031 +
  1.1032 +/* http://bugzilla.gnome.org/show_bug.cgi?id=316309 */
  1.1033 +static void
  1.1034 +test_groups (void)
  1.1035 +{
  1.1036 +  GKeyFile *keyfile;
  1.1037 +
  1.1038 +  const gchar *data = 
  1.1039 +    "[1]\n"
  1.1040 +    "key1=123\n"
  1.1041 +    "[2]\n"
  1.1042 +    "key2=123\n";
  1.1043 +  
  1.1044 +  keyfile = load_data (data, 0);
  1.1045 +
  1.1046 +  check_string_value (keyfile, "1", "key1", "123");
  1.1047 +  check_string_value (keyfile, "2", "key2", "123");
  1.1048 +
  1.1049 +  g_key_file_free (keyfile);  
  1.1050 +}
  1.1051 +
  1.1052 +
  1.1053 +int
  1.1054 +main (int argc, char *argv[])
  1.1055 +{
  1.1056 +  #ifdef SYMBIAN
  1.1057 +  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
  1.1058 +  g_set_print_handler(mrtPrintHandler);
  1.1059 +  #endif /*SYMBIAN*/
  1.1060 +	  
  1.1061 +
  1.1062 +  test_line_ends ();
  1.1063 +  test_whitespace ();
  1.1064 +  test_comments ();
  1.1065 +  test_listing ();
  1.1066 +  test_string ();
  1.1067 +  test_boolean ();
  1.1068 +  test_integer ();
  1.1069 +  test_locale_string ();
  1.1070 +  test_lists ();
  1.1071 +  test_group_remove ();
  1.1072 +  test_key_remove ();
  1.1073 +  test_groups ();
  1.1074 +  
  1.1075 +  #ifdef SYMBIAN
  1.1076 +  testResultXml("keyfile-test");
  1.1077 +  #endif /* EMULATOR */
  1.1078 +  	  
  1.1079 +  return 0;
  1.1080 +}