Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_TEST_UTILS_H_ |
| 6 | #define UPDATE_ENGINE_TEST_UTILS_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
Gilad Arnold | 2c2b884 | 2013-07-16 06:44:37 -0700 | [diff] [blame] | 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 12 | #include <memory> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 13 | #include <set> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 14 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 15 | #include <vector> |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 17 | #include <base/callback.h> |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 18 | #include <glib-object.h> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 20 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 21 | #include "update_engine/action.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 22 | #include "update_engine/subprocess.h" |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 23 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | |
| 25 | // These are some handy functions for unittests. |
| 26 | |
| 27 | namespace chromeos_update_engine { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 28 | namespace test_utils { |
| 29 | |
| 30 | // 300 byte pseudo-random string. Not null terminated. |
| 31 | // This does not gzip compress well. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 32 | extern const uint8_t kRandomString[300]; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | // Writes the data passed to path. The file at path will be overwritten if it |
| 35 | // exists. Returns true on success, false otherwise. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 36 | bool WriteFileVector(const std::string& path, const chromeos::Blob& data); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 37 | bool WriteFileString(const std::string& path, const std::string& data); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 39 | bool BindToUnusedLoopDevice(const std::string &filename, |
| 40 | std::string* lo_dev_name_ptr); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 41 | |
| 42 | // Returns true iff a == b |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 43 | bool ExpectVectorsEq(const chromeos::Blob& a, const chromeos::Blob& b); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 44 | |
| 45 | inline int System(const std::string& cmd) { |
| 46 | return system(cmd.c_str()); |
| 47 | } |
| 48 | |
Gilad Arnold | 2c2b884 | 2013-07-16 06:44:37 -0700 | [diff] [blame] | 49 | inline int Symlink(const std::string& oldpath, const std::string& newpath) { |
| 50 | return symlink(oldpath.c_str(), newpath.c_str()); |
| 51 | } |
| 52 | |
| 53 | inline int Chmod(const std::string& path, mode_t mode) { |
| 54 | return chmod(path.c_str(), mode); |
| 55 | } |
| 56 | |
| 57 | inline int Mkdir(const std::string& path, mode_t mode) { |
| 58 | return mkdir(path.c_str(), mode); |
| 59 | } |
| 60 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 61 | inline int Chdir(const std::string& path) { |
| 62 | return chdir(path.c_str()); |
| 63 | } |
| 64 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 65 | // Checks if xattr is supported in the directory specified by |
| 66 | // |dir_path| which must be writable. Returns true if the feature is |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 67 | // supported, false if not or if an error occurred. |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 68 | bool IsXAttrSupported(const base::FilePath& dir_path); |
| 69 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 70 | void FillWithData(chromeos::Blob* buffer); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 71 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 72 | // Creates an empty ext image. |
| 73 | void CreateEmptyExtImageAtPath(const std::string& path, |
| 74 | size_t size, |
| 75 | int block_size); |
| 76 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 77 | // Creates an ext image with some files in it. The paths creates are |
| 78 | // returned in out_paths. |
| 79 | void 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. |
| 86 | void VerifyAllPaths(const std::string& parent, |
| 87 | std::set<std::string> expected_paths); |
| 88 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 89 | // Class to unmount FS when object goes out of scope |
| 90 | class 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 Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 107 | class ScopedLoopbackDeviceBinder { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 108 | public: |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 109 | ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 110 | is_bound_ = BindToUnusedLoopDevice(file, &dev_); |
| 111 | EXPECT_TRUE(is_bound_); |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 112 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 113 | if (is_bound_ && dev) |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 114 | *dev = dev_; |
| 115 | } |
| 116 | |
| 117 | ~ScopedLoopbackDeviceBinder() { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 118 | if (!is_bound_) |
| 119 | return; |
| 120 | |
Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 121 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 127 | EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, nullptr)); |
Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 128 | if (return_code == 0) { |
| 129 | return; |
| 130 | } |
| 131 | sleep(1); |
| 132 | } |
| 133 | ADD_FAILURE(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 134 | } |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 135 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 136 | const std::string &dev() { |
| 137 | EXPECT_TRUE(is_bound_); |
| 138 | return dev_; |
| 139 | } |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 140 | |
Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 141 | bool is_bound() const { return is_bound_; } |
| 142 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 143 | private: |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 144 | std::string dev_; |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 145 | bool is_bound_; |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 146 | DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 149 | class ScopedTempFile { |
| 150 | public: |
| 151 | ScopedTempFile() { |
| 152 | EXPECT_TRUE(utils::MakeTempFile("/tmp/update_engine_test_temp_file.XXXXXX", |
| 153 | &path_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 154 | nullptr)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 155 | unlinker_.reset(new ScopedPathUnlinker(path_)); |
| 156 | } |
| 157 | const std::string& GetPath() { return path_; } |
| 158 | private: |
| 159 | std::string path_; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 160 | std::unique_ptr<ScopedPathUnlinker> unlinker_; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 163 | class 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. |
| 182 | bool 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. |
| 188 | void 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. |
| 196 | int RunGMainLoopMaxIterations(int iterations); |
| 197 | |
| 198 | // Allocates, initializes and returns a string GValue object. |
| 199 | GValue* GValueNewString(const char* str); |
| 200 | |
| 201 | // Frees a GValue object and its allocated resources. |
| 202 | void 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 209 | |
| 210 | class NoneType; |
| 211 | |
| 212 | template<typename T> |
| 213 | class ObjectFeederAction; |
| 214 | |
| 215 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 216 | class ActionTraits<ObjectFeederAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 217 | 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. |
| 224 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 225 | class ObjectFeederAction : public Action<ObjectFeederAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 226 | 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 Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 235 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 236 | } |
| 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 | |
| 246 | template<typename T> |
| 247 | class ObjectCollectorAction; |
| 248 | |
| 249 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 250 | class ActionTraits<ObjectCollectorAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 251 | 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. |
| 258 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 259 | class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 260 | 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 Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 269 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 270 | } |
| 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 278 | } // namespace chromeos_update_engine |
| 279 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 280 | #endif // UPDATE_ENGINE_TEST_UTILS_H_ |