blob: 7e6f6f53e559eb0e61ce2f97b0fe935f90d6b762 [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
Vishnu Naire97d6122018-01-18 13:58:56 -080017#include <android-base/file.h>
Nandana Dutt16d1aee2019-02-15 16:13:53 +000018#include <android/os/BnDumpstate.h>
19#include <android/os/BnDumpstateListener.h>
20#include <binder/IServiceManager.h>
21#include <binder/ProcessState.h>
Vishnu Naire97d6122018-01-18 13:58:56 -080022#include <cutils/properties.h>
Nandana Duttea71b382019-07-05 09:19:42 +010023#include <fcntl.h>
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26#include <libgen.h>
Vishnu Naire97d6122018-01-18 13:58:56 -080027#include <ziparchive/zip_archive.h>
28
Nandana Duttea71b382019-07-05 09:19:42 +010029#include <fstream>
30#include <regex>
31
Vishnu Naire97d6122018-01-18 13:58:56 -080032#include "dumpstate.h"
33
34#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
35
36namespace android {
37namespace os {
38namespace dumpstate {
39
40using ::testing::Test;
41using ::std::literals::chrono_literals::operator""s;
Nandana Dutt16d1aee2019-02-15 16:13:53 +000042using android::base::unique_fd;
43
44class DumpstateListener;
45
46namespace {
47
Nandana Duttea71b382019-07-05 09:19:42 +010048struct SectionInfo {
49 std::string name;
50 int32_t size_bytes;
51};
52
Nandana Dutt16d1aee2019-02-15 16:13:53 +000053sp<IDumpstate> GetDumpstateService() {
54 return android::interface_cast<IDumpstate>(
55 android::defaultServiceManager()->getService(String16("dumpstate")));
56}
57
58int OpenForWrite(const std::string& filename) {
59 return TEMP_FAILURE_RETRY(open(filename.c_str(),
60 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
61 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
62}
63
Nandana Duttea71b382019-07-05 09:19:42 +010064void GetEntry(const ZipArchiveHandle archive, const std::string_view entry_name, ZipEntry* data) {
65 int32_t e = FindEntry(archive, entry_name, data);
66 EXPECT_EQ(e, 0) << ErrorCodeString(e) << " entry name: " << entry_name;
67}
Vishnu Naire97d6122018-01-18 13:58:56 -080068
Nandana Duttea71b382019-07-05 09:19:42 +010069// Extracts the main bugreport txt from the given archive and writes into output_fd.
70void ExtractBugreport(const ZipArchiveHandle* handle, int output_fd) {
71 // Read contents of main_entry.txt which is a single line indicating the name of the zip entry
72 // that contains the main bugreport txt.
73 ZipEntry main_entry;
74 GetEntry(*handle, "main_entry.txt", &main_entry);
75 std::string bugreport_txt_name;
76 bugreport_txt_name.resize(main_entry.uncompressed_length);
77 ExtractToMemory(*handle, &main_entry, reinterpret_cast<uint8_t*>(bugreport_txt_name.data()),
78 main_entry.uncompressed_length);
79
80 // Read the main bugreport txt and extract to output_fd.
81 ZipEntry entry;
82 GetEntry(*handle, bugreport_txt_name, &entry);
83 ExtractEntryToFile(*handle, &entry, output_fd);
84}
85
86bool IsSectionStart(const std::string& line, std::string* section_name) {
87 static const std::regex kSectionStart = std::regex{"DUMP OF SERVICE (.*):"};
88 std::smatch match;
89 if (std::regex_match(line, match, kSectionStart)) {
90 *section_name = match.str(1);
91 return true;
92 }
93 return false;
94}
95
96bool IsSectionEnd(const std::string& line) {
97 // Not all lines that contain "was the duration of" is a section end, but all section ends do
98 // contain "was the duration of". The disambiguation can be done by the caller.
99 return (line.find("was the duration of") != std::string::npos);
100}
101
102// Extracts the zipped bugreport and identifies the sections.
103void ParseSections(const std::string& zip_path, std::vector<SectionInfo>* sections) {
104 // Open the archive
105 ZipArchiveHandle handle;
106 ASSERT_EQ(OpenArchive(zip_path.c_str(), &handle), 0);
107
108 // Extract the main entry to a temp file
109 TemporaryFile tmp_binary;
110 ASSERT_NE(-1, tmp_binary.fd);
111 ExtractBugreport(&handle, tmp_binary.fd);
112
113 // Read line by line and identify sections
114 std::ifstream ifs(tmp_binary.path, std::ifstream::in);
115 std::string line;
116 int section_bytes = 0;
117 std::string current_section_name;
118 while (std::getline(ifs, line)) {
119 std::string section_name;
120 if (IsSectionStart(line, &section_name)) {
121 section_bytes = 0;
122 current_section_name = section_name;
123 } else if (IsSectionEnd(line)) {
124 if (!current_section_name.empty()) {
125 sections->push_back({current_section_name, section_bytes});
126 }
127 current_section_name = "";
128 } else if (!current_section_name.empty()) {
129 section_bytes += line.length();
130 }
131 }
132
133 CloseArchive(handle);
134}
135
136} // namespace
Vishnu Naire97d6122018-01-18 13:58:56 -0800137
138/**
139 * Listens to bugreport progress and updates the user by writing the progress to STDOUT. All the
140 * section details generated by dumpstate are added to a vector to be used by Tests later.
141 */
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000142class DumpstateListener : public BnDumpstateListener {
Vishnu Naire97d6122018-01-18 13:58:56 -0800143 public:
Vishnu Naire97d6122018-01-18 13:58:56 -0800144 DumpstateListener(int fd, std::shared_ptr<std::vector<SectionInfo>> sections)
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000145 : out_fd_(fd), sections_(sections) {
Vishnu Naire97d6122018-01-18 13:58:56 -0800146 }
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000147
148 DumpstateListener(int fd) : out_fd_(fd) {
149 }
150
Nandana Dutta6a28bd2019-01-14 16:54:38 +0000151 binder::Status onProgress(int32_t progress) override {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000152 dprintf(out_fd_, "\rIn progress %d", progress);
Nandana Dutta6a28bd2019-01-14 16:54:38 +0000153 return binder::Status::ok();
154 }
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000155
Nandana Dutta6a28bd2019-01-14 16:54:38 +0000156 binder::Status onError(int32_t error_code) override {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000157 std::lock_guard<std::mutex> lock(lock_);
158 error_code_ = error_code;
159 dprintf(out_fd_, "\rError code %d", error_code);
Nandana Dutta6a28bd2019-01-14 16:54:38 +0000160 return binder::Status::ok();
161 }
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000162
Nandana Duttcc4ead82019-01-23 08:29:23 +0000163 binder::Status onFinished() override {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000164 std::lock_guard<std::mutex> lock(lock_);
165 is_finished_ = true;
166 dprintf(out_fd_, "\rFinished");
Nandana Dutta6a28bd2019-01-14 16:54:38 +0000167 return binder::Status::ok();
168 }
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000169
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000170 bool getIsFinished() {
171 std::lock_guard<std::mutex> lock(lock_);
172 return is_finished_;
Vishnu Naire97d6122018-01-18 13:58:56 -0800173 }
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000174
175 int getErrorCode() {
176 std::lock_guard<std::mutex> lock(lock_);
177 return error_code_;
178 }
179
180 private:
181 int out_fd_;
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000182 int error_code_ = -1;
183 bool is_finished_ = false;
184 std::shared_ptr<std::vector<SectionInfo>> sections_;
185 std::mutex lock_;
Vishnu Naire97d6122018-01-18 13:58:56 -0800186};
187
188/**
189 * Generates bug report and provide access to the bug report file and other info for other tests.
190 * Since bug report generation is slow, the bugreport is only generated once.
191 */
192class ZippedBugreportGenerationTest : public Test {
193 public:
194 static std::shared_ptr<std::vector<SectionInfo>> sections;
195 static Dumpstate& ds;
196 static std::chrono::milliseconds duration;
197 static void SetUpTestCase() {
198 property_set("dumpstate.options", "bugreportplus");
199 // clang-format off
200 char* argv[] = {
201 (char*)"dumpstate",
202 (char*)"-d",
203 (char*)"-z",
204 (char*)"-B",
205 (char*)"-o",
206 (char*)dirname(android::base::GetExecutablePath().c_str())
207 };
208 // clang-format on
209 sp<DumpstateListener> listener(new DumpstateListener(dup(fileno(stdout)), sections));
210 ds.listener_ = listener;
211 ds.listener_name_ = "Smokey";
212 ds.report_section_ = true;
213 auto start = std::chrono::steady_clock::now();
Nandana Duttf02564e2019-02-15 15:24:24 +0000214 ds.ParseCommandlineAndRun(ARRAY_SIZE(argv), argv);
Vishnu Naire97d6122018-01-18 13:58:56 -0800215 auto end = std::chrono::steady_clock::now();
216 duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
217 }
218
Nick Desaulniers5cc046e2019-10-07 20:37:31 -0700219 static const std::string getZipFilePath() {
220 return ds.GetPath(".zip");
Vishnu Naire97d6122018-01-18 13:58:56 -0800221 }
222};
223std::shared_ptr<std::vector<SectionInfo>> ZippedBugreportGenerationTest::sections =
224 std::make_shared<std::vector<SectionInfo>>();
225Dumpstate& ZippedBugreportGenerationTest::ds = Dumpstate::GetInstance();
226std::chrono::milliseconds ZippedBugreportGenerationTest::duration = 0s;
227
228TEST_F(ZippedBugreportGenerationTest, IsGeneratedWithoutErrors) {
Nick Desaulniers5cc046e2019-10-07 20:37:31 -0700229 EXPECT_EQ(access(getZipFilePath().c_str(), F_OK), 0);
Vishnu Naire97d6122018-01-18 13:58:56 -0800230}
231
232TEST_F(ZippedBugreportGenerationTest, Is3MBto30MBinSize) {
233 struct stat st;
Nick Desaulniers5cc046e2019-10-07 20:37:31 -0700234 EXPECT_EQ(stat(getZipFilePath().c_str(), &st), 0);
Vishnu Naire97d6122018-01-18 13:58:56 -0800235 EXPECT_GE(st.st_size, 3000000 /* 3MB */);
236 EXPECT_LE(st.st_size, 30000000 /* 30MB */);
237}
238
239TEST_F(ZippedBugreportGenerationTest, TakesBetween30And150Seconds) {
240 EXPECT_GE(duration, 30s) << "Expected completion in more than 30s. Actual time "
241 << duration.count() << " s.";
242 EXPECT_LE(duration, 150s) << "Expected completion in less than 150s. Actual time "
243 << duration.count() << " s.";
244}
245
246/**
247 * Run tests on contents of zipped bug report.
248 */
249class ZippedBugReportContentsTest : public Test {
250 public:
251 ZipArchiveHandle handle;
252 void SetUp() {
Nick Desaulniers5cc046e2019-10-07 20:37:31 -0700253 ASSERT_EQ(OpenArchive(ZippedBugreportGenerationTest::getZipFilePath().c_str(), &handle), 0);
Vishnu Naire97d6122018-01-18 13:58:56 -0800254 }
255 void TearDown() {
256 CloseArchive(handle);
257 }
258
259 void FileExists(const char* filename, uint32_t minsize, uint32_t maxsize) {
260 ZipEntry entry;
Nandana Duttea71b382019-07-05 09:19:42 +0100261 GetEntry(handle, filename, &entry);
Vishnu Naire97d6122018-01-18 13:58:56 -0800262 EXPECT_GT(entry.uncompressed_length, minsize);
263 EXPECT_LT(entry.uncompressed_length, maxsize);
264 }
265};
266
267TEST_F(ZippedBugReportContentsTest, ContainsMainEntry) {
Nandana Duttea71b382019-07-05 09:19:42 +0100268 ZipEntry main_entry;
Vishnu Naire97d6122018-01-18 13:58:56 -0800269 // contains main entry name file
Nandana Duttea71b382019-07-05 09:19:42 +0100270 GetEntry(handle, "main_entry.txt", &main_entry);
Vishnu Naire97d6122018-01-18 13:58:56 -0800271
Nandana Duttea71b382019-07-05 09:19:42 +0100272 std::string bugreport_txt_name;
273 bugreport_txt_name.resize(main_entry.uncompressed_length);
274 ExtractToMemory(handle, &main_entry, reinterpret_cast<uint8_t*>(bugreport_txt_name.data()),
275 main_entry.uncompressed_length);
Vishnu Naire97d6122018-01-18 13:58:56 -0800276
277 // contains main entry file
Nandana Duttea71b382019-07-05 09:19:42 +0100278 FileExists(bugreport_txt_name.c_str(), 1000000U, 50000000U);
Vishnu Naire97d6122018-01-18 13:58:56 -0800279}
280
281TEST_F(ZippedBugReportContentsTest, ContainsVersion) {
282 ZipEntry entry;
283 // contains main entry name file
Nandana Duttea71b382019-07-05 09:19:42 +0100284 GetEntry(handle, "version.txt", &entry);
Vishnu Naire97d6122018-01-18 13:58:56 -0800285
286 char* buf = new char[entry.uncompressed_length + 1];
287 ExtractToMemory(handle, &entry, (uint8_t*)buf, entry.uncompressed_length);
288 buf[entry.uncompressed_length] = 0;
289 EXPECT_STREQ(buf, ZippedBugreportGenerationTest::ds.version_.c_str());
290 delete[] buf;
291}
292
293TEST_F(ZippedBugReportContentsTest, ContainsBoardSpecificFiles) {
294 FileExists("dumpstate_board.bin", 1000000U, 80000000U);
295 FileExists("dumpstate_board.txt", 100000U, 1000000U);
296}
297
Nandana Duttea71b382019-07-05 09:19:42 +0100298TEST_F(ZippedBugReportContentsTest, ContainsProtoFile) {
299 FileExists("proto/activity.proto", 100000U, 1000000U);
300}
301
Vishnu Naire97d6122018-01-18 13:58:56 -0800302// Spot check on some files pulled from the file system
303TEST_F(ZippedBugReportContentsTest, ContainsSomeFileSystemFiles) {
304 // FS/proc/*/mountinfo size > 0
305 FileExists("FS/proc/1/mountinfo", 0U, 100000U);
306
307 // FS/data/misc/profiles/cur/0/*/primary.prof size > 0
308 FileExists("FS/data/misc/profiles/cur/0/com.android.phone/primary.prof", 0U, 100000U);
309}
310
311/**
312 * Runs tests on section data generated by dumpstate and captured by DumpstateListener.
313 */
314class BugreportSectionTest : public Test {
315 public:
Nandana Duttea71b382019-07-05 09:19:42 +0100316 static void SetUpTestCase() {
Nick Desaulniers5cc046e2019-10-07 20:37:31 -0700317 ParseSections(ZippedBugreportGenerationTest::getZipFilePath().c_str(),
Nandana Duttea71b382019-07-05 09:19:42 +0100318 ZippedBugreportGenerationTest::sections.get());
319 }
320
Vishnu Naire97d6122018-01-18 13:58:56 -0800321 int numMatches(const std::string& substring) {
322 int matches = 0;
323 for (auto const& section : *ZippedBugreportGenerationTest::sections) {
324 if (section.name.find(substring) != std::string::npos) {
325 matches++;
326 }
327 }
328 return matches;
329 }
Nandana Duttea71b382019-07-05 09:19:42 +0100330
Vishnu Naire97d6122018-01-18 13:58:56 -0800331 void SectionExists(const std::string& sectionName, int minsize) {
332 for (auto const& section : *ZippedBugreportGenerationTest::sections) {
333 if (sectionName == section.name) {
Nandana Duttea71b382019-07-05 09:19:42 +0100334 EXPECT_GE(section.size_bytes, minsize) << " for section:" << sectionName;
Vishnu Naire97d6122018-01-18 13:58:56 -0800335 return;
336 }
337 }
338 FAIL() << sectionName << " not found.";
339 }
340};
341
Vishnu Naire97d6122018-01-18 13:58:56 -0800342TEST_F(BugreportSectionTest, Atleast3CriticalDumpsysSectionsGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100343 int numSections = numMatches("CRITICAL");
Vishnu Naire97d6122018-01-18 13:58:56 -0800344 EXPECT_GE(numSections, 3);
345}
346
347TEST_F(BugreportSectionTest, Atleast2HighDumpsysSectionsGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100348 int numSections = numMatches("HIGH");
Vishnu Naire97d6122018-01-18 13:58:56 -0800349 EXPECT_GE(numSections, 2);
350}
351
352TEST_F(BugreportSectionTest, Atleast50NormalDumpsysSectionsGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100353 int allSections = ZippedBugreportGenerationTest::sections->size();
354 int criticalSections = numMatches("CRITICAL");
355 int highSections = numMatches("HIGH");
Vishnu Naire97d6122018-01-18 13:58:56 -0800356 int normalSections = allSections - criticalSections - highSections;
357
358 EXPECT_GE(normalSections, 50) << "Total sections less than 50 (Critical:" << criticalSections
359 << "High:" << highSections << "Normal:" << normalSections << ")";
360}
361
Vishnu Naire97d6122018-01-18 13:58:56 -0800362// Test if some critical sections are being generated.
363TEST_F(BugreportSectionTest, CriticalSurfaceFlingerSectionGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100364 SectionExists("CRITICAL SurfaceFlinger", /* bytes= */ 10000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800365}
366
367TEST_F(BugreportSectionTest, ActivitySectionsGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100368 SectionExists("CRITICAL activity", /* bytes= */ 5000);
369 SectionExists("activity", /* bytes= */ 10000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800370}
371
372TEST_F(BugreportSectionTest, CpuinfoSectionGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100373 SectionExists("CRITICAL cpuinfo", /* bytes= */ 1000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800374}
375
376TEST_F(BugreportSectionTest, WindowSectionGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100377 SectionExists("CRITICAL window", /* bytes= */ 20000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800378}
379
380TEST_F(BugreportSectionTest, ConnectivitySectionsGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100381 SectionExists("HIGH connectivity", /* bytes= */ 3000);
382 SectionExists("connectivity", /* bytes= */ 5000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800383}
384
385TEST_F(BugreportSectionTest, MeminfoSectionGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100386 SectionExists("HIGH meminfo", /* bytes= */ 100000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800387}
388
389TEST_F(BugreportSectionTest, BatteryStatsSectionGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100390 SectionExists("batterystats", /* bytes= */ 1000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800391}
392
393TEST_F(BugreportSectionTest, WifiSectionGenerated) {
Nandana Duttea71b382019-07-05 09:19:42 +0100394 SectionExists("wifi", /* bytes= */ 100000);
Vishnu Naire97d6122018-01-18 13:58:56 -0800395}
396
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000397class DumpstateBinderTest : public Test {
398 protected:
399 void SetUp() override {
400 // In case there is a stray service, stop it first.
401 property_set("ctl.stop", "bugreportd");
402 // dry_run results in a faster bugreport.
403 property_set("dumpstate.dry_run", "true");
404 // We need to receive some async calls later. Ensure we have binder threads.
405 ProcessState::self()->startThreadPool();
406 }
407
408 void TearDown() override {
409 property_set("ctl.stop", "bugreportd");
410 property_set("dumpstate.dry_run", "");
411
412 unlink("/data/local/tmp/tmp.zip");
413 unlink("/data/local/tmp/tmp.png");
414 }
415
416 // Waits until listener gets the callbacks.
417 void WaitTillExecutionComplete(DumpstateListener* listener) {
418 // Wait till one of finished, error or timeout.
419 static const int kBugreportTimeoutSeconds = 120;
420 int i = 0;
421 while (!listener->getIsFinished() && listener->getErrorCode() == -1 &&
422 i < kBugreportTimeoutSeconds) {
423 sleep(1);
424 i++;
425 }
426 }
427};
428
429TEST_F(DumpstateBinderTest, Baseline) {
430 // In the beginning dumpstate binder service is not running.
431 sp<android::os::IDumpstate> ds_binder(GetDumpstateService());
432 EXPECT_EQ(ds_binder, nullptr);
433
434 // Start bugreportd, which runs dumpstate binary with -w; which starts dumpstate service
435 // and makes it wait.
436 property_set("dumpstate.dry_run", "true");
437 property_set("ctl.start", "bugreportd");
438
439 // Now we are able to retrieve dumpstate binder service.
440 ds_binder = GetDumpstateService();
441 EXPECT_NE(ds_binder, nullptr);
442
443 // Prepare arguments
444 unique_fd bugreport_fd(OpenForWrite("/bugreports/tmp.zip"));
445 unique_fd screenshot_fd(OpenForWrite("/bugreports/tmp.png"));
446
447 EXPECT_NE(bugreport_fd.get(), -1);
448 EXPECT_NE(screenshot_fd.get(), -1);
449
450 sp<DumpstateListener> listener(new DumpstateListener(dup(fileno(stdout))));
451 android::binder::Status status =
452 ds_binder->startBugreport(123, "com.dummy.package", bugreport_fd, screenshot_fd,
453 Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, listener);
454 // startBugreport is an async call. Verify binder call succeeded first, then wait till listener
455 // gets expected callbacks.
456 EXPECT_TRUE(status.isOk());
457 WaitTillExecutionComplete(listener.get());
458
459 // Bugreport generation requires user consent, which we cannot get in a test set up,
460 // so instead of getting is_finished_, we are more likely to get a consent error.
461 EXPECT_TRUE(
462 listener->getErrorCode() == IDumpstateListener::BUGREPORT_ERROR_USER_DENIED_CONSENT ||
463 listener->getErrorCode() == IDumpstateListener::BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT);
464
465 // The service should have died on its own, freeing itself up for a new invocation.
466 sleep(2);
467 ds_binder = GetDumpstateService();
468 EXPECT_EQ(ds_binder, nullptr);
469}
470
471TEST_F(DumpstateBinderTest, ServiceDies_OnInvalidInput) {
472 // Start bugreportd, which runs dumpstate binary with -w; which starts dumpstate service
473 // and makes it wait.
474 property_set("ctl.start", "bugreportd");
475 sp<android::os::IDumpstate> ds_binder(GetDumpstateService());
476 EXPECT_NE(ds_binder, nullptr);
477
478 // Prepare arguments
479 unique_fd bugreport_fd(OpenForWrite("/data/local/tmp/tmp.zip"));
480 unique_fd screenshot_fd(OpenForWrite("/data/local/tmp/tmp.png"));
481
482 EXPECT_NE(bugreport_fd.get(), -1);
483 EXPECT_NE(screenshot_fd.get(), -1);
484
485 // Call startBugreport with bad arguments.
486 sp<DumpstateListener> listener(new DumpstateListener(dup(fileno(stdout))));
487 android::binder::Status status =
488 ds_binder->startBugreport(123, "com.dummy.package", bugreport_fd, screenshot_fd,
489 2000, // invalid bugreport mode
490 listener);
491 EXPECT_EQ(listener->getErrorCode(), IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
492
493 // The service should have died, freeing itself up for a new invocation.
494 sleep(2);
495 ds_binder = GetDumpstateService();
496 EXPECT_EQ(ds_binder, nullptr);
497}
498
499TEST_F(DumpstateBinderTest, SimultaneousBugreportsNotAllowed) {
500 // Start bugreportd, which runs dumpstate binary with -w; which starts dumpstate service
501 // and makes it wait.
502 property_set("dumpstate.dry_run", "true");
503 property_set("ctl.start", "bugreportd");
504 sp<android::os::IDumpstate> ds_binder(GetDumpstateService());
505 EXPECT_NE(ds_binder, nullptr);
506
507 // Prepare arguments
508 unique_fd bugreport_fd(OpenForWrite("/data/local/tmp/tmp.zip"));
509 unique_fd screenshot_fd(OpenForWrite("/data/local/tmp/tmp.png"));
510
511 EXPECT_NE(bugreport_fd.get(), -1);
512 EXPECT_NE(screenshot_fd.get(), -1);
513
514 sp<DumpstateListener> listener1(new DumpstateListener(dup(fileno(stdout))));
515 android::binder::Status status =
516 ds_binder->startBugreport(123, "com.dummy.package", bugreport_fd, screenshot_fd,
517 Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, listener1);
518 EXPECT_TRUE(status.isOk());
519
520 // try to make another call to startBugreport. This should fail.
521 sp<DumpstateListener> listener2(new DumpstateListener(dup(fileno(stdout))));
522 status = ds_binder->startBugreport(123, "com.dummy.package", bugreport_fd, screenshot_fd,
523 Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE, listener2);
524 EXPECT_FALSE(status.isOk());
525 WaitTillExecutionComplete(listener2.get());
Nandana Dutt41d7dac2019-02-19 13:05:37 +0000526 EXPECT_EQ(listener2->getErrorCode(),
Nandana Dutt0eb86bf2019-02-21 16:10:10 +0000527 IDumpstateListener::BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS);
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000528
529 // Meanwhile the first call works as expected. Service should not die in this case.
530 WaitTillExecutionComplete(listener1.get());
531
532 // Bugreport generation requires user consent, which we cannot get in a test set up,
533 // so instead of getting is_finished_, we are more likely to get a consent error.
534 EXPECT_TRUE(
535 listener1->getErrorCode() == IDumpstateListener::BUGREPORT_ERROR_USER_DENIED_CONSENT ||
536 listener1->getErrorCode() == IDumpstateListener::BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT);
537}
538
Vishnu Naire97d6122018-01-18 13:58:56 -0800539} // namespace dumpstate
540} // namespace os
541} // namespace android