blob: 434ee25bfdfae8d1f17cafd98cc9a9d6ff5b0721 [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
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
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
Tao Baob656a152017-04-18 23:54:29 -070033TEST(SysUtilTest, MapFileRegularFile) {
Tao Baoc3292f32016-11-04 10:52:13 -070034 TemporaryFile temp_file1;
35 std::string content = "abc";
36 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path));
37
Tao Baob656a152017-04-18 23:54:29 -070038 // MemMapping::MapFile() should map the file to one range.
Tao Baoc3292f32016-11-04 10:52:13 -070039 MemMapping mapping;
Tao Baob656a152017-04-18 23:54:29 -070040 ASSERT_TRUE(mapping.MapFile(temp_file1.path));
Tao Baoc3292f32016-11-04 10:52:13 -070041 ASSERT_NE(nullptr, mapping.addr);
42 ASSERT_EQ(content.size(), mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070043 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070044}
45
Tao Baob656a152017-04-18 23:54:29 -070046TEST(SysUtilTest, MapFileBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -070047 // 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 Baob656a152017-04-18 23:54:29 -070062 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070063 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070064 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070065
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 Baob656a152017-04-18 23:54:29 -070070 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070071 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070072 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070073
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 Baob656a152017-04-18 23:54:29 -070078 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070079 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070080 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070081
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 Baob656a152017-04-18 23:54:29 -070086 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070087 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070088 ASSERT_EQ(3U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070089}
90
Tao Baob656a152017-04-18 23:54:29 -070091TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -070092 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 Baob656a152017-04-18 23:54:29 -070098 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070099
100 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700101 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700102
103 // Block map file has unexpected number of lines.
104 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700105 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700106
107 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700108 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700109
110 // Invalid size/blksize/range_count.
111 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700112 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700113
114 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700115 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700116
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 Baob656a152017-04-18 23:54:29 -0700119 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700120
121 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700122 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700123
124 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700125 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700126
127 // Invalid block dev path.
128 ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700129 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700130}