Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #include "update_engine/test_utils.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 6 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 7 | #include <errno.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 12 | #include <unistd.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 13 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 14 | #include <set> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 17 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 18 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 19 | #include <base/strings/string_util.h> |
| 20 | #include <base/strings/stringprintf.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 21 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | #include "update_engine/file_writer.h" |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 23 | #include "update_engine/payload_generator/filesystem_iterator.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 24 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 26 | using base::StringPrintf; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 27 | using std::set; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | using std::string; |
| 29 | using std::vector; |
| 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 33 | const char* const kMountPathTemplate = "UpdateEngineTests_mnt-XXXXXX"; |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 34 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 35 | bool WriteFileVector(const string& path, const vector<char>& data) { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 36 | return utils::WriteFile(path.c_str(), &data[0], data.size()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 39 | bool WriteFileString(const string& path, const string& data) { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 40 | return utils::WriteFile(path.c_str(), data.data(), data.size()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 43 | // 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. |
| 47 | bool BindToUnusedLoopDevice(const string& filename, string* lo_dev_name_p) { |
| 48 | CHECK(lo_dev_name_p); |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 49 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 50 | // 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 56 | } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 57 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 58 | // 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 62 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 63 | return true; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 66 | bool ExpectVectorsEq(const vector<char>& expected, const vector<char>& actual) { |
| 67 | EXPECT_EQ(expected.size(), actual.size()); |
| 68 | if (expected.size() != actual.size()) |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 69 | return false; |
Gilad Arnold | 617bbc2 | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 70 | bool is_all_eq = true; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 71 | for (unsigned int i = 0; i < expected.size(); i++) { |
| 72 | EXPECT_EQ(expected[i], actual[i]) << "offset: " << i; |
Gilad Arnold | 617bbc2 | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 73 | is_all_eq = is_all_eq && (expected[i] == actual[i]); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 74 | } |
Gilad Arnold | 617bbc2 | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 75 | return is_all_eq; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 78 | void 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 Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 87 | void 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 97 | void CreateExtImageAtPath(const string& path, vector<string>* out_paths) { |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 98 | // create 10MiB sparse file, mounted at a unique location. |
| 99 | string mount_path; |
| 100 | CHECK(utils::MakeTempDirectory(kMountPathTemplate, &mount_path)); |
Alex Deymo | a58b62a | 2013-08-08 21:21:48 -0700 | [diff] [blame] | 101 | ScopedDirRemover mount_path_unlinker(mount_path); |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 102 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 103 | 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 Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 106 | EXPECT_EQ(0, System(StringPrintf("mkfs.ext3 -b 4096 -F %s", path.c_str()))); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 107 | EXPECT_EQ(0, System(StringPrintf("mount -o loop %s %s", path.c_str(), |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 108 | 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 124 | EXPECT_EQ(0, System(StringPrintf("ln %s/some_dir/test %s/testlink", |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 125 | 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 Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 128 | EXPECT_EQ(0, System(StringPrintf("ln %s/srchardlink0 %s/srchardlink1", |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 129 | mount_path.c_str(), mount_path.c_str()))); |
Andrew de los Reyes | 48a0a48 | 2011-02-22 15:32:11 -0800 | [diff] [blame] | 130 | EXPECT_EQ(0, System(StringPrintf("ln -s bogus %s/boguslink", |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 131 | mount_path.c_str()))); |
| 132 | EXPECT_TRUE(utils::UnmountFilesystem(mount_path.c_str())); |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 133 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 134 | if (out_paths) { |
| 135 | out_paths->clear(); |
| 136 | out_paths->push_back(""); |
| 137 | out_paths->push_back("/hi"); |
Andrew de los Reyes | 48a0a48 | 2011-02-22 15:32:11 -0800 | [diff] [blame] | 138 | out_paths->push_back("/boguslink"); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 139 | 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 Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame] | 148 | out_paths->push_back("/srchardlink0"); |
| 149 | out_paths->push_back("/srchardlink1"); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 150 | out_paths->push_back("/lost+found"); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void 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 Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame^] | 193 | for (const string& path : expected_paths) { |
| 194 | LOG(INFO) << "extra path: " << path; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 199 | ScopedLoopMounter::ScopedLoopMounter(const string& file_path, |
| 200 | string* mnt_path, |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 201 | unsigned long flags) { // NOLINT - long |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 202 | EXPECT_TRUE(utils::MakeTempDirectory("mnt.XXXXXX", mnt_path)); |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 203 | dir_remover_.reset(new ScopedDirRemover(*mnt_path)); |
| 204 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 205 | string loop_dev; |
| 206 | loop_binder_.reset(new ScopedLoopbackDeviceBinder(file_path, &loop_dev)); |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 207 | |
| 208 | EXPECT_TRUE(utils::MountFilesystem(loop_dev, *mnt_path, flags)); |
| 209 | unmounter_.reset(new ScopedFilesystemUnmounter(*mnt_path)); |
| 210 | } |
| 211 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 212 | static gboolean RunGMainLoopOnTimeout(gpointer user_data) { |
| 213 | bool* timeout = static_cast<bool*>(user_data); |
| 214 | *timeout = true; |
| 215 | return FALSE; // Remove timeout source |
| 216 | } |
| 217 | |
| 218 | void RunGMainLoopUntil(int timeout_msec, base::Callback<bool()> terminate) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 219 | GMainLoop* loop = g_main_loop_new(nullptr, FALSE); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 220 | 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 Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 233 | int 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 Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 242 | GValue* GValueNewString(const char* str) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 243 | GValue* gval = g_new0(GValue, 1); |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 244 | g_value_init(gval, G_TYPE_STRING); |
| 245 | g_value_set_string(gval, str); |
| 246 | return gval; |
| 247 | } |
| 248 | |
| 249 | void GValueFree(gpointer arg) { |
| 250 | auto gval = reinterpret_cast<GValue*>(arg); |
| 251 | g_value_unset(gval); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 252 | g_free(gval); |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 253 | } |
| 254 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 255 | } // namespace chromeos_update_engine |