2004-08-28 16:21:25 +00:00
|
|
|
/** @file
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
$Date$
|
|
|
|
$Author$
|
|
|
|
|
|
|
|
@copy © Marc Wäckerlin
|
|
|
|
@license LGPL, see file <a href="license.html">COPYING</a>
|
|
|
|
|
|
|
|
$Log$
|
2005-04-19 18:48:00 +00:00
|
|
|
Revision 1.9 2005/04/19 18:48:00 marc
|
|
|
|
new feature PartialExec
|
|
|
|
|
2005-04-07 20:55:21 +00:00
|
|
|
Revision 1.8 2005/04/07 20:55:21 marc
|
|
|
|
Oops, there's a make distcheck...? Now it works.
|
|
|
|
|
2004-12-20 13:21:21 +00:00
|
|
|
Revision 1.7 2004/12/20 13:21:21 marc
|
|
|
|
exception tests: each exception must be in an own test case
|
|
|
|
|
2004-12-14 20:30:10 +00:00
|
|
|
Revision 1.6 2004/12/14 20:30:10 marc
|
|
|
|
added possibility to pass string to stdin of child process
|
|
|
|
|
2004-10-13 10:43:11 +00:00
|
|
|
Revision 1.5 2004/10/13 10:43:11 marc
|
|
|
|
test for bad exception specification
|
|
|
|
|
2004-08-28 16:21:25 +00:00
|
|
|
Revision 1.4 2004/08/28 16:21:25 marc
|
|
|
|
mrw-c++-0.92 (mrw)
|
2011-12-10 11:39:09 +00:00
|
|
|
- new file: version.cxx
|
2004-08-28 16:21:25 +00:00
|
|
|
- new file header for all sources
|
|
|
|
- work around warning in mrw::auto<T>
|
|
|
|
- possibility to compile without log4cxx
|
|
|
|
- work around bugs in demangle.h and libiberty.h
|
|
|
|
- corrections in documentation
|
|
|
|
- added simple tracing mechanism
|
|
|
|
- more warnings
|
|
|
|
- small corrections in Auto<>::Free and a new test for it
|
|
|
|
- possibility to compile without stack trace
|
|
|
|
|
|
|
|
*/
|
2011-12-10 11:39:09 +00:00
|
|
|
#include <mrw/exec.hxx>
|
|
|
|
#include <mrw/stdext.hxx>
|
2004-04-21 19:03:38 +00:00
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/ui/text/TestRunner.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
2011-12-11 14:24:33 +00:00
|
|
|
#include <cppunit/XmlOutputter.h>
|
|
|
|
#include <fstream>
|
2004-04-21 19:03:38 +00:00
|
|
|
#include <string>
|
2005-04-07 20:55:21 +00:00
|
|
|
#include <stdlib.h>
|
2004-04-21 20:24:14 +00:00
|
|
|
|
2004-04-21 19:03:38 +00:00
|
|
|
class ExecTest: public CppUnit::TestFixture {
|
|
|
|
public:
|
|
|
|
void lsTest() {
|
2005-04-07 20:55:21 +00:00
|
|
|
std::string res = (mrw::Cmd("/bin/ls"), "-l",
|
|
|
|
std::string(getenv("srcdir"))+"/..").execute();
|
2004-04-21 19:03:38 +00:00
|
|
|
CPPUNIT_ASSERT(res.find("COPYING")<res.size());
|
|
|
|
}
|
2004-12-14 20:30:10 +00:00
|
|
|
void catTest() {
|
2005-04-07 20:55:21 +00:00
|
|
|
std::string res = mrw::Cmd("/bin/cat").execute("This is a test");
|
2014-03-28 11:50:39 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("This is a test"), res);
|
2004-12-14 20:30:10 +00:00
|
|
|
}
|
2004-12-20 13:21:21 +00:00
|
|
|
void excTest1() {
|
2014-03-28 11:50:39 +00:00
|
|
|
CPPUNIT_ASSERT_THROW((mrw::Cmd("/bin/false")).execute(),
|
|
|
|
mrw::ExecutionFailedExc);
|
2004-12-20 13:21:21 +00:00
|
|
|
}
|
|
|
|
void excTest2() {
|
2014-03-28 11:50:39 +00:00
|
|
|
CPPUNIT_ASSERT_THROW((mrw::Cmd("/bin/false")).execute(""),
|
|
|
|
mrw::ExecutionFailedExc);
|
2004-10-13 10:43:11 +00:00
|
|
|
}
|
2005-04-19 18:48:00 +00:00
|
|
|
void lsTest2() {
|
|
|
|
std::string res;
|
|
|
|
mrw::PartialExec exec = (mrw::Cmd("/bin/ls"), "-l",
|
|
|
|
std::string(getenv("srcdir"))+"/..").start();
|
|
|
|
while (!exec.finished()) res+=exec.read().first;
|
|
|
|
CPPUNIT_ASSERT(res.find("COPYING")<res.size());
|
|
|
|
}
|
|
|
|
void catTest2() {
|
|
|
|
mrw::PartialExec exec = mrw::Cmd("/bin/cat").start(true);
|
|
|
|
std::string res = exec.read("This is a test\n").first;
|
|
|
|
res += exec.read("More to come...\n").first;
|
|
|
|
exec.finish();
|
|
|
|
while (!exec.finished()) res+=exec.read().first;
|
2014-03-28 11:50:39 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("This is a test\nMore to come...\n"), res);
|
2005-04-19 18:48:00 +00:00
|
|
|
}
|
|
|
|
void excTest12() {
|
2014-03-28 11:50:39 +00:00
|
|
|
try {
|
|
|
|
mrw::PartialExec exec = (mrw::Cmd("/bin/false")).start();
|
|
|
|
while (!exec.finished()) exec.read();
|
|
|
|
CPPUNIT_FAIL("Exception expected, shouldm't reach here");
|
|
|
|
} catch (...) {
|
|
|
|
CPPUNIT_ASSERT_THROW(throw, mrw::ExecutionFailedExc);
|
|
|
|
}
|
2005-04-19 18:48:00 +00:00
|
|
|
}
|
|
|
|
void excTest22() {
|
2014-03-28 11:50:39 +00:00
|
|
|
try {
|
|
|
|
mrw::PartialExec exec = (mrw::Cmd("/bin/false")).start(true);
|
|
|
|
while (!exec.finished()) exec.read("xxx");
|
|
|
|
CPPUNIT_FAIL("Exception expected, shouldm't reach here");
|
|
|
|
} catch (...) {
|
|
|
|
CPPUNIT_ASSERT_THROW(throw, mrw::ExecutionFailedExc);
|
|
|
|
}
|
2005-04-19 18:48:00 +00:00
|
|
|
}
|
2018-11-21 15:24:07 +00:00
|
|
|
void unexpectedExc2() {
|
2014-03-28 11:50:39 +00:00
|
|
|
try {
|
|
|
|
mrw::PartialExec exec = (mrw::Cmd("/bin/false")).start();
|
|
|
|
while (!exec.finished()) exec.read();
|
|
|
|
CPPUNIT_FAIL("Exception expected, shouldm't reach here");
|
|
|
|
} catch (...) {
|
|
|
|
CPPUNIT_ASSERT_THROW(throw, mrw::ExecutionFailedExc);
|
|
|
|
}
|
2005-04-19 18:48:00 +00:00
|
|
|
}
|
2004-04-21 19:03:38 +00:00
|
|
|
CPPUNIT_TEST_SUITE(ExecTest);
|
|
|
|
CPPUNIT_TEST(lsTest);
|
2004-12-14 20:30:10 +00:00
|
|
|
CPPUNIT_TEST(catTest);
|
2014-03-28 11:50:39 +00:00
|
|
|
CPPUNIT_TEST(excTest1);
|
|
|
|
CPPUNIT_TEST(excTest2);
|
2005-04-19 18:48:00 +00:00
|
|
|
CPPUNIT_TEST(lsTest2);
|
|
|
|
CPPUNIT_TEST(catTest2);
|
2014-03-28 11:50:39 +00:00
|
|
|
CPPUNIT_TEST(excTest12);
|
|
|
|
//CPPUNIT_TEST(excTest22); /// @bug strange failure from time to time
|
|
|
|
CPPUNIT_TEST(unexpectedExc2);
|
2004-04-21 19:03:38 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
};
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(ExecTest);
|
2004-04-21 06:39:20 +00:00
|
|
|
|
2011-12-11 13:47:45 +00:00
|
|
|
int main(int argc, char** argv) try {
|
|
|
|
std::ofstream ofs((*argv+std::string(".xml")).c_str());
|
2004-04-21 19:03:38 +00:00
|
|
|
CppUnit::TextUi::TestRunner runner;
|
2011-12-11 13:47:45 +00:00
|
|
|
runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), ofs));
|
2004-04-21 19:03:38 +00:00
|
|
|
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
|
|
|
|
return runner.run() ? 0 : 1;
|
2011-12-11 13:47:45 +00:00
|
|
|
} catch (std::exception& e) {
|
|
|
|
std::cerr<<"***Exception: "<<e.what()<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|