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