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