blob: 63ea74920386240283d682a0b77807f2467ff45d [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_TEST_UTILS_H_
18#define UPDATE_ENGINE_COMMON_TEST_UTILS_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Gilad Arnold2c2b8842013-07-16 06:44:37 -070020#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
Alex Deymo52490e72015-06-04 14:53:44 +020024// Streams used for gtest's PrintTo() functions.
25#include <iostream> // NOLINT(readability/streams)
Ben Chan02f7c1d2014-10-18 15:18:02 -070026#include <memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000027#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000028#include <vector>
Thieu Le5c7d9752010-12-15 16:09:28 -080029
Alex Deymo2b19cfb2015-03-26 00:35:07 -070030#include <base/files/file_path.h>
Sen Jiang371615b2016-04-13 15:54:29 -070031#include <base/files/scoped_temp_dir.h>
Tianjie Xud4777a12017-10-24 14:54:18 -070032#include <gmock/gmock.h>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000033#include <gtest/gtest.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080034
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/action.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/utils.h"
Alex Deymo52490e72015-06-04 14:53:44 +020037#include "update_engine/update_metadata.pb.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
39// These are some handy functions for unittests.
40
41namespace chromeos_update_engine {
Alex Deymo52490e72015-06-04 14:53:44 +020042
43// PrintTo() functions are used by gtest to log these objects. These PrintTo()
44// functions must be defined in the same namespace as the first argument.
45void PrintTo(const Extent& extent, ::std::ostream* os);
Alex Deymo64d98782016-02-05 18:03:48 -080046void PrintTo(const ErrorCode& error_code, ::std::ostream* os);
Alex Deymo52490e72015-06-04 14:53:44 +020047
Alex Deymo10875d92014-11-10 21:52:57 -080048namespace test_utils {
49
50// 300 byte pseudo-random string. Not null terminated.
51// This does not gzip compress well.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080052extern const uint8_t kRandomString[300];
rspangler@google.com49fdf182009-10-10 00:57:34 +000053
rspangler@google.com49fdf182009-10-10 00:57:34 +000054// Writes the data passed to path. The file at path will be overwritten if it
55// exists. Returns true on success, false otherwise.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070056bool WriteFileVector(const std::string& path, const brillo::Blob& data);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000057bool WriteFileString(const std::string& path, const std::string& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000058
Alex Deymocbc22742016-03-04 17:53:02 -080059// Binds provided |filename| to an unused loopback device, whose name is written
60// to the string pointed to by |out_lo_dev_name|. The new loop device will be
61// read-only unless |writable| is set to true. Returns true on success, false
62// otherwise (along with corresponding test failures), in which case the content
63// of |out_lo_dev_name| is unknown.
64bool BindToUnusedLoopDevice(const std::string& filename,
65 bool writable,
66 std::string* out_lo_dev_name);
67bool UnbindLoopDevice(const std::string& lo_dev_name);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000068
69// Returns true iff a == b
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070070bool ExpectVectorsEq(const brillo::Blob& a, const brillo::Blob& b);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000071
72inline int System(const std::string& cmd) {
73 return system(cmd.c_str());
74}
75
Alex Deymod15c5462016-03-09 18:11:12 -080076// Reads a symlink from disk. Returns empty string on failure.
77std::string Readlink(const std::string& path);
78
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070079void FillWithData(brillo::Blob* buffer);
Andrew de los Reyes80061062010-02-04 14:25:00 -080080
Andrewcc6ab9f2020-06-25 07:41:40 -070081// Compare the value of builtin array for download source parameter.
Tianjie Xud4777a12017-10-24 14:54:18 -070082MATCHER_P(DownloadSourceMatcher, source_array, "") {
83 return std::equal(source_array, source_array + kNumDownloadSources, arg);
84}
85
Alex Deymo10875d92014-11-10 21:52:57 -080086// Class to unmount FS when object goes out of scope
87class ScopedFilesystemUnmounter {
88 public:
89 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
Amin Hassanib2689592019-01-13 17:04:28 -080090 : mountpoint_(mountpoint), should_unmount_(true) {}
Alex Deymo10875d92014-11-10 21:52:57 -080091 ~ScopedFilesystemUnmounter() {
92 if (should_unmount_) {
93 utils::UnmountFilesystem(mountpoint_);
94 }
95 }
96 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
Amin Hassanib2689592019-01-13 17:04:28 -080097
Alex Deymo10875d92014-11-10 21:52:57 -080098 private:
99 const std::string mountpoint_;
100 bool should_unmount_;
101 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
102};
103
Don Garrett58e8b1f2012-01-31 16:38:16 -0800104class ScopedLoopbackDeviceBinder {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700105 public:
Alex Deymocbc22742016-03-04 17:53:02 -0800106 ScopedLoopbackDeviceBinder(const std::string& file,
107 bool writable,
108 std::string* dev) {
109 is_bound_ = BindToUnusedLoopDevice(file, writable, &dev_);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700110 EXPECT_TRUE(is_bound_);
Don Garrett58e8b1f2012-01-31 16:38:16 -0800111
Gilad Arnold19a45f02012-07-19 12:36:10 -0700112 if (is_bound_ && dev)
Don Garrett58e8b1f2012-01-31 16:38:16 -0800113 *dev = dev_;
114 }
115
116 ~ScopedLoopbackDeviceBinder() {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700117 if (!is_bound_)
118 return;
119
Darin Petkovcf562482010-12-03 10:31:00 -0800120 for (int retry = 0; retry < 5; retry++) {
Alex Deymocbc22742016-03-04 17:53:02 -0800121 if (UnbindLoopDevice(dev_))
Darin Petkovcf562482010-12-03 10:31:00 -0800122 return;
Darin Petkovcf562482010-12-03 10:31:00 -0800123 sleep(1);
124 }
125 ADD_FAILURE();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700126 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800127
Sen Jiangab6bda92019-01-09 16:33:51 -0800128 const std::string& dev() const {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700129 EXPECT_TRUE(is_bound_);
130 return dev_;
131 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800132
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700133 bool is_bound() const { return is_bound_; }
134
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700135 private:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800136 std::string dev_;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700137 bool is_bound_;
Don Garrett58e8b1f2012-01-31 16:38:16 -0800138 DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700139};
140
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700141class ScopedTempFile {
142 public:
Alex Deymobffa0602016-02-12 17:16:29 -0800143 ScopedTempFile() : ScopedTempFile("update_engine_test_temp_file.XXXXXX") {}
144
145 explicit ScopedTempFile(const std::string& pattern) {
146 EXPECT_TRUE(utils::MakeTempFile(pattern, &path_, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700147 unlinker_.reset(new ScopedPathUnlinker(path_));
148 }
Alex Deymobffa0602016-02-12 17:16:29 -0800149
Alex Deymo2c131bb2016-05-26 16:43:13 -0700150 const std::string& path() const { return path_; }
Alex Deymobffa0602016-02-12 17:16:29 -0800151
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700152 private:
153 std::string path_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700154 std::unique_ptr<ScopedPathUnlinker> unlinker_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700155};
156
Alex Deymo10875d92014-11-10 21:52:57 -0800157class ScopedLoopMounter {
158 public:
159 explicit ScopedLoopMounter(const std::string& file_path,
160 std::string* mnt_path,
161 unsigned long flags); // NOLINT(runtime/int)
162
163 private:
164 // These objects must be destructed in the following order:
165 // ScopedFilesystemUnmounter (the file system must be unmounted first)
166 // ScopedLoopbackDeviceBinder (then the loop device can be deleted)
167 // ScopedDirRemover (then the mount point can be deleted)
Sen Jiang371615b2016-04-13 15:54:29 -0700168 base::ScopedTempDir temp_dir_;
Alex Deymo10875d92014-11-10 21:52:57 -0800169 std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
170 std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
171};
172
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700173// Returns the path where the build artifacts are stored. This is the directory
174// where the unittest executable is being run from.
175base::FilePath GetBuildArtifactsPath();
Sen Jiang260f03b2016-03-21 15:34:58 -0700176// Returns the path of the build artifact specified in |relative_path|.
177std::string GetBuildArtifactsPath(const std::string& relative_path);
Alex Deymo10875d92014-11-10 21:52:57 -0800178
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700179} // namespace test_utils
Alex Deymo10875d92014-11-10 21:52:57 -0800180
181// Useful actions for test. These need to be defined in the
182// chromeos_update_engine namespace.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000183
184class NoneType;
185
Amin Hassanib2689592019-01-13 17:04:28 -0800186template <typename T>
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000187class ObjectFeederAction;
188
Amin Hassanib2689592019-01-13 17:04:28 -0800189template <typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700190class ActionTraits<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000191 public:
192 typedef T OutputObjectType;
193 typedef NoneType InputObjectType;
194};
195
196// This is a simple Action class for testing. It feeds an object into
197// another action.
Amin Hassanib2689592019-01-13 17:04:28 -0800198template <typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700199class ObjectFeederAction : public Action<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000200 public:
201 typedef NoneType InputObjectType;
202 typedef T OutputObjectType;
203 void PerformAction() {
204 LOG(INFO) << "feeder running!";
205 CHECK(this->processor_);
206 if (this->HasOutputPipe()) {
207 this->SetOutputObject(out_obj_);
208 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700209 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000210 }
211 static std::string StaticType() { return "ObjectFeederAction"; }
212 std::string Type() const { return StaticType(); }
Amin Hassanib2689592019-01-13 17:04:28 -0800213 void set_obj(const T& out_obj) { out_obj_ = out_obj; }
214
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000215 private:
216 T out_obj_;
217};
218
Amin Hassanib2689592019-01-13 17:04:28 -0800219template <typename T>
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000220class ObjectCollectorAction;
221
Amin Hassanib2689592019-01-13 17:04:28 -0800222template <typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700223class ActionTraits<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000224 public:
225 typedef NoneType OutputObjectType;
226 typedef T InputObjectType;
227};
228
229// This is a simple Action class for testing. It receives an object from
230// another action.
Amin Hassanib2689592019-01-13 17:04:28 -0800231template <typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700232class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000233 public:
234 typedef T InputObjectType;
235 typedef NoneType OutputObjectType;
236 void PerformAction() {
237 LOG(INFO) << "collector running!";
238 ASSERT_TRUE(this->processor_);
239 if (this->HasInputObject()) {
240 object_ = this->GetInputObject();
241 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700242 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000243 }
244 static std::string StaticType() { return "ObjectCollectorAction"; }
245 std::string Type() const { return StaticType(); }
246 const T& object() const { return object_; }
Amin Hassanib2689592019-01-13 17:04:28 -0800247
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000248 private:
249 T object_;
250};
251
rspangler@google.com49fdf182009-10-10 00:57:34 +0000252} // namespace chromeos_update_engine
253
Alex Deymo39910dc2015-11-09 17:04:30 -0800254#endif // UPDATE_ENGINE_COMMON_TEST_UTILS_H_