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