Commit f85d59eb authored by Dominik Charousset's avatar Dominik Charousset

Fix shell escaping in unit tests

parent 7070b868
......@@ -2,6 +2,7 @@
#include "test.hpp"
#include "caf/all.hpp"
#include "caf/string_algorithms.hpp"
using namespace std;
using namespace caf;
......@@ -68,3 +69,21 @@ void set_default_test_settings() {
set_terminate(verbose_terminate);
cout.unsetf(ios_base::unitbuf);
}
std::thread run_program_impl(const char* cpath, std::vector<std::string> args) {
string path = cpath;
replace_all(path, "'", "\\'");
ostringstream oss;
oss << "'" << path << "'";
for (auto& arg : args) {
oss << " " << arg;
}
oss << to_dev_null;
string cmdstr = oss.str();
return thread{[cmdstr] {
if (system(cmdstr.c_str()) != 0) {
CAF_PRINTERR("FATAL: command line failed: " << cmdstr);
abort();
}
}};
}
......@@ -3,6 +3,7 @@
#include <vector>
#include <string>
#include <thread>
#include <cstring>
#include <cstddef>
#include <sstream>
......@@ -199,10 +200,25 @@ caf::optional<T> spro(const std::string& str) {
return caf::none;
}
std::vector<std::string> split(const std::string& str, char delim = ' ',
bool keep_empties = true);
std::thread run_program_impl(const char* path, std::vector<std::string> args);
std::map<std::string, std::string> get_kv_pairs(int argc, char** argv,
int begin = 1);
template <class T>
typename std::enable_if<
std::is_arithmetic<T>::value,
std::string
>::type
convert_to_str(T value) {
return std::to_string(value);
}
inline std::string convert_to_str(std::string value) {
return std::move(value);
}
template <class... Ts>
std::thread run_program(const char* path, Ts&&... args) {
std::vector<std::string> vec{convert_to_str(std::forward<Ts>(args))...};
return run_program_impl(path, std::move(vec));
}
#endif // TEST_HPP
......@@ -24,6 +24,8 @@
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include "caf/string_algorithms.hpp"
using namespace std;
using namespace caf;
using namespace caf::io;
......@@ -157,16 +159,7 @@ void run_server(bool spawn_client, const char* bin_path) {
CAF_CHECKPOINT();
cout << "server is running on port " << port << endl;
if (spawn_client) {
ostringstream oss;
oss << bin_path << " -c " << port << to_dev_null;
thread child{[&oss] {
CAF_LOGC_TRACE("NONE", "main$thread_launcher", "");
auto cmdstr = oss.str();
if (system(cmdstr.c_str()) != 0) {
CAF_PRINTERR("FATAL: command failed: " << cmdstr);
abort();
}
}};
auto child = run_program(bin_path, "-c", port);
CAF_CHECKPOINT();
child.join();
}
......@@ -189,7 +182,6 @@ int main(int argc, char** argv) {
},
on() >> [&] {
run_server(true, argv[0]);
},
others() >> [&] {
cerr << "usage: " << argv[0] << " [-c PORT]" << endl;
......
......@@ -342,7 +342,7 @@ uint16_t at_some_port(uint16_t first_port, F fun) {
}
}
void test_remote_actor(std::string app_path, bool run_remote_actor) {
void test_remote_actor(const char* app_path, bool run_remote_actor) {
scoped_actor self;
auto serv = self->spawn<server, monitored>();
auto publish_serv = [=](uint16_t p) {
......@@ -365,22 +365,11 @@ void test_remote_actor(std::string app_path, bool run_remote_actor) {
CAF_CHECK(serv2 != invalid_actor && !serv2->is_remote());
CAF_CHECK(serv == serv2);
thread child;
ostringstream oss;
oss << app_path << " -c " << port << " " << port0 << " " << gport;
if (run_remote_actor) {
oss << to_dev_null;
// execute client_part() in a separate process,
// connected via localhost socket
child = thread([&oss]() {
CAF_LOGC_TRACE("NONE", "main$thread_launcher", "");
string cmdstr = oss.str();
if (system(cmdstr.c_str()) != 0) {
CAF_PRINTERR("FATAL: command \"" << cmdstr << "\" failed!");
abort();
}
});
child = run_program(app_path, "-c", port, port0, gport);
} else {
CAF_PRINT("please run client: " << oss.str());
CAF_PRINT("please run client with: "
<< "-c " << port << " " << port0 << " " << gport);
}
CAF_CHECKPOINT();
self->receive(
......@@ -391,7 +380,9 @@ void test_remote_actor(std::string app_path, bool run_remote_actor) {
);
// wait until separate process (in sep. thread) finished execution
CAF_CHECKPOINT();
if (run_remote_actor) child.join();
if (run_remote_actor) {
child.join();
}
CAF_CHECKPOINT();
self->await_all_other_actors_done();
}
......
......@@ -99,18 +99,9 @@ int main(int argc, char** argv) {
on() >> [&] {
auto port = run_server();
CAF_CHECKPOINT();
ostringstream oss;
oss << argv[0] << " -c " << port << to_dev_null;
// execute client_part() in a separate process,
// connected via localhost socket
auto child = thread([&oss]() {
CAF_LOGC_TRACE("NONE", "main$thread_launcher", "");
string cmdstr = oss.str();
if (system(cmdstr.c_str()) != 0) {
CAF_PRINTERR("FATAL: command \"" << cmdstr << "\" failed!");
abort();
}
});
auto child = run_program(argv[0], "-c", port);
CAF_CHECKPOINT();
child.join();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment