blob: c20d810acafab25fe35277385a7be696ad0712bb [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();
59 string dev = GetUnusedLoopDevice();
60 string cmd = string("losetup ") + dev + " " + kTestDir + "/dev2";
61 EXPECT_EQ(0, system(cmd.c_str()));
62 install_action->SetOutputObject(dev);
63 } else if (action->Type() == PostinstallRunnerAction::StaticType()) {
64 PostinstallRunnerAction* postinstall_runner_action =
65 static_cast<PostinstallRunnerAction*>(action);
66 string dev = postinstall_runner_action->GetOutputObject();
67 EXPECT_EQ(0, system((string("losetup -d ") + dev).c_str()));
68 postinstall_runner_action->SetOutputObject(old_dev_);
69 old_dev_ = "";
70 }
71 }
72
73 void set_loop(GMainLoop* loop) {
74 loop_ = loop;
75 }
76
77 private:
78 GMainLoop *loop_;
79 bool processing_done_called_;
80
81 // We have to change the dev for the PostinstallRunnerAction action.
82 // Before that runs, we store the device here, and after it runs, we
83 // restore it.
84 // This is because we use a file, rather than a device, to install into,
85 // but the PostinstallRunnerAction requires a real device. We set up a
86 // loop device pointing to the file when necessary.
87 string old_dev_;
88};
89
90gboolean TestStarter(gpointer data) {
91 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
92 processor->StartProcessing();
93 return FALSE;
94}
95
96} // namespace {}
97
98TEST(IntegrationTest, DISABLED_RunAsRootFullInstallTest) {
99 ASSERT_EQ(0, getuid());
100 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
101
102 ActionProcessor processor;
103 IntegrationTestProcessorDelegate delegate;
104 delegate.set_loop(loop);
105 processor.set_delegate(&delegate);
106
107 // Actions:
108 OmahaRequestPrepAction request_prep_action(false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700109 OmahaRequestAction update_check_action(NULL, new LibcurlHttpFetcher);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110 OmahaResponseHandlerAction response_handler_action;
111 DownloadAction download_action(new LibcurlHttpFetcher);
112 InstallAction install_action;
113 PostinstallRunnerAction postinstall_runner_action;
114 SetBootableFlagAction set_bootable_flag_action;
115
116 // Enqueue the actions
117 processor.EnqueueAction(&request_prep_action);
118 processor.EnqueueAction(&update_check_action);
119 processor.EnqueueAction(&response_handler_action);
120 processor.EnqueueAction(&download_action);
121 processor.EnqueueAction(&install_action);
122 processor.EnqueueAction(&postinstall_runner_action);
123 processor.EnqueueAction(&set_bootable_flag_action);
124
125 // Bond them together
126 BondActions(&request_prep_action, &update_check_action);
127 BondActions(&update_check_action, &response_handler_action);
128 BondActions(&response_handler_action, &download_action);
129 BondActions(&download_action, &install_action);
130 BondActions(&install_action, &postinstall_runner_action);
131 BondActions(&postinstall_runner_action, &set_bootable_flag_action);
132
133 // Set up filesystem to trick some of the actions
134 ASSERT_EQ(0, System(string("rm -rf ") + kTestDir));
135 ASSERT_EQ(0, system("rm -f /tmp/update_engine_test_postinst_out.txt"));
136 ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800137 ASSERT_EQ(0, system((string("mkdir -p ") + kTestDir +
138 utils::kStatefulPartition +
139 "/etc").c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000140 ASSERT_TRUE(WriteFileString(string(kTestDir) + "/etc/lsb-release",
141 "GOOGLE_RELEASE=0.2.0.0\n"
142 "GOOGLE_TRACK=unittest-track"));
143 ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev1"));
144 ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev2"));
145 ASSERT_TRUE(WriteFileVector(string(kTestDir) + "/dev", GenerateSampleMbr()));
146
147 request_prep_action.set_root(kTestDir);
148 response_handler_action.set_boot_device(string(kTestDir) + "/dev1");
149
150 // Run the actions
151 g_timeout_add(0, &TestStarter, &processor);
152 g_main_loop_run(loop);
153 g_main_loop_unref(loop);
154
155 // Verify the results
156 struct stat stbuf;
157 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev1").c_str(), &stbuf));
158 EXPECT_EQ(0, stbuf.st_size);
159 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
160 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev2").c_str(), &stbuf));
161 EXPECT_EQ(996147200, stbuf.st_size);
162 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
163 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev").c_str(), &stbuf));
164 ASSERT_EQ(512, stbuf.st_size);
165 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
166 vector<char> new_mbr;
167 EXPECT_TRUE(utils::ReadFile((string(kTestDir) + "/dev").c_str(), &new_mbr));
168
169 // Check bootable flag in MBR
170 for (int i = 0; i < 4; i++) {
171 char expected_flag = '\0';
172 if (i == 1)
173 expected_flag = 0x80;
174 EXPECT_EQ(expected_flag, new_mbr[446 + 16 * i]);
175 }
176 // Check MBR signature
177 EXPECT_EQ(static_cast<char>(0x55), new_mbr[510]);
178 EXPECT_EQ(static_cast<char>(0xaa), new_mbr[511]);
179
180 ASSERT_EQ(0, lstat("/tmp/update_engine_test_postinst_out.txt", &stbuf));
181 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
182 string file_data;
183 EXPECT_TRUE(utils::ReadFileToString(
184 "/tmp/update_engine_test_postinst_out.txt",
185 &file_data));
186 EXPECT_EQ("POSTINST_DONE\n", file_data);
187
188 // cleanup
189 ASSERT_EQ(0, System(string("rm -rf ") + kTestDir));
190 ASSERT_EQ(0, system("rm -f /tmp/update_engine_test_postinst_out.txt"));
191}
192
193} // namespace chromeos_update_engine