blob: b863d150c9cd7e7e7957b4b612df64a8b572f9cd [file] [log] [blame]
Yi Jinadd11e92017-07-30 16:10:07 -07001// Copyright (C) 2017 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Yi Jin4e843102018-02-14 15:36:18 -080014#define DEBUG false
Yi Jinb592e3b2018-02-01 15:17:04 -080015#include "Log.h"
Yi Jinadd11e92017-07-30 16:10:07 -070016
17#include "Reporter.h"
18
19#include <android/os/BnIncidentReportStatusListener.h>
Yi Jin437aa6e2018-01-10 11:34:26 -080020#include <frameworks/base/libs/incident/proto/android/os/header.pb.h>
Yi Jinadd11e92017-07-30 16:10:07 -070021
Yi Jinadd11e92017-07-30 16:10:07 -070022#include <dirent.h>
Mark Salyzynd1001072018-11-14 15:13:39 -080023#include <string.h>
24
25#include <android-base/file.h>
Yi Jinadd11e92017-07-30 16:10:07 -070026#include <gmock/gmock.h>
27#include <gtest/gtest.h>
Yi Jinadd11e92017-07-30 16:10:07 -070028
Yi Jinadd11e92017-07-30 16:10:07 -070029using namespace android;
30using namespace android::base;
31using namespace android::binder;
Yi Jin437aa6e2018-01-10 11:34:26 -080032using namespace android::os;
Yi Jin6cacbcb2018-03-30 14:04:52 -070033using namespace android::os::incidentd;
Yi Jinadd11e92017-07-30 16:10:07 -070034using namespace std;
35using ::testing::StrEq;
36using ::testing::Test;
Yi Jinadd11e92017-07-30 16:10:07 -070037
Yi Jinb592e3b2018-02-01 15:17:04 -080038class TestListener : public IIncidentReportStatusListener {
Yi Jinadd11e92017-07-30 16:10:07 -070039public:
40 int startInvoked;
41 int finishInvoked;
42 int failedInvoked;
43 map<int, int> startSections;
44 map<int, int> finishSections;
45
Yi Jinb592e3b2018-02-01 15:17:04 -080046 TestListener() : startInvoked(0), finishInvoked(0), failedInvoked(0){};
47 virtual ~TestListener(){};
Yi Jinadd11e92017-07-30 16:10:07 -070048
49 virtual Status onReportStarted() {
50 startInvoked++;
51 return Status::ok();
52 };
53 virtual Status onReportSectionStatus(int section, int status) {
54 switch (status) {
Yi Jinb592e3b2018-02-01 15:17:04 -080055 case IIncidentReportStatusListener::STATUS_STARTING:
56 if (startSections.count(section) == 0) startSections[section] = 0;
57 startSections[section] = startSections[section] + 1;
58 break;
59 case IIncidentReportStatusListener::STATUS_FINISHED:
60 if (finishSections.count(section) == 0) finishSections[section] = 0;
61 finishSections[section] = finishSections[section] + 1;
62 break;
Yi Jinadd11e92017-07-30 16:10:07 -070063 }
64 return Status::ok();
65 };
66 virtual Status onReportFinished() {
67 finishInvoked++;
68 return Status::ok();
69 };
70 virtual Status onReportFailed() {
71 failedInvoked++;
72 return Status::ok();
73 };
74
75protected:
Yi Jin0f047162017-09-05 13:44:22 -070076 virtual IBinder* onAsBinder() override { return nullptr; };
Yi Jinadd11e92017-07-30 16:10:07 -070077};
78
79class ReporterTest : public Test {
80public:
81 virtual void SetUp() {
82 reporter = new Reporter(td.path);
83 l = new TestListener();
84 }
85
86 vector<string> InspectFiles() {
87 DIR* dir;
88 struct dirent* entry;
89 vector<string> results;
90
91 string dirbase = string(td.path) + "/";
92 dir = opendir(td.path);
93
94 while ((entry = readdir(dir)) != NULL) {
95 if (entry->d_name[0] == '.') {
96 continue;
97 }
98 string filename = dirbase + entry->d_name;
99 string content;
100 ReadFileToString(filename, &content);
101 results.push_back(content);
102 }
103 return results;
104 }
105
106protected:
107 TemporaryDir td;
108 ReportRequestSet requests;
109 sp<Reporter> reporter;
110 sp<TestListener> l;
Yi Jin4e843102018-02-14 15:36:18 -0800111 size_t size;
Yi Jinadd11e92017-07-30 16:10:07 -0700112};
113
114TEST_F(ReporterTest, IncidentReportArgs) {
115 IncidentReportArgs args1, args2;
116 args1.addSection(1);
117 args2.addSection(3);
118
119 args1.merge(args2);
120 ASSERT_TRUE(args1.containsSection(1));
121 ASSERT_FALSE(args1.containsSection(2));
122 ASSERT_TRUE(args1.containsSection(3));
123}
124
125TEST_F(ReporterTest, ReportRequestSetEmpty) {
126 requests.setMainFd(STDOUT_FILENO);
Yi Jin99c248f2017-08-25 18:11:58 -0700127 ASSERT_EQ(requests.mainFd(), STDOUT_FILENO);
Yi Jinadd11e92017-07-30 16:10:07 -0700128}
129
130TEST_F(ReporterTest, RunReportEmpty) {
Yi Jin4e843102018-02-14 15:36:18 -0800131 ASSERT_EQ(Reporter::REPORT_FINISHED, reporter->runReport(&size));
Yi Jinadd11e92017-07-30 16:10:07 -0700132 EXPECT_EQ(l->startInvoked, 0);
133 EXPECT_EQ(l->finishInvoked, 0);
134 EXPECT_TRUE(l->startSections.empty());
135 EXPECT_TRUE(l->finishSections.empty());
136 EXPECT_EQ(l->failedInvoked, 0);
137}
138
139TEST_F(ReporterTest, RunReportWithHeaders) {
Yi Jine0833302017-10-23 15:42:44 -0700140 TemporaryFile tf;
Yi Jinadd11e92017-07-30 16:10:07 -0700141 IncidentReportArgs args1, args2;
142 args1.addSection(1);
143 args2.addSection(2);
Yi Jin437aa6e2018-01-10 11:34:26 -0800144 IncidentHeaderProto header;
145 header.set_alert_id(12);
Yi Jinadd11e92017-07-30 16:10:07 -0700146 args2.addHeader(header);
Yi Jine0833302017-10-23 15:42:44 -0700147 sp<ReportRequest> r1 = new ReportRequest(args1, l, tf.fd);
148 sp<ReportRequest> r2 = new ReportRequest(args2, l, tf.fd);
Yi Jinadd11e92017-07-30 16:10:07 -0700149
150 reporter->batch.add(r1);
151 reporter->batch.add(r2);
152
Yi Jin4e843102018-02-14 15:36:18 -0800153 ASSERT_EQ(Reporter::REPORT_FINISHED, reporter->runReport(&size));
Yi Jine0833302017-10-23 15:42:44 -0700154
155 string result;
156 ReadFileToString(tf.path, &result);
Yi Jinb592e3b2018-02-01 15:17:04 -0800157 EXPECT_THAT(result, StrEq("\n\x2"
158 "\b\f"));
Yi Jine0833302017-10-23 15:42:44 -0700159
Yi Jinadd11e92017-07-30 16:10:07 -0700160 EXPECT_EQ(l->startInvoked, 2);
161 EXPECT_EQ(l->finishInvoked, 2);
162 EXPECT_TRUE(l->startSections.empty());
163 EXPECT_TRUE(l->finishSections.empty());
164 EXPECT_EQ(l->failedInvoked, 0);
165}
166
167TEST_F(ReporterTest, RunReportToGivenDirectory) {
168 IncidentReportArgs args;
Yi Jin437aa6e2018-01-10 11:34:26 -0800169 IncidentHeaderProto header1, header2;
170 header1.set_alert_id(12);
171 header2.set_reason("abcd");
172 args.addHeader(header1);
173 args.addHeader(header2);
Yi Jinadd11e92017-07-30 16:10:07 -0700174 sp<ReportRequest> r = new ReportRequest(args, l, -1);
175 reporter->batch.add(r);
176
Yi Jin4e843102018-02-14 15:36:18 -0800177 ASSERT_EQ(Reporter::REPORT_FINISHED, reporter->runReport(&size));
Yi Jinadd11e92017-07-30 16:10:07 -0700178 vector<string> results = InspectFiles();
179 ASSERT_EQ((int)results.size(), 1);
Yi Jinb592e3b2018-02-01 15:17:04 -0800180 EXPECT_EQ(results[0],
181 "\n\x2"
182 "\b\f\n\x6"
183 "\x12\x4"
184 "abcd");
Yi Jinadd11e92017-07-30 16:10:07 -0700185}
Yi Jin329130b2018-02-09 16:47:47 -0800186
187TEST_F(ReporterTest, ReportMetadata) {
188 IncidentReportArgs args;
189 args.addSection(1);
190 args.setDest(android::os::DEST_EXPLICIT);
191 sp<ReportRequest> r = new ReportRequest(args, l, -1);
192 reporter->batch.add(r);
193
Yi Jin4e843102018-02-14 15:36:18 -0800194 ASSERT_EQ(Reporter::REPORT_FINISHED, reporter->runReport(&size));
Yi Jin86dce412018-03-07 11:36:57 -0800195 IncidentMetadata metadata = reporter->batch.metadata();
Yi Jin329130b2018-02-09 16:47:47 -0800196 EXPECT_EQ(IncidentMetadata_Destination_EXPLICIT, metadata.dest());
197 EXPECT_EQ(1, metadata.request_size());
198 EXPECT_TRUE(metadata.use_dropbox());
199 EXPECT_EQ(0, metadata.sections_size());
Mark Salyzynd1001072018-11-14 15:13:39 -0800200}