Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_TEST_UTILS_H_ |
| 18 | #define UPDATE_ENGINE_TEST_UTILS_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
Gilad Arnold | 2c2b884 | 2013-07-16 06:44:37 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 24 | // Streams used for gtest's PrintTo() functions. |
| 25 | #include <iostream> // NOLINT(readability/streams) |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 26 | #include <memory> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 27 | #include <set> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 29 | #include <vector> |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 30 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 31 | #include <base/callback.h> |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 32 | #include <base/files/file_path.h> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 33 | #include <gtest/gtest.h> |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 34 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 35 | #include "update_engine/action.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 36 | #include "update_engine/subprocess.h" |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 37 | #include "update_engine/update_metadata.pb.h" |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 38 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | |
| 40 | // These are some handy functions for unittests. |
| 41 | |
| 42 | namespace chromeos_update_engine { |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 43 | |
| 44 | // PrintTo() functions are used by gtest to log these objects. These PrintTo() |
| 45 | // functions must be defined in the same namespace as the first argument. |
| 46 | void PrintTo(const Extent& extent, ::std::ostream* os); |
| 47 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 48 | namespace test_utils { |
| 49 | |
| 50 | // 300 byte pseudo-random string. Not null terminated. |
| 51 | // This does not gzip compress well. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 52 | extern const uint8_t kRandomString[300]; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | // Writes the data passed to path. The file at path will be overwritten if it |
| 55 | // exists. Returns true on success, false otherwise. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 56 | bool WriteFileVector(const std::string& path, const chromeos::Blob& data); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 57 | bool WriteFileString(const std::string& path, const std::string& data); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 59 | bool BindToUnusedLoopDevice(const std::string &filename, |
| 60 | std::string* lo_dev_name_ptr); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 61 | |
| 62 | // Returns true iff a == b |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 63 | bool ExpectVectorsEq(const chromeos::Blob& a, const chromeos::Blob& b); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 64 | |
| 65 | inline int System(const std::string& cmd) { |
| 66 | return system(cmd.c_str()); |
| 67 | } |
| 68 | |
Gilad Arnold | 2c2b884 | 2013-07-16 06:44:37 -0700 | [diff] [blame] | 69 | inline int Symlink(const std::string& oldpath, const std::string& newpath) { |
| 70 | return symlink(oldpath.c_str(), newpath.c_str()); |
| 71 | } |
| 72 | |
| 73 | inline int Chmod(const std::string& path, mode_t mode) { |
| 74 | return chmod(path.c_str(), mode); |
| 75 | } |
| 76 | |
| 77 | inline int Mkdir(const std::string& path, mode_t mode) { |
| 78 | return mkdir(path.c_str(), mode); |
| 79 | } |
| 80 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 81 | inline int Chdir(const std::string& path) { |
| 82 | return chdir(path.c_str()); |
| 83 | } |
| 84 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 85 | // Checks if xattr is supported in the directory specified by |
| 86 | // |dir_path| which must be writable. Returns true if the feature is |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 87 | // supported, false if not or if an error occurred. |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 88 | bool IsXAttrSupported(const base::FilePath& dir_path); |
| 89 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 90 | void FillWithData(chromeos::Blob* buffer); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 91 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 92 | // Creates an empty ext image. |
| 93 | void CreateEmptyExtImageAtPath(const std::string& path, |
| 94 | size_t size, |
| 95 | int block_size); |
| 96 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 97 | // Creates an ext image with some files in it. The paths creates are |
| 98 | // returned in out_paths. |
| 99 | void CreateExtImageAtPath(const std::string& path, |
| 100 | std::vector<std::string>* out_paths); |
| 101 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 102 | // Class to unmount FS when object goes out of scope |
| 103 | class ScopedFilesystemUnmounter { |
| 104 | public: |
| 105 | explicit ScopedFilesystemUnmounter(const std::string& mountpoint) |
| 106 | : mountpoint_(mountpoint), |
| 107 | should_unmount_(true) {} |
| 108 | ~ScopedFilesystemUnmounter() { |
| 109 | if (should_unmount_) { |
| 110 | utils::UnmountFilesystem(mountpoint_); |
| 111 | } |
| 112 | } |
| 113 | void set_should_unmount(bool unmount) { should_unmount_ = unmount; } |
| 114 | private: |
| 115 | const std::string mountpoint_; |
| 116 | bool should_unmount_; |
| 117 | DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); |
| 118 | }; |
| 119 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 120 | class ScopedLoopbackDeviceBinder { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 121 | public: |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 122 | ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 123 | is_bound_ = BindToUnusedLoopDevice(file, &dev_); |
| 124 | EXPECT_TRUE(is_bound_); |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 125 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 126 | if (is_bound_ && dev) |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 127 | *dev = dev_; |
| 128 | } |
| 129 | |
| 130 | ~ScopedLoopbackDeviceBinder() { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 131 | if (!is_bound_) |
| 132 | return; |
| 133 | |
Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 134 | for (int retry = 0; retry < 5; retry++) { |
| 135 | std::vector<std::string> args; |
| 136 | args.push_back("/sbin/losetup"); |
| 137 | args.push_back("-d"); |
| 138 | args.push_back(dev_); |
| 139 | int return_code = 0; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 140 | EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, nullptr)); |
Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 141 | if (return_code == 0) { |
| 142 | return; |
| 143 | } |
| 144 | sleep(1); |
| 145 | } |
| 146 | ADD_FAILURE(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 147 | } |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 148 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 149 | const std::string &dev() { |
| 150 | EXPECT_TRUE(is_bound_); |
| 151 | return dev_; |
| 152 | } |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 153 | |
Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 154 | bool is_bound() const { return is_bound_; } |
| 155 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 156 | private: |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 157 | std::string dev_; |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 158 | bool is_bound_; |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 159 | DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 162 | class ScopedTempFile { |
| 163 | public: |
| 164 | ScopedTempFile() { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 165 | EXPECT_TRUE(utils::MakeTempFile("update_engine_test_temp_file.XXXXXX", |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 166 | &path_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 167 | nullptr)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 168 | unlinker_.reset(new ScopedPathUnlinker(path_)); |
| 169 | } |
| 170 | const std::string& GetPath() { return path_; } |
| 171 | private: |
| 172 | std::string path_; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 173 | std::unique_ptr<ScopedPathUnlinker> unlinker_; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 176 | class ScopedLoopMounter { |
| 177 | public: |
| 178 | explicit ScopedLoopMounter(const std::string& file_path, |
| 179 | std::string* mnt_path, |
| 180 | unsigned long flags); // NOLINT(runtime/int) |
| 181 | |
| 182 | private: |
| 183 | // These objects must be destructed in the following order: |
| 184 | // ScopedFilesystemUnmounter (the file system must be unmounted first) |
| 185 | // ScopedLoopbackDeviceBinder (then the loop device can be deleted) |
| 186 | // ScopedDirRemover (then the mount point can be deleted) |
| 187 | std::unique_ptr<ScopedDirRemover> dir_remover_; |
| 188 | std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_; |
| 189 | std::unique_ptr<ScopedFilesystemUnmounter> unmounter_; |
| 190 | }; |
| 191 | |
| 192 | // Deletes a directory and all its contents synchronously. Returns true |
| 193 | // on success. This may be called with a regular file--it will just unlink it. |
| 194 | // This WILL cross filesystem boundaries. |
| 195 | bool RecursiveUnlinkDir(const std::string& path); |
| 196 | |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 197 | // Returns the path where the build artifacts are stored. This is the directory |
| 198 | // where the unittest executable is being run from. |
| 199 | base::FilePath GetBuildArtifactsPath(); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 200 | |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 201 | } // namespace test_utils |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 202 | |
| 203 | // Useful actions for test. These need to be defined in the |
| 204 | // chromeos_update_engine namespace. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 205 | |
| 206 | class NoneType; |
| 207 | |
| 208 | template<typename T> |
| 209 | class ObjectFeederAction; |
| 210 | |
| 211 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 212 | class ActionTraits<ObjectFeederAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 213 | public: |
| 214 | typedef T OutputObjectType; |
| 215 | typedef NoneType InputObjectType; |
| 216 | }; |
| 217 | |
| 218 | // This is a simple Action class for testing. It feeds an object into |
| 219 | // another action. |
| 220 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 221 | class ObjectFeederAction : public Action<ObjectFeederAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 222 | public: |
| 223 | typedef NoneType InputObjectType; |
| 224 | typedef T OutputObjectType; |
| 225 | void PerformAction() { |
| 226 | LOG(INFO) << "feeder running!"; |
| 227 | CHECK(this->processor_); |
| 228 | if (this->HasOutputPipe()) { |
| 229 | this->SetOutputObject(out_obj_); |
| 230 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 231 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 232 | } |
| 233 | static std::string StaticType() { return "ObjectFeederAction"; } |
| 234 | std::string Type() const { return StaticType(); } |
| 235 | void set_obj(const T& out_obj) { |
| 236 | out_obj_ = out_obj; |
| 237 | } |
| 238 | private: |
| 239 | T out_obj_; |
| 240 | }; |
| 241 | |
| 242 | template<typename T> |
| 243 | class ObjectCollectorAction; |
| 244 | |
| 245 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 246 | class ActionTraits<ObjectCollectorAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 247 | public: |
| 248 | typedef NoneType OutputObjectType; |
| 249 | typedef T InputObjectType; |
| 250 | }; |
| 251 | |
| 252 | // This is a simple Action class for testing. It receives an object from |
| 253 | // another action. |
| 254 | template<typename T> |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 255 | class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 256 | public: |
| 257 | typedef T InputObjectType; |
| 258 | typedef NoneType OutputObjectType; |
| 259 | void PerformAction() { |
| 260 | LOG(INFO) << "collector running!"; |
| 261 | ASSERT_TRUE(this->processor_); |
| 262 | if (this->HasInputObject()) { |
| 263 | object_ = this->GetInputObject(); |
| 264 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 265 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 266 | } |
| 267 | static std::string StaticType() { return "ObjectCollectorAction"; } |
| 268 | std::string Type() const { return StaticType(); } |
| 269 | const T& object() const { return object_; } |
| 270 | private: |
| 271 | T object_; |
| 272 | }; |
| 273 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 274 | } // namespace chromeos_update_engine |
| 275 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 276 | #endif // UPDATE_ENGINE_TEST_UTILS_H_ |