• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
TEST(SysUtilTest,InvalidArgs)26 TEST(SysUtilTest, InvalidArgs) {
27   MemMapping mapping;
28 
29   // Invalid argument.
30   ASSERT_FALSE(mapping.MapFile(""));
31 }
32 
TEST(SysUtilTest,MapFileRegularFile)33 TEST(SysUtilTest, MapFileRegularFile) {
34   TemporaryFile temp_file1;
35   std::string content = "abc";
36   ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path));
37 
38   // MemMapping::MapFile() should map the file to one range.
39   MemMapping mapping;
40   ASSERT_TRUE(mapping.MapFile(temp_file1.path));
41   ASSERT_NE(nullptr, mapping.addr);
42   ASSERT_EQ(content.size(), mapping.length);
43   ASSERT_EQ(1U, mapping.ranges());
44 }
45 
TEST(SysUtilTest,MapFileBlockMap)46 TEST(SysUtilTest, MapFileBlockMap) {
47   // 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 
62   ASSERT_TRUE(mapping.MapFile(filename));
63   ASSERT_EQ(file_size, mapping.length);
64   ASSERT_EQ(1U, mapping.ranges());
65 
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 
70   ASSERT_TRUE(mapping.MapFile(filename));
71   ASSERT_EQ(file_size, mapping.length);
72   ASSERT_EQ(1U, mapping.ranges());
73 
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 
78   ASSERT_TRUE(mapping.MapFile(filename));
79   ASSERT_EQ(file_size, mapping.length);
80   ASSERT_EQ(1U, mapping.ranges());
81 
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 
86   ASSERT_TRUE(mapping.MapFile(filename));
87   ASSERT_EQ(file_size, mapping.length);
88   ASSERT_EQ(3U, mapping.ranges());
89 }
90 
TEST(SysUtilTest,MapFileBlockMapInvalidBlockMap)91 TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) {
92   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));
98   ASSERT_FALSE(mapping.MapFile(filename));
99 
100   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path));
101   ASSERT_FALSE(mapping.MapFile(filename));
102 
103   // Block map file has unexpected number of lines.
104   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path));
105   ASSERT_FALSE(mapping.MapFile(filename));
106 
107   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path));
108   ASSERT_FALSE(mapping.MapFile(filename));
109 
110   // Invalid size/blksize/range_count.
111   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path));
112   ASSERT_FALSE(mapping.MapFile(filename));
113 
114   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path));
115   ASSERT_FALSE(mapping.MapFile(filename));
116 
117   // size/blksize/range_count don't match.
118   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n0 4096\n1\n0 1\n", temp_file.path));
119   ASSERT_FALSE(mapping.MapFile(filename));
120 
121   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path));
122   ASSERT_FALSE(mapping.MapFile(filename));
123 
124   ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path));
125   ASSERT_FALSE(mapping.MapFile(filename));
126 
127   // Invalid block dev path.
128   ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path));
129   ASSERT_FALSE(mapping.MapFile(filename));
130 }
131