Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "IncidentHelper.h" |
| 18 | |
| 19 | #include "frameworks/base/core/proto/android/os/kernelwake.pb.h" |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 20 | #include "frameworks/base/core/proto/android/os/procrank.pb.h" |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 21 | |
| 22 | #include <android-base/file.h> |
| 23 | #include <android-base/test_utils.h> |
| 24 | #include <gmock/gmock.h> |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 25 | #include <google/protobuf/message.h> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | #include <string.h> |
| 28 | #include <fcntl.h> |
| 29 | |
| 30 | using namespace android::base; |
| 31 | using namespace android::os; |
| 32 | using ::testing::StrEq; |
| 33 | using ::testing::Test; |
| 34 | using ::testing::internal::CaptureStderr; |
| 35 | using ::testing::internal::CaptureStdout; |
| 36 | using ::testing::internal::GetCapturedStderr; |
| 37 | using ::testing::internal::GetCapturedStdout; |
| 38 | |
| 39 | class IncidentHelperTest : public Test { |
| 40 | public: |
| 41 | virtual void SetUp() override { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 42 | ASSERT_TRUE(tf.fd != -1); |
| 43 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 44 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 45 | std::string getSerializedString(::google::protobuf::Message& message) { |
| 46 | std::string expectedStr; |
| 47 | message.SerializeToFileDescriptor(tf.fd); |
| 48 | ReadFileToString(tf.path, &expectedStr); |
| 49 | return expectedStr; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | protected: |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 53 | TemporaryFile tf; |
| 54 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 55 | const std::string kTestPath = GetExecutableDirectory(); |
| 56 | const std::string kTestDataPath = kTestPath + "/testdata/"; |
| 57 | }; |
| 58 | |
| 59 | TEST_F(IncidentHelperTest, ReverseParser) { |
| 60 | ReverseParser parser; |
| 61 | TemporaryFile tf; |
| 62 | |
| 63 | ASSERT_TRUE(tf.fd != -1); |
| 64 | ASSERT_TRUE(WriteStringToFile("TestData", tf.path, false)); |
| 65 | |
| 66 | CaptureStdout(); |
| 67 | ASSERT_EQ(NO_ERROR, parser.Parse(tf.fd, STDOUT_FILENO)); |
| 68 | EXPECT_THAT(GetCapturedStdout(), StrEq("ataDtseT")); |
| 69 | } |
| 70 | |
| 71 | TEST_F(IncidentHelperTest, KernelWakesParser) { |
| 72 | const std::string testFile = kTestDataPath + "kernel_wakeups.txt"; |
| 73 | KernelWakesParser parser; |
| 74 | KernelWakeSources expected; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 75 | |
| 76 | WakeupSourceProto* record1 = expected.add_wakeup_sources(); |
| 77 | record1->set_name("ipc000000ab_ATFWD-daemon"); |
| 78 | record1->set_active_count(8); |
| 79 | record1->set_event_count(8); |
| 80 | record1->set_wakeup_count(0); |
| 81 | record1->set_expire_count(0); |
| 82 | record1->set_active_since(0l); |
| 83 | record1->set_total_time(0l); |
| 84 | record1->set_max_time(0l); |
| 85 | record1->set_last_change(131348l); |
| 86 | record1->set_prevent_suspend_time(0l); |
| 87 | |
| 88 | WakeupSourceProto* record2 = expected.add_wakeup_sources(); |
| 89 | record2->set_name("ipc000000aa_ATFWD-daemon"); |
| 90 | record2->set_active_count(143); |
| 91 | record2->set_event_count(143); |
| 92 | record2->set_wakeup_count(0); |
| 93 | record2->set_expire_count(0); |
| 94 | record2->set_active_since(0l); |
| 95 | record2->set_total_time(123l); |
| 96 | record2->set_max_time(3l); |
| 97 | record2->set_last_change(2067286206l); |
| 98 | record2->set_prevent_suspend_time(0l); |
| 99 | |
George Burgess IV | f416031 | 2017-08-14 18:29:21 -0700 | [diff] [blame] | 100 | int fd = open(testFile.c_str(), O_RDONLY); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(fd != -1); |
| 102 | |
| 103 | CaptureStdout(); |
| 104 | ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 105 | EXPECT_EQ(GetCapturedStdout(), getSerializedString(expected)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 106 | close(fd); |
| 107 | } |
| 108 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 109 | TEST_F(IncidentHelperTest, ProcrankParser) { |
| 110 | const std::string testFile = kTestDataPath + "procrank.txt"; |
| 111 | ProcrankParser parser; |
| 112 | Procrank expected; |
| 113 | |
| 114 | ProcessProto* process1 = expected.add_processes(); |
| 115 | process1->set_pid(1119); |
| 116 | process1->set_vss(2607640); |
| 117 | process1->set_rss(339564); |
| 118 | process1->set_pss(180278); |
| 119 | process1->set_uss(114216); |
| 120 | process1->set_swap(1584); |
| 121 | process1->set_pswap(46); |
| 122 | process1->set_uswap(0); |
| 123 | process1->set_zswap(10); |
| 124 | process1->set_cmdline("system_server"); |
| 125 | |
| 126 | ProcessProto* process2 = expected.add_processes(); |
| 127 | process2->set_pid(649); |
| 128 | process2->set_vss(11016); |
| 129 | process2->set_rss(1448); |
| 130 | process2->set_pss(98); |
| 131 | process2->set_uss(48); |
| 132 | process2->set_swap(472); |
| 133 | process2->set_pswap(342); |
| 134 | process2->set_uswap(212); |
| 135 | process2->set_zswap(75); |
| 136 | process2->set_cmdline("/vendor/bin/qseecomd"); |
| 137 | |
| 138 | ProcessProto* total = expected.mutable_summary()->mutable_total(); |
| 139 | total->set_pss(1201993); |
| 140 | total->set_uss(935300); |
| 141 | total->set_swap(88164); |
| 142 | total->set_pswap(31069); |
| 143 | total->set_uswap(27612); |
| 144 | total->set_zswap(6826); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 145 | total->set_cmdline("TOTAL"); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 146 | |
| 147 | expected.mutable_summary()->mutable_zram() |
| 148 | ->set_raw_text("6828K physical used for 31076K in swap (524284K total swap)"); |
| 149 | expected.mutable_summary()->mutable_ram() |
| 150 | ->set_raw_text("3843972K total, 281424K free, 116764K buffers, 1777452K cached, 1136K shmem, 217916K slab"); |
| 151 | |
George Burgess IV | f416031 | 2017-08-14 18:29:21 -0700 | [diff] [blame] | 152 | int fd = open(testFile.c_str(), O_RDONLY); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 153 | ASSERT_TRUE(fd != -1); |
| 154 | |
| 155 | CaptureStdout(); |
| 156 | ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO)); |
| 157 | EXPECT_EQ(GetCapturedStdout(), getSerializedString(expected)); |
| 158 | close(fd); |
| 159 | } |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 160 | |
| 161 | TEST_F(IncidentHelperTest, ProcrankParserShortHeader) { |
| 162 | const std::string testFile = kTestDataPath + "procrank_short.txt"; |
| 163 | ProcrankParser parser; |
| 164 | Procrank expected; |
| 165 | |
| 166 | ProcessProto* process1 = expected.add_processes(); |
| 167 | process1->set_pid(1119); |
| 168 | process1->set_vss(2607640); |
| 169 | process1->set_rss(339564); |
| 170 | process1->set_pss(180278); |
| 171 | process1->set_uss(114216); |
| 172 | process1->set_cmdline("system_server"); |
| 173 | |
| 174 | ProcessProto* process2 = expected.add_processes(); |
| 175 | process2->set_pid(649); |
| 176 | process2->set_vss(11016); |
| 177 | process2->set_rss(1448); |
| 178 | process2->set_pss(98); |
| 179 | process2->set_uss(48); |
| 180 | process2->set_cmdline("/vendor/bin/qseecomd"); |
| 181 | |
| 182 | ProcessProto* total = expected.mutable_summary()->mutable_total(); |
| 183 | total->set_pss(1201993); |
| 184 | total->set_uss(935300); |
| 185 | total->set_cmdline("TOTAL"); |
| 186 | |
| 187 | expected.mutable_summary()->mutable_ram() |
| 188 | ->set_raw_text("3843972K total, 281424K free, 116764K buffers, 1777452K cached, 1136K shmem, 217916K slab"); |
| 189 | |
George Burgess IV | f416031 | 2017-08-14 18:29:21 -0700 | [diff] [blame] | 190 | int fd = open(testFile.c_str(), O_RDONLY); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 191 | ASSERT_TRUE(fd != -1); |
| 192 | |
| 193 | CaptureStdout(); |
| 194 | ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO)); |
| 195 | EXPECT_EQ(GetCapturedStdout(), getSerializedString(expected)); |
| 196 | close(fd); |
George Burgess IV | f416031 | 2017-08-14 18:29:21 -0700 | [diff] [blame] | 197 | } |