Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | #include <gtest/gtest.h> |
| 12 | #include "base/basictypes.h" |
| 13 | #include "update_engine/extent_mapper.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 14 | #include "update_engine/graph_types.h" |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 15 | #include "update_engine/utils.h" |
| 16 | |
| 17 | using std::set; |
| 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | class ExtentMapperTest : public ::testing::Test {}; |
| 24 | |
| 25 | TEST(ExtentMapperTest, RunAsRootSimpleTest) { |
| 26 | // It's hard to have a concrete test for extent mapping without including |
| 27 | // a specific filesystem image. |
| 28 | // In lieu of this, we do a weak test: make sure the extents of the unittest |
| 29 | // executable are consistent and they match with the size of the file. |
| 30 | const string kFilename = "/proc/self/exe"; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 31 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 32 | uint32_t block_size = 0; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 33 | EXPECT_TRUE(extent_mapper::GetFilesystemBlockSize(kFilename, &block_size)); |
| 34 | EXPECT_GT(block_size, 0); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 35 | |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 36 | vector<Extent> extents; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 37 | |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 38 | ASSERT_TRUE(extent_mapper::ExtentsForFileFibmap(kFilename, &extents)); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 39 | |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 40 | EXPECT_FALSE(extents.empty()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 41 | set<uint64_t> blocks; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 42 | |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 43 | for (vector<Extent>::const_iterator it = extents.begin(); |
| 44 | it != extents.end(); ++it) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 45 | for (uint64_t block = it->start_block(); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 46 | block < it->start_block() + it->num_blocks(); |
| 47 | block++) { |
| 48 | EXPECT_FALSE(utils::SetContainsKey(blocks, block)); |
| 49 | blocks.insert(block); |
| 50 | } |
| 51 | } |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 52 | |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 53 | struct stat stbuf; |
| 54 | EXPECT_EQ(0, stat(kFilename.c_str(), &stbuf)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 55 | EXPECT_EQ(blocks.size(), (stbuf.st_size + block_size - 1)/block_size); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 56 | |
| 57 | // Map a 2-block chunk at offset |block_size|. |
| 58 | vector<Extent> chunk_extents; |
| 59 | ASSERT_TRUE( |
| 60 | extent_mapper::ExtentsForFileChunkFibmap(kFilename, |
| 61 | block_size, |
| 62 | block_size + 1, |
| 63 | &chunk_extents)); |
| 64 | EXPECT_FALSE(chunk_extents.empty()); |
| 65 | int chunk_blocks = 0; |
| 66 | for (vector<Extent>::const_iterator it = chunk_extents.begin(); |
| 67 | it != chunk_extents.end(); ++it) { |
| 68 | chunk_blocks += it->num_blocks(); |
| 69 | } |
| 70 | EXPECT_EQ(2, chunk_blocks); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(ExtentMapperTest, RunAsRootSparseFileTest) { |
| 74 | // Create sparse file with one real block, then two sparse ones, then a real |
| 75 | // block at the end. |
| 76 | const char tmp_name_template[] = |
| 77 | "/tmp/ExtentMapperTest.RunAsRootSparseFileTest.XXXXXX"; |
| 78 | char buf[sizeof(tmp_name_template)]; |
| 79 | strncpy(buf, tmp_name_template, sizeof(buf)); |
| 80 | COMPILE_ASSERT(sizeof(buf) > 8, buf_size_incorrect); |
| 81 | ASSERT_EQ('\0', buf[sizeof(buf) - 1]); |
| 82 | |
| 83 | int fd = mkstemp(buf); |
| 84 | ASSERT_GE(fd, 0); |
| 85 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 86 | uint32_t block_size = 0; |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(extent_mapper::GetFilesystemBlockSize(buf, &block_size)); |
| 88 | EXPECT_GT(block_size, 0); |
| 89 | |
| 90 | EXPECT_EQ(1, pwrite(fd, "x", 1, 0)); |
| 91 | EXPECT_EQ(1, pwrite(fd, "x", 1, 3 * block_size)); |
| 92 | close(fd); |
| 93 | |
| 94 | vector<Extent> extents; |
| 95 | EXPECT_TRUE(extent_mapper::ExtentsForFileFibmap(buf, &extents)); |
| 96 | unlink(buf); |
| 97 | EXPECT_EQ(3, extents.size()); |
| 98 | EXPECT_EQ(1, extents[0].num_blocks()); |
| 99 | EXPECT_EQ(2, extents[1].num_blocks()); |
| 100 | EXPECT_EQ(1, extents[2].num_blocks()); |
| 101 | EXPECT_NE(kSparseHole, extents[0].start_block()); |
| 102 | EXPECT_EQ(kSparseHole, extents[1].start_block()); |
| 103 | EXPECT_NE(kSparseHole, extents[2].start_block()); |
| 104 | EXPECT_NE(extents[2].start_block(), extents[0].start_block()); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace chromeos_update_engine |