1 /*
2  * Copyright (C) 2020 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 "src/profiling/memory/parse_smaps.h"
18 
19 #include "perfetto/ext/base/scoped_file.h"
20 #include "src/base/test/utils.h"
21 #include "test/gtest_and_gmock.h"
22 
23 #include <inttypes.h>
24 
25 namespace perfetto {
26 namespace profiling {
27 
28 bool operator==(const SmapsEntry& a, const SmapsEntry& b);
operator ==(const SmapsEntry & a,const SmapsEntry & b)29 bool operator==(const SmapsEntry& a, const SmapsEntry& b) {
30   return a.pathname == b.pathname && a.size_kb == b.size_kb &&
31          a.private_dirty_kb == b.private_dirty_kb && a.swap_kb == b.swap_kb;
32 }
33 
34 namespace {
35 
36 using ::testing::ElementsAre;
37 
TEST(ParseSmapsTest,Smoke)38 TEST(ParseSmapsTest, Smoke) {
39   base::ScopedFstream fd(fopen(
40       base::GetTestDataPath("src/profiling/memory/test/data/cat_smaps").c_str(),
41       "r"));
42   std::vector<SmapsEntry> entries;
43   EXPECT_TRUE(ParseSmaps(
44       *fd, [&entries](const SmapsEntry& e) { entries.emplace_back(e); }));
45 
46   SmapsEntry cat1;
47   cat1.pathname = "/bin/cat";
48   cat1.size_kb = 8;
49   cat1.private_dirty_kb = 0;
50   cat1.swap_kb = 0;
51   SmapsEntry cat2;
52   cat2.pathname = "/bin/cat";
53   cat2.size_kb = 8;
54   cat2.private_dirty_kb = 0;
55   cat2.swap_kb = 0;
56   SmapsEntry heap;
57   heap.pathname = "[heap stuff]";
58   heap.size_kb = 132;
59   heap.private_dirty_kb = 8;
60   heap.swap_kb = 4;
61   EXPECT_THAT(entries, ElementsAre(cat1, cat2, heap));
62 }
63 
TEST(ParseSmapsTest,SmokeNoEol)64 TEST(ParseSmapsTest, SmokeNoEol) {
65   base::ScopedFstream fd(fopen(
66       base::GetTestDataPath("src/profiling/memory/test/data/cat_smaps_noeol")
67           .c_str(),
68       "r"));
69   std::vector<SmapsEntry> entries;
70   EXPECT_TRUE(ParseSmaps(
71       *fd, [&entries](const SmapsEntry& e) { entries.emplace_back(e); }));
72 
73   SmapsEntry cat1;
74   cat1.pathname = "/bin/cat";
75   cat1.size_kb = 8;
76   cat1.private_dirty_kb = 0;
77   cat1.swap_kb = 0;
78   SmapsEntry cat2;
79   cat2.pathname = "/bin/cat";
80   cat2.size_kb = 8;
81   cat2.private_dirty_kb = 0;
82   cat2.swap_kb = 0;
83   SmapsEntry heap;
84   heap.pathname = "[heap stuff]";
85   heap.size_kb = 132;
86   heap.private_dirty_kb = 8;
87   heap.swap_kb = 4;
88   EXPECT_THAT(entries, ElementsAre(cat1, cat2, heap));
89 }
90 
91 }  // namespace
92 }  // namespace profiling
93 }  // namespace perfetto
94