Tao Bao | 9c05a82 | 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. |
| 30 | ASSERT_EQ(-1, sysMapFile(nullptr, &mapping)); |
| 31 | ASSERT_EQ(-1, sysMapFile("/somefile", nullptr)); |
| 32 | } |
| 33 | |
| 34 | TEST(SysUtilTest, sysMapFileRegularFile) { |
| 35 | TemporaryFile temp_file1; |
| 36 | std::string content = "abc"; |
| 37 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path)); |
| 38 | |
| 39 | // sysMapFile() should map the file to one range. |
| 40 | MemMapping mapping; |
| 41 | ASSERT_EQ(0, sysMapFile(temp_file1.path, &mapping)); |
| 42 | ASSERT_NE(nullptr, mapping.addr); |
| 43 | ASSERT_EQ(content.size(), mapping.length); |
| 44 | ASSERT_EQ(1U, mapping.ranges.size()); |
| 45 | |
| 46 | sysReleaseMap(&mapping); |
| 47 | ASSERT_EQ(0U, mapping.ranges.size()); |
| 48 | } |
| 49 | |
| 50 | TEST(SysUtilTest, sysMapFileBlockMap) { |
| 51 | // Create a file that has 10 blocks. |
| 52 | TemporaryFile package; |
| 53 | std::string content; |
| 54 | constexpr size_t file_size = 4096 * 10; |
| 55 | content.reserve(file_size); |
| 56 | ASSERT_TRUE(android::base::WriteStringToFile(content, package.path)); |
| 57 | |
| 58 | TemporaryFile block_map_file; |
| 59 | std::string filename = std::string("@") + block_map_file.path; |
| 60 | MemMapping mapping; |
| 61 | |
| 62 | // One range. |
| 63 | std::string block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n"; |
| 64 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 65 | |
| 66 | ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); |
| 67 | ASSERT_EQ(file_size, mapping.length); |
| 68 | ASSERT_EQ(1U, mapping.ranges.size()); |
| 69 | |
| 70 | // It's okay to not have the trailing '\n'. |
| 71 | block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10"; |
| 72 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 73 | |
| 74 | ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); |
| 75 | ASSERT_EQ(file_size, mapping.length); |
| 76 | ASSERT_EQ(1U, mapping.ranges.size()); |
| 77 | |
| 78 | // Or having multiple trailing '\n's. |
| 79 | block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n\n\n"; |
| 80 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 81 | |
| 82 | ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); |
| 83 | ASSERT_EQ(file_size, mapping.length); |
| 84 | ASSERT_EQ(1U, mapping.ranges.size()); |
| 85 | |
| 86 | // Multiple ranges. |
| 87 | block_map_content = std::string(package.path) + "\n40960 4096\n3\n0 3\n3 5\n5 10\n"; |
| 88 | ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); |
| 89 | |
| 90 | ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); |
| 91 | ASSERT_EQ(file_size, mapping.length); |
| 92 | ASSERT_EQ(3U, mapping.ranges.size()); |
| 93 | |
| 94 | sysReleaseMap(&mapping); |
| 95 | ASSERT_EQ(0U, mapping.ranges.size()); |
| 96 | } |
| 97 | |
| 98 | TEST(SysUtilTest, sysMapFileBlockMapInvalidBlockMap) { |
| 99 | MemMapping mapping; |
| 100 | TemporaryFile temp_file; |
| 101 | std::string filename = std::string("@") + temp_file.path; |
| 102 | |
| 103 | // Block map file is too short. |
| 104 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n", temp_file.path)); |
| 105 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 106 | |
| 107 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path)); |
| 108 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 109 | |
| 110 | // Block map file has unexpected number of lines. |
| 111 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path)); |
| 112 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 113 | |
| 114 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path)); |
| 115 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 116 | |
| 117 | // Invalid size/blksize/range_count. |
| 118 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path)); |
| 119 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 120 | |
| 121 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path)); |
| 122 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 123 | |
| 124 | // size/blksize/range_count don't match. |
| 125 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n0 4096\n1\n0 1\n", temp_file.path)); |
| 126 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 127 | |
| 128 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path)); |
| 129 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 130 | |
| 131 | ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path)); |
| 132 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 133 | |
| 134 | // Invalid block dev path. |
| 135 | ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path)); |
| 136 | ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); |
| 137 | |
| 138 | sysReleaseMap(&mapping); |
| 139 | ASSERT_EQ(0U, mapping.ranges.size()); |
| 140 | } |