blob: 8a0ac61fd9460f1e31fb64a040fc1fa3f0e1e3d1 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// 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"
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 }
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080043 virtual void ProcessingDone(const ActionProcessor* processor, bool success) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000044 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
89gboolean TestStarter(gpointer data) {
90 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
91 processor->StartProcessing();
92 return FALSE;
93}
94
95} // namespace {}
96
97TEST(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);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700108 OmahaRequestAction update_check_action(NULL, new LibcurlHttpFetcher);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000109 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"));
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800136 ASSERT_EQ(0, system((string("mkdir -p ") + kTestDir +
137 utils::kStatefulPartition +
138 "/etc").c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000139 ASSERT_TRUE(WriteFileString(string(kTestDir) + "/etc/lsb-release",
140 "GOOGLE_RELEASE=0.2.0.0\n"
141 "GOOGLE_TRACK=unittest-track"));
142 ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev1"));
143 ASSERT_EQ(0, System(string("touch ") + kTestDir + "/dev2"));
144 ASSERT_TRUE(WriteFileVector(string(kTestDir) + "/dev", GenerateSampleMbr()));
145
146 request_prep_action.set_root(kTestDir);
147 response_handler_action.set_boot_device(string(kTestDir) + "/dev1");
148
149 // Run the actions
150 g_timeout_add(0, &TestStarter, &processor);
151 g_main_loop_run(loop);
152 g_main_loop_unref(loop);
153
154 // Verify the results
155 struct stat stbuf;
156 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev1").c_str(), &stbuf));
157 EXPECT_EQ(0, stbuf.st_size);
158 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
159 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev2").c_str(), &stbuf));
160 EXPECT_EQ(996147200, stbuf.st_size);
161 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
162 ASSERT_EQ(0, lstat((string(kTestDir) + "/dev").c_str(), &stbuf));
163 ASSERT_EQ(512, stbuf.st_size);
164 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
165 vector<char> new_mbr;
166 EXPECT_TRUE(utils::ReadFile((string(kTestDir) + "/dev").c_str(), &new_mbr));
167
168 // Check bootable flag in MBR
169 for (int i = 0; i < 4; i++) {
170 char expected_flag = '\0';
171 if (i == 1)
172 expected_flag = 0x80;
173 EXPECT_EQ(expected_flag, new_mbr[446 + 16 * i]);
174 }
175 // Check MBR signature
176 EXPECT_EQ(static_cast<char>(0x55), new_mbr[510]);
177 EXPECT_EQ(static_cast<char>(0xaa), new_mbr[511]);
178
179 ASSERT_EQ(0, lstat("/tmp/update_engine_test_postinst_out.txt", &stbuf));
180 EXPECT_TRUE(S_ISREG(stbuf.st_mode));
181 string file_data;
182 EXPECT_TRUE(utils::ReadFileToString(
183 "/tmp/update_engine_test_postinst_out.txt",
184 &file_data));
185 EXPECT_EQ("POSTINST_DONE\n", file_data);
186
187 // cleanup
188 ASSERT_EQ(0, System(string("rm -rf ") + kTestDir));
189 ASSERT_EQ(0, system("rm -f /tmp/update_engine_test_postinst_out.txt"));
190}
191
192} // namespace chromeos_update_engine