blob: ccf2d634f6983661aaa172323fb2977412f52b6e [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 Hung37de9b72020-01-02 13:54:05 -080038TEST(mediametrics_tests, startsWith) {
39 std::string s("test");
40 ASSERT_EQ(true, android::mediametrics::startsWith(s, "te"));
41 ASSERT_EQ(true, android::mediametrics::startsWith(s, std::string("tes")));
42 ASSERT_EQ(false, android::mediametrics::startsWith(s, "ts"));
43 ASSERT_EQ(false, android::mediametrics::startsWith(s, std::string("est")));
44}
45
46TEST(mediametrics_tests, defer) {
47 bool check = false;
48 {
49 android::mediametrics::Defer defer([&] { check = true; });
50 ASSERT_EQ(false, check);
51 }
52 ASSERT_EQ(true, check);
53}
54
Andy Hungc89c8dc2019-10-16 17:48:21 -070055TEST(mediametrics_tests, instantiate) {
Ray Essickf27e9872019-12-07 06:28:46 -080056 sp mediaMetrics = new MediaMetricsService();
Andy Hungc89c8dc2019-10-16 17:48:21 -070057 status_t status;
58
Andy Hungc89c8dc2019-10-16 17:48:21 -070059 // random keys ignored when empty
Ray Essickf27e9872019-12-07 06:28:46 -080060 std::unique_ptr<mediametrics::Item> random_key(mediametrics::Item::create("random_key"));
Andy Hunga87e69c2019-10-18 10:07:40 -070061 status = mediaMetrics->submit(random_key.get());
62 ASSERT_EQ(PERMISSION_DENIED, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070063
64 // random keys ignored with data
Andy Hungc89c8dc2019-10-16 17:48:21 -070065 random_key->setInt32("foo", 10);
Andy Hunga87e69c2019-10-18 10:07:40 -070066 status = mediaMetrics->submit(random_key.get());
67 ASSERT_EQ(PERMISSION_DENIED, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070068
69 // known keys ignored if empty
Ray Essickf27e9872019-12-07 06:28:46 -080070 std::unique_ptr<mediametrics::Item> audiotrack_key(mediametrics::Item::create("audiotrack"));
Andy Hunga87e69c2019-10-18 10:07:40 -070071 status = mediaMetrics->submit(audiotrack_key.get());
72 ASSERT_EQ(BAD_VALUE, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070073
Andy Hunga87e69c2019-10-18 10:07:40 -070074 // known keys not ignored if not empty
75 audiotrack_key->addInt32("foo", 10);
76 status = mediaMetrics->submit(audiotrack_key.get());
77 ASSERT_EQ(NO_ERROR, status);
Andy Hungc89c8dc2019-10-16 17:48:21 -070078
Andy Hungaeef7882019-10-18 15:18:14 -070079
80 /*
81 // fluent style that goes directly to mediametrics
Ray Essickf27e9872019-12-07 06:28:46 -080082 ASSERT_EQ(true, mediametrics::Item("audiorecord")
Andy Hungaeef7882019-10-18 15:18:14 -070083 .setInt32("value", 2)
84 .addInt32("bar", 1)
85 .addInt32("value", 3)
86 .selfrecord());
87 */
88
Andy Hungc89c8dc2019-10-16 17:48:21 -070089 mediaMetrics->dump(fileno(stdout), {} /* args */);
90}
Andy Hungaeef7882019-10-18 15:18:14 -070091
92TEST(mediametrics_tests, item_manipulation) {
Ray Essickf27e9872019-12-07 06:28:46 -080093 mediametrics::Item item("audiorecord");
Andy Hungaeef7882019-10-18 15:18:14 -070094
95 item.setInt32("value", 2).addInt32("bar", 3).addInt32("value", 4);
96
97 int32_t i32;
98 ASSERT_TRUE(item.getInt32("value", &i32));
99 ASSERT_EQ(6, i32);
100
101 ASSERT_TRUE(item.getInt32("bar", &i32));
102 ASSERT_EQ(3, i32);
103
104 item.setInt64("big", INT64_MAX).setInt64("smaller", INT64_MAX - 1).addInt64("smaller", -2);
105
106 int64_t i64;
107 ASSERT_TRUE(item.getInt64("big", &i64));
108 ASSERT_EQ(INT64_MAX, i64);
109
110 ASSERT_TRUE(item.getInt64("smaller", &i64));
111 ASSERT_EQ(INT64_MAX - 3, i64);
112
113 item.setDouble("precise", 10.5).setDouble("small", 0.125).addDouble("precise", 0.25);
114
115 double d;
116 ASSERT_TRUE(item.getDouble("precise", &d));
117 ASSERT_EQ(10.75, d);
118
119 ASSERT_TRUE(item.getDouble("small", &d));
120 ASSERT_EQ(0.125, d);
121
122 char *s;
123 item.setCString("name", "Frank").setCString("mother", "June").setCString("mother", "July");
124 ASSERT_TRUE(item.getCString("name", &s));
125 ASSERT_EQ(0, strcmp(s, "Frank"));
126 free(s);
127
128 ASSERT_TRUE(item.getCString("mother", &s));
129 ASSERT_EQ(0, strcmp(s, "July")); // "July" overwrites "June"
130 free(s);
131
132 item.setRate("burgersPerHour", 5, 2);
133 int64_t b, h;
134 ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d));
135 ASSERT_EQ(5, b);
136 ASSERT_EQ(2, h);
137 ASSERT_EQ(2.5, d);
138
139 item.addRate("burgersPerHour", 4, 2);
140 ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d));
141 ASSERT_EQ(9, b);
142 ASSERT_EQ(4, h);
143 ASSERT_EQ(2.25, d);
144
145 printf("item: %s\n", item.toString().c_str());
146 fflush(stdout);
147
Ray Essickf27e9872019-12-07 06:28:46 -0800148 sp mediaMetrics = new MediaMetricsService();
Andy Hungaeef7882019-10-18 15:18:14 -0700149 status_t status = mediaMetrics->submit(&item);
150 ASSERT_EQ(NO_ERROR, status);
151 mediaMetrics->dump(fileno(stdout), {} /* args */);
152}
153
154TEST(mediametrics_tests, superbig_item) {
Ray Essickf27e9872019-12-07 06:28:46 -0800155 mediametrics::Item item("TheBigOne");
Andy Hungaeef7882019-10-18 15:18:14 -0700156 constexpr size_t count = 10000;
157
158 for (size_t i = 0; i < count; ++i) {
159 item.setInt32(std::to_string(i).c_str(), i);
160 }
161 for (size_t i = 0; i < count; ++i) {
162 int32_t i32;
163 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
164 ASSERT_EQ((int32_t)i, i32);
165 }
166}
167
168TEST(mediametrics_tests, superbig_item_removal) {
Ray Essickf27e9872019-12-07 06:28:46 -0800169 mediametrics::Item item("TheOddBigOne");
Andy Hungaeef7882019-10-18 15:18:14 -0700170 constexpr size_t count = 10000;
171
172 for (size_t i = 0; i < count; ++i) {
173 item.setInt32(std::to_string(i).c_str(), i);
174 }
175 for (size_t i = 0; i < count; i += 2) {
176 item.filter(std::to_string(i).c_str()); // filter out all the evens.
177 }
178 for (size_t i = 0; i < count; ++i) {
179 int32_t i32;
180 if (i & 1) { // check to see that only the odds are left.
181 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
182 ASSERT_EQ((int32_t)i, i32);
183 } else {
184 ASSERT_FALSE(item.getInt32(std::to_string(i).c_str(), &i32));
185 }
186 }
187}
188
Andy Hung3253f2d2019-10-21 14:50:07 -0700189TEST(mediametrics_tests, superbig_item_removal2) {
Ray Essickf27e9872019-12-07 06:28:46 -0800190 mediametrics::Item item("TheOne");
Andy Hung3253f2d2019-10-21 14:50:07 -0700191 constexpr size_t count = 10000;
192
193 for (size_t i = 0; i < count; ++i) {
194 item.setInt32(std::to_string(i).c_str(), i);
195 }
196 static const char *attrs[] = { "1", };
197 item.filterNot(1, attrs);
198
199 for (size_t i = 0; i < count; ++i) {
200 int32_t i32;
201 if (i == 1) { // check to see that there is only one
202 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
203 ASSERT_EQ((int32_t)i, i32);
204 } else {
205 ASSERT_FALSE(item.getInt32(std::to_string(i).c_str(), &i32));
206 }
207 }
208}
209
Andy Hungaeef7882019-10-18 15:18:14 -0700210TEST(mediametrics_tests, item_transmutation) {
Ray Essickf27e9872019-12-07 06:28:46 -0800211 mediametrics::Item item("Alchemist's Stone");
Andy Hungaeef7882019-10-18 15:18:14 -0700212
213 item.setInt64("convert", 123);
214 int64_t i64;
215 ASSERT_TRUE(item.getInt64("convert", &i64));
216 ASSERT_EQ(123, i64);
217
218 item.addInt32("convert", 2); // changes type of 'convert' from i64 to i32 (and re-init).
219 ASSERT_FALSE(item.getInt64("convert", &i64)); // should be false, no value in i64.
220
221 int32_t i32;
222 ASSERT_TRUE(item.getInt32("convert", &i32)); // check it is i32 and 2 (123 is discarded).
223 ASSERT_EQ(2, i32);
224}
Andy Hung3253f2d2019-10-21 14:50:07 -0700225
226TEST(mediametrics_tests, item_binderization) {
Ray Essickf27e9872019-12-07 06:28:46 -0800227 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700228 item.setInt32("i32", 1)
229 .setInt64("i64", 2)
230 .setDouble("double", 3.1)
231 .setCString("string", "abc")
232 .setRate("rate", 11, 12);
233
234 Parcel p;
235 item.writeToParcel(&p);
236
237 p.setDataPosition(0); // rewind for reading
Ray Essickf27e9872019-12-07 06:28:46 -0800238 mediametrics::Item item2;
Andy Hung3253f2d2019-10-21 14:50:07 -0700239 item2.readFromParcel(p);
240
241 ASSERT_EQ(item, item2);
242}
243
244TEST(mediametrics_tests, item_byteserialization) {
Ray Essickf27e9872019-12-07 06:28:46 -0800245 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700246 item.setInt32("i32", 1)
247 .setInt64("i64", 2)
248 .setDouble("double", 3.1)
249 .setCString("string", "abc")
250 .setRate("rate", 11, 12);
251
252 char *data;
253 size_t length;
254 ASSERT_EQ(0, item.writeToByteString(&data, &length));
255 ASSERT_GT(length, (size_t)0);
256
Ray Essickf27e9872019-12-07 06:28:46 -0800257 mediametrics::Item item2;
Andy Hung3253f2d2019-10-21 14:50:07 -0700258 item2.readFromByteString(data, length);
259
260 printf("item: %s\n", item.toString().c_str());
261 printf("item2: %s\n", item2.toString().c_str());
262 ASSERT_EQ(item, item2);
263
264 free(data);
265}
266
267TEST(mediametrics_tests, item_iteration) {
Ray Essickf27e9872019-12-07 06:28:46 -0800268 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700269 item.setInt32("i32", 1)
270 .setInt64("i64", 2)
271 .setDouble("double", 3.125)
272 .setCString("string", "abc")
273 .setRate("rate", 11, 12);
274
275 int mask = 0;
276 for (auto &prop : item) {
277 const char *name = prop.getName();
278 if (!strcmp(name, "i32")) {
279 int32_t i32;
280 ASSERT_TRUE(prop.get(&i32));
281 ASSERT_EQ(1, i32);
Andy Hung692870b2020-01-02 13:46:06 -0800282 ASSERT_EQ(1, std::get<int32_t>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700283 mask |= 1;
284 } else if (!strcmp(name, "i64")) {
285 int64_t i64;
286 ASSERT_TRUE(prop.get(&i64));
287 ASSERT_EQ(2, i64);
Andy Hung692870b2020-01-02 13:46:06 -0800288 ASSERT_EQ(2, std::get<int64_t>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700289 mask |= 2;
290 } else if (!strcmp(name, "double")) {
291 double d;
292 ASSERT_TRUE(prop.get(&d));
293 ASSERT_EQ(3.125, d);
Andy Hung692870b2020-01-02 13:46:06 -0800294 ASSERT_EQ(3.125, std::get<double>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700295 mask |= 4;
296 } else if (!strcmp(name, "string")) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800297 std::string s;
Andy Hung3253f2d2019-10-21 14:50:07 -0700298 ASSERT_TRUE(prop.get(&s));
Andy Hungb7aadb32019-12-09 19:40:42 -0800299 ASSERT_EQ("abc", s);
Andy Hung692870b2020-01-02 13:46:06 -0800300 ASSERT_EQ(s, std::get<std::string>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700301 mask |= 8;
302 } else if (!strcmp(name, "rate")) {
303 std::pair<int64_t, int64_t> r;
304 ASSERT_TRUE(prop.get(&r));
305 ASSERT_EQ(11, r.first);
306 ASSERT_EQ(12, r.second);
Andy Hung692870b2020-01-02 13:46:06 -0800307 ASSERT_EQ(r, std::get<decltype(r)>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700308 mask |= 16;
309 } else {
310 FAIL();
311 }
312 }
313 ASSERT_EQ(31, mask);
314}
Andy Hung1efc9c62019-12-03 13:43:33 -0800315
316TEST(mediametrics_tests, item_expansion) {
Ray Essickf27e9872019-12-07 06:28:46 -0800317 mediametrics::LogItem<1> item("I");
Andy Hung1efc9c62019-12-03 13:43:33 -0800318 item.set("i32", (int32_t)1)
319 .set("i64", (int64_t)2)
320 .set("double", (double)3.125)
321 .set("string", "abcdefghijklmnopqrstuvwxyz")
322 .set("rate", std::pair<int64_t, int64_t>(11, 12));
323 ASSERT_TRUE(item.updateHeader());
324
Ray Essickf27e9872019-12-07 06:28:46 -0800325 mediametrics::Item item2;
Andy Hung1efc9c62019-12-03 13:43:33 -0800326 item2.readFromByteString(item.getBuffer(), item.getLength());
327 ASSERT_EQ((pid_t)-1, item2.getPid());
328 ASSERT_EQ((uid_t)-1, item2.getUid());
329 int mask = 0;
330 for (auto &prop : item2) {
331 const char *name = prop.getName();
332 if (!strcmp(name, "i32")) {
333 int32_t i32;
334 ASSERT_TRUE(prop.get(&i32));
335 ASSERT_EQ(1, i32);
336 mask |= 1;
337 } else if (!strcmp(name, "i64")) {
338 int64_t i64;
339 ASSERT_TRUE(prop.get(&i64));
340 ASSERT_EQ(2, i64);
341 mask |= 2;
342 } else if (!strcmp(name, "double")) {
343 double d;
344 ASSERT_TRUE(prop.get(&d));
345 ASSERT_EQ(3.125, d);
346 mask |= 4;
347 } else if (!strcmp(name, "string")) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800348 std::string s;
Andy Hung1efc9c62019-12-03 13:43:33 -0800349 ASSERT_TRUE(prop.get(&s));
Andy Hungb7aadb32019-12-09 19:40:42 -0800350 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
Andy Hung1efc9c62019-12-03 13:43:33 -0800351 mask |= 8;
352 } else if (!strcmp(name, "rate")) {
353 std::pair<int64_t, int64_t> r;
354 ASSERT_TRUE(prop.get(&r));
355 ASSERT_EQ(11, r.first);
356 ASSERT_EQ(12, r.second);
357 mask |= 16;
358 } else {
359 FAIL();
360 }
361 }
362 ASSERT_EQ(31, mask);
363}
364
365TEST(mediametrics_tests, item_expansion2) {
Ray Essickf27e9872019-12-07 06:28:46 -0800366 mediametrics::LogItem<1> item("Bigly");
Andy Hung1efc9c62019-12-03 13:43:33 -0800367 item.setPid(123)
368 .setUid(456);
369 constexpr size_t count = 10000;
370
371 for (size_t i = 0; i < count; ++i) {
372 // printf("recording %zu, %p, len:%zu of %zu remaining:%zu \n", i, item.getBuffer(), item.getLength(), item.getCapacity(), item.getRemaining());
373 item.set(std::to_string(i).c_str(), (int32_t)i);
374 }
375 ASSERT_TRUE(item.updateHeader());
376
Ray Essickf27e9872019-12-07 06:28:46 -0800377 mediametrics::Item item2;
Andy Hung1efc9c62019-12-03 13:43:33 -0800378 printf("begin buffer:%p length:%zu\n", item.getBuffer(), item.getLength());
379 fflush(stdout);
380 item2.readFromByteString(item.getBuffer(), item.getLength());
381
382 ASSERT_EQ((pid_t)123, item2.getPid());
383 ASSERT_EQ((uid_t)456, item2.getUid());
384 for (size_t i = 0; i < count; ++i) {
385 int32_t i32;
386 ASSERT_TRUE(item2.getInt32(std::to_string(i).c_str(), &i32));
387 ASSERT_EQ((int32_t)i, i32);
388 }
389}
Andy Hung06f3aba2019-12-03 16:36:42 -0800390
391TEST(mediametrics_tests, time_machine_storage) {
Ray Essickf27e9872019-12-07 06:28:46 -0800392 auto item = std::make_shared<mediametrics::Item>("Key");
Andy Hung06f3aba2019-12-03 16:36:42 -0800393 (*item).set("i32", (int32_t)1)
394 .set("i64", (int64_t)2)
395 .set("double", (double)3.125)
396 .set("string", "abcdefghijklmnopqrstuvwxyz")
397 .set("rate", std::pair<int64_t, int64_t>(11, 12));
398
399 // Let's put the item in
400 android::mediametrics::TimeMachine timeMachine;
401 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
402
403 // Can we read the values?
404 int32_t i32;
405 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i32", &i32, -1));
406 ASSERT_EQ(1, i32);
407
408 int64_t i64;
409 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i64", &i64, -1));
410 ASSERT_EQ(2, i64);
411
412 double d;
413 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "double", &d, -1));
414 ASSERT_EQ(3.125, d);
415
416 std::string s;
417 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "string", &s, -1));
418 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
419
420 // Using fully qualified name?
421 i32 = 0;
422 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i32", &i32, -1));
423 ASSERT_EQ(1, i32);
424
425 i64 = 0;
426 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i64", &i64, -1));
427 ASSERT_EQ(2, i64);
428
429 d = 0.;
430 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.double", &d, -1));
431 ASSERT_EQ(3.125, d);
432
433 s.clear();
434 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.string", &s, -1));
435 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
436}
437
438TEST(mediametrics_tests, time_machine_remote_key) {
Ray Essickf27e9872019-12-07 06:28:46 -0800439 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800440 (*item).set("one", (int32_t)1)
441 .set("two", (int32_t)2);
442
443 android::mediametrics::TimeMachine timeMachine;
444 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
445
Ray Essickf27e9872019-12-07 06:28:46 -0800446 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800447 (*item2).set("three", (int32_t)3)
448 .set("[Key1]four", (int32_t)4) // affects Key1
449 .set("[Key1]five", (int32_t)5); // affects key1
450
451 ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true));
452
Ray Essickf27e9872019-12-07 06:28:46 -0800453 auto item3 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800454 (*item3).set("six", (int32_t)6)
455 .set("[Key1]seven", (int32_t)7); // affects Key1
456
457 ASSERT_EQ(NO_ERROR, timeMachine.put(item3, false)); // remote keys not allowed.
458
459 // Can we read the values?
460 int32_t i32;
461 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.one", &i32, -1));
462 ASSERT_EQ(1, i32);
463
464 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.two", &i32, -1));
465 ASSERT_EQ(2, i32);
466
467 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1));
468
469 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1));
470 ASSERT_EQ(3, i32);
471
472 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.four", &i32, -1));
473 ASSERT_EQ(4, i32);
474
475 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.four", &i32, -1));
476
477 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.five", &i32, -1));
478 ASSERT_EQ(5, i32);
479
480 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.five", &i32, -1));
481
482 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.six", &i32, -1));
483 ASSERT_EQ(6, i32);
484
485 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.seven", &i32, -1));
486}
487
488TEST(mediametrics_tests, time_machine_gc) {
Ray Essickf27e9872019-12-07 06:28:46 -0800489 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800490 (*item).set("one", (int32_t)1)
491 .set("two", (int32_t)2)
492 .setTimestamp(10);
493
494 android::mediametrics::TimeMachine timeMachine(1, 2); // keep at most 2 keys.
495
496 ASSERT_EQ((size_t)0, timeMachine.size());
497
498 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
499
500 ASSERT_EQ((size_t)1, timeMachine.size());
501
Ray Essickf27e9872019-12-07 06:28:46 -0800502 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800503 (*item2).set("three", (int32_t)3)
504 .set("[Key1]three", (int32_t)3)
505 .setTimestamp(11);
506
507 ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true));
508 ASSERT_EQ((size_t)2, timeMachine.size());
509
510 //printf("Before\n%s\n\n", timeMachine.dump().c_str());
511
Ray Essickf27e9872019-12-07 06:28:46 -0800512 auto item3 = std::make_shared<mediametrics::Item>("Key3");
Andy Hung06f3aba2019-12-03 16:36:42 -0800513 (*item3).set("six", (int32_t)6)
514 .set("[Key1]four", (int32_t)4) // affects Key1
515 .set("[Key1]five", (int32_t)5) // affects key1
516 .setTimestamp(12);
517
518 ASSERT_EQ(NO_ERROR, timeMachine.put(item3, true));
519
520 ASSERT_EQ((size_t)2, timeMachine.size());
521
522 // Can we read the values?
523 int32_t i32;
524 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.one", &i32, -1));
525 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.two", &i32, -1));
526 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1));
527 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.four", &i32, -1));
528 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.five", &i32, -1));
529
530 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1));
531 ASSERT_EQ(3, i32);
532
533 ASSERT_EQ(NO_ERROR, timeMachine.get("Key3.six", &i32, -1));
534 ASSERT_EQ(6, i32);
535
536 printf("After\n%s\n", timeMachine.dump().first.c_str());
537}
538
539TEST(mediametrics_tests, transaction_log_gc) {
Ray Essickf27e9872019-12-07 06:28:46 -0800540 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800541 (*item).set("one", (int32_t)1)
542 .set("two", (int32_t)2)
543 .setTimestamp(10);
544
545 android::mediametrics::TransactionLog transactionLog(1, 2); // keep at most 2 items
546 ASSERT_EQ((size_t)0, transactionLog.size());
547
548 ASSERT_EQ(NO_ERROR, transactionLog.put(item));
549 ASSERT_EQ((size_t)1, transactionLog.size());
550
Ray Essickf27e9872019-12-07 06:28:46 -0800551 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800552 (*item2).set("three", (int32_t)3)
553 .set("[Key1]three", (int32_t)3)
554 .setTimestamp(11);
555
556 ASSERT_EQ(NO_ERROR, transactionLog.put(item2));
557 ASSERT_EQ((size_t)2, transactionLog.size());
558
Ray Essickf27e9872019-12-07 06:28:46 -0800559 auto item3 = std::make_shared<mediametrics::Item>("Key3");
Andy Hung06f3aba2019-12-03 16:36:42 -0800560 (*item3).set("six", (int32_t)6)
561 .set("[Key1]four", (int32_t)4) // affects Key1
562 .set("[Key1]five", (int32_t)5) // affects key1
563 .setTimestamp(12);
564
565 ASSERT_EQ(NO_ERROR, transactionLog.put(item3));
566 ASSERT_EQ((size_t)2, transactionLog.size());
567}
568
569TEST(mediametrics_tests, audio_analytics_permission) {
Ray Essickf27e9872019-12-07 06:28:46 -0800570 auto item = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800571 (*item).set("one", (int32_t)1)
572 .set("two", (int32_t)2)
573 .setTimestamp(10);
574
Ray Essickf27e9872019-12-07 06:28:46 -0800575 auto item2 = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800576 (*item2).set("three", (int32_t)3)
577 .setTimestamp(11);
578
Ray Essickf27e9872019-12-07 06:28:46 -0800579 auto item3 = std::make_shared<mediametrics::Item>("audio.2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800580 (*item3).set("four", (int32_t)4)
581 .setTimestamp(12);
582
583 android::mediametrics::AudioAnalytics audioAnalytics;
584
585 // untrusted entities cannot create a new key.
586 ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item, false /* isTrusted */));
587 ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item2, false /* isTrusted */));
588
589 // TODO: Verify contents of AudioAnalytics.
590 // Currently there is no getter API in AudioAnalytics besides dump.
591 ASSERT_EQ(4, audioAnalytics.dump(1000).second /* lines */);
592
593 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */));
594 // untrusted entities can add to an existing key
595 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */));
596
597 // Check that we have some info in the dump.
598 ASSERT_LT(4, audioAnalytics.dump(1000).second /* lines */);
599}
600
601TEST(mediametrics_tests, audio_analytics_dump) {
Ray Essickf27e9872019-12-07 06:28:46 -0800602 auto item = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800603 (*item).set("one", (int32_t)1)
604 .set("two", (int32_t)2)
605 .setTimestamp(10);
606
Ray Essickf27e9872019-12-07 06:28:46 -0800607 auto item2 = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800608 (*item2).set("three", (int32_t)3)
609 .setTimestamp(11);
610
Ray Essickf27e9872019-12-07 06:28:46 -0800611 auto item3 = std::make_shared<mediametrics::Item>("audio.2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800612 (*item3).set("four", (int32_t)4)
613 .setTimestamp(12);
614
615 android::mediametrics::AudioAnalytics audioAnalytics;
616
617 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */));
618 // untrusted entities can add to an existing key
619 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */));
620 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item3, true /* isTrusted */));
621
622 // find out how many lines we have.
623 auto [string, lines] = audioAnalytics.dump(1000);
624 ASSERT_EQ(lines, (int32_t) countNewlines(string.c_str()));
625
626 printf("AudioAnalytics: %s", string.c_str());
627 // ensure that dump operates over those lines.
628 for (int32_t ll = 0; ll < lines; ++ll) {
629 auto [s, l] = audioAnalytics.dump(ll);
630 ASSERT_EQ(ll, l);
631 ASSERT_EQ(ll, (int32_t) countNewlines(s.c_str()));
632 }
633}