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 "ProcrankParser.h"
18
19 #include "frameworks/base/core/proto/android/os/procrank.pb.h"
20
21 #include <android-base/file.h>
22 #include <android-base/test_utils.h>
23 #include <gmock/gmock.h>
24 #include <google/protobuf/message_lite.h>
25 #include <gtest/gtest.h>
26 #include <string.h>
27 #include <fcntl.h>
28
29 using namespace android::base;
30 using namespace android::os;
31 using namespace std;
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 ProcrankParserTest : public Test {
40 public:
SetUp()41 virtual void SetUp() override {
42 ASSERT_TRUE(tf.fd != -1);
43 }
44
45 protected:
46 TemporaryFile tf;
47
48 const string kTestPath = GetExecutableDirectory();
49 const string kTestDataPath = kTestPath + "/testdata/";
50 };
51
TEST_F(ProcrankParserTest,HasSwapInfo)52 TEST_F(ProcrankParserTest, HasSwapInfo) {
53 const string testFile = kTestDataPath + "procrank.txt";
54 ProcrankParser parser;
55 ProcrankProto expected;
56
57 ProcrankProto::Process* process1 = expected.add_processes();
58 process1->set_pid(1119);
59 process1->set_vss(2607640);
60 process1->set_rss(339564);
61 process1->set_pss(180278);
62 process1->set_uss(114216);
63 process1->set_swap(1584);
64 process1->set_pswap(46);
65 process1->set_uswap(0);
66 process1->set_zswap(10);
67 process1->set_cmdline("system_server");
68
69 ProcrankProto::Process* process2 = expected.add_processes();
70 process2->set_pid(649);
71 process2->set_vss(11016);
72 process2->set_rss(1448);
73 process2->set_pss(98);
74 process2->set_uss(48);
75 process2->set_swap(472);
76 process2->set_pswap(342);
77 process2->set_uswap(212);
78 process2->set_zswap(75);
79 process2->set_cmdline("/vendor/bin/qseecomd");
80
81 ProcrankProto::Process* total = expected.mutable_summary()->mutable_total();
82 total->set_pss(1201993);
83 total->set_uss(935300);
84 total->set_swap(88164);
85 total->set_pswap(31069);
86 total->set_uswap(27612);
87 total->set_zswap(6826);
88 total->set_cmdline("TOTAL");
89
90 expected.mutable_summary()->mutable_zram()
91 ->set_raw_text("6828K physical used for 31076K in swap (524284K total swap)");
92 expected.mutable_summary()->mutable_ram()
93 ->set_raw_text("3843972K total, 281424K free, 116764K buffers, 1777452K cached, 1136K shmem, 217916K slab");
94
95 int fd = open(testFile.c_str(), O_RDONLY);
96 ASSERT_TRUE(fd != -1);
97
98 CaptureStdout();
99 ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
100 EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
101 close(fd);
102 }
103
TEST_F(ProcrankParserTest,NoSwapInfo)104 TEST_F(ProcrankParserTest, NoSwapInfo) {
105 const string testFile = kTestDataPath + "procrank_short.txt";
106 ProcrankParser parser;
107 ProcrankProto expected;
108
109 ProcrankProto::Process* process1 = expected.add_processes();
110 process1->set_pid(1119);
111 process1->set_vss(2607640);
112 process1->set_rss(339564);
113 process1->set_pss(180278);
114 process1->set_uss(114216);
115 process1->set_cmdline("system_server");
116
117 ProcrankProto::Process* process2 = expected.add_processes();
118 process2->set_pid(649);
119 process2->set_vss(11016);
120 process2->set_rss(1448);
121 process2->set_pss(98);
122 process2->set_uss(48);
123 process2->set_cmdline("/vendor/bin/qseecomd");
124
125 ProcrankProto::Process* total = expected.mutable_summary()->mutable_total();
126 total->set_pss(1201993);
127 total->set_uss(935300);
128 total->set_cmdline("TOTAL");
129
130 expected.mutable_summary()->mutable_ram()
131 ->set_raw_text("3843972K total, 281424K free, 116764K buffers, 1777452K cached, 1136K shmem, 217916K slab");
132
133 int fd = open(testFile.c_str(), O_RDONLY);
134 ASSERT_TRUE(fd != -1);
135
136 CaptureStdout();
137 ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
138 EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
139 close(fd);
140 }
141