Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "update_engine/subprocess.h" |
| 6 | |
Alex Deymo | 5d52780 | 2014-07-18 14:24:13 -0700 | [diff] [blame] | 7 | #include <fcntl.h> |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 8 | #include <netinet/in.h> |
| 9 | #include <netinet/ip.h> |
| 10 | #include <poll.h> |
| 11 | #include <sys/socket.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 12 | #include <sys/stat.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 15 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 19 | #include <base/bind.h> |
| 20 | #include <base/location.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 21 | #include <base/strings/string_util.h> |
| 22 | #include <base/strings/stringprintf.h> |
| 23 | #include <base/time/time.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 24 | #include <chromeos/bind_lambda.h> |
| 25 | #include <chromeos/message_loops/glib_message_loop.h> |
| 26 | #include <chromeos/message_loops/message_loop.h> |
| 27 | #include <chromeos/message_loops/message_loop_utils.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 28 | #include <glib.h> |
| 29 | #include <gtest/gtest.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 30 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 31 | #include "update_engine/test_utils.h" |
| 32 | #include "update_engine/utils.h" |
| 33 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 34 | using base::TimeDelta; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 35 | using chromeos::MessageLoop; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | using std::string; |
| 37 | using std::vector; |
| 38 | |
| 39 | namespace chromeos_update_engine { |
| 40 | |
| 41 | class SubprocessTest : public ::testing::Test { |
| 42 | protected: |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 43 | void SetUp() override { |
| 44 | loop_.SetAsCurrent(); |
| 45 | } |
| 46 | |
| 47 | void TearDown() override { |
| 48 | EXPECT_EQ(0, chromeos::MessageLoopRunMaxIterations(&loop_, 1)); |
| 49 | } |
| 50 | |
| 51 | // TODO(deymo): Replace this with a FakeMessageLoop. Subprocess uses glib to |
| 52 | // asynchronously spawn a process, so we need to run a GlibMessageLoop here. |
| 53 | chromeos::GlibMessageLoop loop_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | namespace { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 57 | int local_server_port = 0; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 58 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 59 | void Callback(int return_code, const string& output, void* /* unused */) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 60 | EXPECT_EQ(1, return_code); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 61 | MessageLoop::current()->BreakLoop(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 64 | void CallbackEcho(int return_code, const string& output, void* /* unused */) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 65 | EXPECT_EQ(0, return_code); |
| 66 | EXPECT_NE(string::npos, output.find("this is stdout")); |
| 67 | EXPECT_NE(string::npos, output.find("this is stderr")); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 68 | MessageLoop::current()->BreakLoop(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame^] | 71 | void CallbackStdoutOnlyEcho(int return_code, |
| 72 | const string& output, |
| 73 | void* /* unused */) { |
| 74 | EXPECT_EQ(0, return_code); |
| 75 | EXPECT_NE(string::npos, output.find("on stdout")); |
| 76 | EXPECT_EQ(string::npos, output.find("on stderr")); |
| 77 | MessageLoop::current()->BreakLoop(); |
| 78 | } |
| 79 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 80 | } // namespace |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 81 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 82 | TEST_F(SubprocessTest, SimpleTest) { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame^] | 83 | Subprocess::Get().Exec(vector<string>{"/bin/false"}, Callback, nullptr); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 84 | loop_.Run(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 87 | TEST_F(SubprocessTest, EchoTest) { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame^] | 88 | Subprocess::Get().Exec( |
| 89 | vector<string>{ |
| 90 | "/bin/sh", |
| 91 | "-c", |
| 92 | "echo this is stdout; echo this is stderr > /dev/stderr"}, |
| 93 | CallbackEcho, |
| 94 | nullptr); |
| 95 | loop_.Run(); |
| 96 | } |
| 97 | |
| 98 | TEST_F(SubprocessTest, StderrNotIncludedInOutputTest) { |
| 99 | Subprocess::Get().ExecFlags( |
| 100 | vector<string>{"/bin/sh", "-c", "echo on stdout; echo on stderr >&2"}, |
| 101 | static_cast<GSpawnFlags>(0), |
| 102 | false, // don't redirect stderr |
| 103 | CallbackStdoutOnlyEcho, |
| 104 | nullptr); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 105 | loop_.Run(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 108 | TEST_F(SubprocessTest, SynchronousEchoTest) { |
| 109 | vector<string> cmd = { |
| 110 | "/bin/sh", |
| 111 | "-c", |
| 112 | "echo -n stdout-here; echo -n stderr-there > /dev/stderr"}; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 113 | int rc = -1; |
| 114 | string stdout; |
| 115 | ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, &stdout)); |
| 116 | EXPECT_EQ(0, rc); |
| 117 | EXPECT_EQ("stdout-herestderr-there", stdout); |
| 118 | } |
| 119 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 120 | TEST_F(SubprocessTest, SynchronousEchoNoOutputTest) { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame^] | 121 | vector<string> cmd = {"/bin/sh", "-c", "echo test"}; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 122 | int rc = -1; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, nullptr)); |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 124 | EXPECT_EQ(0, rc); |
| 125 | } |
| 126 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 127 | namespace { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 128 | void CallbackBad(int return_code, const string& output, void* p) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 129 | CHECK(false) << "should never be called."; |
| 130 | } |
| 131 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 132 | // TODO(garnold) this test method uses test_http_server as a representative for |
| 133 | // interactive processes that can be spawned/terminated at will. This causes us |
| 134 | // to go through hoops when spawning this process (e.g. obtaining the port |
| 135 | // number it uses so we can control it with wget). It would have been much |
| 136 | // preferred to use something else and thus simplify both test_http_server |
| 137 | // (doesn't have to be able to communicate through a temp file) and the test |
| 138 | // code below; for example, it sounds like a brain dead sleep loop with proper |
| 139 | // signal handlers could be used instead. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 140 | void StartAndCancelInRunLoop(bool* spawned) { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 141 | // Create a temp file for test_http_server to communicate its port number. |
| 142 | char temp_file_name[] = "/tmp/subprocess_unittest-test_http_server-XXXXXX"; |
| 143 | int temp_fd = mkstemp(temp_file_name); |
| 144 | CHECK_GE(temp_fd, 0); |
| 145 | int temp_flags = fcntl(temp_fd, F_GETFL, 0) | O_NONBLOCK; |
| 146 | CHECK_EQ(fcntl(temp_fd, F_SETFL, temp_flags), 0); |
| 147 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 148 | vector<string> cmd; |
| 149 | cmd.push_back("./test_http_server"); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 150 | cmd.push_back(temp_file_name); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 151 | uint32_t tag = Subprocess::Get().Exec(cmd, CallbackBad, nullptr); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 152 | EXPECT_NE(0, tag); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 153 | *spawned = true; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 154 | printf("test http server spawned\n"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | // Wait for server to be up and running |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 156 | TimeDelta total_wait_time; |
| 157 | const TimeDelta kSleepTime = TimeDelta::FromMilliseconds(100); |
| 158 | const TimeDelta kMaxWaitTime = TimeDelta::FromSeconds(3); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 159 | local_server_port = 0; |
| 160 | static const char* kServerListeningMsgPrefix = "listening on port "; |
| 161 | while (total_wait_time.InMicroseconds() < kMaxWaitTime.InMicroseconds()) { |
| 162 | char line[80]; |
| 163 | int line_len = read(temp_fd, line, sizeof(line) - 1); |
| 164 | if (line_len > 0) { |
| 165 | line[line_len] = '\0'; |
| 166 | CHECK_EQ(strstr(line, kServerListeningMsgPrefix), line); |
| 167 | const char* listening_port_str = |
| 168 | line + strlen(kServerListeningMsgPrefix); |
| 169 | char* end_ptr; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 170 | long raw_port = strtol(listening_port_str, // NOLINT(runtime/int) |
| 171 | &end_ptr, 10); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 172 | CHECK(!*end_ptr || *end_ptr == '\n'); |
| 173 | local_server_port = static_cast<in_port_t>(raw_port); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 174 | break; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 175 | } else if (line_len < 0 && errno != EAGAIN) { |
| 176 | LOG(INFO) << "error reading from " << temp_file_name << ": " |
| 177 | << strerror(errno); |
| 178 | break; |
| 179 | } |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 180 | g_usleep(kSleepTime.InMicroseconds()); |
Darin Petkov | 27fa9c5 | 2010-07-15 15:11:55 -0700 | [diff] [blame] | 181 | total_wait_time += kSleepTime; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 182 | } |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 183 | close(temp_fd); |
| 184 | remove(temp_file_name); |
| 185 | CHECK_GT(local_server_port, 0); |
| 186 | LOG(INFO) << "server listening on port " << local_server_port; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame^] | 187 | Subprocess::Get().KillExec(tag); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 188 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 189 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 190 | void ExitWhenDone(bool* spawned) { |
| 191 | if (*spawned && !Subprocess::Get().SubprocessInFlight()) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 192 | // tear down the sub process |
| 193 | printf("tear down time\n"); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 194 | int status = test_utils::System( |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 195 | base::StringPrintf("wget -O /dev/null http://127.0.0.1:%d/quitquitquit", |
| 196 | local_server_port)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 197 | EXPECT_NE(-1, status) << "system() failed"; |
| 198 | EXPECT_TRUE(WIFEXITED(status)) |
| 199 | << "command failed to run or died abnormally"; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 200 | MessageLoop::current()->BreakLoop(); |
| 201 | } else { |
| 202 | // Re-run this callback again in 10 ms. |
| 203 | MessageLoop::current()->PostDelayedTask( |
| 204 | FROM_HERE, |
| 205 | base::Bind(&ExitWhenDone, spawned), |
| 206 | TimeDelta::FromMilliseconds(10)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 207 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 210 | } // namespace |
| 211 | |
| 212 | TEST_F(SubprocessTest, CancelTest) { |
| 213 | bool spawned = false; |
| 214 | loop_.PostDelayedTask( |
| 215 | FROM_HERE, |
| 216 | base::Bind(&StartAndCancelInRunLoop, &spawned), |
| 217 | TimeDelta::FromMilliseconds(100)); |
| 218 | loop_.PostDelayedTask( |
| 219 | FROM_HERE, |
| 220 | base::Bind(&ExitWhenDone, &spawned), |
| 221 | TimeDelta::FromMilliseconds(10)); |
| 222 | loop_.Run(); |
| 223 | // This test would leak a callback that runs when the child process exits |
| 224 | // unless we wait for it to run. |
| 225 | chromeos::MessageLoopRunUntil( |
| 226 | &loop_, |
| 227 | TimeDelta::FromSeconds(10), |
| 228 | base::Bind([] { |
| 229 | return Subprocess::Get().subprocess_records_.empty(); |
| 230 | })); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | } // namespace chromeos_update_engine |