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 | 5d52780 | 2014-07-18 14:24:13 -0700 | [diff] [blame^] | 5 | #include <fcntl.h> |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 6 | #include <netinet/in.h> |
| 7 | #include <netinet/ip.h> |
| 8 | #include <poll.h> |
| 9 | #include <sys/socket.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <unistd.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 13 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 16 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 17 | #include <base/strings/string_util.h> |
| 18 | #include <base/strings/stringprintf.h> |
| 19 | #include <base/time/time.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 20 | #include <glib.h> |
| 21 | #include <gtest/gtest.h> |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 22 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 23 | #include "update_engine/subprocess.h" |
| 24 | #include "update_engine/test_utils.h" |
| 25 | #include "update_engine/utils.h" |
| 26 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 27 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 28 | using std::string; |
| 29 | using std::vector; |
| 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | class SubprocessTest : public ::testing::Test { |
| 34 | protected: |
| 35 | bool callback_done; |
| 36 | }; |
| 37 | |
| 38 | namespace { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 39 | int local_server_port = 0; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 40 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 41 | void Callback(int return_code, const string& output, void *p) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 42 | EXPECT_EQ(1, return_code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 43 | GMainLoop* loop = reinterpret_cast<GMainLoop*>(p); |
| 44 | g_main_loop_quit(loop); |
| 45 | } |
| 46 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 47 | void CallbackEcho(int return_code, const string& output, void *p) { |
| 48 | EXPECT_EQ(0, return_code); |
| 49 | EXPECT_NE(string::npos, output.find("this is stdout")); |
| 50 | EXPECT_NE(string::npos, output.find("this is stderr")); |
| 51 | GMainLoop* loop = reinterpret_cast<GMainLoop*>(p); |
| 52 | g_main_loop_quit(loop); |
| 53 | } |
| 54 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | gboolean LaunchFalseInMainLoop(gpointer data) { |
| 56 | vector<string> cmd; |
| 57 | cmd.push_back("/bin/false"); |
| 58 | Subprocess::Get().Exec(cmd, Callback, data); |
| 59 | return FALSE; |
| 60 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 61 | |
| 62 | gboolean LaunchEchoInMainLoop(gpointer data) { |
| 63 | vector<string> cmd; |
| 64 | cmd.push_back("/bin/sh"); |
| 65 | cmd.push_back("-c"); |
| 66 | cmd.push_back("echo this is stdout; echo this is stderr > /dev/stderr"); |
| 67 | Subprocess::Get().Exec(cmd, CallbackEcho, data); |
| 68 | return FALSE; |
| 69 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 70 | } // namespace |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 71 | |
| 72 | TEST(SubprocessTest, SimpleTest) { |
| 73 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 74 | g_timeout_add(0, &LaunchFalseInMainLoop, loop); |
| 75 | g_main_loop_run(loop); |
| 76 | g_main_loop_unref(loop); |
| 77 | } |
| 78 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 79 | TEST(SubprocessTest, EchoTest) { |
| 80 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 81 | g_timeout_add(0, &LaunchEchoInMainLoop, loop); |
| 82 | g_main_loop_run(loop); |
| 83 | g_main_loop_unref(loop); |
| 84 | } |
| 85 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 86 | TEST(SubprocessTest, SynchronousEchoTest) { |
| 87 | vector<string> cmd; |
| 88 | cmd.push_back("/bin/sh"); |
| 89 | cmd.push_back("-c"); |
| 90 | cmd.push_back("echo -n stdout-here; echo -n stderr-there > /dev/stderr"); |
| 91 | int rc = -1; |
| 92 | string stdout; |
| 93 | ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, &stdout)); |
| 94 | EXPECT_EQ(0, rc); |
| 95 | EXPECT_EQ("stdout-herestderr-there", stdout); |
| 96 | } |
| 97 | |
| 98 | TEST(SubprocessTest, SynchronousEchoNoOutputTest) { |
| 99 | vector<string> cmd; |
| 100 | cmd.push_back("/bin/sh"); |
| 101 | cmd.push_back("-c"); |
| 102 | cmd.push_back("echo test"); |
| 103 | int rc = -1; |
| 104 | ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, NULL)); |
| 105 | EXPECT_EQ(0, rc); |
| 106 | } |
| 107 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 108 | namespace { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 109 | void CallbackBad(int return_code, const string& output, void *p) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 110 | CHECK(false) << "should never be called."; |
| 111 | } |
| 112 | |
| 113 | struct CancelTestData { |
| 114 | bool spawned; |
| 115 | GMainLoop *loop; |
| 116 | }; |
| 117 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 118 | // TODO(garnold) this test method uses test_http_server as a representative for |
| 119 | // interactive processes that can be spawned/terminated at will. This causes us |
| 120 | // to go through hoops when spawning this process (e.g. obtaining the port |
| 121 | // number it uses so we can control it with wget). It would have been much |
| 122 | // preferred to use something else and thus simplify both test_http_server |
| 123 | // (doesn't have to be able to communicate through a temp file) and the test |
| 124 | // code below; for example, it sounds like a brain dead sleep loop with proper |
| 125 | // signal handlers could be used instead. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 126 | gboolean StartAndCancelInRunLoop(gpointer data) { |
| 127 | CancelTestData* cancel_test_data = reinterpret_cast<CancelTestData*>(data); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 128 | |
| 129 | // Create a temp file for test_http_server to communicate its port number. |
| 130 | char temp_file_name[] = "/tmp/subprocess_unittest-test_http_server-XXXXXX"; |
| 131 | int temp_fd = mkstemp(temp_file_name); |
| 132 | CHECK_GE(temp_fd, 0); |
| 133 | int temp_flags = fcntl(temp_fd, F_GETFL, 0) | O_NONBLOCK; |
| 134 | CHECK_EQ(fcntl(temp_fd, F_SETFL, temp_flags), 0); |
| 135 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 136 | vector<string> cmd; |
| 137 | cmd.push_back("./test_http_server"); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 138 | cmd.push_back(temp_file_name); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 139 | uint32_t tag = Subprocess::Get().Exec(cmd, CallbackBad, NULL); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 140 | EXPECT_NE(0, tag); |
| 141 | cancel_test_data->spawned = true; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 142 | printf("test http server spawned\n"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 143 | // Wait for server to be up and running |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 144 | TimeDelta total_wait_time; |
| 145 | const TimeDelta kSleepTime = TimeDelta::FromMilliseconds(100); |
| 146 | const TimeDelta kMaxWaitTime = TimeDelta::FromSeconds(3); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 147 | local_server_port = 0; |
| 148 | static const char* kServerListeningMsgPrefix = "listening on port "; |
| 149 | while (total_wait_time.InMicroseconds() < kMaxWaitTime.InMicroseconds()) { |
| 150 | char line[80]; |
| 151 | int line_len = read(temp_fd, line, sizeof(line) - 1); |
| 152 | if (line_len > 0) { |
| 153 | line[line_len] = '\0'; |
| 154 | CHECK_EQ(strstr(line, kServerListeningMsgPrefix), line); |
| 155 | const char* listening_port_str = |
| 156 | line + strlen(kServerListeningMsgPrefix); |
| 157 | char* end_ptr; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 158 | long raw_port = strtol(listening_port_str, // NOLINT(runtime/int) |
| 159 | &end_ptr, 10); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 160 | CHECK(!*end_ptr || *end_ptr == '\n'); |
| 161 | local_server_port = static_cast<in_port_t>(raw_port); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 162 | break; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 163 | } else if (line_len < 0 && errno != EAGAIN) { |
| 164 | LOG(INFO) << "error reading from " << temp_file_name << ": " |
| 165 | << strerror(errno); |
| 166 | break; |
| 167 | } |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 168 | g_usleep(kSleepTime.InMicroseconds()); |
Darin Petkov | 27fa9c5 | 2010-07-15 15:11:55 -0700 | [diff] [blame] | 169 | total_wait_time += kSleepTime; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 170 | } |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 171 | close(temp_fd); |
| 172 | remove(temp_file_name); |
| 173 | CHECK_GT(local_server_port, 0); |
| 174 | LOG(INFO) << "server listening on port " << local_server_port; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 175 | Subprocess::Get().CancelExec(tag); |
| 176 | return FALSE; |
| 177 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 178 | } // namespace |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 179 | |
| 180 | gboolean ExitWhenDone(gpointer data) { |
| 181 | CancelTestData* cancel_test_data = reinterpret_cast<CancelTestData*>(data); |
| 182 | if (cancel_test_data->spawned && !Subprocess::Get().SubprocessInFlight()) { |
| 183 | // tear down the sub process |
| 184 | printf("tear down time\n"); |
Darin Petkov | b7de1d5 | 2010-08-24 13:38:33 -0700 | [diff] [blame] | 185 | int status = System( |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 186 | base::StringPrintf("wget -O /dev/null http://127.0.0.1:%d/quitquitquit", |
| 187 | local_server_port)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 188 | EXPECT_NE(-1, status) << "system() failed"; |
| 189 | EXPECT_TRUE(WIFEXITED(status)) |
| 190 | << "command failed to run or died abnormally"; |
| 191 | g_main_loop_quit(cancel_test_data->loop); |
| 192 | return FALSE; |
| 193 | } |
| 194 | return TRUE; |
| 195 | } |
| 196 | |
| 197 | TEST(SubprocessTest, CancelTest) { |
| 198 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 199 | CancelTestData cancel_test_data; |
| 200 | cancel_test_data.spawned = false; |
| 201 | cancel_test_data.loop = loop; |
| 202 | g_timeout_add(100, &StartAndCancelInRunLoop, &cancel_test_data); |
| 203 | g_timeout_add(10, &ExitWhenDone, &cancel_test_data); |
| 204 | g_main_loop_run(loop); |
| 205 | g_main_loop_unref(loop); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } // namespace chromeos_update_engine |