blob: 578f7f42d77c00d8f2bb142d41d686668ea4c6c0 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 The Chromium 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 <sys/stat.h>
6#include <sys/types.h>
7#include <unistd.h>
8#include <string>
9#include <vector>
10#include <gtest/gtest.h>
11#include "update_engine/postinstall_runner_action.h"
12#include "update_engine/test_utils.h"
13#include "update_engine/utils.h"
14
15using std::string;
16using std::vector;
17
18namespace chromeos_update_engine {
19
20class PostinstallRunnerActionTest : public ::testing::Test {
21 public:
22 void DoTest(bool do_losetup, bool do_err_script);
23};
24
25class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
26 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070027 PostinstActionProcessorDelegate()
28 : code_(kActionCodeError),
29 code_set_(false) {}
adlr@google.com3defe6a2009-12-04 20:57:17 +000030 void ActionCompleted(ActionProcessor* processor,
31 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070032 ActionExitCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000033 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070034 code_ = code;
35 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000036 }
37 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070038 ActionExitCode code_;
39 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000040};
41
42TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
43 ASSERT_EQ(0, getuid());
44 DoTest(true, false);
45}
46
47TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
48 ASSERT_EQ(0, getuid());
49 DoTest(false, false);
50}
51
52TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
53 ASSERT_EQ(0, getuid());
54 DoTest(true, true);
55}
56
57void PostinstallRunnerActionTest::DoTest(bool do_losetup, bool do_err_script) {
58 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
59 << "as root, tho.";
60
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080061 const string mountpoint(string(utils::kStatefulPartition) +
62 "/au_destination");
adlr@google.com3defe6a2009-12-04 20:57:17 +000063
64 string cwd;
65 {
66 vector<char> buf(1000);
67 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size()));
68 cwd = string(&buf[0], strlen(&buf[0]));
69 }
70
71 // create the au destination, if it doesn't exist
72 ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint));
73
74 // create 10MiB sparse file
75 ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 "
76 "count=1"));
77
78 // format it as ext3
79 ASSERT_EQ(0, system("mkfs.ext3 -F image.dat"));
80
81 // mount it
82 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
83
84 // put a postinst script in
85 string script = string("#!/bin/bash\ntouch ") + cwd + "/postinst_called\n";
86 if (do_err_script) {
87 script = "#!/bin/bash\nexit 1";
88 }
89 ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script));
90 ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst"));
91
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070092 ASSERT_EQ(0, System(string("umount -d ") + mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +000093
94 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
95
96 // get a loop device we can use for the install device
97 FILE* find_dev_cmd = popen("losetup -f", "r");
98 ASSERT_TRUE(find_dev_cmd);
99
100 char dev[100] = {0};
101 size_t r = fread(dev, 1, sizeof(dev), find_dev_cmd);
102 ASSERT_GT(r, 0);
103 ASSERT_LT(r, sizeof(dev));
104 ASSERT_TRUE(feof(find_dev_cmd));
105 fclose(find_dev_cmd);
106
107 // strip trailing newline on dev
108 if (dev[strlen(dev) - 1] == '\n')
109 dev[strlen(dev) - 1] = '\0';
110
111 if (do_losetup)
112 ASSERT_EQ(0, System(string("losetup ") + dev + " " + cwd + "/image.dat"));
113
114 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700115 ObjectFeederAction<InstallPlan> feeder_action;
116 InstallPlan install_plan;
117 install_plan.install_path = dev;
118 feeder_action.set_obj(install_plan);
119 PostinstallRunnerAction runner_action(true);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000120 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700121 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000122 BondActions(&runner_action, &collector_action);
123 PostinstActionProcessorDelegate delegate;
124 processor.EnqueueAction(&feeder_action);
125 processor.EnqueueAction(&runner_action);
126 processor.EnqueueAction(&collector_action);
127 processor.set_delegate(&delegate);
128 processor.StartProcessing();
129 ASSERT_FALSE(processor.IsRunning())
130 << "Update test to handle non-asynch actions";
131
Darin Petkovc1a8b422010-07-19 11:34:49 -0700132 EXPECT_TRUE(delegate.code_set_);
133 EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700134 EXPECT_EQ(do_losetup && !do_err_script,
135 !collector_action.object().install_path.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000136 if (do_losetup && !do_err_script) {
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700137 EXPECT_TRUE(install_plan == collector_action.object());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000138 }
139
140 struct stat stbuf;
141 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
142 if (do_losetup && !do_err_script)
143 ASSERT_EQ(0, rc);
144 else
145 ASSERT_LT(rc, 0);
146
147 if (do_losetup)
148 ASSERT_EQ(0, System(string("losetup -d ") + dev));
149 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
150 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat"));
151}
152
153// Death tests don't seem to be working on Hardy
154TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
155 ASSERT_EQ(0, getuid());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700156 PostinstallRunnerAction runner_action(true);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
158 "postinstall_runner_action.h:.*] Check failed");
159}
160
161} // namespace chromeos_update_engine