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