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