blob: 90a856504c02a023ebdde574402435f2c2ca1fb6 [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
Andy Hunga85efab2019-12-23 11:41:29 -080092TEST(mediametrics_tests, package_installer_check) {
93 ASSERT_EQ(false, MediaMetricsService::useUidForPackage(
94 "abcd", "installer")); // ok, package name has no dot.
95 ASSERT_EQ(false, MediaMetricsService::useUidForPackage(
96 "android.com", "installer")); // ok, package name starts with android
97
98 ASSERT_EQ(false, MediaMetricsService::useUidForPackage(
99 "abc.def", "com.android.foo")); // ok, installer name starts with com.android
100 ASSERT_EQ(false, MediaMetricsService::useUidForPackage(
101 "123.456", "com.google.bar")); // ok, installer name starts with com.google
102 ASSERT_EQ(false, MediaMetricsService::useUidForPackage(
103 "r2.d2", "preload")); // ok, installer name is preload
104
105 ASSERT_EQ(true, MediaMetricsService::useUidForPackage(
106 "abc.def", "installer")); // unknown installer
107 ASSERT_EQ(true, MediaMetricsService::useUidForPackage(
108 "123.456", "installer")); // unknown installer
109 ASSERT_EQ(true, MediaMetricsService::useUidForPackage(
110 "r2.d2", "preload23")); // unknown installer
111
112 ASSERT_EQ(true, MediaMetricsService::useUidForPackage(
113 "com.android.foo", "abc.def")); // unknown installer
114 ASSERT_EQ(true, MediaMetricsService::useUidForPackage(
115 "com.google.bar", "123.456")); // unknown installer
116}
117
Andy Hungaeef7882019-10-18 15:18:14 -0700118TEST(mediametrics_tests, item_manipulation) {
Ray Essickf27e9872019-12-07 06:28:46 -0800119 mediametrics::Item item("audiorecord");
Andy Hungaeef7882019-10-18 15:18:14 -0700120
121 item.setInt32("value", 2).addInt32("bar", 3).addInt32("value", 4);
122
123 int32_t i32;
124 ASSERT_TRUE(item.getInt32("value", &i32));
125 ASSERT_EQ(6, i32);
126
127 ASSERT_TRUE(item.getInt32("bar", &i32));
128 ASSERT_EQ(3, i32);
129
130 item.setInt64("big", INT64_MAX).setInt64("smaller", INT64_MAX - 1).addInt64("smaller", -2);
131
132 int64_t i64;
133 ASSERT_TRUE(item.getInt64("big", &i64));
134 ASSERT_EQ(INT64_MAX, i64);
135
136 ASSERT_TRUE(item.getInt64("smaller", &i64));
137 ASSERT_EQ(INT64_MAX - 3, i64);
138
139 item.setDouble("precise", 10.5).setDouble("small", 0.125).addDouble("precise", 0.25);
140
141 double d;
142 ASSERT_TRUE(item.getDouble("precise", &d));
143 ASSERT_EQ(10.75, d);
144
145 ASSERT_TRUE(item.getDouble("small", &d));
146 ASSERT_EQ(0.125, d);
147
148 char *s;
149 item.setCString("name", "Frank").setCString("mother", "June").setCString("mother", "July");
150 ASSERT_TRUE(item.getCString("name", &s));
151 ASSERT_EQ(0, strcmp(s, "Frank"));
152 free(s);
153
154 ASSERT_TRUE(item.getCString("mother", &s));
155 ASSERT_EQ(0, strcmp(s, "July")); // "July" overwrites "June"
156 free(s);
157
158 item.setRate("burgersPerHour", 5, 2);
159 int64_t b, h;
160 ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d));
161 ASSERT_EQ(5, b);
162 ASSERT_EQ(2, h);
163 ASSERT_EQ(2.5, d);
164
165 item.addRate("burgersPerHour", 4, 2);
166 ASSERT_TRUE(item.getRate("burgersPerHour", &b, &h, &d));
167 ASSERT_EQ(9, b);
168 ASSERT_EQ(4, h);
169 ASSERT_EQ(2.25, d);
170
171 printf("item: %s\n", item.toString().c_str());
172 fflush(stdout);
173
Ray Essickf27e9872019-12-07 06:28:46 -0800174 sp mediaMetrics = new MediaMetricsService();
Andy Hungaeef7882019-10-18 15:18:14 -0700175 status_t status = mediaMetrics->submit(&item);
176 ASSERT_EQ(NO_ERROR, status);
177 mediaMetrics->dump(fileno(stdout), {} /* args */);
178}
179
180TEST(mediametrics_tests, superbig_item) {
Ray Essickf27e9872019-12-07 06:28:46 -0800181 mediametrics::Item item("TheBigOne");
Andy Hungaeef7882019-10-18 15:18:14 -0700182 constexpr size_t count = 10000;
183
184 for (size_t i = 0; i < count; ++i) {
185 item.setInt32(std::to_string(i).c_str(), i);
186 }
187 for (size_t i = 0; i < count; ++i) {
188 int32_t i32;
189 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
190 ASSERT_EQ((int32_t)i, i32);
191 }
192}
193
194TEST(mediametrics_tests, superbig_item_removal) {
Ray Essickf27e9872019-12-07 06:28:46 -0800195 mediametrics::Item item("TheOddBigOne");
Andy Hungaeef7882019-10-18 15:18:14 -0700196 constexpr size_t count = 10000;
197
198 for (size_t i = 0; i < count; ++i) {
199 item.setInt32(std::to_string(i).c_str(), i);
200 }
201 for (size_t i = 0; i < count; i += 2) {
202 item.filter(std::to_string(i).c_str()); // filter out all the evens.
203 }
204 for (size_t i = 0; i < count; ++i) {
205 int32_t i32;
206 if (i & 1) { // check to see that only the odds are left.
207 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
208 ASSERT_EQ((int32_t)i, i32);
209 } else {
210 ASSERT_FALSE(item.getInt32(std::to_string(i).c_str(), &i32));
211 }
212 }
213}
214
Andy Hung3253f2d2019-10-21 14:50:07 -0700215TEST(mediametrics_tests, superbig_item_removal2) {
Ray Essickf27e9872019-12-07 06:28:46 -0800216 mediametrics::Item item("TheOne");
Andy Hung3253f2d2019-10-21 14:50:07 -0700217 constexpr size_t count = 10000;
218
219 for (size_t i = 0; i < count; ++i) {
220 item.setInt32(std::to_string(i).c_str(), i);
221 }
222 static const char *attrs[] = { "1", };
223 item.filterNot(1, attrs);
224
225 for (size_t i = 0; i < count; ++i) {
226 int32_t i32;
227 if (i == 1) { // check to see that there is only one
228 ASSERT_TRUE(item.getInt32(std::to_string(i).c_str(), &i32));
229 ASSERT_EQ((int32_t)i, i32);
230 } else {
231 ASSERT_FALSE(item.getInt32(std::to_string(i).c_str(), &i32));
232 }
233 }
234}
235
Andy Hungaeef7882019-10-18 15:18:14 -0700236TEST(mediametrics_tests, item_transmutation) {
Ray Essickf27e9872019-12-07 06:28:46 -0800237 mediametrics::Item item("Alchemist's Stone");
Andy Hungaeef7882019-10-18 15:18:14 -0700238
239 item.setInt64("convert", 123);
240 int64_t i64;
241 ASSERT_TRUE(item.getInt64("convert", &i64));
242 ASSERT_EQ(123, i64);
243
244 item.addInt32("convert", 2); // changes type of 'convert' from i64 to i32 (and re-init).
245 ASSERT_FALSE(item.getInt64("convert", &i64)); // should be false, no value in i64.
246
247 int32_t i32;
248 ASSERT_TRUE(item.getInt32("convert", &i32)); // check it is i32 and 2 (123 is discarded).
249 ASSERT_EQ(2, i32);
250}
Andy Hung3253f2d2019-10-21 14:50:07 -0700251
252TEST(mediametrics_tests, item_binderization) {
Ray Essickf27e9872019-12-07 06:28:46 -0800253 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700254 item.setInt32("i32", 1)
255 .setInt64("i64", 2)
256 .setDouble("double", 3.1)
257 .setCString("string", "abc")
258 .setRate("rate", 11, 12);
259
260 Parcel p;
261 item.writeToParcel(&p);
262
263 p.setDataPosition(0); // rewind for reading
Ray Essickf27e9872019-12-07 06:28:46 -0800264 mediametrics::Item item2;
Andy Hung3253f2d2019-10-21 14:50:07 -0700265 item2.readFromParcel(p);
266
267 ASSERT_EQ(item, item2);
268}
269
270TEST(mediametrics_tests, item_byteserialization) {
Ray Essickf27e9872019-12-07 06:28:46 -0800271 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700272 item.setInt32("i32", 1)
273 .setInt64("i64", 2)
274 .setDouble("double", 3.1)
275 .setCString("string", "abc")
276 .setRate("rate", 11, 12);
277
278 char *data;
279 size_t length;
280 ASSERT_EQ(0, item.writeToByteString(&data, &length));
281 ASSERT_GT(length, (size_t)0);
282
Ray Essickf27e9872019-12-07 06:28:46 -0800283 mediametrics::Item item2;
Andy Hung3253f2d2019-10-21 14:50:07 -0700284 item2.readFromByteString(data, length);
285
286 printf("item: %s\n", item.toString().c_str());
287 printf("item2: %s\n", item2.toString().c_str());
288 ASSERT_EQ(item, item2);
289
290 free(data);
291}
292
293TEST(mediametrics_tests, item_iteration) {
Ray Essickf27e9872019-12-07 06:28:46 -0800294 mediametrics::Item item;
Andy Hung3253f2d2019-10-21 14:50:07 -0700295 item.setInt32("i32", 1)
296 .setInt64("i64", 2)
297 .setDouble("double", 3.125)
298 .setCString("string", "abc")
299 .setRate("rate", 11, 12);
300
301 int mask = 0;
302 for (auto &prop : item) {
303 const char *name = prop.getName();
304 if (!strcmp(name, "i32")) {
305 int32_t i32;
306 ASSERT_TRUE(prop.get(&i32));
307 ASSERT_EQ(1, i32);
Andy Hung692870b2020-01-02 13:46:06 -0800308 ASSERT_EQ(1, std::get<int32_t>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700309 mask |= 1;
310 } else if (!strcmp(name, "i64")) {
311 int64_t i64;
312 ASSERT_TRUE(prop.get(&i64));
313 ASSERT_EQ(2, i64);
Andy Hung692870b2020-01-02 13:46:06 -0800314 ASSERT_EQ(2, std::get<int64_t>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700315 mask |= 2;
316 } else if (!strcmp(name, "double")) {
317 double d;
318 ASSERT_TRUE(prop.get(&d));
319 ASSERT_EQ(3.125, d);
Andy Hung692870b2020-01-02 13:46:06 -0800320 ASSERT_EQ(3.125, std::get<double>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700321 mask |= 4;
322 } else if (!strcmp(name, "string")) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800323 std::string s;
Andy Hung3253f2d2019-10-21 14:50:07 -0700324 ASSERT_TRUE(prop.get(&s));
Andy Hungb7aadb32019-12-09 19:40:42 -0800325 ASSERT_EQ("abc", s);
Andy Hung692870b2020-01-02 13:46:06 -0800326 ASSERT_EQ(s, std::get<std::string>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700327 mask |= 8;
328 } else if (!strcmp(name, "rate")) {
329 std::pair<int64_t, int64_t> r;
330 ASSERT_TRUE(prop.get(&r));
331 ASSERT_EQ(11, r.first);
332 ASSERT_EQ(12, r.second);
Andy Hung692870b2020-01-02 13:46:06 -0800333 ASSERT_EQ(r, std::get<decltype(r)>(prop.get()));
Andy Hung3253f2d2019-10-21 14:50:07 -0700334 mask |= 16;
335 } else {
336 FAIL();
337 }
338 }
339 ASSERT_EQ(31, mask);
340}
Andy Hung1efc9c62019-12-03 13:43:33 -0800341
342TEST(mediametrics_tests, item_expansion) {
Ray Essickf27e9872019-12-07 06:28:46 -0800343 mediametrics::LogItem<1> item("I");
Andy Hung1efc9c62019-12-03 13:43:33 -0800344 item.set("i32", (int32_t)1)
345 .set("i64", (int64_t)2)
346 .set("double", (double)3.125)
347 .set("string", "abcdefghijklmnopqrstuvwxyz")
348 .set("rate", std::pair<int64_t, int64_t>(11, 12));
349 ASSERT_TRUE(item.updateHeader());
350
Ray Essickf27e9872019-12-07 06:28:46 -0800351 mediametrics::Item item2;
Andy Hung1efc9c62019-12-03 13:43:33 -0800352 item2.readFromByteString(item.getBuffer(), item.getLength());
353 ASSERT_EQ((pid_t)-1, item2.getPid());
354 ASSERT_EQ((uid_t)-1, item2.getUid());
355 int mask = 0;
356 for (auto &prop : item2) {
357 const char *name = prop.getName();
358 if (!strcmp(name, "i32")) {
359 int32_t i32;
360 ASSERT_TRUE(prop.get(&i32));
361 ASSERT_EQ(1, i32);
362 mask |= 1;
363 } else if (!strcmp(name, "i64")) {
364 int64_t i64;
365 ASSERT_TRUE(prop.get(&i64));
366 ASSERT_EQ(2, i64);
367 mask |= 2;
368 } else if (!strcmp(name, "double")) {
369 double d;
370 ASSERT_TRUE(prop.get(&d));
371 ASSERT_EQ(3.125, d);
372 mask |= 4;
373 } else if (!strcmp(name, "string")) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800374 std::string s;
Andy Hung1efc9c62019-12-03 13:43:33 -0800375 ASSERT_TRUE(prop.get(&s));
Andy Hungb7aadb32019-12-09 19:40:42 -0800376 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
Andy Hung1efc9c62019-12-03 13:43:33 -0800377 mask |= 8;
378 } else if (!strcmp(name, "rate")) {
379 std::pair<int64_t, int64_t> r;
380 ASSERT_TRUE(prop.get(&r));
381 ASSERT_EQ(11, r.first);
382 ASSERT_EQ(12, r.second);
383 mask |= 16;
384 } else {
385 FAIL();
386 }
387 }
388 ASSERT_EQ(31, mask);
389}
390
391TEST(mediametrics_tests, item_expansion2) {
Ray Essickf27e9872019-12-07 06:28:46 -0800392 mediametrics::LogItem<1> item("Bigly");
Andy Hung1efc9c62019-12-03 13:43:33 -0800393 item.setPid(123)
394 .setUid(456);
395 constexpr size_t count = 10000;
396
397 for (size_t i = 0; i < count; ++i) {
398 // printf("recording %zu, %p, len:%zu of %zu remaining:%zu \n", i, item.getBuffer(), item.getLength(), item.getCapacity(), item.getRemaining());
399 item.set(std::to_string(i).c_str(), (int32_t)i);
400 }
401 ASSERT_TRUE(item.updateHeader());
402
Ray Essickf27e9872019-12-07 06:28:46 -0800403 mediametrics::Item item2;
Andy Hung1efc9c62019-12-03 13:43:33 -0800404 printf("begin buffer:%p length:%zu\n", item.getBuffer(), item.getLength());
405 fflush(stdout);
406 item2.readFromByteString(item.getBuffer(), item.getLength());
407
408 ASSERT_EQ((pid_t)123, item2.getPid());
409 ASSERT_EQ((uid_t)456, item2.getUid());
410 for (size_t i = 0; i < count; ++i) {
411 int32_t i32;
412 ASSERT_TRUE(item2.getInt32(std::to_string(i).c_str(), &i32));
413 ASSERT_EQ((int32_t)i, i32);
414 }
415}
Andy Hung06f3aba2019-12-03 16:36:42 -0800416
417TEST(mediametrics_tests, time_machine_storage) {
Ray Essickf27e9872019-12-07 06:28:46 -0800418 auto item = std::make_shared<mediametrics::Item>("Key");
Andy Hung06f3aba2019-12-03 16:36:42 -0800419 (*item).set("i32", (int32_t)1)
420 .set("i64", (int64_t)2)
421 .set("double", (double)3.125)
422 .set("string", "abcdefghijklmnopqrstuvwxyz")
423 .set("rate", std::pair<int64_t, int64_t>(11, 12));
424
425 // Let's put the item in
426 android::mediametrics::TimeMachine timeMachine;
427 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
428
429 // Can we read the values?
430 int32_t i32;
431 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i32", &i32, -1));
432 ASSERT_EQ(1, i32);
433
434 int64_t i64;
435 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i64", &i64, -1));
436 ASSERT_EQ(2, i64);
437
438 double d;
439 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "double", &d, -1));
440 ASSERT_EQ(3.125, d);
441
442 std::string s;
443 ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "string", &s, -1));
444 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
445
446 // Using fully qualified name?
447 i32 = 0;
448 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i32", &i32, -1));
449 ASSERT_EQ(1, i32);
450
451 i64 = 0;
452 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i64", &i64, -1));
453 ASSERT_EQ(2, i64);
454
455 d = 0.;
456 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.double", &d, -1));
457 ASSERT_EQ(3.125, d);
458
459 s.clear();
460 ASSERT_EQ(NO_ERROR, timeMachine.get("Key.string", &s, -1));
461 ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s);
462}
463
464TEST(mediametrics_tests, time_machine_remote_key) {
Ray Essickf27e9872019-12-07 06:28:46 -0800465 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800466 (*item).set("one", (int32_t)1)
467 .set("two", (int32_t)2);
468
469 android::mediametrics::TimeMachine timeMachine;
470 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
471
Ray Essickf27e9872019-12-07 06:28:46 -0800472 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800473 (*item2).set("three", (int32_t)3)
474 .set("[Key1]four", (int32_t)4) // affects Key1
475 .set("[Key1]five", (int32_t)5); // affects key1
476
477 ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true));
478
Ray Essickf27e9872019-12-07 06:28:46 -0800479 auto item3 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800480 (*item3).set("six", (int32_t)6)
481 .set("[Key1]seven", (int32_t)7); // affects Key1
482
483 ASSERT_EQ(NO_ERROR, timeMachine.put(item3, false)); // remote keys not allowed.
484
485 // Can we read the values?
486 int32_t i32;
487 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.one", &i32, -1));
488 ASSERT_EQ(1, i32);
489
490 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.two", &i32, -1));
491 ASSERT_EQ(2, i32);
492
493 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1));
494
495 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1));
496 ASSERT_EQ(3, i32);
497
498 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.four", &i32, -1));
499 ASSERT_EQ(4, i32);
500
501 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.four", &i32, -1));
502
503 ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.five", &i32, -1));
504 ASSERT_EQ(5, i32);
505
506 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.five", &i32, -1));
507
508 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.six", &i32, -1));
509 ASSERT_EQ(6, i32);
510
511 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.seven", &i32, -1));
512}
513
514TEST(mediametrics_tests, time_machine_gc) {
Ray Essickf27e9872019-12-07 06:28:46 -0800515 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800516 (*item).set("one", (int32_t)1)
517 .set("two", (int32_t)2)
518 .setTimestamp(10);
519
520 android::mediametrics::TimeMachine timeMachine(1, 2); // keep at most 2 keys.
521
522 ASSERT_EQ((size_t)0, timeMachine.size());
523
524 ASSERT_EQ(NO_ERROR, timeMachine.put(item, true));
525
526 ASSERT_EQ((size_t)1, timeMachine.size());
527
Ray Essickf27e9872019-12-07 06:28:46 -0800528 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800529 (*item2).set("three", (int32_t)3)
530 .set("[Key1]three", (int32_t)3)
531 .setTimestamp(11);
532
533 ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true));
534 ASSERT_EQ((size_t)2, timeMachine.size());
535
536 //printf("Before\n%s\n\n", timeMachine.dump().c_str());
537
Ray Essickf27e9872019-12-07 06:28:46 -0800538 auto item3 = std::make_shared<mediametrics::Item>("Key3");
Andy Hung06f3aba2019-12-03 16:36:42 -0800539 (*item3).set("six", (int32_t)6)
540 .set("[Key1]four", (int32_t)4) // affects Key1
541 .set("[Key1]five", (int32_t)5) // affects key1
542 .setTimestamp(12);
543
544 ASSERT_EQ(NO_ERROR, timeMachine.put(item3, true));
545
546 ASSERT_EQ((size_t)2, timeMachine.size());
547
548 // Can we read the values?
549 int32_t i32;
550 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.one", &i32, -1));
551 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.two", &i32, -1));
552 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1));
553 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.four", &i32, -1));
554 ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.five", &i32, -1));
555
556 ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1));
557 ASSERT_EQ(3, i32);
558
559 ASSERT_EQ(NO_ERROR, timeMachine.get("Key3.six", &i32, -1));
560 ASSERT_EQ(6, i32);
561
562 printf("After\n%s\n", timeMachine.dump().first.c_str());
563}
564
565TEST(mediametrics_tests, transaction_log_gc) {
Ray Essickf27e9872019-12-07 06:28:46 -0800566 auto item = std::make_shared<mediametrics::Item>("Key1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800567 (*item).set("one", (int32_t)1)
568 .set("two", (int32_t)2)
569 .setTimestamp(10);
570
571 android::mediametrics::TransactionLog transactionLog(1, 2); // keep at most 2 items
572 ASSERT_EQ((size_t)0, transactionLog.size());
573
574 ASSERT_EQ(NO_ERROR, transactionLog.put(item));
575 ASSERT_EQ((size_t)1, transactionLog.size());
576
Ray Essickf27e9872019-12-07 06:28:46 -0800577 auto item2 = std::make_shared<mediametrics::Item>("Key2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800578 (*item2).set("three", (int32_t)3)
579 .set("[Key1]three", (int32_t)3)
580 .setTimestamp(11);
581
582 ASSERT_EQ(NO_ERROR, transactionLog.put(item2));
583 ASSERT_EQ((size_t)2, transactionLog.size());
584
Ray Essickf27e9872019-12-07 06:28:46 -0800585 auto item3 = std::make_shared<mediametrics::Item>("Key3");
Andy Hung06f3aba2019-12-03 16:36:42 -0800586 (*item3).set("six", (int32_t)6)
587 .set("[Key1]four", (int32_t)4) // affects Key1
588 .set("[Key1]five", (int32_t)5) // affects key1
589 .setTimestamp(12);
590
591 ASSERT_EQ(NO_ERROR, transactionLog.put(item3));
592 ASSERT_EQ((size_t)2, transactionLog.size());
593}
594
595TEST(mediametrics_tests, audio_analytics_permission) {
Ray Essickf27e9872019-12-07 06:28:46 -0800596 auto item = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800597 (*item).set("one", (int32_t)1)
598 .set("two", (int32_t)2)
599 .setTimestamp(10);
600
Ray Essickf27e9872019-12-07 06:28:46 -0800601 auto item2 = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800602 (*item2).set("three", (int32_t)3)
603 .setTimestamp(11);
604
Ray Essickf27e9872019-12-07 06:28:46 -0800605 auto item3 = std::make_shared<mediametrics::Item>("audio.2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800606 (*item3).set("four", (int32_t)4)
607 .setTimestamp(12);
608
609 android::mediametrics::AudioAnalytics audioAnalytics;
610
611 // untrusted entities cannot create a new key.
612 ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item, false /* isTrusted */));
613 ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item2, false /* isTrusted */));
614
615 // TODO: Verify contents of AudioAnalytics.
616 // Currently there is no getter API in AudioAnalytics besides dump.
617 ASSERT_EQ(4, audioAnalytics.dump(1000).second /* lines */);
618
619 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */));
620 // untrusted entities can add to an existing key
621 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */));
622
623 // Check that we have some info in the dump.
624 ASSERT_LT(4, audioAnalytics.dump(1000).second /* lines */);
625}
626
627TEST(mediametrics_tests, audio_analytics_dump) {
Ray Essickf27e9872019-12-07 06:28:46 -0800628 auto item = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800629 (*item).set("one", (int32_t)1)
630 .set("two", (int32_t)2)
631 .setTimestamp(10);
632
Ray Essickf27e9872019-12-07 06:28:46 -0800633 auto item2 = std::make_shared<mediametrics::Item>("audio.1");
Andy Hung06f3aba2019-12-03 16:36:42 -0800634 (*item2).set("three", (int32_t)3)
635 .setTimestamp(11);
636
Ray Essickf27e9872019-12-07 06:28:46 -0800637 auto item3 = std::make_shared<mediametrics::Item>("audio.2");
Andy Hung06f3aba2019-12-03 16:36:42 -0800638 (*item3).set("four", (int32_t)4)
639 .setTimestamp(12);
640
641 android::mediametrics::AudioAnalytics audioAnalytics;
642
643 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */));
644 // untrusted entities can add to an existing key
645 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */));
646 ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item3, true /* isTrusted */));
647
648 // find out how many lines we have.
649 auto [string, lines] = audioAnalytics.dump(1000);
650 ASSERT_EQ(lines, (int32_t) countNewlines(string.c_str()));
651
652 printf("AudioAnalytics: %s", string.c_str());
653 // ensure that dump operates over those lines.
654 for (int32_t ll = 0; ll < lines; ++ll) {
655 auto [s, l] = audioAnalytics.dump(ll);
656 ASSERT_EQ(ll, l);
657 ASSERT_EQ(ll, (int32_t) countNewlines(s.c_str()));
658 }
659}