Debian Codegraph @

libs/androidfw/tests/CursorWindow_test.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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 <memory>
18 #include <utility>
19
20 #include "androidfw/CursorWindow.h"
21
22 #include "TestHelpers.h"
23
24 #define CREATE_WINDOW_1K \
25 CursorWindow* w; \
26 CursorWindow::create(String8("test"), 1 << 10, &w);
27
28 #define CREATE_WINDOW_1K_3X3 \
29 CursorWindow* w; \
30 CursorWindow::create(String8("test"), 1 << 10, &w); \
31 ASSERT_EQ(w->setNumColumns(3), OK); \
32 ASSERT_EQ(w->allocRow(), OK); \
33 ASSERT_EQ(w->allocRow(), OK); \
34 ASSERT_EQ(w->allocRow(), OK);
35
36 #define CREATE_WINDOW_2M \
37 CursorWindow* w; \
38 CursorWindow::create(String8("test"), 1 << 21, &w);
39
40 static constexpr const size_t kHalfInlineSize = 8192;
41 static constexpr const size_t kGiantSize = 1048576;
42
43 namespace android {
44
45 TEST(CursorWindowTest, Empty) {
46 CREATE_WINDOW_1K;
47
48 ASSERT_EQ(w->getNumRows(), 0);
49 ASSERT_EQ(w->getNumColumns(), 0);
50 ASSERT_EQ(w->size(), 1 << 10);
51 ASSERT_EQ(w->freeSpace(), 1 << 10);
52 }
53
54 TEST(CursorWindowTest, SetNumColumns) {
55 CREATE_WINDOW_1K;
56
57 // Once we've locked in columns, we can't adjust
58 ASSERT_EQ(w->getNumColumns(), 0);
59 ASSERT_EQ(w->setNumColumns(4), OK);
60 ASSERT_NE(w->setNumColumns(5), OK);
61 ASSERT_NE(w->setNumColumns(3), OK);
62 ASSERT_EQ(w->getNumColumns(), 4);
63 }
64
65 TEST(CursorWindowTest, SetNumColumnsAfterRow) {
66 CREATE_WINDOW_1K;
67
68 // Once we've locked in a row, we can't adjust columns
69 ASSERT_EQ(w->getNumColumns(), 0);
70 ASSERT_EQ(w->allocRow(), OK);
71 ASSERT_NE(w->setNumColumns(4), OK);
72 ASSERT_EQ(w->getNumColumns(), 0);
73 }
74
75 TEST(CursorWindowTest, AllocRow) {
76 CREATE_WINDOW_1K;
77
78 ASSERT_EQ(w->setNumColumns(4), OK);
79
80 // Rolling forward means we have less free space
81 ASSERT_EQ(w->getNumRows(), 0);
82 auto before = w->freeSpace();
83 ASSERT_EQ(w->allocRow(), OK);
84 ASSERT_LT(w->freeSpace(), before);
85 ASSERT_EQ(w->getNumRows(), 1);
86
87 // Verify we can unwind
88 ASSERT_EQ(w->freeLastRow(), OK);
89 ASSERT_EQ(w->freeSpace(), before);
90 ASSERT_EQ(w->getNumRows(), 0);
91
92 // Can't unwind when no rows left
93 ASSERT_NE(w->freeLastRow(), OK);
94 }
95
96 TEST(CursorWindowTest, AllocRowBounds) {
97 CREATE_WINDOW_1K;
98
99 // 60 columns is 960 bytes, which means only a single row can fit
100 ASSERT_EQ(w->setNumColumns(60), OK);
101 ASSERT_EQ(w->allocRow(), OK);
102 ASSERT_NE(w->allocRow(), OK);
103 }
104
105 TEST(CursorWindowTest, StoreNull) {
106 CREATE_WINDOW_1K_3X3;
107
108 ASSERT_EQ(w->putNull(1, 1), OK);
109 ASSERT_EQ(w->putNull(0, 0), OK);
110
111 {
112 auto field = w->getFieldSlot(1, 1);
113 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_NULL);
114 }
115 {
116 auto field = w->getFieldSlot(0, 0);
117 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_NULL);
118 }
119 }
120
121 TEST(CursorWindowTest, StoreLong) {
122 CREATE_WINDOW_1K_3X3;
123
124 ASSERT_EQ(w->putLong(1, 1, 0xf00d), OK);
125 ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
126
127 {
128 auto field = w->getFieldSlot(1, 1);
129 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_INTEGER);
130 ASSERT_EQ(w->getFieldSlotValueLong(field), 0xf00d);
131 }
132 {
133 auto field = w->getFieldSlot(0, 0);
134 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_INTEGER);
135 ASSERT_EQ(w->getFieldSlotValueLong(field), 0xcafe);
136 }
137 }
138
139 TEST(CursorWindowTest, StoreString) {
140 CREATE_WINDOW_1K_3X3;
141
142 ASSERT_EQ(w->putString(1, 1, "food", 5), OK);
143 ASSERT_EQ(w->putString(0, 0, "cafe", 5), OK);
144
145 size_t size;
146 {
147 auto field = w->getFieldSlot(1, 1);
148 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_STRING);
149 auto actual = w->getFieldSlotValueString(field, &size);
150 ASSERT_EQ(std::string(actual), "food");
151 }
152 {
153 auto field = w->getFieldSlot(0, 0);
154 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_STRING);
155 auto actual = w->getFieldSlotValueString(field, &size);
156 ASSERT_EQ(std::string(actual), "cafe");
157 }
158 }
159
160 TEST(CursorWindowTest, StoreBounds) {
161 CREATE_WINDOW_1K_3X3;
162
163 // Can't work with values beyond bounds
164 ASSERT_NE(w->putLong(0, 3, 0xcafe), OK);
165 ASSERT_NE(w->putLong(3, 0, 0xcafe), OK);
166 ASSERT_NE(w->putLong(3, 3, 0xcafe), OK);
167 ASSERT_EQ(w->getFieldSlot(0, 3), nullptr);
168 ASSERT_EQ(w->getFieldSlot(3, 0), nullptr);
169 ASSERT_EQ(w->getFieldSlot(3, 3), nullptr);
170
171 // Can't work with invalid indexes
172 ASSERT_NE(w->putLong(-1, 0, 0xcafe), OK);
173 ASSERT_NE(w->putLong(0, -1, 0xcafe), OK);
174 ASSERT_NE(w->putLong(-1, -1, 0xcafe), OK);
175 ASSERT_EQ(w->getFieldSlot(-1, 0), nullptr);
176 ASSERT_EQ(w->getFieldSlot(0, -1), nullptr);
177 ASSERT_EQ(w->getFieldSlot(-1, -1), nullptr);
178 }
179
180 TEST(CursorWindowTest, Inflate) {
181 CREATE_WINDOW_2M;
182
183 auto before = w->size();
184 ASSERT_EQ(w->setNumColumns(4), OK);
185 ASSERT_EQ(w->allocRow(), OK);
186
187 // Scratch buffer that will fit before inflation
188 char buf[kHalfInlineSize];
189
190 // Store simple value
191 ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
192
193 // Store first object that fits inside
194 memset(buf, 42, kHalfInlineSize);
195 ASSERT_EQ(w->putBlob(0, 1, buf, kHalfInlineSize), OK);
196 ASSERT_EQ(w->size(), before);
197
198 // Store second simple value
199 ASSERT_EQ(w->putLong(0, 2, 0xface), OK);
200
201 // Store second object that requires inflation
202 memset(buf, 84, kHalfInlineSize);
203 ASSERT_EQ(w->putBlob(0, 3, buf, kHalfInlineSize), OK);
204 ASSERT_GT(w->size(), before);
205
206 // Verify data is intact
207 {
208 auto field = w->getFieldSlot(0, 0);
209 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_INTEGER);
210 ASSERT_EQ(w->getFieldSlotValueLong(field), 0xcafe);
211 }
212 {
213 auto field = w->getFieldSlot(0, 1);
214 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_BLOB);
215 size_t actualSize;
216 auto actual = w->getFieldSlotValueBlob(field, &actualSize);
217 ASSERT_EQ(actualSize, kHalfInlineSize);
218 memset(buf, 42, kHalfInlineSize);
219 ASSERT_NE(actual, buf);
220 ASSERT_EQ(memcmp(buf, actual, kHalfInlineSize), 0);
221 }
222 {
223 auto field = w->getFieldSlot(0, 2);
224 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_INTEGER);
225 ASSERT_EQ(w->getFieldSlotValueLong(field), 0xface);
226 }
227 {
228 auto field = w->getFieldSlot(0, 3);
229 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_BLOB);
230 size_t actualSize;
231 auto actual = w->getFieldSlotValueBlob(field, &actualSize);
232 ASSERT_EQ(actualSize, kHalfInlineSize);
233 memset(buf, 84, kHalfInlineSize);
234 ASSERT_NE(actual, buf);
235 ASSERT_EQ(memcmp(buf, actual, kHalfInlineSize), 0);
236 }
237 }
238
239 TEST(CursorWindowTest, ParcelEmpty) {
240 CREATE_WINDOW_2M;
241
242 Parcel p;
243 w->writeToParcel(&p);
244 p.setDataPosition(0);
245 w = nullptr;
246
247 ASSERT_EQ(CursorWindow::createFromParcel(&p, &w), OK);
248 ASSERT_EQ(w->getNumRows(), 0);
249 ASSERT_EQ(w->getNumColumns(), 0);
250 ASSERT_EQ(w->size(), 0);
251 ASSERT_EQ(w->freeSpace(), 0);
252
253 // We can't mutate the window after parceling
254 ASSERT_NE(w->setNumColumns(4), OK);
255 ASSERT_NE(w->allocRow(), OK);
256 }
257
258 TEST(CursorWindowTest, ParcelSmall) {
259 CREATE_WINDOW_2M;
260
261 auto before = w->size();
262 ASSERT_EQ(w->setNumColumns(4), OK);
263 ASSERT_EQ(w->allocRow(), OK);
264
265 // Scratch buffer that will fit before inflation
266 char buf[kHalfInlineSize];
267
268 // Store simple value
269 ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
270
271 // Store first object that fits inside
272 memset(buf, 42, kHalfInlineSize);
273 ASSERT_EQ(w->putBlob(0, 1, buf, kHalfInlineSize), OK);
274 ASSERT_EQ(w->size(), before);
275
276 // Store second object with zero length
277 ASSERT_EQ(w->putBlob(0, 2, buf, 0), OK);
278 ASSERT_EQ(w->size(), before);
279
280 // Force through a parcel
281 Parcel p;
282 w->writeToParcel(&p);
283 p.setDataPosition(0);
284 w = nullptr;
285
286 ASSERT_EQ(CursorWindow::createFromParcel(&p, &w), OK);
287 ASSERT_EQ(w->getNumRows(), 1);
288 ASSERT_EQ(w->getNumColumns(), 4);
289
290 // Verify data is intact
291 {
292 auto field = w->getFieldSlot(0, 0);
293 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_INTEGER);
294 ASSERT_EQ(w->getFieldSlotValueLong(field), 0xcafe);
295 }
296 {
297 auto field = w->getFieldSlot(0, 1);
298 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_BLOB);
299 size_t actualSize;
300 auto actual = w->getFieldSlotValueBlob(field, &actualSize);
301 ASSERT_EQ(actualSize, kHalfInlineSize);
302 memset(buf, 42, kHalfInlineSize);
303 ASSERT_NE(actual, buf);
304 ASSERT_EQ(memcmp(buf, actual, kHalfInlineSize), 0);
305 }
306 {
307 auto field = w->getFieldSlot(0, 2);
308 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_BLOB);
309 size_t actualSize;
310 auto actual = w->getFieldSlotValueBlob(field, &actualSize);
311 ASSERT_EQ(actualSize, 0);
312 ASSERT_NE(actual, nullptr);
313 }
314 }
315
316 TEST(CursorWindowTest, ParcelLarge) {
317 CREATE_WINDOW_2M;
318
319 ASSERT_EQ(w->setNumColumns(4), OK);
320 ASSERT_EQ(w->allocRow(), OK);
321
322 // Store simple value
323 ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
324
325 // Store object that forces inflation
326 std::unique_ptr<char> bufPtr(new char[kGiantSize]);
327 void* buf = bufPtr.get();
328 memset(buf, 42, kGiantSize);
329 ASSERT_EQ(w->putBlob(0, 1, buf, kGiantSize), OK);
330
331 // Store second object with zero length
332 ASSERT_EQ(w->putBlob(0, 2, buf, 0), OK);
333
334 // Force through a parcel
335 Parcel p;
336 w->writeToParcel(&p);
337 p.setDataPosition(0);
338 w = nullptr;
339
340 ASSERT_EQ(CursorWindow::createFromParcel(&p, &w), OK);
341 ASSERT_EQ(w->getNumRows(), 1);
342 ASSERT_EQ(w->getNumColumns(), 4);
343
344 // Verify data is intact
345 {
346 auto field = w->getFieldSlot(0, 0);
347 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_INTEGER);
348 ASSERT_EQ(w->getFieldSlotValueLong(field), 0xcafe);
349 }
350 {
351 auto field = w->getFieldSlot(0, 1);
352 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_BLOB);
353 size_t actualSize;
354 auto actual = w->getFieldSlotValueBlob(field, &actualSize);
355 ASSERT_EQ(actualSize, kGiantSize);
356 memset(buf, 42, kGiantSize);
357 ASSERT_EQ(memcmp(buf, actual, kGiantSize), 0);
358 }
359 {
360 auto field = w->getFieldSlot(0, 2);
361 ASSERT_EQ(w->getFieldSlotType(field), CursorWindow::FIELD_TYPE_BLOB);
362 size_t actualSize;
363 auto actual = w->getFieldSlotValueBlob(field, &actualSize);
364 ASSERT_EQ(actualSize, 0);
365 ASSERT_NE(actual, nullptr);
366 }
367 }
368
369 } // android