blob: ffe6f670ca34d9a1a3bc7665b5942eaf2b100adb [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>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000027#include <set>
rspangler@google.com49fdf182009-10-10 00:57:34 +000028#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000029#include <vector>
Thieu Le5c7d9752010-12-15 16:09:28 -080030
Alex Deymo53556ec2014-03-17 10:05:57 -070031#include <base/callback.h>
Alex Deymo2b19cfb2015-03-26 00:35:07 -070032#include <base/files/file_path.h>
Sen Jiang371615b2016-04-13 15:54:29 -070033#include <base/files/scoped_temp_dir.h>
Tianjie Xud4777a12017-10-24 14:54:18 -070034#include <gmock/gmock.h>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000035#include <gtest/gtest.h>
Thieu Le5c7d9752010-12-15 16:09:28 -080036
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/action.h"
38#include "update_engine/common/subprocess.h"
39#include "update_engine/common/utils.h"
Alex Deymo52490e72015-06-04 14:53:44 +020040#include "update_engine/update_metadata.pb.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000041
42// These are some handy functions for unittests.
43
44namespace chromeos_update_engine {
Alex Deymo52490e72015-06-04 14:53:44 +020045
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.
48void PrintTo(const Extent& extent, ::std::ostream* os);
Alex Deymo64d98782016-02-05 18:03:48 -080049void PrintTo(const ErrorCode& error_code, ::std::ostream* os);
Alex Deymo52490e72015-06-04 14:53:44 +020050
Alex Deymo10875d92014-11-10 21:52:57 -080051namespace test_utils {
52
53// 300 byte pseudo-random string. Not null terminated.
54// This does not gzip compress well.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080055extern const uint8_t kRandomString[300];
rspangler@google.com49fdf182009-10-10 00:57:34 +000056
rspangler@google.com49fdf182009-10-10 00:57:34 +000057// Writes the data passed to path. The file at path will be overwritten if it
58// exists. Returns true on success, false otherwise.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070059bool WriteFileVector(const std::string& path, const brillo::Blob& data);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000060bool WriteFileString(const std::string& path, const std::string& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000061
Alex Deymocbc22742016-03-04 17:53:02 -080062// 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.
67bool BindToUnusedLoopDevice(const std::string& filename,
68 bool writable,
69 std::string* out_lo_dev_name);
70bool UnbindLoopDevice(const std::string& lo_dev_name);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000071
72// Returns true iff a == b
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070073bool ExpectVectorsEq(const brillo::Blob& a, const brillo::Blob& b);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000074
75inline int System(const std::string& cmd) {
76 return system(cmd.c_str());
77}
78
Gilad Arnold2c2b8842013-07-16 06:44:37 -070079inline int Symlink(const std::string& oldpath, const std::string& newpath) {
80 return symlink(oldpath.c_str(), newpath.c_str());
81}
82
83inline int Chmod(const std::string& path, mode_t mode) {
84 return chmod(path.c_str(), mode);
85}
86
87inline int Mkdir(const std::string& path, mode_t mode) {
88 return mkdir(path.c_str(), mode);
89}
90
Gilad Arnold30dedd82013-07-03 06:19:09 -070091inline int Chdir(const std::string& path) {
92 return chdir(path.c_str());
93}
94
Alex Deymod15c5462016-03-09 18:11:12 -080095// Reads a symlink from disk. Returns empty string on failure.
96std::string Readlink(const std::string& path);
97
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070098void FillWithData(brillo::Blob* buffer);
Andrew de los Reyes80061062010-02-04 14:25:00 -080099
Tianjie Xud4777a12017-10-24 14:54:18 -0700100// Compare the value of native array for download source parameter.
101MATCHER_P(DownloadSourceMatcher, source_array, "") {
102 return std::equal(source_array, source_array + kNumDownloadSources, arg);
103}
104
Alex Deymo10875d92014-11-10 21:52:57 -0800105// Class to unmount FS when object goes out of scope
106class ScopedFilesystemUnmounter {
107 public:
108 explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
109 : mountpoint_(mountpoint),
110 should_unmount_(true) {}
111 ~ScopedFilesystemUnmounter() {
112 if (should_unmount_) {
113 utils::UnmountFilesystem(mountpoint_);
114 }
115 }
116 void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
117 private:
118 const std::string mountpoint_;
119 bool should_unmount_;
120 DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
121};
122
Don Garrett58e8b1f2012-01-31 16:38:16 -0800123class ScopedLoopbackDeviceBinder {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700124 public:
Alex Deymocbc22742016-03-04 17:53:02 -0800125 ScopedLoopbackDeviceBinder(const std::string& file,
126 bool writable,
127 std::string* dev) {
128 is_bound_ = BindToUnusedLoopDevice(file, writable, &dev_);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700129 EXPECT_TRUE(is_bound_);
Don Garrett58e8b1f2012-01-31 16:38:16 -0800130
Gilad Arnold19a45f02012-07-19 12:36:10 -0700131 if (is_bound_ && dev)
Don Garrett58e8b1f2012-01-31 16:38:16 -0800132 *dev = dev_;
133 }
134
135 ~ScopedLoopbackDeviceBinder() {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700136 if (!is_bound_)
137 return;
138
Darin Petkovcf562482010-12-03 10:31:00 -0800139 for (int retry = 0; retry < 5; retry++) {
Alex Deymocbc22742016-03-04 17:53:02 -0800140 if (UnbindLoopDevice(dev_))
Darin Petkovcf562482010-12-03 10:31:00 -0800141 return;
Darin Petkovcf562482010-12-03 10:31:00 -0800142 sleep(1);
143 }
144 ADD_FAILURE();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700145 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800146
Gilad Arnold19a45f02012-07-19 12:36:10 -0700147 const std::string &dev() {
148 EXPECT_TRUE(is_bound_);
149 return dev_;
150 }
Don Garrett58e8b1f2012-01-31 16:38:16 -0800151
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700152 bool is_bound() const { return is_bound_; }
153
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700154 private:
Don Garrett58e8b1f2012-01-31 16:38:16 -0800155 std::string dev_;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700156 bool is_bound_;
Don Garrett58e8b1f2012-01-31 16:38:16 -0800157 DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700158};
159
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700160class ScopedTempFile {
161 public:
Alex Deymobffa0602016-02-12 17:16:29 -0800162 ScopedTempFile() : ScopedTempFile("update_engine_test_temp_file.XXXXXX") {}
163
164 explicit ScopedTempFile(const std::string& pattern) {
165 EXPECT_TRUE(utils::MakeTempFile(pattern, &path_, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700166 unlinker_.reset(new ScopedPathUnlinker(path_));
167 }
Alex Deymobffa0602016-02-12 17:16:29 -0800168
Alex Deymo2c131bb2016-05-26 16:43:13 -0700169 const std::string& path() const { return path_; }
Alex Deymobffa0602016-02-12 17:16:29 -0800170
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700171 private:
172 std::string path_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700173 std::unique_ptr<ScopedPathUnlinker> unlinker_;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700174};
175
Alex Deymo10875d92014-11-10 21:52:57 -0800176class 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)
Sen Jiang371615b2016-04-13 15:54:29 -0700187 base::ScopedTempDir temp_dir_;
Alex Deymo10875d92014-11-10 21:52:57 -0800188 std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
189 std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
190};
191
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700192// Returns the path where the build artifacts are stored. This is the directory
193// where the unittest executable is being run from.
194base::FilePath GetBuildArtifactsPath();
Sen Jiang260f03b2016-03-21 15:34:58 -0700195// Returns the path of the build artifact specified in |relative_path|.
196std::string GetBuildArtifactsPath(const std::string& relative_path);
Alex Deymo10875d92014-11-10 21:52:57 -0800197
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700198} // namespace test_utils
Alex Deymo10875d92014-11-10 21:52:57 -0800199
200// Useful actions for test. These need to be defined in the
201// chromeos_update_engine namespace.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000202
203class NoneType;
204
205template<typename T>
206class ObjectFeederAction;
207
208template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700209class ActionTraits<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000210 public:
211 typedef T OutputObjectType;
212 typedef NoneType InputObjectType;
213};
214
215// This is a simple Action class for testing. It feeds an object into
216// another action.
217template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700218class ObjectFeederAction : public Action<ObjectFeederAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000219 public:
220 typedef NoneType InputObjectType;
221 typedef T OutputObjectType;
222 void PerformAction() {
223 LOG(INFO) << "feeder running!";
224 CHECK(this->processor_);
225 if (this->HasOutputPipe()) {
226 this->SetOutputObject(out_obj_);
227 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700228 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000229 }
230 static std::string StaticType() { return "ObjectFeederAction"; }
231 std::string Type() const { return StaticType(); }
232 void set_obj(const T& out_obj) {
233 out_obj_ = out_obj;
234 }
235 private:
236 T out_obj_;
237};
238
239template<typename T>
240class ObjectCollectorAction;
241
242template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700243class ActionTraits<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000244 public:
245 typedef NoneType OutputObjectType;
246 typedef T InputObjectType;
247};
248
249// This is a simple Action class for testing. It receives an object from
250// another action.
251template<typename T>
Ben Chanf9cb98c2014-09-21 18:31:30 -0700252class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000253 public:
254 typedef T InputObjectType;
255 typedef NoneType OutputObjectType;
256 void PerformAction() {
257 LOG(INFO) << "collector running!";
258 ASSERT_TRUE(this->processor_);
259 if (this->HasInputObject()) {
260 object_ = this->GetInputObject();
261 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700262 this->processor_->ActionComplete(this, ErrorCode::kSuccess);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000263 }
264 static std::string StaticType() { return "ObjectCollectorAction"; }
265 std::string Type() const { return StaticType(); }
266 const T& object() const { return object_; }
267 private:
268 T object_;
269};
270
rspangler@google.com49fdf182009-10-10 00:57:34 +0000271} // namespace chromeos_update_engine
272
Alex Deymo39910dc2015-11-09 17:04:30 -0800273#endif // UPDATE_ENGINE_COMMON_TEST_UTILS_H_