2018-09-06 13:47:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2017-2018, U.S. Government
|
|
|
|
*/
|
|
|
|
#include "Process.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "log4cplus/configurator.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2018-11-26 21:45:22 +00:00
|
|
|
using hirs::utils::Process;
|
|
|
|
using std::string;
|
|
|
|
using std::stringstream;
|
|
|
|
|
2018-09-06 13:47:33 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ProcessTest : public :: testing::Test {
|
|
|
|
protected:
|
|
|
|
ProcessTest() {
|
|
|
|
// You can do set-up work for each test here.
|
|
|
|
log4cplus::initialize();
|
|
|
|
log4cplus::BasicConfigurator::doConfigure();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ProcessTest() {
|
|
|
|
// You can do clean-up work that doesn't throw exceptions here.
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetUp() {
|
|
|
|
// Code here will be called immediately after the constructor (right
|
|
|
|
// before each test).
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() {
|
|
|
|
// Code here will be called immediately after each test (right
|
|
|
|
// before the destructor).
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, ProcessWorks) {
|
2018-11-26 21:45:22 +00:00
|
|
|
Process p("echo \"Hello World\"");
|
2018-09-06 13:47:33 +00:00
|
|
|
int retVal = p.run();
|
|
|
|
ASSERT_EQ(retVal, 0);
|
|
|
|
ASSERT_EQ("Hello World\n", p.getOutputString());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, ProcessTwoArgConstructorWorks) {
|
2018-11-26 21:45:22 +00:00
|
|
|
Process p("echo", "\"Hello World\"");
|
2018-09-06 13:47:33 +00:00
|
|
|
int retVal = p.run();
|
|
|
|
ASSERT_EQ(retVal, 0);
|
|
|
|
ASSERT_EQ("Hello World\n", p.getOutputString());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, ProcessFailsWithNonZeroReturnValue) {
|
2018-11-26 21:45:22 +00:00
|
|
|
Process p("ls", "isjlfidjsaij");
|
2018-09-06 13:47:33 +00:00
|
|
|
int retVal = p.run();
|
|
|
|
ASSERT_EQ(retVal, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, NonExistentProcessFailsWithNonZeroReturnValue) {
|
2018-11-26 21:45:22 +00:00
|
|
|
Process p("isjlfidjsaij");
|
2018-09-06 13:47:33 +00:00
|
|
|
int retVal = p.run();
|
|
|
|
ASSERT_EQ(retVal, 127);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, NonExistentProcessFailsAndGivesErrorMessage) {
|
2018-11-26 21:45:22 +00:00
|
|
|
Process p("isjlfidjsaij", "ijijdfi");
|
|
|
|
stringstream expectedError;
|
2018-09-06 13:47:33 +00:00
|
|
|
expectedError << "Call to isjlfidjsaij returned 127" << std::endl
|
|
|
|
<< "Is isjlfidjsaij in your path?" << std::endl;
|
2018-11-26 21:45:22 +00:00
|
|
|
string expectedErrorString(expectedError.str());
|
2018-09-06 13:47:33 +00:00
|
|
|
|
2018-11-26 21:45:22 +00:00
|
|
|
stringstream errorStream;
|
2018-09-06 13:47:33 +00:00
|
|
|
int retVal = p.run(errorStream);
|
|
|
|
ASSERT_EQ(retVal, 127);
|
2018-11-26 21:45:22 +00:00
|
|
|
string receivedErrorString(errorStream.str());
|
2018-09-06 13:47:33 +00:00
|
|
|
ASSERT_EQ(receivedErrorString, expectedErrorString);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, SuccessfulProcessDoesNotProduceErrorMessage) {
|
2018-11-26 21:45:22 +00:00
|
|
|
Process p("echo", "\"Hello World\"");
|
2018-09-06 13:47:33 +00:00
|
|
|
|
2018-11-26 21:45:22 +00:00
|
|
|
stringstream errorStream;
|
2018-09-06 13:47:33 +00:00
|
|
|
int retVal = p.run(errorStream);
|
|
|
|
ASSERT_EQ(retVal, 0);
|
2018-11-26 21:45:22 +00:00
|
|
|
string receivedErrorString(errorStream.str());
|
2018-09-06 13:47:33 +00:00
|
|
|
ASSERT_TRUE(receivedErrorString.empty());
|
|
|
|
}
|
|
|
|
|
2018-11-26 21:45:22 +00:00
|
|
|
TEST_F(ProcessTest, ProcessIsRunningSuccessful) {
|
|
|
|
ASSERT_TRUE(Process::isRunning("Process"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, ProcessIsRunningSuccessfulPathBased) {
|
|
|
|
ASSERT_TRUE(Process::isRunning("/opt/Process"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, ProcessIsRunningFalse) {
|
|
|
|
ASSERT_FALSE(Process::isRunning("foobar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, ProcessIsRunningEmptyStringReturnsFalse) {
|
|
|
|
ASSERT_FALSE(Process::isRunning(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProcessTest, ProcessIsRunningPreventCommandHijack) {
|
|
|
|
ASSERT_FALSE(Process::isRunning("foobar; echo blarg"));
|
|
|
|
}
|
|
|
|
|
2018-09-06 13:47:33 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|