| 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 |  | 
| Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 12 | // Streams used for gtest's PrintTo() functions. | 
|  | 13 | #include <iostream>  // NOLINT(readability/streams) | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 14 | #include <memory> | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 15 | #include <set> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | #include <string> | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 17 | #include <vector> | 
| Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 18 |  | 
| Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 19 | #include <base/callback.h> | 
| Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 20 | #include <base/files/file_path.h> | 
| Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 21 | #include <glib-object.h> | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 22 | #include <gtest/gtest.h> | 
| Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 23 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 24 | #include "update_engine/action.h" | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 25 | #include "update_engine/subprocess.h" | 
| Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 26 | #include "update_engine/update_metadata.pb.h" | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 27 | #include "update_engine/utils.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | // These are some handy functions for unittests. | 
|  | 30 |  | 
|  | 31 | namespace chromeos_update_engine { | 
| Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 32 |  | 
|  | 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. | 
|  | 35 | void PrintTo(const Extent& extent, ::std::ostream* os); | 
|  | 36 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 37 | namespace test_utils { | 
|  | 38 |  | 
|  | 39 | // 300 byte pseudo-random string. Not null terminated. | 
|  | 40 | // This does not gzip compress well. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 41 | extern const uint8_t kRandomString[300]; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 42 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 43 | // Writes the data passed to path. The file at path will be overwritten if it | 
|  | 44 | // exists. Returns true on success, false otherwise. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 45 | bool WriteFileVector(const std::string& path, const chromeos::Blob& data); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 46 | bool WriteFileString(const std::string& path, const std::string& data); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 |  | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 48 | bool BindToUnusedLoopDevice(const std::string &filename, | 
|  | 49 | std::string* lo_dev_name_ptr); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 50 |  | 
|  | 51 | // Returns true iff a == b | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 52 | bool ExpectVectorsEq(const chromeos::Blob& a, const chromeos::Blob& b); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 53 |  | 
|  | 54 | inline int System(const std::string& cmd) { | 
|  | 55 | return system(cmd.c_str()); | 
|  | 56 | } | 
|  | 57 |  | 
| Gilad Arnold | 2c2b884 | 2013-07-16 06:44:37 -0700 | [diff] [blame] | 58 | inline int Symlink(const std::string& oldpath, const std::string& newpath) { | 
|  | 59 | return symlink(oldpath.c_str(), newpath.c_str()); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | inline int Chmod(const std::string& path, mode_t mode) { | 
|  | 63 | return chmod(path.c_str(), mode); | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | inline int Mkdir(const std::string& path, mode_t mode) { | 
|  | 67 | return mkdir(path.c_str(), mode); | 
|  | 68 | } | 
|  | 69 |  | 
| Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 70 | inline int Chdir(const std::string& path) { | 
|  | 71 | return chdir(path.c_str()); | 
|  | 72 | } | 
|  | 73 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 74 | // Checks if xattr is supported in the directory specified by | 
|  | 75 | // |dir_path| which must be writable. Returns true if the feature is | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 76 | // supported, false if not or if an error occurred. | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 77 | bool IsXAttrSupported(const base::FilePath& dir_path); | 
|  | 78 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 79 | void FillWithData(chromeos::Blob* buffer); | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 80 |  | 
| Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 81 | // Creates an empty ext image. | 
|  | 82 | void CreateEmptyExtImageAtPath(const std::string& path, | 
|  | 83 | size_t size, | 
|  | 84 | int block_size); | 
|  | 85 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 86 | // Creates an ext image with some files in it. The paths creates are | 
|  | 87 | // returned in out_paths. | 
|  | 88 | void CreateExtImageAtPath(const std::string& path, | 
|  | 89 | std::vector<std::string>* out_paths); | 
|  | 90 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 91 | // Class to unmount FS when object goes out of scope | 
|  | 92 | class 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 Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 109 | class ScopedLoopbackDeviceBinder { | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 110 | public: | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 111 | ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) { | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 112 | is_bound_ = BindToUnusedLoopDevice(file, &dev_); | 
|  | 113 | EXPECT_TRUE(is_bound_); | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 114 |  | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 115 | if (is_bound_ && dev) | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 116 | *dev = dev_; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | ~ScopedLoopbackDeviceBinder() { | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 120 | if (!is_bound_) | 
|  | 121 | return; | 
|  | 122 |  | 
| Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 123 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 129 | EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, nullptr)); | 
| Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 130 | if (return_code == 0) { | 
|  | 131 | return; | 
|  | 132 | } | 
|  | 133 | sleep(1); | 
|  | 134 | } | 
|  | 135 | ADD_FAILURE(); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 136 | } | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 137 |  | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 138 | const std::string &dev() { | 
|  | 139 | EXPECT_TRUE(is_bound_); | 
|  | 140 | return dev_; | 
|  | 141 | } | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 142 |  | 
| Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 143 | bool is_bound() const { return is_bound_; } | 
|  | 144 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 145 | private: | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 146 | std::string dev_; | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 147 | bool is_bound_; | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 149 | }; | 
|  | 150 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 151 | class ScopedTempFile { | 
|  | 152 | public: | 
|  | 153 | ScopedTempFile() { | 
|  | 154 | EXPECT_TRUE(utils::MakeTempFile("/tmp/update_engine_test_temp_file.XXXXXX", | 
|  | 155 | &path_, | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 156 | nullptr)); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 157 | unlinker_.reset(new ScopedPathUnlinker(path_)); | 
|  | 158 | } | 
|  | 159 | const std::string& GetPath() { return path_; } | 
|  | 160 | private: | 
|  | 161 | std::string path_; | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 162 | std::unique_ptr<ScopedPathUnlinker> unlinker_; | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 163 | }; | 
|  | 164 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 165 | class 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. | 
|  | 184 | bool RecursiveUnlinkDir(const std::string& path); | 
|  | 185 |  | 
|  | 186 | // Runs the default GLib main loop for at most |timeout_msec| or until the | 
|  | 187 | // function |terminate| returns true, whichever happens first. The function | 
|  | 188 | // |terminate| is called before every GLib main loop iteration and its value is | 
|  | 189 | // checked. | 
|  | 190 | void RunGMainLoopUntil(int timeout_msec, base::Callback<bool()> terminate); | 
|  | 191 |  | 
|  | 192 | // Runs the default GLib main loop at most |iterations| times. This | 
|  | 193 | // dispatches all the events that are already waiting in the main loop and | 
|  | 194 | // those that get scheduled as a result of these events being attended. | 
|  | 195 | // Returns the number of iterations the main loop was ran. If there are more | 
|  | 196 | // than |iterations| events to attend, then this function returns |iterations| | 
|  | 197 | // and the remaining events are not dispatched. | 
|  | 198 | int RunGMainLoopMaxIterations(int iterations); | 
|  | 199 |  | 
|  | 200 | // Allocates, initializes and returns a string GValue object. | 
|  | 201 | GValue* GValueNewString(const char* str); | 
|  | 202 |  | 
|  | 203 | // Frees a GValue object and its allocated resources. | 
|  | 204 | void GValueFree(gpointer arg); | 
|  | 205 |  | 
| Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 206 | // Returns the path where the build artifacts are stored. This is the directory | 
|  | 207 | // where the unittest executable is being run from. | 
|  | 208 | base::FilePath GetBuildArtifactsPath(); | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 209 |  | 
| Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 210 | }  // namespace test_utils | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 211 |  | 
|  | 212 | // Useful actions for test. These need to be defined in the | 
|  | 213 | // chromeos_update_engine namespace. | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 214 |  | 
|  | 215 | class NoneType; | 
|  | 216 |  | 
|  | 217 | template<typename T> | 
|  | 218 | class ObjectFeederAction; | 
|  | 219 |  | 
|  | 220 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 221 | class ActionTraits<ObjectFeederAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 222 | public: | 
|  | 223 | typedef T OutputObjectType; | 
|  | 224 | typedef NoneType InputObjectType; | 
|  | 225 | }; | 
|  | 226 |  | 
|  | 227 | // This is a simple Action class for testing. It feeds an object into | 
|  | 228 | // another action. | 
|  | 229 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 230 | class ObjectFeederAction : public Action<ObjectFeederAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 231 | public: | 
|  | 232 | typedef NoneType InputObjectType; | 
|  | 233 | typedef T OutputObjectType; | 
|  | 234 | void PerformAction() { | 
|  | 235 | LOG(INFO) << "feeder running!"; | 
|  | 236 | CHECK(this->processor_); | 
|  | 237 | if (this->HasOutputPipe()) { | 
|  | 238 | this->SetOutputObject(out_obj_); | 
|  | 239 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 240 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 241 | } | 
|  | 242 | static std::string StaticType() { return "ObjectFeederAction"; } | 
|  | 243 | std::string Type() const { return StaticType(); } | 
|  | 244 | void set_obj(const T& out_obj) { | 
|  | 245 | out_obj_ = out_obj; | 
|  | 246 | } | 
|  | 247 | private: | 
|  | 248 | T out_obj_; | 
|  | 249 | }; | 
|  | 250 |  | 
|  | 251 | template<typename T> | 
|  | 252 | class ObjectCollectorAction; | 
|  | 253 |  | 
|  | 254 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 255 | class ActionTraits<ObjectCollectorAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 256 | public: | 
|  | 257 | typedef NoneType OutputObjectType; | 
|  | 258 | typedef T InputObjectType; | 
|  | 259 | }; | 
|  | 260 |  | 
|  | 261 | // This is a simple Action class for testing. It receives an object from | 
|  | 262 | // another action. | 
|  | 263 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 264 | class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 265 | public: | 
|  | 266 | typedef T InputObjectType; | 
|  | 267 | typedef NoneType OutputObjectType; | 
|  | 268 | void PerformAction() { | 
|  | 269 | LOG(INFO) << "collector running!"; | 
|  | 270 | ASSERT_TRUE(this->processor_); | 
|  | 271 | if (this->HasInputObject()) { | 
|  | 272 | object_ = this->GetInputObject(); | 
|  | 273 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 274 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 275 | } | 
|  | 276 | static std::string StaticType() { return "ObjectCollectorAction"; } | 
|  | 277 | std::string Type() const { return StaticType(); } | 
|  | 278 | const T& object() const { return object_; } | 
|  | 279 | private: | 
|  | 280 | T object_; | 
|  | 281 | }; | 
|  | 282 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 283 | }  // namespace chromeos_update_engine | 
|  | 284 |  | 
| Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 285 | #endif  // UPDATE_ENGINE_TEST_UTILS_H_ |