blob: c5995dd7d0bc561b1802f6effa4153e3b4e0fb15 [file] [log] [blame]
xunchang311e6ca2019-03-22 08:54:35 -07001/*
2 * Copyright (C) 2019 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 <stdint.h>
18#include <unistd.h>
19
20#include <functional>
21#include <string>
22#include <vector>
23
24#include <android-base/file.h>
25#include <android-base/logging.h>
26#include <android-base/strings.h>
27#include <android-base/unique_fd.h>
28#include <gtest/gtest.h>
29
30#include "fuse_provider.h"
31#include "fuse_sideload.h"
32#include "install/install.h"
33
34TEST(FuseBlockMapTest, CreateFromBlockMap_smoke) {
35 TemporaryFile fake_block_device;
36 std::vector<std::string> lines = {
37 fake_block_device.path, "10000 4096", "3", "10 11", "20 21", "22 23",
38 };
39
40 TemporaryFile temp_file;
41 android::base::WriteStringToFile(android::base::Join(lines, '\n'), temp_file.path);
42 auto block_map_data = FuseBlockDataProvider::CreateFromBlockMap(temp_file.path, 4096);
43
44 ASSERT_TRUE(block_map_data);
45 ASSERT_EQ(10000, block_map_data->file_size());
46 ASSERT_EQ(4096, block_map_data->fuse_block_size());
47 ASSERT_EQ(RangeSet({ { 10, 11 }, { 20, 21 }, { 22, 23 } }), block_map_data->ranges());
48}
49
50TEST(FuseBlockMapTest, ReadBlockAlignedData_smoke) {
51 std::string content;
52 content.reserve(40960);
53 for (char c = 0; c < 10; c++) {
54 content += std::string(4096, c);
55 }
56 TemporaryFile fake_block_device;
57 ASSERT_TRUE(android::base::WriteStringToFile(content, fake_block_device.path));
58
59 std::vector<std::string> lines = {
60 fake_block_device.path,
61 "20000 4096",
62 "1",
63 "0 5",
64 };
65 TemporaryFile temp_file;
66 android::base::WriteStringToFile(android::base::Join(lines, '\n'), temp_file.path);
67 auto block_map_data = FuseBlockDataProvider::CreateFromBlockMap(temp_file.path, 4096);
68
69 std::vector<uint8_t> result(2000);
70 ASSERT_TRUE(block_map_data->ReadBlockAlignedData(result.data(), 2000, 1));
71 ASSERT_EQ(std::vector<uint8_t>(content.begin() + 4096, content.begin() + 6096), result);
72
73 result.resize(20000);
74 ASSERT_TRUE(block_map_data->ReadBlockAlignedData(result.data(), 20000, 0));
75 ASSERT_EQ(std::vector<uint8_t>(content.begin(), content.begin() + 20000), result);
76}
77
78TEST(FuseBlockMapTest, ReadBlockAlignedData_large_fuse_block) {
79 std::string content;
80 for (char c = 0; c < 10; c++) {
81 content += std::string(4096, c);
82 }
83
84 TemporaryFile temp_file;
85 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
86
87 std::vector<std::string> lines = {
88 temp_file.path, "36384 4096", "2", "0 5", "6 10",
89 };
90 TemporaryFile block_map;
91 ASSERT_TRUE(android::base::WriteStringToFile(android::base::Join(lines, '\n'), block_map.path));
92
93 auto block_map_data = FuseBlockDataProvider::CreateFromBlockMap(block_map.path, 16384);
94 ASSERT_TRUE(block_map_data);
95
96 std::vector<uint8_t> result(20000);
97 // Out of bound read
98 ASSERT_FALSE(block_map_data->ReadBlockAlignedData(result.data(), 20000, 2));
99 ASSERT_TRUE(block_map_data->ReadBlockAlignedData(result.data(), 20000, 1));
100 // expected source block contains: 4, 6-9
101 std::string expected = content.substr(16384, 4096) + content.substr(24576, 15904);
102 ASSERT_EQ(std::vector<uint8_t>(expected.begin(), expected.end()), result);
103}