| 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 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_TEST_UTILS_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_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> | 
| Sen Jiang | 371615b | 2016-04-13 15:54:29 -0700 | [diff] [blame] | 33 | #include <base/files/scoped_temp_dir.h> | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 34 | #include <gmock/gmock.h> | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 35 | #include <gtest/gtest.h> | 
| Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 36 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 37 | #include "update_engine/common/action.h" | 
|  | 38 | #include "update_engine/common/subprocess.h" | 
|  | 39 | #include "update_engine/common/utils.h" | 
| Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 40 | #include "update_engine/update_metadata.pb.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 41 |  | 
|  | 42 | // These are some handy functions for unittests. | 
|  | 43 |  | 
|  | 44 | namespace chromeos_update_engine { | 
| Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 45 |  | 
|  | 46 | // PrintTo() functions are used by gtest to log these objects. These PrintTo() | 
|  | 47 | // functions must be defined in the same namespace as the first argument. | 
|  | 48 | void PrintTo(const Extent& extent, ::std::ostream* os); | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 49 | void PrintTo(const ErrorCode& error_code, ::std::ostream* os); | 
| Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 50 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 51 | namespace test_utils { | 
|  | 52 |  | 
|  | 53 | // 300 byte pseudo-random string. Not null terminated. | 
|  | 54 | // This does not gzip compress well. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 55 | extern const uint8_t kRandomString[300]; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | // Writes the data passed to path. The file at path will be overwritten if it | 
|  | 58 | // exists. Returns true on success, false otherwise. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 59 | bool WriteFileVector(const std::string& path, const brillo::Blob& data); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 60 | bool WriteFileString(const std::string& path, const std::string& data); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 |  | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 62 | // Binds provided |filename| to an unused loopback device, whose name is written | 
|  | 63 | // to the string pointed to by |out_lo_dev_name|. The new loop device will be | 
|  | 64 | // read-only unless |writable| is set to true. Returns true on success, false | 
|  | 65 | // otherwise (along with corresponding test failures), in which case the content | 
|  | 66 | // of |out_lo_dev_name| is unknown. | 
|  | 67 | bool BindToUnusedLoopDevice(const std::string& filename, | 
|  | 68 | bool writable, | 
|  | 69 | std::string* out_lo_dev_name); | 
|  | 70 | bool UnbindLoopDevice(const std::string& lo_dev_name); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | // Returns true iff a == b | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 73 | bool ExpectVectorsEq(const brillo::Blob& a, const brillo::Blob& b); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 74 |  | 
|  | 75 | inline int System(const std::string& cmd) { | 
|  | 76 | return system(cmd.c_str()); | 
|  | 77 | } | 
|  | 78 |  | 
| Gilad Arnold | 2c2b884 | 2013-07-16 06:44:37 -0700 | [diff] [blame] | 79 | inline int Symlink(const std::string& oldpath, const std::string& newpath) { | 
|  | 80 | return symlink(oldpath.c_str(), newpath.c_str()); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | inline int Chmod(const std::string& path, mode_t mode) { | 
|  | 84 | return chmod(path.c_str(), mode); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | inline int Mkdir(const std::string& path, mode_t mode) { | 
|  | 88 | return mkdir(path.c_str(), mode); | 
|  | 89 | } | 
|  | 90 |  | 
| Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 91 | inline int Chdir(const std::string& path) { | 
|  | 92 | return chdir(path.c_str()); | 
|  | 93 | } | 
|  | 94 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 95 | // Reads a symlink from disk. Returns empty string on failure. | 
|  | 96 | std::string Readlink(const std::string& path); | 
|  | 97 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 98 | // Checks if xattr is supported in the directory specified by | 
|  | 99 | // |dir_path| which must be writable. Returns true if the feature is | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 100 | // supported, false if not or if an error occurred. | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 101 | bool IsXAttrSupported(const base::FilePath& dir_path); | 
|  | 102 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 103 | void FillWithData(brillo::Blob* buffer); | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 104 |  | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 105 | // Compare the value of native array for download source parameter. | 
|  | 106 | MATCHER_P(DownloadSourceMatcher, source_array, "") { | 
|  | 107 | return std::equal(source_array, source_array + kNumDownloadSources, arg); | 
|  | 108 | } | 
|  | 109 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 110 | // Class to unmount FS when object goes out of scope | 
|  | 111 | class ScopedFilesystemUnmounter { | 
|  | 112 | public: | 
|  | 113 | explicit ScopedFilesystemUnmounter(const std::string& mountpoint) | 
|  | 114 | : mountpoint_(mountpoint), | 
|  | 115 | should_unmount_(true) {} | 
|  | 116 | ~ScopedFilesystemUnmounter() { | 
|  | 117 | if (should_unmount_) { | 
|  | 118 | utils::UnmountFilesystem(mountpoint_); | 
|  | 119 | } | 
|  | 120 | } | 
|  | 121 | void set_should_unmount(bool unmount) { should_unmount_ = unmount; } | 
|  | 122 | private: | 
|  | 123 | const std::string mountpoint_; | 
|  | 124 | bool should_unmount_; | 
|  | 125 | DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter); | 
|  | 126 | }; | 
|  | 127 |  | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 128 | class ScopedLoopbackDeviceBinder { | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 129 | public: | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 130 | ScopedLoopbackDeviceBinder(const std::string& file, | 
|  | 131 | bool writable, | 
|  | 132 | std::string* dev) { | 
|  | 133 | is_bound_ = BindToUnusedLoopDevice(file, writable, &dev_); | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(is_bound_); | 
| 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 | if (is_bound_ && dev) | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 137 | *dev = dev_; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | ~ScopedLoopbackDeviceBinder() { | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 141 | if (!is_bound_) | 
|  | 142 | return; | 
|  | 143 |  | 
| Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 144 | for (int retry = 0; retry < 5; retry++) { | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 145 | if (UnbindLoopDevice(dev_)) | 
| Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 146 | return; | 
| Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 147 | sleep(1); | 
|  | 148 | } | 
|  | 149 | ADD_FAILURE(); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 150 | } | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 151 |  | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 152 | const std::string &dev() { | 
|  | 153 | EXPECT_TRUE(is_bound_); | 
|  | 154 | return dev_; | 
|  | 155 | } | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 156 |  | 
| Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 157 | bool is_bound() const { return is_bound_; } | 
|  | 158 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 159 | private: | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 160 | std::string dev_; | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 161 | bool is_bound_; | 
| Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 162 | DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 163 | }; | 
|  | 164 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 165 | class ScopedTempFile { | 
|  | 166 | public: | 
| Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 167 | ScopedTempFile() : ScopedTempFile("update_engine_test_temp_file.XXXXXX") {} | 
|  | 168 |  | 
|  | 169 | explicit ScopedTempFile(const std::string& pattern) { | 
|  | 170 | EXPECT_TRUE(utils::MakeTempFile(pattern, &path_, nullptr)); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 171 | unlinker_.reset(new ScopedPathUnlinker(path_)); | 
|  | 172 | } | 
| Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 173 |  | 
| Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 174 | const std::string& path() const { return path_; } | 
| Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 175 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 176 | private: | 
|  | 177 | std::string path_; | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 178 | std::unique_ptr<ScopedPathUnlinker> unlinker_; | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 179 | }; | 
|  | 180 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 181 | class ScopedLoopMounter { | 
|  | 182 | public: | 
|  | 183 | explicit ScopedLoopMounter(const std::string& file_path, | 
|  | 184 | std::string* mnt_path, | 
|  | 185 | unsigned long flags);  // NOLINT(runtime/int) | 
|  | 186 |  | 
|  | 187 | private: | 
|  | 188 | // These objects must be destructed in the following order: | 
|  | 189 | //   ScopedFilesystemUnmounter (the file system must be unmounted first) | 
|  | 190 | //   ScopedLoopbackDeviceBinder (then the loop device can be deleted) | 
|  | 191 | //   ScopedDirRemover (then the mount point can be deleted) | 
| Sen Jiang | 371615b | 2016-04-13 15:54:29 -0700 | [diff] [blame] | 192 | base::ScopedTempDir temp_dir_; | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 193 | std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_; | 
|  | 194 | std::unique_ptr<ScopedFilesystemUnmounter> unmounter_; | 
|  | 195 | }; | 
|  | 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(); | 
| Sen Jiang | 260f03b | 2016-03-21 15:34:58 -0700 | [diff] [blame] | 200 | // Returns the path of the build artifact specified in |relative_path|. | 
|  | 201 | std::string GetBuildArtifactsPath(const std::string& relative_path); | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 202 |  | 
| Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 203 | }  // namespace test_utils | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 204 |  | 
|  | 205 | // Useful actions for test. These need to be defined in the | 
|  | 206 | // chromeos_update_engine namespace. | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 207 |  | 
|  | 208 | class NoneType; | 
|  | 209 |  | 
|  | 210 | template<typename T> | 
|  | 211 | class ObjectFeederAction; | 
|  | 212 |  | 
|  | 213 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 214 | class ActionTraits<ObjectFeederAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 215 | public: | 
|  | 216 | typedef T OutputObjectType; | 
|  | 217 | typedef NoneType InputObjectType; | 
|  | 218 | }; | 
|  | 219 |  | 
|  | 220 | // This is a simple Action class for testing. It feeds an object into | 
|  | 221 | // another action. | 
|  | 222 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 223 | class ObjectFeederAction : public Action<ObjectFeederAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 224 | public: | 
|  | 225 | typedef NoneType InputObjectType; | 
|  | 226 | typedef T OutputObjectType; | 
|  | 227 | void PerformAction() { | 
|  | 228 | LOG(INFO) << "feeder running!"; | 
|  | 229 | CHECK(this->processor_); | 
|  | 230 | if (this->HasOutputPipe()) { | 
|  | 231 | this->SetOutputObject(out_obj_); | 
|  | 232 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 233 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 234 | } | 
|  | 235 | static std::string StaticType() { return "ObjectFeederAction"; } | 
|  | 236 | std::string Type() const { return StaticType(); } | 
|  | 237 | void set_obj(const T& out_obj) { | 
|  | 238 | out_obj_ = out_obj; | 
|  | 239 | } | 
|  | 240 | private: | 
|  | 241 | T out_obj_; | 
|  | 242 | }; | 
|  | 243 |  | 
|  | 244 | template<typename T> | 
|  | 245 | class ObjectCollectorAction; | 
|  | 246 |  | 
|  | 247 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 248 | class ActionTraits<ObjectCollectorAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 249 | public: | 
|  | 250 | typedef NoneType OutputObjectType; | 
|  | 251 | typedef T InputObjectType; | 
|  | 252 | }; | 
|  | 253 |  | 
|  | 254 | // This is a simple Action class for testing. It receives an object from | 
|  | 255 | // another action. | 
|  | 256 | template<typename T> | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 257 | class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 258 | public: | 
|  | 259 | typedef T InputObjectType; | 
|  | 260 | typedef NoneType OutputObjectType; | 
|  | 261 | void PerformAction() { | 
|  | 262 | LOG(INFO) << "collector running!"; | 
|  | 263 | ASSERT_TRUE(this->processor_); | 
|  | 264 | if (this->HasInputObject()) { | 
|  | 265 | object_ = this->GetInputObject(); | 
|  | 266 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 267 | this->processor_->ActionComplete(this, ErrorCode::kSuccess); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 268 | } | 
|  | 269 | static std::string StaticType() { return "ObjectCollectorAction"; } | 
|  | 270 | std::string Type() const { return StaticType(); } | 
|  | 271 | const T& object() const { return object_; } | 
|  | 272 | private: | 
|  | 273 | T object_; | 
|  | 274 | }; | 
|  | 275 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 276 | }  // namespace chromeos_update_engine | 
|  | 277 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 278 | #endif  // UPDATE_ENGINE_COMMON_TEST_UTILS_H_ |