void testCaptureOutput() { OsStatus stat; // this command will produce a mix of stdout and stderr UtlString appName = "ls"; UtlString params[10]; params[0] = "-d"; params[1] = "/madeUpNameDoesNotExist"; params[2] = "."; params[3] = NULL; OsProcess process; OsPath startupDir = "."; // std::cout << "Launching process: " << appName.data() << std::endl; OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "launching process %s %s", appName.data(), params[0].data()); stat = process.launch(appName, params, startupDir, OsProcessBase::NormalPriorityClass, false, false/* don't ignore child signals*/); CPPUNIT_ASSERT(stat == OS_SUCCESS); CPPUNIT_ASSERT(process.isRunning()); UtlString stdoutMsg, stderrMsg; int rc; // can't guarantee in what order we'll get the output, just check for both bool bGotStdout = false; bool bGotStderr = false; OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "calling getOutput"); while ( (rc = process.getOutput(&stdoutMsg, &stderrMsg)) > 0 ) { if ( stdoutMsg.length() > 0 ) { OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "got stdout: %s", stdoutMsg.data()); bGotStdout = true; } if ( stderrMsg.length() > 0 ) { OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "got stderr: %s", stderrMsg.data()); bGotStderr = true; } } CPPUNIT_ASSERT(bGotStdout==true); CPPUNIT_ASSERT(bGotStderr==true); CPPUNIT_ASSERT(rc == 0); // since we forced an invalid command, we expect a non-zero return code rc = process.wait(0); CPPUNIT_ASSERT(rc != 0); }
void testLaunch() { OsStatus stat; UtlString appName = "ping"; UtlString params[10]; params[0] = "127.0.0.1"; #ifdef _WIN32 //need to do this only on win32, linux already does this by default params[1] = "-t"; #endif OsProcess process; UtlString envKey = "TESTKEY1"; UtlString envValue = "TESTVALUE1"; process.setEnv(envKey,envValue); envKey = "TESTKEY2"; envValue ="TESTVALUE2"; process.setEnv(envKey,envValue); envKey = "TESTKEY3"; envValue = "TESTVALUE3"; process.setEnv(envKey,envValue); OsPath startupDir = "."; //std::cout << "Launching process: " << appName.data() << std::endl; stat = process.launch(appName,params,startupDir); CPPUNIT_ASSERT_MESSAGE("Launched application", stat == OS_SUCCESS); CPPUNIT_ASSERT_MESSAGE("Application running", process.isRunning()); int priority; process.setPriority(1); process.getPriority(priority); KNOWN_BUG("INTERMITTENT on F8 with 64Bit changes", "XECS-480"); CPPUNIT_ASSERT_MESSAGE("Set priority ok", priority == 1); OsProcess newProcess; stat = OsProcess::getByPID(process.getPID(), newProcess); CPPUNIT_ASSERT_MESSAGE("Got process pid ok", stat == OS_SUCCESS); //std::cout << "Waiting 5 secs before killing process..." << std::endl; OsTask::delay(5000); stat = newProcess.kill(); CPPUNIT_ASSERT_MESSAGE("Able to kill process", stat == OS_SUCCESS); }
OsStatus TestProcessClass() { OsStatus retval = OS_FAILED; cout << "Starting Process Class Method Test...\n"; //try to launch dir UtlString appName = "ping"; UtlString params[10]; params[0] = "127.0.0.1"; #ifdef _WIN32 //need to do this only on win32, linux already does this by default params[1] = "-t"; #endif //try and launch IE UtlString envKey = "TESTKEY1"; UtlString envValue = "TESTVALUE1"; process.setEnv(envKey,envValue); envKey = "TESTKEY2"; envValue ="TESTVALUE2"; process.setEnv(envKey,envValue); envKey = "TESTKEY3"; envValue = "TESTVALUE3"; process.setEnv(envKey,envValue); OsPath startupDir = "."; cout << "Launching process: " << appName.data() << endl; if (process.launch(appName,params,startupDir) == OS_SUCCESS) { if (process.isRunning()) { cout << "Successful launch. " << endl; //try to set the prio to 1 int prio; process.getPriority(prio); cout << "Current priority = " << prio << endl; process.setPriority(1); process.getPriority(prio); cout << "New priority (should say 1) = " << prio << endl; OsProcess newProcess; //see if we can get the process we started by pid if (OsProcess::getByPID(process.getPID(),newProcess) == OS_SUCCESS) { //wait a bit cout << "Waiting 5 secs before killing process..." << endl; OsTask::delay(5000); //ok, now kill that bad boy... if (newProcess.kill() == OS_SUCCESS) cout << "Killed\n"; else cout << "ERROR: Could not kill process!\n"; retval = OS_SUCCESS; } } else cout << "ERROR: Process says it's not running!\n"; } else { cout << "ERROR: Could not create process " << appName.data() << endl; } cout << "DONE Process Class Method Test.\n"; return retval; }
void testSendInput() { OsStatus stat; OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "testSendInput"); // this command will produce a mix of stdout and stderr UtlString appName = "sh"; UtlString params[10]; params[0] = "-c"; params[1] = "cat"; params[2] = NULL; OsProcess process; OsPath startupDir = "."; // std::cout << "Launching process: " << appName.data() << std::endl; OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "launching process %s %s", appName.data(), params[0].data()); stat = process.launch(appName, params, startupDir, OsProcessBase::NormalPriorityClass, false, false/* don't ignore child signals*/); CPPUNIT_ASSERT(stat == OS_SUCCESS); CPPUNIT_ASSERT(process.isRunning()); UtlString stdoutMsg, stderrMsg; int rc; // send "well", then "hello", and expect "goodbye" back bool bGotGoodbye = false; OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "starting sendInput task"); SimpleTask * pTask = new SimpleTask(&process); pTask->start(); OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "calling getOutput"); while ( !bGotGoodbye && pTask->isStarted() && (rc = process.getOutput(&stdoutMsg, &stderrMsg)) > 0 ) { if ( stdoutMsg.length() > 0 ) { // The output is sure to contain newlines, and may contain several lines. // Clean it up before dispatching. UtlTokenizer tokenizer(stdoutMsg); UtlString msg; while ( tokenizer.next(msg, "\r\n") ) { OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "got stdout: %s", msg.data()); if ( msg == "goodbye" ) { OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "got goodbye command"); bGotGoodbye = true; } } } if ( stderrMsg.length() > 0 ) { OsSysLog::add(FAC_KERNEL, PRI_DEBUG, "got stderr: %s", stderrMsg.data()); } } CPPUNIT_ASSERT(bGotGoodbye==true); pTask->requestShutdown(); delete pTask; }