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 Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 71 | } // namespace |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 72 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 73 | TEST_F(SubprocessTest, SimpleTest) { |
| 74 | loop_.PostTask( |
| 75 | FROM_HERE, |
| 76 | base::Bind([] { |
| 77 | Subprocess::Get().Exec(vector<string>{"/bin/false"}, Callback, nullptr); |
| 78 | })); |
| 79 | loop_.Run(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 82 | TEST_F(SubprocessTest, EchoTest) { |
| 83 | loop_.PostTask( |
| 84 | FROM_HERE, |
| 85 | base::Bind([] { |
| 86 | Subprocess::Get().Exec( |
| 87 | vector<string>{ |
| 88 | "/bin/sh", |
| 89 | "-c", |
| 90 | "echo this is stdout; echo this is stderr > /dev/stderr"}, |
| 91 | CallbackEcho, |
| 92 | nullptr); |
| 93 | })); |
| 94 | loop_.Run(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 97 | TEST_F(SubprocessTest, SynchronousEchoTest) { |
| 98 | vector<string> cmd = { |
| 99 | "/bin/sh", |
| 100 | "-c", |
| 101 | "echo -n stdout-here; echo -n stderr-there > /dev/stderr"}; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 102 | int rc = -1; |
| 103 | string stdout; |
| 104 | ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, &stdout)); |
| 105 | EXPECT_EQ(0, rc); |
| 106 | EXPECT_EQ("stdout-herestderr-there", stdout); |
| 107 | } |
| 108 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 109 | TEST_F(SubprocessTest, SynchronousEchoNoOutputTest) { |
| 110 | vector<string> cmd = { |
| 111 | "/bin/sh", |
| 112 | "-c", |
| 113 | "echo test"}; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 114 | int rc = -1; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 115 | ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, nullptr)); |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 116 | EXPECT_EQ(0, rc); |
| 117 | } |
| 118 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 119 | namespace { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 120 | void CallbackBad(int return_code, const string& output, void* p) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | CHECK(false) << "should never be called."; |
| 122 | } |
| 123 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 124 | // TODO(garnold) this test method uses test_http_server as a representative for |
| 125 | // interactive processes that can be spawned/terminated at will. This causes us |
| 126 | // to go through hoops when spawning this process (e.g. obtaining the port |
| 127 | // number it uses so we can control it with wget). It would have been much |
| 128 | // preferred to use something else and thus simplify both test_http_server |
| 129 | // (doesn't have to be able to communicate through a temp file) and the test |
| 130 | // code below; for example, it sounds like a brain dead sleep loop with proper |
| 131 | // signal handlers could be used instead. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 132 | void StartAndCancelInRunLoop(bool* spawned) { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 133 | // Create a temp file for test_http_server to communicate its port number. |
| 134 | char temp_file_name[] = "/tmp/subprocess_unittest-test_http_server-XXXXXX"; |
| 135 | int temp_fd = mkstemp(temp_file_name); |
| 136 | CHECK_GE(temp_fd, 0); |
| 137 | int temp_flags = fcntl(temp_fd, F_GETFL, 0) | O_NONBLOCK; |
| 138 | CHECK_EQ(fcntl(temp_fd, F_SETFL, temp_flags), 0); |
| 139 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 140 | vector<string> cmd; |
| 141 | cmd.push_back("./test_http_server"); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 142 | cmd.push_back(temp_file_name); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 143 | uint32_t tag = Subprocess::Get().Exec(cmd, CallbackBad, nullptr); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 144 | EXPECT_NE(0, tag); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 145 | *spawned = true; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 146 | printf("test http server spawned\n"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 147 | // Wait for server to be up and running |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 148 | TimeDelta total_wait_time; |
| 149 | const TimeDelta kSleepTime = TimeDelta::FromMilliseconds(100); |
| 150 | const TimeDelta kMaxWaitTime = TimeDelta::FromSeconds(3); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 151 | local_server_port = 0; |
| 152 | static const char* kServerListeningMsgPrefix = "listening on port "; |
| 153 | while (total_wait_time.InMicroseconds() < kMaxWaitTime.InMicroseconds()) { |
| 154 | char line[80]; |
| 155 | int line_len = read(temp_fd, line, sizeof(line) - 1); |
| 156 | if (line_len > 0) { |
| 157 | line[line_len] = '\0'; |
| 158 | CHECK_EQ(strstr(line, kServerListeningMsgPrefix), line); |
| 159 | const char* listening_port_str = |
| 160 | line + strlen(kServerListeningMsgPrefix); |
| 161 | char* end_ptr; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 162 | long raw_port = strtol(listening_port_str, // NOLINT(runtime/int) |
| 163 | &end_ptr, 10); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 164 | CHECK(!*end_ptr || *end_ptr == '\n'); |
| 165 | local_server_port = static_cast<in_port_t>(raw_port); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 166 | break; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 167 | } else if (line_len < 0 && errno != EAGAIN) { |
| 168 | LOG(INFO) << "error reading from " << temp_file_name << ": " |
| 169 | << strerror(errno); |
| 170 | break; |
| 171 | } |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 172 | g_usleep(kSleepTime.InMicroseconds()); |
Darin Petkov | 27fa9c5 | 2010-07-15 15:11:55 -0700 | [diff] [blame] | 173 | total_wait_time += kSleepTime; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 174 | } |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 175 | close(temp_fd); |
| 176 | remove(temp_file_name); |
| 177 | CHECK_GT(local_server_port, 0); |
| 178 | LOG(INFO) << "server listening on port " << local_server_port; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 179 | Subprocess::Get().CancelExec(tag); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 180 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 181 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 182 | void ExitWhenDone(bool* spawned) { |
| 183 | if (*spawned && !Subprocess::Get().SubprocessInFlight()) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 184 | // tear down the sub process |
| 185 | printf("tear down time\n"); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 186 | int status = test_utils::System( |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 187 | base::StringPrintf("wget -O /dev/null http://127.0.0.1:%d/quitquitquit", |
| 188 | local_server_port)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 189 | EXPECT_NE(-1, status) << "system() failed"; |
| 190 | EXPECT_TRUE(WIFEXITED(status)) |
| 191 | << "command failed to run or died abnormally"; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 192 | MessageLoop::current()->BreakLoop(); |
| 193 | } else { |
| 194 | // Re-run this callback again in 10 ms. |
| 195 | MessageLoop::current()->PostDelayedTask( |
| 196 | FROM_HERE, |
| 197 | base::Bind(&ExitWhenDone, spawned), |
| 198 | TimeDelta::FromMilliseconds(10)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 199 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 202 | } // namespace |
| 203 | |
| 204 | TEST_F(SubprocessTest, CancelTest) { |
| 205 | bool spawned = false; |
| 206 | loop_.PostDelayedTask( |
| 207 | FROM_HERE, |
| 208 | base::Bind(&StartAndCancelInRunLoop, &spawned), |
| 209 | TimeDelta::FromMilliseconds(100)); |
| 210 | loop_.PostDelayedTask( |
| 211 | FROM_HERE, |
| 212 | base::Bind(&ExitWhenDone, &spawned), |
| 213 | TimeDelta::FromMilliseconds(10)); |
| 214 | loop_.Run(); |
| 215 | // This test would leak a callback that runs when the child process exits |
| 216 | // unless we wait for it to run. |
| 217 | chromeos::MessageLoopRunUntil( |
| 218 | &loop_, |
| 219 | TimeDelta::FromSeconds(10), |
| 220 | base::Bind([] { |
| 221 | return Subprocess::Get().subprocess_records_.empty(); |
| 222 | })); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } // namespace chromeos_update_engine |