blob: 13bbd41f9da5ab9eb94443748d1a5c9cb0ac33df [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>
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070021#include <glib-object.h>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000022#include <gtest/gtest.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080023
adlr@google.comc98a7ed2009-12-04 18:54:03 +000024#include "update_engine/action.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070025#include "update_engine/subprocess.h"
Alex Deymo52490e72015-06-04 14:53:44 +020026#include "update_engine/update_metadata.pb.h"
Andrew de los Reyesf9185172010-05-03 11:07:05 -070027#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000028
29// These are some handy functions for unittests.
30
31namespace chromeos_update_engine {
Alex Deymo52490e72015-06-04 14:53:44 +020032
33// PrintTo() functions are used by gtest to log these objects. These PrintTo()
34// functions must be defined in the same namespace as the first argument.
35void PrintTo(const Extent& extent, ::std::ostream* os);
36
Alex Deymo10875d92014-11-10 21:52:57 -080037namespace test_utils {
38
39// 300 byte pseudo-random string. Not null terminated.
40// This does not gzip compress well.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080041extern const uint8_t kRandomString[300];
rspangler@google.com49fdf182009-10-10 00:57:34 +000042
rspangler@google.com49fdf182009-10-10 00:57:34 +000043// Writes the data passed to path. The file at path will be overwritten if it
44// exists. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080045bool WriteFileVector(const std::string& path, const chromeos::Blob& data);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000046bool WriteFileString(const std::string& path, const std::string& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
Gilad Arnold19a45f02012-07-19 12:36:10 -070048bool BindToUnusedLoopDevice(const std::string &filename,
49 std::string* lo_dev_name_ptr);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000050
51// Returns true iff a == b
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080052bool ExpectVectorsEq(const chromeos::Blob& a, const chromeos::Blob& b);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000053
54inline int System(const std::string& cmd) {
55 return system(cmd.c_str());
56}
57
Gilad Arnold2c2b8842013-07-16 06:44:37 -070058inline int Symlink(const std::string& oldpath, const std::string& newpath) {
59 return symlink(oldpath.c_str(), newpath.c_str());
60}
61
62inline int Chmod(const std::string& path, mode_t mode) {
63 return chmod(path.c_str(), mode);
64}
65
66inline int Mkdir(const std::string& path, mode_t mode) {
67 return mkdir(path.c_str(), mode);
68}
69
Gilad Arnold30dedd82013-07-03 06:19:09 -070070inline int Chdir(const std::string& path) {
71 return chdir(path.c_str());
72}
73
Alex Deymo10875d92014-11-10 21:52:57 -080074// Checks if xattr is supported in the directory specified by
75// |dir_path| which must be writable. Returns true if the feature is
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080076// supported, false if not or if an error occurred.
Alex Deymo10875d92014-11-10 21:52:57 -080077bool IsXAttrSupported(const base::FilePath& dir_path);
78
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080079void FillWithData(chromeos::Blob* buffer);
Andrew de los Reyes80061062010-02-04 14:25:00 -080080
Thieu Le5c7d9752010-12-15 16:09:28 -080081// Creates an empty ext image.
82void CreateEmptyExtImageAtPath(const std::string& path,
83 size_t size,
84 int block_size);
85
adlr@google.comc98a7ed2009-12-04 18:54:03 +000086// Creates an ext image with some files in it. The paths creates are
87// returned in out_paths.
88void CreateExtImageAtPath(const std::string& path,
89 std::vector<std::string>* out_paths);
90
Alex Deymo10875d92014-11-10 21:52:57 -080091// Class to unmount FS when object goes out of scope
92class ScopedFilesystemUnmounter {
93 public:
94 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
95 : mountpoint_(mountpoint),
96 should_unmount_(true) {}
97 ~ScopedFilesystemUnmounter() {
98 if (should_unmount_) {
99 utils::UnmountFilesystem(mountpoint_);
100 }
101 }
102 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
103 private:
104 const std::string mountpoint_;
105 bool should_unmount_;
106 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
107};
108
Don Garrett58e8b1f2012-01-31 16:38:16 -0800109class ScopedLoopbackDeviceBinder {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700110 public:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800111 ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700112 is_bound_ = BindToUnusedLoopDevice(file, &dev_);
113 EXPECT_TRUE(is_bound_);
Don Garrett58e8b1f2012-01-31 16:38:16 -0800114
Gilad Arnold19a45f02012-07-19 12:36:10 -0700115 if (is_bound_ && dev)
Don Garrett58e8b1f2012-01-31 16:38:16 -0800116 *dev = dev_;
117 }
118
119 ~ScopedLoopbackDeviceBinder() {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700120 if (!is_bound_)
121 return;
122
Darin Petkovcf562482010-12-03 10:31:00 -0800123 for (int retry = 0; retry < 5; retry++) {
124 std::vector<std::string> args;
125 args.push_back("/sbin/losetup");
126 args.push_back("-d");
127 args.push_back(dev_);
128 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700129 EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, nullptr));
Darin Petkovcf562482010-12-03 10:31:00 -0800130 if (return_code == 0) {
131 return;
132 }
133 sleep(1);
134 }
135 ADD_FAILURE();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700136 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800137
Gilad Arnold19a45f02012-07-19 12:36:10 -0700138 const std::string &dev() {
139 EXPECT_TRUE(is_bound_);
140 return dev_;
141 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800142
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700143 bool is_bound() const { return is_bound_; }
144
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700145 private:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800146 std::string dev_;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700147 bool is_bound_;
Don Garrett58e8b1f2012-01-31 16:38:16 -0800148 DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700149};
150
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700151class ScopedTempFile {
152 public:
153 ScopedTempFile() {
154 EXPECT_TRUE(utils::MakeTempFile("/tmp/update_engine_test_temp_file.XXXXXX",
155 &path_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700156 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700157 unlinker_.reset(new ScopedPathUnlinker(path_));
158 }
159 const std::string& GetPath() { return path_; }
160 private:
161 std::string path_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700162 std::unique_ptr<ScopedPathUnlinker> unlinker_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700163};
164
Alex Deymo10875d92014-11-10 21:52:57 -0800165class ScopedLoopMounter {
166 public:
167 explicit ScopedLoopMounter(const std::string& file_path,
168 std::string* mnt_path,
169 unsigned long flags); // NOLINT(runtime/int)
170
171 private:
172 // These objects must be destructed in the following order:
173 // ScopedFilesystemUnmounter (the file system must be unmounted first)
174 // ScopedLoopbackDeviceBinder (then the loop device can be deleted)
175 // ScopedDirRemover (then the mount point can be deleted)
176 std::unique_ptr<ScopedDirRemover> dir_remover_;
177 std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
178 std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
179};
180
181// Deletes a directory and all its contents synchronously. Returns true
182// on success. This may be called with a regular file--it will just unlink it.
183// This WILL cross filesystem boundaries.
184bool RecursiveUnlinkDir(const std::string& path);
185
Alex Deymo10875d92014-11-10 21:52:57 -0800186// Allocates, initializes and returns a string GValue object.
187GValue* GValueNewString(const char* str);
188
189// Frees a GValue object and its allocated resources.
190void GValueFree(gpointer arg);
191
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700192// Returns the path where the build artifacts are stored. This is the directory
193// where the unittest executable is being run from.
194base::FilePath GetBuildArtifactsPath();
Alex Deymo10875d92014-11-10 21:52:57 -0800195
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700196} // namespace test_utils
Alex Deymo10875d92014-11-10 21:52:57 -0800197
198// Useful actions for test. These need to be defined in the
199// chromeos_update_engine namespace.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000200
201class NoneType;
202
203template<typename T>
204class ObjectFeederAction;
205
206template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700207class ActionTraits<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000208 public:
209 typedef T OutputObjectType;
210 typedef NoneType InputObjectType;
211};
212
213// This is a simple Action class for testing. It feeds an object into
214// another action.
215template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700216class ObjectFeederAction : public Action<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000217 public:
218 typedef NoneType InputObjectType;
219 typedef T OutputObjectType;
220 void PerformAction() {
221 LOG(INFO) << "feeder running!";
222 CHECK(this->processor_);
223 if (this->HasOutputPipe()) {
224 this->SetOutputObject(out_obj_);
225 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700226 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000227 }
228 static std::string StaticType() { return "ObjectFeederAction"; }
229 std::string Type() const { return StaticType(); }
230 void set_obj(const T& out_obj) {
231 out_obj_ = out_obj;
232 }
233 private:
234 T out_obj_;
235};
236
237template<typename T>
238class ObjectCollectorAction;
239
240template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700241class ActionTraits<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000242 public:
243 typedef NoneType OutputObjectType;
244 typedef T InputObjectType;
245};
246
247// This is a simple Action class for testing. It receives an object from
248// another action.
249template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700250class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000251 public:
252 typedef T InputObjectType;
253 typedef NoneType OutputObjectType;
254 void PerformAction() {
255 LOG(INFO) << "collector running!";
256 ASSERT_TRUE(this->processor_);
257 if (this->HasInputObject()) {
258 object_ = this->GetInputObject();
259 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700260 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000261 }
262 static std::string StaticType() { return "ObjectCollectorAction"; }
263 std::string Type() const { return StaticType(); }
264 const T& object() const { return object_; }
265 private:
266 T object_;
267};
268
rspangler@google.com49fdf182009-10-10 00:57:34 +0000269} // namespace chromeos_update_engine
270
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700271#endif // UPDATE_ENGINE_TEST_UTILS_H_