Beispiel #1
0
    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);
    }
Beispiel #2
0
    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;
    }