blob: 19fa4c59e8505c3f8746c251b1a7ac3e7cbeb45a [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>
20#include <android-base/test_utils.h>
Tao Bao17054c02018-05-03 22:41:23 -070021#include <gtest/gtest.h>
Tao Baoc3292f32016-11-04 10:52:13 -070022
Tao Bao17054c02018-05-03 22:41:23 -070023#include "otautil/sysutil.h"
Tao Baoc3292f32016-11-04 10:52:13 -070024
25TEST(SysUtilTest, InvalidArgs) {
26 MemMapping mapping;
27
28 // Invalid argument.
Tao Baob656a152017-04-18 23:54:29 -070029 ASSERT_FALSE(mapping.MapFile(""));
Tao Baoc3292f32016-11-04 10:52:13 -070030}
31
Tao Baob656a152017-04-18 23:54:29 -070032TEST(SysUtilTest, MapFileRegularFile) {
Tao Baoc3292f32016-11-04 10:52:13 -070033 TemporaryFile temp_file1;
34 std::string content = "abc";
35 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path));
36
Tao Baob656a152017-04-18 23:54:29 -070037 // MemMapping::MapFile() should map the file to one range.
Tao Baoc3292f32016-11-04 10:52:13 -070038 MemMapping mapping;
Tao Baob656a152017-04-18 23:54:29 -070039 ASSERT_TRUE(mapping.MapFile(temp_file1.path));
Tao Baoc3292f32016-11-04 10:52:13 -070040 ASSERT_NE(nullptr, mapping.addr);
41 ASSERT_EQ(content.size(), mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070042 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070043}
44
Tao Baob656a152017-04-18 23:54:29 -070045TEST(SysUtilTest, MapFileBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -070046 // 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 Baob656a152017-04-18 23:54:29 -070061 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070062 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070063 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070064
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 Baob656a152017-04-18 23:54:29 -070069 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070070 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070071 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070072
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 Baob656a152017-04-18 23:54:29 -070077 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070078 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070079 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070080
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 Baob656a152017-04-18 23:54:29 -070085 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070086 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070087 ASSERT_EQ(3U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070088}
89
Tao Baob656a152017-04-18 23:54:29 -070090TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -070091 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 Baob656a152017-04-18 23:54:29 -070097 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070098
99 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700100 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700101
102 // Block map file has unexpected number of lines.
103 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700104 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700105
106 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700107 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700108
109 // Invalid size/blksize/range_count.
110 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700111 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700112
113 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700114 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700115
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 Baob656a152017-04-18 23:54:29 -0700118 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700119
120 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700121 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700122
123 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700124 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700125
126 // Invalid block dev path.
127 ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700128 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700129}