GH #1222 Escape command line arguments passed to Process::launch() on Windows

This commit is contained in:
Tony Abbott
2016-04-15 16:45:41 +02:00
parent 8fd1065804
commit 1db7811ce5
5 changed files with 176 additions and 3 deletions

View File

@@ -147,6 +147,66 @@ void ProcessTest::testLaunchEnv()
}
void ProcessTest::testLaunchArgs()
{
#if !defined(_WIN32_WCE)
std::string name("TestApp");
std::string cmd;
#if defined(POCO_OS_FAMILY_UNIX)
cmd = "./";
cmd += name;
#else
cmd = name;
#endif
std::vector<std::string> args;
args.push_back("-echo-args");
args.push_back("simple");
args.push_back("with space");
args.push_back("with\ttab");
args.push_back("with\vverticaltab");
// can't test newline here because TestApp -echo-args uses newline to separate the echoed args
//args.push_back("with\nnewline");
args.push_back("with \" quotes");
args.push_back("ends with \"quotes\"");
args.push_back("\"starts\" with quotes");
args.push_back("\"");
args.push_back("\\");
args.push_back("c:\\program files\\ends with backslash\\");
args.push_back("\"already quoted \\\" \\\\\"");
Pipe outPipe;
ProcessHandle ph = Process::launch(cmd, args, 0, &outPipe, 0);
PipeInputStream istr(outPipe);
std::string receivedArg;
int c = istr.get();
int argNumber = 1;
while (c != -1)
{
if ('\n' == c)
{
assert(argNumber < args.size());
std::string expectedArg = args[argNumber];
if (expectedArg.npos != expectedArg.find("already quoted")) {
expectedArg = "already quoted \" \\";
}
assert(receivedArg == expectedArg);
++argNumber;
receivedArg = "";
}
else if ('\r' != c)
{
receivedArg += (char)c;
}
c = istr.get();
}
assert(argNumber == args.size());
int rc = ph.wait();
assert(rc == args.size());
#endif // !defined(_WIN32_WCE)
}
void ProcessTest::testIsRunning()
{
#if !defined(_WIN32_WCE)
@@ -234,6 +294,7 @@ CppUnit::Test* ProcessTest::suite()
CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectIn);
CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectOut);
CppUnit_addTest(pSuite, ProcessTest, testLaunchEnv);
CppUnit_addTest(pSuite, ProcessTest, testLaunchArgs);
CppUnit_addTest(pSuite, ProcessTest, testIsRunning);
CppUnit_addTest(pSuite, ProcessTest, testIsRunningAllowsForTermination);
CppUnit_addTest(pSuite, ProcessTest, testSignalExitCode);