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 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include <android-base/file.h> |
| 22 | #include <android-base/test_utils.h> |
| 23 | |
| 24 | #include "otautil/SysUtil.h" |
| 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 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 33 | TEST(SysUtilTest, MapFileRegularFile) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 34 | TemporaryFile temp_file1; |
| 35 | std::string content = "abc"; |
| 36 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path)); |
| 37 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 38 | // MemMapping::MapFile() should map the file to one range. |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 39 | MemMapping mapping; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 40 | ASSERT_TRUE(mapping.MapFile(temp_file1.path)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 41 | ASSERT_NE(nullptr, mapping.addr); |
| 42 | ASSERT_EQ(content.size(), mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 43 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 46 | TEST(SysUtilTest, MapFileBlockMap) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 47 | // Create a file that has 10 blocks. |
| 48 | TemporaryFile package; |
| 49 | std::string content; |
| 50 | constexpr size_t file_size = 4096 * 10; |
| 51 | content.reserve(file_size); |
| 52 | ASSERT_TRUE(android::base::WriteStringToFile(content, package.path)); |
| 53 | |
| 54 | TemporaryFile block_map_file; |
| 55 | std::string filename = std::string("@") + block_map_file.path; |
| 56 | MemMapping mapping; |
| 57 | |
| 58 | // One range. |
| 59 | std::string block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n"; |
| 60 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 61 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 62 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 63 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 64 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 65 | |
| 66 | // It's okay to not have the trailing '\n'. |
| 67 | block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10"; |
| 68 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 69 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 70 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 71 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 72 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 73 | |
| 74 | // Or having multiple trailing '\n's. |
| 75 | block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n\n\n"; |
| 76 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 77 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 79 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 80 | ASSERT_EQ(1U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 81 | |
| 82 | // Multiple ranges. |
| 83 | block_map_content = std::string(package.path) + "\n40960 4096\n3\n0 3\n3 5\n5 10\n"; |
| 84 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 85 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 86 | ASSERT_TRUE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 87 | ASSERT_EQ(file_size, mapping.length); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 88 | ASSERT_EQ(3U, mapping.ranges()); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 91 | TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 92 | MemMapping mapping; |
| 93 | TemporaryFile temp_file; |
| 94 | std::string filename = std::string("@") + temp_file.path; |
| 95 | |
| 96 | // Block map file is too short. |
| 97 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n", temp_file.path)); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 98 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 99 | |
| 100 | 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] | 101 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 102 | |
| 103 | // Block map file has unexpected number of lines. |
| 104 | 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] | 105 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 106 | |
| 107 | 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] | 108 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 109 | |
| 110 | // Invalid size/blksize/range_count. |
| 111 | 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] | 112 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 113 | |
| 114 | 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] | 115 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 116 | |
| 117 | // size/blksize/range_count don't match. |
| 118 | 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] | 119 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 120 | |
| 121 | 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] | 122 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 123 | |
| 124 | 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] | 125 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 126 | |
| 127 | // Invalid block dev path. |
| 128 | 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] | 129 | ASSERT_FALSE(mapping.MapFile(filename)); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 130 | } |