1 //
2 // Copyright (C) 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/payload_generator/mapfile_filesystem.h"
18
19 #include <unistd.h>
20
21 #include <map>
22 #include <string>
23 #include <vector>
24
25 #include <base/format_macros.h>
26 #include <base/logging.h>
27 #include <base/strings/string_number_conversions.h>
28 #include <android-base/stringprintf.h>
29 #include <gtest/gtest.h>
30
31 #include "update_engine/common/test_utils.h"
32 #include "update_engine/common/utils.h"
33 #include "update_engine/payload_generator/extent_ranges.h"
34 #include "update_engine/payload_generator/extent_utils.h"
35
36 using std::map;
37 using std::string;
38 using std::unique_ptr;
39 using std::vector;
40
41 namespace chromeos_update_engine {
42
43 namespace {
44
45 // Checks that all the blocks in |extents| are in the range [0, total_blocks).
ExpectBlocksInRange(const vector<Extent> & extents,uint64_t total_blocks)46 void ExpectBlocksInRange(const vector<Extent>& extents, uint64_t total_blocks) {
47 for (const Extent& extent : extents) {
48 EXPECT_LE(0U, extent.start_block());
49 EXPECT_LE(extent.start_block() + extent.num_blocks(), total_blocks);
50 }
51 }
52
53 } // namespace
54
55 class MapfileFilesystemTest : public ::testing::Test {
56 protected:
57 ScopedTempFile temp_file_{"mapfile_file.XXXXXX"};
58 ScopedTempFile temp_mapfile_{"mapfile_mapfile.XXXXXX"};
59 };
60
TEST_F(MapfileFilesystemTest,EmptyFilesystem)61 TEST_F(MapfileFilesystemTest, EmptyFilesystem) {
62 unique_ptr<MapfileFilesystem> fs = MapfileFilesystem::CreateFromFile(
63 temp_file_.path(), temp_mapfile_.path());
64 ASSERT_NE(nullptr, fs.get());
65
66 EXPECT_EQ(0U, fs->GetBlockCount());
67 // .map files are always 4KiB blocks.
68 EXPECT_EQ(4096U, fs->GetBlockSize());
69 }
70
TEST_F(MapfileFilesystemTest,SeveralFileFormatTest)71 TEST_F(MapfileFilesystemTest, SeveralFileFormatTest) {
72 string text =
73 "/fileA 1\n"
74 "/fileB 2-4\n"
75 "/fileC 5-6 9 11-12\n"
76 "/file with spaces 14 19\n"
77 "/1234 7\n";
78 test_utils::WriteFileString(temp_mapfile_.path(), text);
79 EXPECT_EQ(0, HANDLE_EINTR(truncate(temp_file_.path().c_str(), 4096 * 20)));
80
81 unique_ptr<MapfileFilesystem> fs = MapfileFilesystem::CreateFromFile(
82 temp_file_.path(), temp_mapfile_.path());
83 ASSERT_NE(nullptr, fs.get());
84
85 vector<FilesystemInterface::File> files;
86 EXPECT_TRUE(fs->GetFiles(&files));
87
88 map<string, FilesystemInterface::File> map_files;
89 for (const auto& file : files) {
90 EXPECT_EQ(map_files.end(), map_files.find(file.name))
91 << "File " << file.name << " repeated in the list.";
92 map_files[file.name] = file;
93 ExpectBlocksInRange(file.extents, fs->GetBlockCount());
94 }
95
96 EXPECT_EQ(map_files["/fileA"].extents,
97 (vector<Extent>{ExtentForRange(1, 1)}));
98 EXPECT_EQ(map_files["/fileB"].extents,
99 (vector<Extent>{ExtentForRange(2, 3)}));
100 EXPECT_EQ(
101 map_files["/fileC"].extents,
102 (vector<Extent>{
103 ExtentForRange(5, 2), ExtentForRange(9, 1), ExtentForRange(11, 2)}));
104 EXPECT_EQ(map_files["/file with spaces"].extents,
105 (vector<Extent>{ExtentForRange(14, 1), ExtentForRange(19, 1)}));
106 EXPECT_EQ(map_files["/1234"].extents, (vector<Extent>{ExtentForRange(7, 1)}));
107 }
108
TEST_F(MapfileFilesystemTest,BlockNumberTooBigTest)109 TEST_F(MapfileFilesystemTest, BlockNumberTooBigTest) {
110 test_utils::WriteFileString(temp_mapfile_.path(), "/some/file 1-4\n");
111 EXPECT_EQ(0, HANDLE_EINTR(truncate(temp_file_.path().c_str(), 4096 * 3)));
112
113 unique_ptr<MapfileFilesystem> fs = MapfileFilesystem::CreateFromFile(
114 temp_file_.path(), temp_mapfile_.path());
115 ASSERT_NE(nullptr, fs.get());
116
117 vector<FilesystemInterface::File> files;
118 EXPECT_FALSE(fs->GetFiles(&files));
119 }
120
TEST_F(MapfileFilesystemTest,EndBeforeStartTest)121 TEST_F(MapfileFilesystemTest, EndBeforeStartTest) {
122 test_utils::WriteFileString(temp_mapfile_.path(), "/some/file 2-1\n");
123 EXPECT_EQ(0, HANDLE_EINTR(truncate(temp_file_.path().c_str(), 4096 * 3)));
124
125 unique_ptr<MapfileFilesystem> fs = MapfileFilesystem::CreateFromFile(
126 temp_file_.path(), temp_mapfile_.path());
127 ASSERT_NE(nullptr, fs.get());
128
129 vector<FilesystemInterface::File> files;
130 EXPECT_FALSE(fs->GetFiles(&files));
131 }
132
133 } // namespace chromeos_update_engine
134