blob: 4c43b35ebec785474168b931bf1cd5fa125bc4a4 [file] [log] [blame]
Darin Petkovc1a8b422010-07-19 11:34:49 -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 <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"
Darin Petkov6a5b3222010-07-13 14:55:28 -070014#include "update_engine/omaha_request_action.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000015#include "update_engine/omaha_request_prep_action.h"
16#include "update_engine/omaha_response_handler_action.h"
17#include "update_engine/postinstall_runner_action.h"
18#include "update_engine/set_bootable_flag_action.h"
19#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#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
26namespace chromeos_update_engine {
27
28using std::string;
29using std::vector;
30
31class IntegrationTest : public ::testing::Test { };
32
33namespace {
34const char* kTestDir = "/tmp/update_engine-integration-test";
35
36class IntegrationTestProcessorDelegate : public ActionProcessorDelegate {
37 public:
38 IntegrationTestProcessorDelegate()
39 : loop_(NULL), processing_done_called_(false) {}
40 virtual ~IntegrationTestProcessorDelegate() {
41 EXPECT_TRUE(processing_done_called_);
42 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070043 virtual void ProcessingDone(const ActionProcessor* processor,
44 ActionExitCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000045 processing_done_called_ = true;
46 g_main_loop_quit(loop_);
47 }
48
49 virtual void ActionCompleted(ActionProcessor* processor,
50 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070051 ActionExitCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000052 // make sure actions always succeed
Darin Petkovc1a8b422010-07-19 11:34:49 -070053 EXPECT_EQ(kActionCodeSuccess, code);
adlr@google.com3defe6a2009-12-04 20:57:17 +000054
55 // Swap in the device path for PostinstallRunnerAction with a loop device
56 if (action->Type() == InstallAction::StaticType()) {
57 InstallAction* install_action = static_cast<InstallAction*>(action);
58 old_dev_ = install_action->GetOutputObject();
Don Garrett58e8b1f2012-01-31 16:38:16 -080059 string dev = BindToUnusedDevice(kTestDir + "/dev2");
adlr@google.com3defe6a2009-12-04 20:57:17 +000060 install_action->SetOutputObject(dev);
61 } else if (action->Type() == PostinstallRunnerAction::StaticType()) {
62 PostinstallRunnerAction* postinstall_runner_action =
63 static_cast<PostinstallRunnerAction*>(action);
64 string dev = postinstall_runner_action->GetOutputObject();
65 EXPECT_EQ(0, system((string("losetup -d ") + dev).c_str()));
66 postinstall_runner_action->SetOutputObject(old_dev_);
67 old_dev_ = "";
68 }
69 }
70
71 void set_loop(GMainLoop* loop) {
72 loop_ = loop;
73 }
74
75 private:
76 GMainLoop *loop_;
77 bool processing_done_called_;
78
79 // We have to change the dev for the PostinstallRunnerAction action.
80 // Before that runs, we store the device here, and after it runs, we
81 // restore it.
82 // This is because we use a file, rather than a device, to install into,
83 // but the PostinstallRunnerAction requires a real device. We set up a
84 // loop device pointing to the file when necessary.
85 string old_dev_;
86};
87
88gboolean TestStarter(gpointer data) {
89 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
90 processor->StartProcessing();
91 return FALSE;
92}
93
94} // namespace {}
95
96TEST(IntegrationTest, DISABLED_RunAsRootFullInstallTest) {
97 ASSERT_EQ(0, getuid());
98 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
99
100 ActionProcessor processor;
101 IntegrationTestProcessorDelegate delegate;
102 delegate.set_loop(loop);
103 processor.set_delegate(&delegate);
104
105 // Actions:
106 OmahaRequestPrepAction request_prep_action(false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700107 OmahaRequestAction update_check_action(NULL, new LibcurlHttpFetcher);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000108 OmahaResponseHandlerAction response_handler_action;
109 DownloadAction download_action(new LibcurlHttpFetcher);
110 InstallAction install_action;
111 PostinstallRunnerAction postinstall_runner_action;
112 SetBootableFlagAction set_bootable_flag_action;
113
114 // Enqueue the actions
115 processor.EnqueueAction(&request_prep_action);
116 processor.EnqueueAction(&update_check_action);
117 processor.EnqueueAction(&response_handler_action);
118 processor.EnqueueAction(&download_action);
119 processor.EnqueueAction(&install_action);
120 processor.EnqueueAction(&postinstall_runner_action);
121 processor.EnqueueAction(&set_bootable_flag_action);
122
123 // Bond them together
124 BondActions(&request_prep_action, &update_check_action);
125 BondActions(&update_check_action, &response_handler_action);
126 BondActions(&response_handler_action, &download_action);
127 BondActions(&download_action, &install_action);
128 BondActions(&install_action, &postinstall_runner_action);
129 BondActions(&postinstall_runner_action, &set_bootable_flag_action);
130
131 // Set up filesystem to trick some of the actions
132 ASSERT_EQ(0, System(string("rm -rf ") + kTestDir));
133 ASSERT_EQ(0, system("rm -f /tmp/update_engine_test_postinst_out.txt"));
134 ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800135 ASSERT_EQ(0, system((string("mkdir -p ") + kTestDir +
136 utils::kStatefulPartition +
137 "/etc").c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000138 ASSERT_TRUE(WriteFileString(string(kTestDir) + "/etc/lsb-release",
139 "GOOGLE_RELEASE=0.2.0.0\n"
140 "GOOGLE_TRACK=unittest-track"));
141 ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev1"));
142 ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev2"));
143 ASSERT_TRUE(WriteFileVector(string(kTestDir) + "/dev", GenerateSampleMbr()));
144
145 request_prep_action.set_root(kTestDir);
146 response_handler_action.set_boot_device(string(kTestDir) + "/dev1");
147
148 // Run the actions
149 g_timeout_add(0, &TestStarter, &processor);
150 g_main_loop_run(loop);
151 g_main_loop_unref(loop);
152
153 // Verify the results
154 struct stat stbuf;
155 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev1").c_str(), &stbuf));
156 EXPECT_EQ(0, stbuf.st_size);
157 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
158 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev2").c_str(), &stbuf));
159 EXPECT_EQ(996147200, stbuf.st_size);
160 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
161 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev").c_str(), &stbuf));
162 ASSERT_EQ(512, stbuf.st_size);
163 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
164 vector<char> new_mbr;
165 EXPECT_TRUE(utils::ReadFile((string(kTestDir) + "/dev").c_str(), &new_mbr));
166
167 // Check bootable flag in MBR
168 for (int i = 0; i < 4; i++) {
169 char expected_flag = '\0';
170 if (i == 1)
171 expected_flag = 0x80;
172 EXPECT_EQ(expected_flag, new_mbr[446 + 16 * i]);
173 }
174 // Check MBR signature
175 EXPECT_EQ(static_cast<char>(0x55), new_mbr[510]);
176 EXPECT_EQ(static_cast<char>(0xaa), new_mbr[511]);
177
178 ASSERT_EQ(0, lstat("/tmp/update_engine_test_postinst_out.txt", &stbuf));
179 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
180 string file_data;
181 EXPECT_TRUE(utils::ReadFileToString(
182 "/tmp/update_engine_test_postinst_out.txt",
183 &file_data));
184 EXPECT_EQ("POSTINST_DONE\n", file_data);
185
186 // cleanup
187 ASSERT_EQ(0, System(string("rm -rf ") + kTestDir));
188 ASSERT_EQ(0, system("rm -f /tmp/update_engine_test_postinst_out.txt"));
189}
190
191} // namespace chromeos_update_engine