blob: 64b8956f729a0edba9128784d286b8c670e3743c [file] [log] [blame]
Tao Baoc3292f32016-11-04 10:52:13 -07001/*
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 Baoc3292f32016-11-04 10:52:13 -070017#include <string>
18
19#include <android-base/file.h>
xunchang625c5882019-03-22 09:33:49 -070020#include <android-base/strings.h>
Tao Bao17054c02018-05-03 22:41:23 -070021#include <gtest/gtest.h>
Tao Baoc3292f32016-11-04 10:52:13 -070022
xunchang625c5882019-03-22 09:33:49 -070023#include "otautil/rangeset.h"
Tao Bao17054c02018-05-03 22:41:23 -070024#include "otautil/sysutil.h"
Tao Baoc3292f32016-11-04 10:52:13 -070025
26TEST(SysUtilTest, InvalidArgs) {
27 MemMapping mapping;
28
29 // Invalid argument.
Tao Baob656a152017-04-18 23:54:29 -070030 ASSERT_FALSE(mapping.MapFile(""));
Tao Baoc3292f32016-11-04 10:52:13 -070031}
32
xunchang625c5882019-03-22 09:33:49 -070033TEST(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
53TEST(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
65TEST(SysUtilTest, ParseBlockMapFile_invalid_size) {
66 std::vector<std::string> content = {
67 "/dev/abc",
68 "42949672950 4294967295",
69 "1",
xunchang311e6ca2019-03-22 08:54:35 -070070 "0 10",
xunchang625c5882019-03-22 09:33:49 -070071 };
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 Baob656a152017-04-18 23:54:29 -070092TEST(SysUtilTest, MapFileRegularFile) {
Tao Baoc3292f32016-11-04 10:52:13 -070093 TemporaryFile temp_file1;
94 std::string content = "abc";
95 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path));
96
Tao Baob656a152017-04-18 23:54:29 -070097 // MemMapping::MapFile() should map the file to one range.
Tao Baoc3292f32016-11-04 10:52:13 -070098 MemMapping mapping;
Tao Baob656a152017-04-18 23:54:29 -070099 ASSERT_TRUE(mapping.MapFile(temp_file1.path));
Tao Baoc3292f32016-11-04 10:52:13 -0700100 ASSERT_NE(nullptr, mapping.addr);
101 ASSERT_EQ(content.size(), mapping.length);
Tao Baob656a152017-04-18 23:54:29 -0700102 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -0700103}
104
Tao Baob656a152017-04-18 23:54:29 -0700105TEST(SysUtilTest, MapFileBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -0700106 // 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 Baob656a152017-04-18 23:54:29 -0700121 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700122 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -0700123 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -0700124
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 Baob656a152017-04-18 23:54:29 -0700129 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700130 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -0700131 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -0700132
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 Baob656a152017-04-18 23:54:29 -0700137 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700138 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -0700139 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -0700140
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 Baob656a152017-04-18 23:54:29 -0700145 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700146 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -0700147 ASSERT_EQ(3U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -0700148}
149
Tao Baob656a152017-04-18 23:54:29 -0700150TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -0700151 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 Baob656a152017-04-18 23:54:29 -0700157 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700158
159 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700160 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700161
162 // Block map file has unexpected number of lines.
163 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700164 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700165
166 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700167 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700168
169 // Invalid size/blksize/range_count.
170 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700171 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700172
173 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700174 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700175
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 Baob656a152017-04-18 23:54:29 -0700178 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700179
180 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700181 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700182
183 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700184 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700185
186 // Invalid block dev path.
187 ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700188 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700189}
Tao Bao1700cc42018-07-16 22:09:59 -0700190
191TEST(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}