blob: a65b04216fe4092f5f61b76cfd7bf572bf4dcc57 [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
Ben Chan02f7c1d2014-10-18 15:18:02 -070012#include <memory>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000013#include <set>
rspangler@google.com49fdf182009-10-10 00:57:34 +000014#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000015#include <vector>
Thieu Le5c7d9752010-12-15 16:09:28 -080016
Alex Deymo53556ec2014-03-17 10:05:57 -070017#include <base/callback.h>
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070018#include <glib-object.h>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000019#include <gtest/gtest.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080020
adlr@google.comc98a7ed2009-12-04 18:54:03 +000021#include "update_engine/action.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include "update_engine/subprocess.h"
Andrew de los Reyesf9185172010-05-03 11:07:05 -070023#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000024
25// These are some handy functions for unittests.
26
27namespace chromeos_update_engine {
Alex Deymo10875d92014-11-10 21:52:57 -080028namespace test_utils {
29
30// 300 byte pseudo-random string. Not null terminated.
31// This does not gzip compress well.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080032extern const uint8_t kRandomString[300];
rspangler@google.com49fdf182009-10-10 00:57:34 +000033
rspangler@google.com49fdf182009-10-10 00:57:34 +000034// Writes the data passed to path. The file at path will be overwritten if it
35// exists. Returns true on success, false otherwise.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080036bool WriteFileVector(const std::string& path, const chromeos::Blob& data);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037bool WriteFileString(const std::string& path, const std::string& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
Gilad Arnold19a45f02012-07-19 12:36:10 -070039bool BindToUnusedLoopDevice(const std::string &filename,
40 std::string* lo_dev_name_ptr);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000041
42// Returns true iff a == b
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080043bool ExpectVectorsEq(const chromeos::Blob& a, const chromeos::Blob& b);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000044
45inline int System(const std::string& cmd) {
46 return system(cmd.c_str());
47}
48
Gilad Arnold2c2b8842013-07-16 06:44:37 -070049inline int Symlink(const std::string& oldpath, const std::string& newpath) {
50 return symlink(oldpath.c_str(), newpath.c_str());
51}
52
53inline int Chmod(const std::string& path, mode_t mode) {
54 return chmod(path.c_str(), mode);
55}
56
57inline int Mkdir(const std::string& path, mode_t mode) {
58 return mkdir(path.c_str(), mode);
59}
60
Gilad Arnold30dedd82013-07-03 06:19:09 -070061inline int Chdir(const std::string& path) {
62 return chdir(path.c_str());
63}
64
Alex Deymo10875d92014-11-10 21:52:57 -080065// Checks if xattr is supported in the directory specified by
66// |dir_path| which must be writable. Returns true if the feature is
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080067// supported, false if not or if an error occurred.
Alex Deymo10875d92014-11-10 21:52:57 -080068bool IsXAttrSupported(const base::FilePath& dir_path);
69
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080070void FillWithData(chromeos::Blob* buffer);
Andrew de los Reyes80061062010-02-04 14:25:00 -080071
Thieu Le5c7d9752010-12-15 16:09:28 -080072// Creates an empty ext image.
73void CreateEmptyExtImageAtPath(const std::string& path,
74 size_t size,
75 int block_size);
76
adlr@google.comc98a7ed2009-12-04 18:54:03 +000077// Creates an ext image with some files in it. The paths creates are
78// returned in out_paths.
79void CreateExtImageAtPath(const std::string& path,
80 std::vector<std::string>* out_paths);
81
82// Verifies that for each path in paths, it exists in the filesystem under
83// parent. Also, verifies that no additional paths are present under parent.
84// Also tests properties of various files created by CreateExtImageAtPath().
85// Intentionally copies expected_paths.
86void VerifyAllPaths(const std::string& parent,
87 std::set<std::string> expected_paths);
88
Alex Deymo10875d92014-11-10 21:52:57 -080089// Class to unmount FS when object goes out of scope
90class ScopedFilesystemUnmounter {
91 public:
92 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
93 : mountpoint_(mountpoint),
94 should_unmount_(true) {}
95 ~ScopedFilesystemUnmounter() {
96 if (should_unmount_) {
97 utils::UnmountFilesystem(mountpoint_);
98 }
99 }
100 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
101 private:
102 const std::string mountpoint_;
103 bool should_unmount_;
104 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
105};
106
Don Garrett58e8b1f2012-01-31 16:38:16 -0800107class ScopedLoopbackDeviceBinder {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700108 public:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800109 ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700110 is_bound_ = BindToUnusedLoopDevice(file, &dev_);
111 EXPECT_TRUE(is_bound_);
Don Garrett58e8b1f2012-01-31 16:38:16 -0800112
Gilad Arnold19a45f02012-07-19 12:36:10 -0700113 if (is_bound_ && dev)
Don Garrett58e8b1f2012-01-31 16:38:16 -0800114 *dev = dev_;
115 }
116
117 ~ScopedLoopbackDeviceBinder() {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700118 if (!is_bound_)
119 return;
120
Darin Petkovcf562482010-12-03 10:31:00 -0800121 for (int retry = 0; retry < 5; retry++) {
122 std::vector<std::string> args;
123 args.push_back("/sbin/losetup");
124 args.push_back("-d");
125 args.push_back(dev_);
126 int return_code = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700127 EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, nullptr));
Darin Petkovcf562482010-12-03 10:31:00 -0800128 if (return_code == 0) {
129 return;
130 }
131 sleep(1);
132 }
133 ADD_FAILURE();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700134 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800135
Gilad Arnold19a45f02012-07-19 12:36:10 -0700136 const std::string &dev() {
137 EXPECT_TRUE(is_bound_);
138 return dev_;
139 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800140
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700141 bool is_bound() const { return is_bound_; }
142
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700143 private:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800144 std::string dev_;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700145 bool is_bound_;
Don Garrett58e8b1f2012-01-31 16:38:16 -0800146 DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700147};
148
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700149class ScopedTempFile {
150 public:
151 ScopedTempFile() {
152 EXPECT_TRUE(utils::MakeTempFile("/tmp/update_engine_test_temp_file.XXXXXX",
153 &path_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700154 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700155 unlinker_.reset(new ScopedPathUnlinker(path_));
156 }
157 const std::string& GetPath() { return path_; }
158 private:
159 std::string path_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700160 std::unique_ptr<ScopedPathUnlinker> unlinker_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700161};
162
Alex Deymo10875d92014-11-10 21:52:57 -0800163class ScopedLoopMounter {
164 public:
165 explicit ScopedLoopMounter(const std::string& file_path,
166 std::string* mnt_path,
167 unsigned long flags); // NOLINT(runtime/int)
168
169 private:
170 // These objects must be destructed in the following order:
171 // ScopedFilesystemUnmounter (the file system must be unmounted first)
172 // ScopedLoopbackDeviceBinder (then the loop device can be deleted)
173 // ScopedDirRemover (then the mount point can be deleted)
174 std::unique_ptr<ScopedDirRemover> dir_remover_;
175 std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
176 std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
177};
178
179// Deletes a directory and all its contents synchronously. Returns true
180// on success. This may be called with a regular file--it will just unlink it.
181// This WILL cross filesystem boundaries.
182bool RecursiveUnlinkDir(const std::string& path);
183
184// Runs the default GLib main loop for at most |timeout_msec| or until the
185// function |terminate| returns true, whichever happens first. The function
186// |terminate| is called before every GLib main loop iteration and its value is
187// checked.
188void RunGMainLoopUntil(int timeout_msec, base::Callback<bool()> terminate);
189
190// Runs the default GLib main loop at most |iterations| times. This
191// dispatches all the events that are already waiting in the main loop and
192// those that get scheduled as a result of these events being attended.
193// Returns the number of iterations the main loop was ran. If there are more
194// than |iterations| events to attend, then this function returns |iterations|
195// and the remaining events are not dispatched.
196int RunGMainLoopMaxIterations(int iterations);
197
198// Allocates, initializes and returns a string GValue object.
199GValue* GValueNewString(const char* str);
200
201// Frees a GValue object and its allocated resources.
202void GValueFree(gpointer arg);
203
204} // namespace test_utils
205
206
207// Useful actions for test. These need to be defined in the
208// chromeos_update_engine namespace.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000209
210class NoneType;
211
212template<typename T>
213class ObjectFeederAction;
214
215template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700216class ActionTraits<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000217 public:
218 typedef T OutputObjectType;
219 typedef NoneType InputObjectType;
220};
221
222// This is a simple Action class for testing. It feeds an object into
223// another action.
224template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700225class ObjectFeederAction : public Action<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000226 public:
227 typedef NoneType InputObjectType;
228 typedef T OutputObjectType;
229 void PerformAction() {
230 LOG(INFO) << "feeder running!";
231 CHECK(this->processor_);
232 if (this->HasOutputPipe()) {
233 this->SetOutputObject(out_obj_);
234 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700235 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000236 }
237 static std::string StaticType() { return "ObjectFeederAction"; }
238 std::string Type() const { return StaticType(); }
239 void set_obj(const T& out_obj) {
240 out_obj_ = out_obj;
241 }
242 private:
243 T out_obj_;
244};
245
246template<typename T>
247class ObjectCollectorAction;
248
249template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700250class ActionTraits<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000251 public:
252 typedef NoneType OutputObjectType;
253 typedef T InputObjectType;
254};
255
256// This is a simple Action class for testing. It receives an object from
257// another action.
258template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700259class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000260 public:
261 typedef T InputObjectType;
262 typedef NoneType OutputObjectType;
263 void PerformAction() {
264 LOG(INFO) << "collector running!";
265 ASSERT_TRUE(this->processor_);
266 if (this->HasInputObject()) {
267 object_ = this->GetInputObject();
268 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700269 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000270 }
271 static std::string StaticType() { return "ObjectCollectorAction"; }
272 std::string Type() const { return StaticType(); }
273 const T& object() const { return object_; }
274 private:
275 T object_;
276};
277
rspangler@google.com49fdf182009-10-10 00:57:34 +0000278} // namespace chromeos_update_engine
279
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700280#endif // UPDATE_ENGINE_TEST_UTILS_H_