xref: /aosp_15_r20/system/update_engine/common/test_utils.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2012 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_TEST_UTILS_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_TEST_UTILS_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <sys/stat.h>
21*5a923131SAndroid Build Coastguard Worker #include <sys/types.h>
22*5a923131SAndroid Build Coastguard Worker #include <unistd.h>
23*5a923131SAndroid Build Coastguard Worker 
24*5a923131SAndroid Build Coastguard Worker // Streams used for gtest's PrintTo() functions.
25*5a923131SAndroid Build Coastguard Worker #include <iostream>  // NOLINT(readability/streams)
26*5a923131SAndroid Build Coastguard Worker #include <memory>
27*5a923131SAndroid Build Coastguard Worker #include <string>
28*5a923131SAndroid Build Coastguard Worker #include <vector>
29*5a923131SAndroid Build Coastguard Worker 
30*5a923131SAndroid Build Coastguard Worker #include <base/files/file_path.h>
31*5a923131SAndroid Build Coastguard Worker #include <base/files/scoped_temp_dir.h>
32*5a923131SAndroid Build Coastguard Worker #include <gtest/gtest.h>
33*5a923131SAndroid Build Coastguard Worker 
34*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/action.h"
35*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/testing_constants.h"
36*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/utils.h"
37*5a923131SAndroid Build Coastguard Worker #include "update_engine/update_metadata.pb.h"
38*5a923131SAndroid Build Coastguard Worker 
39*5a923131SAndroid Build Coastguard Worker // These are some handy functions for unittests.
40*5a923131SAndroid Build Coastguard Worker 
41*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
42*5a923131SAndroid Build Coastguard Worker 
43*5a923131SAndroid Build Coastguard Worker // PrintTo() functions are used by gtest to log these objects. These PrintTo()
44*5a923131SAndroid Build Coastguard Worker // functions must be defined in the same namespace as the first argument.
45*5a923131SAndroid Build Coastguard Worker void PrintTo(const Extent& extent, ::std::ostream* os);
46*5a923131SAndroid Build Coastguard Worker void PrintTo(const ErrorCode& error_code, ::std::ostream* os);
47*5a923131SAndroid Build Coastguard Worker 
48*5a923131SAndroid Build Coastguard Worker namespace test_utils {
49*5a923131SAndroid Build Coastguard Worker 
50*5a923131SAndroid Build Coastguard Worker // 300 byte pseudo-random string. Not null terminated.
51*5a923131SAndroid Build Coastguard Worker // This does not gzip compress well.
52*5a923131SAndroid Build Coastguard Worker extern const uint8_t kRandomString[300];
53*5a923131SAndroid Build Coastguard Worker 
54*5a923131SAndroid Build Coastguard Worker // Writes the data passed to path. The file at path will be overwritten if it
55*5a923131SAndroid Build Coastguard Worker // exists. Returns true on success, false otherwise.
56*5a923131SAndroid Build Coastguard Worker bool WriteFileVector(const std::string& path, const brillo::Blob& data);
57*5a923131SAndroid Build Coastguard Worker bool WriteFileString(const std::string& path, const std::string& data);
58*5a923131SAndroid Build Coastguard Worker 
59*5a923131SAndroid Build Coastguard Worker // Binds provided |filename| to an unused loopback device, whose name is written
60*5a923131SAndroid Build Coastguard Worker // to the string pointed to by |out_lo_dev_name|. The new loop device will be
61*5a923131SAndroid Build Coastguard Worker // read-only unless |writable| is set to true. Returns true on success, false
62*5a923131SAndroid Build Coastguard Worker // otherwise (along with corresponding test failures), in which case the content
63*5a923131SAndroid Build Coastguard Worker // of |out_lo_dev_name| is unknown.
64*5a923131SAndroid Build Coastguard Worker bool BindToUnusedLoopDevice(const std::string& filename,
65*5a923131SAndroid Build Coastguard Worker                             bool writable,
66*5a923131SAndroid Build Coastguard Worker                             std::string* out_lo_dev_name);
67*5a923131SAndroid Build Coastguard Worker bool UnbindLoopDevice(const std::string& lo_dev_name);
68*5a923131SAndroid Build Coastguard Worker 
69*5a923131SAndroid Build Coastguard Worker // Returns true iff a == b
70*5a923131SAndroid Build Coastguard Worker bool ExpectVectorsEq(const brillo::Blob& a, const brillo::Blob& b);
71*5a923131SAndroid Build Coastguard Worker 
System(const std::string & cmd)72*5a923131SAndroid Build Coastguard Worker inline int System(const std::string& cmd) {
73*5a923131SAndroid Build Coastguard Worker   return system(cmd.c_str());
74*5a923131SAndroid Build Coastguard Worker }
75*5a923131SAndroid Build Coastguard Worker 
76*5a923131SAndroid Build Coastguard Worker // Reads a symlink from disk. Returns empty string on failure.
77*5a923131SAndroid Build Coastguard Worker std::string Readlink(const std::string& path);
78*5a923131SAndroid Build Coastguard Worker 
79*5a923131SAndroid Build Coastguard Worker void FillWithData(brillo::Blob* buffer);
80*5a923131SAndroid Build Coastguard Worker 
81*5a923131SAndroid Build Coastguard Worker // Class to unmount FS when object goes out of scope
82*5a923131SAndroid Build Coastguard Worker class ScopedFilesystemUnmounter {
83*5a923131SAndroid Build Coastguard Worker  public:
ScopedFilesystemUnmounter(const std::string & mountpoint)84*5a923131SAndroid Build Coastguard Worker   explicit ScopedFilesystemUnmounter(const std::string& mountpoint)
85*5a923131SAndroid Build Coastguard Worker       : mountpoint_(mountpoint), should_unmount_(true) {}
~ScopedFilesystemUnmounter()86*5a923131SAndroid Build Coastguard Worker   ~ScopedFilesystemUnmounter() {
87*5a923131SAndroid Build Coastguard Worker     if (should_unmount_) {
88*5a923131SAndroid Build Coastguard Worker       utils::UnmountFilesystem(mountpoint_);
89*5a923131SAndroid Build Coastguard Worker     }
90*5a923131SAndroid Build Coastguard Worker   }
set_should_unmount(bool unmount)91*5a923131SAndroid Build Coastguard Worker   void set_should_unmount(bool unmount) { should_unmount_ = unmount; }
92*5a923131SAndroid Build Coastguard Worker 
93*5a923131SAndroid Build Coastguard Worker  private:
94*5a923131SAndroid Build Coastguard Worker   const std::string mountpoint_;
95*5a923131SAndroid Build Coastguard Worker   bool should_unmount_;
96*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ScopedFilesystemUnmounter);
97*5a923131SAndroid Build Coastguard Worker };
98*5a923131SAndroid Build Coastguard Worker 
99*5a923131SAndroid Build Coastguard Worker class ScopedLoopbackDeviceBinder {
100*5a923131SAndroid Build Coastguard Worker  public:
ScopedLoopbackDeviceBinder(const std::string & file,bool writable,std::string * dev)101*5a923131SAndroid Build Coastguard Worker   ScopedLoopbackDeviceBinder(const std::string& file,
102*5a923131SAndroid Build Coastguard Worker                              bool writable,
103*5a923131SAndroid Build Coastguard Worker                              std::string* dev) {
104*5a923131SAndroid Build Coastguard Worker     is_bound_ = BindToUnusedLoopDevice(file, writable, &dev_);
105*5a923131SAndroid Build Coastguard Worker     EXPECT_TRUE(is_bound_);
106*5a923131SAndroid Build Coastguard Worker 
107*5a923131SAndroid Build Coastguard Worker     if (is_bound_ && dev)
108*5a923131SAndroid Build Coastguard Worker       *dev = dev_;
109*5a923131SAndroid Build Coastguard Worker   }
110*5a923131SAndroid Build Coastguard Worker 
~ScopedLoopbackDeviceBinder()111*5a923131SAndroid Build Coastguard Worker   ~ScopedLoopbackDeviceBinder() {
112*5a923131SAndroid Build Coastguard Worker     if (!is_bound_)
113*5a923131SAndroid Build Coastguard Worker       return;
114*5a923131SAndroid Build Coastguard Worker 
115*5a923131SAndroid Build Coastguard Worker     for (int retry = 0; retry < 5; retry++) {
116*5a923131SAndroid Build Coastguard Worker       if (UnbindLoopDevice(dev_))
117*5a923131SAndroid Build Coastguard Worker         return;
118*5a923131SAndroid Build Coastguard Worker       sleep(1);
119*5a923131SAndroid Build Coastguard Worker     }
120*5a923131SAndroid Build Coastguard Worker     ADD_FAILURE();
121*5a923131SAndroid Build Coastguard Worker   }
122*5a923131SAndroid Build Coastguard Worker 
dev()123*5a923131SAndroid Build Coastguard Worker   const std::string& dev() const {
124*5a923131SAndroid Build Coastguard Worker     EXPECT_TRUE(is_bound_);
125*5a923131SAndroid Build Coastguard Worker     return dev_;
126*5a923131SAndroid Build Coastguard Worker   }
127*5a923131SAndroid Build Coastguard Worker 
is_bound()128*5a923131SAndroid Build Coastguard Worker   bool is_bound() const { return is_bound_; }
129*5a923131SAndroid Build Coastguard Worker 
130*5a923131SAndroid Build Coastguard Worker  private:
131*5a923131SAndroid Build Coastguard Worker   std::string dev_;
132*5a923131SAndroid Build Coastguard Worker   bool is_bound_;
133*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ScopedLoopbackDeviceBinder);
134*5a923131SAndroid Build Coastguard Worker };
135*5a923131SAndroid Build Coastguard Worker 
136*5a923131SAndroid Build Coastguard Worker class ScopedLoopMounter {
137*5a923131SAndroid Build Coastguard Worker  public:
138*5a923131SAndroid Build Coastguard Worker   explicit ScopedLoopMounter(const std::string& file_path,
139*5a923131SAndroid Build Coastguard Worker                              std::string* mnt_path,
140*5a923131SAndroid Build Coastguard Worker                              unsigned long flags);  // NOLINT(runtime/int)
141*5a923131SAndroid Build Coastguard Worker 
142*5a923131SAndroid Build Coastguard Worker  private:
143*5a923131SAndroid Build Coastguard Worker   // These objects must be destructed in the following order:
144*5a923131SAndroid Build Coastguard Worker   //   ScopedFilesystemUnmounter (the file system must be unmounted first)
145*5a923131SAndroid Build Coastguard Worker   //   ScopedLoopbackDeviceBinder (then the loop device can be deleted)
146*5a923131SAndroid Build Coastguard Worker   //   ScopedDirRemover (then the mount point can be deleted)
147*5a923131SAndroid Build Coastguard Worker   base::ScopedTempDir temp_dir_;
148*5a923131SAndroid Build Coastguard Worker   std::unique_ptr<ScopedLoopbackDeviceBinder> loop_binder_;
149*5a923131SAndroid Build Coastguard Worker   std::unique_ptr<ScopedFilesystemUnmounter> unmounter_;
150*5a923131SAndroid Build Coastguard Worker };
151*5a923131SAndroid Build Coastguard Worker 
152*5a923131SAndroid Build Coastguard Worker // Returns the path where the build artifacts are stored. This is the directory
153*5a923131SAndroid Build Coastguard Worker // where the unittest executable is being run from.
154*5a923131SAndroid Build Coastguard Worker base::FilePath GetBuildArtifactsPath();
155*5a923131SAndroid Build Coastguard Worker // Returns the path of the build artifact specified in |relative_path|.
156*5a923131SAndroid Build Coastguard Worker std::string GetBuildArtifactsPath(const std::string& relative_path);
157*5a923131SAndroid Build Coastguard Worker 
158*5a923131SAndroid Build Coastguard Worker }  // namespace test_utils
159*5a923131SAndroid Build Coastguard Worker 
160*5a923131SAndroid Build Coastguard Worker // Useful actions for test. These need to be defined in the
161*5a923131SAndroid Build Coastguard Worker // chromeos_update_engine namespace.
162*5a923131SAndroid Build Coastguard Worker 
163*5a923131SAndroid Build Coastguard Worker class NoneType;
164*5a923131SAndroid Build Coastguard Worker 
165*5a923131SAndroid Build Coastguard Worker template <typename T>
166*5a923131SAndroid Build Coastguard Worker class ObjectFeederAction;
167*5a923131SAndroid Build Coastguard Worker 
168*5a923131SAndroid Build Coastguard Worker template <typename T>
169*5a923131SAndroid Build Coastguard Worker class ActionTraits<ObjectFeederAction<T>> {
170*5a923131SAndroid Build Coastguard Worker  public:
171*5a923131SAndroid Build Coastguard Worker   typedef T OutputObjectType;
172*5a923131SAndroid Build Coastguard Worker   typedef NoneType InputObjectType;
173*5a923131SAndroid Build Coastguard Worker };
174*5a923131SAndroid Build Coastguard Worker 
175*5a923131SAndroid Build Coastguard Worker // This is a simple Action class for testing. It feeds an object into
176*5a923131SAndroid Build Coastguard Worker // another action.
177*5a923131SAndroid Build Coastguard Worker template <typename T>
178*5a923131SAndroid Build Coastguard Worker class ObjectFeederAction final : public Action<ObjectFeederAction<T>> {
179*5a923131SAndroid Build Coastguard Worker  public:
180*5a923131SAndroid Build Coastguard Worker   typedef NoneType InputObjectType;
181*5a923131SAndroid Build Coastguard Worker   typedef T OutputObjectType;
PerformAction()182*5a923131SAndroid Build Coastguard Worker   void PerformAction() {
183*5a923131SAndroid Build Coastguard Worker     LOG(INFO) << "feeder running!";
184*5a923131SAndroid Build Coastguard Worker     CHECK(this->processor_);
185*5a923131SAndroid Build Coastguard Worker     if (this->HasOutputPipe()) {
186*5a923131SAndroid Build Coastguard Worker       this->SetOutputObject(out_obj_);
187*5a923131SAndroid Build Coastguard Worker     }
188*5a923131SAndroid Build Coastguard Worker     this->processor_->ActionComplete(this, ErrorCode::kSuccess);
189*5a923131SAndroid Build Coastguard Worker   }
StaticType()190*5a923131SAndroid Build Coastguard Worker   static std::string StaticType() { return "ObjectFeederAction"; }
Type()191*5a923131SAndroid Build Coastguard Worker   std::string Type() const { return StaticType(); }
set_obj(const T & out_obj)192*5a923131SAndroid Build Coastguard Worker   void set_obj(const T& out_obj) { out_obj_ = out_obj; }
193*5a923131SAndroid Build Coastguard Worker 
194*5a923131SAndroid Build Coastguard Worker  private:
195*5a923131SAndroid Build Coastguard Worker   T out_obj_;
196*5a923131SAndroid Build Coastguard Worker };
197*5a923131SAndroid Build Coastguard Worker 
198*5a923131SAndroid Build Coastguard Worker template <typename T>
199*5a923131SAndroid Build Coastguard Worker class ObjectCollectorAction;
200*5a923131SAndroid Build Coastguard Worker 
201*5a923131SAndroid Build Coastguard Worker template <typename T>
202*5a923131SAndroid Build Coastguard Worker class ActionTraits<ObjectCollectorAction<T>> {
203*5a923131SAndroid Build Coastguard Worker  public:
204*5a923131SAndroid Build Coastguard Worker   typedef NoneType OutputObjectType;
205*5a923131SAndroid Build Coastguard Worker   typedef T InputObjectType;
206*5a923131SAndroid Build Coastguard Worker };
207*5a923131SAndroid Build Coastguard Worker 
208*5a923131SAndroid Build Coastguard Worker // This is a simple Action class for testing. It receives an object from
209*5a923131SAndroid Build Coastguard Worker // another action.
210*5a923131SAndroid Build Coastguard Worker template <typename T>
211*5a923131SAndroid Build Coastguard Worker class ObjectCollectorAction : public Action<ObjectCollectorAction<T>> {
212*5a923131SAndroid Build Coastguard Worker  public:
213*5a923131SAndroid Build Coastguard Worker   typedef T InputObjectType;
214*5a923131SAndroid Build Coastguard Worker   typedef NoneType OutputObjectType;
PerformAction()215*5a923131SAndroid Build Coastguard Worker   void PerformAction() {
216*5a923131SAndroid Build Coastguard Worker     LOG(INFO) << "collector running!";
217*5a923131SAndroid Build Coastguard Worker     ASSERT_TRUE(this->processor_);
218*5a923131SAndroid Build Coastguard Worker     if (this->HasInputObject()) {
219*5a923131SAndroid Build Coastguard Worker       object_ = this->GetInputObject();
220*5a923131SAndroid Build Coastguard Worker     }
221*5a923131SAndroid Build Coastguard Worker     this->processor_->ActionComplete(this, ErrorCode::kSuccess);
222*5a923131SAndroid Build Coastguard Worker   }
StaticType()223*5a923131SAndroid Build Coastguard Worker   static std::string StaticType() { return "ObjectCollectorAction"; }
Type()224*5a923131SAndroid Build Coastguard Worker   std::string Type() const { return StaticType(); }
object()225*5a923131SAndroid Build Coastguard Worker   const T& object() const { return object_; }
226*5a923131SAndroid Build Coastguard Worker 
227*5a923131SAndroid Build Coastguard Worker  private:
228*5a923131SAndroid Build Coastguard Worker   T object_;
229*5a923131SAndroid Build Coastguard Worker };
230*5a923131SAndroid Build Coastguard Worker 
231*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
232*5a923131SAndroid Build Coastguard Worker 
233*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_COMMON_TEST_UTILS_H_
234