shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | using namespace mediaprovider::fuse; |
| 27 | |
| 28 | using std::unique_ptr; |
| 29 | using std::vector; |
| 30 | |
| 31 | unique_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 | */ |
| 42 | TEST(RedactionInfoTest, testNoRedactionRanges) { |
| 43 | RedactionInfo info(0, nullptr); |
| 44 | EXPECT_EQ(0, info.size()); |
| 45 | EXPECT_EQ(false, info.isRedactionNeeded()); |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 46 | |
| 47 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 1000); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 48 | EXPECT_EQ(0, overlapping_rr->size()); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Test the case where there is 1 redaction range. |
| 53 | */ |
| 54 | TEST(RedactionInfoTest, testSingleRedactionRange) { |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 55 | off64_t ranges[2] = { |
| 56 | 1, |
| 57 | 10, |
| 58 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 59 | RedactionInfo info(1, ranges); |
| 60 | EXPECT_EQ(1, info.size()); |
| 61 | EXPECT_EQ(true, info.isRedactionNeeded()); |
| 62 | // Overlapping ranges |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 63 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 0); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 64 | EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr); |
| 65 | |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 66 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 5, /*off*/ 0); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 67 | EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr); |
| 68 | |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 69 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 5, /*off*/ 5); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 70 | EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr); |
| 71 | |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 72 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 10, /*off*/ 1); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 73 | EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr); |
| 74 | |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 75 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1, /*off*/ 1); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 76 | EXPECT_EQ(*(createRedactionRangeVector(1, ranges)), *overlapping_rr); |
| 77 | |
| 78 | // Non-overlapping range |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 79 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 11); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 80 | EXPECT_EQ(*(createRedactionRangeVector(0, nullptr)), *overlapping_rr); |
| 81 | |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 82 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1, /*off*/ 11); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 83 | 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 | */ |
| 89 | TEST(RedactionInfoTest, testSortedAndNonOverlappingRedactionRanges) { |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 90 | off64_t ranges[6] = { |
| 91 | 1, 10, 15, 21, 32, 40, |
| 92 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 93 | |
| 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] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 99 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 50, /*off*/ 0); |
| 100 | off64_t expected1[] = { |
| 101 | 1, 10, 15, 21, 32, 40, |
| 102 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 103 | EXPECT_EQ(*(createRedactionRangeVector(3, expected1)), *overlapping_rr); |
| 104 | |
| 105 | // Read request strictly contains a subset of the ranges: [15, 40] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 106 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15); |
| 107 | off64_t expected2[] = { |
| 108 | 15, |
| 109 | 21, |
| 110 | 32, |
| 111 | 40, |
| 112 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 113 | EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr); |
| 114 | |
| 115 | // Read request intersects with a subset of the ranges" [16, 32] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 116 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 17, /*off*/ 16); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 117 | EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Test the case where the redaction ranges require sorting |
| 122 | */ |
| 123 | TEST(RedactionInfoTest, testSortRedactionRanges) { |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 124 | off64_t ranges[6] = { |
| 125 | 1, 10, 32, 40, 15, 21, |
| 126 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 127 | |
| 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] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 133 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 50, /*off*/ 0); |
| 134 | off64_t expected1[] = { |
| 135 | 1, 10, 15, 21, 32, 40, |
| 136 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 137 | EXPECT_EQ(*(createRedactionRangeVector(3, expected1)), *overlapping_rr); |
| 138 | |
| 139 | // Read request strictly contains a subset of the ranges: [15, 40] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 140 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15); |
| 141 | off64_t expected2[] = { |
| 142 | 15, |
| 143 | 21, |
| 144 | 32, |
| 145 | 40, |
| 146 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 147 | EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr); |
| 148 | |
| 149 | // Read request intersects with a subset of the ranges" [16, 32] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 150 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 17, /*off*/ 16); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 151 | EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Test the case where the redaction ranges require sorting or merging |
| 156 | */ |
| 157 | TEST(RedactionInfoTest, testSortAndMergeRedactionRanges) { |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 158 | off64_t ranges[8] = { |
| 159 | 35, 40, 1, 10, 32, 35, 15, 21, |
| 160 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 161 | |
| 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] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 167 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 50, /*off*/ 0); |
| 168 | off64_t expected1[] = { |
| 169 | 1, 10, 15, 21, 32, 40, |
| 170 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 171 | EXPECT_EQ(*(createRedactionRangeVector(3, expected1)), *overlapping_rr); |
| 172 | |
| 173 | // Read request strictly contains a subset of the ranges: [15, 40] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 174 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15); |
| 175 | off64_t expected2[] = { |
| 176 | 15, |
| 177 | 21, |
| 178 | 32, |
| 179 | 40, |
| 180 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 181 | EXPECT_EQ(*(createRedactionRangeVector(2, expected2)), *overlapping_rr); |
| 182 | |
| 183 | // Read request intersects with a subset of the ranges" [16, 32] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 184 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 17, /*off*/ 16); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 185 | 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 | */ |
| 191 | TEST(RedactionInfoTest, testMergeAllRangesIntoTheFirstRange) { |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 192 | off64_t ranges[10] = { |
| 193 | 1, 100, 2, 99, 3, 98, 4, 97, 3, 15, |
| 194 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 195 | |
| 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] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 201 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 1); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 202 | off64_t expected[] = {1, 100}; |
| 203 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 204 | |
| 205 | // Read request is contained in the range: [15, 40] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 206 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 207 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 208 | |
| 209 | // Read request that strictly contains all of the redaction ranges |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 210 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 0); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 211 | 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 | */ |
| 217 | TEST(RedactionInfoTest, testMergeAllRangesIntoTheLastRange) { |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 218 | off64_t ranges[10] = { |
| 219 | 4, 96, 3, 97, 2, 98, 1, 99, 0, 100, |
| 220 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 221 | |
| 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] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 227 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 0); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 228 | off64_t expected[] = {0, 100}; |
| 229 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 230 | |
| 231 | // Read request is contained in the range: [15, 40] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 232 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 26, /*off*/ 15); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 233 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 234 | |
| 235 | // Read request that strictly contains all of the redaction ranges |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 236 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 1000, /*off*/ 0); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 237 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Test the case where the redaction ranges progressively merge |
| 242 | */ |
| 243 | TEST(RedactionInfoTest, testMergeAllRangesProgressively) { |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 244 | off64_t ranges[10] = { |
| 245 | 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, |
| 246 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 247 | |
| 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] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 253 | auto overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 15, /*off*/ 1); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 254 | off64_t expected[] = {1, 15}; |
| 255 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 256 | |
| 257 | // Read request is contained in the range: [2, 12] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 258 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 10, /*off*/ 2); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 259 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 260 | |
| 261 | // Read request that strictly contains all of the redaction ranges |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 262 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 100, /*off*/ 0); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 263 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 264 | |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 265 | off64_t reverse_rr[10] = { |
| 266 | 5, 15, 4, 14, 3, 13, 2, 12, 1, 11, |
| 267 | }; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 268 | |
| 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] |
Zim | 2e5ad88 | 2020-01-13 14:11:19 +0000 | [diff] [blame] | 274 | overlapping_rr = info.getOverlappingRedactionRanges(/*size*/ 15, /*off*/ 1); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 275 | EXPECT_EQ(*(createRedactionRangeVector(1, expected)), *overlapping_rr); |
| 276 | } |