2023-08-31 17:35:12 +02:00
|
|
|
/* Unit tests for bi-directional popen.
|
2019-06-02 20:48:04 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <iostream>
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
#include <villas/popen.hpp>
|
|
|
|
|
|
|
|
using namespace villas::utils;
|
|
|
|
|
2020-09-11 15:16:53 +02:00
|
|
|
// cppcheck-suppress unknownMacro
|
2019-06-02 20:48:04 +02:00
|
|
|
TestSuite(popen, .description = "Bi-directional popen");
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(popen, no_shell) {
|
|
|
|
PopenStream proc("/usr/bin/tee", {"tee", "test"});
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.cout() << "Hello World" << std::endl;
|
|
|
|
proc.cout().flush();
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::string str, str2;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.cin() >> str >> str2;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::cout << str << str2 << std::endl;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(str, "Hello");
|
|
|
|
cr_assert_eq(str2, "World");
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.kill();
|
|
|
|
proc.close();
|
2019-06-23 10:50:35 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(popen, shell) {
|
|
|
|
PopenStream proc("echo \"Hello World\"", {}, {}, std::string(), true);
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::string str, str2;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.cin() >> str >> str2;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(str, "Hello");
|
|
|
|
cr_assert_eq(str2, "World");
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.kill();
|
|
|
|
proc.close();
|
2019-06-02 20:48:04 +02:00
|
|
|
}
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(popen, wd) {
|
|
|
|
PopenStream proc("/usr/bin/pwd", {"pwd"}, {}, "/usr/lib");
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::string wd;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.cin() >> wd;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(wd, "/usr/lib");
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.kill();
|
|
|
|
proc.close();
|
2019-06-23 10:50:35 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(popen, env) {
|
|
|
|
PopenStream proc("echo $MYVAR", {}, {{"MYVAR", "TESTVAL"}}, std::string(),
|
|
|
|
true);
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::string var;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.cin() >> var;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(var, "TESTVAL");
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
proc.kill();
|
|
|
|
proc.close();
|
2019-06-23 10:50:35 +02:00
|
|
|
}
|