Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | |
| 19 | #include <android-base/file.h> |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 20 | #include <android-base/strings.h> |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 22 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 23 | #include "otautil/rangeset.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 24 | #include "otautil/sysutil.h" |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 25 | |
| 26 | TEST(SysUtilTest, InvalidArgs) { |
| 27 | MemMapping mapping; |
| 28 | |
| 29 | // Invalid argument. |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 30 | ASSERT_FALSE(mapping.MapFile("")); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 31 | } |
| 32 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 33 | TEST(SysUtilTest, ParseBlockMapFile_smoke) { |
| 34 | std::vector<std::string> content = { |
| 35 | "/dev/abc", "49652 4096", "3", "1000 1008", "2100 2102", "30 33", |
| 36 | }; |
| 37 | |
| 38 | TemporaryFile temp_file; |
| 39 | ASSERT_TRUE(android::base::WriteStringToFile(android::base::Join(content, '\n'), temp_file.path)); |
| 40 | |
| 41 | auto block_map_data = BlockMapData::ParseBlockMapFile(temp_file.path); |
| 42 | ASSERT_EQ("/dev/abc", block_map_data.path()); |
| 43 | ASSERT_EQ(49652, block_map_data.file_size()); |
| 44 | ASSERT_EQ(4096, block_map_data.block_size()); |
| 45 | ASSERT_EQ(RangeSet(std::vector<Range>{ |
| 46 | { 1000, 1008 }, |
| 47 | { 2100, 2102 }, |
| 48 | { 30, 33 }, |
| 49 | }), |
| 50 | block_map_data.block_ranges()); |
| 51 | } |
| 52 | |
| 53 | TEST(SysUtilTest, ParseBlockMapFile_invalid_line_count) { |
| 54 | std::vector<std::string> content = { |
| 55 | "/dev/abc", "49652 4096", "2", "1000 1008", "2100 2102", "30 33", |
| 56 | }; |
| 57 | |
| 58 | TemporaryFile temp_file; |
| 59 | ASSERT_TRUE(android::base::WriteStringToFile(android::base::Join(content, '\n'), temp_file.path)); |
| 60 | |
| 61 | auto block_map_data1 = BlockMapData::ParseBlockMapFile(temp_file.path); |
| 62 | ASSERT_FALSE(block_map_data1); |
| 63 | } |
| 64 | |
| 65 | TEST(SysUtilTest, ParseBlockMapFile_invalid_size) { |
| 66 | std::vector<std::string> content = { |
| 67 | "/dev/abc", |
| 68 | "42949672950 4294967295", |
| 69 | "1", |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 70 | "0 10", |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | TemporaryFile temp_file; |
| 74 | ASSERT_TRUE(android::base::WriteStringToFile(android::base::Join(content, '\n'), temp_file.path)); |
| 75 | |
| 76 | auto block_map_data = BlockMapData::ParseBlockMapFile(temp_file.path); |
| 77 | ASSERT_EQ("/dev/abc", block_map_data.path()); |
| 78 | ASSERT_EQ(42949672950, block_map_data.file_size()); |
| 79 | ASSERT_EQ(4294967295, block_map_data.block_size()); |
| 80 | |
| 81 | content[1] = "42949672950 4294967296"; |
| 82 | ASSERT_TRUE(android::base::WriteStringToFile(android::base::Join(content, '\n'), temp_file.path)); |
| 83 | auto large_block_size = BlockMapData::ParseBlockMapFile(temp_file.path); |
| 84 | ASSERT_FALSE(large_block_size); |
| 85 | |
| 86 | content[1] = "4294967296 1"; |
| 87 | ASSERT_TRUE(android::base::WriteStringToFile(android::base::Join(content, '\n'), temp_file.path)); |
| 88 | auto too_many_blocks = BlockMapData::ParseBlockMapFile(temp_file.path); |
| 89 | ASSERT_FALSE(too_many_blocks); |
| 90 | } |
| 91 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 92 | TEST(SysUtilTest, MapFileRegularFile) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 93 | TemporaryFile temp_file1; |
| 94 | std::string content = "abc"; |
| 95 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path)); |
| 96 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 97 | // MemMapping::MapFile() should map the file to one range. |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 98 | MemMapping mapping; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 99 | ASSERT_TRUE(mapping.MapFile(temp_file1.path)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 100 | ASSERT_NE(nullptr, mapping.addr); |
| 101 | ASSERT_EQ(content.size(), mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 102 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 105 | TEST(SysUtilTest, MapFileBlockMap) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 106 | // Create a file that has 10 blocks. |
| 107 | TemporaryFile package; |
| 108 | std::string content; |
| 109 | constexpr size_t file_size = 4096 * 10; |
| 110 | content.reserve(file_size); |
| 111 | ASSERT_TRUE(android::base::WriteStringToFile(content, package.path)); |
| 112 | |
| 113 | TemporaryFile block_map_file; |
| 114 | std::string filename = std::string("@") + block_map_file.path; |
| 115 | MemMapping mapping; |
| 116 | |
| 117 | // One range. |
| 118 | std::string block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n"; |
| 119 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 120 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 122 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 123 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 124 | |
| 125 | // It's okay to not have the trailing '\n'. |
| 126 | block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10"; |
| 127 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 128 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 129 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 130 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 131 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 132 | |
| 133 | // Or having multiple trailing '\n's. |
| 134 | block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n\n\n"; |
| 135 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 136 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 137 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 138 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 139 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 140 | |
| 141 | // Multiple ranges. |
| 142 | block_map_content = std::string(package.path) + "\n40960 4096\n3\n0 3\n3 5\n5 10\n"; |
| 143 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 144 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 145 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 146 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 147 | ASSERT_EQ(3U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 150 | TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 151 | MemMapping mapping; |
| 152 | TemporaryFile temp_file; |
| 153 | std::string filename = std::string("@") + temp_file.path; |
| 154 | |
| 155 | // Block map file is too short. |
| 156 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 157 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 158 | |
| 159 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 160 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 161 | |
| 162 | // Block map file has unexpected number of lines. |
| 163 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 164 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 165 | |
| 166 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 167 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 168 | |
| 169 | // Invalid size/blksize/range_count. |
| 170 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 171 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 172 | |
| 173 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 174 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 175 | |
| 176 | // size/blksize/range_count don't match. |
| 177 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n0 4096\n1\n0 1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 178 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 179 | |
| 180 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 181 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 182 | |
| 183 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 184 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 185 | |
| 186 | // Invalid block dev path. |
| 187 | ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 188 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 189 | } |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 190 | |
| 191 | TEST(SysUtilTest, StringVectorToNullTerminatedArray) { |
| 192 | std::vector<std::string> args{ "foo", "bar", "baz" }; |
| 193 | auto args_with_nullptr = StringVectorToNullTerminatedArray(args); |
| 194 | ASSERT_EQ(4, args_with_nullptr.size()); |
| 195 | ASSERT_STREQ("foo", args_with_nullptr[0]); |
| 196 | ASSERT_STREQ("bar", args_with_nullptr[1]); |
| 197 | ASSERT_STREQ("baz", args_with_nullptr[2]); |
| 198 | ASSERT_EQ(nullptr, args_with_nullptr[3]); |
| 199 | } |