Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [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 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 Essick | 40e8e5e | 2019-12-05 20:19:40 -0800 | [diff] [blame] | 20 | #include "MediaMetricsService.h" |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 21 | |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 25 | #include <media/MediaMetricsItem.h> |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
| 28 | |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 29 | static 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 Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 38 | TEST(mediametrics_tests, instantiate) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 39 | sp mediaMetrics = new MediaMetricsService(); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 40 | status_t status; |
| 41 | |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 42 | // random keys ignored when empty |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 43 | std::unique_ptr<mediametrics::Item> random_key(mediametrics::Item::create("random_key")); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 44 | status = mediaMetrics->submit(random_key.get()); |
| 45 | ASSERT_EQ(PERMISSION_DENIED, status); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 46 | |
| 47 | // random keys ignored with data |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 48 | random_key->setInt32("foo", 10); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 49 | status = mediaMetrics->submit(random_key.get()); |
| 50 | ASSERT_EQ(PERMISSION_DENIED, status); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 51 | |
| 52 | // known keys ignored if empty |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 53 | std::unique_ptr<mediametrics::Item> audiotrack_key(mediametrics::Item::create("audiotrack")); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 54 | status = mediaMetrics->submit(audiotrack_key.get()); |
| 55 | ASSERT_EQ(BAD_VALUE, status); |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 56 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 57 | // 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 Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 61 | |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | // fluent style that goes directly to mediametrics |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 65 | ASSERT_EQ(true, mediametrics::Item("audiorecord") |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 66 | .setInt32("value", 2) |
| 67 | .addInt32("bar", 1) |
| 68 | .addInt32("value", 3) |
| 69 | .selfrecord()); |
| 70 | */ |
| 71 | |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 72 | mediaMetrics->dump(fileno(stdout), {} /* args */); |
| 73 | } |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 74 | |
| 75 | TEST(mediametrics_tests, item_manipulation) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 76 | mediametrics::Item item("audiorecord"); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 77 | |
| 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 Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 131 | sp mediaMetrics = new MediaMetricsService(); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 132 | status_t status = mediaMetrics->submit(&item); |
| 133 | ASSERT_EQ(NO_ERROR, status); |
| 134 | mediaMetrics->dump(fileno(stdout), {} /* args */); |
| 135 | } |
| 136 | |
| 137 | TEST(mediametrics_tests, superbig_item) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 138 | mediametrics::Item item("TheBigOne"); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 139 | 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 | |
| 151 | TEST(mediametrics_tests, superbig_item_removal) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 152 | mediametrics::Item item("TheOddBigOne"); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 153 | 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 Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 172 | TEST(mediametrics_tests, superbig_item_removal2) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 173 | mediametrics::Item item("TheOne"); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 174 | 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 Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 193 | TEST(mediametrics_tests, item_transmutation) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 194 | mediametrics::Item item("Alchemist's Stone"); |
Andy Hung | aeef788 | 2019-10-18 15:18:14 -0700 | [diff] [blame] | 195 | |
| 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 Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 208 | |
| 209 | TEST(mediametrics_tests, item_binderization) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 210 | mediametrics::Item item; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 211 | 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 Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 221 | mediametrics::Item item2; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 222 | item2.readFromParcel(p); |
| 223 | |
| 224 | ASSERT_EQ(item, item2); |
| 225 | } |
| 226 | |
| 227 | TEST(mediametrics_tests, item_byteserialization) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 228 | mediametrics::Item item; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 229 | 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 Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 240 | mediametrics::Item item2; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 241 | 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 | |
| 250 | TEST(mediametrics_tests, item_iteration) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 251 | mediametrics::Item item; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 252 | 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); |
| 265 | mask |= 1; |
| 266 | } else if (!strcmp(name, "i64")) { |
| 267 | int64_t i64; |
| 268 | ASSERT_TRUE(prop.get(&i64)); |
| 269 | ASSERT_EQ(2, i64); |
| 270 | mask |= 2; |
| 271 | } else if (!strcmp(name, "double")) { |
| 272 | double d; |
| 273 | ASSERT_TRUE(prop.get(&d)); |
| 274 | ASSERT_EQ(3.125, d); |
| 275 | mask |= 4; |
| 276 | } else if (!strcmp(name, "string")) { |
| 277 | const char *s; |
| 278 | ASSERT_TRUE(prop.get(&s)); |
| 279 | ASSERT_EQ(0, strcmp(s, "abc")); |
| 280 | mask |= 8; |
| 281 | } else if (!strcmp(name, "rate")) { |
| 282 | std::pair<int64_t, int64_t> r; |
| 283 | ASSERT_TRUE(prop.get(&r)); |
| 284 | ASSERT_EQ(11, r.first); |
| 285 | ASSERT_EQ(12, r.second); |
| 286 | mask |= 16; |
| 287 | } else { |
| 288 | FAIL(); |
| 289 | } |
| 290 | } |
| 291 | ASSERT_EQ(31, mask); |
| 292 | } |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 293 | |
| 294 | TEST(mediametrics_tests, item_expansion) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 295 | mediametrics::LogItem<1> item("I"); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 296 | item.set("i32", (int32_t)1) |
| 297 | .set("i64", (int64_t)2) |
| 298 | .set("double", (double)3.125) |
| 299 | .set("string", "abcdefghijklmnopqrstuvwxyz") |
| 300 | .set("rate", std::pair<int64_t, int64_t>(11, 12)); |
| 301 | ASSERT_TRUE(item.updateHeader()); |
| 302 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 303 | mediametrics::Item item2; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 304 | item2.readFromByteString(item.getBuffer(), item.getLength()); |
| 305 | ASSERT_EQ((pid_t)-1, item2.getPid()); |
| 306 | ASSERT_EQ((uid_t)-1, item2.getUid()); |
| 307 | int mask = 0; |
| 308 | for (auto &prop : item2) { |
| 309 | const char *name = prop.getName(); |
| 310 | if (!strcmp(name, "i32")) { |
| 311 | int32_t i32; |
| 312 | ASSERT_TRUE(prop.get(&i32)); |
| 313 | ASSERT_EQ(1, i32); |
| 314 | mask |= 1; |
| 315 | } else if (!strcmp(name, "i64")) { |
| 316 | int64_t i64; |
| 317 | ASSERT_TRUE(prop.get(&i64)); |
| 318 | ASSERT_EQ(2, i64); |
| 319 | mask |= 2; |
| 320 | } else if (!strcmp(name, "double")) { |
| 321 | double d; |
| 322 | ASSERT_TRUE(prop.get(&d)); |
| 323 | ASSERT_EQ(3.125, d); |
| 324 | mask |= 4; |
| 325 | } else if (!strcmp(name, "string")) { |
| 326 | const char *s; |
| 327 | ASSERT_TRUE(prop.get(&s)); |
| 328 | ASSERT_EQ(0, strcmp(s, "abcdefghijklmnopqrstuvwxyz")); |
| 329 | mask |= 8; |
| 330 | } else if (!strcmp(name, "rate")) { |
| 331 | std::pair<int64_t, int64_t> r; |
| 332 | ASSERT_TRUE(prop.get(&r)); |
| 333 | ASSERT_EQ(11, r.first); |
| 334 | ASSERT_EQ(12, r.second); |
| 335 | mask |= 16; |
| 336 | } else { |
| 337 | FAIL(); |
| 338 | } |
| 339 | } |
| 340 | ASSERT_EQ(31, mask); |
| 341 | } |
| 342 | |
| 343 | TEST(mediametrics_tests, item_expansion2) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 344 | mediametrics::LogItem<1> item("Bigly"); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 345 | item.setPid(123) |
| 346 | .setUid(456); |
| 347 | constexpr size_t count = 10000; |
| 348 | |
| 349 | for (size_t i = 0; i < count; ++i) { |
| 350 | // printf("recording %zu, %p, len:%zu of %zu remaining:%zu \n", i, item.getBuffer(), item.getLength(), item.getCapacity(), item.getRemaining()); |
| 351 | item.set(std::to_string(i).c_str(), (int32_t)i); |
| 352 | } |
| 353 | ASSERT_TRUE(item.updateHeader()); |
| 354 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 355 | mediametrics::Item item2; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 356 | printf("begin buffer:%p length:%zu\n", item.getBuffer(), item.getLength()); |
| 357 | fflush(stdout); |
| 358 | item2.readFromByteString(item.getBuffer(), item.getLength()); |
| 359 | |
| 360 | ASSERT_EQ((pid_t)123, item2.getPid()); |
| 361 | ASSERT_EQ((uid_t)456, item2.getUid()); |
| 362 | for (size_t i = 0; i < count; ++i) { |
| 363 | int32_t i32; |
| 364 | ASSERT_TRUE(item2.getInt32(std::to_string(i).c_str(), &i32)); |
| 365 | ASSERT_EQ((int32_t)i, i32); |
| 366 | } |
| 367 | } |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 368 | |
| 369 | TEST(mediametrics_tests, time_machine_storage) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 370 | auto item = std::make_shared<mediametrics::Item>("Key"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 371 | (*item).set("i32", (int32_t)1) |
| 372 | .set("i64", (int64_t)2) |
| 373 | .set("double", (double)3.125) |
| 374 | .set("string", "abcdefghijklmnopqrstuvwxyz") |
| 375 | .set("rate", std::pair<int64_t, int64_t>(11, 12)); |
| 376 | |
| 377 | // Let's put the item in |
| 378 | android::mediametrics::TimeMachine timeMachine; |
| 379 | ASSERT_EQ(NO_ERROR, timeMachine.put(item, true)); |
| 380 | |
| 381 | // Can we read the values? |
| 382 | int32_t i32; |
| 383 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i32", &i32, -1)); |
| 384 | ASSERT_EQ(1, i32); |
| 385 | |
| 386 | int64_t i64; |
| 387 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "i64", &i64, -1)); |
| 388 | ASSERT_EQ(2, i64); |
| 389 | |
| 390 | double d; |
| 391 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "double", &d, -1)); |
| 392 | ASSERT_EQ(3.125, d); |
| 393 | |
| 394 | std::string s; |
| 395 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key", "string", &s, -1)); |
| 396 | ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s); |
| 397 | |
| 398 | // Using fully qualified name? |
| 399 | i32 = 0; |
| 400 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i32", &i32, -1)); |
| 401 | ASSERT_EQ(1, i32); |
| 402 | |
| 403 | i64 = 0; |
| 404 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key.i64", &i64, -1)); |
| 405 | ASSERT_EQ(2, i64); |
| 406 | |
| 407 | d = 0.; |
| 408 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key.double", &d, -1)); |
| 409 | ASSERT_EQ(3.125, d); |
| 410 | |
| 411 | s.clear(); |
| 412 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key.string", &s, -1)); |
| 413 | ASSERT_EQ("abcdefghijklmnopqrstuvwxyz", s); |
| 414 | } |
| 415 | |
| 416 | TEST(mediametrics_tests, time_machine_remote_key) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 417 | auto item = std::make_shared<mediametrics::Item>("Key1"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 418 | (*item).set("one", (int32_t)1) |
| 419 | .set("two", (int32_t)2); |
| 420 | |
| 421 | android::mediametrics::TimeMachine timeMachine; |
| 422 | ASSERT_EQ(NO_ERROR, timeMachine.put(item, true)); |
| 423 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 424 | auto item2 = std::make_shared<mediametrics::Item>("Key2"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 425 | (*item2).set("three", (int32_t)3) |
| 426 | .set("[Key1]four", (int32_t)4) // affects Key1 |
| 427 | .set("[Key1]five", (int32_t)5); // affects key1 |
| 428 | |
| 429 | ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true)); |
| 430 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 431 | auto item3 = std::make_shared<mediametrics::Item>("Key2"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 432 | (*item3).set("six", (int32_t)6) |
| 433 | .set("[Key1]seven", (int32_t)7); // affects Key1 |
| 434 | |
| 435 | ASSERT_EQ(NO_ERROR, timeMachine.put(item3, false)); // remote keys not allowed. |
| 436 | |
| 437 | // Can we read the values? |
| 438 | int32_t i32; |
| 439 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.one", &i32, -1)); |
| 440 | ASSERT_EQ(1, i32); |
| 441 | |
| 442 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.two", &i32, -1)); |
| 443 | ASSERT_EQ(2, i32); |
| 444 | |
| 445 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1)); |
| 446 | |
| 447 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1)); |
| 448 | ASSERT_EQ(3, i32); |
| 449 | |
| 450 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.four", &i32, -1)); |
| 451 | ASSERT_EQ(4, i32); |
| 452 | |
| 453 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.four", &i32, -1)); |
| 454 | |
| 455 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key1.five", &i32, -1)); |
| 456 | ASSERT_EQ(5, i32); |
| 457 | |
| 458 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.five", &i32, -1)); |
| 459 | |
| 460 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.six", &i32, -1)); |
| 461 | ASSERT_EQ(6, i32); |
| 462 | |
| 463 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key2.seven", &i32, -1)); |
| 464 | } |
| 465 | |
| 466 | TEST(mediametrics_tests, time_machine_gc) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 467 | auto item = std::make_shared<mediametrics::Item>("Key1"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 468 | (*item).set("one", (int32_t)1) |
| 469 | .set("two", (int32_t)2) |
| 470 | .setTimestamp(10); |
| 471 | |
| 472 | android::mediametrics::TimeMachine timeMachine(1, 2); // keep at most 2 keys. |
| 473 | |
| 474 | ASSERT_EQ((size_t)0, timeMachine.size()); |
| 475 | |
| 476 | ASSERT_EQ(NO_ERROR, timeMachine.put(item, true)); |
| 477 | |
| 478 | ASSERT_EQ((size_t)1, timeMachine.size()); |
| 479 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 480 | auto item2 = std::make_shared<mediametrics::Item>("Key2"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 481 | (*item2).set("three", (int32_t)3) |
| 482 | .set("[Key1]three", (int32_t)3) |
| 483 | .setTimestamp(11); |
| 484 | |
| 485 | ASSERT_EQ(NO_ERROR, timeMachine.put(item2, true)); |
| 486 | ASSERT_EQ((size_t)2, timeMachine.size()); |
| 487 | |
| 488 | //printf("Before\n%s\n\n", timeMachine.dump().c_str()); |
| 489 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 490 | auto item3 = std::make_shared<mediametrics::Item>("Key3"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 491 | (*item3).set("six", (int32_t)6) |
| 492 | .set("[Key1]four", (int32_t)4) // affects Key1 |
| 493 | .set("[Key1]five", (int32_t)5) // affects key1 |
| 494 | .setTimestamp(12); |
| 495 | |
| 496 | ASSERT_EQ(NO_ERROR, timeMachine.put(item3, true)); |
| 497 | |
| 498 | ASSERT_EQ((size_t)2, timeMachine.size()); |
| 499 | |
| 500 | // Can we read the values? |
| 501 | int32_t i32; |
| 502 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.one", &i32, -1)); |
| 503 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.two", &i32, -1)); |
| 504 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.three", &i32, -1)); |
| 505 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.four", &i32, -1)); |
| 506 | ASSERT_EQ(BAD_VALUE, timeMachine.get("Key1.five", &i32, -1)); |
| 507 | |
| 508 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key2.three", &i32, -1)); |
| 509 | ASSERT_EQ(3, i32); |
| 510 | |
| 511 | ASSERT_EQ(NO_ERROR, timeMachine.get("Key3.six", &i32, -1)); |
| 512 | ASSERT_EQ(6, i32); |
| 513 | |
| 514 | printf("After\n%s\n", timeMachine.dump().first.c_str()); |
| 515 | } |
| 516 | |
| 517 | TEST(mediametrics_tests, transaction_log_gc) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 518 | auto item = std::make_shared<mediametrics::Item>("Key1"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 519 | (*item).set("one", (int32_t)1) |
| 520 | .set("two", (int32_t)2) |
| 521 | .setTimestamp(10); |
| 522 | |
| 523 | android::mediametrics::TransactionLog transactionLog(1, 2); // keep at most 2 items |
| 524 | ASSERT_EQ((size_t)0, transactionLog.size()); |
| 525 | |
| 526 | ASSERT_EQ(NO_ERROR, transactionLog.put(item)); |
| 527 | ASSERT_EQ((size_t)1, transactionLog.size()); |
| 528 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 529 | auto item2 = std::make_shared<mediametrics::Item>("Key2"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 530 | (*item2).set("three", (int32_t)3) |
| 531 | .set("[Key1]three", (int32_t)3) |
| 532 | .setTimestamp(11); |
| 533 | |
| 534 | ASSERT_EQ(NO_ERROR, transactionLog.put(item2)); |
| 535 | ASSERT_EQ((size_t)2, transactionLog.size()); |
| 536 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 537 | auto item3 = std::make_shared<mediametrics::Item>("Key3"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 538 | (*item3).set("six", (int32_t)6) |
| 539 | .set("[Key1]four", (int32_t)4) // affects Key1 |
| 540 | .set("[Key1]five", (int32_t)5) // affects key1 |
| 541 | .setTimestamp(12); |
| 542 | |
| 543 | ASSERT_EQ(NO_ERROR, transactionLog.put(item3)); |
| 544 | ASSERT_EQ((size_t)2, transactionLog.size()); |
| 545 | } |
| 546 | |
| 547 | TEST(mediametrics_tests, audio_analytics_permission) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 548 | auto item = std::make_shared<mediametrics::Item>("audio.1"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 549 | (*item).set("one", (int32_t)1) |
| 550 | .set("two", (int32_t)2) |
| 551 | .setTimestamp(10); |
| 552 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 553 | auto item2 = std::make_shared<mediametrics::Item>("audio.1"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 554 | (*item2).set("three", (int32_t)3) |
| 555 | .setTimestamp(11); |
| 556 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 557 | auto item3 = std::make_shared<mediametrics::Item>("audio.2"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 558 | (*item3).set("four", (int32_t)4) |
| 559 | .setTimestamp(12); |
| 560 | |
| 561 | android::mediametrics::AudioAnalytics audioAnalytics; |
| 562 | |
| 563 | // untrusted entities cannot create a new key. |
| 564 | ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item, false /* isTrusted */)); |
| 565 | ASSERT_EQ(PERMISSION_DENIED, audioAnalytics.submit(item2, false /* isTrusted */)); |
| 566 | |
| 567 | // TODO: Verify contents of AudioAnalytics. |
| 568 | // Currently there is no getter API in AudioAnalytics besides dump. |
| 569 | ASSERT_EQ(4, audioAnalytics.dump(1000).second /* lines */); |
| 570 | |
| 571 | ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */)); |
| 572 | // untrusted entities can add to an existing key |
| 573 | ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */)); |
| 574 | |
| 575 | // Check that we have some info in the dump. |
| 576 | ASSERT_LT(4, audioAnalytics.dump(1000).second /* lines */); |
| 577 | } |
| 578 | |
| 579 | TEST(mediametrics_tests, audio_analytics_dump) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 580 | auto item = std::make_shared<mediametrics::Item>("audio.1"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 581 | (*item).set("one", (int32_t)1) |
| 582 | .set("two", (int32_t)2) |
| 583 | .setTimestamp(10); |
| 584 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 585 | auto item2 = std::make_shared<mediametrics::Item>("audio.1"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 586 | (*item2).set("three", (int32_t)3) |
| 587 | .setTimestamp(11); |
| 588 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame^] | 589 | auto item3 = std::make_shared<mediametrics::Item>("audio.2"); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 590 | (*item3).set("four", (int32_t)4) |
| 591 | .setTimestamp(12); |
| 592 | |
| 593 | android::mediametrics::AudioAnalytics audioAnalytics; |
| 594 | |
| 595 | ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item, true /* isTrusted */)); |
| 596 | // untrusted entities can add to an existing key |
| 597 | ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item2, false /* isTrusted */)); |
| 598 | ASSERT_EQ(NO_ERROR, audioAnalytics.submit(item3, true /* isTrusted */)); |
| 599 | |
| 600 | // find out how many lines we have. |
| 601 | auto [string, lines] = audioAnalytics.dump(1000); |
| 602 | ASSERT_EQ(lines, (int32_t) countNewlines(string.c_str())); |
| 603 | |
| 604 | printf("AudioAnalytics: %s", string.c_str()); |
| 605 | // ensure that dump operates over those lines. |
| 606 | for (int32_t ll = 0; ll < lines; ++ll) { |
| 607 | auto [s, l] = audioAnalytics.dump(ll); |
| 608 | ASSERT_EQ(ll, l); |
| 609 | ASSERT_EQ(ll, (int32_t) countNewlines(s.c_str())); |
| 610 | } |
| 611 | } |