2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/Subprocess.h"
20 #include <sys/types.h>
23 #include <boost/container/flat_set.hpp>
24 #include <glog/logging.h>
25 #include <gtest/gtest.h>
27 #include "folly/Exception.h"
28 #include "folly/Format.h"
29 #include "folly/experimental/Gen.h"
30 #include "folly/experimental/FileGen.h"
31 #include "folly/experimental/StringGen.h"
32 #include "folly/experimental/io/FsUtil.h"
34 using namespace folly;
36 TEST(SimpleSubprocessTest, ExitsSuccessfully) {
37 Subprocess proc(std::vector<std::string>{ "/bin/true" });
38 EXPECT_EQ(0, proc.wait().exitStatus());
41 TEST(SimpleSubprocessTest, ExitsSuccessfullyChecked) {
42 Subprocess proc(std::vector<std::string>{ "/bin/true" });
46 TEST(SimpleSubprocessTest, ExitsWithError) {
47 Subprocess proc(std::vector<std::string>{ "/bin/false" });
48 EXPECT_EQ(1, proc.wait().exitStatus());
51 TEST(SimpleSubprocessTest, ExitsWithErrorChecked) {
52 Subprocess proc(std::vector<std::string>{ "/bin/false" });
53 EXPECT_THROW(proc.waitChecked(), CalledProcessError);
56 #define EXPECT_SPAWN_ERROR(err, errMsg, cmd, ...) \
59 Subprocess proc(std::vector<std::string>{ (cmd), ## __VA_ARGS__ }); \
60 ADD_FAILURE() << "expected an error when running " << (cmd); \
61 } catch (const SubprocessSpawnError& ex) { \
62 EXPECT_EQ((err), ex.errnoValue()); \
63 if (StringPiece(ex.what()).find(errMsg) == StringPiece::npos) { \
64 ADD_FAILURE() << "failed to find \"" << (errMsg) << \
65 "\" in exception: \"" << ex.what() << "\""; \
70 TEST(SimpleSubprocessTest, ExecFails) {
71 EXPECT_SPAWN_ERROR(ENOENT, "failed to execute /no/such/file:",
73 EXPECT_SPAWN_ERROR(EACCES, "failed to execute /etc/passwd:",
75 EXPECT_SPAWN_ERROR(ENOTDIR, "failed to execute /etc/passwd/not/a/file:",
76 "/etc/passwd/not/a/file");
79 TEST(SimpleSubprocessTest, ShellExitsSuccesssfully) {
80 Subprocess proc("true");
81 EXPECT_EQ(0, proc.wait().exitStatus());
84 TEST(SimpleSubprocessTest, ShellExitsWithError) {
85 Subprocess proc("false");
86 EXPECT_EQ(1, proc.wait().exitStatus());
90 boost::container::flat_set<int> getOpenFds() {
92 auto dirname = to<std::string>("/proc/", pid, "/fd");
94 boost::container::flat_set<int> fds;
95 for (fs::directory_iterator it(dirname);
96 it != fs::directory_iterator();
98 int fd = to<int>(it->path().filename().native());
104 template<class Runnable>
105 void checkFdLeak(const Runnable& r) {
106 // Get the currently open fds. Check that they are the same both before and
107 // after calling the specified function. We read the open fds from /proc.
108 // (If we wanted to work even on systems that don't have /proc, we could
109 // perhaps create and immediately close a socket both before and after
110 // running the function, and make sure we got the same fd number both times.)
111 auto fdsBefore = getOpenFds();
113 auto fdsAfter = getOpenFds();
114 EXPECT_EQ(fdsAfter.size(), fdsBefore.size());
118 // Make sure Subprocess doesn't leak any file descriptors
119 TEST(SimpleSubprocessTest, FdLeakTest) {
122 Subprocess proc("true");
123 EXPECT_EQ(0, proc.wait().exitStatus());
125 // Normal execution with pipes
127 Subprocess proc("echo foo; echo bar >&2",
128 Subprocess::pipeStdout() | Subprocess::pipeStderr());
129 auto p = proc.communicate(Subprocess::readStdout() |
130 Subprocess::readStderr());
131 EXPECT_EQ("foo\n", p.first);
132 EXPECT_EQ("bar\n", p.second);
136 // Test where the exec call fails()
138 EXPECT_SPAWN_ERROR(ENOENT, "failed to execute", "/no/such/file");
140 // Test where the exec call fails() with pipes
143 Subprocess proc(std::vector<std::string>({"/no/such/file"}),
144 Subprocess::pipeStdout().stderr(Subprocess::PIPE));
145 ADD_FAILURE() << "expected an error when running /no/such/file";
146 } catch (const SubprocessSpawnError& ex) {
147 EXPECT_EQ(ENOENT, ex.errnoValue());
152 TEST(ParentDeathSubprocessTest, ParentDeathSignal) {
153 // Find out where we are.
154 static constexpr size_t pathLength = 2048;
155 char buf[pathLength + 1];
156 int r = readlink("/proc/self/exe", buf, pathLength);
160 fs::path helper(buf);
161 helper.remove_filename();
162 helper /= "subprocess_test_parent_death_helper";
164 fs::path tempFile(fs::temp_directory_path() / fs::unique_path());
166 std::vector<std::string> args {helper.string(), tempFile.string()};
167 Subprocess proc(args);
168 // The helper gets killed by its child, see details in
169 // SubprocessTestParentDeathHelper.cpp
170 ASSERT_EQ(SIGKILL, proc.wait().killSignal());
172 // Now wait for the file to be created, see details in
173 // SubprocessTestParentDeathHelper.cpp
174 while (!fs::exists(tempFile)) {
175 usleep(20000); // 20ms
178 fs::remove(tempFile);
181 TEST(PopenSubprocessTest, PopenRead) {
182 Subprocess proc("ls /", Subprocess::pipeStdout());
184 gen::byLine(proc.stdout()) |
185 [&] (StringPiece line) {
186 if (line == "etc" || line == "bin" || line == "usr") {
194 TEST(CommunicateSubprocessTest, SimpleRead) {
195 Subprocess proc(std::vector<std::string>{ "/bin/echo", "-n", "foo", "bar"},
196 Subprocess::pipeStdout());
197 auto p = proc.communicate();
198 EXPECT_EQ("foo bar", p.first);
202 TEST(CommunicateSubprocessTest, BigWrite) {
203 const int numLines = 1 << 20;
204 std::string line("hello\n");
206 data.reserve(numLines * line.size());
207 for (int i = 0; i < numLines; ++i) {
211 Subprocess proc("wc -l", Subprocess::pipeStdin() | Subprocess::pipeStdout());
212 auto p = proc.communicate(Subprocess::writeStdin() | Subprocess::readStdout(),
214 EXPECT_EQ(folly::format("{}\n", numLines).str(), p.first);
218 TEST(CommunicateSubprocessTest, Duplex) {
219 // Take 10MB of data and pass them through a filter.
220 // One line, as tr is line-buffered
221 const int bytes = 10 << 20;
222 std::string line(bytes, 'x');
224 Subprocess proc("tr a-z A-Z",
225 Subprocess::pipeStdin() | Subprocess::pipeStdout());
226 auto p = proc.communicate(Subprocess::writeStdin() | Subprocess::readStdout(),
228 EXPECT_EQ(bytes, p.first.size());
229 EXPECT_EQ(std::string::npos, p.first.find_first_not_of('X'));