1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
//
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/payload_consumer/filesystem_verifier_action.h"
#include <fcntl.h>
#include <set>
#include <string>
#include <vector>
#include <base/bind.h>
#include <base/posix/eintr_wrapper.h>
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
#include <brillo/bind_lambda.h>
#include <brillo/message_loops/fake_message_loop.h>
#include <brillo/message_loops/message_loop_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "update_engine/common/hash_calculator.h"
#include "update_engine/common/test_utils.h"
#include "update_engine/common/utils.h"
#include "update_engine/payload_consumer/payload_constants.h"
using brillo::MessageLoop;
using std::set;
using std::string;
using std::vector;
namespace chromeos_update_engine {
class FilesystemVerifierActionTest : public ::testing::Test {
protected:
void SetUp() override {
loop_.SetAsCurrent();
}
void TearDown() override {
EXPECT_EQ(0, brillo::MessageLoopRunMaxIterations(&loop_, 1));
}
// Returns true iff test has completed successfully.
bool DoTest(bool terminate_early, bool hash_fail);
brillo::FakeMessageLoop loop_{nullptr};
};
class FilesystemVerifierActionTestDelegate : public ActionProcessorDelegate {
public:
explicit FilesystemVerifierActionTestDelegate(
FilesystemVerifierAction* action)
: action_(action), ran_(false), code_(ErrorCode::kError) {}
void ExitMainLoop() {
// We need to wait for the Action to call Cleanup.
if (action_->IsCleanupPending()) {
LOG(INFO) << "Waiting for Cleanup() to be called.";
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&FilesystemVerifierActionTestDelegate::ExitMainLoop,
base::Unretained(this)),
base::TimeDelta::FromMilliseconds(100));
} else {
MessageLoop::current()->BreakLoop();
}
}
void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
ExitMainLoop();
}
void ProcessingStopped(const ActionProcessor* processor) {
ExitMainLoop();
}
void ActionCompleted(ActionProcessor* processor,
AbstractAction* action,
ErrorCode code) {
if (action->Type() == FilesystemVerifierAction::StaticType()) {
ran_ = true;
code_ = code;
}
}
bool ran() const { return ran_; }
ErrorCode code() const { return code_; }
private:
FilesystemVerifierAction* action_;
bool ran_;
ErrorCode code_;
};
void StartProcessorInRunLoop(ActionProcessor* processor,
FilesystemVerifierAction* filesystem_copier_action,
bool terminate_early) {
processor->StartProcessing();
if (terminate_early) {
EXPECT_NE(nullptr, filesystem_copier_action);
processor->StopProcessing();
}
}
bool FilesystemVerifierActionTest::DoTest(bool terminate_early,
bool hash_fail) {
string a_loop_file;
if (!(utils::MakeTempFile("a_loop_file.XXXXXX", &a_loop_file, nullptr))) {
ADD_FAILURE();
return false;
}
ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
// Make random data for a.
const size_t kLoopFileSize = 10 * 1024 * 1024 + 512;
brillo::Blob a_loop_data(kLoopFileSize);
test_utils::FillWithData(&a_loop_data);
// Write data to disk
if (!(test_utils::WriteFileVector(a_loop_file, a_loop_data))) {
ADD_FAILURE();
return false;
}
// Attach loop devices to the files
string a_dev;
test_utils::ScopedLoopbackDeviceBinder a_dev_releaser(
a_loop_file, false, &a_dev);
if (!(a_dev_releaser.is_bound())) {
ADD_FAILURE();
return false;
}
LOG(INFO) << "verifying: " << a_loop_file << " (" << a_dev << ")";
bool success = true;
// Set up the action objects
InstallPlan install_plan;
install_plan.source_slot = 0;
install_plan.target_slot = 1;
InstallPlan::Partition part;
part.name = "part";
part.target_size = kLoopFileSize - (hash_fail ? 1 : 0);
part.target_path = a_dev;
if (!HashCalculator::RawHashOfData(a_loop_data, &part.target_hash)) {
ADD_FAILURE();
success = false;
}
part.source_size = kLoopFileSize;
part.source_path = a_dev;
if (!HashCalculator::RawHashOfData(a_loop_data, &part.source_hash)) {
ADD_FAILURE();
success = false;
}
install_plan.partitions = {part};
ActionProcessor processor;
ObjectFeederAction<InstallPlan> feeder_action;
FilesystemVerifierAction copier_action;
ObjectCollectorAction<InstallPlan> collector_action;
BondActions(&feeder_action, &copier_action);
BondActions(&copier_action, &collector_action);
FilesystemVerifierActionTestDelegate delegate(&copier_action);
processor.set_delegate(&delegate);
processor.EnqueueAction(&feeder_action);
processor.EnqueueAction(&copier_action);
processor.EnqueueAction(&collector_action);
feeder_action.set_obj(install_plan);
loop_.PostTask(FROM_HERE, base::Bind(&StartProcessorInRunLoop,
&processor,
&copier_action,
terminate_early));
loop_.Run();
if (!terminate_early) {
bool is_delegate_ran = delegate.ran();
EXPECT_TRUE(is_delegate_ran);
success = success && is_delegate_ran;
} else {
EXPECT_EQ(ErrorCode::kError, delegate.code());
return (ErrorCode::kError == delegate.code());
}
if (hash_fail) {
ErrorCode expected_exit_code = ErrorCode::kNewRootfsVerificationError;
EXPECT_EQ(expected_exit_code, delegate.code());
return (expected_exit_code == delegate.code());
}
EXPECT_EQ(ErrorCode::kSuccess, delegate.code());
// Make sure everything in the out_image is there
brillo::Blob a_out;
if (!utils::ReadFile(a_dev, &a_out)) {
ADD_FAILURE();
return false;
}
const bool is_a_file_reading_eq =
test_utils::ExpectVectorsEq(a_loop_data, a_out);
EXPECT_TRUE(is_a_file_reading_eq);
success = success && is_a_file_reading_eq;
bool is_install_plan_eq = (collector_action.object() == install_plan);
EXPECT_TRUE(is_install_plan_eq);
success = success && is_install_plan_eq;
return success;
}
class FilesystemVerifierActionTest2Delegate : public ActionProcessorDelegate {
public:
void ActionCompleted(ActionProcessor* processor,
AbstractAction* action,
ErrorCode code) {
if (action->Type() == FilesystemVerifierAction::StaticType()) {
ran_ = true;
code_ = code;
}
}
bool ran_;
ErrorCode code_;
};
TEST_F(FilesystemVerifierActionTest, MissingInputObjectTest) {
ActionProcessor processor;
FilesystemVerifierActionTest2Delegate delegate;
processor.set_delegate(&delegate);
FilesystemVerifierAction copier_action;
ObjectCollectorAction<InstallPlan> collector_action;
BondActions(&copier_action, &collector_action);
processor.EnqueueAction(&copier_action);
processor.EnqueueAction(&collector_action);
processor.StartProcessing();
EXPECT_FALSE(processor.IsRunning());
EXPECT_TRUE(delegate.ran_);
EXPECT_EQ(ErrorCode::kError, delegate.code_);
}
TEST_F(FilesystemVerifierActionTest, NonExistentDriveTest) {
ActionProcessor processor;
FilesystemVerifierActionTest2Delegate delegate;
processor.set_delegate(&delegate);
ObjectFeederAction<InstallPlan> feeder_action;
InstallPlan install_plan;
InstallPlan::Partition part;
part.name = "nope";
part.source_path = "/no/such/file";
part.target_path = "/no/such/file";
install_plan.partitions = {part};
feeder_action.set_obj(install_plan);
FilesystemVerifierAction verifier_action;
ObjectCollectorAction<InstallPlan> collector_action;
BondActions(&verifier_action, &collector_action);
processor.EnqueueAction(&feeder_action);
processor.EnqueueAction(&verifier_action);
processor.EnqueueAction(&collector_action);
processor.StartProcessing();
EXPECT_FALSE(processor.IsRunning());
EXPECT_TRUE(delegate.ran_);
EXPECT_EQ(ErrorCode::kError, delegate.code_);
}
TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashTest) {
ASSERT_EQ(0U, getuid());
EXPECT_TRUE(DoTest(false, false));
}
TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashFailTest) {
ASSERT_EQ(0U, getuid());
EXPECT_TRUE(DoTest(false, true));
}
TEST_F(FilesystemVerifierActionTest, RunAsRootTerminateEarlyTest) {
ASSERT_EQ(0U, getuid());
EXPECT_TRUE(DoTest(true, false));
// TerminateEarlyTest may leak some null callbacks from the Stream class.
while (loop_.RunOnce(false)) {}
}
} // namespace chromeos_update_engine
|