os/ossrv/glib/tsrc/BC/tests/spawn_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 <gspawn.h>
sl@0
    31
#include <mrtlib.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
/* The following test function returns 1 only when the any one of the g_spawn_async tests fails. *
sl@0
    39
 * For pass of all the test the function returns 0.												 */
sl@0
    40
sl@0
    41
int g_spawn_async_test()
sl@0
    42
{
sl@0
    43
	gchar *working_directory = NULL;
sl@0
    44
    gchar **envp = NULL;
sl@0
    45
    gpointer user_data = "123";
sl@0
    46
	GError *error = NULL;
sl@0
    47
	GPid child_pid;
sl@0
    48
    GSpawnChildSetupFunc child_setup = NULL;
sl@0
    49
    int retVal = 0;
sl@0
    50
    
sl@0
    51
    int flags = 0;
sl@0
    52
    
sl@0
    53
    char **argv = NULL; // argv is NULL. should cause g_spawn_async to fail.
sl@0
    54
     
sl@0
    55
	argv = (char **)malloc(3*sizeof(char *));    
sl@0
    56
	argv[0] = "Helloworld1.exe"; // wrong exe name. Should cause g_spawn_async to fail
sl@0
    57
    argv[1] = "Hello";
sl@0
    58
    argv[2] = NULL;
sl@0
    59
	
sl@0
    60
	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
sl@0
    61
	{
sl@0
    62
	 	retVal = 1;
sl@0
    63
	}
sl@0
    64
	else
sl@0
    65
	{
sl@0
    66
		
sl@0
    67
	}	
sl@0
    68
sl@0
    69
	argv[0] = "Helloworld.exe"; // set the correct value of argv so that g_spawn_sync is sucessful
sl@0
    70
	
sl@0
    71
	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
sl@0
    72
	{
sl@0
    73
	
sl@0
    74
	}
sl@0
    75
	else
sl@0
    76
	{
sl@0
    77
		if(error)
sl@0
    78
		{
sl@0
    79
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
    80
			g_error_free(error);
sl@0
    81
			error = NULL;
sl@0
    82
		}
sl@0
    83
		else
sl@0
    84
		{
sl@0
    85
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
    86
		}
sl@0
    87
		retVal = 1;
sl@0
    88
	}	
sl@0
    89
	
sl@0
    90
	child_setup = NULL;
sl@0
    91
	
sl@0
    92
	flags = G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH;
sl@0
    93
	
sl@0
    94
	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
sl@0
    95
	{
sl@0
    96
		g_spawn_close_pid(child_pid);
sl@0
    97
	}
sl@0
    98
	else
sl@0
    99
	{
sl@0
   100
		if(error)
sl@0
   101
		{
sl@0
   102
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   103
			g_error_free(error);
sl@0
   104
			error = NULL;
sl@0
   105
		}
sl@0
   106
		else
sl@0
   107
		{
sl@0
   108
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   109
		}
sl@0
   110
		retVal = 1;
sl@0
   111
	}	
sl@0
   112
	
sl@0
   113
	
sl@0
   114
	flags = G_SPAWN_FILE_AND_ARGV_ZERO;
sl@0
   115
	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
sl@0
   116
	{
sl@0
   117
	
sl@0
   118
	}
sl@0
   119
	else
sl@0
   120
	{
sl@0
   121
		if(error)
sl@0
   122
		{
sl@0
   123
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   124
			g_error_free(error);
sl@0
   125
			error = NULL;
sl@0
   126
		}
sl@0
   127
		else
sl@0
   128
		{
sl@0
   129
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   130
		}
sl@0
   131
		retVal = 1;
sl@0
   132
	}	
sl@0
   133
	
sl@0
   134
	working_directory = "c:\\sys\\bin";
sl@0
   135
	envp = (char **)malloc(2*sizeof(char *));
sl@0
   136
	envp[0] = "path = c:\\sys\\bin";
sl@0
   137
	envp[1] = NULL;
sl@0
   138
	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
sl@0
   139
	{
sl@0
   140
	
sl@0
   141
	}
sl@0
   142
	else
sl@0
   143
	{
sl@0
   144
		if(error)
sl@0
   145
		{
sl@0
   146
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   147
			g_error_free(error);
sl@0
   148
			error = NULL;
sl@0
   149
		}
sl@0
   150
		else
sl@0
   151
		{
sl@0
   152
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   153
		}
sl@0
   154
		retVal = 1;
sl@0
   155
	}
sl@0
   156
	
sl@0
   157
	if(envp)
sl@0
   158
		free(envp);
sl@0
   159
	if(argv)
sl@0
   160
		free(argv);
sl@0
   161
	
sl@0
   162
	return retVal;
sl@0
   163
}
sl@0
   164
sl@0
   165
sl@0
   166
/* The following test function returns 1 only when the any one of the g_spawn_async_with_pipes   *
sl@0
   167
   tests fails.For pass of all the test the function returns 0.									 */
sl@0
   168
int g_spawn_async_with_pipes_test()
sl@0
   169
{
sl@0
   170
	gchar *working_directory = NULL;
sl@0
   171
    gchar **envp = NULL;
sl@0
   172
    gpointer user_data = "123";
sl@0
   173
	GError *error = NULL;
sl@0
   174
	GPid child_pid;
sl@0
   175
    GSpawnChildSetupFunc child_setup = NULL;
sl@0
   176
    int retVal = 0;
sl@0
   177
    
sl@0
   178
    int flags = 0;
sl@0
   179
    
sl@0
   180
    int standard_input, standard_output,standard_error;
sl@0
   181
    
sl@0
   182
    char **argv = (char **)malloc(3*sizeof(char *));    
sl@0
   183
	argv[0] = "Helloworld.exe"; // wrong exe name. Should cause g_spawn_async to fail
sl@0
   184
    argv[1] = "Hello";
sl@0
   185
    argv[2] = NULL;
sl@0
   186
    
sl@0
   187
    
sl@0
   188
    if(g_spawn_async_with_pipes(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,NULL,NULL,NULL,&error))
sl@0
   189
    {
sl@0
   190
	
sl@0
   191
	}
sl@0
   192
	else
sl@0
   193
	{
sl@0
   194
		if(error)
sl@0
   195
		{
sl@0
   196
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   197
			g_error_free(error);
sl@0
   198
			error = NULL;
sl@0
   199
		}
sl@0
   200
		else
sl@0
   201
		{
sl@0
   202
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   203
		}
sl@0
   204
		retVal = 1;
sl@0
   205
	}
sl@0
   206
   	
sl@0
   207
   	flags = G_SPAWN_FILE_AND_ARGV_ZERO;
sl@0
   208
   	
sl@0
   209
   	if(g_spawn_async_with_pipes(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&standard_input,&standard_output,&standard_error,&error))
sl@0
   210
    {
sl@0
   211
    	if(standard_input != -1 || standard_output != -1 || standard_error != -1)
sl@0
   212
    	{
sl@0
   213
    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   214
    		retVal = 1;
sl@0
   215
    	}
sl@0
   216
	}
sl@0
   217
	else
sl@0
   218
	{
sl@0
   219
		if(error)
sl@0
   220
		{
sl@0
   221
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   222
			g_error_free(error);
sl@0
   223
			error = NULL;
sl@0
   224
		}
sl@0
   225
		else
sl@0
   226
		{
sl@0
   227
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   228
		}
sl@0
   229
		retVal = 1;
sl@0
   230
	}
sl@0
   231
   	return retVal; 
sl@0
   232
}
sl@0
   233
sl@0
   234
/* The following test function returns 1 only when the any one of the g_spawn_sync			     *
sl@0
   235
   tests fails.For pass of all the test the function returns 0.									 */
sl@0
   236
int g_spawn_sync_test()
sl@0
   237
{
sl@0
   238
	gchar *working_directory = NULL;
sl@0
   239
    gchar **envp = NULL;
sl@0
   240
    gpointer user_data = "123";
sl@0
   241
	GError *error = NULL;
sl@0
   242
	int exit_status;
sl@0
   243
	GPid child_pid;
sl@0
   244
    GSpawnChildSetupFunc child_setup = NULL;
sl@0
   245
    int retVal = 0;
sl@0
   246
    
sl@0
   247
    int flags = 0;
sl@0
   248
    
sl@0
   249
    gchar *standard_output = NULL, *standard_error = NULL;
sl@0
   250
    
sl@0
   251
    char **argv = (char **)malloc(3*sizeof(char *));    
sl@0
   252
	argv[0] = "Helloworld.exe"; 
sl@0
   253
    argv[1] = "Hello";
sl@0
   254
    argv[2] = NULL;
sl@0
   255
    
sl@0
   256
    flags = G_SPAWN_FILE_AND_ARGV_ZERO;
sl@0
   257
    
sl@0
   258
    if(g_spawn_sync(working_directory,argv,envp,flags,NULL,user_data,NULL,NULL,&exit_status,&error))
sl@0
   259
    {
sl@0
   260
    	if(exit_status != 0)
sl@0
   261
    	{
sl@0
   262
    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   263
    		retVal = 1;
sl@0
   264
    	}
sl@0
   265
    }
sl@0
   266
	else
sl@0
   267
	{
sl@0
   268
		if(error)
sl@0
   269
		{
sl@0
   270
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   271
			g_error_free(error);
sl@0
   272
			error = NULL;
sl@0
   273
		}
sl@0
   274
		else
sl@0
   275
		{
sl@0
   276
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   277
		}
sl@0
   278
		retVal = 1;
sl@0
   279
	}
sl@0
   280
	
sl@0
   281
	if(g_spawn_sync(working_directory,argv,envp,flags,NULL,user_data,&standard_output,&standard_error,NULL,&error))
sl@0
   282
    {
sl@0
   283
    
sl@0
   284
   	}
sl@0
   285
	else
sl@0
   286
	{
sl@0
   287
		if(error)
sl@0
   288
		{
sl@0
   289
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   290
			g_error_free(error);
sl@0
   291
			error = NULL;
sl@0
   292
		}
sl@0
   293
		else
sl@0
   294
		{
sl@0
   295
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   296
		}
sl@0
   297
		retVal = 1;
sl@0
   298
	}
sl@0
   299
   	
sl@0
   300
   	
sl@0
   301
   	return retVal; 
sl@0
   302
    
sl@0
   303
}
sl@0
   304
sl@0
   305
sl@0
   306
/* The following test function returns 1 only when the any one of the g_spawn_command_line_async *
sl@0
   307
   tests fails.For pass of all the test the function returns 0.									 */
sl@0
   308
int g_spawn_command_line_async_tests()
sl@0
   309
{
sl@0
   310
	GError *error = NULL;
sl@0
   311
	int retVal = 0;
sl@0
   312
	
sl@0
   313
	if(g_spawn_command_line_async("helloworld.exe 1 2 3",&error))
sl@0
   314
	{
sl@0
   315
    
sl@0
   316
   	}
sl@0
   317
	else
sl@0
   318
	{
sl@0
   319
		if(error)
sl@0
   320
		{
sl@0
   321
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   322
			g_error_free(error);
sl@0
   323
			error = NULL;
sl@0
   324
		}
sl@0
   325
		else
sl@0
   326
		{
sl@0
   327
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   328
		}
sl@0
   329
		retVal = 1;
sl@0
   330
	}
sl@0
   331
	
sl@0
   332
	return retVal;
sl@0
   333
   	
sl@0
   334
}
sl@0
   335
sl@0
   336
/* The following test function returns 1 only when the any one of the g_spawn_command_line_sync  *
sl@0
   337
   tests fails.For pass of all the test the function returns 0.									 */
sl@0
   338
int g_spawn_command_line_sync_tests()
sl@0
   339
{
sl@0
   340
	GError *error = NULL;
sl@0
   341
	int retVal = 0;
sl@0
   342
	int exit_status;
sl@0
   343
	gchar *standard_output, *standard_error;
sl@0
   344
	
sl@0
   345
	if(g_spawn_command_line_sync("helloworld.exe 10 11 12",NULL,NULL,&exit_status,&error))
sl@0
   346
	{
sl@0
   347
    	if(exit_status != 0)
sl@0
   348
    	{
sl@0
   349
    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   350
    		retVal = 1;
sl@0
   351
    	}
sl@0
   352
   	}
sl@0
   353
	else
sl@0
   354
	{
sl@0
   355
		if(error)
sl@0
   356
		{
sl@0
   357
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   358
			g_error_free(error);
sl@0
   359
			error = NULL;
sl@0
   360
		}
sl@0
   361
		else
sl@0
   362
		{
sl@0
   363
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   364
		}
sl@0
   365
		retVal = 1;
sl@0
   366
	}
sl@0
   367
	
sl@0
   368
	
sl@0
   369
	if(g_spawn_command_line_sync("helloworld.exe 10 11 12",&standard_output,&standard_error,&exit_status,&error))
sl@0
   370
	{
sl@0
   371
    	if(exit_status != 0 || standard_output != NULL || standard_error != NULL)
sl@0
   372
    	{
sl@0
   373
    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   374
    		retVal = 1;
sl@0
   375
    	}
sl@0
   376
   	}
sl@0
   377
	else
sl@0
   378
	{
sl@0
   379
		if(error)
sl@0
   380
		{
sl@0
   381
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
sl@0
   382
			g_error_free(error);
sl@0
   383
			error = NULL;
sl@0
   384
		}
sl@0
   385
		else
sl@0
   386
		{
sl@0
   387
			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
sl@0
   388
		}
sl@0
   389
		retVal = 1;
sl@0
   390
	}
sl@0
   391
	
sl@0
   392
	return retVal;
sl@0
   393
   	
sl@0
   394
}
sl@0
   395
sl@0
   396
sl@0
   397
int main()
sl@0
   398
{
sl@0
   399
	int retval = -1;
sl@0
   400
	
sl@0
   401
	#if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__))
sl@0
   402
     
sl@0
   403
   	testResultXml("spawn_test");
sl@0
   404
   	return 0;
sl@0
   405
  
sl@0
   406
  	#endif // EMULATOR
sl@0
   407
	
sl@0
   408
	#ifdef SYMBIAN
sl@0
   409
	GLIB_INIT();
sl@0
   410
  	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
   411
  	#endif
sl@0
   412
  	
sl@0
   413
    // calling g_spawn_close_pid() with some invalid handle. Should not cause panic.
sl@0
   414
    // Instead the API should return without any problems.
sl@0
   415
    g_spawn_close_pid(146545);
sl@0
   416
sl@0
   417
	if(g_spawn_async_test() || g_spawn_async_with_pipes_test() || g_spawn_sync_test() || g_spawn_command_line_async_tests() || g_spawn_command_line_sync_tests())
sl@0
   418
		retval = 1;
sl@0
   419
	else
sl@0
   420
		retval = 0;
sl@0
   421
	
sl@0
   422
	assert_failed = retval;
sl@0
   423
	
sl@0
   424
	#ifdef SYMBIAN
sl@0
   425
  	testResultXml("spawn_test");
sl@0
   426
  	#endif /* EMULATOR */
sl@0
   427
	
sl@0
   428
	return retval;
sl@0
   429
}