blob: 7be027a03e246b537d55df04409f4de0f8445cc4 [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>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000033#include <gtest/gtest.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080034
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/action.h"
36#include "update_engine/common/subprocess.h"
37#include "update_engine/common/utils.h"
Alex Deymo52490e72015-06-04 14:53:44 +020038#include "update_engine/update_metadata.pb.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000039
40// These are some handy functions for unittests.
41
42namespace chromeos_update_engine {
Alex Deymo52490e72015-06-04 14:53:44 +020043
44// PrintTo() functions are used by gtest to log these objects. These PrintTo()
45// functions must be defined in the same namespace as the first argument.
46void PrintTo(const Extent& extent, ::std::ostream* os);
Alex Deymo64d98782016-02-05 18:03:48 -080047void PrintTo(const ErrorCode& error_code, ::std::ostream* os);
Alex Deymo52490e72015-06-04 14:53:44 +020048
Alex Deymo10875d92014-11-10 21:52:57 -080049namespace test_utils {
50
51// 300 byte pseudo-random string. Not null terminated.
52// This does not gzip compress well.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080053extern const uint8_t kRandomString[300];
rspangler@google.com49fdf182009-10-10 00:57:34 +000054
rspangler@google.com49fdf182009-10-10 00:57:34 +000055// Writes the data passed to path. The file at path will be overwritten if it
56// exists. Returns true on success, false otherwise.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070057bool WriteFileVector(const std::string& path, const brillo::Blob& data);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000058bool WriteFileString(const std::string& path, const std::string& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
Gilad Arnold19a45f02012-07-19 12:36:10 -070060bool BindToUnusedLoopDevice(const std::string &filename,
61 std::string* lo_dev_name_ptr);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000062
63// Returns true iff a == b
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070064bool ExpectVectorsEq(const brillo::Blob& a, const brillo::Blob& b);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000065
66inline int System(const std::string& cmd) {
67 return system(cmd.c_str());
68}
69
Gilad Arnold2c2b8842013-07-16 06:44:37 -070070inline int Symlink(const std::string& oldpath, const std::string& newpath) {
71 return symlink(oldpath.c_str(), newpath.c_str());
72}
73
74inline int Chmod(const std::string& path, mode_t mode) {
75 return chmod(path.c_str(), mode);
76}
77
78inline int Mkdir(const std::string& path, mode_t mode) {
79 return mkdir(path.c_str(), mode);
80}
81
Gilad Arnold30dedd82013-07-03 06:19:09 -070082inline int Chdir(const std::string& path) {
83 return chdir(path.c_str());
84}
85
Alex Deymo10875d92014-11-10 21:52:57 -080086// Checks if xattr is supported in the directory specified by
87// |dir_path| which must be writable. Returns true if the feature is
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080088// supported, false if not or if an error occurred.
Alex Deymo10875d92014-11-10 21:52:57 -080089bool IsXAttrSupported(const base::FilePath& dir_path);
90
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070091void FillWithData(brillo::Blob* buffer);
Andrew de los Reyes80061062010-02-04 14:25:00 -080092
Thieu Le5c7d9752010-12-15 16:09:28 -080093// Creates an empty ext image.
94void CreateEmptyExtImageAtPath(const std::string& path,
95 size_t size,
96 int block_size);
97
adlr@google.comc98a7ed2009-12-04 18:54:03 +000098// Creates an ext image with some files in it. The paths creates are
99// returned in out_paths.
100void CreateExtImageAtPath(const std::string& path,
101 std::vector<std::string>* out_paths);
102
Alex Deymo10875d92014-11-10 21:52:57 -0800103// Class to unmount FS when object goes out of scope
104class ScopedFilesystemUnmounter {
105 public:
106 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
107 : mountpoint_(mountpoint),
108 should_unmount_(true) {}
109 ~ScopedFilesystemUnmounter() {
110 if (should_unmount_) {
111 utils::UnmountFilesystem(mountpoint_);
112 }
113 }
114 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
115 private:
116 const std::string mountpoint_;
117 bool should_unmount_;
118 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
119};
120
Don Garrett58e8b1f2012-01-31 16:38:16 -0800121class ScopedLoopbackDeviceBinder {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700122 public:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800123 ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700124 is_bound_ = BindToUnusedLoopDevice(file, &dev_);
125 EXPECT_TRUE(is_bound_);
Don Garrett58e8b1f2012-01-31 16:38:16 -0800126
Gilad Arnold19a45f02012-07-19 12:36:10 -0700127 if (is_bound_ && dev)
Don Garrett58e8b1f2012-01-31 16:38:16 -0800128 *dev = dev_;
129 }
130
131 ~ScopedLoopbackDeviceBinder() {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700132 if (!is_bound_)
133 return;
134
Darin Petkovcf562482010-12-03 10:31:00 -0800135 for (int retry = 0; retry < 5; retry++) {
136 std::vector<std::string> args;
137 args.push_back("/sbin/losetup");
138 args.push_back("-d");
139 args.push_back(dev_);
140 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700141 EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, nullptr));
Darin Petkovcf562482010-12-03 10:31:00 -0800142 if (return_code == 0) {
143 return;
144 }
145 sleep(1);
146 }
147 ADD_FAILURE();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700148 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800149
Gilad Arnold19a45f02012-07-19 12:36:10 -0700150 const std::string &dev() {
151 EXPECT_TRUE(is_bound_);
152 return dev_;
153 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800154
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700155 bool is_bound() const { return is_bound_; }
156
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700157 private:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800158 std::string dev_;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700159 bool is_bound_;
Don Garrett58e8b1f2012-01-31 16:38:16 -0800160 DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700161};
162
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700163class ScopedTempFile {
164 public:
Alex Deymobffa0602016-02-12 17:16:29 -0800165 ScopedTempFile() : ScopedTempFile("update_engine_test_temp_file.XXXXXX") {}
166
167 explicit ScopedTempFile(const std::string& pattern) {
168 EXPECT_TRUE(utils::MakeTempFile(pattern, &path_, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700169 unlinker_.reset(new ScopedPathUnlinker(path_));
170 }
Alex Deymobffa0602016-02-12 17:16:29 -0800171
172 const std::string& path() { return path_; }
173
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700174 private:
175 std::string path_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700176 std::unique_ptr<ScopedPathUnlinker> unlinker_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700177};
178
Alex Deymo10875d92014-11-10 21:52:57 -0800179class ScopedLoopMounter {
180 public:
181 explicit ScopedLoopMounter(const std::string& file_path,
182 std::string* mnt_path,
183 unsigned long flags); // NOLINT(runtime/int)
184
185 private:
186 // These objects must be destructed in the following order:
187 // ScopedFilesystemUnmounter (the file system must be unmounted first)
188 // ScopedLoopbackDeviceBinder (then the loop device can be deleted)
189 // ScopedDirRemover (then the mount point can be deleted)
190 std::unique_ptr<ScopedDirRemover> dir_remover_;
191 std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
192 std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
193};
194
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700195// Returns the path where the build artifacts are stored. This is the directory
196// where the unittest executable is being run from.
197base::FilePath GetBuildArtifactsPath();
Alex Deymo10875d92014-11-10 21:52:57 -0800198
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700199} // namespace test_utils
Alex Deymo10875d92014-11-10 21:52:57 -0800200
201// Useful actions for test. These need to be defined in the
202// chromeos_update_engine namespace.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000203
204class NoneType;
205
206template<typename T>
207class ObjectFeederAction;
208
209template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700210class ActionTraits<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000211 public:
212 typedef T OutputObjectType;
213 typedef NoneType InputObjectType;
214};
215
216// This is a simple Action class for testing. It feeds an object into
217// another action.
218template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700219class ObjectFeederAction : public Action<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000220 public:
221 typedef NoneType InputObjectType;
222 typedef T OutputObjectType;
223 void PerformAction() {
224 LOG(INFO) << "feeder running!";
225 CHECK(this->processor_);
226 if (this->HasOutputPipe()) {
227 this->SetOutputObject(out_obj_);
228 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700229 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000230 }
231 static std::string StaticType() { return "ObjectFeederAction"; }
232 std::string Type() const { return StaticType(); }
233 void set_obj(const T& out_obj) {
234 out_obj_ = out_obj;
235 }
236 private:
237 T out_obj_;
238};
239
240template<typename T>
241class ObjectCollectorAction;
242
243template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700244class ActionTraits<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000245 public:
246 typedef NoneType OutputObjectType;
247 typedef T InputObjectType;
248};
249
250// This is a simple Action class for testing. It receives an object from
251// another action.
252template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700253class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000254 public:
255 typedef T InputObjectType;
256 typedef NoneType OutputObjectType;
257 void PerformAction() {
258 LOG(INFO) << "collector running!";
259 ASSERT_TRUE(this->processor_);
260 if (this->HasInputObject()) {
261 object_ = this->GetInputObject();
262 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700263 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000264 }
265 static std::string StaticType() { return "ObjectCollectorAction"; }
266 std::string Type() const { return StaticType(); }
267 const T& object() const { return object_; }
268 private:
269 T object_;
270};
271
rspangler@google.com49fdf182009-10-10 00:57:34 +0000272} // namespace chromeos_update_engine
273
Alex Deymo39910dc2015-11-09 17:04:30 -0800274#endif // UPDATE_ENGINE_COMMON_TEST_UTILS_H_