Home | History | Annotate | Download | only in tests
      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 
     15 #include "src/logd/LogEvent.h"
     16 #include <gtest/gtest.h>
     17 #include <log/log_event_list.h>
     18 #include "frameworks/base/cmds/statsd/src/atoms.pb.h"
     19 #include "frameworks/base/core/proto/android/stats/launcher/launcher.pb.h"
     20 
     21 #ifdef __ANDROID__
     22 
     23 namespace android {
     24 namespace os {
     25 namespace statsd {
     26 
     27 using std::string;
     28 using util::ProtoOutputStream;
     29 using util::ProtoReader;
     30 
     31 TEST(LogEventTest, TestLogParsing) {
     32     LogEvent event1(1, 2000);
     33 
     34     std::vector<AttributionNodeInternal> nodes;
     35 
     36     AttributionNodeInternal node1;
     37     node1.set_uid(1000);
     38     node1.set_tag("tag1");
     39     nodes.push_back(node1);
     40 
     41     AttributionNodeInternal node2;
     42     node2.set_uid(2000);
     43     node2.set_tag("tag2");
     44     nodes.push_back(node2);
     45 
     46     event1.write(nodes);
     47     event1.write("hello");
     48     event1.write((int32_t)10);
     49     event1.write((int64_t)20);
     50     event1.write((float)1.1);
     51     event1.init();
     52 
     53     const auto& items = event1.getValues();
     54     EXPECT_EQ((size_t)8, items.size());
     55     EXPECT_EQ(1, event1.GetTagId());
     56 
     57     const FieldValue& item0 = event1.getValues()[0];
     58     EXPECT_EQ(0x2010101, item0.mField.getField());
     59     EXPECT_EQ(Type::INT, item0.mValue.getType());
     60     EXPECT_EQ(1000, item0.mValue.int_value);
     61 
     62     const FieldValue& item1 = event1.getValues()[1];
     63     EXPECT_EQ(0x2010182, item1.mField.getField());
     64     EXPECT_EQ(Type::STRING, item1.mValue.getType());
     65     EXPECT_EQ("tag1", item1.mValue.str_value);
     66 
     67     const FieldValue& item2 = event1.getValues()[2];
     68     EXPECT_EQ(0x2018201, item2.mField.getField());
     69     EXPECT_EQ(Type::INT, item2.mValue.getType());
     70     EXPECT_EQ(2000, item2.mValue.int_value);
     71 
     72     const FieldValue& item3 = event1.getValues()[3];
     73     EXPECT_EQ(0x2018282, item3.mField.getField());
     74     EXPECT_EQ(Type::STRING, item3.mValue.getType());
     75     EXPECT_EQ("tag2", item3.mValue.str_value);
     76 
     77     const FieldValue& item4 = event1.getValues()[4];
     78     EXPECT_EQ(0x20000, item4.mField.getField());
     79     EXPECT_EQ(Type::STRING, item4.mValue.getType());
     80     EXPECT_EQ("hello", item4.mValue.str_value);
     81 
     82     const FieldValue& item5 = event1.getValues()[5];
     83     EXPECT_EQ(0x30000, item5.mField.getField());
     84     EXPECT_EQ(Type::INT, item5.mValue.getType());
     85     EXPECT_EQ(10, item5.mValue.int_value);
     86 
     87     const FieldValue& item6 = event1.getValues()[6];
     88     EXPECT_EQ(0x40000, item6.mField.getField());
     89     EXPECT_EQ(Type::LONG, item6.mValue.getType());
     90     EXPECT_EQ((int64_t)20, item6.mValue.long_value);
     91 
     92     const FieldValue& item7 = event1.getValues()[7];
     93     EXPECT_EQ(0x50000, item7.mField.getField());
     94     EXPECT_EQ(Type::FLOAT, item7.mValue.getType());
     95     EXPECT_EQ((float)1.1, item7.mValue.float_value);
     96 }
     97 
     98 TEST(LogEventTest, TestKeyValuePairsAtomParsing) {
     99     LogEvent event1(83, 2000, 1000);
    100     std::map<int32_t, int32_t> int_map;
    101     std::map<int32_t, int64_t> long_map;
    102     std::map<int32_t, std::string> string_map;
    103     std::map<int32_t, float> float_map;
    104 
    105     int_map[11] = 123;
    106     int_map[22] = 345;
    107 
    108     long_map[33] = 678L;
    109     long_map[44] = 890L;
    110 
    111     string_map[1] = "test2";
    112     string_map[2] = "test1";
    113 
    114     float_map[111] = 2.2f;
    115     float_map[222] = 1.1f;
    116 
    117     EXPECT_TRUE(event1.writeKeyValuePairs(0, // Logging side logs 0 uid.
    118                                           int_map,
    119                                           long_map,
    120                                           string_map,
    121                                           float_map));
    122     event1.init();
    123 
    124     EXPECT_EQ(83, event1.GetTagId());
    125     const auto& items = event1.getValues();
    126     EXPECT_EQ((size_t)17, items.size());
    127 
    128     const FieldValue& item0 = event1.getValues()[0];
    129     EXPECT_EQ(0x10000, item0.mField.getField());
    130     EXPECT_EQ(Type::INT, item0.mValue.getType());
    131     EXPECT_EQ(1000, item0.mValue.int_value);
    132 
    133     const FieldValue& item1 = event1.getValues()[1];
    134     EXPECT_EQ(0x2010201, item1.mField.getField());
    135     EXPECT_EQ(Type::INT, item1.mValue.getType());
    136     EXPECT_EQ(11, item1.mValue.int_value);
    137 
    138     const FieldValue& item2 = event1.getValues()[2];
    139     EXPECT_EQ(0x2010282, item2.mField.getField());
    140     EXPECT_EQ(Type::INT, item2.mValue.getType());
    141     EXPECT_EQ(123, item2.mValue.int_value);
    142 
    143     const FieldValue& item3 = event1.getValues()[3];
    144     EXPECT_EQ(0x2010301, item3.mField.getField());
    145     EXPECT_EQ(Type::INT, item3.mValue.getType());
    146     EXPECT_EQ(22, item3.mValue.int_value);
    147 
    148     const FieldValue& item4 = event1.getValues()[4];
    149     EXPECT_EQ(0x2010382, item4.mField.getField());
    150     EXPECT_EQ(Type::INT, item4.mValue.getType());
    151     EXPECT_EQ(345, item4.mValue.int_value);
    152 
    153     const FieldValue& item5 = event1.getValues()[5];
    154     EXPECT_EQ(0x2010401, item5.mField.getField());
    155     EXPECT_EQ(Type::INT, item5.mValue.getType());
    156     EXPECT_EQ(33, item5.mValue.int_value);
    157 
    158     const FieldValue& item6 = event1.getValues()[6];
    159     EXPECT_EQ(0x2010483, item6.mField.getField());
    160     EXPECT_EQ(Type::LONG, item6.mValue.getType());
    161     EXPECT_EQ(678L, item6.mValue.int_value);
    162 
    163     const FieldValue& item7 = event1.getValues()[7];
    164     EXPECT_EQ(0x2010501, item7.mField.getField());
    165     EXPECT_EQ(Type::INT, item7.mValue.getType());
    166     EXPECT_EQ(44, item7.mValue.int_value);
    167 
    168     const FieldValue& item8 = event1.getValues()[8];
    169     EXPECT_EQ(0x2010583, item8.mField.getField());
    170     EXPECT_EQ(Type::LONG, item8.mValue.getType());
    171     EXPECT_EQ(890L, item8.mValue.int_value);
    172 
    173     const FieldValue& item9 = event1.getValues()[9];
    174     EXPECT_EQ(0x2010601, item9.mField.getField());
    175     EXPECT_EQ(Type::INT, item9.mValue.getType());
    176     EXPECT_EQ(1, item9.mValue.int_value);
    177 
    178     const FieldValue& item10 = event1.getValues()[10];
    179     EXPECT_EQ(0x2010684, item10.mField.getField());
    180     EXPECT_EQ(Type::STRING, item10.mValue.getType());
    181     EXPECT_EQ("test2", item10.mValue.str_value);
    182 
    183     const FieldValue& item11 = event1.getValues()[11];
    184     EXPECT_EQ(0x2010701, item11.mField.getField());
    185     EXPECT_EQ(Type::INT, item11.mValue.getType());
    186     EXPECT_EQ(2, item11.mValue.int_value);
    187 
    188     const FieldValue& item12 = event1.getValues()[12];
    189     EXPECT_EQ(0x2010784, item12.mField.getField());
    190     EXPECT_EQ(Type::STRING, item12.mValue.getType());
    191     EXPECT_EQ("test1", item12.mValue.str_value);
    192 
    193     const FieldValue& item13 = event1.getValues()[13];
    194     EXPECT_EQ(0x2010801, item13.mField.getField());
    195     EXPECT_EQ(Type::INT, item13.mValue.getType());
    196     EXPECT_EQ(111, item13.mValue.int_value);
    197 
    198     const FieldValue& item14 = event1.getValues()[14];
    199     EXPECT_EQ(0x2010885, item14.mField.getField());
    200     EXPECT_EQ(Type::FLOAT, item14.mValue.getType());
    201     EXPECT_EQ(2.2f, item14.mValue.float_value);
    202 
    203     const FieldValue& item15 = event1.getValues()[15];
    204     EXPECT_EQ(0x2018901, item15.mField.getField());
    205     EXPECT_EQ(Type::INT, item15.mValue.getType());
    206     EXPECT_EQ(222, item15.mValue.int_value);
    207 
    208     const FieldValue& item16 = event1.getValues()[16];
    209     EXPECT_EQ(0x2018985, item16.mField.getField());
    210     EXPECT_EQ(Type::FLOAT, item16.mValue.getType());
    211     EXPECT_EQ(1.1f, item16.mValue.float_value);
    212 }
    213 
    214 TEST(LogEventTest, TestLogParsing2) {
    215     LogEvent event1(1, 2000);
    216 
    217     std::vector<AttributionNodeInternal> nodes;
    218 
    219     event1.write("hello");
    220 
    221     // repeated msg can be in the middle
    222     AttributionNodeInternal node1;
    223     node1.set_uid(1000);
    224     node1.set_tag("tag1");
    225     nodes.push_back(node1);
    226 
    227     AttributionNodeInternal node2;
    228     node2.set_uid(2000);
    229     node2.set_tag("tag2");
    230     nodes.push_back(node2);
    231     event1.write(nodes);
    232 
    233     event1.write((int32_t)10);
    234     event1.write((int64_t)20);
    235     event1.write((float)1.1);
    236     event1.init();
    237 
    238     const auto& items = event1.getValues();
    239     EXPECT_EQ((size_t)8, items.size());
    240     EXPECT_EQ(1, event1.GetTagId());
    241 
    242     const FieldValue& item = event1.getValues()[0];
    243     EXPECT_EQ(0x00010000, item.mField.getField());
    244     EXPECT_EQ(Type::STRING, item.mValue.getType());
    245     EXPECT_EQ("hello", item.mValue.str_value);
    246 
    247     const FieldValue& item0 = event1.getValues()[1];
    248     EXPECT_EQ(0x2020101, item0.mField.getField());
    249     EXPECT_EQ(Type::INT, item0.mValue.getType());
    250     EXPECT_EQ(1000, item0.mValue.int_value);
    251 
    252     const FieldValue& item1 = event1.getValues()[2];
    253     EXPECT_EQ(0x2020182, item1.mField.getField());
    254     EXPECT_EQ(Type::STRING, item1.mValue.getType());
    255     EXPECT_EQ("tag1", item1.mValue.str_value);
    256 
    257     const FieldValue& item2 = event1.getValues()[3];
    258     EXPECT_EQ(0x2028201, item2.mField.getField());
    259     EXPECT_EQ(Type::INT, item2.mValue.getType());
    260     EXPECT_EQ(2000, item2.mValue.int_value);
    261 
    262     const FieldValue& item3 = event1.getValues()[4];
    263     EXPECT_EQ(0x2028282, item3.mField.getField());
    264     EXPECT_EQ(Type::STRING, item3.mValue.getType());
    265     EXPECT_EQ("tag2", item3.mValue.str_value);
    266 
    267     const FieldValue& item5 = event1.getValues()[5];
    268     EXPECT_EQ(0x30000, item5.mField.getField());
    269     EXPECT_EQ(Type::INT, item5.mValue.getType());
    270     EXPECT_EQ(10, item5.mValue.int_value);
    271 
    272     const FieldValue& item6 = event1.getValues()[6];
    273     EXPECT_EQ(0x40000, item6.mField.getField());
    274     EXPECT_EQ(Type::LONG, item6.mValue.getType());
    275     EXPECT_EQ((int64_t)20, item6.mValue.long_value);
    276 
    277     const FieldValue& item7 = event1.getValues()[7];
    278     EXPECT_EQ(0x50000, item7.mField.getField());
    279     EXPECT_EQ(Type::FLOAT, item7.mValue.getType());
    280     EXPECT_EQ((float)1.1, item7.mValue.float_value);
    281 }
    282 
    283 TEST(LogEventTest, TestKeyValuePairsEvent) {
    284     std::map<int32_t, int32_t> int_map;
    285     std::map<int32_t, int64_t> long_map;
    286     std::map<int32_t, std::string> string_map;
    287     std::map<int32_t, float> float_map;
    288 
    289     int_map[11] = 123;
    290     int_map[22] = 345;
    291 
    292     long_map[33] = 678L;
    293     long_map[44] = 890L;
    294 
    295     string_map[1] = "test2";
    296     string_map[2] = "test1";
    297 
    298     float_map[111] = 2.2f;
    299     float_map[222] = 1.1f;
    300 
    301     LogEvent event1(83, 2000, 2001, 10001, int_map, long_map, string_map, float_map);
    302     event1.init();
    303 
    304     EXPECT_EQ(83, event1.GetTagId());
    305     EXPECT_EQ((int64_t)2000, event1.GetLogdTimestampNs());
    306     EXPECT_EQ((int64_t)2001, event1.GetElapsedTimestampNs());
    307     EXPECT_EQ((int64_t)10001, event1.GetUid());
    308 
    309     const auto& items = event1.getValues();
    310     EXPECT_EQ((size_t)17, items.size());
    311 
    312     const FieldValue& item0 = event1.getValues()[0];
    313     EXPECT_EQ(0x00010000, item0.mField.getField());
    314     EXPECT_EQ(Type::INT, item0.mValue.getType());
    315     EXPECT_EQ(10001, item0.mValue.int_value);
    316 
    317     const FieldValue& item1 = event1.getValues()[1];
    318     EXPECT_EQ(0x2020101, item1.mField.getField());
    319     EXPECT_EQ(Type::INT, item1.mValue.getType());
    320     EXPECT_EQ(11, item1.mValue.int_value);
    321 
    322     const FieldValue& item2 = event1.getValues()[2];
    323     EXPECT_EQ(0x2020182, item2.mField.getField());
    324     EXPECT_EQ(Type::INT, item2.mValue.getType());
    325     EXPECT_EQ(123, item2.mValue.int_value);
    326 
    327     const FieldValue& item3 = event1.getValues()[3];
    328     EXPECT_EQ(0x2020201, item3.mField.getField());
    329     EXPECT_EQ(Type::INT, item3.mValue.getType());
    330     EXPECT_EQ(22, item3.mValue.int_value);
    331 
    332     const FieldValue& item4 = event1.getValues()[4];
    333     EXPECT_EQ(0x2020282, item4.mField.getField());
    334     EXPECT_EQ(Type::INT, item4.mValue.getType());
    335     EXPECT_EQ(345, item4.mValue.int_value);
    336 
    337     const FieldValue& item5 = event1.getValues()[5];
    338     EXPECT_EQ(0x2020301, item5.mField.getField());
    339     EXPECT_EQ(Type::INT, item5.mValue.getType());
    340     EXPECT_EQ(33, item5.mValue.int_value);
    341 
    342     const FieldValue& item6 = event1.getValues()[6];
    343     EXPECT_EQ(0x2020383, item6.mField.getField());
    344     EXPECT_EQ(Type::LONG, item6.mValue.getType());
    345     EXPECT_EQ(678L, item6.mValue.long_value);
    346 
    347     const FieldValue& item7 = event1.getValues()[7];
    348     EXPECT_EQ(0x2020401, item7.mField.getField());
    349     EXPECT_EQ(Type::INT, item7.mValue.getType());
    350     EXPECT_EQ(44, item7.mValue.int_value);
    351 
    352     const FieldValue& item8 = event1.getValues()[8];
    353     EXPECT_EQ(0x2020483, item8.mField.getField());
    354     EXPECT_EQ(Type::LONG, item8.mValue.getType());
    355     EXPECT_EQ(890L, item8.mValue.long_value);
    356 
    357     const FieldValue& item9 = event1.getValues()[9];
    358     EXPECT_EQ(0x2020501, item9.mField.getField());
    359     EXPECT_EQ(Type::INT, item9.mValue.getType());
    360     EXPECT_EQ(1, item9.mValue.int_value);
    361 
    362     const FieldValue& item10 = event1.getValues()[10];
    363     EXPECT_EQ(0x2020584, item10.mField.getField());
    364     EXPECT_EQ(Type::STRING, item10.mValue.getType());
    365     EXPECT_EQ("test2", item10.mValue.str_value);
    366 
    367     const FieldValue& item11 = event1.getValues()[11];
    368     EXPECT_EQ(0x2020601, item11.mField.getField());
    369     EXPECT_EQ(Type::INT, item11.mValue.getType());
    370     EXPECT_EQ(2, item11.mValue.int_value);
    371 
    372     const FieldValue& item12 = event1.getValues()[12];
    373     EXPECT_EQ(0x2020684, item12.mField.getField());
    374     EXPECT_EQ(Type::STRING, item12.mValue.getType());
    375     EXPECT_EQ("test1", item12.mValue.str_value);
    376 
    377     const FieldValue& item13 = event1.getValues()[13];
    378     EXPECT_EQ(0x2020701, item13.mField.getField());
    379     EXPECT_EQ(Type::INT, item13.mValue.getType());
    380     EXPECT_EQ(111, item13.mValue.int_value);
    381 
    382     const FieldValue& item14 = event1.getValues()[14];
    383     EXPECT_EQ(0x2020785, item14.mField.getField());
    384     EXPECT_EQ(Type::FLOAT, item14.mValue.getType());
    385     EXPECT_EQ(2.2f, item14.mValue.float_value);
    386 
    387     const FieldValue& item15 = event1.getValues()[15];
    388     EXPECT_EQ(0x2028801, item15.mField.getField());
    389     EXPECT_EQ(Type::INT, item15.mValue.getType());
    390     EXPECT_EQ(222, item15.mValue.int_value);
    391 
    392     const FieldValue& item16 = event1.getValues()[16];
    393     EXPECT_EQ(0x2028885, item16.mField.getField());
    394     EXPECT_EQ(Type::FLOAT, item16.mValue.getType());
    395     EXPECT_EQ(1.1f, item16.mValue.float_value);
    396 }
    397 
    398 TEST(LogEventTest, TestStatsLogEventWrapperNoChain) {
    399     Parcel parcel;
    400     // tag id
    401     parcel.writeInt32(1);
    402     // elapsed realtime
    403     parcel.writeInt64(1111L);
    404     // wallclock time
    405     parcel.writeInt64(2222L);
    406     // no chain
    407     parcel.writeInt32(0);
    408     // 2 data
    409     parcel.writeInt32(2);
    410     // int 6
    411     parcel.writeInt32(1);
    412     parcel.writeInt32(6);
    413     // long 10
    414     parcel.writeInt32(2);
    415     parcel.writeInt64(10);
    416     parcel.setDataPosition(0);
    417 
    418     StatsLogEventWrapper statsLogEventWrapper;
    419     EXPECT_EQ(NO_ERROR, statsLogEventWrapper.readFromParcel(&parcel));
    420     EXPECT_EQ(1, statsLogEventWrapper.getTagId());
    421     EXPECT_EQ(1111L, statsLogEventWrapper.getElapsedRealTimeNs());
    422     EXPECT_EQ(2222L, statsLogEventWrapper.getWallClockTimeNs());
    423     EXPECT_EQ(0, statsLogEventWrapper.getWorkChains().size());
    424     EXPECT_EQ(2, statsLogEventWrapper.getElements().size());
    425     EXPECT_EQ(6, statsLogEventWrapper.getElements()[0].int_value);
    426     EXPECT_EQ(10L, statsLogEventWrapper.getElements()[1].long_value);
    427     LogEvent event(statsLogEventWrapper, -1);
    428     EXPECT_EQ(1, event.GetTagId());
    429     EXPECT_EQ(1111L, event.GetElapsedTimestampNs());
    430     EXPECT_EQ(2222L, event.GetLogdTimestampNs());
    431     EXPECT_EQ(2, event.size());
    432     EXPECT_EQ(6, event.getValues()[0].mValue.int_value);
    433     EXPECT_EQ(10, event.getValues()[1].mValue.long_value);
    434 }
    435 
    436 TEST(LogEventTest, TestStatsLogEventWrapperWithChain) {
    437     Parcel parcel;
    438     // tag id
    439     parcel.writeInt32(1);
    440     // elapsed realtime
    441     parcel.writeInt64(1111L);
    442     // wallclock time
    443     parcel.writeInt64(2222L);
    444     // 3 chains
    445     parcel.writeInt32(3);
    446     // chain1, 2 nodes (1, "tag1") (2, "tag2")
    447     parcel.writeInt32(2);
    448     parcel.writeInt32(1);
    449     parcel.writeString16(String16("tag1"));
    450     parcel.writeInt32(2);
    451     parcel.writeString16(String16("tag2"));
    452     // chain2, 1 node (3, "tag3")
    453     parcel.writeInt32(1);
    454     parcel.writeInt32(3);
    455     parcel.writeString16(String16("tag3"));
    456     // chain3, 2 nodes (4, "") (5, "")
    457     parcel.writeInt32(2);
    458     parcel.writeInt32(4);
    459     parcel.writeString16(String16(""));
    460     parcel.writeInt32(5);
    461     parcel.writeString16(String16(""));
    462     // 2 data
    463     parcel.writeInt32(2);
    464     // int 6
    465     parcel.writeInt32(1);
    466     parcel.writeInt32(6);
    467     // long 10
    468     parcel.writeInt32(2);
    469     parcel.writeInt64(10);
    470     parcel.setDataPosition(0);
    471 
    472     StatsLogEventWrapper statsLogEventWrapper;
    473     EXPECT_EQ(NO_ERROR, statsLogEventWrapper.readFromParcel(&parcel));
    474     EXPECT_EQ(1, statsLogEventWrapper.getTagId());
    475     EXPECT_EQ(1111L, statsLogEventWrapper.getElapsedRealTimeNs());
    476     EXPECT_EQ(2222L, statsLogEventWrapper.getWallClockTimeNs());
    477     EXPECT_EQ(3, statsLogEventWrapper.getWorkChains().size());
    478     EXPECT_EQ(2, statsLogEventWrapper.getWorkChains()[0].uids.size());
    479     EXPECT_EQ(1, statsLogEventWrapper.getWorkChains()[0].uids[0]);
    480     EXPECT_EQ(2, statsLogEventWrapper.getWorkChains()[0].uids[1]);
    481     EXPECT_EQ(2, statsLogEventWrapper.getWorkChains()[0].tags.size());
    482     EXPECT_EQ("tag1", statsLogEventWrapper.getWorkChains()[0].tags[0]);
    483     EXPECT_EQ("tag2", statsLogEventWrapper.getWorkChains()[0].tags[1]);
    484     EXPECT_EQ(1, statsLogEventWrapper.getWorkChains()[1].uids.size());
    485     EXPECT_EQ(3, statsLogEventWrapper.getWorkChains()[1].uids[0]);
    486     EXPECT_EQ(1, statsLogEventWrapper.getWorkChains()[1].tags.size());
    487     EXPECT_EQ("tag3", statsLogEventWrapper.getWorkChains()[1].tags[0]);
    488     EXPECT_EQ(2, statsLogEventWrapper.getElements().size());
    489     EXPECT_EQ(6, statsLogEventWrapper.getElements()[0].int_value);
    490     EXPECT_EQ(10L, statsLogEventWrapper.getElements()[1].long_value);
    491     EXPECT_EQ(2, statsLogEventWrapper.getWorkChains()[2].uids.size());
    492     EXPECT_EQ(4, statsLogEventWrapper.getWorkChains()[2].uids[0]);
    493     EXPECT_EQ(5, statsLogEventWrapper.getWorkChains()[2].uids[1]);
    494     EXPECT_EQ(2, statsLogEventWrapper.getWorkChains()[2].tags.size());
    495     EXPECT_EQ("", statsLogEventWrapper.getWorkChains()[2].tags[0]);
    496     EXPECT_EQ("", statsLogEventWrapper.getWorkChains()[2].tags[1]);
    497 
    498     LogEvent event(statsLogEventWrapper, -1);
    499     EXPECT_EQ(1, event.GetTagId());
    500     EXPECT_EQ(1111L, event.GetElapsedTimestampNs());
    501     EXPECT_EQ(2222L, event.GetLogdTimestampNs());
    502     EXPECT_EQ(2, event.size());
    503     EXPECT_EQ(6, event.getValues()[0].mValue.int_value);
    504     EXPECT_EQ(10, event.getValues()[1].mValue.long_value);
    505 
    506     LogEvent event1(statsLogEventWrapper, 0);
    507 
    508     EXPECT_EQ(1, event1.GetTagId());
    509     EXPECT_EQ(1111L, event1.GetElapsedTimestampNs());
    510     EXPECT_EQ(2222L, event1.GetLogdTimestampNs());
    511     EXPECT_EQ(6, event1.size());
    512     EXPECT_EQ(1, event1.getValues()[0].mValue.int_value);
    513     EXPECT_EQ(0x2010101, event1.getValues()[0].mField.getField());
    514     EXPECT_EQ("tag1", event1.getValues()[1].mValue.str_value);
    515     EXPECT_EQ(0x2010182, event1.getValues()[1].mField.getField());
    516     EXPECT_EQ(2, event1.getValues()[2].mValue.int_value);
    517     EXPECT_EQ(0x2010201, event1.getValues()[2].mField.getField());
    518     EXPECT_EQ("tag2", event1.getValues()[3].mValue.str_value);
    519     EXPECT_EQ(0x2018282, event1.getValues()[3].mField.getField());
    520     EXPECT_EQ(6, event1.getValues()[4].mValue.int_value);
    521     EXPECT_EQ(0x20000, event1.getValues()[4].mField.getField());
    522     EXPECT_EQ(10, event1.getValues()[5].mValue.long_value);
    523     EXPECT_EQ(0x30000, event1.getValues()[5].mField.getField());
    524 
    525     LogEvent event2(statsLogEventWrapper, 1);
    526 
    527     EXPECT_EQ(1, event2.GetTagId());
    528     EXPECT_EQ(1111L, event2.GetElapsedTimestampNs());
    529     EXPECT_EQ(2222L, event2.GetLogdTimestampNs());
    530     EXPECT_EQ(4, event2.size());
    531     EXPECT_EQ(3, event2.getValues()[0].mValue.int_value);
    532     EXPECT_EQ(0x2010101, event2.getValues()[0].mField.getField());
    533     EXPECT_EQ("tag3", event2.getValues()[1].mValue.str_value);
    534     EXPECT_EQ(0x2018182, event2.getValues()[1].mField.getField());
    535     EXPECT_EQ(6, event2.getValues()[2].mValue.int_value);
    536     EXPECT_EQ(0x20000, event2.getValues()[2].mField.getField());
    537     EXPECT_EQ(10, event2.getValues()[3].mValue.long_value);
    538     EXPECT_EQ(0x30000, event2.getValues()[3].mField.getField());
    539 
    540     LogEvent event3(statsLogEventWrapper, 2);
    541 
    542     EXPECT_EQ(1, event3.GetTagId());
    543     EXPECT_EQ(1111L, event3.GetElapsedTimestampNs());
    544     EXPECT_EQ(2222L, event3.GetLogdTimestampNs());
    545     EXPECT_EQ(6, event3.size());
    546     EXPECT_EQ(4, event3.getValues()[0].mValue.int_value);
    547     EXPECT_EQ(0x2010101, event3.getValues()[0].mField.getField());
    548     EXPECT_EQ("", event3.getValues()[1].mValue.str_value);
    549     EXPECT_EQ(0x2010182, event3.getValues()[1].mField.getField());
    550     EXPECT_EQ(5, event3.getValues()[2].mValue.int_value);
    551     EXPECT_EQ(0x2010201, event3.getValues()[2].mField.getField());
    552     EXPECT_EQ("", event3.getValues()[3].mValue.str_value);
    553     EXPECT_EQ(0x2018282, event3.getValues()[3].mField.getField());
    554     EXPECT_EQ(6, event3.getValues()[4].mValue.int_value);
    555     EXPECT_EQ(0x20000, event3.getValues()[4].mField.getField());
    556     EXPECT_EQ(10, event3.getValues()[5].mValue.long_value);
    557     EXPECT_EQ(0x30000, event3.getValues()[5].mField.getField());
    558 }
    559 
    560 TEST(LogEventTest, TestBinaryFieldAtom) {
    561     Atom launcherAtom;
    562     auto launcher_event = launcherAtom.mutable_launcher_event();
    563     launcher_event->set_action(stats::launcher::LauncherAction::LONGPRESS);
    564     launcher_event->set_src_state(stats::launcher::LauncherState::OVERVIEW);
    565     launcher_event->set_dst_state(stats::launcher::LauncherState::ALLAPPS);
    566 
    567     auto extension = launcher_event->mutable_extension();
    568 
    569     auto src_target = extension->add_src_target();
    570     src_target->set_type(stats::launcher::LauncherTarget_Type_ITEM_TYPE);
    571     src_target->set_item(stats::launcher::LauncherTarget_Item_FOLDER_ICON);
    572 
    573     auto dst_target = extension->add_dst_target();
    574     dst_target->set_type(stats::launcher::LauncherTarget_Type_ITEM_TYPE);
    575     dst_target->set_item(stats::launcher::LauncherTarget_Item_WIDGET);
    576 
    577     string extension_str;
    578     extension->SerializeToString(&extension_str);
    579 
    580     LogEvent event1(Atom::kLauncherEventFieldNumber, 1000);
    581 
    582     event1.write((int32_t)stats::launcher::LauncherAction::LONGPRESS);
    583     event1.write((int32_t)stats::launcher::LauncherState::OVERVIEW);
    584     event1.write((int64_t)stats::launcher::LauncherState::ALLAPPS);
    585     event1.write(extension_str);
    586     event1.init();
    587 
    588     ProtoOutputStream proto;
    589     event1.ToProto(proto);
    590 
    591     std::vector<uint8_t> outData;
    592     outData.resize(proto.size());
    593     size_t pos = 0;
    594     sp<ProtoReader> reader = proto.data();
    595     while (reader->readBuffer() != NULL) {
    596         size_t toRead = reader->currentToRead();
    597         std::memcpy(&(outData[pos]), reader->readBuffer(), toRead);
    598         pos += toRead;
    599         reader->move(toRead);
    600     }
    601 
    602     std::string result_str(outData.begin(), outData.end());
    603     std::string orig_str;
    604     launcherAtom.SerializeToString(&orig_str);
    605 
    606     EXPECT_EQ(orig_str, result_str);
    607 }
    608 
    609 TEST(LogEventTest, TestBinaryFieldAtom_empty) {
    610     Atom launcherAtom;
    611     auto launcher_event = launcherAtom.mutable_launcher_event();
    612     launcher_event->set_action(stats::launcher::LauncherAction::LONGPRESS);
    613     launcher_event->set_src_state(stats::launcher::LauncherState::OVERVIEW);
    614     launcher_event->set_dst_state(stats::launcher::LauncherState::ALLAPPS);
    615 
    616     // empty string.
    617     string extension_str;
    618 
    619     LogEvent event1(Atom::kLauncherEventFieldNumber, 1000);
    620 
    621     event1.write((int32_t)stats::launcher::LauncherAction::LONGPRESS);
    622     event1.write((int32_t)stats::launcher::LauncherState::OVERVIEW);
    623     event1.write((int64_t)stats::launcher::LauncherState::ALLAPPS);
    624     event1.write(extension_str);
    625     event1.init();
    626 
    627     ProtoOutputStream proto;
    628     event1.ToProto(proto);
    629 
    630     std::vector<uint8_t> outData;
    631     outData.resize(proto.size());
    632     size_t pos = 0;
    633     sp<ProtoReader> reader = proto.data();
    634     while (reader->readBuffer() != NULL) {
    635         size_t toRead = reader->currentToRead();
    636         std::memcpy(&(outData[pos]), reader->readBuffer(), toRead);
    637         pos += toRead;
    638         reader->move(toRead);
    639     }
    640 
    641     std::string result_str(outData.begin(), outData.end());
    642     std::string orig_str;
    643     launcherAtom.SerializeToString(&orig_str);
    644 
    645     EXPECT_EQ(orig_str, result_str);
    646 }
    647 
    648 TEST(LogEventTest, TestWriteExperimentIdsToProto) {
    649     std::vector<int64_t> expIds;
    650     expIds.push_back(5038);
    651     std::vector<uint8_t> proto;
    652 
    653     writeExperimentIdsToProto(expIds, &proto);
    654 
    655     EXPECT_EQ(proto.size(), 3);
    656     // Proto wire format for field ID 1, varint
    657     EXPECT_EQ(proto[0], 0x08);
    658     // varint of 5038, 2 bytes long
    659     EXPECT_EQ(proto[1], 0xae);
    660     EXPECT_EQ(proto[2], 0x27);
    661 }
    662 
    663 
    664 }  // namespace statsd
    665 }  // namespace os
    666 }  // namespace android
    667 #else
    668 GTEST_LOG_(INFO) << "This test does nothing.\n";
    669 #endif
    670