blob: 314e0ae28dba1119cc77e0ec694a82e7febb1806 [file] [log] [blame]
Darin Petkov27fa9c52010-07-15 15:11:55 -07001// Copyright (c) 2010 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
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <unistd.h>
8#include <string>
9#include <vector>
10#include "base/string_util.h"
11#include <glib.h>
12#include <gtest/gtest.h>
13#include "update_engine/subprocess.h"
14#include "update_engine/test_utils.h"
15#include "update_engine/utils.h"
16
17using std::string;
18using std::vector;
19
20namespace chromeos_update_engine {
21
22class SubprocessTest : public ::testing::Test {
23 protected:
24 bool callback_done;
25};
26
27namespace {
Darin Petkovf67bb1f2010-11-08 16:10:26 -080028const int kLocalHttpPort = 8088;
adlr@google.com3defe6a2009-12-04 20:57:17 +000029
Darin Petkov6f03a3b2010-11-10 14:27:14 -080030void Callback(int return_code, const string& output, void *p) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000031 EXPECT_EQ(256, return_code);
32 GMainLoop* loop = reinterpret_cast<GMainLoop*>(p);
33 g_main_loop_quit(loop);
34}
35
Darin Petkov6f03a3b2010-11-10 14:27:14 -080036void CallbackEcho(int return_code, const string& output, void *p) {
37 EXPECT_EQ(0, return_code);
38 EXPECT_NE(string::npos, output.find("this is stdout"));
39 EXPECT_NE(string::npos, output.find("this is stderr"));
40 GMainLoop* loop = reinterpret_cast<GMainLoop*>(p);
41 g_main_loop_quit(loop);
42}
43
adlr@google.com3defe6a2009-12-04 20:57:17 +000044gboolean LaunchFalseInMainLoop(gpointer data) {
45 vector<string> cmd;
46 cmd.push_back("/bin/false");
47 Subprocess::Get().Exec(cmd, Callback, data);
48 return FALSE;
49}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080050
51gboolean LaunchEchoInMainLoop(gpointer data) {
52 vector<string> cmd;
53 cmd.push_back("/bin/sh");
54 cmd.push_back("-c");
55 cmd.push_back("echo this is stdout; echo this is stderr > /dev/stderr");
56 Subprocess::Get().Exec(cmd, CallbackEcho, data);
57 return FALSE;
58}
adlr@google.com3defe6a2009-12-04 20:57:17 +000059} // namespace {}
60
61TEST(SubprocessTest, SimpleTest) {
62 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
63 g_timeout_add(0, &LaunchFalseInMainLoop, loop);
64 g_main_loop_run(loop);
65 g_main_loop_unref(loop);
66}
67
Darin Petkov6f03a3b2010-11-10 14:27:14 -080068TEST(SubprocessTest, EchoTest) {
69 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
70 g_timeout_add(0, &LaunchEchoInMainLoop, loop);
71 g_main_loop_run(loop);
72 g_main_loop_unref(loop);
73}
74
adlr@google.com3defe6a2009-12-04 20:57:17 +000075namespace {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080076void CallbackBad(int return_code, const string& output, void *p) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000077 CHECK(false) << "should never be called.";
78}
79
80struct CancelTestData {
81 bool spawned;
82 GMainLoop *loop;
83};
84
85gboolean StartAndCancelInRunLoop(gpointer data) {
86 CancelTestData* cancel_test_data = reinterpret_cast<CancelTestData*>(data);
87 vector<string> cmd;
88 cmd.push_back("./test_http_server");
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070089 uint32_t tag = Subprocess::Get().Exec(cmd, CallbackBad, NULL);
adlr@google.com3defe6a2009-12-04 20:57:17 +000090 EXPECT_NE(0, tag);
91 cancel_test_data->spawned = true;
92 printf("spawned\n");
93 // Wait for server to be up and running
Darin Petkov27fa9c52010-07-15 15:11:55 -070094 useconds_t total_wait_time = 0;
95 const useconds_t kMaxWaitTime = 3 * 1000000; // 3 seconds
adlr@google.com3defe6a2009-12-04 20:57:17 +000096 for (;;) {
97 int status =
98 System(StringPrintf("wget -O /dev/null http://127.0.0.1:%d/foo",
99 kLocalHttpPort));
100 EXPECT_NE(-1, status) << "system() failed";
101 EXPECT_TRUE(WIFEXITED(status))
102 << "command failed to run or died abnormally";
103 if (0 == WEXITSTATUS(status))
104 break;
Darin Petkov27fa9c52010-07-15 15:11:55 -0700105
106 const useconds_t kSleepTime = 100 * 1000; // 100ms
107 usleep(kSleepTime); // 100 ms
108 total_wait_time += kSleepTime;
109 CHECK_LT(total_wait_time, kMaxWaitTime);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110 }
111 Subprocess::Get().CancelExec(tag);
112 return FALSE;
113}
114} // namespace {}
115
116gboolean ExitWhenDone(gpointer data) {
117 CancelTestData* cancel_test_data = reinterpret_cast<CancelTestData*>(data);
118 if (cancel_test_data->spawned && !Subprocess::Get().SubprocessInFlight()) {
119 // tear down the sub process
120 printf("tear down time\n");
Darin Petkovb7de1d52010-08-24 13:38:33 -0700121 int status = System(
122 StringPrintf("wget -O /dev/null http://127.0.0.1:%d/quitquitquit",
123 kLocalHttpPort));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000124 EXPECT_NE(-1, status) << "system() failed";
125 EXPECT_TRUE(WIFEXITED(status))
126 << "command failed to run or died abnormally";
127 g_main_loop_quit(cancel_test_data->loop);
128 return FALSE;
129 }
130 return TRUE;
131}
132
133TEST(SubprocessTest, CancelTest) {
134 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
135 CancelTestData cancel_test_data;
136 cancel_test_data.spawned = false;
137 cancel_test_data.loop = loop;
138 g_timeout_add(100, &StartAndCancelInRunLoop, &cancel_test_data);
139 g_timeout_add(10, &ExitWhenDone, &cancel_test_data);
140 g_main_loop_run(loop);
141 g_main_loop_unref(loop);
142 printf("here\n");
143}
144
145} // namespace chromeos_update_engine