blob: 37f99f92e4c5212a975a6104976344fdd5c84642 [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());
Tianjie Xuf6158eb2019-06-11 16:09:07 -070047 ASSERT_EQ(RangeSet({ { 10, 11 }, { 20, 21 }, { 22, 23 } }),
48 static_cast<FuseBlockDataProvider*>(block_map_data.get())->ranges());
xunchang311e6ca2019-03-22 08:54:35 -070049}
50
51TEST(FuseBlockMapTest, ReadBlockAlignedData_smoke) {
52 std::string content;
53 content.reserve(40960);
54 for (char c = 0; c < 10; c++) {
55 content += std::string(4096, c);
56 }
57 TemporaryFile fake_block_device;
58 ASSERT_TRUE(android::base::WriteStringToFile(content, fake_block_device.path));
59
60 std::vector<std::string> lines = {
61 fake_block_device.path,
62 "20000 4096",
63 "1",
64 "0 5",
65 };
66 TemporaryFile temp_file;
67 android::base::WriteStringToFile(android::base::Join(lines, '\n'), temp_file.path);
68 auto block_map_data = FuseBlockDataProvider::CreateFromBlockMap(temp_file.path, 4096);
69
70 std::vector<uint8_t> result(2000);
71 ASSERT_TRUE(block_map_data->ReadBlockAlignedData(result.data(), 2000, 1));
72 ASSERT_EQ(std::vector<uint8_t>(content.begin() + 4096, content.begin() + 6096), result);
73
74 result.resize(20000);
75 ASSERT_TRUE(block_map_data->ReadBlockAlignedData(result.data(), 20000, 0));
76 ASSERT_EQ(std::vector<uint8_t>(content.begin(), content.begin() + 20000), result);
77}
78
79TEST(FuseBlockMapTest, ReadBlockAlignedData_large_fuse_block) {
80 std::string content;
81 for (char c = 0; c < 10; c++) {
82 content += std::string(4096, c);
83 }
84
85 TemporaryFile temp_file;
86 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
87
88 std::vector<std::string> lines = {
89 temp_file.path, "36384 4096", "2", "0 5", "6 10",
90 };
91 TemporaryFile block_map;
92 ASSERT_TRUE(android::base::WriteStringToFile(android::base::Join(lines, '\n'), block_map.path));
93
94 auto block_map_data = FuseBlockDataProvider::CreateFromBlockMap(block_map.path, 16384);
95 ASSERT_TRUE(block_map_data);
96
97 std::vector<uint8_t> result(20000);
98 // Out of bound read
99 ASSERT_FALSE(block_map_data->ReadBlockAlignedData(result.data(), 20000, 2));
100 ASSERT_TRUE(block_map_data->ReadBlockAlignedData(result.data(), 20000, 1));
101 // expected source block contains: 4, 6-9
102 std::string expected = content.substr(16384, 4096) + content.substr(24576, 15904);
103 ASSERT_EQ(std::vector<uint8_t>(expected.begin(), expected.end()), result);
104}