OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <sys/stat.h> | 5 #include <sys/stat.h> |
6 #include <sys/types.h> | 6 #include <sys/types.h> |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 13 matching lines...) Expand all Loading... |
24 namespace { | 24 namespace { |
25 gboolean StartProcessorInRunLoop(gpointer data) { | 25 gboolean StartProcessorInRunLoop(gpointer data) { |
26 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 26 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
27 processor->StartProcessing(); | 27 processor->StartProcessing(); |
28 return FALSE; | 28 return FALSE; |
29 } | 29 } |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 class PostinstallRunnerActionTest : public ::testing::Test { | 32 class PostinstallRunnerActionTest : public ::testing::Test { |
33 public: | 33 public: |
34 void DoTest(bool do_losetup, bool do_err_script); | 34 void DoTest(bool do_losetup, int err_code); |
35 }; | 35 }; |
36 | 36 |
37 class PostinstActionProcessorDelegate : public ActionProcessorDelegate { | 37 class PostinstActionProcessorDelegate : public ActionProcessorDelegate { |
38 public: | 38 public: |
39 PostinstActionProcessorDelegate() | 39 PostinstActionProcessorDelegate() |
40 : loop_(NULL), | 40 : loop_(NULL), |
41 code_(kActionCodeError), | 41 code_(kActionCodeError), |
42 code_set_(false) {} | 42 code_set_(false) {} |
43 void ProcessingDone(const ActionProcessor* processor, | 43 void ProcessingDone(const ActionProcessor* processor, |
44 ActionExitCode code) { | 44 ActionExitCode code) { |
45 ASSERT_TRUE(loop_); | 45 ASSERT_TRUE(loop_); |
46 g_main_loop_quit(loop_); | 46 g_main_loop_quit(loop_); |
47 } | 47 } |
48 void ActionCompleted(ActionProcessor* processor, | 48 void ActionCompleted(ActionProcessor* processor, |
49 AbstractAction* action, | 49 AbstractAction* action, |
50 ActionExitCode code) { | 50 ActionExitCode code) { |
51 if (action->Type() == PostinstallRunnerAction::StaticType()) { | 51 if (action->Type() == PostinstallRunnerAction::StaticType()) { |
52 code_ = code; | 52 code_ = code; |
53 code_set_ = true; | 53 code_set_ = true; |
54 } | 54 } |
55 } | 55 } |
56 GMainLoop* loop_; | 56 GMainLoop* loop_; |
57 ActionExitCode code_; | 57 ActionExitCode code_; |
58 bool code_set_; | 58 bool code_set_; |
59 }; | 59 }; |
60 | 60 |
61 TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) { | 61 TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) { |
62 ASSERT_EQ(0, getuid()); | 62 ASSERT_EQ(0, getuid()); |
63 DoTest(true, false); | 63 DoTest(true, 0); |
64 } | 64 } |
65 | 65 |
66 TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { | 66 TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { |
67 ASSERT_EQ(0, getuid()); | 67 ASSERT_EQ(0, getuid()); |
68 DoTest(false, false); | 68 DoTest(false, 0); |
69 } | 69 } |
70 | 70 |
71 TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { | 71 TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { |
72 ASSERT_EQ(0, getuid()); | 72 ASSERT_EQ(0, getuid()); |
73 DoTest(true, true); | 73 DoTest(true, 1); |
74 } | 74 } |
75 | 75 |
76 void PostinstallRunnerActionTest::DoTest(bool do_losetup, bool do_err_script) { | 76 TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) { |
| 77 ASSERT_EQ(0, getuid()); |
| 78 DoTest(true, 2); |
| 79 } |
| 80 |
| 81 void PostinstallRunnerActionTest::DoTest(bool do_losetup, int err_code) { |
77 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests " | 82 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests " |
78 << "as root, tho."; | 83 << "as root, tho."; |
79 | 84 |
80 const string mountpoint(string(utils::kStatefulPartition) + | 85 const string mountpoint(string(utils::kStatefulPartition) + |
81 "/au_destination"); | 86 "/au_destination"); |
82 | 87 |
83 string cwd; | 88 string cwd; |
84 { | 89 { |
85 vector<char> buf(1000); | 90 vector<char> buf(1000); |
86 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size())); | 91 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size())); |
(...skipping 13 matching lines...) Expand all Loading... |
100 // mount it | 105 // mount it |
101 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint)); | 106 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint)); |
102 | 107 |
103 // put a postinst script in | 108 // put a postinst script in |
104 string script = StringPrintf("#!/bin/bash\n" | 109 string script = StringPrintf("#!/bin/bash\n" |
105 "mount | grep au_postint_mount | grep ext2\n" | 110 "mount | grep au_postint_mount | grep ext2\n" |
106 "if [ $? -eq 0 ]; then\n" | 111 "if [ $? -eq 0 ]; then\n" |
107 " touch %s/postinst_called\n" | 112 " touch %s/postinst_called\n" |
108 "fi\n", | 113 "fi\n", |
109 cwd.c_str()); | 114 cwd.c_str()); |
110 if (do_err_script) { | 115 if (err_code) { |
111 script = "#!/bin/bash\nexit 1"; | 116 script = StringPrintf("#!/bin/bash\nexit %d", err_code); |
112 } | 117 } |
113 ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script)); | 118 ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script)); |
114 ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst")); | 119 ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst")); |
115 | 120 |
116 ASSERT_EQ(0, System(string("umount -d ") + mountpoint)); | 121 ASSERT_EQ(0, System(string("umount -d ") + mountpoint)); |
117 | 122 |
118 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); | 123 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
119 | 124 |
120 // get a loop device we can use for the install device | 125 // get a loop device we can use for the install device |
121 FILE* find_dev_cmd = popen("losetup -f", "r"); | 126 FILE* find_dev_cmd = popen("losetup -f", "r"); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 processor.set_delegate(&delegate); | 159 processor.set_delegate(&delegate); |
155 | 160 |
156 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); | 161 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
157 delegate.loop_ = loop; | 162 delegate.loop_ = loop; |
158 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 163 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
159 g_main_loop_run(loop); | 164 g_main_loop_run(loop); |
160 g_main_loop_unref(loop); | 165 g_main_loop_unref(loop); |
161 ASSERT_FALSE(processor.IsRunning()); | 166 ASSERT_FALSE(processor.IsRunning()); |
162 | 167 |
163 EXPECT_TRUE(delegate.code_set_); | 168 EXPECT_TRUE(delegate.code_set_); |
164 EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess); | 169 EXPECT_EQ(do_losetup && !err_code, delegate.code_ == kActionCodeSuccess); |
165 EXPECT_EQ(do_losetup && !do_err_script, | 170 EXPECT_EQ(do_losetup && !err_code, |
166 !collector_action.object().install_path.empty()); | 171 !collector_action.object().install_path.empty()); |
167 if (do_losetup && !do_err_script) { | 172 if (do_losetup && !err_code) { |
168 EXPECT_TRUE(install_plan == collector_action.object()); | 173 EXPECT_TRUE(install_plan == collector_action.object()); |
169 } | 174 } |
| 175 if (err_code == 2) |
| 176 EXPECT_EQ(kActionCodePostinstallBootedFromFirmwareB, delegate.code_); |
170 | 177 |
171 struct stat stbuf; | 178 struct stat stbuf; |
172 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); | 179 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); |
173 if (do_losetup && !do_err_script) | 180 if (do_losetup && !err_code) |
174 ASSERT_EQ(0, rc); | 181 ASSERT_EQ(0, rc); |
175 else | 182 else |
176 ASSERT_LT(rc, 0); | 183 ASSERT_LT(rc, 0); |
177 | 184 |
178 if (do_losetup) { | 185 if (do_losetup) { |
179 loop_releaser.reset(NULL); | 186 loop_releaser.reset(NULL); |
180 } | 187 } |
181 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); | 188 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
182 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat")); | 189 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat")); |
183 } | 190 } |
184 | 191 |
185 // Death tests don't seem to be working on Hardy | 192 // Death tests don't seem to be working on Hardy |
186 TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { | 193 TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
187 ASSERT_EQ(0, getuid()); | 194 ASSERT_EQ(0, getuid()); |
188 PostinstallRunnerAction runner_action; | 195 PostinstallRunnerAction runner_action; |
189 ASSERT_DEATH({ runner_action.TerminateProcessing(); }, | 196 ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
190 "postinstall_runner_action.h:.*] Check failed"); | 197 "postinstall_runner_action.h:.*] Check failed"); |
191 } | 198 } |
192 | 199 |
193 } // namespace chromeos_update_engine | 200 } // namespace chromeos_update_engine |
OLD | NEW |