blob: c57775fac8fe9d9e0ab5c8a090fc78ae4d7797e1 [file] [log] [blame]
Vishnu Naire97d6122018-01-18 13:58:56 -08001/*
2 * Copyright (C) 2018 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 <gmock/gmock.h>
18#include <gtest/gtest.h>
19
20#include <fcntl.h>
21#include <libgen.h>
22
23#include <android-base/file.h>
24#include <cutils/properties.h>
25#include <ziparchive/zip_archive.h>
26
27#include "dumpstate.h"
28
29#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
30
31namespace android {
32namespace os {
33namespace dumpstate {
34
35using ::testing::Test;
36using ::std::literals::chrono_literals::operator""s;
37
38struct SectionInfo {
39 std::string name;
40 status_t status;
41 int32_t size_bytes;
42 int32_t duration_ms;
43};
44
45/**
46 * Listens to bugreport progress and updates the user by writing the progress to STDOUT. All the
47 * section details generated by dumpstate are added to a vector to be used by Tests later.
48 */
49class DumpstateListener : public IDumpstateListener {
50 public:
51 int outFd_, max_progress_;
52 std::shared_ptr<std::vector<SectionInfo>> sections_;
53 DumpstateListener(int fd, std::shared_ptr<std::vector<SectionInfo>> sections)
54 : outFd_(fd), max_progress_(5000), sections_(sections) {
55 }
Nandana Dutta6a28bd2019-01-14 16:54:38 +000056 binder::Status onProgress(int32_t progress) override {
57 dprintf(outFd_, "\rIn progress %d", progress);
58 return binder::Status::ok();
59 }
60 binder::Status onError(int32_t error_code) override {
61 dprintf(outFd_, "\rError %d", error_code);
62 return binder::Status::ok();
63 }
64 binder::Status onFinished(int64_t duration_ms, const ::std::string&,
65 const ::std::string&) override {
66 dprintf(outFd_, "\rFinished in %lld", (long long) duration_ms);
67 return binder::Status::ok();
68 }
Vishnu Naire97d6122018-01-18 13:58:56 -080069 binder::Status onProgressUpdated(int32_t progress) override {
70 dprintf(outFd_, "\rIn progress %d/%d", progress, max_progress_);
71 return binder::Status::ok();
72 }
73 binder::Status onMaxProgressUpdated(int32_t max_progress) override {
74 max_progress_ = max_progress;
75 return binder::Status::ok();
76 }
77 binder::Status onSectionComplete(const ::std::string& name, int32_t status, int32_t size_bytes,
78 int32_t duration_ms) override {
79 sections_->push_back({name, status, size_bytes, duration_ms});
80 return binder::Status::ok();
81 }
82 IBinder* onAsBinder() override {
83 return nullptr;
84 }
85};
86
87/**
88 * Generates bug report and provide access to the bug report file and other info for other tests.
89 * Since bug report generation is slow, the bugreport is only generated once.
90 */
91class ZippedBugreportGenerationTest : public Test {
92 public:
93 static std::shared_ptr<std::vector<SectionInfo>> sections;
94 static Dumpstate& ds;
95 static std::chrono::milliseconds duration;
96 static void SetUpTestCase() {
97 property_set("dumpstate.options", "bugreportplus");
98 // clang-format off
99 char* argv[] = {
100 (char*)"dumpstate",
101 (char*)"-d",
102 (char*)"-z",
103 (char*)"-B",
104 (char*)"-o",
105 (char*)dirname(android::base::GetExecutablePath().c_str())
106 };
107 // clang-format on
108 sp<DumpstateListener> listener(new DumpstateListener(dup(fileno(stdout)), sections));
109 ds.listener_ = listener;
110 ds.listener_name_ = "Smokey";
111 ds.report_section_ = true;
112 auto start = std::chrono::steady_clock::now();
113 run_main(ARRAY_SIZE(argv), argv);
114 auto end = std::chrono::steady_clock::now();
115 duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
116 }
117
118 static const char* getZipFilePath() {
119 return ds.GetPath(".zip").c_str();
120 }
121};
122std::shared_ptr<std::vector<SectionInfo>> ZippedBugreportGenerationTest::sections =
123 std::make_shared<std::vector<SectionInfo>>();
124Dumpstate& ZippedBugreportGenerationTest::ds = Dumpstate::GetInstance();
125std::chrono::milliseconds ZippedBugreportGenerationTest::duration = 0s;
126
127TEST_F(ZippedBugreportGenerationTest, IsGeneratedWithoutErrors) {
128 EXPECT_EQ(access(getZipFilePath(), F_OK), 0);
129}
130
131TEST_F(ZippedBugreportGenerationTest, Is3MBto30MBinSize) {
132 struct stat st;
133 EXPECT_EQ(stat(getZipFilePath(), &st), 0);
134 EXPECT_GE(st.st_size, 3000000 /* 3MB */);
135 EXPECT_LE(st.st_size, 30000000 /* 30MB */);
136}
137
138TEST_F(ZippedBugreportGenerationTest, TakesBetween30And150Seconds) {
139 EXPECT_GE(duration, 30s) << "Expected completion in more than 30s. Actual time "
140 << duration.count() << " s.";
141 EXPECT_LE(duration, 150s) << "Expected completion in less than 150s. Actual time "
142 << duration.count() << " s.";
143}
144
145/**
146 * Run tests on contents of zipped bug report.
147 */
148class ZippedBugReportContentsTest : public Test {
149 public:
150 ZipArchiveHandle handle;
151 void SetUp() {
152 ASSERT_EQ(OpenArchive(ZippedBugreportGenerationTest::getZipFilePath(), &handle), 0);
153 }
154 void TearDown() {
155 CloseArchive(handle);
156 }
157
158 void FileExists(const char* filename, uint32_t minsize, uint32_t maxsize) {
159 ZipEntry entry;
160 EXPECT_EQ(FindEntry(handle, ZipString(filename), &entry), 0);
161 EXPECT_GT(entry.uncompressed_length, minsize);
162 EXPECT_LT(entry.uncompressed_length, maxsize);
163 }
164};
165
166TEST_F(ZippedBugReportContentsTest, ContainsMainEntry) {
167 ZipEntry mainEntryLoc;
168 // contains main entry name file
169 EXPECT_EQ(FindEntry(handle, ZipString("main_entry.txt"), &mainEntryLoc), 0);
170
171 char* buf = new char[mainEntryLoc.uncompressed_length];
172 ExtractToMemory(handle, &mainEntryLoc, (uint8_t*)buf, mainEntryLoc.uncompressed_length);
173 delete[] buf;
174
175 // contains main entry file
176 FileExists(buf, 1000000U, 50000000U);
177}
178
179TEST_F(ZippedBugReportContentsTest, ContainsVersion) {
180 ZipEntry entry;
181 // contains main entry name file
182 EXPECT_EQ(FindEntry(handle, ZipString("version.txt"), &entry), 0);
183
184 char* buf = new char[entry.uncompressed_length + 1];
185 ExtractToMemory(handle, &entry, (uint8_t*)buf, entry.uncompressed_length);
186 buf[entry.uncompressed_length] = 0;
187 EXPECT_STREQ(buf, ZippedBugreportGenerationTest::ds.version_.c_str());
188 delete[] buf;
189}
190
191TEST_F(ZippedBugReportContentsTest, ContainsBoardSpecificFiles) {
192 FileExists("dumpstate_board.bin", 1000000U, 80000000U);
193 FileExists("dumpstate_board.txt", 100000U, 1000000U);
194}
195
196// Spot check on some files pulled from the file system
197TEST_F(ZippedBugReportContentsTest, ContainsSomeFileSystemFiles) {
198 // FS/proc/*/mountinfo size > 0
199 FileExists("FS/proc/1/mountinfo", 0U, 100000U);
200
201 // FS/data/misc/profiles/cur/0/*/primary.prof size > 0
202 FileExists("FS/data/misc/profiles/cur/0/com.android.phone/primary.prof", 0U, 100000U);
203}
204
205/**
206 * Runs tests on section data generated by dumpstate and captured by DumpstateListener.
207 */
208class BugreportSectionTest : public Test {
209 public:
210 int numMatches(const std::string& substring) {
211 int matches = 0;
212 for (auto const& section : *ZippedBugreportGenerationTest::sections) {
213 if (section.name.find(substring) != std::string::npos) {
214 matches++;
215 }
216 }
217 return matches;
218 }
219 void SectionExists(const std::string& sectionName, int minsize) {
220 for (auto const& section : *ZippedBugreportGenerationTest::sections) {
221 if (sectionName == section.name) {
222 EXPECT_GE(section.size_bytes, minsize);
223 return;
224 }
225 }
226 FAIL() << sectionName << " not found.";
227 }
228};
229
230// Test all sections are generated without timeouts or errors
231TEST_F(BugreportSectionTest, GeneratedWithoutErrors) {
232 for (auto const& section : *ZippedBugreportGenerationTest::sections) {
233 EXPECT_EQ(section.status, 0) << section.name << " failed with status " << section.status;
234 }
235}
236
237TEST_F(BugreportSectionTest, Atleast3CriticalDumpsysSectionsGenerated) {
238 int numSections = numMatches("DUMPSYS CRITICAL");
239 EXPECT_GE(numSections, 3);
240}
241
242TEST_F(BugreportSectionTest, Atleast2HighDumpsysSectionsGenerated) {
243 int numSections = numMatches("DUMPSYS HIGH");
244 EXPECT_GE(numSections, 2);
245}
246
247TEST_F(BugreportSectionTest, Atleast50NormalDumpsysSectionsGenerated) {
248 int allSections = numMatches("DUMPSYS");
249 int criticalSections = numMatches("DUMPSYS CRITICAL");
250 int highSections = numMatches("DUMPSYS HIGH");
251 int normalSections = allSections - criticalSections - highSections;
252
253 EXPECT_GE(normalSections, 50) << "Total sections less than 50 (Critical:" << criticalSections
254 << "High:" << highSections << "Normal:" << normalSections << ")";
255}
256
257TEST_F(BugreportSectionTest, Atleast1ProtoDumpsysSectionGenerated) {
258 int numSections = numMatches("proto/");
259 EXPECT_GE(numSections, 1);
260}
261
262// Test if some critical sections are being generated.
263TEST_F(BugreportSectionTest, CriticalSurfaceFlingerSectionGenerated) {
264 SectionExists("DUMPSYS CRITICAL - SurfaceFlinger", /* bytes= */ 10000);
265}
266
267TEST_F(BugreportSectionTest, ActivitySectionsGenerated) {
268 SectionExists("DUMPSYS CRITICAL - activity", /* bytes= */ 5000);
269 SectionExists("DUMPSYS - activity", /* bytes= */ 10000);
270}
271
272TEST_F(BugreportSectionTest, CpuinfoSectionGenerated) {
273 SectionExists("DUMPSYS CRITICAL - cpuinfo", /* bytes= */ 1000);
274}
275
276TEST_F(BugreportSectionTest, WindowSectionGenerated) {
277 SectionExists("DUMPSYS CRITICAL - window", /* bytes= */ 20000);
278}
279
280TEST_F(BugreportSectionTest, ConnectivitySectionsGenerated) {
281 SectionExists("DUMPSYS HIGH - connectivity", /* bytes= */ 5000);
282 SectionExists("DUMPSYS - connectivity", /* bytes= */ 5000);
283}
284
285TEST_F(BugreportSectionTest, MeminfoSectionGenerated) {
286 SectionExists("DUMPSYS HIGH - meminfo", /* bytes= */ 100000);
287}
288
289TEST_F(BugreportSectionTest, BatteryStatsSectionGenerated) {
290 SectionExists("DUMPSYS - batterystats", /* bytes= */ 1000);
291}
292
293TEST_F(BugreportSectionTest, WifiSectionGenerated) {
294 SectionExists("DUMPSYS - wifi", /* bytes= */ 100000);
295}
296
297} // namespace dumpstate
298} // namespace os
299} // namespace android