| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sys/stat.h> | 5 #include <sys/stat.h> |
| 6 #include <sys/types.h> | 6 #include <sys/types.h> |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include <glib.h> | 11 #include <glib.h> |
| 12 #include <gtest/gtest.h> | 12 #include <gtest/gtest.h> |
| 13 #include "update_engine/subprocess.h" | 13 #include "update_engine/subprocess.h" |
| 14 #include "update_engine/test_utils.h" | 14 #include "update_engine/test_utils.h" |
| 15 #include "update_engine/utils.h" | 15 #include "update_engine/utils.h" |
| 16 | 16 |
| 17 using std::string; | 17 using std::string; |
| 18 using std::vector; | 18 using std::vector; |
| 19 | 19 |
| 20 namespace chromeos_update_engine { | 20 namespace chromeos_update_engine { |
| 21 | 21 |
| 22 class SubprocessTest : public ::testing::Test { | 22 class SubprocessTest : public ::testing::Test { |
| 23 protected: | 23 protected: |
| 24 bool callback_done; | 24 bool callback_done; |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 const int kLocalHttpPort = 8088; | 28 const int kLocalHttpPort = 8088; |
| 29 | 29 |
| 30 void Callback(int return_code, void *p) { | 30 void Callback(int return_code, const string& output, void *p) { |
| 31 EXPECT_EQ(256, return_code); | 31 EXPECT_EQ(256, return_code); |
| 32 GMainLoop* loop = reinterpret_cast<GMainLoop*>(p); | 32 GMainLoop* loop = reinterpret_cast<GMainLoop*>(p); |
| 33 g_main_loop_quit(loop); | 33 g_main_loop_quit(loop); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void 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 |
| 36 gboolean LaunchFalseInMainLoop(gpointer data) { | 44 gboolean LaunchFalseInMainLoop(gpointer data) { |
| 37 vector<string> cmd; | 45 vector<string> cmd; |
| 38 cmd.push_back("/bin/false"); | 46 cmd.push_back("/bin/false"); |
| 39 Subprocess::Get().Exec(cmd, Callback, data); | 47 Subprocess::Get().Exec(cmd, Callback, data); |
| 40 return FALSE; | 48 return FALSE; |
| 41 } | 49 } |
| 50 |
| 51 gboolean 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 } |
| 42 } // namespace {} | 59 } // namespace {} |
| 43 | 60 |
| 44 TEST(SubprocessTest, SimpleTest) { | 61 TEST(SubprocessTest, SimpleTest) { |
| 45 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 62 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 46 g_timeout_add(0, &LaunchFalseInMainLoop, loop); | 63 g_timeout_add(0, &LaunchFalseInMainLoop, loop); |
| 47 g_main_loop_run(loop); | 64 g_main_loop_run(loop); |
| 48 g_main_loop_unref(loop); | 65 g_main_loop_unref(loop); |
| 49 } | 66 } |
| 50 | 67 |
| 68 TEST(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 |
| 51 namespace { | 75 namespace { |
| 52 void CallbackBad(int return_code, void *p) { | 76 void CallbackBad(int return_code, const string& output, void *p) { |
| 53 CHECK(false) << "should never be called."; | 77 CHECK(false) << "should never be called."; |
| 54 } | 78 } |
| 55 | 79 |
| 56 struct CancelTestData { | 80 struct CancelTestData { |
| 57 bool spawned; | 81 bool spawned; |
| 58 GMainLoop *loop; | 82 GMainLoop *loop; |
| 59 }; | 83 }; |
| 60 | 84 |
| 61 gboolean StartAndCancelInRunLoop(gpointer data) { | 85 gboolean StartAndCancelInRunLoop(gpointer data) { |
| 62 CancelTestData* cancel_test_data = reinterpret_cast<CancelTestData*>(data); | 86 CancelTestData* cancel_test_data = reinterpret_cast<CancelTestData*>(data); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 cancel_test_data.spawned = false; | 136 cancel_test_data.spawned = false; |
| 113 cancel_test_data.loop = loop; | 137 cancel_test_data.loop = loop; |
| 114 g_timeout_add(100, &StartAndCancelInRunLoop, &cancel_test_data); | 138 g_timeout_add(100, &StartAndCancelInRunLoop, &cancel_test_data); |
| 115 g_timeout_add(10, &ExitWhenDone, &cancel_test_data); | 139 g_timeout_add(10, &ExitWhenDone, &cancel_test_data); |
| 116 g_main_loop_run(loop); | 140 g_main_loop_run(loop); |
| 117 g_main_loop_unref(loop); | 141 g_main_loop_unref(loop); |
| 118 printf("here\n"); | 142 printf("here\n"); |
| 119 } | 143 } |
| 120 | 144 |
| 121 } // namespace chromeos_update_engine | 145 } // namespace chromeos_update_engine |
| OLD | NEW |