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