blob: 77625dbe99576cb67dbad9b6869f9b6e34f8bb7c [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>
Tao Bao17054c02018-05-03 22:41:23 -070020#include <gtest/gtest.h>
Tao Baoc3292f32016-11-04 10:52:13 -070021
Tao Bao17054c02018-05-03 22:41:23 -070022#include "otautil/sysutil.h"
Tao Baoc3292f32016-11-04 10:52:13 -070023
24TEST(SysUtilTest, InvalidArgs) {
25 MemMapping mapping;
26
27 // Invalid argument.
Tao Baob656a152017-04-18 23:54:29 -070028 ASSERT_FALSE(mapping.MapFile(""));
Tao Baoc3292f32016-11-04 10:52:13 -070029}
30
Tao Baob656a152017-04-18 23:54:29 -070031TEST(SysUtilTest, MapFileRegularFile) {
Tao Baoc3292f32016-11-04 10:52:13 -070032 TemporaryFile temp_file1;
33 std::string content = "abc";
34 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path));
35
Tao Baob656a152017-04-18 23:54:29 -070036 // MemMapping::MapFile() should map the file to one range.
Tao Baoc3292f32016-11-04 10:52:13 -070037 MemMapping mapping;
Tao Baob656a152017-04-18 23:54:29 -070038 ASSERT_TRUE(mapping.MapFile(temp_file1.path));
Tao Baoc3292f32016-11-04 10:52:13 -070039 ASSERT_NE(nullptr, mapping.addr);
40 ASSERT_EQ(content.size(), mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070041 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070042}
43
Tao Baob656a152017-04-18 23:54:29 -070044TEST(SysUtilTest, MapFileBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -070045 // 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 Baob656a152017-04-18 23:54:29 -070060 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070061 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070062 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070063
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 Baob656a152017-04-18 23:54:29 -070068 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070069 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070070 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070071
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 Baob656a152017-04-18 23:54:29 -070076 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070077 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070078 ASSERT_EQ(1U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070079
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 Baob656a152017-04-18 23:54:29 -070084 ASSERT_TRUE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070085 ASSERT_EQ(file_size, mapping.length);
Tao Baob656a152017-04-18 23:54:29 -070086 ASSERT_EQ(3U, mapping.ranges());
Tao Baoc3292f32016-11-04 10:52:13 -070087}
88
Tao Baob656a152017-04-18 23:54:29 -070089TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) {
Tao Baoc3292f32016-11-04 10:52:13 -070090 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 Baob656a152017-04-18 23:54:29 -070096 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -070097
98 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -070099 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700100
101 // Block map file has unexpected number of lines.
102 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700103 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700104
105 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700106 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700107
108 // Invalid size/blksize/range_count.
109 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700110 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700111
112 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700113 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700114
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 Baob656a152017-04-18 23:54:29 -0700117 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700118
119 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700120 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700121
122 ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700123 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700124
125 // Invalid block dev path.
126 ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path));
Tao Baob656a152017-04-18 23:54:29 -0700127 ASSERT_FALSE(mapping.MapFile(filename));
Tao Baoc3292f32016-11-04 10:52:13 -0700128}
Tao Bao1700cc42018-07-16 22:09:59 -0700129
130TEST(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}