Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 15 | #include "src/logd/LogEvent.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 16 | #include <gtest/gtest.h> |
| 17 | #include <log/log_event_list.h> |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 18 | #include "frameworks/base/cmds/statsd/src/atoms.pb.h" |
| 19 | #include "frameworks/base/core/proto/android/stats/launcher/launcher.pb.h" |
Ruchir Rastogi | 1fb525e | 2019-11-07 14:08:24 -0800 | [diff] [blame] | 20 | #include <stats_event.h> |
| 21 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 22 | |
| 23 | #ifdef __ANDROID__ |
| 24 | |
| 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 29 | using std::string; |
Ruchir Rastogi | 1fb525e | 2019-11-07 14:08:24 -0800 | [diff] [blame] | 30 | using std::vector; |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 31 | using util::ProtoOutputStream; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 32 | using util::ProtoReader; |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 33 | |
Ruchir Rastogi | 1fb525e | 2019-11-07 14:08:24 -0800 | [diff] [blame] | 34 | |
| 35 | #ifdef NEW_ENCODING_SCHEME |
| 36 | |
| 37 | Field getField(int32_t tag, const vector<int32_t>& pos, int32_t depth, const vector<bool>& last) { |
| 38 | Field f(tag, (int32_t*)pos.data(), depth); |
| 39 | |
| 40 | // For loop starts at 1 because the last field at depth 0 is not decorated. |
| 41 | for (int i = 1; i < depth; i++) { |
| 42 | if (last[i]) f.decorateLastPos(i); |
| 43 | } |
| 44 | |
| 45 | return f; |
| 46 | } |
| 47 | |
| 48 | TEST(LogEventTest, TestPrimitiveParsing) { |
| 49 | struct stats_event* event = stats_event_obtain(); |
| 50 | stats_event_set_atom_id(event, 100); |
| 51 | stats_event_write_int32(event, 10); |
| 52 | stats_event_write_int64(event, 0x123456789); |
| 53 | stats_event_write_float(event, 2.0); |
| 54 | stats_event_write_bool(event, true); |
| 55 | stats_event_build(event); |
| 56 | |
| 57 | size_t size; |
| 58 | uint8_t* buf = stats_event_get_buffer(event, &size); |
| 59 | |
| 60 | LogEvent logEvent(buf, size, /*uid=*/ 1000); |
| 61 | EXPECT_TRUE(logEvent.isValid()); |
| 62 | EXPECT_EQ(100, logEvent.GetTagId()); |
| 63 | |
| 64 | const vector<FieldValue>& values = logEvent.getValues(); |
| 65 | EXPECT_EQ(4, values.size()); |
| 66 | |
| 67 | const FieldValue& int32Item = values[0]; |
| 68 | Field expectedField = getField(100, {1, 1, 1}, 0, {false, false, false}); |
| 69 | EXPECT_EQ(expectedField, int32Item.mField); |
| 70 | EXPECT_EQ(Type::INT, int32Item.mValue.getType()); |
| 71 | EXPECT_EQ(10, int32Item.mValue.int_value); |
| 72 | |
| 73 | const FieldValue& int64Item = values[1]; |
| 74 | expectedField = getField(100, {2, 1, 1}, 0, {false, false, false}); |
| 75 | EXPECT_EQ(expectedField, int64Item.mField); |
| 76 | EXPECT_EQ(Type::LONG, int64Item.mValue.getType()); |
| 77 | EXPECT_EQ(0x123456789, int64Item.mValue.long_value); |
| 78 | |
| 79 | const FieldValue& floatItem = values[2]; |
| 80 | expectedField = getField(100, {3, 1, 1}, 0, {false, false, false}); |
| 81 | EXPECT_EQ(expectedField, floatItem.mField); |
| 82 | EXPECT_EQ(Type::FLOAT, floatItem.mValue.getType()); |
| 83 | EXPECT_EQ(2.0, floatItem.mValue.float_value); |
| 84 | |
| 85 | const FieldValue& boolItem = values[3]; |
| 86 | expectedField = getField(100, {4, 1, 1}, 0, {true, false, false}); |
| 87 | EXPECT_EQ(expectedField, boolItem.mField); |
| 88 | EXPECT_EQ(Type::INT, boolItem.mValue.getType()); // FieldValue does not support boolean type |
| 89 | EXPECT_EQ(1, boolItem.mValue.int_value); |
| 90 | |
| 91 | stats_event_release(event); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | TEST(LogEventTest, TestStringAndByteArrayParsing) { |
| 96 | struct stats_event* event = stats_event_obtain(); |
| 97 | stats_event_set_atom_id(event, 100); |
| 98 | string str = "test"; |
| 99 | stats_event_write_string8(event, str.c_str()); |
| 100 | stats_event_write_byte_array(event, (uint8_t*)str.c_str(), str.length()); |
| 101 | stats_event_build(event); |
| 102 | |
| 103 | size_t size; |
| 104 | uint8_t* buf = stats_event_get_buffer(event, &size); |
| 105 | |
| 106 | LogEvent logEvent(buf, size, /*uid=*/ 1000); |
| 107 | EXPECT_TRUE(logEvent.isValid()); |
| 108 | EXPECT_EQ(100, logEvent.GetTagId()); |
| 109 | |
| 110 | const vector<FieldValue>& values = logEvent.getValues(); |
| 111 | EXPECT_EQ(2, values.size()); |
| 112 | |
| 113 | const FieldValue& stringItem = values[0]; |
| 114 | Field expectedField = getField(100, {1, 1, 1}, 0, {false, false, false}); |
| 115 | EXPECT_EQ(expectedField, stringItem.mField); |
| 116 | EXPECT_EQ(Type::STRING, stringItem.mValue.getType()); |
| 117 | EXPECT_EQ(str, stringItem.mValue.str_value); |
| 118 | |
| 119 | const FieldValue& storageItem = values[1]; |
| 120 | expectedField = getField(100, {2, 1, 1}, 0, {true, false, false}); |
| 121 | EXPECT_EQ(expectedField, storageItem.mField); |
| 122 | EXPECT_EQ(Type::STORAGE, storageItem.mValue.getType()); |
| 123 | vector<uint8_t> expectedValue = {'t', 'e', 's', 't'}; |
| 124 | EXPECT_EQ(expectedValue, storageItem.mValue.storage_value); |
| 125 | |
| 126 | stats_event_release(event); |
| 127 | } |
| 128 | |
| 129 | TEST(LogEventTest, TestEmptyString) { |
| 130 | struct stats_event* event = stats_event_obtain(); |
| 131 | stats_event_set_atom_id(event, 100); |
| 132 | string empty = ""; |
| 133 | stats_event_write_string8(event, empty.c_str()); |
| 134 | stats_event_build(event); |
| 135 | |
| 136 | size_t size; |
| 137 | uint8_t* buf = stats_event_get_buffer(event, &size); |
| 138 | |
| 139 | LogEvent logEvent(buf, size, /*uid=*/ 1000); |
| 140 | EXPECT_TRUE(logEvent.isValid()); |
| 141 | EXPECT_EQ(100, logEvent.GetTagId()); |
| 142 | |
| 143 | const vector<FieldValue>& values = logEvent.getValues(); |
| 144 | EXPECT_EQ(1, values.size()); |
| 145 | |
| 146 | const FieldValue& item = values[0]; |
| 147 | Field expectedField = getField(100, {1, 1, 1}, 0, {true, false, false}); |
| 148 | EXPECT_EQ(expectedField, item.mField); |
| 149 | EXPECT_EQ(Type::STRING, item.mValue.getType()); |
| 150 | EXPECT_EQ(empty, item.mValue.str_value); |
| 151 | |
| 152 | stats_event_release(event); |
| 153 | } |
| 154 | |
| 155 | TEST(LogEventTest, TestByteArrayWithNullCharacter) { |
| 156 | struct stats_event* event = stats_event_obtain(); |
| 157 | stats_event_set_atom_id(event, 100); |
| 158 | uint8_t message[] = {'\t', 'e', '\0', 's', 't'}; |
| 159 | stats_event_write_byte_array(event, message, 5); |
| 160 | stats_event_build(event); |
| 161 | |
| 162 | size_t size; |
| 163 | uint8_t* buf = stats_event_get_buffer(event, &size); |
| 164 | |
| 165 | LogEvent logEvent(buf, size, /*uid=*/ 1000); |
| 166 | EXPECT_TRUE(logEvent.isValid()); |
| 167 | EXPECT_EQ(100, logEvent.GetTagId()); |
| 168 | |
| 169 | const vector<FieldValue>& values = logEvent.getValues(); |
| 170 | EXPECT_EQ(1, values.size()); |
| 171 | |
| 172 | const FieldValue& item = values[0]; |
| 173 | Field expectedField = getField(100, {1, 1, 1}, 0, {true, false, false}); |
| 174 | EXPECT_EQ(expectedField, item.mField); |
| 175 | EXPECT_EQ(Type::STORAGE, item.mValue.getType()); |
| 176 | vector<uint8_t> expectedValue(message, message + 5); |
| 177 | EXPECT_EQ(expectedValue, item.mValue.storage_value); |
| 178 | |
| 179 | stats_event_release(event); |
| 180 | } |
| 181 | |
| 182 | TEST(LogEventTest, TestKeyValuePairs) { |
| 183 | struct stats_event* event = stats_event_obtain(); |
| 184 | stats_event_set_atom_id(event, 100); |
| 185 | |
| 186 | struct key_value_pair pairs[4]; |
| 187 | pairs[0] = {.key = 0, .valueType = INT32_TYPE, .int32Value = 1}; |
| 188 | pairs[1] = {.key = 1, .valueType = INT64_TYPE, .int64Value = 0x123456789}; |
| 189 | pairs[2] = {.key = 2, .valueType = FLOAT_TYPE, .floatValue = 2.0}; |
| 190 | string str = "test"; |
| 191 | pairs[3] = {.key = 3, .valueType = STRING_TYPE, .stringValue = str.c_str()}; |
| 192 | |
| 193 | stats_event_write_key_value_pairs(event, pairs, 4); |
| 194 | stats_event_build(event); |
| 195 | |
| 196 | size_t size; |
| 197 | uint8_t* buf = stats_event_get_buffer(event, &size); |
| 198 | |
| 199 | LogEvent logEvent(buf, size, /*uid=*/ 1000); |
| 200 | EXPECT_TRUE(logEvent.isValid()); |
| 201 | EXPECT_EQ(100, logEvent.GetTagId()); |
| 202 | |
| 203 | const vector<FieldValue>& values = logEvent.getValues(); |
| 204 | EXPECT_EQ(8, values.size()); // 2 FieldValues per key-value pair |
| 205 | |
| 206 | // Check the keys first |
| 207 | for (int i = 0; i < values.size() / 2; i++) { |
| 208 | const FieldValue& item = values[2 * i]; |
| 209 | int32_t depth1Pos = i + 1; |
| 210 | bool depth1Last = i == (values.size() / 2 - 1); |
| 211 | Field expectedField = getField(100, {1, depth1Pos, 1}, 2, {true, depth1Last, false}); |
| 212 | |
| 213 | EXPECT_EQ(expectedField, item.mField); |
| 214 | EXPECT_EQ(Type::INT, item.mValue.getType()); |
| 215 | EXPECT_EQ(i, item.mValue.int_value); |
| 216 | } |
| 217 | |
| 218 | // Check the values now |
| 219 | // Note: pos[2] = index of type in KeyValuePair in atoms.proto |
| 220 | const FieldValue& int32Item = values[1]; |
| 221 | Field expectedField = getField(100, {1, 1, 2}, 2, {true, false, true}); |
| 222 | EXPECT_EQ(expectedField, int32Item.mField); |
| 223 | EXPECT_EQ(Type::INT, int32Item.mValue.getType()); |
| 224 | EXPECT_EQ(1, int32Item.mValue.int_value); |
| 225 | |
| 226 | const FieldValue& int64Item = values[3]; |
| 227 | expectedField = getField(100, {1, 2, 3}, 2, {true, false, true}); |
| 228 | EXPECT_EQ(expectedField, int64Item.mField); |
| 229 | EXPECT_EQ(Type::LONG, int64Item.mValue.getType()); |
| 230 | EXPECT_EQ(0x123456789, int64Item.mValue.long_value); |
| 231 | |
| 232 | const FieldValue& floatItem = values[5]; |
| 233 | expectedField = getField(100, {1, 3, 5}, 2, {true, false, true}); |
| 234 | EXPECT_EQ(expectedField, floatItem.mField); |
| 235 | EXPECT_EQ(Type::FLOAT, floatItem.mValue.getType()); |
| 236 | EXPECT_EQ(2.0, floatItem.mValue.float_value); |
| 237 | |
| 238 | const FieldValue& stringItem = values[7]; |
| 239 | expectedField = getField(100, {1, 4, 4}, 2, {true, true, true}); |
| 240 | EXPECT_EQ(expectedField, stringItem.mField); |
| 241 | EXPECT_EQ(Type::STRING, stringItem.mValue.getType()); |
| 242 | EXPECT_EQ(str, stringItem.mValue.str_value); |
| 243 | |
| 244 | stats_event_release(event); |
| 245 | } |
| 246 | |
| 247 | TEST(LogEventTest, TestAttributionChain) { |
| 248 | struct stats_event* event = stats_event_obtain(); |
| 249 | stats_event_set_atom_id(event, 100); |
| 250 | |
| 251 | string tag1 = "tag1"; |
| 252 | string tag2 = "tag2"; |
| 253 | |
| 254 | uint32_t uids[] = {1001, 1002}; |
| 255 | const char* tags[] = {tag1.c_str(), tag2.c_str()}; |
| 256 | |
| 257 | stats_event_write_attribution_chain(event, uids, tags, 2); |
| 258 | stats_event_build(event); |
| 259 | |
| 260 | size_t size; |
| 261 | uint8_t* buf = stats_event_get_buffer(event, &size); |
| 262 | |
| 263 | LogEvent logEvent(buf, size, /*uid=*/ 1000); |
| 264 | EXPECT_TRUE(logEvent.isValid()); |
| 265 | EXPECT_EQ(100, logEvent.GetTagId()); |
| 266 | |
| 267 | const vector<FieldValue>& values = logEvent.getValues(); |
| 268 | EXPECT_EQ(4, values.size()); // 2 per attribution node |
| 269 | |
| 270 | // Check first attribution node |
| 271 | const FieldValue& uid1Item = values[0]; |
| 272 | Field expectedField = getField(100, {1, 1, 1}, 2, {true, false, false}); |
| 273 | EXPECT_EQ(expectedField, uid1Item.mField); |
| 274 | EXPECT_EQ(Type::INT, uid1Item.mValue.getType()); |
| 275 | EXPECT_EQ(1001, uid1Item.mValue.int_value); |
| 276 | |
| 277 | const FieldValue& tag1Item = values[1]; |
| 278 | expectedField = getField(100, {1, 1, 2}, 2, {true, false, true}); |
| 279 | EXPECT_EQ(expectedField, tag1Item.mField); |
| 280 | EXPECT_EQ(Type::STRING, tag1Item.mValue.getType()); |
| 281 | EXPECT_EQ(tag1, tag1Item.mValue.str_value); |
| 282 | |
| 283 | // Check second attribution nodes |
| 284 | const FieldValue& uid2Item = values[2]; |
| 285 | expectedField = getField(100, {1, 2, 1}, 2, {true, true, false}); |
| 286 | EXPECT_EQ(expectedField, uid2Item.mField); |
| 287 | EXPECT_EQ(Type::INT, uid2Item.mValue.getType()); |
| 288 | EXPECT_EQ(1002, uid2Item.mValue.int_value); |
| 289 | |
| 290 | const FieldValue& tag2Item = values[3]; |
| 291 | expectedField = getField(100, {1, 2, 2}, 2, {true, true, true}); |
| 292 | EXPECT_EQ(expectedField, tag2Item.mField); |
| 293 | EXPECT_EQ(Type::STRING, tag2Item.mValue.getType()); |
| 294 | EXPECT_EQ(tag2, tag2Item.mValue.str_value); |
| 295 | |
| 296 | stats_event_release(event); |
| 297 | } |
| 298 | |
| 299 | #else // NEW_ENCODING_SCHEME |
| 300 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 301 | TEST(LogEventTest, TestLogParsing) { |
| 302 | LogEvent event1(1, 2000); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 303 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 304 | std::vector<AttributionNodeInternal> nodes; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 305 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 306 | AttributionNodeInternal node1; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 307 | node1.set_uid(1000); |
| 308 | node1.set_tag("tag1"); |
| 309 | nodes.push_back(node1); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 310 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 311 | AttributionNodeInternal node2; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 312 | node2.set_uid(2000); |
| 313 | node2.set_tag("tag2"); |
| 314 | nodes.push_back(node2); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 315 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 316 | event1.write(nodes); |
| 317 | event1.write("hello"); |
| 318 | event1.write((int32_t)10); |
| 319 | event1.write((int64_t)20); |
| 320 | event1.write((float)1.1); |
| 321 | event1.init(); |
| 322 | |
| 323 | const auto& items = event1.getValues(); |
| 324 | EXPECT_EQ((size_t)8, items.size()); |
| 325 | EXPECT_EQ(1, event1.GetTagId()); |
| 326 | |
| 327 | const FieldValue& item0 = event1.getValues()[0]; |
| 328 | EXPECT_EQ(0x2010101, item0.mField.getField()); |
| 329 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
| 330 | EXPECT_EQ(1000, item0.mValue.int_value); |
| 331 | |
| 332 | const FieldValue& item1 = event1.getValues()[1]; |
| 333 | EXPECT_EQ(0x2010182, item1.mField.getField()); |
| 334 | EXPECT_EQ(Type::STRING, item1.mValue.getType()); |
| 335 | EXPECT_EQ("tag1", item1.mValue.str_value); |
| 336 | |
| 337 | const FieldValue& item2 = event1.getValues()[2]; |
| 338 | EXPECT_EQ(0x2018201, item2.mField.getField()); |
| 339 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
| 340 | EXPECT_EQ(2000, item2.mValue.int_value); |
| 341 | |
| 342 | const FieldValue& item3 = event1.getValues()[3]; |
| 343 | EXPECT_EQ(0x2018282, item3.mField.getField()); |
| 344 | EXPECT_EQ(Type::STRING, item3.mValue.getType()); |
| 345 | EXPECT_EQ("tag2", item3.mValue.str_value); |
| 346 | |
| 347 | const FieldValue& item4 = event1.getValues()[4]; |
| 348 | EXPECT_EQ(0x20000, item4.mField.getField()); |
| 349 | EXPECT_EQ(Type::STRING, item4.mValue.getType()); |
| 350 | EXPECT_EQ("hello", item4.mValue.str_value); |
| 351 | |
| 352 | const FieldValue& item5 = event1.getValues()[5]; |
| 353 | EXPECT_EQ(0x30000, item5.mField.getField()); |
| 354 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
| 355 | EXPECT_EQ(10, item5.mValue.int_value); |
| 356 | |
| 357 | const FieldValue& item6 = event1.getValues()[6]; |
| 358 | EXPECT_EQ(0x40000, item6.mField.getField()); |
| 359 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 360 | EXPECT_EQ((int64_t)20, item6.mValue.long_value); |
| 361 | |
| 362 | const FieldValue& item7 = event1.getValues()[7]; |
| 363 | EXPECT_EQ(0x50000, item7.mField.getField()); |
| 364 | EXPECT_EQ(Type::FLOAT, item7.mValue.getType()); |
| 365 | EXPECT_EQ((float)1.1, item7.mValue.float_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 368 | TEST(LogEventTest, TestKeyValuePairsAtomParsing) { |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 369 | LogEvent event1(83, 2000, 1000); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 370 | std::map<int32_t, int32_t> int_map; |
| 371 | std::map<int32_t, int64_t> long_map; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 372 | std::map<int32_t, std::string> string_map; |
| 373 | std::map<int32_t, float> float_map; |
| 374 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 375 | int_map[11] = 123; |
| 376 | int_map[22] = 345; |
| 377 | |
| 378 | long_map[33] = 678L; |
| 379 | long_map[44] = 890L; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 380 | |
| 381 | string_map[1] = "test2"; |
| 382 | string_map[2] = "test1"; |
| 383 | |
| 384 | float_map[111] = 2.2f; |
| 385 | float_map[222] = 1.1f; |
| 386 | |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 387 | EXPECT_TRUE(event1.writeKeyValuePairs(0, // Logging side logs 0 uid. |
| 388 | int_map, |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 389 | long_map, |
| 390 | string_map, |
| 391 | float_map)); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 392 | event1.init(); |
| 393 | |
| 394 | EXPECT_EQ(83, event1.GetTagId()); |
| 395 | const auto& items = event1.getValues(); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 396 | EXPECT_EQ((size_t)17, items.size()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 397 | |
| 398 | const FieldValue& item0 = event1.getValues()[0]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 399 | EXPECT_EQ(0x10000, item0.mField.getField()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 400 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 401 | EXPECT_EQ(1000, item0.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 402 | |
| 403 | const FieldValue& item1 = event1.getValues()[1]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 404 | EXPECT_EQ(0x2010201, item1.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 405 | EXPECT_EQ(Type::INT, item1.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 406 | EXPECT_EQ(11, item1.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 407 | |
| 408 | const FieldValue& item2 = event1.getValues()[2]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 409 | EXPECT_EQ(0x2010282, item2.mField.getField()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 410 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 411 | EXPECT_EQ(123, item2.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 412 | |
| 413 | const FieldValue& item3 = event1.getValues()[3]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 414 | EXPECT_EQ(0x2010301, item3.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 415 | EXPECT_EQ(Type::INT, item3.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 416 | EXPECT_EQ(22, item3.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 417 | |
| 418 | const FieldValue& item4 = event1.getValues()[4]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 419 | EXPECT_EQ(0x2010382, item4.mField.getField()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 420 | EXPECT_EQ(Type::INT, item4.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 421 | EXPECT_EQ(345, item4.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 422 | |
| 423 | const FieldValue& item5 = event1.getValues()[5]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 424 | EXPECT_EQ(0x2010401, item5.mField.getField()); |
| 425 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
| 426 | EXPECT_EQ(33, item5.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 427 | |
| 428 | const FieldValue& item6 = event1.getValues()[6]; |
Tej Singh | 2c96b5a | 2019-02-04 21:28:49 -0800 | [diff] [blame] | 429 | EXPECT_EQ(0x2010483, item6.mField.getField()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 430 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 431 | EXPECT_EQ(678L, item6.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 432 | |
| 433 | const FieldValue& item7 = event1.getValues()[7]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 434 | EXPECT_EQ(0x2010501, item7.mField.getField()); |
| 435 | EXPECT_EQ(Type::INT, item7.mValue.getType()); |
| 436 | EXPECT_EQ(44, item7.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 437 | |
| 438 | const FieldValue& item8 = event1.getValues()[8]; |
Tej Singh | 2c96b5a | 2019-02-04 21:28:49 -0800 | [diff] [blame] | 439 | EXPECT_EQ(0x2010583, item8.mField.getField()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 440 | EXPECT_EQ(Type::LONG, item8.mValue.getType()); |
| 441 | EXPECT_EQ(890L, item8.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 442 | |
| 443 | const FieldValue& item9 = event1.getValues()[9]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 444 | EXPECT_EQ(0x2010601, item9.mField.getField()); |
| 445 | EXPECT_EQ(Type::INT, item9.mValue.getType()); |
| 446 | EXPECT_EQ(1, item9.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 447 | |
| 448 | const FieldValue& item10 = event1.getValues()[10]; |
Tej Singh | 2c96b5a | 2019-02-04 21:28:49 -0800 | [diff] [blame] | 449 | EXPECT_EQ(0x2010684, item10.mField.getField()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 450 | EXPECT_EQ(Type::STRING, item10.mValue.getType()); |
| 451 | EXPECT_EQ("test2", item10.mValue.str_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 452 | |
| 453 | const FieldValue& item11 = event1.getValues()[11]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 454 | EXPECT_EQ(0x2010701, item11.mField.getField()); |
| 455 | EXPECT_EQ(Type::INT, item11.mValue.getType()); |
| 456 | EXPECT_EQ(2, item11.mValue.int_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 457 | |
| 458 | const FieldValue& item12 = event1.getValues()[12]; |
Tej Singh | 2c96b5a | 2019-02-04 21:28:49 -0800 | [diff] [blame] | 459 | EXPECT_EQ(0x2010784, item12.mField.getField()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 460 | EXPECT_EQ(Type::STRING, item12.mValue.getType()); |
| 461 | EXPECT_EQ("test1", item12.mValue.str_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 462 | |
| 463 | const FieldValue& item13 = event1.getValues()[13]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 464 | EXPECT_EQ(0x2010801, item13.mField.getField()); |
| 465 | EXPECT_EQ(Type::INT, item13.mValue.getType()); |
| 466 | EXPECT_EQ(111, item13.mValue.int_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 467 | |
| 468 | const FieldValue& item14 = event1.getValues()[14]; |
Tej Singh | 2c96b5a | 2019-02-04 21:28:49 -0800 | [diff] [blame] | 469 | EXPECT_EQ(0x2010885, item14.mField.getField()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 470 | EXPECT_EQ(Type::FLOAT, item14.mValue.getType()); |
| 471 | EXPECT_EQ(2.2f, item14.mValue.float_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 472 | |
| 473 | const FieldValue& item15 = event1.getValues()[15]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 474 | EXPECT_EQ(0x2018901, item15.mField.getField()); |
| 475 | EXPECT_EQ(Type::INT, item15.mValue.getType()); |
| 476 | EXPECT_EQ(222, item15.mValue.int_value); |
| 477 | |
| 478 | const FieldValue& item16 = event1.getValues()[16]; |
Tej Singh | 2c96b5a | 2019-02-04 21:28:49 -0800 | [diff] [blame] | 479 | EXPECT_EQ(0x2018985, item16.mField.getField()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 480 | EXPECT_EQ(Type::FLOAT, item16.mValue.getType()); |
| 481 | EXPECT_EQ(1.1f, item16.mValue.float_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 484 | TEST(LogEventTest, TestLogParsing2) { |
| 485 | LogEvent event1(1, 2000); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 486 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 487 | std::vector<AttributionNodeInternal> nodes; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 488 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 489 | event1.write("hello"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 490 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 491 | // repeated msg can be in the middle |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 492 | AttributionNodeInternal node1; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 493 | node1.set_uid(1000); |
| 494 | node1.set_tag("tag1"); |
| 495 | nodes.push_back(node1); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 496 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 497 | AttributionNodeInternal node2; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 498 | node2.set_uid(2000); |
| 499 | node2.set_tag("tag2"); |
| 500 | nodes.push_back(node2); |
| 501 | event1.write(nodes); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 502 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 503 | event1.write((int32_t)10); |
| 504 | event1.write((int64_t)20); |
| 505 | event1.write((float)1.1); |
| 506 | event1.init(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 507 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 508 | const auto& items = event1.getValues(); |
| 509 | EXPECT_EQ((size_t)8, items.size()); |
| 510 | EXPECT_EQ(1, event1.GetTagId()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 511 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 512 | const FieldValue& item = event1.getValues()[0]; |
| 513 | EXPECT_EQ(0x00010000, item.mField.getField()); |
| 514 | EXPECT_EQ(Type::STRING, item.mValue.getType()); |
| 515 | EXPECT_EQ("hello", item.mValue.str_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 516 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 517 | const FieldValue& item0 = event1.getValues()[1]; |
| 518 | EXPECT_EQ(0x2020101, item0.mField.getField()); |
| 519 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
| 520 | EXPECT_EQ(1000, item0.mValue.int_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 521 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 522 | const FieldValue& item1 = event1.getValues()[2]; |
| 523 | EXPECT_EQ(0x2020182, item1.mField.getField()); |
| 524 | EXPECT_EQ(Type::STRING, item1.mValue.getType()); |
| 525 | EXPECT_EQ("tag1", item1.mValue.str_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 526 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 527 | const FieldValue& item2 = event1.getValues()[3]; |
| 528 | EXPECT_EQ(0x2028201, item2.mField.getField()); |
| 529 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
| 530 | EXPECT_EQ(2000, item2.mValue.int_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 531 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 532 | const FieldValue& item3 = event1.getValues()[4]; |
| 533 | EXPECT_EQ(0x2028282, item3.mField.getField()); |
| 534 | EXPECT_EQ(Type::STRING, item3.mValue.getType()); |
| 535 | EXPECT_EQ("tag2", item3.mValue.str_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 536 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 537 | const FieldValue& item5 = event1.getValues()[5]; |
| 538 | EXPECT_EQ(0x30000, item5.mField.getField()); |
| 539 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
| 540 | EXPECT_EQ(10, item5.mValue.int_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 541 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 542 | const FieldValue& item6 = event1.getValues()[6]; |
| 543 | EXPECT_EQ(0x40000, item6.mField.getField()); |
| 544 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 545 | EXPECT_EQ((int64_t)20, item6.mValue.long_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 546 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 547 | const FieldValue& item7 = event1.getValues()[7]; |
| 548 | EXPECT_EQ(0x50000, item7.mField.getField()); |
| 549 | EXPECT_EQ(Type::FLOAT, item7.mValue.getType()); |
| 550 | EXPECT_EQ((float)1.1, item7.mValue.float_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 553 | TEST(LogEventTest, TestKeyValuePairsEvent) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 554 | std::map<int32_t, int32_t> int_map; |
| 555 | std::map<int32_t, int64_t> long_map; |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 556 | std::map<int32_t, std::string> string_map; |
| 557 | std::map<int32_t, float> float_map; |
| 558 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 559 | int_map[11] = 123; |
| 560 | int_map[22] = 345; |
| 561 | |
| 562 | long_map[33] = 678L; |
| 563 | long_map[44] = 890L; |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 564 | |
| 565 | string_map[1] = "test2"; |
| 566 | string_map[2] = "test1"; |
| 567 | |
| 568 | float_map[111] = 2.2f; |
| 569 | float_map[222] = 1.1f; |
| 570 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 571 | LogEvent event1(83, 2000, 2001, 10001, int_map, long_map, string_map, float_map); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 572 | event1.init(); |
| 573 | |
| 574 | EXPECT_EQ(83, event1.GetTagId()); |
| 575 | EXPECT_EQ((int64_t)2000, event1.GetLogdTimestampNs()); |
| 576 | EXPECT_EQ((int64_t)2001, event1.GetElapsedTimestampNs()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 577 | EXPECT_EQ((int64_t)10001, event1.GetUid()); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 578 | |
| 579 | const auto& items = event1.getValues(); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 580 | EXPECT_EQ((size_t)17, items.size()); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 581 | |
| 582 | const FieldValue& item0 = event1.getValues()[0]; |
| 583 | EXPECT_EQ(0x00010000, item0.mField.getField()); |
| 584 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
| 585 | EXPECT_EQ(10001, item0.mValue.int_value); |
| 586 | |
| 587 | const FieldValue& item1 = event1.getValues()[1]; |
| 588 | EXPECT_EQ(0x2020101, item1.mField.getField()); |
| 589 | EXPECT_EQ(Type::INT, item1.mValue.getType()); |
| 590 | EXPECT_EQ(11, item1.mValue.int_value); |
| 591 | |
| 592 | const FieldValue& item2 = event1.getValues()[2]; |
| 593 | EXPECT_EQ(0x2020182, item2.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 594 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
| 595 | EXPECT_EQ(123, item2.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 596 | |
| 597 | const FieldValue& item3 = event1.getValues()[3]; |
| 598 | EXPECT_EQ(0x2020201, item3.mField.getField()); |
| 599 | EXPECT_EQ(Type::INT, item3.mValue.getType()); |
| 600 | EXPECT_EQ(22, item3.mValue.int_value); |
| 601 | |
| 602 | const FieldValue& item4 = event1.getValues()[4]; |
| 603 | EXPECT_EQ(0x2020282, item4.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 604 | EXPECT_EQ(Type::INT, item4.mValue.getType()); |
| 605 | EXPECT_EQ(345, item4.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 606 | |
| 607 | const FieldValue& item5 = event1.getValues()[5]; |
| 608 | EXPECT_EQ(0x2020301, item5.mField.getField()); |
| 609 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 610 | EXPECT_EQ(33, item5.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 611 | |
| 612 | const FieldValue& item6 = event1.getValues()[6]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 613 | EXPECT_EQ(0x2020383, item6.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 614 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 615 | EXPECT_EQ(678L, item6.mValue.long_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 616 | |
| 617 | const FieldValue& item7 = event1.getValues()[7]; |
| 618 | EXPECT_EQ(0x2020401, item7.mField.getField()); |
| 619 | EXPECT_EQ(Type::INT, item7.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 620 | EXPECT_EQ(44, item7.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 621 | |
| 622 | const FieldValue& item8 = event1.getValues()[8]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 623 | EXPECT_EQ(0x2020483, item8.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 624 | EXPECT_EQ(Type::LONG, item8.mValue.getType()); |
| 625 | EXPECT_EQ(890L, item8.mValue.long_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 626 | |
| 627 | const FieldValue& item9 = event1.getValues()[9]; |
| 628 | EXPECT_EQ(0x2020501, item9.mField.getField()); |
| 629 | EXPECT_EQ(Type::INT, item9.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 630 | EXPECT_EQ(1, item9.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 631 | |
| 632 | const FieldValue& item10 = event1.getValues()[10]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 633 | EXPECT_EQ(0x2020584, item10.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 634 | EXPECT_EQ(Type::STRING, item10.mValue.getType()); |
| 635 | EXPECT_EQ("test2", item10.mValue.str_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 636 | |
| 637 | const FieldValue& item11 = event1.getValues()[11]; |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 638 | EXPECT_EQ(0x2020601, item11.mField.getField()); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 639 | EXPECT_EQ(Type::INT, item11.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 640 | EXPECT_EQ(2, item11.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 641 | |
| 642 | const FieldValue& item12 = event1.getValues()[12]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 643 | EXPECT_EQ(0x2020684, item12.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 644 | EXPECT_EQ(Type::STRING, item12.mValue.getType()); |
| 645 | EXPECT_EQ("test1", item12.mValue.str_value); |
| 646 | |
| 647 | const FieldValue& item13 = event1.getValues()[13]; |
| 648 | EXPECT_EQ(0x2020701, item13.mField.getField()); |
| 649 | EXPECT_EQ(Type::INT, item13.mValue.getType()); |
| 650 | EXPECT_EQ(111, item13.mValue.int_value); |
| 651 | |
| 652 | const FieldValue& item14 = event1.getValues()[14]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 653 | EXPECT_EQ(0x2020785, item14.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 654 | EXPECT_EQ(Type::FLOAT, item14.mValue.getType()); |
| 655 | EXPECT_EQ(2.2f, item14.mValue.float_value); |
| 656 | |
| 657 | const FieldValue& item15 = event1.getValues()[15]; |
| 658 | EXPECT_EQ(0x2028801, item15.mField.getField()); |
| 659 | EXPECT_EQ(Type::INT, item15.mValue.getType()); |
| 660 | EXPECT_EQ(222, item15.mValue.int_value); |
| 661 | |
| 662 | const FieldValue& item16 = event1.getValues()[16]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 663 | EXPECT_EQ(0x2028885, item16.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 664 | EXPECT_EQ(Type::FLOAT, item16.mValue.getType()); |
| 665 | EXPECT_EQ(1.1f, item16.mValue.float_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 668 | TEST(LogEventTest, TestBinaryFieldAtom) { |
| 669 | Atom launcherAtom; |
| 670 | auto launcher_event = launcherAtom.mutable_launcher_event(); |
| 671 | launcher_event->set_action(stats::launcher::LauncherAction::LONGPRESS); |
| 672 | launcher_event->set_src_state(stats::launcher::LauncherState::OVERVIEW); |
| 673 | launcher_event->set_dst_state(stats::launcher::LauncherState::ALLAPPS); |
| 674 | |
| 675 | auto extension = launcher_event->mutable_extension(); |
| 676 | |
| 677 | auto src_target = extension->add_src_target(); |
| 678 | src_target->set_type(stats::launcher::LauncherTarget_Type_ITEM_TYPE); |
| 679 | src_target->set_item(stats::launcher::LauncherTarget_Item_FOLDER_ICON); |
| 680 | |
| 681 | auto dst_target = extension->add_dst_target(); |
| 682 | dst_target->set_type(stats::launcher::LauncherTarget_Type_ITEM_TYPE); |
| 683 | dst_target->set_item(stats::launcher::LauncherTarget_Item_WIDGET); |
| 684 | |
| 685 | string extension_str; |
| 686 | extension->SerializeToString(&extension_str); |
| 687 | |
| 688 | LogEvent event1(Atom::kLauncherEventFieldNumber, 1000); |
| 689 | |
| 690 | event1.write((int32_t)stats::launcher::LauncherAction::LONGPRESS); |
| 691 | event1.write((int32_t)stats::launcher::LauncherState::OVERVIEW); |
| 692 | event1.write((int64_t)stats::launcher::LauncherState::ALLAPPS); |
Tej Singh | a02bfab | 2019-10-01 19:03:24 -0700 | [diff] [blame] | 693 | event1.writeBytes(extension_str); |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 694 | event1.init(); |
| 695 | |
| 696 | ProtoOutputStream proto; |
| 697 | event1.ToProto(proto); |
| 698 | |
| 699 | std::vector<uint8_t> outData; |
| 700 | outData.resize(proto.size()); |
| 701 | size_t pos = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 702 | sp<ProtoReader> reader = proto.data(); |
| 703 | while (reader->readBuffer() != NULL) { |
| 704 | size_t toRead = reader->currentToRead(); |
| 705 | std::memcpy(&(outData[pos]), reader->readBuffer(), toRead); |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 706 | pos += toRead; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 707 | reader->move(toRead); |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | std::string result_str(outData.begin(), outData.end()); |
| 711 | std::string orig_str; |
| 712 | launcherAtom.SerializeToString(&orig_str); |
| 713 | |
| 714 | EXPECT_EQ(orig_str, result_str); |
| 715 | } |
| 716 | |
Yao Chen | 8e6f998 | 2018-11-29 09:39:45 -0800 | [diff] [blame] | 717 | TEST(LogEventTest, TestBinaryFieldAtom_empty) { |
| 718 | Atom launcherAtom; |
| 719 | auto launcher_event = launcherAtom.mutable_launcher_event(); |
| 720 | launcher_event->set_action(stats::launcher::LauncherAction::LONGPRESS); |
| 721 | launcher_event->set_src_state(stats::launcher::LauncherState::OVERVIEW); |
| 722 | launcher_event->set_dst_state(stats::launcher::LauncherState::ALLAPPS); |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 723 | |
Yao Chen | 8e6f998 | 2018-11-29 09:39:45 -0800 | [diff] [blame] | 724 | // empty string. |
| 725 | string extension_str; |
| 726 | |
| 727 | LogEvent event1(Atom::kLauncherEventFieldNumber, 1000); |
| 728 | |
| 729 | event1.write((int32_t)stats::launcher::LauncherAction::LONGPRESS); |
| 730 | event1.write((int32_t)stats::launcher::LauncherState::OVERVIEW); |
| 731 | event1.write((int64_t)stats::launcher::LauncherState::ALLAPPS); |
Tej Singh | a02bfab | 2019-10-01 19:03:24 -0700 | [diff] [blame] | 732 | event1.writeBytes(extension_str); |
Yao Chen | 8e6f998 | 2018-11-29 09:39:45 -0800 | [diff] [blame] | 733 | event1.init(); |
| 734 | |
| 735 | ProtoOutputStream proto; |
| 736 | event1.ToProto(proto); |
| 737 | |
| 738 | std::vector<uint8_t> outData; |
| 739 | outData.resize(proto.size()); |
| 740 | size_t pos = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 741 | sp<ProtoReader> reader = proto.data(); |
| 742 | while (reader->readBuffer() != NULL) { |
| 743 | size_t toRead = reader->currentToRead(); |
| 744 | std::memcpy(&(outData[pos]), reader->readBuffer(), toRead); |
Yao Chen | 8e6f998 | 2018-11-29 09:39:45 -0800 | [diff] [blame] | 745 | pos += toRead; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 746 | reader->move(toRead); |
Yao Chen | 8e6f998 | 2018-11-29 09:39:45 -0800 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | std::string result_str(outData.begin(), outData.end()); |
| 750 | std::string orig_str; |
| 751 | launcherAtom.SerializeToString(&orig_str); |
| 752 | |
| 753 | EXPECT_EQ(orig_str, result_str); |
| 754 | } |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 755 | |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 756 | TEST(LogEventTest, TestWriteExperimentIdsToProto) { |
| 757 | std::vector<int64_t> expIds; |
| 758 | expIds.push_back(5038); |
| 759 | std::vector<uint8_t> proto; |
| 760 | |
| 761 | writeExperimentIdsToProto(expIds, &proto); |
| 762 | |
| 763 | EXPECT_EQ(proto.size(), 3); |
| 764 | // Proto wire format for field ID 1, varint |
| 765 | EXPECT_EQ(proto[0], 0x08); |
| 766 | // varint of 5038, 2 bytes long |
| 767 | EXPECT_EQ(proto[1], 0xae); |
| 768 | EXPECT_EQ(proto[2], 0x27); |
| 769 | } |
Ruchir Rastogi | 1fb525e | 2019-11-07 14:08:24 -0800 | [diff] [blame] | 770 | #endif // NEW_ENCODING_SCHEME |
Jeff Hamilton | fa2f91c | 2019-03-22 00:25:02 -0400 | [diff] [blame] | 771 | |
| 772 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 773 | } // namespace statsd |
| 774 | } // namespace os |
| 775 | } // namespace android |
| 776 | #else |
| 777 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 778 | #endif |