os/ossrv/glib/tsrc/BC/src/keyfile_test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     3
*
sl@0
     4
* This library is free software; you can redistribute it and/or
sl@0
     5
* modify it under the terms of the GNU Lesser General Public
sl@0
     6
* License as published by the Free Software Foundation; either
sl@0
     7
* version 2 of the License, or (at your option) any later version.
sl@0
     8
*
sl@0
     9
* This library is distributed in the hope that it will be useful,
sl@0
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
* Lesser General Public License for more details.
sl@0
    13
*
sl@0
    14
* You should have received a copy of the GNU Lesser General Public
sl@0
    15
* License along with this library; if not, write to the
sl@0
    16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
* Boston, MA 02111-1307, USA.
sl@0
    18
*
sl@0
    19
* Description:
sl@0
    20
*
sl@0
    21
*/
sl@0
    22
sl@0
    23
sl@0
    24
sl@0
    25
#undef G_DISABLE_ASSERT
sl@0
    26
#undef G_LOG_DOMAIN
sl@0
    27
sl@0
    28
#include <stdio.h>
sl@0
    29
#include <glib.h>
sl@0
    30
#include <locale.h>
sl@0
    31
#include <string.h>
sl@0
    32
#include <stdlib.h>
sl@0
    33
sl@0
    34
#ifdef SYMBIAN
sl@0
    35
#include "mrt2_glib2_test.h"
sl@0
    36
#endif /*SYMBIAN*/
sl@0
    37
sl@0
    38
sl@0
    39
static GKeyFile *
sl@0
    40
load_data (const gchar   *data, 
sl@0
    41
	   GKeyFileFlags  flags)
sl@0
    42
{
sl@0
    43
  GKeyFile *keyfile;
sl@0
    44
  GError *error = NULL;
sl@0
    45
sl@0
    46
  keyfile = g_key_file_new ();
sl@0
    47
  g_key_file_load_from_data (keyfile, data, -1, flags, &error);
sl@0
    48
  if (error)
sl@0
    49
    {
sl@0
    50
      g_print ("Could not load data: %s\n", error->message);
sl@0
    51
      
sl@0
    52
      g_assert(FALSE && "keyfile_test failed");
sl@0
    53
      
sl@0
    54
      #ifdef SYMBIAN
sl@0
    55
  	  testResultXml("keyfile_test");
sl@0
    56
  	  #endif /* EMULATOR */
sl@0
    57
      
sl@0
    58
      exit (1);
sl@0
    59
    }
sl@0
    60
  
sl@0
    61
  return keyfile;
sl@0
    62
}
sl@0
    63
sl@0
    64
static void
sl@0
    65
check_error (GError **error,
sl@0
    66
	     GQuark   domain,
sl@0
    67
	     gint     code)
sl@0
    68
{
sl@0
    69
  if (*error == NULL)
sl@0
    70
    {
sl@0
    71
      g_print ("Missing an error\n");
sl@0
    72
      exit (1);
sl@0
    73
    }
sl@0
    74
  
sl@0
    75
  if ((*error)->domain != domain)
sl@0
    76
    {
sl@0
    77
      g_print ("Wrong error domain: got %s, expected %s\n",
sl@0
    78
	       g_quark_to_string ((*error)->domain),
sl@0
    79
	       g_quark_to_string (domain));
sl@0
    80
	  
sl@0
    81
	  g_assert(FALSE && "keyfile_test failed");
sl@0
    82
      
sl@0
    83
      #ifdef SYMBIAN
sl@0
    84
  	  testResultXml("keyfile_test");
sl@0
    85
  	  #endif /* EMULATOR */
sl@0
    86
  	  
sl@0
    87
      exit (1);
sl@0
    88
    }
sl@0
    89
  
sl@0
    90
  if ((*error)->code != code)
sl@0
    91
    {
sl@0
    92
      g_print ("Wrong error code: got %d, expected %d\n",
sl@0
    93
	       (*error)->code, code);
sl@0
    94
	       
sl@0
    95
	  g_assert(FALSE && "keyfile_test failed");
sl@0
    96
      
sl@0
    97
      #ifdef SYMBIAN
sl@0
    98
  	  testResultXml("keyfile_test");
sl@0
    99
  	  #endif /* EMULATOR */
sl@0
   100
      
sl@0
   101
      exit (1);
sl@0
   102
    }
sl@0
   103
sl@0
   104
  g_error_free (*error);
sl@0
   105
  *error = NULL;
sl@0
   106
}
sl@0
   107
sl@0
   108
static void 
sl@0
   109
check_no_error (GError **error)
sl@0
   110
{
sl@0
   111
  if (*error != NULL)
sl@0
   112
    {
sl@0
   113
      g_print ("Unexpected error: (%s, %d) %s\n",
sl@0
   114
	       g_quark_to_string ((*error)->domain),
sl@0
   115
	       (*error)->code, (*error)->message);
sl@0
   116
	  g_assert(FALSE && "keyfile_test failed");
sl@0
   117
      
sl@0
   118
      #ifdef SYMBIAN
sl@0
   119
  	  testResultXml("keyfile_test");
sl@0
   120
  	  #endif /* EMULATOR */
sl@0
   121
      
sl@0
   122
      exit (1);
sl@0
   123
    }
sl@0
   124
}
sl@0
   125
sl@0
   126
static void
sl@0
   127
check_string_value (GKeyFile    *keyfile,
sl@0
   128
		    const gchar *group,
sl@0
   129
		    const gchar *key,
sl@0
   130
		    const gchar *expected) 
sl@0
   131
{
sl@0
   132
  GError *error = NULL;
sl@0
   133
  gchar *value;
sl@0
   134
sl@0
   135
  value = g_key_file_get_string (keyfile, group, key, &error);
sl@0
   136
  check_no_error (&error);
sl@0
   137
  g_assert (value != NULL);
sl@0
   138
sl@0
   139
  if (strcmp (value, expected) != 0)
sl@0
   140
    {
sl@0
   141
      g_print ("Group %s key %s: "
sl@0
   142
	       "expected string value '%s', actual value '%s'\n",
sl@0
   143
	       group, key, expected, value);      
sl@0
   144
	  
sl@0
   145
	  g_assert(FALSE && "keyfile_test failed");
sl@0
   146
      
sl@0
   147
      #ifdef SYMBIAN
sl@0
   148
  	  testResultXml("keyfile_test");
sl@0
   149
  	  #endif /* EMULATOR */
sl@0
   150
      
sl@0
   151
      exit (1);
sl@0
   152
    }
sl@0
   153
sl@0
   154
  g_free (value);
sl@0
   155
}
sl@0
   156
sl@0
   157
static void
sl@0
   158
check_locale_string_value (GKeyFile    *keyfile,
sl@0
   159
			   const gchar *group,
sl@0
   160
			   const gchar *key,
sl@0
   161
			   const gchar *locale,
sl@0
   162
			   const gchar *expected) 
sl@0
   163
{
sl@0
   164
  GError *error = NULL;
sl@0
   165
  gchar *value;
sl@0
   166
sl@0
   167
  value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
sl@0
   168
  check_no_error (&error);
sl@0
   169
  g_assert (value != NULL);
sl@0
   170
sl@0
   171
  if (strcmp (value, expected) != 0)
sl@0
   172
    {
sl@0
   173
      g_print ("Group %s key %s locale %s: "
sl@0
   174
	       "expected string value '%s', actual value '%s'\n",
sl@0
   175
	       group, key, locale, expected, value);      
sl@0
   176
	  
sl@0
   177
	  g_assert(FALSE && "keyfile_test failed");
sl@0
   178
      
sl@0
   179
      #ifdef SYMBIAN
sl@0
   180
  	  testResultXml("keyfile_test");
sl@0
   181
  	  #endif /* EMULATOR */
sl@0
   182
  	  
sl@0
   183
      exit (1);
sl@0
   184
    }
sl@0
   185
sl@0
   186
  g_free (value);
sl@0
   187
}
sl@0
   188
sl@0
   189
static void
sl@0
   190
check_boolean_value (GKeyFile    *keyfile,
sl@0
   191
		     const gchar *group,
sl@0
   192
		     const gchar *key,
sl@0
   193
		     gboolean     expected) 
sl@0
   194
{
sl@0
   195
  GError *error = NULL;
sl@0
   196
  gboolean value;
sl@0
   197
sl@0
   198
  value = g_key_file_get_boolean (keyfile, group, key, &error);
sl@0
   199
  check_no_error (&error);
sl@0
   200
sl@0
   201
  if (value != expected)
sl@0
   202
    {
sl@0
   203
      g_print ("Group %s key %s: "
sl@0
   204
	       "expected boolean value '%s', actual value '%s'\n",
sl@0
   205
	       group, key, 
sl@0
   206
	       expected ? "true" : "false", 
sl@0
   207
	       value ? "true" : "false");      
sl@0
   208
	       
sl@0
   209
	       
sl@0
   210
	  g_assert(FALSE && "keyfile_test failed");
sl@0
   211
      
sl@0
   212
      #ifdef SYMBIAN
sl@0
   213
  	  testResultXml("keyfile_test");
sl@0
   214
  	  #endif /* EMULATOR */
sl@0
   215
  	  
sl@0
   216
      exit (1);
sl@0
   217
    }
sl@0
   218
}
sl@0
   219
sl@0
   220
static void
sl@0
   221
check_integer_value (GKeyFile    *keyfile,
sl@0
   222
		     const gchar *group,
sl@0
   223
		     const gchar *key,
sl@0
   224
		     gint         expected) 
sl@0
   225
{
sl@0
   226
  GError *error = NULL;
sl@0
   227
  gint value;
sl@0
   228
sl@0
   229
  value = g_key_file_get_integer (keyfile, group, key, &error);
sl@0
   230
  check_no_error (&error);
sl@0
   231
sl@0
   232
  if (value != expected)
sl@0
   233
    {
sl@0
   234
      g_print ("Group %s key %s: "
sl@0
   235
	       "expected integer value %d, actual value %d\n",
sl@0
   236
	       group, key, expected, value);      
sl@0
   237
	  
sl@0
   238
	  g_assert(FALSE && "keyfile_test failed");
sl@0
   239
      
sl@0
   240
      #ifdef SYMBIAN
sl@0
   241
  	  testResultXml("keyfile_test");
sl@0
   242
  	  #endif /* EMULATOR */
sl@0
   243
  	  
sl@0
   244
      exit (1);
sl@0
   245
    }
sl@0
   246
}
sl@0
   247
sl@0
   248
int create_file(char *path)
sl@0
   249
{
sl@0
   250
	FILE *fp = fopen(path,"w");
sl@0
   251
	if(fp)
sl@0
   252
	{
sl@0
   253
		fprintf(fp,"[group1]\n");
sl@0
   254
		fprintf(fp,"key1=123\n");
sl@0
   255
		fprintf(fp,"key2=456\n");
sl@0
   256
		fclose(fp);
sl@0
   257
		return TRUE;
sl@0
   258
	}
sl@0
   259
	return FALSE;
sl@0
   260
}
sl@0
   261
sl@0
   262
void test_loadfromfile()
sl@0
   263
{
sl@0
   264
	
sl@0
   265
	GKeyFile *keyfile = g_key_file_new(),*keyfile1=g_key_file_new();
sl@0
   266
	
sl@0
   267
	gchar *full_path = NULL;	
sl@0
   268
sl@0
   269
	int ret;
sl@0
   270
	if(create_file("c:\\tempfile.txt"))
sl@0
   271
	{
sl@0
   272
		g_assert(g_key_file_load_from_file(keyfile,"c:\\tempfile.txt",0,NULL));
sl@0
   273
		check_string_value (keyfile, "group1", "key1", "123");
sl@0
   274
		check_string_value (keyfile, "group1", "key2", "456");
sl@0
   275
	}
sl@0
   276
	else
sl@0
   277
		g_assert(FALSE);
sl@0
   278
	
sl@0
   279
	g_key_file_free(keyfile);
sl@0
   280
	
sl@0
   281
	if(create_file("tempfile.txt"))
sl@0
   282
		g_assert(g_key_file_load_from_data_dirs(keyfile1,"tempfile.txt",&full_path,0,NULL));
sl@0
   283
	
sl@0
   284
	
sl@0
   285
}
sl@0
   286
sl@0
   287
void test_setstring()
sl@0
   288
{
sl@0
   289
	GKeyFile *keyfile;
sl@0
   290
	gchar **value;
sl@0
   291
	gsize n = 2;
sl@0
   292
	
sl@0
   293
	const gchar *list[2] = 
sl@0
   294
	{
sl@0
   295
		"test1","test2"	
sl@0
   296
	};
sl@0
   297
sl@0
   298
  	const gchar *data = 
sl@0
   299
	    "[1]\n"
sl@0
   300
	    "key1=123\n"
sl@0
   301
	    "[2]\n"
sl@0
   302
	    "key2=456\n";
sl@0
   303
sl@0
   304
	keyfile = load_data (data, 0);
sl@0
   305
	
sl@0
   306
	check_string_value (keyfile, "1", "key1", "123");
sl@0
   307
	g_key_file_set_string(keyfile,"1","key1","789");
sl@0
   308
	check_string_value (keyfile, "1", "key1", "789");
sl@0
   309
	
sl@0
   310
	g_key_file_set_string_list(keyfile,"1","key1",list,2);
sl@0
   311
	
sl@0
   312
	value = g_key_file_get_string_list(keyfile, "1", "key1",&n, NULL);
sl@0
   313
	
sl@0
   314
	g_assert(!strcmp(value[0],"test1"));
sl@0
   315
	g_assert(!strcmp(value[1],"test2"));
sl@0
   316
	g_strfreev(value);
sl@0
   317
}
sl@0
   318
sl@0
   319
void test_setboolean()
sl@0
   320
{
sl@0
   321
	GKeyFile *keyfile;
sl@0
   322
	gboolean *value;
sl@0
   323
	gsize n = 2;
sl@0
   324
	
sl@0
   325
	gboolean list[2] = {TRUE,FALSE};
sl@0
   326
	
sl@0
   327
	const gchar *data = 
sl@0
   328
	    "[1]\n"
sl@0
   329
	    "key1=true\n"
sl@0
   330
	    "key2=false\n";
sl@0
   331
sl@0
   332
	keyfile = load_data (data, 0);
sl@0
   333
	
sl@0
   334
	check_boolean_value (keyfile, "1", "key1", TRUE);
sl@0
   335
	g_key_file_set_boolean(keyfile,"1","key1",FALSE);
sl@0
   336
	check_boolean_value (keyfile, "1", "key1", FALSE);
sl@0
   337
	
sl@0
   338
	g_key_file_set_boolean_list(keyfile,"1","key1",list,2);
sl@0
   339
	
sl@0
   340
	value = g_key_file_get_boolean_list(keyfile, "1", "key1",&n, NULL);
sl@0
   341
	
sl@0
   342
	g_assert(value[0] == TRUE);
sl@0
   343
	g_assert(value[1] == FALSE);
sl@0
   344
	
sl@0
   345
}
sl@0
   346
sl@0
   347
void test_setinteger()
sl@0
   348
{
sl@0
   349
	GKeyFile *keyfile;
sl@0
   350
	gint *value;
sl@0
   351
	gsize n = 2;
sl@0
   352
	
sl@0
   353
	gint list[2] = {111,222};
sl@0
   354
	
sl@0
   355
	const gchar *data = 
sl@0
   356
	    "[1]\n"
sl@0
   357
	    "key1=123\n"
sl@0
   358
	    "key2=456\n";
sl@0
   359
sl@0
   360
	keyfile = load_data (data, 0);
sl@0
   361
	
sl@0
   362
	check_integer_value (keyfile, "1", "key1", 123);
sl@0
   363
	g_key_file_set_integer(keyfile,"1","key1",789);
sl@0
   364
	check_integer_value (keyfile, "1", "key1", 789);
sl@0
   365
	
sl@0
   366
	g_key_file_set_integer_list(keyfile,"1","key1",list,2);
sl@0
   367
	
sl@0
   368
	value = g_key_file_get_integer_list(keyfile, "1", "key1",&n, NULL);
sl@0
   369
	
sl@0
   370
	g_assert(value[0] == 111);
sl@0
   371
	g_assert(value[1] == 222);
sl@0
   372
	
sl@0
   373
}
sl@0
   374
sl@0
   375
void test_keyfiletodata()
sl@0
   376
{
sl@0
   377
	GKeyFile *keyfile;
sl@0
   378
	
sl@0
   379
	gchar *retVal = NULL;
sl@0
   380
	gsize length = 0;
sl@0
   381
	
sl@0
   382
	const gchar *data = 
sl@0
   383
	    "[1]\n"
sl@0
   384
	    "key1=123\n"
sl@0
   385
	    "key2=456\n";
sl@0
   386
	
sl@0
   387
	keyfile = load_data (data, 0);
sl@0
   388
	
sl@0
   389
	retVal = g_key_file_to_data(keyfile,&length,NULL);
sl@0
   390
	
sl@0
   391
	g_assert(retVal != NULL && length > 0);
sl@0
   392
}
sl@0
   393
sl@0
   394
void test_setcomment()
sl@0
   395
{
sl@0
   396
	GKeyFile *keyfile;
sl@0
   397
	gchar *comment;
sl@0
   398
sl@0
   399
	const gchar *data = 
sl@0
   400
	    "[1]\n"
sl@0
   401
	    "key1=123\n"
sl@0
   402
	    "key2=456\n";
sl@0
   403
	
sl@0
   404
	keyfile = load_data (data, 0);
sl@0
   405
	
sl@0
   406
	g_key_file_set_comment(keyfile,"1","key1","comment1",NULL);
sl@0
   407
	
sl@0
   408
	comment = g_key_file_get_comment(keyfile,"1","key1",NULL);
sl@0
   409
	
sl@0
   410
	g_assert(!strcmp(comment,"comment1\n"));
sl@0
   411
	
sl@0
   412
	g_key_file_remove_comment(keyfile,"1","key1",NULL);
sl@0
   413
	
sl@0
   414
	g_free(comment);
sl@0
   415
	
sl@0
   416
	comment = g_key_file_get_comment(keyfile,"1","key1",NULL);
sl@0
   417
	
sl@0
   418
	g_assert(comment == NULL);
sl@0
   419
	
sl@0
   420
}
sl@0
   421
sl@0
   422
void test_setlocale()
sl@0
   423
{
sl@0
   424
	GKeyFile *keyfile;
sl@0
   425
	gchar *value;
sl@0
   426
	gchar **list;
sl@0
   427
	gchar *input_list[2] = {"test2","test3"};
sl@0
   428
	gsize length = 2;
sl@0
   429
	
sl@0
   430
	const gchar *data = 
sl@0
   431
	    "[1]\n"
sl@0
   432
	    "key1=123\n"
sl@0
   433
	    "key2=456\n";
sl@0
   434
	
sl@0
   435
	keyfile = load_data (data, 0);
sl@0
   436
	
sl@0
   437
	g_key_file_set_locale_string(keyfile,"1","key3","C","test");
sl@0
   438
	
sl@0
   439
	value = g_key_file_get_locale_string(keyfile,"1","key3","C",NULL);
sl@0
   440
	
sl@0
   441
	g_assert(!strcmp(value,"test"));
sl@0
   442
	
sl@0
   443
	g_free(value);
sl@0
   444
	
sl@0
   445
	value = g_key_file_get_locale_string(keyfile,"1","key3","de",NULL);
sl@0
   446
	
sl@0
   447
	g_assert(value == NULL);
sl@0
   448
	
sl@0
   449
	g_key_file_set_locale_string_list(keyfile,"1","key4","C",input_list,2);
sl@0
   450
	
sl@0
   451
	list = g_key_file_get_locale_string_list(keyfile,"1","key4","C",&length,NULL);
sl@0
   452
	
sl@0
   453
	g_assert(!strcmp(list[0],"test2"));
sl@0
   454
	g_assert(!strcmp(list[1],"test3"));
sl@0
   455
	
sl@0
   456
}
sl@0
   457
sl@0
   458
int
sl@0
   459
main (int argc, char *argv[])
sl@0
   460
{
sl@0
   461
  	#ifdef SYMBIAN
sl@0
   462
	
sl@0
   463
	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);
sl@0
   464
	#endif /*SYMBIAN*/
sl@0
   465
  
sl@0
   466
  test_loadfromfile();
sl@0
   467
  test_setstring();
sl@0
   468
  test_setboolean();
sl@0
   469
  test_setinteger();
sl@0
   470
  test_keyfiletodata();
sl@0
   471
  test_setcomment();
sl@0
   472
  test_setlocale();
sl@0
   473
  
sl@0
   474
  #ifdef SYMBIAN
sl@0
   475
  testResultXml("keyfile_test");
sl@0
   476
  #endif /* EMULATOR */
sl@0
   477
   
sl@0
   478
  #ifdef SYMBIAN
sl@0
   479
  testResultXml("keyfile_test");
sl@0
   480
  #endif /* EMULATOR */
sl@0
   481
  
sl@0
   482
  return 0;
sl@0
   483
}