blob: 84aeae14e5307943f3ef40adef703ddc2b5bf1a0 [file] [log] [blame]
Gilad Arnoldc33faeb2012-07-24 15:11:11 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_TEST_UTILS_H_
6#define UPDATE_ENGINE_TEST_UTILS_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
Gilad Arnold2c2b8842013-07-16 06:44:37 -07008#include <sys/stat.h>
9#include <sys/types.h>
10#include <unistd.h>
11
Alex Deymo52490e72015-06-04 14:53:44 +020012// Streams used for gtest's PrintTo() functions.
13#include <iostream> // NOLINT(readability/streams)
Ben Chan02f7c1d2014-10-18 15:18:02 -070014#include <memory>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000015#include <set>
rspangler@google.com49fdf182009-10-10 00:57:34 +000016#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000017#include <vector>
Thieu Le5c7d9752010-12-15 16:09:28 -080018
Alex Deymo53556ec2014-03-17 10:05:57 -070019#include <base/callback.h>
Alex Deymo2b19cfb2015-03-26 00:35:07 -070020#include <base/files/file_path.h>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000021#include <gtest/gtest.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080022
adlr@google.comc98a7ed2009-12-04 18:54:03 +000023#include "update_engine/action.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070024#include "update_engine/subprocess.h"
Alex Deymo52490e72015-06-04 14:53:44 +020025#include "update_engine/update_metadata.pb.h"
Andrew de los Reyesf9185172010-05-03 11:07:05 -070026#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
28// These are some handy functions for unittests.
29
30namespace chromeos_update_engine {
Alex Deymo52490e72015-06-04 14:53:44 +020031
32// PrintTo() functions are used by gtest to log these objects. These PrintTo()
33// functions must be defined in the same namespace as the first argument.
34void PrintTo(const Extent& extent, ::std::ostream* os);
35
Alex Deymo10875d92014-11-10 21:52:57 -080036namespace test_utils {
37
38// 300 byte pseudo-random string. Not null terminated.
39// This does not gzip compress well.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080040extern const uint8_t kRandomString[300];
rspangler@google.com49fdf182009-10-10 00:57:34 +000041
rspangler@google.com49fdf182009-10-10 00:57:34 +000042// Writes the data passed to path. The file at path will be overwritten if it
43// exists. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080044bool WriteFileVector(const std::string& path, const chromeos::Blob& data);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000045bool WriteFileString(const std::string& path, const std::string& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000046
Gilad Arnold19a45f02012-07-19 12:36:10 -070047bool BindToUnusedLoopDevice(const std::string &filename,
48 std::string* lo_dev_name_ptr);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000049
50// Returns true iff a == b
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080051bool ExpectVectorsEq(const chromeos::Blob& a, const chromeos::Blob& b);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000052
53inline int System(const std::string& cmd) {
54 return system(cmd.c_str());
55}
56
Gilad Arnold2c2b8842013-07-16 06:44:37 -070057inline int Symlink(const std::string& oldpath, const std::string& newpath) {
58 return symlink(oldpath.c_str(), newpath.c_str());
59}
60
61inline int Chmod(const std::string& path, mode_t mode) {
62 return chmod(path.c_str(), mode);
63}
64
65inline int Mkdir(const std::string& path, mode_t mode) {
66 return mkdir(path.c_str(), mode);
67}
68
Gilad Arnold30dedd82013-07-03 06:19:09 -070069inline int Chdir(const std::string& path) {
70 return chdir(path.c_str());
71}
72
Alex Deymo10875d92014-11-10 21:52:57 -080073// Checks if xattr is supported in the directory specified by
74// |dir_path| which must be writable. Returns true if the feature is
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080075// supported, false if not or if an error occurred.
Alex Deymo10875d92014-11-10 21:52:57 -080076bool IsXAttrSupported(const base::FilePath& dir_path);
77
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080078void FillWithData(chromeos::Blob* buffer);
Andrew de los Reyes80061062010-02-04 14:25:00 -080079
Thieu Le5c7d9752010-12-15 16:09:28 -080080// Creates an empty ext image.
81void CreateEmptyExtImageAtPath(const std::string& path,
82 size_t size,
83 int block_size);
84
adlr@google.comc98a7ed2009-12-04 18:54:03 +000085// Creates an ext image with some files in it. The paths creates are
86// returned in out_paths.
87void CreateExtImageAtPath(const std::string& path,
88 std::vector<std::string>* out_paths);
89
Alex Deymo10875d92014-11-10 21:52:57 -080090// Class to unmount FS when object goes out of scope
91class ScopedFilesystemUnmounter {
92 public:
93 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
94 : mountpoint_(mountpoint),
95 should_unmount_(true) {}
96 ~ScopedFilesystemUnmounter() {
97 if (should_unmount_) {
98 utils::UnmountFilesystem(mountpoint_);
99 }
100 }
101 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
102 private:
103 const std::string mountpoint_;
104 bool should_unmount_;
105 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
106};
107
Don Garrett58e8b1f2012-01-31 16:38:16 -0800108class ScopedLoopbackDeviceBinder {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700109 public:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800110 ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700111 is_bound_ = BindToUnusedLoopDevice(file, &dev_);
112 EXPECT_TRUE(is_bound_);
Don Garrett58e8b1f2012-01-31 16:38:16 -0800113
Gilad Arnold19a45f02012-07-19 12:36:10 -0700114 if (is_bound_ && dev)
Don Garrett58e8b1f2012-01-31 16:38:16 -0800115 *dev = dev_;
116 }
117
118 ~ScopedLoopbackDeviceBinder() {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700119 if (!is_bound_)
120 return;
121
Darin Petkovcf562482010-12-03 10:31:00 -0800122 for (int retry = 0; retry < 5; retry++) {
123 std::vector<std::string> args;
124 args.push_back("/sbin/losetup");
125 args.push_back("-d");
126 args.push_back(dev_);
127 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700128 EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, nullptr));
Darin Petkovcf562482010-12-03 10:31:00 -0800129 if (return_code == 0) {
130 return;
131 }
132 sleep(1);
133 }
134 ADD_FAILURE();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700135 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800136
Gilad Arnold19a45f02012-07-19 12:36:10 -0700137 const std::string &dev() {
138 EXPECT_TRUE(is_bound_);
139 return dev_;
140 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800141
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700142 bool is_bound() const { return is_bound_; }
143
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700144 private:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800145 std::string dev_;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700146 bool is_bound_;
Don Garrett58e8b1f2012-01-31 16:38:16 -0800147 DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700148};
149
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150class ScopedTempFile {
151 public:
152 ScopedTempFile() {
153 EXPECT_TRUE(utils::MakeTempFile("/tmp/update_engine_test_temp_file.XXXXXX",
154 &path_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700155 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700156 unlinker_.reset(new ScopedPathUnlinker(path_));
157 }
158 const std::string& GetPath() { return path_; }
159 private:
160 std::string path_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700161 std::unique_ptr<ScopedPathUnlinker> unlinker_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700162};
163
Alex Deymo10875d92014-11-10 21:52:57 -0800164class ScopedLoopMounter {
165 public:
166 explicit ScopedLoopMounter(const std::string& file_path,
167 std::string* mnt_path,
168 unsigned long flags); // NOLINT(runtime/int)
169
170 private:
171 // These objects must be destructed in the following order:
172 // ScopedFilesystemUnmounter (the file system must be unmounted first)
173 // ScopedLoopbackDeviceBinder (then the loop device can be deleted)
174 // ScopedDirRemover (then the mount point can be deleted)
175 std::unique_ptr<ScopedDirRemover> dir_remover_;
176 std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
177 std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
178};
179
180// Deletes a directory and all its contents synchronously. Returns true
181// on success. This may be called with a regular file--it will just unlink it.
182// This WILL cross filesystem boundaries.
183bool RecursiveUnlinkDir(const std::string& path);
184
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700185// Returns the path where the build artifacts are stored. This is the directory
186// where the unittest executable is being run from.
187base::FilePath GetBuildArtifactsPath();
Alex Deymo10875d92014-11-10 21:52:57 -0800188
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700189} // namespace test_utils
Alex Deymo10875d92014-11-10 21:52:57 -0800190
191// Useful actions for test. These need to be defined in the
192// chromeos_update_engine namespace.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000193
194class NoneType;
195
196template<typename T>
197class ObjectFeederAction;
198
199template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700200class ActionTraits<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000201 public:
202 typedef T OutputObjectType;
203 typedef NoneType InputObjectType;
204};
205
206// This is a simple Action class for testing. It feeds an object into
207// another action.
208template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700209class ObjectFeederAction : public Action<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000210 public:
211 typedef NoneType InputObjectType;
212 typedef T OutputObjectType;
213 void PerformAction() {
214 LOG(INFO) << "feeder running!";
215 CHECK(this->processor_);
216 if (this->HasOutputPipe()) {
217 this->SetOutputObject(out_obj_);
218 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700219 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000220 }
221 static std::string StaticType() { return "ObjectFeederAction"; }
222 std::string Type() const { return StaticType(); }
223 void set_obj(const T& out_obj) {
224 out_obj_ = out_obj;
225 }
226 private:
227 T out_obj_;
228};
229
230template<typename T>
231class ObjectCollectorAction;
232
233template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700234class ActionTraits<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000235 public:
236 typedef NoneType OutputObjectType;
237 typedef T InputObjectType;
238};
239
240// This is a simple Action class for testing. It receives an object from
241// another action.
242template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700243class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000244 public:
245 typedef T InputObjectType;
246 typedef NoneType OutputObjectType;
247 void PerformAction() {
248 LOG(INFO) << "collector running!";
249 ASSERT_TRUE(this->processor_);
250 if (this->HasInputObject()) {
251 object_ = this->GetInputObject();
252 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700253 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000254 }
255 static std::string StaticType() { return "ObjectCollectorAction"; }
256 std::string Type() const { return StaticType(); }
257 const T& object() const { return object_; }
258 private:
259 T object_;
260};
261
rspangler@google.com49fdf182009-10-10 00:57:34 +0000262} // namespace chromeos_update_engine
263
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700264#endif // UPDATE_ENGINE_TEST_UTILS_H_