Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 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 |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 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. |
| 15 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/subprocess.h" |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 5d52780 | 2014-07-18 14:24:13 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 20 | #include <poll.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 23 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 24 | #include <set> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 27 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 28 | #include <base/bind.h> |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 29 | #include <base/files/scoped_temp_dir.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 30 | #include <base/location.h> |
Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 31 | #include <base/message_loop/message_loop.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 32 | #include <base/strings/string_util.h> |
| 33 | #include <base/strings/stringprintf.h> |
| 34 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 35 | #include <brillo/bind_lambda.h> |
| 36 | #include <brillo/message_loops/base_message_loop.h> |
| 37 | #include <brillo/message_loops/message_loop.h> |
| 38 | #include <brillo/message_loops/message_loop_utils.h> |
| 39 | #include <brillo/strings/string_utils.h> |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame^] | 40 | #include <brillo/unittest_utils.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | #include <gtest/gtest.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 42 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 43 | #include "update_engine/common/test_utils.h" |
| 44 | #include "update_engine/common/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 46 | using base::TimeDelta; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 47 | using brillo::MessageLoop; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 48 | using std::string; |
| 49 | using std::vector; |
| 50 | |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 51 | namespace { |
| 52 | |
| 53 | #ifdef __ANDROID__ |
| 54 | #define kBinPath "/system/bin" |
| 55 | #define kUsrBinPath "/system/bin" |
| 56 | #else |
| 57 | #define kBinPath "/bin" |
| 58 | #define kUsrBinPath "/usr/bin" |
| 59 | #endif // __ANDROID__ |
| 60 | |
| 61 | } // namespace |
| 62 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 63 | namespace chromeos_update_engine { |
| 64 | |
| 65 | class SubprocessTest : public ::testing::Test { |
| 66 | protected: |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 67 | void SetUp() override { |
| 68 | loop_.SetAsCurrent(); |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 69 | async_signal_handler_.Init(); |
| 70 | subprocess_.Init(&async_signal_handler_); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 73 | base::MessageLoopForIO base_loop_; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 74 | brillo::BaseMessageLoop loop_{&base_loop_}; |
| 75 | brillo::AsynchronousSignalHandler async_signal_handler_; |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 76 | Subprocess subprocess_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | namespace { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 80 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 81 | void ExpectedResults(int expected_return_code, const string& expected_output, |
| 82 | int return_code, const string& output) { |
| 83 | EXPECT_EQ(expected_return_code, return_code); |
| 84 | EXPECT_EQ(expected_output, output); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 85 | MessageLoop::current()->BreakLoop(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 88 | void ExpectedEnvVars(int return_code, const string& output) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 89 | EXPECT_EQ(0, return_code); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 90 | const std::set<string> allowed_envs = {"LD_LIBRARY_PATH", "PATH"}; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 91 | for (string key_value : brillo::string_utils::Split(output, "\n")) { |
| 92 | auto key_value_pair = brillo::string_utils::SplitAtFirst( |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 93 | key_value, "=", true); |
| 94 | EXPECT_NE(allowed_envs.end(), allowed_envs.find(key_value_pair.first)); |
| 95 | } |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 96 | MessageLoop::current()->BreakLoop(); |
| 97 | } |
| 98 | |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame^] | 99 | void ExpectedDataOnPipe(const Subprocess* subprocess, |
| 100 | pid_t* pid, |
| 101 | int child_fd, |
| 102 | const string& child_fd_data, |
| 103 | int expected_return_code, |
| 104 | int return_code, |
| 105 | const string& /* output */) { |
| 106 | EXPECT_EQ(expected_return_code, return_code); |
| 107 | |
| 108 | // Verify that we can read the data from our end of |child_fd|. |
| 109 | int fd = subprocess->GetPipeFd(*pid, child_fd); |
| 110 | EXPECT_NE(-1, fd); |
| 111 | vector<char> buf(child_fd_data.size() + 1); |
| 112 | EXPECT_EQ(static_cast<ssize_t>(child_fd_data.size()), |
| 113 | HANDLE_EINTR(read(fd, buf.data(), buf.size()))); |
| 114 | EXPECT_EQ(child_fd_data, |
| 115 | string(buf.begin(), buf.begin() + child_fd_data.size())); |
| 116 | |
| 117 | MessageLoop::current()->BreakLoop(); |
| 118 | } |
| 119 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 120 | } // namespace |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 122 | TEST_F(SubprocessTest, IsASingleton) { |
| 123 | EXPECT_EQ(&subprocess_, &Subprocess::Get()); |
| 124 | } |
| 125 | |
| 126 | TEST_F(SubprocessTest, InactiveInstancesDontChangeTheSingleton) { |
| 127 | std::unique_ptr<Subprocess> another_subprocess(new Subprocess()); |
| 128 | EXPECT_EQ(&subprocess_, &Subprocess::Get()); |
| 129 | another_subprocess.reset(); |
| 130 | EXPECT_EQ(&subprocess_, &Subprocess::Get()); |
| 131 | } |
| 132 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 133 | TEST_F(SubprocessTest, SimpleTest) { |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 134 | EXPECT_TRUE(subprocess_.Exec({kBinPath "/false"}, |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 135 | base::Bind(&ExpectedResults, 1, ""))); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 136 | loop_.Run(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 139 | TEST_F(SubprocessTest, EchoTest) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 140 | EXPECT_TRUE(subprocess_.Exec( |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 141 | {kBinPath "/sh", "-c", "echo this is stdout; echo this is stderr >&2"}, |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 142 | base::Bind(&ExpectedResults, 0, "this is stdout\nthis is stderr\n"))); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 143 | loop_.Run(); |
| 144 | } |
| 145 | |
| 146 | TEST_F(SubprocessTest, StderrNotIncludedInOutputTest) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 147 | EXPECT_TRUE(subprocess_.ExecFlags( |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 148 | {kBinPath "/sh", "-c", "echo on stdout; echo on stderr >&2"}, |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 149 | 0, |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame^] | 150 | {}, |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 151 | base::Bind(&ExpectedResults, 0, "on stdout\n"))); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 152 | loop_.Run(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame^] | 155 | TEST_F(SubprocessTest, PipeRedirectFdTest) { |
| 156 | pid_t pid; |
| 157 | pid = subprocess_.ExecFlags( |
| 158 | {kBinPath "/sh", "-c", "echo on pipe >&3"}, |
| 159 | 0, |
| 160 | {3}, |
| 161 | base::Bind(&ExpectedDataOnPipe, &subprocess_, &pid, 3, "on pipe\n", 0)); |
| 162 | EXPECT_NE(0, pid); |
| 163 | |
| 164 | // Wrong file descriptor values should return -1. |
| 165 | EXPECT_EQ(-1, subprocess_.GetPipeFd(pid, 123)); |
| 166 | loop_.Run(); |
| 167 | // Calling GetPipeFd() after the callback runs is invalid. |
| 168 | EXPECT_EQ(-1, subprocess_.GetPipeFd(pid, 3)); |
| 169 | } |
| 170 | |
| 171 | // Test that a pipe file descriptor open in the parent is not open in the child. |
| 172 | TEST_F(SubprocessTest, PipeClosedWhenNotRedirectedTest) { |
| 173 | brillo::ScopedPipe pipe; |
| 174 | const vector<string> cmd = {kBinPath "/sh", "-c", |
| 175 | base::StringPrintf("echo on pipe >/proc/self/fd/%d", pipe.writer)}; |
| 176 | EXPECT_TRUE(subprocess_.ExecFlags( |
| 177 | cmd, |
| 178 | 0, |
| 179 | {}, |
| 180 | base::Bind(&ExpectedResults, 1, ""))); |
| 181 | loop_.Run(); |
| 182 | } |
| 183 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 184 | TEST_F(SubprocessTest, EnvVarsAreFiltered) { |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 185 | EXPECT_TRUE( |
| 186 | subprocess_.Exec({kUsrBinPath "/env"}, base::Bind(&ExpectedEnvVars))); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 187 | loop_.Run(); |
| 188 | } |
| 189 | |
| 190 | TEST_F(SubprocessTest, SynchronousTrueSearchsOnPath) { |
| 191 | int rc = -1; |
| 192 | EXPECT_TRUE(Subprocess::SynchronousExecFlags( |
| 193 | {"true"}, Subprocess::kSearchPath, &rc, nullptr)); |
| 194 | EXPECT_EQ(0, rc); |
| 195 | } |
| 196 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 197 | TEST_F(SubprocessTest, SynchronousEchoTest) { |
| 198 | vector<string> cmd = { |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 199 | kBinPath "/sh", |
| 200 | "-c", |
| 201 | "echo -n stdout-here; echo -n stderr-there >&2"}; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 202 | int rc = -1; |
| 203 | string stdout; |
| 204 | ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, &stdout)); |
| 205 | EXPECT_EQ(0, rc); |
| 206 | EXPECT_EQ("stdout-herestderr-there", stdout); |
| 207 | } |
| 208 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 209 | TEST_F(SubprocessTest, SynchronousEchoNoOutputTest) { |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 210 | int rc = -1; |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 211 | ASSERT_TRUE(Subprocess::SynchronousExec( |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 212 | {kBinPath "/sh", "-c", "echo test"}, &rc, nullptr)); |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 213 | EXPECT_EQ(0, rc); |
| 214 | } |
| 215 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 216 | namespace { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 217 | void CallbackBad(int return_code, const string& output) { |
| 218 | ADD_FAILURE() << "should never be called."; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 219 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 220 | } // namespace |
| 221 | |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 222 | // Test that you can cancel a program that's already running. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 223 | TEST_F(SubprocessTest, CancelTest) { |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 224 | base::ScopedTempDir tempdir; |
| 225 | ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
| 226 | string fifo_path = tempdir.path().Append("fifo").value(); |
| 227 | EXPECT_EQ(0, mkfifo(fifo_path.c_str(), 0666)); |
| 228 | |
| 229 | // Start a process, make sure it is running and try to cancel it. We write |
| 230 | // two bytes to the fifo, the first one marks that the program is running and |
| 231 | // the second one marks that the process waited for a timeout and was not |
| 232 | // killed. We should read the first byte but not the second one. |
| 233 | vector<string> cmd = { |
| 234 | kBinPath "/sh", |
| 235 | "-c", |
| 236 | base::StringPrintf( |
| 237 | "echo -n X >\"%s\"; sleep 60; echo -n Y >\"%s\"; exit 1", |
| 238 | fifo_path.c_str(), |
| 239 | fifo_path.c_str())}; |
| 240 | uint32_t tag = Subprocess::Get().Exec(cmd, base::Bind(&CallbackBad)); |
| 241 | EXPECT_NE(0U, tag); |
| 242 | |
| 243 | int fifo_fd = HANDLE_EINTR(open(fifo_path.c_str(), O_RDONLY)); |
| 244 | EXPECT_GE(fifo_fd, 0); |
| 245 | |
| 246 | loop_.WatchFileDescriptor(FROM_HERE, |
| 247 | fifo_fd, |
| 248 | MessageLoop::WatchMode::kWatchRead, |
| 249 | false, |
| 250 | base::Bind([fifo_fd, tag] { |
| 251 | char c; |
| 252 | EXPECT_EQ(1, HANDLE_EINTR(read(fifo_fd, &c, 1))); |
| 253 | EXPECT_EQ('X', c); |
| 254 | LOG(INFO) << "Killing tag " << tag; |
| 255 | Subprocess::Get().KillExec(tag); |
| 256 | })); |
| 257 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 258 | // This test would leak a callback that runs when the child process exits |
| 259 | // unless we wait for it to run. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 260 | brillo::MessageLoopRunUntil( |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 261 | &loop_, |
Alex Deymo | 279bbab | 2016-02-12 16:26:17 -0800 | [diff] [blame] | 262 | TimeDelta::FromSeconds(120), |
| 263 | base::Bind([] { return Subprocess::Get().subprocess_records_.empty(); })); |
| 264 | EXPECT_TRUE(Subprocess::Get().subprocess_records_.empty()); |
| 265 | // Check that there isn't anything else to read from the pipe. |
| 266 | char c; |
| 267 | EXPECT_EQ(0, HANDLE_EINTR(read(fifo_fd, &c, 1))); |
| 268 | IGNORE_EINTR(close(fifo_fd)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | } // namespace chromeos_update_engine |