os/ossrv/glib/tsrc/BC/src/spawn_test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/src/spawn_test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,428 @@
     1.4 +/*
     1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.6 +*
     1.7 +* This library is free software; you can redistribute it and/or
     1.8 +* modify it under the terms of the GNU Lesser General Public
     1.9 +* License as published by the Free Software Foundation; either
    1.10 +* version 2 of the License, or (at your option) any later version.
    1.11 +*
    1.12 +* This library is distributed in the hope that it will be useful,
    1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 +* Lesser General Public License for more details.
    1.16 +*
    1.17 +* You should have received a copy of the GNU Lesser General Public
    1.18 +* License along with this library; if not, write to the
    1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    1.20 +* Boston, MA 02111-1307, USA.
    1.21 +*
    1.22 +* Description:
    1.23 +*
    1.24 +*/
    1.25 +
    1.26 +
    1.27 +
    1.28 +#undef G_DISABLE_ASSERT
    1.29 +#undef G_LOG_DOMAIN
    1.30 +
    1.31 +#include <stdio.h>
    1.32 +#include <glib.h>
    1.33 +#include <gspawn.h>
    1.34 +#include <stdlib.h>
    1.35 +
    1.36 +#ifdef SYMBIAN
    1.37 +#include "mrt2_glib2_test.h"
    1.38 +#endif /*SYMBIAN*/
    1.39 +
    1.40 +/* The following test function returns 1 only when the any one of the g_spawn_async tests fails. *
    1.41 + * For pass of all the test the function returns 0.												 */
    1.42 +
    1.43 +int g_spawn_async_test()
    1.44 +{
    1.45 +	gchar *working_directory = NULL;
    1.46 +    gchar **envp = NULL;
    1.47 +    gpointer user_data = "123";
    1.48 +	GError *error = NULL;
    1.49 +	GPid child_pid;
    1.50 +    GSpawnChildSetupFunc child_setup = NULL;
    1.51 +    int retVal = 0;
    1.52 +    
    1.53 +    int flags = 0;
    1.54 +    
    1.55 +    char **argv = NULL; // argv is NULL. should cause g_spawn_async to fail.
    1.56 +     
    1.57 +	argv = (char **)malloc(3*sizeof(char *));    
    1.58 +	argv[0] = "Helloworld1.exe"; // wrong exe name. Should cause g_spawn_async to fail
    1.59 +    argv[1] = "Hello";
    1.60 +    argv[2] = NULL;
    1.61 +	
    1.62 +	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
    1.63 +	{
    1.64 +	 	retVal = 1;
    1.65 +	}
    1.66 +	else
    1.67 +	{
    1.68 +		
    1.69 +	}	
    1.70 +
    1.71 +	argv[0] = "Helloworld.exe"; // set the correct value of argv so that g_spawn_sync is sucessful
    1.72 +	
    1.73 +	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
    1.74 +	{
    1.75 +	
    1.76 +	}
    1.77 +	else
    1.78 +	{
    1.79 +		if(error)
    1.80 +		{
    1.81 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
    1.82 +			g_error_free(error);
    1.83 +			error = NULL;
    1.84 +		}
    1.85 +		else
    1.86 +		{
    1.87 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
    1.88 +		}
    1.89 +		retVal = 1;
    1.90 +	}	
    1.91 +	
    1.92 +	child_setup = NULL;
    1.93 +	
    1.94 +	flags = G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH;
    1.95 +	
    1.96 +	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
    1.97 +	{
    1.98 +		g_spawn_close_pid(child_pid);
    1.99 +	}
   1.100 +	else
   1.101 +	{
   1.102 +		if(error)
   1.103 +		{
   1.104 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.105 +			g_error_free(error);
   1.106 +			error = NULL;
   1.107 +		}
   1.108 +		else
   1.109 +		{
   1.110 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.111 +		}
   1.112 +		retVal = 1;
   1.113 +	}	
   1.114 +	
   1.115 +	
   1.116 +	flags = G_SPAWN_FILE_AND_ARGV_ZERO;
   1.117 +	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
   1.118 +	{
   1.119 +	
   1.120 +	}
   1.121 +	else
   1.122 +	{
   1.123 +		if(error)
   1.124 +		{
   1.125 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.126 +			g_error_free(error);
   1.127 +			error = NULL;
   1.128 +		}
   1.129 +		else
   1.130 +		{
   1.131 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.132 +		}
   1.133 +		retVal = 1;
   1.134 +	}	
   1.135 +	
   1.136 +	working_directory = "c:\\sys\\bin";
   1.137 +	envp = (char **)malloc(2*sizeof(char *));
   1.138 +	envp[0] = "path = c:\\sys\\bin";
   1.139 +	envp[1] = NULL;
   1.140 +	if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error))
   1.141 +	{
   1.142 +	
   1.143 +	}
   1.144 +	else
   1.145 +	{
   1.146 +		if(error)
   1.147 +		{
   1.148 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.149 +			g_error_free(error);
   1.150 +			error = NULL;
   1.151 +		}
   1.152 +		else
   1.153 +		{
   1.154 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.155 +		}
   1.156 +		retVal = 1;
   1.157 +	}
   1.158 +	
   1.159 +	if(envp)
   1.160 +		free(envp);
   1.161 +	if(argv)
   1.162 +		free(argv);
   1.163 +	
   1.164 +	return retVal;
   1.165 +}
   1.166 +
   1.167 +
   1.168 +/* The following test function returns 1 only when the any one of the g_spawn_async_with_pipes   *
   1.169 +   tests fails.For pass of all the test the function returns 0.									 */
   1.170 +int g_spawn_async_with_pipes_test()
   1.171 +{
   1.172 +	gchar *working_directory = NULL;
   1.173 +    gchar **envp = NULL;
   1.174 +    gpointer user_data = "123";
   1.175 +	GError *error = NULL;
   1.176 +	GPid child_pid;
   1.177 +    GSpawnChildSetupFunc child_setup = NULL;
   1.178 +    int retVal = 0;
   1.179 +    
   1.180 +    int flags = 0;
   1.181 +    
   1.182 +    int standard_input, standard_output,standard_error;
   1.183 +    
   1.184 +    char **argv = (char **)malloc(3*sizeof(char *));    
   1.185 +	argv[0] = "Helloworld.exe"; // wrong exe name. Should cause g_spawn_async to fail
   1.186 +    argv[1] = "Hello";
   1.187 +    argv[2] = NULL;
   1.188 +    
   1.189 +    
   1.190 +    if(g_spawn_async_with_pipes(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,NULL,NULL,NULL,&error))
   1.191 +    {
   1.192 +	
   1.193 +	}
   1.194 +	else
   1.195 +	{
   1.196 +		if(error)
   1.197 +		{
   1.198 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.199 +			g_error_free(error);
   1.200 +			error = NULL;
   1.201 +		}
   1.202 +		else
   1.203 +		{
   1.204 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.205 +		}
   1.206 +		retVal = 1;
   1.207 +	}
   1.208 +   	
   1.209 +   	flags = G_SPAWN_FILE_AND_ARGV_ZERO;
   1.210 +   	
   1.211 +   	if(g_spawn_async_with_pipes(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&standard_input,&standard_output,&standard_error,&error))
   1.212 +    {
   1.213 +    	if(standard_input != -1 || standard_output != -1 || standard_error != -1)
   1.214 +    	{
   1.215 +    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.216 +    		retVal = 1;
   1.217 +    	}
   1.218 +	}
   1.219 +	else
   1.220 +	{
   1.221 +		if(error)
   1.222 +		{
   1.223 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.224 +			g_error_free(error);
   1.225 +			error = NULL;
   1.226 +		}
   1.227 +		else
   1.228 +		{
   1.229 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.230 +		}
   1.231 +		retVal = 1;
   1.232 +	}
   1.233 +   	return retVal; 
   1.234 +}
   1.235 +
   1.236 +/* The following test function returns 1 only when the any one of the g_spawn_sync			     *
   1.237 +   tests fails.For pass of all the test the function returns 0.									 */
   1.238 +int g_spawn_sync_test()
   1.239 +{
   1.240 +	gchar *working_directory = NULL;
   1.241 +    gchar **envp = NULL;
   1.242 +    gpointer user_data = "123";
   1.243 +	GError *error = NULL;
   1.244 +	int exit_status;
   1.245 +	GPid child_pid;
   1.246 +    GSpawnChildSetupFunc child_setup = NULL;
   1.247 +    int retVal = 0;
   1.248 +    
   1.249 +    int flags = 0;
   1.250 +    
   1.251 +    gchar *standard_output = NULL, *standard_error = NULL;
   1.252 +    
   1.253 +    char **argv = (char **)malloc(3*sizeof(char *));    
   1.254 +	argv[0] = "Helloworld.exe"; 
   1.255 +    argv[1] = "Hello";
   1.256 +    argv[2] = NULL;
   1.257 +    
   1.258 +    flags = G_SPAWN_FILE_AND_ARGV_ZERO;
   1.259 +    
   1.260 +    if(g_spawn_sync(working_directory,argv,envp,flags,NULL,user_data,NULL,NULL,&exit_status,&error))
   1.261 +    {
   1.262 +    	if(exit_status != 0)
   1.263 +    	{
   1.264 +    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.265 +    		retVal = 1;
   1.266 +    	}
   1.267 +    }
   1.268 +	else
   1.269 +	{
   1.270 +		if(error)
   1.271 +		{
   1.272 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.273 +			g_error_free(error);
   1.274 +			error = NULL;
   1.275 +		}
   1.276 +		else
   1.277 +		{
   1.278 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.279 +		}
   1.280 +		retVal = 1;
   1.281 +	}
   1.282 +	
   1.283 +	if(g_spawn_sync(working_directory,argv,envp,flags,NULL,user_data,&standard_output,&standard_error,NULL,&error))
   1.284 +    {
   1.285 +    
   1.286 +   	}
   1.287 +	else
   1.288 +	{
   1.289 +		if(error)
   1.290 +		{
   1.291 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.292 +			g_error_free(error);
   1.293 +			error = NULL;
   1.294 +		}
   1.295 +		else
   1.296 +		{
   1.297 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.298 +		}
   1.299 +		retVal = 1;
   1.300 +	}
   1.301 +   	
   1.302 +   	
   1.303 +   	return retVal; 
   1.304 +    
   1.305 +}
   1.306 +
   1.307 +
   1.308 +/* The following test function returns 1 only when the any one of the g_spawn_command_line_async *
   1.309 +   tests fails.For pass of all the test the function returns 0.									 */
   1.310 +int g_spawn_command_line_async_tests()
   1.311 +{
   1.312 +	GError *error = NULL;
   1.313 +	int retVal = 0;
   1.314 +	
   1.315 +	if(g_spawn_command_line_async("helloworld.exe 1 2 3",&error))
   1.316 +	{
   1.317 +    
   1.318 +   	}
   1.319 +	else
   1.320 +	{
   1.321 +		if(error)
   1.322 +		{
   1.323 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.324 +			g_error_free(error);
   1.325 +			error = NULL;
   1.326 +		}
   1.327 +		else
   1.328 +		{
   1.329 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.330 +		}
   1.331 +		retVal = 1;
   1.332 +	}
   1.333 +	
   1.334 +	return retVal;
   1.335 +   	
   1.336 +}
   1.337 +
   1.338 +/* The following test function returns 1 only when the any one of the g_spawn_command_line_sync  *
   1.339 +   tests fails.For pass of all the test the function returns 0.									 */
   1.340 +int g_spawn_command_line_sync_tests()
   1.341 +{
   1.342 +	GError *error = NULL;
   1.343 +	int retVal = 0;
   1.344 +	int exit_status;
   1.345 +	gchar *standard_output, *standard_error;
   1.346 +	
   1.347 +	if(g_spawn_command_line_sync("helloworld.exe 10 11 12",NULL,NULL,&exit_status,&error))
   1.348 +	{
   1.349 +    	if(exit_status != 0)
   1.350 +    	{
   1.351 +    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.352 +    		retVal = 1;
   1.353 +    	}
   1.354 +   	}
   1.355 +	else
   1.356 +	{
   1.357 +		if(error)
   1.358 +		{
   1.359 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.360 +			g_error_free(error);
   1.361 +			error = NULL;
   1.362 +		}
   1.363 +		else
   1.364 +		{
   1.365 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.366 +		}
   1.367 +		retVal = 1;
   1.368 +	}
   1.369 +	
   1.370 +	
   1.371 +	if(g_spawn_command_line_sync("helloworld.exe 10 11 12",&standard_output,&standard_error,&exit_status,&error))
   1.372 +	{
   1.373 +    	if(exit_status != 0 || standard_output != NULL || standard_error != NULL)
   1.374 +    	{
   1.375 +    		g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.376 +    		retVal = 1;
   1.377 +    	}
   1.378 +   	}
   1.379 +	else
   1.380 +	{
   1.381 +		if(error)
   1.382 +		{
   1.383 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__,	 __LINE__, error->message);
   1.384 +			g_error_free(error);
   1.385 +			error = NULL;
   1.386 +		}
   1.387 +		else
   1.388 +		{
   1.389 +			g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__);
   1.390 +		}
   1.391 +		retVal = 1;
   1.392 +	}
   1.393 +	
   1.394 +	return retVal;
   1.395 +   	
   1.396 +}
   1.397 +
   1.398 +
   1.399 +int main()
   1.400 +{
   1.401 +	int retval = -1;
   1.402 +	
   1.403 +	#if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__))
   1.404 +     
   1.405 +   	testResultXml("spawn_test");
   1.406 +   	return 0;
   1.407 +  
   1.408 +  	#endif // EMULATOR
   1.409 +	
   1.410 +	#ifdef SYMBIAN
   1.411 +
   1.412 +  	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);
   1.413 +  	#endif
   1.414 +  	
   1.415 +    // calling g_spawn_close_pid() with some invalid handle. Should not cause panic.
   1.416 +    // Instead the API should return without any problems.
   1.417 +    g_spawn_close_pid(146545);
   1.418 +
   1.419 +	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())
   1.420 +		retval = 1;
   1.421 +	else
   1.422 +		retval = 0;
   1.423 +	
   1.424 +	assert_failed = retval;
   1.425 +	
   1.426 +	#ifdef SYMBIAN
   1.427 +  	testResultXml("spawn_test");
   1.428 +  	#endif /* EMULATOR */
   1.429 +	
   1.430 +	return retval;
   1.431 +}