1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specic language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "RedactionInfoTest"
#include <gtest/gtest.h>
#include <memory>
#include <ostream>
#include <vector>
#include "libfuse_jni/RedactionInfo.h"
namespace mediaprovider::fuse {
using std::vector;
std::ostream& operator<<(std::ostream& os, const ReadRange& rr) {
os << "{ " << rr.start << ", " << rr.size << ", " << rr.is_redaction << " }";
return os;
}
TEST(RedactionInfoTest, testNoRedactionRanges) {
RedactionInfo info(0, nullptr);
EXPECT_EQ(0, info.size());
EXPECT_EQ(false, info.isRedactionNeeded());
std::vector<ReadRange> out;
info.getReadRanges(0, std::numeric_limits<size_t>::max(), &out);
EXPECT_EQ(0, out.size());
}
// Test the case where there is 1 redaction range.
TEST(RedactionInfoTest, testSingleRedactionRange) {
off64_t ranges[2] = {
1,
10,
};
RedactionInfo info(1, ranges);
EXPECT_EQ(1, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
// Overlapping ranges
std::vector<ReadRange> out;
info.getReadRanges(0, 1000, &out); // read offsets [0, 1000)
EXPECT_EQ(3, out.size());
EXPECT_EQ(ReadRange(0, 1, false), out[0]);
EXPECT_EQ(ReadRange(1, 9, true), out[1]);
EXPECT_EQ(ReadRange(10, 990, false), out[2]);
out.clear();
info.getReadRanges(0, 5, &out); // read offsets [0, 5)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(0, 1, false), out[0]); // offsets: [0, 1) len = 1
EXPECT_EQ(ReadRange(1, 4, true), out[1]); // offsets: [1, 5) len = 4
out.clear();
info.getReadRanges(1, 10, &out); // read offsets [1, 11)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(1, 9, true), out[0]); // offsets: [1, 10) len = 9
EXPECT_EQ(ReadRange(10, 1, false), out[1]); // offsets: [10, 11) len = 1
// Read ranges that start or end with the boundary of the redacted area.
out.clear();
info.getReadRanges(5, 5, &out); // read offsets [5, 10)
EXPECT_EQ(1, out.size());
EXPECT_EQ(ReadRange(5, 5, true), out[0]); // offsets: [5, 10) len = 5
out.clear();
info.getReadRanges(1, 5, &out); // read offsets [1, 6)
EXPECT_EQ(1, out.size());
EXPECT_EQ(ReadRange(1, 5, true), out[0]); // offsets: [1, 6) len = 5
// Read ranges adjoining the redacted area.
out.clear();
info.getReadRanges(10, 10, &out); // read offsets [10, 20)
EXPECT_EQ(0, out.size());
out.clear();
info.getReadRanges(0, 1, &out); // read offsets [0, 1)
EXPECT_EQ(0, out.size());
// Read Range outside the redacted area.
out.clear();
info.getReadRanges(200, 10, &out); // read offsets [200, 210)
EXPECT_EQ(0, out.size());
}
// Multiple redaction ranges within a given area.
TEST(RedactionInfoTest, testSortedAndNonOverlappingRedactionRanges) {
// [10, 20), [30, 40), [40, 50)
off64_t ranges[4] = {10, 20, 30, 40};
RedactionInfo info = RedactionInfo(2, ranges);
EXPECT_EQ(2, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
std::vector<ReadRange> out;
info.getReadRanges(0, 40, &out); // read offsets [0, 40)
EXPECT_EQ(4, out.size());
EXPECT_EQ(ReadRange(0, 10, false), out[0]); // offsets: [0, 10) len = 10
EXPECT_EQ(ReadRange(10, 10, true), out[1]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 10, false), out[2]); // offsets: [20, 30) len = 10
EXPECT_EQ(ReadRange(30, 10, true), out[3]); // offsets [30, 40) len = 10
// Read request straddling two ranges.
out.clear();
info.getReadRanges(5, 30, &out); // read offsets [5, 35)
EXPECT_EQ(4, out.size());
EXPECT_EQ(ReadRange(5, 5, false), out[0]); // offsets: [5, 10) len = 5
EXPECT_EQ(ReadRange(10, 10, true), out[1]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 10, false), out[2]); // offsets: [20, 30) len = 10
EXPECT_EQ(ReadRange(30, 5, true), out[3]); // offsets [30, 35) len = 5
// Read request overlapping first range only.
out.clear();
info.getReadRanges(5, 10, &out); // read offsets [5, 15)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(5, 5, false), out[0]); // offsets: [5, 10) len = 5
EXPECT_EQ(ReadRange(10, 5, true), out[1]); // offsets: [10, 15) len = 5
// Read request overlapping last range only.
out.clear();
info.getReadRanges(35, 10, &out); // read offsets [35, 45)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(35, 5, true), out[0]); // offsets: [35, 40) len = 5
EXPECT_EQ(ReadRange(40, 5, false), out[1]); // offsets: [40, 45) len = 5
// Read request overlapping no ranges.
out.clear();
info.getReadRanges(0, 10, &out); // read offsets [0, 10)
EXPECT_EQ(0, out.size());
out.clear();
info.getReadRanges(21, 5, &out); // read offsets [21, 26)
EXPECT_EQ(0, out.size());
out.clear();
info.getReadRanges(40, 10, &out); // read offsets [40, 50)
EXPECT_EQ(0, out.size());
}
// Multiple redaction ranges overlapping with read range.
TEST(RedactionInfoTest, testReadRangeOverlappingWithRedactionRanges) {
// [10, 20), [30, 40)
off64_t ranges[4] = {10, 20, 30, 40};
RedactionInfo info = RedactionInfo(2, ranges);
EXPECT_EQ(2, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
std::vector<ReadRange> out;
// Read request overlaps with end of the ranges.
info.getReadRanges(20, 20, &out); // read offsets [20, 40)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(20, 10, false), out[0]); // offsets: [20, 30) len = 10
EXPECT_EQ(ReadRange(30, 10, true), out[1]); // offsets: [30, 40) len = 10
// Read request overlapping with start of the ranges
out.clear();
info.getReadRanges(10, 20, &out); // read offsets [10, 30)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(10, 10, true), out[0]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 10, false), out[1]); // offsets: [20, 30) len = 10
// Read request overlaps with start of one and end of other range.
out.clear();
info.getReadRanges(10, 30, &out); // read offsets [10, 40)
EXPECT_EQ(3, out.size());
EXPECT_EQ(ReadRange(10, 10, true), out[0]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 10, false), out[1]); // offsets: [20, 30) len = 10
EXPECT_EQ(ReadRange(30, 10, true), out[2]); // offsets: [30, 40) len = 10
// Read request overlaps with end of one and start of other range.
out.clear();
info.getReadRanges(20, 10, &out); // read offsets [20, 30)
EXPECT_EQ(0, out.size());
}
TEST(RedactionInfoTest, testRedactionRangesSorted) {
off64_t ranges[6] = {30, 40, 50, 60, 10, 20};
RedactionInfo info = RedactionInfo(3, ranges);
EXPECT_EQ(3, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
std::vector<ReadRange> out;
info.getReadRanges(0, 60, &out); // read offsets [0, 60)
EXPECT_EQ(6, out.size());
EXPECT_EQ(ReadRange(0, 10, false), out[0]); // offsets: [0, 10) len = 10
EXPECT_EQ(ReadRange(10, 10, true), out[1]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 10, false), out[2]); // offsets: [20, 30) len = 10
EXPECT_EQ(ReadRange(30, 10, true), out[3]); // offsets [30, 40) len = 10
EXPECT_EQ(ReadRange(40, 10, false), out[4]); // offsets [40, 50) len = 10
EXPECT_EQ(ReadRange(50, 10, true), out[5]); // offsets [50, 60) len = 10
// Read request overlapping first range only.
out.clear();
info.getReadRanges(5, 10, &out); // read offsets [5, 15)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(5, 5, false), out[0]); // offsets: [5, 10) len = 5
EXPECT_EQ(ReadRange(10, 5, true), out[1]); // offsets: [10, 15) len = 5
// Read request overlapping last range only.
out.clear();
info.getReadRanges(55, 10, &out); // read offsets [55, 65)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(55, 5, true), out[0]); // offsets: [55, 60) len = 5
EXPECT_EQ(ReadRange(60, 5, false), out[1]); // offsets: [60, 65) len = 5
// Read request overlapping no ranges.
out.clear();
info.getReadRanges(0, 10, &out); // read offsets [0, 10)
EXPECT_EQ(0, out.size());
out.clear();
info.getReadRanges(60, 10, &out); // read offsets [60, 70)
EXPECT_EQ(0, out.size());
}
// Test that the ranges are both sorted and merged
TEST(RedactionInfoTest, testSortAndMergeRedactionRanges) {
// Ranges are: [10, 20), [25, 40), [50, 60)
off64_t ranges[8] = {30, 40, 10, 20, 25, 30, 50, 60};
RedactionInfo info = RedactionInfo(4, ranges);
EXPECT_EQ(3, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
std::vector<ReadRange> out;
info.getReadRanges(0, 60, &out); // read offsets [0, 60)
EXPECT_EQ(6, out.size());
EXPECT_EQ(ReadRange(0, 10, false), out[0]); // offsets: [0, 10) len = 10
EXPECT_EQ(ReadRange(10, 10, true), out[1]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 5, false), out[2]); // offsets: [20, 25) len = 5
EXPECT_EQ(ReadRange(25, 15, true), out[3]); // offsets [25, 40) len = 15
EXPECT_EQ(ReadRange(40, 10, false), out[4]); // offsets [40, 50) len = 10
EXPECT_EQ(ReadRange(50, 10, true), out[5]); // offsets [50, 60) len = 10
}
// Test that the ranges are both sorted and merged when there's an overlap.
//
// TODO: Can this ever happen ? Will we ever be in a state where we need to
// redact exif attributes that have overlapping ranges ?
TEST(RedactionInfoTest, testSortAndMergeRedactionRanges_overlap) {
// Ranges are: [10, 20), [25, 40), [50, 60)
off64_t ranges[8] = {30, 40, 10, 20, 25, 34, 50, 60};
RedactionInfo info = RedactionInfo(4, ranges);
EXPECT_EQ(3, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
std::vector<ReadRange> out;
info.getReadRanges(0, 60, &out); // read offsets [0, 60)
EXPECT_EQ(6, out.size());
EXPECT_EQ(ReadRange(0, 10, false), out[0]); // offsets: [0, 10) len = 10
EXPECT_EQ(ReadRange(10, 10, true), out[1]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 5, false), out[2]); // offsets: [20, 25) len = 5
EXPECT_EQ(ReadRange(25, 15, true), out[3]); // offsets [25, 40) len = 15
EXPECT_EQ(ReadRange(40, 10, false), out[4]); // offsets [40, 50) len = 10
EXPECT_EQ(ReadRange(50, 10, true), out[5]); // offsets [50, 60) len = 10
}
// WARNING: The tests below assume that merging of ranges happen during
// object construction (which is asserted by the check on |info.size()|.
// Therefore, we don't write redundant tests for boundary conditions that
// we've covered above. If this ever changes, these tests need to be expanded.
TEST(RedactionInfoTest, testMergeAllRangesIntoSingleRange) {
// Ranges are: [8, 24)
off64_t ranges[8] = {10, 20, 8, 14, 14, 24, 12, 16};
RedactionInfo info = RedactionInfo(4, ranges);
EXPECT_EQ(1, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
std::vector<ReadRange> out;
info.getReadRanges(0, 30, &out); // read offsets [0, 30)
EXPECT_EQ(3, out.size());
EXPECT_EQ(ReadRange(0, 8, false), out[0]); // offsets: [0, 8) len = 8
EXPECT_EQ(ReadRange(8, 16, true), out[1]); // offsets: [8, 24) len = 16
EXPECT_EQ(ReadRange(24, 6, false), out[2]); // offsets: [24, 30) len = 6
// Ranges are: [85, 100)
off64_t ranges2[10] = {90, 95, 95, 100, 85, 91, 92, 94, 99, 100};
info = RedactionInfo(5, ranges2);
EXPECT_EQ(1, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
out.clear();
info.getReadRanges(80, 30, &out); // read offsets [80, 110)
EXPECT_EQ(3, out.size());
EXPECT_EQ(ReadRange(80, 5, false), out[0]); // offsets: [80, 85) len = 5
EXPECT_EQ(ReadRange(85, 15, true), out[1]); // offsets: [85, 100) len = 15
EXPECT_EQ(ReadRange(100, 10, false), out[2]); // offsets: [100, 110) len = 10
}
TEST(RedactionInfoTest, testMergeMultipleRanges) {
// Ranges are: [10, 30), [60, 80)
off64_t ranges[8] = {20, 30, 10, 20, 70, 80, 60, 70};
RedactionInfo info = RedactionInfo(4, ranges);
EXPECT_EQ(2, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
std::vector<ReadRange> out;
info.getReadRanges(0, 100, &out); // read offsets [0, 100)
EXPECT_EQ(5, out.size());
EXPECT_EQ(ReadRange(0, 10, false), out[0]); // offsets: [0, 10) len = 10
EXPECT_EQ(ReadRange(10, 20, true), out[1]); // offsets: [10, 30) len = 20
EXPECT_EQ(ReadRange(30, 30, false), out[2]); // offsets: [30, 60) len = 30
EXPECT_EQ(ReadRange(60, 20, true), out[3]); // offsets [60, 80) len = 20
EXPECT_EQ(ReadRange(80, 20, false), out[4]); // offsets [80, 100) len = 20
}
// Redaction ranges of size zero.
TEST(RedactionInfoTest, testRedactionRangesZeroSize) {
// [10, 20), [30, 40)
off64_t ranges[6] = {10, 20, 30, 40, 25, 25};
RedactionInfo info = RedactionInfo(3, ranges);
EXPECT_EQ(2, info.size());
EXPECT_EQ(true, info.isRedactionNeeded());
// Normal read request, should skip range with zero size
std::vector<ReadRange> out;
info.getReadRanges(0, 40, &out); // read offsets [0, 40)
EXPECT_EQ(4, out.size());
EXPECT_EQ(ReadRange(0, 10, false), out[0]); // offsets: [0, 10) len = 10
EXPECT_EQ(ReadRange(10, 10, true), out[1]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 10, false), out[2]); // offsets: [20, 30) len = 10
EXPECT_EQ(ReadRange(30, 10, true), out[3]); // offsets [30, 40) len = 10
// Read request starting at offset overlapping with zero size range.
out.clear();
info.getReadRanges(25, 10, &out); // read offsets [25, 35)
EXPECT_EQ(2, out.size());
EXPECT_EQ(ReadRange(25, 5, false), out[0]); // offsets: [25, 30) len = 5
EXPECT_EQ(ReadRange(30, 5, true), out[1]); // offsets [30, 35) len = 5
// 1 byte read request starting at offset overlapping with zero size range.
out.clear();
info.getReadRanges(25, 1, &out); // read offsets [25, 26)
EXPECT_EQ(0, out.size());
// Read request ending at offset overlapping with zero size range.
out.clear();
info.getReadRanges(0, 25, &out); // read offsets [0, 25)
EXPECT_EQ(3, out.size());
EXPECT_EQ(ReadRange(0, 10, false), out[0]); // offsets: [0, 10) len = 10
EXPECT_EQ(ReadRange(10, 10, true), out[1]); // offsets: [10, 20) len = 10
EXPECT_EQ(ReadRange(20, 5, false), out[2]); // offsets: [20, 25) len = 10
// Read request that includes only zero size range
out.clear();
info.getReadRanges(20, 10, &out); // read offsets [20, 27)
EXPECT_EQ(0, out.size());
}
// Single redaction range with zero size
TEST(RedactionInfoTest, testSingleRedactionRangesZeroSize) {
off64_t ranges[2] = {10, 10};
RedactionInfo info = RedactionInfo(1, ranges);
EXPECT_EQ(0, info.size());
EXPECT_EQ(false, info.isRedactionNeeded());
// Normal read request, should skip range with zero size
std::vector<ReadRange> out;
info.getReadRanges(0, 40, &out); // read offsets [0, 40)
EXPECT_EQ(0, out.size());
}
} // namespace mediaprovider::fuse
|