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" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 20 | |
| 21 | #ifdef __ANDROID__ |
| 22 | |
| 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 27 | using std::string; |
| 28 | using util::ProtoOutputStream; |
| 29 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 30 | TEST(LogEventTest, TestLogParsing) { |
| 31 | LogEvent event1(1, 2000); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 32 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 33 | std::vector<AttributionNodeInternal> nodes; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 34 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 35 | AttributionNodeInternal node1; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 36 | node1.set_uid(1000); |
| 37 | node1.set_tag("tag1"); |
| 38 | nodes.push_back(node1); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 39 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 40 | AttributionNodeInternal node2; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 41 | node2.set_uid(2000); |
| 42 | node2.set_tag("tag2"); |
| 43 | nodes.push_back(node2); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 44 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 45 | event1.write(nodes); |
| 46 | event1.write("hello"); |
| 47 | event1.write((int32_t)10); |
| 48 | event1.write((int64_t)20); |
| 49 | event1.write((float)1.1); |
| 50 | event1.init(); |
| 51 | |
| 52 | const auto& items = event1.getValues(); |
| 53 | EXPECT_EQ((size_t)8, items.size()); |
| 54 | EXPECT_EQ(1, event1.GetTagId()); |
| 55 | |
| 56 | const FieldValue& item0 = event1.getValues()[0]; |
| 57 | EXPECT_EQ(0x2010101, item0.mField.getField()); |
| 58 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
| 59 | EXPECT_EQ(1000, item0.mValue.int_value); |
| 60 | |
| 61 | const FieldValue& item1 = event1.getValues()[1]; |
| 62 | EXPECT_EQ(0x2010182, item1.mField.getField()); |
| 63 | EXPECT_EQ(Type::STRING, item1.mValue.getType()); |
| 64 | EXPECT_EQ("tag1", item1.mValue.str_value); |
| 65 | |
| 66 | const FieldValue& item2 = event1.getValues()[2]; |
| 67 | EXPECT_EQ(0x2018201, item2.mField.getField()); |
| 68 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
| 69 | EXPECT_EQ(2000, item2.mValue.int_value); |
| 70 | |
| 71 | const FieldValue& item3 = event1.getValues()[3]; |
| 72 | EXPECT_EQ(0x2018282, item3.mField.getField()); |
| 73 | EXPECT_EQ(Type::STRING, item3.mValue.getType()); |
| 74 | EXPECT_EQ("tag2", item3.mValue.str_value); |
| 75 | |
| 76 | const FieldValue& item4 = event1.getValues()[4]; |
| 77 | EXPECT_EQ(0x20000, item4.mField.getField()); |
| 78 | EXPECT_EQ(Type::STRING, item4.mValue.getType()); |
| 79 | EXPECT_EQ("hello", item4.mValue.str_value); |
| 80 | |
| 81 | const FieldValue& item5 = event1.getValues()[5]; |
| 82 | EXPECT_EQ(0x30000, item5.mField.getField()); |
| 83 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
| 84 | EXPECT_EQ(10, item5.mValue.int_value); |
| 85 | |
| 86 | const FieldValue& item6 = event1.getValues()[6]; |
| 87 | EXPECT_EQ(0x40000, item6.mField.getField()); |
| 88 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 89 | EXPECT_EQ((int64_t)20, item6.mValue.long_value); |
| 90 | |
| 91 | const FieldValue& item7 = event1.getValues()[7]; |
| 92 | EXPECT_EQ(0x50000, item7.mField.getField()); |
| 93 | EXPECT_EQ(Type::FLOAT, item7.mValue.getType()); |
| 94 | EXPECT_EQ((float)1.1, item7.mValue.float_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 97 | TEST(LogEventTest, TestKeyValuePairsAtomParsing) { |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 98 | LogEvent event1(83, 2000, 1000); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 99 | std::map<int32_t, int32_t> int_map; |
| 100 | std::map<int32_t, int64_t> long_map; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 101 | std::map<int32_t, std::string> string_map; |
| 102 | std::map<int32_t, float> float_map; |
| 103 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 104 | int_map[11] = 123; |
| 105 | int_map[22] = 345; |
| 106 | |
| 107 | long_map[33] = 678L; |
| 108 | long_map[44] = 890L; |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 109 | |
| 110 | string_map[1] = "test2"; |
| 111 | string_map[2] = "test1"; |
| 112 | |
| 113 | float_map[111] = 2.2f; |
| 114 | float_map[222] = 1.1f; |
| 115 | |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 116 | EXPECT_TRUE(event1.writeKeyValuePairs(0, // Logging side logs 0 uid. |
| 117 | int_map, |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 118 | long_map, |
| 119 | string_map, |
| 120 | float_map)); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 121 | event1.init(); |
| 122 | |
| 123 | EXPECT_EQ(83, event1.GetTagId()); |
| 124 | const auto& items = event1.getValues(); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 125 | EXPECT_EQ((size_t)17, items.size()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 126 | |
| 127 | const FieldValue& item0 = event1.getValues()[0]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 128 | EXPECT_EQ(0x10000, item0.mField.getField()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 129 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 130 | EXPECT_EQ(1000, item0.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 131 | |
| 132 | const FieldValue& item1 = event1.getValues()[1]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 133 | EXPECT_EQ(0x2010201, item1.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 134 | EXPECT_EQ(Type::INT, item1.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 135 | EXPECT_EQ(11, item1.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 136 | |
| 137 | const FieldValue& item2 = event1.getValues()[2]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 138 | EXPECT_EQ(0x2010282, item2.mField.getField()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 139 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 140 | EXPECT_EQ(123, item2.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 141 | |
| 142 | const FieldValue& item3 = event1.getValues()[3]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 143 | EXPECT_EQ(0x2010301, item3.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 144 | EXPECT_EQ(Type::INT, item3.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 145 | EXPECT_EQ(22, item3.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 146 | |
| 147 | const FieldValue& item4 = event1.getValues()[4]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 148 | EXPECT_EQ(0x2010382, item4.mField.getField()); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 149 | EXPECT_EQ(Type::INT, item4.mValue.getType()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 150 | EXPECT_EQ(345, item4.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 151 | |
| 152 | const FieldValue& item5 = event1.getValues()[5]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 153 | EXPECT_EQ(0x2010401, item5.mField.getField()); |
| 154 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
| 155 | EXPECT_EQ(33, item5.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 156 | |
| 157 | const FieldValue& item6 = event1.getValues()[6]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 158 | EXPECT_EQ(0x2010482, item6.mField.getField()); |
| 159 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 160 | EXPECT_EQ(678L, item6.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 161 | |
| 162 | const FieldValue& item7 = event1.getValues()[7]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 163 | EXPECT_EQ(0x2010501, item7.mField.getField()); |
| 164 | EXPECT_EQ(Type::INT, item7.mValue.getType()); |
| 165 | EXPECT_EQ(44, item7.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 166 | |
| 167 | const FieldValue& item8 = event1.getValues()[8]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 168 | EXPECT_EQ(0x2010582, item8.mField.getField()); |
| 169 | EXPECT_EQ(Type::LONG, item8.mValue.getType()); |
| 170 | EXPECT_EQ(890L, item8.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 171 | |
| 172 | const FieldValue& item9 = event1.getValues()[9]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 173 | EXPECT_EQ(0x2010601, item9.mField.getField()); |
| 174 | EXPECT_EQ(Type::INT, item9.mValue.getType()); |
| 175 | EXPECT_EQ(1, item9.mValue.int_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 176 | |
| 177 | const FieldValue& item10 = event1.getValues()[10]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 178 | EXPECT_EQ(0x2010683, item10.mField.getField()); |
| 179 | EXPECT_EQ(Type::STRING, item10.mValue.getType()); |
| 180 | EXPECT_EQ("test2", item10.mValue.str_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 181 | |
| 182 | const FieldValue& item11 = event1.getValues()[11]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 183 | EXPECT_EQ(0x2010701, item11.mField.getField()); |
| 184 | EXPECT_EQ(Type::INT, item11.mValue.getType()); |
| 185 | EXPECT_EQ(2, item11.mValue.int_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 186 | |
| 187 | const FieldValue& item12 = event1.getValues()[12]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 188 | EXPECT_EQ(0x2010783, item12.mField.getField()); |
| 189 | EXPECT_EQ(Type::STRING, item12.mValue.getType()); |
| 190 | EXPECT_EQ("test1", item12.mValue.str_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 191 | |
| 192 | const FieldValue& item13 = event1.getValues()[13]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 193 | EXPECT_EQ(0x2010801, item13.mField.getField()); |
| 194 | EXPECT_EQ(Type::INT, item13.mValue.getType()); |
| 195 | EXPECT_EQ(111, item13.mValue.int_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 196 | |
| 197 | const FieldValue& item14 = event1.getValues()[14]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 198 | EXPECT_EQ(0x2010884, item14.mField.getField()); |
| 199 | EXPECT_EQ(Type::FLOAT, item14.mValue.getType()); |
| 200 | EXPECT_EQ(2.2f, item14.mValue.float_value); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 201 | |
| 202 | const FieldValue& item15 = event1.getValues()[15]; |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 203 | EXPECT_EQ(0x2018901, item15.mField.getField()); |
| 204 | EXPECT_EQ(Type::INT, item15.mValue.getType()); |
| 205 | EXPECT_EQ(222, item15.mValue.int_value); |
| 206 | |
| 207 | const FieldValue& item16 = event1.getValues()[16]; |
| 208 | EXPECT_EQ(0x2018984, item16.mField.getField()); |
| 209 | EXPECT_EQ(Type::FLOAT, item16.mValue.getType()); |
| 210 | EXPECT_EQ(1.1f, item16.mValue.float_value); |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 213 | TEST(LogEventTest, TestLogParsing2) { |
| 214 | LogEvent event1(1, 2000); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 215 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 216 | std::vector<AttributionNodeInternal> nodes; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 217 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 218 | event1.write("hello"); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 219 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 220 | // repeated msg can be in the middle |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 221 | AttributionNodeInternal node1; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 222 | node1.set_uid(1000); |
| 223 | node1.set_tag("tag1"); |
| 224 | nodes.push_back(node1); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 225 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 226 | AttributionNodeInternal node2; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 227 | node2.set_uid(2000); |
| 228 | node2.set_tag("tag2"); |
| 229 | nodes.push_back(node2); |
| 230 | event1.write(nodes); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 231 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 232 | event1.write((int32_t)10); |
| 233 | event1.write((int64_t)20); |
| 234 | event1.write((float)1.1); |
| 235 | event1.init(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 236 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 237 | const auto& items = event1.getValues(); |
| 238 | EXPECT_EQ((size_t)8, items.size()); |
| 239 | EXPECT_EQ(1, event1.GetTagId()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 240 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 241 | const FieldValue& item = event1.getValues()[0]; |
| 242 | EXPECT_EQ(0x00010000, item.mField.getField()); |
| 243 | EXPECT_EQ(Type::STRING, item.mValue.getType()); |
| 244 | EXPECT_EQ("hello", item.mValue.str_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 245 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 246 | const FieldValue& item0 = event1.getValues()[1]; |
| 247 | EXPECT_EQ(0x2020101, item0.mField.getField()); |
| 248 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
| 249 | EXPECT_EQ(1000, item0.mValue.int_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 250 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 251 | const FieldValue& item1 = event1.getValues()[2]; |
| 252 | EXPECT_EQ(0x2020182, item1.mField.getField()); |
| 253 | EXPECT_EQ(Type::STRING, item1.mValue.getType()); |
| 254 | EXPECT_EQ("tag1", item1.mValue.str_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 255 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 256 | const FieldValue& item2 = event1.getValues()[3]; |
| 257 | EXPECT_EQ(0x2028201, item2.mField.getField()); |
| 258 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
| 259 | EXPECT_EQ(2000, item2.mValue.int_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 260 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 261 | const FieldValue& item3 = event1.getValues()[4]; |
| 262 | EXPECT_EQ(0x2028282, item3.mField.getField()); |
| 263 | EXPECT_EQ(Type::STRING, item3.mValue.getType()); |
| 264 | EXPECT_EQ("tag2", item3.mValue.str_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 265 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 266 | const FieldValue& item5 = event1.getValues()[5]; |
| 267 | EXPECT_EQ(0x30000, item5.mField.getField()); |
| 268 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
| 269 | EXPECT_EQ(10, item5.mValue.int_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 270 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 271 | const FieldValue& item6 = event1.getValues()[6]; |
| 272 | EXPECT_EQ(0x40000, item6.mField.getField()); |
| 273 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 274 | EXPECT_EQ((int64_t)20, item6.mValue.long_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 275 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 276 | const FieldValue& item7 = event1.getValues()[7]; |
| 277 | EXPECT_EQ(0x50000, item7.mField.getField()); |
| 278 | EXPECT_EQ(Type::FLOAT, item7.mValue.getType()); |
| 279 | EXPECT_EQ((float)1.1, item7.mValue.float_value); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 282 | TEST(LogEventTest, TestKeyValuePairsEvent) { |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 283 | std::map<int32_t, int32_t> int_map; |
| 284 | std::map<int32_t, int64_t> long_map; |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 285 | std::map<int32_t, std::string> string_map; |
| 286 | std::map<int32_t, float> float_map; |
| 287 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 288 | int_map[11] = 123; |
| 289 | int_map[22] = 345; |
| 290 | |
| 291 | long_map[33] = 678L; |
| 292 | long_map[44] = 890L; |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 293 | |
| 294 | string_map[1] = "test2"; |
| 295 | string_map[2] = "test1"; |
| 296 | |
| 297 | float_map[111] = 2.2f; |
| 298 | float_map[222] = 1.1f; |
| 299 | |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 300 | 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] | 301 | event1.init(); |
| 302 | |
| 303 | EXPECT_EQ(83, event1.GetTagId()); |
| 304 | EXPECT_EQ((int64_t)2000, event1.GetLogdTimestampNs()); |
| 305 | EXPECT_EQ((int64_t)2001, event1.GetElapsedTimestampNs()); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 306 | EXPECT_EQ((int64_t)10001, event1.GetUid()); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 307 | |
| 308 | const auto& items = event1.getValues(); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 309 | EXPECT_EQ((size_t)17, items.size()); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 310 | |
| 311 | const FieldValue& item0 = event1.getValues()[0]; |
| 312 | EXPECT_EQ(0x00010000, item0.mField.getField()); |
| 313 | EXPECT_EQ(Type::INT, item0.mValue.getType()); |
| 314 | EXPECT_EQ(10001, item0.mValue.int_value); |
| 315 | |
| 316 | const FieldValue& item1 = event1.getValues()[1]; |
| 317 | EXPECT_EQ(0x2020101, item1.mField.getField()); |
| 318 | EXPECT_EQ(Type::INT, item1.mValue.getType()); |
| 319 | EXPECT_EQ(11, item1.mValue.int_value); |
| 320 | |
| 321 | const FieldValue& item2 = event1.getValues()[2]; |
| 322 | EXPECT_EQ(0x2020182, item2.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 323 | EXPECT_EQ(Type::INT, item2.mValue.getType()); |
| 324 | EXPECT_EQ(123, item2.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 325 | |
| 326 | const FieldValue& item3 = event1.getValues()[3]; |
| 327 | EXPECT_EQ(0x2020201, item3.mField.getField()); |
| 328 | EXPECT_EQ(Type::INT, item3.mValue.getType()); |
| 329 | EXPECT_EQ(22, item3.mValue.int_value); |
| 330 | |
| 331 | const FieldValue& item4 = event1.getValues()[4]; |
| 332 | EXPECT_EQ(0x2020282, item4.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 333 | EXPECT_EQ(Type::INT, item4.mValue.getType()); |
| 334 | EXPECT_EQ(345, item4.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 335 | |
| 336 | const FieldValue& item5 = event1.getValues()[5]; |
| 337 | EXPECT_EQ(0x2020301, item5.mField.getField()); |
| 338 | EXPECT_EQ(Type::INT, item5.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 339 | EXPECT_EQ(33, item5.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 340 | |
| 341 | const FieldValue& item6 = event1.getValues()[6]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 342 | EXPECT_EQ(0x2020383, item6.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 343 | EXPECT_EQ(Type::LONG, item6.mValue.getType()); |
| 344 | EXPECT_EQ(678L, item6.mValue.long_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 345 | |
| 346 | const FieldValue& item7 = event1.getValues()[7]; |
| 347 | EXPECT_EQ(0x2020401, item7.mField.getField()); |
| 348 | EXPECT_EQ(Type::INT, item7.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 349 | EXPECT_EQ(44, item7.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 350 | |
| 351 | const FieldValue& item8 = event1.getValues()[8]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 352 | EXPECT_EQ(0x2020483, item8.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 353 | EXPECT_EQ(Type::LONG, item8.mValue.getType()); |
| 354 | EXPECT_EQ(890L, item8.mValue.long_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 355 | |
| 356 | const FieldValue& item9 = event1.getValues()[9]; |
| 357 | EXPECT_EQ(0x2020501, item9.mField.getField()); |
| 358 | EXPECT_EQ(Type::INT, item9.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 359 | EXPECT_EQ(1, item9.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 360 | |
| 361 | const FieldValue& item10 = event1.getValues()[10]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 362 | EXPECT_EQ(0x2020584, item10.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 363 | EXPECT_EQ(Type::STRING, item10.mValue.getType()); |
| 364 | EXPECT_EQ("test2", item10.mValue.str_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 365 | |
| 366 | const FieldValue& item11 = event1.getValues()[11]; |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 367 | EXPECT_EQ(0x2020601, item11.mField.getField()); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 368 | EXPECT_EQ(Type::INT, item11.mValue.getType()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 369 | EXPECT_EQ(2, item11.mValue.int_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 370 | |
| 371 | const FieldValue& item12 = event1.getValues()[12]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 372 | EXPECT_EQ(0x2020684, item12.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 373 | EXPECT_EQ(Type::STRING, item12.mValue.getType()); |
| 374 | EXPECT_EQ("test1", item12.mValue.str_value); |
| 375 | |
| 376 | const FieldValue& item13 = event1.getValues()[13]; |
| 377 | EXPECT_EQ(0x2020701, item13.mField.getField()); |
| 378 | EXPECT_EQ(Type::INT, item13.mValue.getType()); |
| 379 | EXPECT_EQ(111, item13.mValue.int_value); |
| 380 | |
| 381 | const FieldValue& item14 = event1.getValues()[14]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 382 | EXPECT_EQ(0x2020785, item14.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 383 | EXPECT_EQ(Type::FLOAT, item14.mValue.getType()); |
| 384 | EXPECT_EQ(2.2f, item14.mValue.float_value); |
| 385 | |
| 386 | const FieldValue& item15 = event1.getValues()[15]; |
| 387 | EXPECT_EQ(0x2028801, item15.mField.getField()); |
| 388 | EXPECT_EQ(Type::INT, item15.mValue.getType()); |
| 389 | EXPECT_EQ(222, item15.mValue.int_value); |
| 390 | |
| 391 | const FieldValue& item16 = event1.getValues()[16]; |
Howard Ro | a5b0f1e | 2018-10-02 16:20:43 -0700 | [diff] [blame] | 392 | EXPECT_EQ(0x2028885, item16.mField.getField()); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 393 | EXPECT_EQ(Type::FLOAT, item16.mValue.getType()); |
| 394 | EXPECT_EQ(1.1f, item16.mValue.float_value); |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 397 | |
Yao Chen | 8c43386 | 2018-10-24 14:09:20 -0700 | [diff] [blame] | 398 | TEST(LogEventTest, TestBinaryFieldAtom) { |
| 399 | Atom launcherAtom; |
| 400 | auto launcher_event = launcherAtom.mutable_launcher_event(); |
| 401 | launcher_event->set_action(stats::launcher::LauncherAction::LONGPRESS); |
| 402 | launcher_event->set_src_state(stats::launcher::LauncherState::OVERVIEW); |
| 403 | launcher_event->set_dst_state(stats::launcher::LauncherState::ALLAPPS); |
| 404 | |
| 405 | auto extension = launcher_event->mutable_extension(); |
| 406 | |
| 407 | auto src_target = extension->add_src_target(); |
| 408 | src_target->set_type(stats::launcher::LauncherTarget_Type_ITEM_TYPE); |
| 409 | src_target->set_item(stats::launcher::LauncherTarget_Item_FOLDER_ICON); |
| 410 | |
| 411 | auto dst_target = extension->add_dst_target(); |
| 412 | dst_target->set_type(stats::launcher::LauncherTarget_Type_ITEM_TYPE); |
| 413 | dst_target->set_item(stats::launcher::LauncherTarget_Item_WIDGET); |
| 414 | |
| 415 | string extension_str; |
| 416 | extension->SerializeToString(&extension_str); |
| 417 | |
| 418 | LogEvent event1(Atom::kLauncherEventFieldNumber, 1000); |
| 419 | |
| 420 | event1.write((int32_t)stats::launcher::LauncherAction::LONGPRESS); |
| 421 | event1.write((int32_t)stats::launcher::LauncherState::OVERVIEW); |
| 422 | event1.write((int64_t)stats::launcher::LauncherState::ALLAPPS); |
| 423 | event1.write(extension_str); |
| 424 | event1.init(); |
| 425 | |
| 426 | ProtoOutputStream proto; |
| 427 | event1.ToProto(proto); |
| 428 | |
| 429 | std::vector<uint8_t> outData; |
| 430 | outData.resize(proto.size()); |
| 431 | size_t pos = 0; |
| 432 | auto iter = proto.data(); |
| 433 | while (iter.readBuffer() != NULL) { |
| 434 | size_t toRead = iter.currentToRead(); |
| 435 | std::memcpy(&(outData[pos]), iter.readBuffer(), toRead); |
| 436 | pos += toRead; |
| 437 | iter.rp()->move(toRead); |
| 438 | } |
| 439 | |
| 440 | std::string result_str(outData.begin(), outData.end()); |
| 441 | std::string orig_str; |
| 442 | launcherAtom.SerializeToString(&orig_str); |
| 443 | |
| 444 | EXPECT_EQ(orig_str, result_str); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 449 | } // namespace statsd |
| 450 | } // namespace os |
| 451 | } // namespace android |
| 452 | #else |
| 453 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 454 | #endif |