sl@0: /* sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: /* The following test function returns 1 only when the any one of the g_spawn_async tests fails. * sl@0: * For pass of all the test the function returns 0. */ sl@0: sl@0: int g_spawn_async_test() sl@0: { sl@0: gchar *working_directory = NULL; sl@0: gchar **envp = NULL; sl@0: gpointer user_data = "123"; sl@0: GError *error = NULL; sl@0: GPid child_pid; sl@0: GSpawnChildSetupFunc child_setup = NULL; sl@0: int retVal = 0; sl@0: sl@0: int flags = 0; sl@0: sl@0: char **argv = NULL; // argv is NULL. should cause g_spawn_async to fail. sl@0: sl@0: argv = (char **)malloc(3*sizeof(char *)); sl@0: argv[0] = "Helloworld1.exe"; // wrong exe name. Should cause g_spawn_async to fail sl@0: argv[1] = "Hello"; sl@0: argv[2] = NULL; sl@0: sl@0: if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error)) sl@0: { sl@0: retVal = 1; sl@0: } sl@0: else sl@0: { sl@0: sl@0: } sl@0: sl@0: argv[0] = "Helloworld.exe"; // set the correct value of argv so that g_spawn_sync is sucessful sl@0: sl@0: if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error)) sl@0: { sl@0: sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: child_setup = NULL; sl@0: sl@0: flags = G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH; sl@0: sl@0: if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error)) sl@0: { sl@0: g_spawn_close_pid(child_pid); sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: sl@0: flags = G_SPAWN_FILE_AND_ARGV_ZERO; sl@0: if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error)) sl@0: { sl@0: sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: working_directory = "c:\\sys\\bin"; sl@0: envp = (char **)malloc(2*sizeof(char *)); sl@0: envp[0] = "path = c:\\sys\\bin"; sl@0: envp[1] = NULL; sl@0: if (g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error)) sl@0: { sl@0: sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: if(envp) sl@0: free(envp); sl@0: if(argv) sl@0: free(argv); sl@0: sl@0: return retVal; sl@0: } sl@0: sl@0: sl@0: /* The following test function returns 1 only when the any one of the g_spawn_async_with_pipes * sl@0: tests fails.For pass of all the test the function returns 0. */ sl@0: int g_spawn_async_with_pipes_test() sl@0: { sl@0: gchar *working_directory = NULL; sl@0: gchar **envp = NULL; sl@0: gpointer user_data = "123"; sl@0: GError *error = NULL; sl@0: GPid child_pid; sl@0: GSpawnChildSetupFunc child_setup = NULL; sl@0: int retVal = 0; sl@0: sl@0: int flags = 0; sl@0: sl@0: int standard_input, standard_output,standard_error; sl@0: sl@0: char **argv = (char **)malloc(3*sizeof(char *)); sl@0: argv[0] = "Helloworld.exe"; // wrong exe name. Should cause g_spawn_async to fail sl@0: argv[1] = "Hello"; sl@0: argv[2] = NULL; sl@0: sl@0: sl@0: if(g_spawn_async_with_pipes(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,NULL,NULL,NULL,&error)) sl@0: { sl@0: sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: flags = G_SPAWN_FILE_AND_ARGV_ZERO; sl@0: sl@0: if(g_spawn_async_with_pipes(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&standard_input,&standard_output,&standard_error,&error)) sl@0: { sl@0: if(standard_input != -1 || standard_output != -1 || standard_error != -1) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: retVal = 1; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: return retVal; sl@0: } sl@0: sl@0: /* The following test function returns 1 only when the any one of the g_spawn_sync * sl@0: tests fails.For pass of all the test the function returns 0. */ sl@0: int g_spawn_sync_test() sl@0: { sl@0: gchar *working_directory = NULL; sl@0: gchar **envp = NULL; sl@0: gpointer user_data = "123"; sl@0: GError *error = NULL; sl@0: int exit_status; sl@0: GPid child_pid; sl@0: GSpawnChildSetupFunc child_setup = NULL; sl@0: int retVal = 0; sl@0: sl@0: int flags = 0; sl@0: sl@0: gchar *standard_output = NULL, *standard_error = NULL; sl@0: sl@0: char **argv = (char **)malloc(3*sizeof(char *)); sl@0: argv[0] = "Helloworld.exe"; sl@0: argv[1] = "Hello"; sl@0: argv[2] = NULL; sl@0: sl@0: flags = G_SPAWN_FILE_AND_ARGV_ZERO; sl@0: sl@0: if(g_spawn_sync(working_directory,argv,envp,flags,NULL,user_data,NULL,NULL,&exit_status,&error)) sl@0: { sl@0: if(exit_status != 0) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: retVal = 1; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: if(g_spawn_sync(working_directory,argv,envp,flags,NULL,user_data,&standard_output,&standard_error,NULL,&error)) sl@0: { sl@0: sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: sl@0: return retVal; sl@0: sl@0: } sl@0: sl@0: sl@0: /* The following test function returns 1 only when the any one of the g_spawn_command_line_async * sl@0: tests fails.For pass of all the test the function returns 0. */ sl@0: int g_spawn_command_line_async_tests() sl@0: { sl@0: GError *error = NULL; sl@0: int retVal = 0; sl@0: sl@0: if(g_spawn_command_line_async("helloworld.exe 1 2 3",&error)) sl@0: { sl@0: sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: return retVal; sl@0: sl@0: } sl@0: sl@0: /* The following test function returns 1 only when the any one of the g_spawn_command_line_sync * sl@0: tests fails.For pass of all the test the function returns 0. */ sl@0: int g_spawn_command_line_sync_tests() sl@0: { sl@0: GError *error = NULL; sl@0: int retVal = 0; sl@0: int exit_status; sl@0: gchar *standard_output, *standard_error; sl@0: sl@0: if(g_spawn_command_line_sync("helloworld.exe 10 11 12",NULL,NULL,&exit_status,&error)) sl@0: { sl@0: if(exit_status != 0) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: retVal = 1; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: sl@0: if(g_spawn_command_line_sync("helloworld.exe 10 11 12",&standard_output,&standard_error,&exit_status,&error)) sl@0: { sl@0: if(exit_status != 0 || standard_output != NULL || standard_error != NULL) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: retVal = 1; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if(error) sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"file %s: line %d Error Message:%s",__FILE__, __LINE__, error->message); sl@0: g_error_free(error); sl@0: error = NULL; sl@0: } sl@0: else sl@0: { sl@0: g_log(NULL,G_LOG_LEVEL_CRITICAL,"Error in file %s: at line %d",__FILE__, __LINE__); sl@0: } sl@0: retVal = 1; sl@0: } sl@0: sl@0: return retVal; sl@0: sl@0: } sl@0: sl@0: sl@0: int main() sl@0: { sl@0: int retval = -1; sl@0: sl@0: #if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__)) sl@0: sl@0: testResultXml("spawn_test"); sl@0: return 0; sl@0: sl@0: #endif // EMULATOR sl@0: sl@0: #ifdef SYMBIAN sl@0: sl@0: 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); sl@0: #endif sl@0: sl@0: // calling g_spawn_close_pid() with some invalid handle. Should not cause panic. sl@0: // Instead the API should return without any problems. sl@0: g_spawn_close_pid(146545); sl@0: sl@0: 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()) sl@0: retval = 1; sl@0: else sl@0: retval = 0; sl@0: sl@0: assert_failed = retval; sl@0: sl@0: #ifdef SYMBIAN sl@0: testResultXml("spawn_test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return retval; sl@0: }