blob: 4a49fb273707ff6cd72b5c875824ee4bcbd3e372 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
adlr@google.comc98a7ed2009-12-04 18:54:03 +00005#include "update_engine/test_utils.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07006
adlr@google.comc98a7ed2009-12-04 18:54:03 +00007#include <errno.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <stdio.h>
9#include <stdlib.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070010#include <sys/stat.h>
11#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000012#include <unistd.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070013
adlr@google.comc98a7ed2009-12-04 18:54:03 +000014#include <set>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <string>
16#include <vector>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070017
Alex Deymo161c4a12014-05-16 15:56:21 -070018#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/strings/string_util.h>
20#include <base/strings/stringprintf.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070021
rspangler@google.com49fdf182009-10-10 00:57:34 +000022#include "update_engine/file_writer.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070023#include "update_engine/payload_generator/filesystem_iterator.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000024#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
Alex Deymo161c4a12014-05-16 15:56:21 -070026using base::StringPrintf;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000027using std::set;
rspangler@google.com49fdf182009-10-10 00:57:34 +000028using std::string;
29using std::vector;
30
31namespace chromeos_update_engine {
32
Gilad Arnolda6742b32014-01-11 00:18:34 -080033const char* const kMountPathTemplate = "UpdateEngineTests_mnt-XXXXXX";
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070034
Alex Deymof329b932014-10-30 01:37:48 -070035bool WriteFileVector(const string& path, const vector<char>& data) {
Andrew de los Reyes970bb282009-12-09 16:34:04 -080036 return utils::WriteFile(path.c_str(), &data[0], data.size());
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037}
38
Alex Deymof329b932014-10-30 01:37:48 -070039bool WriteFileString(const string& path, const string& data) {
Andrew de los Reyes970bb282009-12-09 16:34:04 -080040 return utils::WriteFile(path.c_str(), data.data(), data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +000041}
42
Gilad Arnold19a45f02012-07-19 12:36:10 -070043// Binds provided |filename| to an unused loopback device, whose name is written
44// to the string pointed to by |lo_dev_name_p|. Returns true on success, false
45// otherwise (along with corresponding test failures), in which case the content
46// of |lo_dev_name_p| is unknown.
47bool BindToUnusedLoopDevice(const string& filename, string* lo_dev_name_p) {
48 CHECK(lo_dev_name_p);
Don Garrett58e8b1f2012-01-31 16:38:16 -080049
Gilad Arnold19a45f02012-07-19 12:36:10 -070050 // Bind to an unused loopback device, sanity check the device name.
51 lo_dev_name_p->clear();
52 if (!(utils::ReadPipe("losetup --show -f " + filename, lo_dev_name_p) &&
53 StartsWithASCII(*lo_dev_name_p, "/dev/loop", true))) {
54 ADD_FAILURE();
55 return false;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000056 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000057
Gilad Arnold19a45f02012-07-19 12:36:10 -070058 // Strip anything from the first newline char.
59 size_t newline_pos = lo_dev_name_p->find('\n');
60 if (newline_pos != string::npos)
61 lo_dev_name_p->erase(newline_pos);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000062
Gilad Arnold19a45f02012-07-19 12:36:10 -070063 return true;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000064}
65
Andrew de los Reyes80061062010-02-04 14:25:00 -080066bool ExpectVectorsEq(const vector<char>& expected, const vector<char>& actual) {
67 EXPECT_EQ(expected.size(), actual.size());
68 if (expected.size() != actual.size())
adlr@google.comc98a7ed2009-12-04 18:54:03 +000069 return false;
Gilad Arnold617bbc22012-05-15 08:48:13 -070070 bool is_all_eq = true;
Andrew de los Reyes80061062010-02-04 14:25:00 -080071 for (unsigned int i = 0; i < expected.size(); i++) {
72 EXPECT_EQ(expected[i], actual[i]) << "offset: " << i;
Gilad Arnold617bbc22012-05-15 08:48:13 -070073 is_all_eq = is_all_eq && (expected[i] == actual[i]);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000074 }
Gilad Arnold617bbc22012-05-15 08:48:13 -070075 return is_all_eq;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000076}
77
Andrew de los Reyes80061062010-02-04 14:25:00 -080078void FillWithData(vector<char>* buffer) {
79 size_t input_counter = 0;
80 for (vector<char>::iterator it = buffer->begin(); it != buffer->end(); ++it) {
81 *it = kRandomString[input_counter];
82 input_counter++;
83 input_counter %= sizeof(kRandomString);
84 }
85}
86
Thieu Le5c7d9752010-12-15 16:09:28 -080087void CreateEmptyExtImageAtPath(const string& path,
88 size_t size,
89 int block_size) {
90 EXPECT_EQ(0, System(StringPrintf("dd if=/dev/zero of=%s"
91 " seek=%zu bs=1 count=1",
92 path.c_str(), size)));
93 EXPECT_EQ(0, System(StringPrintf("mkfs.ext3 -b %d -F %s",
94 block_size, path.c_str())));
95}
96
adlr@google.comc98a7ed2009-12-04 18:54:03 +000097void CreateExtImageAtPath(const string& path, vector<string>* out_paths) {
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070098 // create 10MiB sparse file, mounted at a unique location.
99 string mount_path;
100 CHECK(utils::MakeTempDirectory(kMountPathTemplate, &mount_path));
Alex Deymoa58b62a2013-08-08 21:21:48 -0700101 ScopedDirRemover mount_path_unlinker(mount_path);
Gilad Arnold61d9d2c2013-07-22 17:54:52 -0700102
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000103 EXPECT_EQ(0, System(StringPrintf("dd if=/dev/zero of=%s"
104 " seek=10485759 bs=1 count=1",
105 path.c_str())));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700106 EXPECT_EQ(0, System(StringPrintf("mkfs.ext3 -b 4096 -F %s", path.c_str())));
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000107 EXPECT_EQ(0, System(StringPrintf("mount -o loop %s %s", path.c_str(),
Gilad Arnold61d9d2c2013-07-22 17:54:52 -0700108 mount_path.c_str())));
109 EXPECT_EQ(0, System(StringPrintf("echo hi > %s/hi", mount_path.c_str())));
110 EXPECT_EQ(0, System(StringPrintf("echo hello > %s/hello",
111 mount_path.c_str())));
112 EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir", mount_path.c_str())));
113 EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir/empty_dir",
114 mount_path.c_str())));
115 EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir/mnt",
116 mount_path.c_str())));
117 EXPECT_EQ(0, System(StringPrintf("echo T > %s/some_dir/test",
118 mount_path.c_str())));
119 EXPECT_EQ(0, System(StringPrintf("mkfifo %s/some_dir/fifo",
120 mount_path.c_str())));
121 EXPECT_EQ(0, System(StringPrintf("mknod %s/cdev c 2 3", mount_path.c_str())));
122 EXPECT_EQ(0, System(StringPrintf("ln -s /some/target %s/sym",
123 mount_path.c_str())));
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000124 EXPECT_EQ(0, System(StringPrintf("ln %s/some_dir/test %s/testlink",
Gilad Arnold61d9d2c2013-07-22 17:54:52 -0700125 mount_path.c_str(), mount_path.c_str())));
126 EXPECT_EQ(0, System(StringPrintf("echo T > %s/srchardlink0",
127 mount_path.c_str())));
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800128 EXPECT_EQ(0, System(StringPrintf("ln %s/srchardlink0 %s/srchardlink1",
Gilad Arnold61d9d2c2013-07-22 17:54:52 -0700129 mount_path.c_str(), mount_path.c_str())));
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800130 EXPECT_EQ(0, System(StringPrintf("ln -s bogus %s/boguslink",
Gilad Arnold61d9d2c2013-07-22 17:54:52 -0700131 mount_path.c_str())));
132 EXPECT_TRUE(utils::UnmountFilesystem(mount_path.c_str()));
Thieu Le5c7d9752010-12-15 16:09:28 -0800133
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000134 if (out_paths) {
135 out_paths->clear();
136 out_paths->push_back("");
137 out_paths->push_back("/hi");
Andrew de los Reyes48a0a482011-02-22 15:32:11 -0800138 out_paths->push_back("/boguslink");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000139 out_paths->push_back("/hello");
140 out_paths->push_back("/some_dir");
141 out_paths->push_back("/some_dir/empty_dir");
142 out_paths->push_back("/some_dir/mnt");
143 out_paths->push_back("/some_dir/test");
144 out_paths->push_back("/some_dir/fifo");
145 out_paths->push_back("/cdev");
146 out_paths->push_back("/testlink");
147 out_paths->push_back("/sym");
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -0800148 out_paths->push_back("/srchardlink0");
149 out_paths->push_back("/srchardlink1");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000150 out_paths->push_back("/lost+found");
151 }
152}
153
154void VerifyAllPaths(const string& parent, set<string> expected_paths) {
155 FilesystemIterator iter(parent, set<string>());
156 ino_t test_ino = 0;
157 ino_t testlink_ino = 0;
158 while (!iter.IsEnd()) {
159 string path = iter.GetFullPath();
160 EXPECT_TRUE(expected_paths.find(path) != expected_paths.end()) << path;
161 EXPECT_EQ(1, expected_paths.erase(path));
162 if (utils::StringHasSuffix(path, "/hi") ||
163 utils::StringHasSuffix(path, "/hello") ||
164 utils::StringHasSuffix(path, "/test") ||
165 utils::StringHasSuffix(path, "/testlink")) {
166 EXPECT_TRUE(S_ISREG(iter.GetStat().st_mode));
167 if (utils::StringHasSuffix(path, "/test"))
168 test_ino = iter.GetStat().st_ino;
169 else if (utils::StringHasSuffix(path, "/testlink"))
170 testlink_ino = iter.GetStat().st_ino;
171 } else if (utils::StringHasSuffix(path, "/some_dir") ||
172 utils::StringHasSuffix(path, "/empty_dir") ||
173 utils::StringHasSuffix(path, "/mnt") ||
174 utils::StringHasSuffix(path, "/lost+found") ||
175 parent == path) {
176 EXPECT_TRUE(S_ISDIR(iter.GetStat().st_mode));
177 } else if (utils::StringHasSuffix(path, "/fifo")) {
178 EXPECT_TRUE(S_ISFIFO(iter.GetStat().st_mode));
179 } else if (utils::StringHasSuffix(path, "/cdev")) {
180 EXPECT_TRUE(S_ISCHR(iter.GetStat().st_mode));
181 } else if (utils::StringHasSuffix(path, "/sym")) {
182 EXPECT_TRUE(S_ISLNK(iter.GetStat().st_mode));
183 } else {
184 LOG(INFO) << "got non hardcoded path: " << path;
185 }
186 iter.Increment();
187 }
188 EXPECT_EQ(testlink_ino, test_ino);
189 EXPECT_NE(0, test_ino);
190 EXPECT_FALSE(iter.IsErr());
191 EXPECT_TRUE(expected_paths.empty());
192 if (!expected_paths.empty()) {
Alex Deymo020600d2014-11-05 21:05:55 -0800193 for (const string& path : expected_paths) {
194 LOG(INFO) << "extra path: " << path;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000195 }
196 }
197}
198
Don Garrett58e8b1f2012-01-31 16:38:16 -0800199ScopedLoopMounter::ScopedLoopMounter(const string& file_path,
200 string* mnt_path,
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700201 unsigned long flags) { // NOLINT - long
Gilad Arnolda6742b32014-01-11 00:18:34 -0800202 EXPECT_TRUE(utils::MakeTempDirectory("mnt.XXXXXX", mnt_path));
Thieu Le5c7d9752010-12-15 16:09:28 -0800203 dir_remover_.reset(new ScopedDirRemover(*mnt_path));
204
Don Garrett58e8b1f2012-01-31 16:38:16 -0800205 string loop_dev;
206 loop_binder_.reset(new ScopedLoopbackDeviceBinder(file_path, &loop_dev));
Thieu Le5c7d9752010-12-15 16:09:28 -0800207
208 EXPECT_TRUE(utils::MountFilesystem(loop_dev, *mnt_path, flags));
209 unmounter_.reset(new ScopedFilesystemUnmounter(*mnt_path));
210}
211
Alex Deymo53556ec2014-03-17 10:05:57 -0700212static gboolean RunGMainLoopOnTimeout(gpointer user_data) {
213 bool* timeout = static_cast<bool*>(user_data);
214 *timeout = true;
215 return FALSE; // Remove timeout source
216}
217
218void RunGMainLoopUntil(int timeout_msec, base::Callback<bool()> terminate) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700219 GMainLoop* loop = g_main_loop_new(nullptr, FALSE);
Alex Deymo53556ec2014-03-17 10:05:57 -0700220 GMainContext* context = g_main_context_default();
221
222 bool timeout = false;
223 guint source_id = g_timeout_add(
224 timeout_msec, RunGMainLoopOnTimeout, &timeout);
225
226 while (!timeout && (terminate.is_null() || !terminate.Run()))
227 g_main_context_iteration(context, TRUE);
228
229 g_source_remove(source_id);
230 g_main_loop_unref(loop);
231}
232
Alex Deymo7b948f02014-03-10 17:01:10 -0700233int RunGMainLoopMaxIterations(int iterations) {
234 int result;
235 GMainContext* context = g_main_context_default();
236 for (result = 0;
237 result < iterations && g_main_context_iteration(context, FALSE);
238 result++) {}
239 return result;
240}
241
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700242GValue* GValueNewString(const char* str) {
Alex Deymo5665d0c2014-05-28 17:45:43 -0700243 GValue* gval = g_new0(GValue, 1);
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700244 g_value_init(gval, G_TYPE_STRING);
245 g_value_set_string(gval, str);
246 return gval;
247}
248
249void GValueFree(gpointer arg) {
250 auto gval = reinterpret_cast<GValue*>(arg);
251 g_value_unset(gval);
Alex Deymo5665d0c2014-05-28 17:45:43 -0700252 g_free(gval);
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700253}
254
rspangler@google.com49fdf182009-10-10 00:57:34 +0000255} // namespace chromeos_update_engine