Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_TEST_UTILS_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_TEST_UTILS_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 8 | #include <set> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 9 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 10 | #include <vector> |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 11 | |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 12 | #include <base/memory/scoped_ptr.h> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 14 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 15 | #include "update_engine/action.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 16 | #include "update_engine/subprocess.h" |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | |
| 19 | // These are some handy functions for unittests. |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | // Writes the data passed to path. The file at path will be overwritten if it |
| 24 | // exists. Returns true on success, false otherwise. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 25 | bool WriteFileVector(const std::string& path, const std::vector<char>& data); |
| 26 | bool WriteFileString(const std::string& path, const std::string& data); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 28 | // Reads a symlink from disk. Returns empty string on failure. |
| 29 | std::string Readlink(const std::string& path); |
| 30 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | // Gzip compresses the data passed using the gzip command line program. |
| 32 | // Returns compressed data back. |
| 33 | std::vector<char> GzipCompressData(const std::vector<char>& data); |
| 34 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 35 | // Gives back a 512-bytes length array that contains an MBR with |
| 36 | // the first partition is marked bootable. |
| 37 | std::vector<char> GenerateSampleMbr(); |
| 38 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame^] | 39 | std::string BindToUnusedLoopDevice(const std::string &filename); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 40 | |
| 41 | // Returns true iff a == b |
| 42 | bool ExpectVectorsEq(const std::vector<char>& a, const std::vector<char>& b); |
| 43 | |
| 44 | inline int System(const std::string& cmd) { |
| 45 | return system(cmd.c_str()); |
| 46 | } |
| 47 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 48 | void FillWithData(std::vector<char>* buffer); |
| 49 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | // 300 byte pseudo-random string. Not null terminated. |
| 52 | // This does not gzip compress well. |
| 53 | const unsigned char kRandomString[] = { |
| 54 | 0xf2, 0xb7, 0x55, 0x92, 0xea, 0xa6, 0xc9, 0x57, |
| 55 | 0xe0, 0xf8, 0xeb, 0x34, 0x93, 0xd9, 0xc4, 0x8f, |
| 56 | 0xcb, 0x20, 0xfa, 0x37, 0x4b, 0x40, 0xcf, 0xdc, |
| 57 | 0xa5, 0x08, 0x70, 0x89, 0x79, 0x35, 0xe2, 0x3d, |
| 58 | 0x56, 0xa4, 0x75, 0x73, 0xa3, 0x6d, 0xd1, 0xd5, |
| 59 | 0x26, 0xbb, 0x9c, 0x60, 0xbd, 0x2f, 0x5a, 0xfa, |
| 60 | 0xb7, 0xd4, 0x3a, 0x50, 0xa7, 0x6b, 0x3e, 0xfd, |
| 61 | 0x61, 0x2b, 0x3a, 0x31, 0x30, 0x13, 0x33, 0x53, |
| 62 | 0xdb, 0xd0, 0x32, 0x71, 0x5c, 0x39, 0xed, 0xda, |
| 63 | 0xb4, 0x84, 0xca, 0xbc, 0xbd, 0x78, 0x1c, 0x0c, |
| 64 | 0xd8, 0x0b, 0x41, 0xe8, 0xe1, 0xe0, 0x41, 0xad, |
| 65 | 0x03, 0x12, 0xd3, 0x3d, 0xb8, 0x75, 0x9b, 0xe6, |
| 66 | 0xd9, 0x01, 0xd0, 0x87, 0xf4, 0x36, 0xfa, 0xa7, |
| 67 | 0x0a, 0xfa, 0xc5, 0x87, 0x65, 0xab, 0x9a, 0x7b, |
| 68 | 0xeb, 0x58, 0x23, 0xf0, 0xa8, 0x0a, 0xf2, 0x33, |
| 69 | 0x3a, 0xe2, 0xe3, 0x35, 0x74, 0x95, 0xdd, 0x3c, |
| 70 | 0x59, 0x5a, 0xd9, 0x52, 0x3a, 0x3c, 0xac, 0xe5, |
| 71 | 0x15, 0x87, 0x6d, 0x82, 0xbc, 0xf8, 0x7d, 0xbe, |
| 72 | 0xca, 0xd3, 0x2c, 0xd6, 0xec, 0x38, 0xeb, 0xe4, |
| 73 | 0x53, 0xb0, 0x4c, 0x3f, 0x39, 0x29, 0xf7, 0xa4, |
| 74 | 0x73, 0xa8, 0xcb, 0x32, 0x50, 0x05, 0x8c, 0x1c, |
| 75 | 0x1c, 0xca, 0xc9, 0x76, 0x0b, 0x8f, 0x6b, 0x57, |
| 76 | 0x1f, 0x24, 0x2b, 0xba, 0x82, 0xba, 0xed, 0x58, |
| 77 | 0xd8, 0xbf, 0xec, 0x06, 0x64, 0x52, 0x6a, 0x3f, |
| 78 | 0xe4, 0xad, 0xce, 0x84, 0xb4, 0x27, 0x55, 0x14, |
| 79 | 0xe3, 0x75, 0x59, 0x73, 0x71, 0x51, 0xea, 0xe8, |
| 80 | 0xcc, 0xda, 0x4f, 0x09, 0xaf, 0xa4, 0xbc, 0x0e, |
| 81 | 0xa6, 0x1f, 0xe2, 0x3a, 0xf8, 0x96, 0x7d, 0x30, |
| 82 | 0x23, 0xc5, 0x12, 0xb5, 0xd8, 0x73, 0x6b, 0x71, |
| 83 | 0xab, 0xf1, 0xd7, 0x43, 0x58, 0xa7, 0xc9, 0xf0, |
| 84 | 0xe4, 0x85, 0x1c, 0xd6, 0x92, 0x50, 0x2c, 0x98, |
| 85 | 0x36, 0xfe, 0x87, 0xaf, 0x43, 0x8f, 0x8f, 0xf5, |
| 86 | 0x88, 0x48, 0x18, 0x42, 0xcf, 0x42, 0xc1, 0xa8, |
| 87 | 0xe8, 0x05, 0x08, 0xa1, 0x45, 0x70, 0x5b, 0x8c, |
| 88 | 0x39, 0x28, 0xab, 0xe9, 0x6b, 0x51, 0xd2, 0xcb, |
| 89 | 0x30, 0x04, 0xea, 0x7d, 0x2f, 0x6e, 0x6c, 0x3b, |
| 90 | 0x5f, 0x82, 0xd9, 0x5b, 0x89, 0x37, 0x65, 0x65, |
| 91 | 0xbe, 0x9f, 0xa3, 0x5d |
| 92 | }; |
| 93 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 94 | const char* const kMountPath = "/tmp/UpdateEngineTests_mnt"; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 95 | } // namespace {} |
| 96 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 97 | // Creates an empty ext image. |
| 98 | void CreateEmptyExtImageAtPath(const std::string& path, |
| 99 | size_t size, |
| 100 | int block_size); |
| 101 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 102 | // Creates an ext image with some files in it. The paths creates are |
| 103 | // returned in out_paths. |
| 104 | void CreateExtImageAtPath(const std::string& path, |
| 105 | std::vector<std::string>* out_paths); |
| 106 | |
| 107 | // Verifies that for each path in paths, it exists in the filesystem under |
| 108 | // parent. Also, verifies that no additional paths are present under parent. |
| 109 | // Also tests properties of various files created by CreateExtImageAtPath(). |
| 110 | // Intentionally copies expected_paths. |
| 111 | void VerifyAllPaths(const std::string& parent, |
| 112 | std::set<std::string> expected_paths); |
| 113 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame^] | 114 | class ScopedLoopbackDeviceBinder { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 115 | public: |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame^] | 116 | ScopedLoopbackDeviceBinder(const std::string& file, std::string* dev) { |
| 117 | dev_ = BindToUnusedLoopDevice(file); |
| 118 | |
| 119 | if (dev) |
| 120 | *dev = dev_; |
| 121 | } |
| 122 | |
| 123 | ~ScopedLoopbackDeviceBinder() { |
Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 124 | for (int retry = 0; retry < 5; retry++) { |
| 125 | std::vector<std::string> args; |
| 126 | args.push_back("/sbin/losetup"); |
| 127 | args.push_back("-d"); |
| 128 | args.push_back(dev_); |
| 129 | int return_code = 0; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 130 | EXPECT_TRUE(Subprocess::SynchronousExec(args, &return_code, NULL)); |
Darin Petkov | cf56248 | 2010-12-03 10:31:00 -0800 | [diff] [blame] | 131 | if (return_code == 0) { |
| 132 | return; |
| 133 | } |
| 134 | sleep(1); |
| 135 | } |
| 136 | ADD_FAILURE(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 137 | } |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame^] | 138 | |
| 139 | const std::string &dev() { return dev_; } |
| 140 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 141 | private: |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame^] | 142 | std::string dev_; |
| 143 | DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 146 | class ScopedTempFile { |
| 147 | public: |
| 148 | ScopedTempFile() { |
| 149 | EXPECT_TRUE(utils::MakeTempFile("/tmp/update_engine_test_temp_file.XXXXXX", |
| 150 | &path_, |
| 151 | NULL)); |
| 152 | unlinker_.reset(new ScopedPathUnlinker(path_)); |
| 153 | } |
| 154 | const std::string& GetPath() { return path_; } |
| 155 | private: |
| 156 | std::string path_; |
| 157 | scoped_ptr<ScopedPathUnlinker> unlinker_; |
| 158 | }; |
| 159 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 160 | // Useful actions for test |
| 161 | |
| 162 | class NoneType; |
| 163 | |
| 164 | template<typename T> |
| 165 | class ObjectFeederAction; |
| 166 | |
| 167 | template<typename T> |
| 168 | class ActionTraits<ObjectFeederAction<T> > { |
| 169 | public: |
| 170 | typedef T OutputObjectType; |
| 171 | typedef NoneType InputObjectType; |
| 172 | }; |
| 173 | |
| 174 | // This is a simple Action class for testing. It feeds an object into |
| 175 | // another action. |
| 176 | template<typename T> |
| 177 | struct ObjectFeederAction : public Action<ObjectFeederAction<T> > { |
| 178 | public: |
| 179 | typedef NoneType InputObjectType; |
| 180 | typedef T OutputObjectType; |
| 181 | void PerformAction() { |
| 182 | LOG(INFO) << "feeder running!"; |
| 183 | CHECK(this->processor_); |
| 184 | if (this->HasOutputPipe()) { |
| 185 | this->SetOutputObject(out_obj_); |
| 186 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 187 | this->processor_->ActionComplete(this, kActionCodeSuccess); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 188 | } |
| 189 | static std::string StaticType() { return "ObjectFeederAction"; } |
| 190 | std::string Type() const { return StaticType(); } |
| 191 | void set_obj(const T& out_obj) { |
| 192 | out_obj_ = out_obj; |
| 193 | } |
| 194 | private: |
| 195 | T out_obj_; |
| 196 | }; |
| 197 | |
| 198 | template<typename T> |
| 199 | class ObjectCollectorAction; |
| 200 | |
| 201 | template<typename T> |
| 202 | class ActionTraits<ObjectCollectorAction<T> > { |
| 203 | public: |
| 204 | typedef NoneType OutputObjectType; |
| 205 | typedef T InputObjectType; |
| 206 | }; |
| 207 | |
| 208 | // This is a simple Action class for testing. It receives an object from |
| 209 | // another action. |
| 210 | template<typename T> |
| 211 | struct ObjectCollectorAction : public Action<ObjectCollectorAction<T> > { |
| 212 | public: |
| 213 | typedef T InputObjectType; |
| 214 | typedef NoneType OutputObjectType; |
| 215 | void PerformAction() { |
| 216 | LOG(INFO) << "collector running!"; |
| 217 | ASSERT_TRUE(this->processor_); |
| 218 | if (this->HasInputObject()) { |
| 219 | object_ = this->GetInputObject(); |
| 220 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 221 | this->processor_->ActionComplete(this, kActionCodeSuccess); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 222 | } |
| 223 | static std::string StaticType() { return "ObjectCollectorAction"; } |
| 224 | std::string Type() const { return StaticType(); } |
| 225 | const T& object() const { return object_; } |
| 226 | private: |
| 227 | T object_; |
| 228 | }; |
| 229 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 230 | class ScopedLoopMounter { |
| 231 | public: |
| 232 | explicit ScopedLoopMounter(const std::string& file_path, |
| 233 | std::string* mnt_path, |
| 234 | unsigned long flags); |
| 235 | |
| 236 | private: |
| 237 | // These objects must be destructed in the following order: |
| 238 | // ScopedFilesystemUnmounter (the file system must be unmounted first) |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame^] | 239 | // ScopedLoopbackDeviceBinder (then the loop device can be deleted) |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 240 | // ScopedDirRemover (then the mount point can be deleted) |
| 241 | scoped_ptr<ScopedDirRemover> dir_remover_; |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame^] | 242 | scoped_ptr<ScopedLoopbackDeviceBinder> loop_binder_; |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 243 | scoped_ptr<ScopedFilesystemUnmounter> unmounter_; |
| 244 | }; |
| 245 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 246 | } // namespace chromeos_update_engine |
| 247 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 248 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_TEST_UTILS_H__ |