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