blob: 7c2507526c7d93ad29a763a230c59ad4fc41c7c3 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo5d527802014-07-18 14:24:13 -07005#include <fcntl.h>
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07006#include <netinet/in.h>
7#include <netinet/ip.h>
8#include <poll.h>
9#include <sys/socket.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000010#include <sys/stat.h>
11#include <sys/types.h>
12#include <unistd.h>
Gilad Arnold8e3f1262013-01-08 14:59:54 -080013
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include <string>
15#include <vector>
Gilad Arnold8e3f1262013-01-08 14:59:54 -080016
Alex Vakulenko75039d72014-03-25 12:36:28 -070017#include <base/strings/string_util.h>
18#include <base/strings/stringprintf.h>
19#include <base/time/time.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include <glib.h>
21#include <gtest/gtest.h>
Gilad Arnold8e3f1262013-01-08 14:59:54 -080022
adlr@google.com3defe6a2009-12-04 20:57:17 +000023#include "update_engine/subprocess.h"
24#include "update_engine/test_utils.h"
25#include "update_engine/utils.h"
26
Gilad Arnold8e3f1262013-01-08 14:59:54 -080027using base::TimeDelta;
adlr@google.com3defe6a2009-12-04 20:57:17 +000028using std::string;
29using std::vector;
30
31namespace chromeos_update_engine {
32
33class SubprocessTest : public ::testing::Test {
34 protected:
35 bool callback_done;
36};
37
38namespace {
Gilad Arnoldb6c562a2013-07-01 02:19:26 -070039int local_server_port = 0;
adlr@google.com3defe6a2009-12-04 20:57:17 +000040
Darin Petkov6f03a3b2010-11-10 14:27:14 -080041void Callback(int return_code, const string& output, void *p) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070042 EXPECT_EQ(1, return_code);
adlr@google.com3defe6a2009-12-04 20:57:17 +000043 GMainLoop* loop = reinterpret_cast<GMainLoop*>(p);
44 g_main_loop_quit(loop);
45}
46
Darin Petkov6f03a3b2010-11-10 14:27:14 -080047void 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.com3defe6a2009-12-04 20:57:17 +000055gboolean 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 Petkov6f03a3b2010-11-10 14:27:14 -080061
62gboolean 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 Vakulenkod2779df2014-06-16 13:19:00 -070070} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +000071
72TEST(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 Petkov6f03a3b2010-11-10 14:27:14 -080079TEST(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 Petkov85d02b72011-05-17 13:25:51 -070086TEST(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
98TEST(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.com3defe6a2009-12-04 20:57:17 +0000108namespace {
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800109void CallbackBad(int return_code, const string& output, void *p) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110 CHECK(false) << "should never be called.";
111}
112
113struct CancelTestData {
114 bool spawned;
115 GMainLoop *loop;
116};
117
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700118// 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.com3defe6a2009-12-04 20:57:17 +0000126gboolean StartAndCancelInRunLoop(gpointer data) {
127 CancelTestData* cancel_test_data = reinterpret_cast<CancelTestData*>(data);
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700128
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.com3defe6a2009-12-04 20:57:17 +0000136 vector<string> cmd;
137 cmd.push_back("./test_http_server");
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700138 cmd.push_back(temp_file_name);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700139 uint32_t tag = Subprocess::Get().Exec(cmd, CallbackBad, NULL);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000140 EXPECT_NE(0, tag);
141 cancel_test_data->spawned = true;
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700142 printf("test http server spawned\n");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000143 // Wait for server to be up and running
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800144 TimeDelta total_wait_time;
145 const TimeDelta kSleepTime = TimeDelta::FromMilliseconds(100);
146 const TimeDelta kMaxWaitTime = TimeDelta::FromSeconds(3);
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700147 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 Vakulenkod2779df2014-06-16 13:19:00 -0700158 long raw_port = strtol(listening_port_str, // NOLINT(runtime/int)
159 &end_ptr, 10);
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700160 CHECK(!*end_ptr || *end_ptr == '\n');
161 local_server_port = static_cast<in_port_t>(raw_port);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000162 break;
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700163 } else if (line_len < 0 && errno != EAGAIN) {
164 LOG(INFO) << "error reading from " << temp_file_name << ": "
165 << strerror(errno);
166 break;
167 }
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800168 g_usleep(kSleepTime.InMicroseconds());
Darin Petkov27fa9c52010-07-15 15:11:55 -0700169 total_wait_time += kSleepTime;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170 }
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700171 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.com3defe6a2009-12-04 20:57:17 +0000175 Subprocess::Get().CancelExec(tag);
176 return FALSE;
177}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700178} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +0000179
180gboolean 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 Petkovb7de1d52010-08-24 13:38:33 -0700185 int status = System(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700186 base::StringPrintf("wget -O /dev/null http://127.0.0.1:%d/quitquitquit",
187 local_server_port));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000188 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
197TEST(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.com3defe6a2009-12-04 20:57:17 +0000206}
207
208} // namespace chromeos_update_engine