| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sys/stat.h> | 5 #include <sys/stat.h> |
| 6 #include <sys/types.h> | 6 #include <sys/types.h> |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 class ExtentMapperTest : public ::testing::Test {}; | 23 class ExtentMapperTest : public ::testing::Test {}; |
| 24 | 24 |
| 25 TEST(ExtentMapperTest, RunAsRootSimpleTest) { | 25 TEST(ExtentMapperTest, RunAsRootSimpleTest) { |
| 26 // It's hard to have a concrete test for extent mapping without including | 26 // It's hard to have a concrete test for extent mapping without including |
| 27 // a specific filesystem image. | 27 // a specific filesystem image. |
| 28 // In lieu of this, we do a weak test: make sure the extents of the unittest | 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. | 29 // executable are consistent and they match with the size of the file. |
| 30 const string kFilename = "/proc/self/exe"; | 30 const string kFilename = "/proc/self/exe"; |
| 31 | 31 |
| 32 uint32 block_size = 0; | 32 uint32_t block_size = 0; |
| 33 EXPECT_TRUE(extent_mapper::GetFilesystemBlockSize(kFilename, &block_size)); | 33 EXPECT_TRUE(extent_mapper::GetFilesystemBlockSize(kFilename, &block_size)); |
| 34 EXPECT_GT(block_size, 0); | 34 EXPECT_GT(block_size, 0); |
| 35 | 35 |
| 36 vector<Extent> extents; | 36 vector<Extent> extents; |
| 37 | 37 |
| 38 ASSERT_TRUE(extent_mapper::ExtentsForFileFibmap(kFilename, &extents)); | 38 ASSERT_TRUE(extent_mapper::ExtentsForFileFibmap(kFilename, &extents)); |
| 39 | 39 |
| 40 EXPECT_FALSE(extents.empty()); | 40 EXPECT_FALSE(extents.empty()); |
| 41 set<uint64> blocks; | 41 set<uint64_t> blocks; |
| 42 | 42 |
| 43 for (vector<Extent>::const_iterator it = extents.begin(); | 43 for (vector<Extent>::const_iterator it = extents.begin(); |
| 44 it != extents.end(); ++it) { | 44 it != extents.end(); ++it) { |
| 45 for (uint64 block = it->start_block(); | 45 for (uint64_t block = it->start_block(); |
| 46 block < it->start_block() + it->num_blocks(); | 46 block < it->start_block() + it->num_blocks(); |
| 47 block++) { | 47 block++) { |
| 48 EXPECT_FALSE(utils::SetContainsKey(blocks, block)); | 48 EXPECT_FALSE(utils::SetContainsKey(blocks, block)); |
| 49 blocks.insert(block); | 49 blocks.insert(block); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 struct stat stbuf; | 53 struct stat stbuf; |
| 54 EXPECT_EQ(0, stat(kFilename.c_str(), &stbuf)); | 54 EXPECT_EQ(0, stat(kFilename.c_str(), &stbuf)); |
| 55 EXPECT_EQ(blocks.size(), (stbuf.st_size + block_size - 1)/block_size); | 55 EXPECT_EQ(blocks.size(), (stbuf.st_size + block_size - 1)/block_size); |
| 56 } | 56 } |
| 57 | 57 |
| 58 TEST(ExtentMapperTest, RunAsRootSparseFileTest) { | 58 TEST(ExtentMapperTest, RunAsRootSparseFileTest) { |
| 59 // Create sparse file with one real block, then two sparse ones, then a real | 59 // Create sparse file with one real block, then two sparse ones, then a real |
| 60 // block at the end. | 60 // block at the end. |
| 61 const char tmp_name_template[] = | 61 const char tmp_name_template[] = |
| 62 "/tmp/ExtentMapperTest.RunAsRootSparseFileTest.XXXXXX"; | 62 "/tmp/ExtentMapperTest.RunAsRootSparseFileTest.XXXXXX"; |
| 63 char buf[sizeof(tmp_name_template)]; | 63 char buf[sizeof(tmp_name_template)]; |
| 64 strncpy(buf, tmp_name_template, sizeof(buf)); | 64 strncpy(buf, tmp_name_template, sizeof(buf)); |
| 65 COMPILE_ASSERT(sizeof(buf) > 8, buf_size_incorrect); | 65 COMPILE_ASSERT(sizeof(buf) > 8, buf_size_incorrect); |
| 66 ASSERT_EQ('\0', buf[sizeof(buf) - 1]); | 66 ASSERT_EQ('\0', buf[sizeof(buf) - 1]); |
| 67 | 67 |
| 68 int fd = mkstemp(buf); | 68 int fd = mkstemp(buf); |
| 69 ASSERT_GE(fd, 0); | 69 ASSERT_GE(fd, 0); |
| 70 | 70 |
| 71 uint32 block_size = 0; | 71 uint32_t block_size = 0; |
| 72 EXPECT_TRUE(extent_mapper::GetFilesystemBlockSize(buf, &block_size)); | 72 EXPECT_TRUE(extent_mapper::GetFilesystemBlockSize(buf, &block_size)); |
| 73 EXPECT_GT(block_size, 0); | 73 EXPECT_GT(block_size, 0); |
| 74 | 74 |
| 75 EXPECT_EQ(1, pwrite(fd, "x", 1, 0)); | 75 EXPECT_EQ(1, pwrite(fd, "x", 1, 0)); |
| 76 EXPECT_EQ(1, pwrite(fd, "x", 1, 3 * block_size)); | 76 EXPECT_EQ(1, pwrite(fd, "x", 1, 3 * block_size)); |
| 77 close(fd); | 77 close(fd); |
| 78 | 78 |
| 79 vector<Extent> extents; | 79 vector<Extent> extents; |
| 80 EXPECT_TRUE(extent_mapper::ExtentsForFileFibmap(buf, &extents)); | 80 EXPECT_TRUE(extent_mapper::ExtentsForFileFibmap(buf, &extents)); |
| 81 unlink(buf); | 81 unlink(buf); |
| 82 EXPECT_EQ(3, extents.size()); | 82 EXPECT_EQ(3, extents.size()); |
| 83 EXPECT_EQ(1, extents[0].num_blocks()); | 83 EXPECT_EQ(1, extents[0].num_blocks()); |
| 84 EXPECT_EQ(2, extents[1].num_blocks()); | 84 EXPECT_EQ(2, extents[1].num_blocks()); |
| 85 EXPECT_EQ(1, extents[2].num_blocks()); | 85 EXPECT_EQ(1, extents[2].num_blocks()); |
| 86 EXPECT_NE(kSparseHole, extents[0].start_block()); | 86 EXPECT_NE(kSparseHole, extents[0].start_block()); |
| 87 EXPECT_EQ(kSparseHole, extents[1].start_block()); | 87 EXPECT_EQ(kSparseHole, extents[1].start_block()); |
| 88 EXPECT_NE(kSparseHole, extents[2].start_block()); | 88 EXPECT_NE(kSparseHole, extents[2].start_block()); |
| 89 EXPECT_NE(extents[2].start_block(), extents[0].start_block()); | 89 EXPECT_NE(extents[2].start_block(), extents[0].start_block()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 } // namespace chromeos_update_engine | 92 } // namespace chromeos_update_engine |
| OLD | NEW |