blob: ba9f5f2ffc425852ab3f2b1ff17e94435dcf3312 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_TEST_UTILS_H_
18#define UPDATE_ENGINE_COMMON_TEST_UTILS_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Gilad Arnold2c2b8842013-07-16 06:44:37 -070020#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
Alex Deymo52490e72015-06-04 14:53:44 +020024// Streams used for gtest's PrintTo() functions.
25#include <iostream> // NOLINT(readability/streams)
Ben Chan02f7c1d2014-10-18 15:18:02 -070026#include <memory>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000027#include <set>
rspangler@google.com49fdf182009-10-10 00:57:34 +000028#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000029#include <vector>
Thieu Le5c7d9752010-12-15 16:09:28 -080030
Alex Deymo53556ec2014-03-17 10:05:57 -070031#include <base/callback.h>
Alex Deymo2b19cfb2015-03-26 00:35:07 -070032#include <base/files/file_path.h>
Sen Jiang371615b2016-04-13 15:54:29 -070033#include <base/files/scoped_temp_dir.h>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000034#include <gtest/gtest.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080035
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/action.h"
37#include "update_engine/common/subprocess.h"
38#include "update_engine/common/utils.h"
Alex Deymo52490e72015-06-04 14:53:44 +020039#include "update_engine/update_metadata.pb.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000040
41// These are some handy functions for unittests.
42
43namespace chromeos_update_engine {
Alex Deymo52490e72015-06-04 14:53:44 +020044
45// PrintTo() functions are used by gtest to log these objects. These PrintTo()
46// functions must be defined in the same namespace as the first argument.
47void PrintTo(const Extent& extent, ::std::ostream* os);
Alex Deymo64d98782016-02-05 18:03:48 -080048void PrintTo(const ErrorCode& error_code, ::std::ostream* os);
Alex Deymo52490e72015-06-04 14:53:44 +020049
Alex Deymo10875d92014-11-10 21:52:57 -080050namespace test_utils {
51
52// 300 byte pseudo-random string. Not null terminated.
53// This does not gzip compress well.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080054extern const uint8_t kRandomString[300];
rspangler@google.com49fdf182009-10-10 00:57:34 +000055
rspangler@google.com49fdf182009-10-10 00:57:34 +000056// Writes the data passed to path. The file at path will be overwritten if it
57// exists. Returns true on success, false otherwise.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070058bool WriteFileVector(const std::string& path, const brillo::Blob& data);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000059bool WriteFileString(const std::string& path, const std::string& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000060
Alex Deymocbc22742016-03-04 17:53:02 -080061// Binds provided |filename| to an unused loopback device, whose name is written
62// to the string pointed to by |out_lo_dev_name|. The new loop device will be
63// read-only unless |writable| is set to true. Returns true on success, false
64// otherwise (along with corresponding test failures), in which case the content
65// of |out_lo_dev_name| is unknown.
66bool BindToUnusedLoopDevice(const std::string& filename,
67 bool writable,
68 std::string* out_lo_dev_name);
69bool UnbindLoopDevice(const std::string& lo_dev_name);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000070
71// Returns true iff a == b
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070072bool ExpectVectorsEq(const brillo::Blob& a, const brillo::Blob& b);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000073
74inline int System(const std::string& cmd) {
75 return system(cmd.c_str());
76}
77
Gilad Arnold2c2b8842013-07-16 06:44:37 -070078inline int Symlink(const std::string& oldpath, const std::string& newpath) {
79 return symlink(oldpath.c_str(), newpath.c_str());
80}
81
82inline int Chmod(const std::string& path, mode_t mode) {
83 return chmod(path.c_str(), mode);
84}
85
86inline int Mkdir(const std::string& path, mode_t mode) {
87 return mkdir(path.c_str(), mode);
88}
89
Gilad Arnold30dedd82013-07-03 06:19:09 -070090inline int Chdir(const std::string& path) {
91 return chdir(path.c_str());
92}
93
Alex Deymod15c5462016-03-09 18:11:12 -080094// Reads a symlink from disk. Returns empty string on failure.
95std::string Readlink(const std::string& path);
96
Alex Deymo10875d92014-11-10 21:52:57 -080097// Checks if xattr is supported in the directory specified by
98// |dir_path| which must be writable. Returns true if the feature is
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080099// supported, false if not or if an error occurred.
Alex Deymo10875d92014-11-10 21:52:57 -0800100bool IsXAttrSupported(const base::FilePath& dir_path);
101
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700102void FillWithData(brillo::Blob* buffer);
Andrew de los Reyes80061062010-02-04 14:25:00 -0800103
Alex Deymo10875d92014-11-10 21:52:57 -0800104// Class to unmount FS when object goes out of scope
105class ScopedFilesystemUnmounter {
106 public:
107 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
108 : mountpoint_(mountpoint),
109 should_unmount_(true) {}
110 ~ScopedFilesystemUnmounter() {
111 if (should_unmount_) {
112 utils::UnmountFilesystem(mountpoint_);
113 }
114 }
115 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
116 private:
117 const std::string mountpoint_;
118 bool should_unmount_;
119 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
120};
121
Don Garrett58e8b1f2012-01-31 16:38:16 -0800122class ScopedLoopbackDeviceBinder {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700123 public:
Alex Deymocbc22742016-03-04 17:53:02 -0800124 ScopedLoopbackDeviceBinder(const std::string& file,
125 bool writable,
126 std::string* dev) {
127 is_bound_ = BindToUnusedLoopDevice(file, writable, &dev_);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700128 EXPECT_TRUE(is_bound_);
Don Garrett58e8b1f2012-01-31 16:38:16 -0800129
Gilad Arnold19a45f02012-07-19 12:36:10 -0700130 if (is_bound_ && dev)
Don Garrett58e8b1f2012-01-31 16:38:16 -0800131 *dev = dev_;
132 }
133
134 ~ScopedLoopbackDeviceBinder() {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700135 if (!is_bound_)
136 return;
137
Darin Petkovcf562482010-12-03 10:31:00 -0800138 for (int retry = 0; retry < 5; retry++) {
Alex Deymocbc22742016-03-04 17:53:02 -0800139 if (UnbindLoopDevice(dev_))
Darin Petkovcf562482010-12-03 10:31:00 -0800140 return;
Darin Petkovcf562482010-12-03 10:31:00 -0800141 sleep(1);
142 }
143 ADD_FAILURE();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700144 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800145
Gilad Arnold19a45f02012-07-19 12:36:10 -0700146 const std::string &dev() {
147 EXPECT_TRUE(is_bound_);
148 return dev_;
149 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800150
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700151 bool is_bound() const { return is_bound_; }
152
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700153 private:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800154 std::string dev_;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700155 bool is_bound_;
Don Garrett58e8b1f2012-01-31 16:38:16 -0800156 DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700157};
158
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700159class ScopedTempFile {
160 public:
Alex Deymobffa0602016-02-12 17:16:29 -0800161 ScopedTempFile() : ScopedTempFile("update_engine_test_temp_file.XXXXXX") {}
162
163 explicit ScopedTempFile(const std::string& pattern) {
164 EXPECT_TRUE(utils::MakeTempFile(pattern, &path_, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700165 unlinker_.reset(new ScopedPathUnlinker(path_));
166 }
Alex Deymobffa0602016-02-12 17:16:29 -0800167
Alex Deymo2c131bb2016-05-26 16:43:13 -0700168 const std::string& path() const { return path_; }
Alex Deymobffa0602016-02-12 17:16:29 -0800169
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700170 private:
171 std::string path_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700172 std::unique_ptr<ScopedPathUnlinker> unlinker_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700173};
174
Alex Deymo10875d92014-11-10 21:52:57 -0800175class ScopedLoopMounter {
176 public:
177 explicit ScopedLoopMounter(const std::string& file_path,
178 std::string* mnt_path,
179 unsigned long flags); // NOLINT(runtime/int)
180
181 private:
182 // These objects must be destructed in the following order:
183 // ScopedFilesystemUnmounter (the file system must be unmounted first)
184 // ScopedLoopbackDeviceBinder (then the loop device can be deleted)
185 // ScopedDirRemover (then the mount point can be deleted)
Sen Jiang371615b2016-04-13 15:54:29 -0700186 base::ScopedTempDir temp_dir_;
Alex Deymo10875d92014-11-10 21:52:57 -0800187 std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
188 std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
189};
190
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700191// Returns the path where the build artifacts are stored. This is the directory
192// where the unittest executable is being run from.
193base::FilePath GetBuildArtifactsPath();
Sen Jiang260f03b2016-03-21 15:34:58 -0700194// Returns the path of the build artifact specified in |relative_path|.
195std::string GetBuildArtifactsPath(const std::string& relative_path);
Alex Deymo10875d92014-11-10 21:52:57 -0800196
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700197} // namespace test_utils
Alex Deymo10875d92014-11-10 21:52:57 -0800198
199// Useful actions for test. These need to be defined in the
200// chromeos_update_engine namespace.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000201
202class NoneType;
203
204template<typename T>
205class ObjectFeederAction;
206
207template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700208class ActionTraits<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000209 public:
210 typedef T OutputObjectType;
211 typedef NoneType InputObjectType;
212};
213
214// This is a simple Action class for testing. It feeds an object into
215// another action.
216template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700217class ObjectFeederAction : public Action<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000218 public:
219 typedef NoneType InputObjectType;
220 typedef T OutputObjectType;
221 void PerformAction() {
222 LOG(INFO) << "feeder running!";
223 CHECK(this->processor_);
224 if (this->HasOutputPipe()) {
225 this->SetOutputObject(out_obj_);
226 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700227 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000228 }
229 static std::string StaticType() { return "ObjectFeederAction"; }
230 std::string Type() const { return StaticType(); }
231 void set_obj(const T& out_obj) {
232 out_obj_ = out_obj;
233 }
234 private:
235 T out_obj_;
236};
237
238template<typename T>
239class ObjectCollectorAction;
240
241template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700242class ActionTraits<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000243 public:
244 typedef NoneType OutputObjectType;
245 typedef T InputObjectType;
246};
247
248// This is a simple Action class for testing. It receives an object from
249// another action.
250template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700251class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000252 public:
253 typedef T InputObjectType;
254 typedef NoneType OutputObjectType;
255 void PerformAction() {
256 LOG(INFO) << "collector running!";
257 ASSERT_TRUE(this->processor_);
258 if (this->HasInputObject()) {
259 object_ = this->GetInputObject();
260 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700261 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000262 }
263 static std::string StaticType() { return "ObjectCollectorAction"; }
264 std::string Type() const { return StaticType(); }
265 const T& object() const { return object_; }
266 private:
267 T object_;
268};
269
rspangler@google.com49fdf182009-10-10 00:57:34 +0000270} // namespace chromeos_update_engine
271
Alex Deymo39910dc2015-11-09 17:04:30 -0800272#endif // UPDATE_ENGINE_COMMON_TEST_UTILS_H_