blob: 79cb2af52dbc411a009a89fc89c5a8ad7947f9a3 [file] [log] [blame]
Andy Hungc89c8dc2019-10-16 17:48:21 -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#define LOG_TAG "mediametrics_tests"
18#include <utils/Log.h>
19
Ray Essick40e8e5e2019-12-05 20:19:40 -080020#include "MediaMetricsService.h"
Andy Hungc89c8dc2019-10-16 17:48:21 -070021
22#include <stdio.h>
23
24#include <gtest/gtest.h>
Ray Essickf27e9872019-12-07 06:28:46 -080025#include <media/MediaMetricsItem.h>
Andy Hungc89c8dc2019-10-16 17:48:21 -070026
27using namespace android;
28
Andy Hung06f3aba2019-12-03 16:36:42 -080029static size_t countNewlines(const char *s) {
30 size_t count = 0;
31 while ((s = strchr(s, '\n')) != nullptr) {
32 ++s;
33 ++count;
34 }
35 return count;
36}
37
Andy Hungc89c8dc2019-10-16 17:48:21 -070038TEST(mediametrics_tests, instantiate) {
Ray Essickf27e9872019-12-07 06:28:46 -080039 sp mediaMetrics = new MediaMetricsService();
Andy Hungc89c8dc2019-10-16 17:48:21 -070040 status_t status;
41
Andy Hungc89c8dc2019-10-16 17:48:21 -070042 // random keys ignored when empty
Ray Essickf27e9872019-12-07 06:28:46 -080043 std::unique_ptr<mediametrics::Item> random_key(mediametrics::Item::create("random_key"));
Andy Hunga87e69c2019-10-18 10:07:40 -070044 status = mediaMetrics->submit(random_key.get());
45 ASSERT_EQ(PERMISSION_DENIED, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070046
47 // random keys ignored with data
Andy Hungc89c8dc2019-10-16 17:48:21 -070048 random_key->setInt32("foo", 10);
Andy Hunga87e69c2019-10-18 10:07:40 -070049 status = mediaMetrics->submit(random_key.get());
50 ASSERT_EQ(PERMISSION_DENIED, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070051
52 // known keys ignored if empty
Ray Essickf27e9872019-12-07 06:28:46 -080053 std::unique_ptr<mediametrics::Item> audiotrack_key(mediametrics::Item::create("audiotrack"));
Andy Hunga87e69c2019-10-18 10:07:40 -070054 status = mediaMetrics->submit(audiotrack_key.get());
55 ASSERT_EQ(BAD_VALUE, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070056
Andy Hunga87e69c2019-10-18 10:07:40 -070057 // known keys not ignored if not empty
58 audiotrack_key->addInt32("foo", 10);
59 status = mediaMetrics->submit(audiotrack_key.get());
60 ASSERT_EQ(NO_ERROR, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070061
Andy Hungaeef7882019-10-18 15:18:14 -070062
63 /*
64 // fluent style that goes directly to mediametrics
Ray Essickf27e9872019-12-07 06:28:46 -080065 ASSERT_EQ(true, mediametrics::Item("audiorecord")
Andy Hungaeef7882019-10-18 15:18:14 -070066 .setInt32("value", 2)
67 .addInt32("bar", 1)
68 .addInt32("value", 3)
69 .selfrecord());
70 */
71
Andy Hungc89c8dc2019-10-16 17:48:21 -070072 mediaMetrics->dump(fileno(stdout), {} /* args */);
73}
Andy Hungaeef7882019-10-18 15:18:14 -070074
75TEST(mediametrics_tests, item_manipulation) {
Ray Essickf27e9872019-12-07 06:28:46 -080076 mediametrics::Item item("audiorecord");
Andy Hungaeef7882019-10-18 15:18:14 -070077
78 item.setInt32("value", 2).addInt32("bar", 3).addInt32("value", 4);
79
80 int32_t i32;
81 ASSERT_TRUE(item.getInt32("value", &i32));
82 ASSERT_EQ(6, i32);
83
84 ASSERT_TRUE(item.getInt32("bar", &i32));
85 ASSERT_EQ(3, i32);
86
87 item.setInt64("big", INT64_MAX).setInt64("smaller", INT64_MAX - 1).addInt64("smaller", -2);
88
89 int64_t i64;
90 ASSERT_TRUE(item.getInt64("big", &i64));
91 ASSERT_EQ(INT64_MAX, i64);
92
93 ASSERT_TRUE(item.getInt64("smaller", &i64));
94 ASSERT_EQ(INT64_MAX - 3, i64);
95
96 item.setDouble("precise", 10.5).setDouble("small", 0.125).addDouble("precise", 0.25);
97
98 double d;
99 ASSERT_TRUE(item.getDouble("precise", &d));
100 ASSERT_EQ(10.75, d);
101
102 ASSERT_TRUE(item.getDouble("small", &d));
103 ASSERT_EQ(0.125, d);
104
105 char *s;
106 item.setCString("name", "Frank").setCString("mother", "June").setCString("mother", "July");
107 ASSERT_TRUE(item.getCString("name", &s));
108 ASSERT_EQ(0, strcmp(s, "Frank"));
109 free(s);
110
111 ASSERT_TRUE(item.getCString("mother", &s));
112 ASSERT_EQ(0, strcmp(s, "July")); // "July" overwrites "June"
113 free(s);
114
115 item.setRate("burgersPerHour", 5, 2);
116 int64_t b, h;
117 ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d));
118 ASSERT_EQ(5, b);
119 ASSERT_EQ(2, h);
120 ASSERT_EQ(2.5, d);
121
122 item.addRate("burgersPerHour", 4, 2);
123 ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d));
124 ASSERT_EQ(9, b);
125 ASSERT_EQ(4, h);
126 ASSERT_EQ(2.25, d);
127
128 printf("item: %s\n", item.toString().c_str());
129 fflush(stdout);
130
Ray Essickf27e9872019-12-07 06:28:46 -0800131 sp mediaMetrics = new MediaMetricsService();
Andy Hungaeef7882019-10-18 15:18:14 -0700132 status_t status = mediaMetrics->submit(&item);
133 ASSERT_EQ(NO_ERROR, status);
134 mediaMetrics->dump(fileno(stdout), {} /* args */);
135}
136
137TEST(mediametrics_tests, superbig_item) {
Ray Essickf27e9872019-12-07 06:28:46 -0800138 mediametrics::Item item("TheBigOne");
Andy Hungaeef7882019-10-18 15:18:14 -0700139 constexpr size_t count = 10000;
140
141 for (size_t i = 0; i < count; ++i) {
142 item.setInt32(std::to_string(i).c_str(), i);
143 }
144 for (size_t i = 0; i < count; ++i) {
145 int32_t i32;
146 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
147 ASSERT_EQ((int32_t)i, i32);
148 }
149}
150
151TEST(mediametrics_tests, superbig_item_removal) {
Ray Essickf27e9872019-12-07 06:28:46 -0800152 mediametrics::Item item("TheOddBigOne");
Andy Hungaeef7882019-10-18 15:18:14 -0700153 constexpr size_t count = 10000;
154
155 for (size_t i = 0; i < count; ++i) {
156 item.setInt32(std::to_string(i).c_str(), i);
157 }
158 for (size_t i = 0; i < count; i += 2) {
159 item.filter(std::to_string(i).c_str()); // filter out all the evens.
160 }
161 for (size_t i = 0; i < count; ++i) {
162 int32_t i32;
163 if (i & 1) { // check to see that only the odds are left.
164 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
165 ASSERT_EQ((int32_t)i, i32);
166 } else {
167 ASSERT_FALSE(item.getInt32(std::to_string(i).c_str(), &i32));
168 }
169 }
170}
171
Andy Hung3253f2d2019-10-21 14:50:07 -0700172TEST(mediametrics_tests, superbig_item_removal2) {
Ray Essickf27e9872019-12-07 06:28:46 -0800173 mediametrics::Item item("TheOne");
Andy Hung3253f2d2019-10-21 14:50:07 -0700174 constexpr size_t count = 10000;
175
176 for (size_t i = 0; i < count; ++i) {
177 item.setInt32(std::to_string(i).c_str(), i);
178 }
179 static const char *attrs[] = { "1", };
180 item.filterNot(1, attrs);
181
182 for (size_t i = 0; i < count; ++i) {
183 int32_t i32;
184 if (i == 1) { // check to see that there is only one
185 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
186 ASSERT_EQ((int32_t)i, i32);
187 } else {
188 ASSERT_FALSE(item.getInt32(std::to_string(i).c_str(), &i32));
189 }
190 }
191}
192
Andy Hungaeef7882019-10-18 15:18:14 -0700193TEST(mediametrics_tests, item_transmutation) {
Ray Essickf27e9872019-12-07 06:28:46 -0800194 mediametrics::Item item("Alchemist's Stone");
Andy Hungaeef7882019-10-18 15:18:14 -0700195
196 item.setInt64("convert", 123);
197 int64_t i64;
198 ASSERT_TRUE(item.getInt64("convert", &i64));
199 ASSERT_EQ(123, i64);
200
201 item.addInt32("convert", 2); // changes type of 'convert' from i64 to i32 (and re-init).
202 ASSERT_FALSE(item.getInt64("convert", &i64)); // should be false, no value in i64.
203
204 int32_t i32;
205 ASSERT_TRUE(item.getInt32("convert", &i32)); // check it is i32 and 2 (123 is discarded).
206 ASSERT_EQ(2, i32);
207}
Andy Hung3253f2d2019-10-21 14:50:07 -0700208
209TEST(mediametrics_tests, item_binderization) {
Ray Essickf27e9872019-12-07 06:28:46 -0800210 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700211 item.setInt32("i32", 1)
212 .setInt64("i64", 2)
213 .setDouble("double", 3.1)
214 .setCString("string", "abc")
215 .setRate("rate", 11, 12);
216
217 Parcel p;
218 item.writeToParcel(&p);
219
220 p.setDataPosition(0); // rewind for reading
Ray Essickf27e9872019-12-07 06:28:46 -0800221 mediametrics::Item item2;
Andy Hung3253f2d2019-10-21 14:50:07 -0700222 item2.readFromParcel(p);
223
224 ASSERT_EQ(item, item2);
225}
226
227TEST(mediametrics_tests, item_byteserialization) {
Ray Essickf27e9872019-12-07 06:28:46 -0800228 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700229 item.setInt32("i32", 1)
230 .setInt64("i64", 2)
231 .setDouble("double", 3.1)
232 .setCString("string", "abc")
233 .setRate("rate", 11, 12);
234
235 char *data;
236 size_t length;
237 ASSERT_EQ(0, item.writeToByteString(&data, &length));
238 ASSERT_GT(length, (size_t)0);
239
Ray Essickf27e9872019-12-07 06:28:46 -0800240 mediametrics::Item item2;
Andy Hung3253f2d2019-10-21 14:50:07 -0700241 item2.readFromByteString(data, length);
242
243 printf("item: %s\n", item.toString().c_str());
244 printf("item2: %s\n", item2.toString().c_str());
245 ASSERT_EQ(item, item2);
246
247 free(data);
248}
249
250TEST(mediametrics_tests, item_iteration) {
Ray Essickf27e9872019-12-07 06:28:46 -0800251 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700252 item.setInt32("i32", 1)
253 .setInt64("i64", 2)
254 .setDouble("double", 3.125)
255 .setCString("string", "abc")
256 .setRate("rate", 11, 12);
257
258 int mask = 0;
259 for (auto &prop : item) {
260 const char *name = prop.getName();
261 if (!strcmp(name, "i32")) {
262 int32_t i32;
263 ASSERT_TRUE(prop.get(&i32));
264 ASSERT_EQ(1, i32);
Andy Hung692870b2020-01-02 13:46:06 -0800265 ASSERT_EQ(1, std::get<int32_t>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700266 mask |= 1;
267 } else if (!strcmp(name, "i64")) {
268 int64_t i64;
269 ASSERT_TRUE(prop.get(&i64));
270 ASSERT_EQ(2, i64);
Andy Hung692870b2020-01-02 13:46:06 -0800271 ASSERT_EQ(2, std::get<int64_t>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700272 mask |= 2;
273 } else if (!strcmp(name, "double")) {
274 double d;
275 ASSERT_TRUE(prop.get(&d));
276 ASSERT_EQ(3.125, d);
Andy Hung692870b2020-01-02 13:46:06 -0800277 ASSERT_EQ(3.125, std::get<double>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700278 mask |= 4;
279 } else if (!strcmp(name, "string")) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800280 std::string s;
Andy Hung3253f2d2019-10-21 14:50:07 -0700281 ASSERT_TRUE(prop.get(&s));
Andy Hungb7aadb32019-12-09 19:40:42 -0800282 ASSERT_EQ("abc", s);
Andy Hung692870b2020-01-02 13:46:06 -0800283 ASSERT_EQ(s, std::get<std::string>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700284 mask |= 8;
285 } else if (!strcmp(name, "rate")) {
286 std::pair<int64_t, int64_t> r;
287 ASSERT_TRUE(prop.get(&r));
288 ASSERT_EQ(11, r.first);
289 ASSERT_EQ(12, r.second);
Andy Hung692870b2020-01-02 13:46:06 -0800290 ASSERT_EQ(r, std::get<decltype(r)>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700291 mask |= 16;
292 } else {
293 FAIL();
294 }
295 }
296 ASSERT_EQ(31, mask);
297}
Andy Hung1efc9c62019-12-03 13:43:33 -0800298
299TEST(mediametrics_tests, item_expansion) {
Ray Essickf27e9872019-12-07 06:28:46 -0800300 mediametrics::LogItem<1> item("I");
Andy Hung1efc9c62019-12-03 13:43:33 -0800301 item.set("i32", (int32_t)1)
302 .set("i64", (int64_t)2)
303 .set("double", (double)3.125)
304 .set("string", "abcdefghijklmnopqrstuvwxyz")
305 .set("rate", std::pair<int64_t, int64_t>(11, 12));
306 ASSERT_TRUE(item.updateHeader());
307
Ray Essickf27e9872019-12-07 06:28:46 -0800308 mediametrics::Item item2;
Andy Hung1efc9c62019-12-03 13:43:33 -0800309 item2.readFromByteString(item.getBuffer(), item.getLength());
310 ASSERT_EQ((pid_t)-1, item2.getPid());
311 ASSERT_EQ((uid_t)-1, item2.getUid());
312 int mask = 0;
313 for (auto &prop : item2) {
314 const char *name = prop.getName();
315 if (!strcmp(name, "i32")) {
316 int32_t i32;
317 ASSERT_TRUE(prop.get(&i32));
318 ASSERT_EQ(1, i32);
319 mask |= 1;
320 } else if (!strcmp(name, "i64")) {
321 int64_t i64;
322 ASSERT_TRUE(prop.get(&i64));
323 ASSERT_EQ(2, i64);
324 mask |= 2;
325 } else if (!strcmp(name, "double")) {
326 double d;
327 ASSERT_TRUE(prop.get(&d));
328 ASSERT_EQ(3.125, d);
329 mask |= 4;
330 } else if (!strcmp(name, "string")) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800331 std::string s;
Andy Hung1efc9c62019-12-03 13:43:33 -0800332 ASSERT_TRUE(prop.get(&s));
Andy Hungb7aadb32019-12-09 19:40:42 -0800333 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
Andy Hung1efc9c62019-12-03 13:43:33 -0800334 mask |= 8;
335 } else if (!strcmp(name, "rate")) {
336 std::pair<int64_t, int64_t> r;
337 ASSERT_TRUE(prop.get(&r));
338 ASSERT_EQ(11, r.first);
339 ASSERT_EQ(12, r.second);
340 mask |= 16;
341 } else {
342 FAIL();
343 }
344 }
345 ASSERT_EQ(31, mask);
346}
347
348TEST(mediametrics_tests, item_expansion2) {
Ray Essickf27e9872019-12-07 06:28:46 -0800349 mediametrics::LogItem<1> item("Bigly");
Andy Hung1efc9c62019-12-03 13:43:33 -0800350 item.setPid(123)
351 .setUid(456);
352 constexpr size_t count = 10000;
353
354 for (size_t i = 0; i < count; ++i) {
355 // printf("recording %zu, %p, len:%zu of %zu remaining:%zu \n", i, item.getBuffer(), item.getLength(), item.getCapacity(), item.getRemaining());
356 item.set(std::to_string(i).c_str(), (int32_t)i);
357 }
358 ASSERT_TRUE(item.updateHeader());
359
Ray Essickf27e9872019-12-07 06:28:46 -0800360 mediametrics::Item item2;
Andy Hung1efc9c62019-12-03 13:43:33 -0800361 printf("begin buffer:%p length:%zu\n", item.getBuffer(), item.getLength());
362 fflush(stdout);
363 item2.readFromByteString(item.getBuffer(), item.getLength());
364
365 ASSERT_EQ((pid_t)123, item2.getPid());
366 ASSERT_EQ((uid_t)456, item2.getUid());
367 for (size_t i = 0; i < count; ++i) {
368 int32_t i32;
369 ASSERT_TRUE(item2.getInt32(std::to_string(i).c_str(), &i32));
370 ASSERT_EQ((int32_t)i, i32);
371 }
372}
Andy Hung06f3aba2019-12-03 16:36:42 -0800373
374TEST(mediametrics_tests, time_machine_storage) {
Ray Essickf27e9872019-12-07 06:28:46 -0800375 auto item = std::make_shared<mediametrics::Item>("Key");
Andy Hung06f3aba2019-12-03 16:36:42 -0800376 (*item).set("i32", (int32_t)1)
377 .set("i64", (int64_t)2)
378 .set("double", (double)3.125)
379 .set("string", "abcdefghijklmnopqrstuvwxyz")
380 .set("rate", std::pair<int64_t, int64_t>(11, 12));
381
382 // Let's put the item in
383 android::mediametrics::TimeMachine timeMachine;
384 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
385
386 // Can we read the values?
387 int32_t i32;
388 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i32", &i32, -1));
389 ASSERT_EQ(1, i32);
390
391 int64_t i64;
392 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i64", &i64, -1));
393 ASSERT_EQ(2, i64);
394
395 double d;
396 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "double", &d, -1));
397 ASSERT_EQ(3.125, d);
398
399 std::string s;
400 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "string", &s, -1));
401 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
402
403 // Using fully qualified name?
404 i32 = 0;
405 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i32", &i32, -1));
406 ASSERT_EQ(1, i32);
407
408 i64 = 0;
409 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i64", &i64, -1));
410 ASSERT_EQ(2, i64);
411
412 d = 0.;
413 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.double", &d, -1));
414 ASSERT_EQ(3.125, d);
415
416 s.clear();
417 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.string", &s, -1));
418 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
419}
420
421TEST(mediametrics_tests, time_machine_remote_key) {
Ray Essickf27e9872019-12-07 06:28:46 -0800422 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800423 (*item).set("one", (int32_t)1)
424 .set("two", (int32_t)2);
425
426 android::mediametrics::TimeMachine timeMachine;
427 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
428
Ray Essickf27e9872019-12-07 06:28:46 -0800429 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800430 (*item2).set("three", (int32_t)3)
431 .set("[Key1]four", (int32_t)4) // affects Key1
432 .set("[Key1]five", (int32_t)5); // affects key1
433
434 ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true));
435
Ray Essickf27e9872019-12-07 06:28:46 -0800436 auto item3 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800437 (*item3).set("six", (int32_t)6)
438 .set("[Key1]seven", (int32_t)7); // affects Key1
439
440 ASSERT_EQ(NO_ERROR, timeMachine.put(item3, false)); // remote keys not allowed.
441
442 // Can we read the values?
443 int32_t i32;
444 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.one", &i32, -1));
445 ASSERT_EQ(1, i32);
446
447 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.two", &i32, -1));
448 ASSERT_EQ(2, i32);
449
450 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1));
451
452 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1));
453 ASSERT_EQ(3, i32);
454
455 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.four", &i32, -1));
456 ASSERT_EQ(4, i32);
457
458 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.four", &i32, -1));
459
460 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.five", &i32, -1));
461 ASSERT_EQ(5, i32);
462
463 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.five", &i32, -1));
464
465 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.six", &i32, -1));
466 ASSERT_EQ(6, i32);
467
468 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.seven", &i32, -1));
469}
470
471TEST(mediametrics_tests, time_machine_gc) {
Ray Essickf27e9872019-12-07 06:28:46 -0800472 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800473 (*item).set("one", (int32_t)1)
474 .set("two", (int32_t)2)
475 .setTimestamp(10);
476
477 android::mediametrics::TimeMachine timeMachine(1, 2); // keep at most 2 keys.
478
479 ASSERT_EQ((size_t)0, timeMachine.size());
480
481 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
482
483 ASSERT_EQ((size_t)1, timeMachine.size());
484
Ray Essickf27e9872019-12-07 06:28:46 -0800485 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800486 (*item2).set("three", (int32_t)3)
487 .set("[Key1]three", (int32_t)3)
488 .setTimestamp(11);
489
490 ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true));
491 ASSERT_EQ((size_t)2, timeMachine.size());
492
493 //printf("Before\n%s\n\n", timeMachine.dump().c_str());
494
Ray Essickf27e9872019-12-07 06:28:46 -0800495 auto item3 = std::make_shared<mediametrics::Item>("Key3");
Andy Hung06f3aba2019-12-03 16:36:42 -0800496 (*item3).set("six", (int32_t)6)
497 .set("[Key1]four", (int32_t)4) // affects Key1
498 .set("[Key1]five", (int32_t)5) // affects key1
499 .setTimestamp(12);
500
501 ASSERT_EQ(NO_ERROR, timeMachine.put(item3, true));
502
503 ASSERT_EQ((size_t)2, timeMachine.size());
504
505 // Can we read the values?
506 int32_t i32;
507 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.one", &i32, -1));
508 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.two", &i32, -1));
509 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1));
510 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.four", &i32, -1));
511 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.five", &i32, -1));
512
513 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1));
514 ASSERT_EQ(3, i32);
515
516 ASSERT_EQ(NO_ERROR, timeMachine.get("Key3.six", &i32, -1));
517 ASSERT_EQ(6, i32);
518
519 printf("After\n%s\n", timeMachine.dump().first.c_str());
520}
521
522TEST(mediametrics_tests, transaction_log_gc) {
Ray Essickf27e9872019-12-07 06:28:46 -0800523 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800524 (*item).set("one", (int32_t)1)
525 .set("two", (int32_t)2)
526 .setTimestamp(10);
527
528 android::mediametrics::TransactionLog transactionLog(1, 2); // keep at most 2 items
529 ASSERT_EQ((size_t)0, transactionLog.size());
530
531 ASSERT_EQ(NO_ERROR, transactionLog.put(item));
532 ASSERT_EQ((size_t)1, transactionLog.size());
533
Ray Essickf27e9872019-12-07 06:28:46 -0800534 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800535 (*item2).set("three", (int32_t)3)
536 .set("[Key1]three", (int32_t)3)
537 .setTimestamp(11);
538
539 ASSERT_EQ(NO_ERROR, transactionLog.put(item2));
540 ASSERT_EQ((size_t)2, transactionLog.size());
541
Ray Essickf27e9872019-12-07 06:28:46 -0800542 auto item3 = std::make_shared<mediametrics::Item>("Key3");
Andy Hung06f3aba2019-12-03 16:36:42 -0800543 (*item3).set("six", (int32_t)6)
544 .set("[Key1]four", (int32_t)4) // affects Key1
545 .set("[Key1]five", (int32_t)5) // affects key1
546 .setTimestamp(12);
547
548 ASSERT_EQ(NO_ERROR, transactionLog.put(item3));
549 ASSERT_EQ((size_t)2, transactionLog.size());
550}
551
552TEST(mediametrics_tests, audio_analytics_permission) {
Ray Essickf27e9872019-12-07 06:28:46 -0800553 auto item = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800554 (*item).set("one", (int32_t)1)
555 .set("two", (int32_t)2)
556 .setTimestamp(10);
557
Ray Essickf27e9872019-12-07 06:28:46 -0800558 auto item2 = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800559 (*item2).set("three", (int32_t)3)
560 .setTimestamp(11);
561
Ray Essickf27e9872019-12-07 06:28:46 -0800562 auto item3 = std::make_shared<mediametrics::Item>("audio.2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800563 (*item3).set("four", (int32_t)4)
564 .setTimestamp(12);
565
566 android::mediametrics::AudioAnalytics audioAnalytics;
567
568 // untrusted entities cannot create a new key.
569 ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item, false /* isTrusted */));
570 ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item2, false /* isTrusted */));
571
572 // TODO: Verify contents of AudioAnalytics.
573 // Currently there is no getter API in AudioAnalytics besides dump.
574 ASSERT_EQ(4, audioAnalytics.dump(1000).second /* lines */);
575
576 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */));
577 // untrusted entities can add to an existing key
578 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */));
579
580 // Check that we have some info in the dump.
581 ASSERT_LT(4, audioAnalytics.dump(1000).second /* lines */);
582}
583
584TEST(mediametrics_tests, audio_analytics_dump) {
Ray Essickf27e9872019-12-07 06:28:46 -0800585 auto item = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800586 (*item).set("one", (int32_t)1)
587 .set("two", (int32_t)2)
588 .setTimestamp(10);
589
Ray Essickf27e9872019-12-07 06:28:46 -0800590 auto item2 = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800591 (*item2).set("three", (int32_t)3)
592 .setTimestamp(11);
593
Ray Essickf27e9872019-12-07 06:28:46 -0800594 auto item3 = std::make_shared<mediametrics::Item>("audio.2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800595 (*item3).set("four", (int32_t)4)
596 .setTimestamp(12);
597
598 android::mediametrics::AudioAnalytics audioAnalytics;
599
600 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */));
601 // untrusted entities can add to an existing key
602 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */));
603 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item3, true /* isTrusted */));
604
605 // find out how many lines we have.
606 auto [string, lines] = audioAnalytics.dump(1000);
607 ASSERT_EQ(lines, (int32_t) countNewlines(string.c_str()));
608
609 printf("AudioAnalytics: %s", string.c_str());
610 // ensure that dump operates over those lines.
611 for (int32_t ll = 0; ll < lines; ++ll) {
612 auto [s, l] = audioAnalytics.dump(ll);
613 ASSERT_EQ(ll, l);
614 ASSERT_EQ(ll, (int32_t) countNewlines(s.c_str()));
615 }
616}