adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | #include <glib.h> |
| 8 | #include <pthread.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | #include "update_engine/download_action.h" |
| 11 | #include "update_engine/install_action.h" |
| 12 | #include "update_engine/libcurl_http_fetcher.h" |
| 13 | #include "update_engine/mock_http_fetcher.h" |
| 14 | #include "update_engine/omaha_request_prep_action.h" |
| 15 | #include "update_engine/omaha_response_handler_action.h" |
| 16 | #include "update_engine/postinstall_runner_action.h" |
| 17 | #include "update_engine/set_bootable_flag_action.h" |
| 18 | #include "update_engine/test_utils.h" |
| 19 | #include "update_engine/update_check_action.h" |
| 20 | #include "update_engine/utils.h" |
| 21 | |
| 22 | // The tests here integrate many Action objects together. This test that |
| 23 | // the objects work well together, whereas most other tests focus on a single |
| 24 | // action at a time. |
| 25 | |
| 26 | namespace chromeos_update_engine { |
| 27 | |
| 28 | using std::string; |
| 29 | using std::vector; |
| 30 | |
| 31 | class IntegrationTest : public ::testing::Test { }; |
| 32 | |
| 33 | namespace { |
| 34 | const char* kTestDir = "/tmp/update_engine-integration-test"; |
| 35 | |
| 36 | class IntegrationTestProcessorDelegate : public ActionProcessorDelegate { |
| 37 | public: |
| 38 | IntegrationTestProcessorDelegate() |
| 39 | : loop_(NULL), processing_done_called_(false) {} |
| 40 | virtual ~IntegrationTestProcessorDelegate() { |
| 41 | EXPECT_TRUE(processing_done_called_); |
| 42 | } |
| 43 | virtual void ProcessingDone(const ActionProcessor* processor) { |
| 44 | processing_done_called_ = true; |
| 45 | g_main_loop_quit(loop_); |
| 46 | } |
| 47 | |
| 48 | virtual void ActionCompleted(ActionProcessor* processor, |
| 49 | AbstractAction* action, |
| 50 | bool success) { |
| 51 | // make sure actions always succeed |
| 52 | EXPECT_TRUE(success); |
| 53 | |
| 54 | // Swap in the device path for PostinstallRunnerAction with a loop device |
| 55 | if (action->Type() == InstallAction::StaticType()) { |
| 56 | InstallAction* install_action = static_cast<InstallAction*>(action); |
| 57 | old_dev_ = install_action->GetOutputObject(); |
| 58 | string dev = GetUnusedLoopDevice(); |
| 59 | string cmd = string("losetup ") + dev + " " + kTestDir + "/dev2"; |
| 60 | EXPECT_EQ(0, system(cmd.c_str())); |
| 61 | install_action->SetOutputObject(dev); |
| 62 | } else if (action->Type() == PostinstallRunnerAction::StaticType()) { |
| 63 | PostinstallRunnerAction* postinstall_runner_action = |
| 64 | static_cast<PostinstallRunnerAction*>(action); |
| 65 | string dev = postinstall_runner_action->GetOutputObject(); |
| 66 | EXPECT_EQ(0, system((string("losetup -d ") + dev).c_str())); |
| 67 | postinstall_runner_action->SetOutputObject(old_dev_); |
| 68 | old_dev_ = ""; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void set_loop(GMainLoop* loop) { |
| 73 | loop_ = loop; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | GMainLoop *loop_; |
| 78 | bool processing_done_called_; |
| 79 | |
| 80 | // We have to change the dev for the PostinstallRunnerAction action. |
| 81 | // Before that runs, we store the device here, and after it runs, we |
| 82 | // restore it. |
| 83 | // This is because we use a file, rather than a device, to install into, |
| 84 | // but the PostinstallRunnerAction requires a real device. We set up a |
| 85 | // loop device pointing to the file when necessary. |
| 86 | string old_dev_; |
| 87 | }; |
| 88 | |
| 89 | gboolean TestStarter(gpointer data) { |
| 90 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 91 | processor->StartProcessing(); |
| 92 | return FALSE; |
| 93 | } |
| 94 | |
| 95 | } // namespace {} |
| 96 | |
| 97 | TEST(IntegrationTest, DISABLED_RunAsRootFullInstallTest) { |
| 98 | ASSERT_EQ(0, getuid()); |
| 99 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 100 | |
| 101 | ActionProcessor processor; |
| 102 | IntegrationTestProcessorDelegate delegate; |
| 103 | delegate.set_loop(loop); |
| 104 | processor.set_delegate(&delegate); |
| 105 | |
| 106 | // Actions: |
| 107 | OmahaRequestPrepAction request_prep_action(false); |
| 108 | UpdateCheckAction update_check_action(new LibcurlHttpFetcher); |
| 109 | OmahaResponseHandlerAction response_handler_action; |
| 110 | DownloadAction download_action(new LibcurlHttpFetcher); |
| 111 | InstallAction install_action; |
| 112 | PostinstallRunnerAction postinstall_runner_action; |
| 113 | SetBootableFlagAction set_bootable_flag_action; |
| 114 | |
| 115 | // Enqueue the actions |
| 116 | processor.EnqueueAction(&request_prep_action); |
| 117 | processor.EnqueueAction(&update_check_action); |
| 118 | processor.EnqueueAction(&response_handler_action); |
| 119 | processor.EnqueueAction(&download_action); |
| 120 | processor.EnqueueAction(&install_action); |
| 121 | processor.EnqueueAction(&postinstall_runner_action); |
| 122 | processor.EnqueueAction(&set_bootable_flag_action); |
| 123 | |
| 124 | // Bond them together |
| 125 | BondActions(&request_prep_action, &update_check_action); |
| 126 | BondActions(&update_check_action, &response_handler_action); |
| 127 | BondActions(&response_handler_action, &download_action); |
| 128 | BondActions(&download_action, &install_action); |
| 129 | BondActions(&install_action, &postinstall_runner_action); |
| 130 | BondActions(&postinstall_runner_action, &set_bootable_flag_action); |
| 131 | |
| 132 | // Set up filesystem to trick some of the actions |
| 133 | ASSERT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 134 | ASSERT_EQ(0, system("rm -f /tmp/update_engine_test_postinst_out.txt")); |
| 135 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
| 136 | ASSERT_TRUE(WriteFileString(string(kTestDir) + "/etc/lsb-release", |
| 137 | "GOOGLE_RELEASE=0.2.0.0\n" |
| 138 | "GOOGLE_TRACK=unittest-track")); |
| 139 | ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev1")); |
| 140 | ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev2")); |
| 141 | ASSERT_TRUE(WriteFileVector(string(kTestDir) + "/dev", GenerateSampleMbr())); |
| 142 | |
| 143 | request_prep_action.set_root(kTestDir); |
| 144 | response_handler_action.set_boot_device(string(kTestDir) + "/dev1"); |
| 145 | |
| 146 | // Run the actions |
| 147 | g_timeout_add(0, &TestStarter, &processor); |
| 148 | g_main_loop_run(loop); |
| 149 | g_main_loop_unref(loop); |
| 150 | |
| 151 | // Verify the results |
| 152 | struct stat stbuf; |
| 153 | ASSERT_EQ(0, lstat((string(kTestDir) + "/dev1").c_str(), &stbuf)); |
| 154 | EXPECT_EQ(0, stbuf.st_size); |
| 155 | EXPECT_TRUE(S_ISREG(stbuf.st_mode)); |
| 156 | ASSERT_EQ(0, lstat((string(kTestDir) + "/dev2").c_str(), &stbuf)); |
| 157 | EXPECT_EQ(996147200, stbuf.st_size); |
| 158 | EXPECT_TRUE(S_ISREG(stbuf.st_mode)); |
| 159 | ASSERT_EQ(0, lstat((string(kTestDir) + "/dev").c_str(), &stbuf)); |
| 160 | ASSERT_EQ(512, stbuf.st_size); |
| 161 | EXPECT_TRUE(S_ISREG(stbuf.st_mode)); |
| 162 | vector<char> new_mbr; |
| 163 | EXPECT_TRUE(utils::ReadFile((string(kTestDir) + "/dev").c_str(), &new_mbr)); |
| 164 | |
| 165 | // Check bootable flag in MBR |
| 166 | for (int i = 0; i < 4; i++) { |
| 167 | char expected_flag = '\0'; |
| 168 | if (i == 1) |
| 169 | expected_flag = 0x80; |
| 170 | EXPECT_EQ(expected_flag, new_mbr[446 + 16 * i]); |
| 171 | } |
| 172 | // Check MBR signature |
| 173 | EXPECT_EQ(static_cast<char>(0x55), new_mbr[510]); |
| 174 | EXPECT_EQ(static_cast<char>(0xaa), new_mbr[511]); |
| 175 | |
| 176 | ASSERT_EQ(0, lstat("/tmp/update_engine_test_postinst_out.txt", &stbuf)); |
| 177 | EXPECT_TRUE(S_ISREG(stbuf.st_mode)); |
| 178 | string file_data; |
| 179 | EXPECT_TRUE(utils::ReadFileToString( |
| 180 | "/tmp/update_engine_test_postinst_out.txt", |
| 181 | &file_data)); |
| 182 | EXPECT_EQ("POSTINST_DONE\n", file_data); |
| 183 | |
| 184 | // cleanup |
| 185 | ASSERT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 186 | ASSERT_EQ(0, system("rm -f /tmp/update_engine_test_postinst_out.txt")); |
| 187 | } |
| 188 | |
| 189 | } // namespace chromeos_update_engine |