Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(828)

Unified Diff: src/platform/update_engine/test_utils.cc

Issue 466036: AU: Beginnings of delta support (Closed)
Patch Set: Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/platform/update_engine/test_utils.h ('k') | src/platform/update_engine/testrunner.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/platform/update_engine/test_utils.cc
diff --git a/src/platform/update_engine/test_utils.cc b/src/platform/update_engine/test_utils.cc
index 6255119981c2ff23b44d95a622620969713c1a73..6346bf393523ed813b158091bbfbdfab29705570 100644
--- a/src/platform/update_engine/test_utils.cc
+++ b/src/platform/update_engine/test_utils.cc
@@ -2,72 +2,273 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "update_engine/test_utils.h"
#include <sys/stat.h>
#include <sys/types.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <set>
#include <string>
#include <vector>
-#include "update_engine/test_utils.h"
-#include "base/logging.h"
+#include "base/string_util.h"
+#include "chromeos/obsolete_logging.h"
#include "update_engine/file_writer.h"
+#include "update_engine/filesystem_iterator.h"
+#include "update_engine/utils.h"
+using std::set;
using std::string;
using std::vector;
namespace chromeos_update_engine {
-vector<char> ReadFile(const string& path) {
- vector<char> ret;
- FILE* fp = fopen(path.c_str(), "r");
- if (!fp)
- return ret;
- const size_t kChunkSize(1024);
- size_t read_size;
- do {
- char buf[kChunkSize];
- read_size = fread(buf, 1, kChunkSize, fp);
- ret.insert(ret.end(), buf, buf + read_size);
- } while (read_size == kChunkSize);
- fclose(fp);
- return ret;
-}
-
-bool WriteFile(const std::string& path, const std::vector<char>& data) {
+bool WriteFile(const char* path, const char* data, int data_len) {
DirectFileWriter writer;
- if (0 != writer.Open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644)) {
- return false;
- }
- if (static_cast<int>(data.size()) != writer.Write(&data[0], data.size())) {
- writer.Close();
- return false;
- }
- writer.Close();
+ TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path,
+ O_WRONLY | O_CREAT | O_TRUNC,
+ 0666));
+ ScopedFileWriterCloser closer(&writer);
+ TEST_AND_RETURN_FALSE_ERRNO(data_len == writer.Write(data, data_len));
return true;
}
+bool WriteFileVector(const std::string& path, const std::vector<char>& data) {
+ return WriteFile(path.c_str(), &data[0], data.size());
+}
+
+bool WriteFileString(const std::string& path, const std::string& data) {
+ return WriteFile(path.c_str(), data.data(), data.size());
+}
+
off_t FileSize(const string& path) {
struct stat stbuf;
int rc = stat(path.c_str(), &stbuf);
- CHECK_EQ(0, rc);
+ CHECK_EQ(rc, 0);
if (rc < 0)
return rc;
return stbuf.st_size;
}
+std::string Readlink(const std::string& path) {
+ vector<char> buf(PATH_MAX + 1);
+ ssize_t r = readlink(path.c_str(), &buf[0], buf.size());
+ if (r < 0)
+ return "";
+ CHECK_LT(r, static_cast<ssize_t>(buf.size()));
+ buf.resize(r);
+ string ret;
+ ret.insert(ret.begin(), buf.begin(), buf.end());
+ return ret;
+}
+
std::vector<char> GzipCompressData(const std::vector<char>& data) {
const char fname[] = "/tmp/GzipCompressDataTemp";
- if (!WriteFile(fname, data)) {
+ if (!WriteFileVector(fname, data)) {
system((string("rm ") + fname).c_str());
return vector<char>();
}
system((string("cat ") + fname + "|gzip>" + fname + ".gz").c_str());
system((string("rm ") + fname).c_str());
- vector<char> ret = ReadFile(string(fname) + ".gz");
+ vector<char> ret;
+ EXPECT_TRUE(utils::ReadFile(string(fname) + ".gz", &ret));
system((string("rm ") + fname + ".gz").c_str());
return ret;
}
+vector<char> GenerateSampleMbr() {
+ // This is the actual MBR from my dev machine. Partition 1 (the first)
+ // is currently marked bootable
+ char mbr[512] = {
+ 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c, 0xbe, 0x85, 0x7d, 0xe8, 0x40, 0x00, 0xeb,
+ 0x0e, 0xbe, 0x8a, 0x7d, 0xe8, 0x38, 0x00, 0xeb,
+ 0x06, 0xbe, 0x94, 0x7d, 0xe8, 0x30, 0x00, 0xbe,
+ 0x99, 0x7d, 0xe8, 0x2a, 0x00, 0xeb, 0xfe, 0x47,
+ 0x52, 0x55, 0x42, 0x20, 0x00, 0x47, 0x65, 0x6f,
+ 0x6d, 0x00, 0x48, 0x61, 0x72, 0x64, 0x20, 0x44,
+ 0x69, 0x73, 0x6b, 0x00, 0x52, 0x65, 0x61, 0x64,
+ 0x00, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x00,
+ 0xbb, 0x01, 0x00, 0xb4, 0x0e, 0xcd, 0x10, 0xac,
+ 0x3c, 0x00, 0x75, 0xf4, 0xc3, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x50, 0xc1, 0x04, 0x00, 0x00, 0x00, 0x80, 0x01,
+ 0x01, 0x00, 0x83, 0xfe, 0xff, 0xff, 0x3f, 0x00,
+ 0x00, 0x00, 0x09, 0x7f, 0x32, 0x06, 0x00, 0xfe,
+ 0xff, 0xff, 0x05, 0xfe, 0xff, 0xff, 0x48, 0x7f,
+ 0x32, 0x06, 0x79, 0x59, 0x2d, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+ };
+ vector<char> ret;
+ ret.insert(ret.begin(), mbr, mbr + sizeof(mbr));
+ return ret;
+}
+
+std::string GetUnusedLoopDevice() {
+ // get a loop device we can use for the install device
+ FILE* find_dev_cmd = popen("losetup -f", "r");
+ CHECK(find_dev_cmd);
+
+ string ret;
+ char dev[100] = {0};
+ size_t r;
+ while ((r = fread(dev, 1, sizeof(dev - 1), find_dev_cmd)) > 0) {
+ EXPECT_LT(r, sizeof(dev));
+ ret.insert(ret.end(), dev, dev + r);
+ }
+ EXPECT_EQ(r, 0);
+ EXPECT_TRUE(feof(find_dev_cmd));
+ fclose(find_dev_cmd);
+
+ // strip trailing \n on dev
+ if (*ret.rbegin() == '\n')
+ ret.resize(ret.size() - 1);
+
+ return ret;
+}
+
+bool ExpectVectorsEq(const vector<char>& a, const vector<char>& b) {
+ EXPECT_EQ(a.size(), b.size());
+ if (a.size() != b.size())
+ return false;
+ for (unsigned int i = 0; i < a.size(); i++) {
+ EXPECT_EQ(a[i], b[i]) << "offset: " << i;
+ }
+ return true;
+}
+
+void CreateExtImageAtPath(const string& path, vector<string>* out_paths) {
+ // create 10MiB sparse file
+ const char* const mount_path = kMountPath.c_str();
+ EXPECT_EQ(0, System(StringPrintf("dd if=/dev/zero of=%s"
+ " seek=10485759 bs=1 count=1",
+ path.c_str())));
+ EXPECT_EQ(0, System(StringPrintf("mkfs.ext3 -F %s", path.c_str())));
+ EXPECT_EQ(0, System(StringPrintf("mkdir -p %s", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("mount -o loop %s %s", path.c_str(),
+ mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("echo hi > %s/hi", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("echo hello > %s/hello", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir/empty_dir", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir/mnt", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("echo T > %s/some_dir/test", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("mkfifo %s/some_dir/fifo", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("mknod %s/cdev c 2 3", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("ln -s /some/target %s/sym", mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("ln %s/some_dir/test %s/testlink",
+ mount_path, mount_path)));
+ EXPECT_EQ(0, System(StringPrintf("umount %s", mount_path)));
+
+ if (out_paths) {
+ out_paths->clear();
+ out_paths->push_back("");
+ out_paths->push_back("/hi");
+ out_paths->push_back("/hello");
+ out_paths->push_back("/some_dir");
+ out_paths->push_back("/some_dir/empty_dir");
+ out_paths->push_back("/some_dir/mnt");
+ out_paths->push_back("/some_dir/test");
+ out_paths->push_back("/some_dir/fifo");
+ out_paths->push_back("/cdev");
+ out_paths->push_back("/testlink");
+ out_paths->push_back("/sym");
+ out_paths->push_back("/lost+found");
+ }
+}
+
+void VerifyAllPaths(const string& parent, set<string> expected_paths) {
+ FilesystemIterator iter(parent, set<string>());
+ ino_t test_ino = 0;
+ ino_t testlink_ino = 0;
+ while (!iter.IsEnd()) {
+ string path = iter.GetFullPath();
+ EXPECT_TRUE(expected_paths.find(path) != expected_paths.end()) << path;
+ EXPECT_EQ(1, expected_paths.erase(path));
+ if (utils::StringHasSuffix(path, "/hi") ||
+ utils::StringHasSuffix(path, "/hello") ||
+ utils::StringHasSuffix(path, "/test") ||
+ utils::StringHasSuffix(path, "/testlink")) {
+ EXPECT_TRUE(S_ISREG(iter.GetStat().st_mode));
+ if (utils::StringHasSuffix(path, "/test"))
+ test_ino = iter.GetStat().st_ino;
+ else if (utils::StringHasSuffix(path, "/testlink"))
+ testlink_ino = iter.GetStat().st_ino;
+ } else if (utils::StringHasSuffix(path, "/some_dir") ||
+ utils::StringHasSuffix(path, "/empty_dir") ||
+ utils::StringHasSuffix(path, "/mnt") ||
+ utils::StringHasSuffix(path, "/lost+found") ||
+ parent == path) {
+ EXPECT_TRUE(S_ISDIR(iter.GetStat().st_mode));
+ } else if (utils::StringHasSuffix(path, "/fifo")) {
+ EXPECT_TRUE(S_ISFIFO(iter.GetStat().st_mode));
+ } else if (utils::StringHasSuffix(path, "/cdev")) {
+ EXPECT_TRUE(S_ISCHR(iter.GetStat().st_mode));
+ } else if (utils::StringHasSuffix(path, "/sym")) {
+ EXPECT_TRUE(S_ISLNK(iter.GetStat().st_mode));
+ } else {
+ LOG(INFO) << "got non hardcoded path: " << path;
+ }
+ iter.Increment();
+ }
+ EXPECT_EQ(testlink_ino, test_ino);
+ EXPECT_NE(0, test_ino);
+ EXPECT_FALSE(iter.IsErr());
+ EXPECT_TRUE(expected_paths.empty());
+ if (!expected_paths.empty()) {
+ for (set<string>::const_iterator it = expected_paths.begin();
+ it != expected_paths.end(); ++it) {
+ LOG(INFO) << "extra path: " << *it;
+ }
+ }
+}
+
} // namespace chromeos_update_engine
« no previous file with comments | « src/platform/update_engine/test_utils.h ('k') | src/platform/update_engine/testrunner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698