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