blob: 9d98058e11b5dd2303787f51a5267b5806b83069 [file] [log] [blame]
shafikc3f62672019-08-30 11:15:48 +01001/*
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 specic language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "RedactionInfoTest"
18
19#include <gtest/gtest.h>
20
21#include <memory>
22#include <vector>
23
24#include "libfuse_jni/RedactionInfo.h"
25
26using namespace mediaprovider::fuse;
27
28using std::unique_ptr;
29using std::vector;
30
31unique_ptr<vector<RedactionRange>> createRedactionRangeVector(int num_rr, off64_t* rr) {
32 auto res = std::make_unique<vector<RedactionRange>>();
33 for (int i = 0; i < num_rr; ++i) {
34 res->push_back(RedactionRange(rr[2 * i], rr[2 * i + 1]));
35 }
36 return res;
37}
38
39/**
40 * Test the case where there are no redaction ranges.
41 */
42TEST(RedactionInfoTest, testNoRedactionRanges) {
43 RedactionInfo info(0, nullptr);
44 EXPECT_EQ(0, info.size());
45 EXPECT_EQ(false, info.isRedactionNeeded());
Zim2e5ad882020-01-13 14:11:19 +000046
47 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 1000);
shafikc3f62672019-08-30 11:15:48 +010048 EXPECT_EQ(0, overlapping_rr->size());
49}
50
51/**
52 * Test the case where there is 1 redaction range.
53 */
54TEST(RedactionInfoTest, testSingleRedactionRange) {
Zim2e5ad882020-01-13 14:11:19 +000055 off64_t ranges[2] = {
56 1,
57 10,
58 };
shafikc3f62672019-08-30 11:15:48 +010059 RedactionInfo info(1, ranges);
60 EXPECT_EQ(1, info.size());
61 EXPECT_EQ(true, info.isRedactionNeeded());
62 // Overlapping ranges
Zim2e5ad882020-01-13 14:11:19 +000063 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 0);
shafikc3f62672019-08-30 11:15:48 +010064 EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr);
65
Zim2e5ad882020-01-13 14:11:19 +000066 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 5, /*off*/ 0);
shafikc3f62672019-08-30 11:15:48 +010067 EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr);
68
Zim2e5ad882020-01-13 14:11:19 +000069 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 5, /*off*/ 5);
shafikc3f62672019-08-30 11:15:48 +010070 EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr);
71
Zim2e5ad882020-01-13 14:11:19 +000072 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 10, /*off*/ 1);
shafikc3f62672019-08-30 11:15:48 +010073 EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr);
74
Zim2e5ad882020-01-13 14:11:19 +000075 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1, /*off*/ 1);
shafikc3f62672019-08-30 11:15:48 +010076 EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr);
77
78 // Non-overlapping range
Zim2e5ad882020-01-13 14:11:19 +000079 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 11);
shafikc3f62672019-08-30 11:15:48 +010080 EXPECT_EQ(*(createRedactionRangeVector(0, nullptr)), *overlapping_rr);
81
Zim2e5ad882020-01-13 14:11:19 +000082 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1, /*off*/ 11);
shafikc3f62672019-08-30 11:15:48 +010083 EXPECT_EQ(*(createRedactionRangeVector(0, nullptr)), *overlapping_rr);
84}
85
86/**
87 * Test the case where the redaction ranges don't require sorting or merging
88 */
89TEST(RedactionInfoTest, testSortedAndNonOverlappingRedactionRanges) {
Zim2e5ad882020-01-13 14:11:19 +000090 off64_t ranges[6] = {
91 1, 10, 15, 21, 32, 40,
92 };
shafikc3f62672019-08-30 11:15:48 +010093
94 RedactionInfo info = RedactionInfo(3, ranges);
95 EXPECT_EQ(3, info.size());
96 EXPECT_EQ(true, info.isRedactionNeeded());
97
98 // Read request strictly contains all ranges: [0, 49]
Zim2e5ad882020-01-13 14:11:19 +000099 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 50, /*off*/ 0);
100 off64_t expected1[] = {
101 1, 10, 15, 21, 32, 40,
102 };
shafikc3f62672019-08-30 11:15:48 +0100103 EXPECT_EQ(*(createRedactionRangeVector(3, expected1)), *overlapping_rr);
104
105 // Read request strictly contains a subset of the ranges: [15, 40]
Zim2e5ad882020-01-13 14:11:19 +0000106 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15);
107 off64_t expected2[] = {
108 15,
109 21,
110 32,
111 40,
112 };
shafikc3f62672019-08-30 11:15:48 +0100113 EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr);
114
115 // Read request intersects with a subset of the ranges" [16, 32]
Zim2e5ad882020-01-13 14:11:19 +0000116 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 17, /*off*/ 16);
shafikc3f62672019-08-30 11:15:48 +0100117 EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr);
118}
119
120/**
121 * Test the case where the redaction ranges require sorting
122 */
123TEST(RedactionInfoTest, testSortRedactionRanges) {
Zim2e5ad882020-01-13 14:11:19 +0000124 off64_t ranges[6] = {
125 1, 10, 32, 40, 15, 21,
126 };
shafikc3f62672019-08-30 11:15:48 +0100127
128 RedactionInfo info = RedactionInfo(3, ranges);
129 EXPECT_EQ(3, info.size());
130 EXPECT_EQ(true, info.isRedactionNeeded());
131
132 // Read request strictly contains all ranges: [0, 49]
Zim2e5ad882020-01-13 14:11:19 +0000133 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 50, /*off*/ 0);
134 off64_t expected1[] = {
135 1, 10, 15, 21, 32, 40,
136 };
shafikc3f62672019-08-30 11:15:48 +0100137 EXPECT_EQ(*(createRedactionRangeVector(3, expected1)), *overlapping_rr);
138
139 // Read request strictly contains a subset of the ranges: [15, 40]
Zim2e5ad882020-01-13 14:11:19 +0000140 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15);
141 off64_t expected2[] = {
142 15,
143 21,
144 32,
145 40,
146 };
shafikc3f62672019-08-30 11:15:48 +0100147 EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr);
148
149 // Read request intersects with a subset of the ranges" [16, 32]
Zim2e5ad882020-01-13 14:11:19 +0000150 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 17, /*off*/ 16);
shafikc3f62672019-08-30 11:15:48 +0100151 EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr);
152}
153
154/**
155 * Test the case where the redaction ranges require sorting or merging
156 */
157TEST(RedactionInfoTest, testSortAndMergeRedactionRanges) {
Zim2e5ad882020-01-13 14:11:19 +0000158 off64_t ranges[8] = {
159 35, 40, 1, 10, 32, 35, 15, 21,
160 };
shafikc3f62672019-08-30 11:15:48 +0100161
162 RedactionInfo info = RedactionInfo(4, ranges);
163 EXPECT_EQ(3, info.size());
164 EXPECT_EQ(true, info.isRedactionNeeded());
165
166 // Read request strictly contains all ranges: [0, 49]
Zim2e5ad882020-01-13 14:11:19 +0000167 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 50, /*off*/ 0);
168 off64_t expected1[] = {
169 1, 10, 15, 21, 32, 40,
170 };
shafikc3f62672019-08-30 11:15:48 +0100171 EXPECT_EQ(*(createRedactionRangeVector(3, expected1)), *overlapping_rr);
172
173 // Read request strictly contains a subset of the ranges: [15, 40]
Zim2e5ad882020-01-13 14:11:19 +0000174 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15);
175 off64_t expected2[] = {
176 15,
177 21,
178 32,
179 40,
180 };
shafikc3f62672019-08-30 11:15:48 +0100181 EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr);
182
183 // Read request intersects with a subset of the ranges" [16, 32]
Zim2e5ad882020-01-13 14:11:19 +0000184 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 17, /*off*/ 16);
shafikc3f62672019-08-30 11:15:48 +0100185 EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr);
186}
187
188/**
189 * Test the case where the redaction ranges all merge into the first range
190 */
191TEST(RedactionInfoTest, testMergeAllRangesIntoTheFirstRange) {
Zim2e5ad882020-01-13 14:11:19 +0000192 off64_t ranges[10] = {
193 1, 100, 2, 99, 3, 98, 4, 97, 3, 15,
194 };
shafikc3f62672019-08-30 11:15:48 +0100195
196 RedactionInfo info = RedactionInfo(5, ranges);
197 EXPECT_EQ(1, info.size());
198 EXPECT_EQ(true, info.isRedactionNeeded());
199
200 // Read request equals the range: [1, 100]
Zim2e5ad882020-01-13 14:11:19 +0000201 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 1);
shafikc3f62672019-08-30 11:15:48 +0100202 off64_t expected[] = {1, 100};
203 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
204
205 // Read request is contained in the range: [15, 40]
Zim2e5ad882020-01-13 14:11:19 +0000206 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15);
shafikc3f62672019-08-30 11:15:48 +0100207 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
208
209 // Read request that strictly contains all of the redaction ranges
Zim2e5ad882020-01-13 14:11:19 +0000210 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 0);
shafikc3f62672019-08-30 11:15:48 +0100211 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
212}
213
214/**
215 * Test the case where the redaction ranges all merge into the last range
216 */
217TEST(RedactionInfoTest, testMergeAllRangesIntoTheLastRange) {
Zim2e5ad882020-01-13 14:11:19 +0000218 off64_t ranges[10] = {
219 4, 96, 3, 97, 2, 98, 1, 99, 0, 100,
220 };
shafikc3f62672019-08-30 11:15:48 +0100221
222 RedactionInfo info = RedactionInfo(5, ranges);
223 EXPECT_EQ(1, info.size());
224 EXPECT_EQ(true, info.isRedactionNeeded());
225
226 // Read request equals the range: [0, 100]
Zim2e5ad882020-01-13 14:11:19 +0000227 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 0);
shafikc3f62672019-08-30 11:15:48 +0100228 off64_t expected[] = {0, 100};
229 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
230
231 // Read request is contained in the range: [15, 40]
Zim2e5ad882020-01-13 14:11:19 +0000232 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15);
shafikc3f62672019-08-30 11:15:48 +0100233 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
234
235 // Read request that strictly contains all of the redaction ranges
Zim2e5ad882020-01-13 14:11:19 +0000236 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 0);
shafikc3f62672019-08-30 11:15:48 +0100237 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
238}
239
240/**
241 * Test the case where the redaction ranges progressively merge
242 */
243TEST(RedactionInfoTest, testMergeAllRangesProgressively) {
Zim2e5ad882020-01-13 14:11:19 +0000244 off64_t ranges[10] = {
245 1, 11, 2, 12, 3, 13, 4, 14, 5, 15,
246 };
shafikc3f62672019-08-30 11:15:48 +0100247
248 RedactionInfo info = RedactionInfo(5, ranges);
249 EXPECT_EQ(1, info.size());
250 EXPECT_EQ(true, info.isRedactionNeeded());
251
252 // Read request equals the range: [1, 15]
Zim2e5ad882020-01-13 14:11:19 +0000253 auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 15, /*off*/ 1);
shafikc3f62672019-08-30 11:15:48 +0100254 off64_t expected[] = {1, 15};
255 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
256
257 // Read request is contained in the range: [2, 12]
Zim2e5ad882020-01-13 14:11:19 +0000258 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 10, /*off*/ 2);
shafikc3f62672019-08-30 11:15:48 +0100259 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
260
261 // Read request that strictly contains all of the redaction ranges
Zim2e5ad882020-01-13 14:11:19 +0000262 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 0);
shafikc3f62672019-08-30 11:15:48 +0100263 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
264
Zim2e5ad882020-01-13 14:11:19 +0000265 off64_t reverse_rr[10] = {
266 5, 15, 4, 14, 3, 13, 2, 12, 1, 11,
267 };
shafikc3f62672019-08-30 11:15:48 +0100268
269 RedactionInfo reverse_info = RedactionInfo(5, reverse_rr);
270 EXPECT_EQ(1, info.size());
271 EXPECT_EQ(true, info.isRedactionNeeded());
272
273 // Read request equals the range: [1, 15]
Zim2e5ad882020-01-13 14:11:19 +0000274 overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 15, /*off*/ 1);
shafikc3f62672019-08-30 11:15:48 +0100275 EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr);
276}