Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 1 | // 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 Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 14 | #define DEBUG false |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 15 | #include "Log.h" |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 16 | |
| 17 | #include "Section.h" |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/test_utils.h> |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 21 | #include <android/os/IncidentReportArgs.h> |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 22 | #include <android/util/protobuf.h> |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 23 | #include <frameworks/base/core/proto/android/os/incident.pb.h> |
| 24 | #include <frameworks/base/core/proto/android/os/header.pb.h> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 25 | #include <gmock/gmock.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <string.h> |
| 28 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 29 | using namespace android; |
| 30 | using namespace android::base; |
| 31 | using namespace android::binder; |
| 32 | using namespace android::os; |
| 33 | using namespace android::os::incidentd; |
| 34 | using namespace android::util; |
| 35 | using ::testing::StrEq; |
| 36 | using ::testing::Test; |
| 37 | using ::testing::internal::CaptureStdout; |
| 38 | using ::testing::internal::GetCapturedStdout; |
| 39 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 40 | const int TIMEOUT_PARSER = -1; |
| 41 | const int NOOP_PARSER = 0; |
| 42 | const int REVERSE_PARSER = 1; |
| 43 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 44 | const int QUICK_TIMEOUT_MS = 100; |
| 45 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 46 | const std::string VARINT_FIELD_1 = "\x08\x96\x01"; // 150 |
| 47 | const std::string STRING_FIELD_2 = "\x12\vandroidwins"; |
| 48 | const std::string FIX64_FIELD_3 = "\x19\xff\xff\xff\xff\xff\xff\xff\xff"; // -1 |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 49 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 50 | // NOTICE: this test requires /system/bin/incident_helper is installed. |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 51 | class SectionTest : public Test { |
| 52 | public: |
| 53 | virtual void SetUp() override { ASSERT_NE(tf.fd, -1); } |
| 54 | |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 55 | void printDebugString(std::string s) { |
| 56 | fprintf(stderr, "size: %zu\n", s.length()); |
| 57 | for (size_t i = 0; i < s.length(); i++) { |
| 58 | char c = s[i]; |
| 59 | fprintf(stderr, "\\x%x", c); |
| 60 | } |
| 61 | fprintf(stderr, "\n"); |
| 62 | } |
| 63 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 64 | protected: |
| 65 | TemporaryFile tf; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 66 | |
| 67 | const std::string kTestPath = GetExecutableDirectory(); |
| 68 | const std::string kTestDataPath = kTestPath + "/testdata/"; |
| 69 | }; |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 70 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 71 | class SimpleListener : public IIncidentReportStatusListener { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 72 | public: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 73 | SimpleListener(){}; |
| 74 | virtual ~SimpleListener(){}; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 75 | |
| 76 | virtual Status onReportStarted() { return Status::ok(); }; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 77 | virtual Status onReportSectionStatus(int /*section*/, int /*status*/) |
| 78 | { return Status::ok(); }; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 79 | virtual Status onReportFinished() { return Status::ok(); }; |
| 80 | virtual Status onReportFailed() { return Status::ok(); }; |
| 81 | |
| 82 | protected: |
| 83 | virtual IBinder* onAsBinder() override { return nullptr; }; |
| 84 | }; |
| 85 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 86 | /* |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 87 | TEST_F(SectionTest, MetadataSection) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 88 | MetadataSection ms; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 89 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 90 | vector<sp<ReportRequest>> requests; |
| 91 | ReportRequestSet requestSet(requests); |
| 92 | requestSet.setMainFd(STDOUT_FILENO); |
| 93 | |
| 94 | requestSet.setMainPrivacyPolicy(android::os::PRIVACY_POLICY_LOCAL); |
| 95 | requestSet.editSectionStats(1)->set_success(true); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 96 | |
| 97 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 98 | ASSERT_EQ(NO_ERROR, ms.Execute(&requestSet)); |
| 99 | |
| 100 | string out = GetCapturedStdout(); |
| 101 | IncidentProto expectedIncident; |
| 102 | expectedIncident.ParseFromArray(out.data(), out.size()); |
| 103 | ASSERT_TRUE(expectedIncident.has_metadata()); |
| 104 | const IncidentMetadata& expectedMetadata = expectedIncident.metadata(); |
| 105 | ASSERT_EQ(IncidentMetadata::LOCAL, expectedMetadata.dest()); |
| 106 | ASSERT_EQ(1, expectedMetadata.sections_size()); |
| 107 | ASSERT_EQ(1, expectedMetadata.sections(0).id()); |
| 108 | ASSERT_TRUE(expectedMetadata.sections(0).has_success()); |
| 109 | ASSERT_TRUE(expectedMetadata.sections(0).success()); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 112 | TEST_F(SectionTest, FileSection) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 113 | FileSection fs(REVERSE_PARSER, tf.path); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 114 | |
Wei Wang | 254102d | 2017-11-09 21:45:29 -0800 | [diff] [blame] | 115 | ASSERT_TRUE(WriteStringToFile("iamtestdata", tf.path)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 116 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 117 | vector<sp<ReportRequest>> requests; |
| 118 | ReportRequestSet requestSet(requests); |
| 119 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 120 | |
| 121 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 122 | ASSERT_EQ(NO_ERROR, fs.Execute(&requestSet)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 123 | // The input string is reversed in incident helper |
| 124 | // The length is 11, in 128Varint it is "0000 1011" -> \v |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 125 | EXPECT_THAT(GetCapturedStdout(), StrEq("\xa\vatadtsetmai")); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Yi Jin | 3f36035 | 2018-04-16 16:13:04 -0700 | [diff] [blame] | 128 | TEST_F(SectionTest, FileSectionNotExist) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 129 | vector<sp<ReportRequest>> requests; |
| 130 | ReportRequestSet requestSet(requests); |
| 131 | |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 132 | FileSection fs1(NOOP_PARSER, "notexist", QUICK_TIMEOUT_MS); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 133 | ASSERT_EQ(NO_ERROR, fs1.Execute(&requestSet)); |
Yi Jin | 3f36035 | 2018-04-16 16:13:04 -0700 | [diff] [blame] | 134 | |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 135 | FileSection fs2(NOOP_PARSER, "notexist", QUICK_TIMEOUT_MS); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 136 | ASSERT_EQ(NO_ERROR, fs2.Execute(&requestSet)); |
Yi Jin | 3f36035 | 2018-04-16 16:13:04 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 139 | TEST_F(SectionTest, FileSectionTimeout) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 140 | vector<sp<ReportRequest>> requests; |
| 141 | ReportRequestSet requestSet(requests); |
| 142 | |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 143 | FileSection fs(TIMEOUT_PARSER, tf.path, QUICK_TIMEOUT_MS); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 144 | ASSERT_EQ(NO_ERROR, fs.Execute(&requestSet)); |
| 145 | ASSERT_TRUE(requestSet.getSectionStats(TIMEOUT_PARSER)->timed_out()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 148 | TEST_F(SectionTest, GZipSection) { |
| 149 | const std::string testFile = kTestDataPath + "kmsg.txt"; |
| 150 | const std::string testGzFile = testFile + ".gz"; |
| 151 | GZipSection gs(NOOP_PARSER, "/tmp/nonexist", testFile.c_str(), NULL); |
| 152 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 153 | vector<sp<ReportRequest>> requests; |
| 154 | ReportRequestSet requestSet(requests); |
| 155 | requestSet.setMainFd(tf.fd); |
| 156 | requestSet.setMainPrivacyPolicy(android::os::PRIVACY_POLICY_LOCAL); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 157 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 158 | ASSERT_EQ(NO_ERROR, gs.Execute(&requestSet)); |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 159 | std::string expected, gzFile, actual; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 160 | ASSERT_TRUE(ReadFileToString(testGzFile, &gzFile)); |
| 161 | ASSERT_TRUE(ReadFileToString(tf.path, &actual)); |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 162 | // generates the expected protobuf result. |
| 163 | size_t fileLen = testFile.size(); |
| 164 | size_t totalLen = 1 + get_varint_size(fileLen) + fileLen + 3 + gzFile.size(); |
| 165 | uint8_t header[20]; |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 166 | header[0] = '\x2'; // header 0 << 3 + 2 |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 167 | uint8_t* ptr = write_raw_varint(header + 1, totalLen); |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 168 | *ptr = '\n'; // header 1 << 3 + 2 |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 169 | ptr = write_raw_varint(++ptr, fileLen); |
| 170 | expected.assign((const char*)header, ptr - header); |
| 171 | expected += testFile + "\x12\x9F\x6" + gzFile; |
| 172 | EXPECT_THAT(actual, StrEq(expected)); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | TEST_F(SectionTest, GZipSectionNoFileFound) { |
| 176 | GZipSection gs(NOOP_PARSER, "/tmp/nonexist1", "/tmp/nonexist2", NULL); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 177 | vector<sp<ReportRequest>> requests; |
| 178 | ReportRequestSet requestSet(requests); |
| 179 | requestSet.setMainFd(STDOUT_FILENO); |
| 180 | ASSERT_EQ(NO_ERROR, gs.Execute(&requestSet)); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | TEST_F(SectionTest, CommandSectionConstructor) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 184 | CommandSection cs1(1, "echo", "\"this is a test\"", "ooo", NULL); |
| 185 | CommandSection cs2(2, "single_command", NULL); |
| 186 | CommandSection cs3(1, 3123, "echo", "\"this is a test\"", "ooo", NULL); |
| 187 | CommandSection cs4(2, 43214, "single_command", NULL); |
| 188 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 189 | EXPECT_THAT(cs1.name.string(), StrEq("cmd echo \"this is a test\" ooo")); |
| 190 | EXPECT_THAT(cs2.name.string(), StrEq("cmd single_command")); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 191 | EXPECT_EQ(3123, cs3.timeoutMs); |
| 192 | EXPECT_EQ(43214, cs4.timeoutMs); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 193 | EXPECT_THAT(cs3.name.string(), StrEq("cmd echo \"this is a test\" ooo")); |
| 194 | EXPECT_THAT(cs4.name.string(), StrEq("cmd single_command")); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 197 | TEST_F(SectionTest, CommandSectionEcho) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 198 | CommandSection cs(REVERSE_PARSER, "/system/bin/echo", "about", NULL); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 199 | vector<sp<ReportRequest>> requests; |
| 200 | ReportRequestSet requestSet(requests); |
| 201 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 202 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 203 | ASSERT_EQ(NO_ERROR, cs.Execute(&requestSet)); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 204 | EXPECT_THAT(GetCapturedStdout(), StrEq("\xa\x06\ntuoba")); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 207 | TEST_F(SectionTest, CommandSectionCommandTimeout) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 208 | CommandSection cs(NOOP_PARSER, QUICK_TIMEOUT_MS, "/system/bin/yes", NULL); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 209 | vector<sp<ReportRequest>> requests; |
| 210 | ReportRequestSet requestSet(requests); |
| 211 | ASSERT_EQ(NO_ERROR, cs.Execute(&requestSet)); |
| 212 | ASSERT_TRUE(requestSet.getSectionStats(NOOP_PARSER)->timed_out()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 215 | TEST_F(SectionTest, CommandSectionIncidentHelperTimeout) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 216 | CommandSection cs(TIMEOUT_PARSER, QUICK_TIMEOUT_MS, "/system/bin/echo", "about", NULL); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 217 | vector<sp<ReportRequest>> requests; |
| 218 | ReportRequestSet requestSet(requests); |
| 219 | requestSet.setMainFd(STDOUT_FILENO); |
| 220 | ASSERT_EQ(NO_ERROR, cs.Execute(&requestSet)); |
| 221 | ASSERT_TRUE(requestSet.getSectionStats(TIMEOUT_PARSER)->timed_out()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 224 | TEST_F(SectionTest, CommandSectionBadCommand) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 225 | CommandSection cs(NOOP_PARSER, "echoo", "about", NULL); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 226 | vector<sp<ReportRequest>> requests; |
| 227 | ReportRequestSet requestSet(requests); |
| 228 | ASSERT_EQ(NAME_NOT_FOUND, cs.Execute(&requestSet)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 231 | TEST_F(SectionTest, CommandSectionBadCommandAndTimeout) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 232 | CommandSection cs(TIMEOUT_PARSER, QUICK_TIMEOUT_MS, "nonexistcommand", "-opt", NULL); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 233 | // timeout will return first |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 234 | vector<sp<ReportRequest>> requests; |
| 235 | ReportRequestSet requestSet(requests); |
| 236 | ASSERT_EQ(NO_ERROR, cs.Execute(&requestSet)); |
| 237 | ASSERT_TRUE(requestSet.getSectionStats(TIMEOUT_PARSER)->timed_out()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 240 | TEST_F(SectionTest, LogSectionBinary) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 241 | LogSection ls(1, LOG_ID_EVENTS); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 242 | vector<sp<ReportRequest>> requests; |
| 243 | ReportRequestSet requestSet(requests); |
| 244 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 245 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 246 | ASSERT_EQ(NO_ERROR, ls.Execute(&requestSet)); |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 247 | std::string results = GetCapturedStdout(); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 248 | EXPECT_FALSE(results.empty()); |
| 249 | } |
| 250 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 251 | TEST_F(SectionTest, LogSectionSystem) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 252 | LogSection ls(1, LOG_ID_SYSTEM); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 253 | vector<sp<ReportRequest>> requests; |
| 254 | ReportRequestSet requestSet(requests); |
| 255 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 256 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 257 | ASSERT_EQ(NO_ERROR, ls.Execute(&requestSet)); |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 258 | std::string results = GetCapturedStdout(); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 259 | EXPECT_FALSE(results.empty()); |
| 260 | } |
| 261 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 262 | TEST_F(SectionTest, TestFilterPiiTaggedFields) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 263 | FileSection fs(NOOP_PARSER, tf.path); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 264 | |
Wei Wang | 254102d | 2017-11-09 21:45:29 -0800 | [diff] [blame] | 265 | ASSERT_TRUE(WriteStringToFile(VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3, tf.path)); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 266 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 267 | vector<sp<ReportRequest>> requests; |
| 268 | ReportRequestSet requestSet(requests); |
| 269 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 270 | |
| 271 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 272 | ASSERT_EQ(NO_ERROR, fs.Execute(&requestSet)); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 273 | EXPECT_THAT(GetCapturedStdout(), StrEq("\x02\r" + STRING_FIELD_2)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 276 | TEST_F(SectionTest, TestBadFdRequest) { |
| 277 | FileSection fs(NOOP_PARSER, tf.path); |
| 278 | ASSERT_TRUE(WriteStringToFile(VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3, tf.path)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 279 | |
| 280 | IncidentReportArgs args; |
| 281 | args.setAll(true); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 282 | args.setPrivacyPolicy(0); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 283 | sp<ReportRequest> badFdRequest = new ReportRequest(args, new SimpleListener(), 1234567); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 284 | |
| 285 | vector<sp<ReportRequest>> requests; |
| 286 | requests.push_back(badFdRequest); |
| 287 | ReportRequestSet requestSet(requests); |
| 288 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 289 | |
| 290 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 291 | ASSERT_EQ(NO_ERROR, fs.Execute(&requestSet)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 292 | EXPECT_THAT(GetCapturedStdout(), StrEq("\x02\r" + STRING_FIELD_2)); |
| 293 | EXPECT_EQ(badFdRequest->err, -EBADF); |
| 294 | } |
| 295 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 296 | TEST_F(SectionTest, TestBadRequests) { |
| 297 | FileSection fs(NOOP_PARSER, tf.path); |
| 298 | ASSERT_TRUE(WriteStringToFile(VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3, tf.path)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 299 | |
| 300 | IncidentReportArgs args; |
| 301 | args.setAll(true); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 302 | args.setPrivacyPolicy(0); |
| 303 | |
| 304 | vector<sp<ReportRequest>> requests; |
| 305 | requests.push_back(new ReportRequest(args, new SimpleListener(), -1)); |
| 306 | ReportRequestSet requestSet(requests); |
| 307 | |
| 308 | EXPECT_EQ(fs.Execute(&requestSet), -EBADF); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 311 | TEST_F(SectionTest, TestMultipleRequests) { |
| 312 | TemporaryFile output1, output2, output3; |
| 313 | FileSection fs(NOOP_PARSER, tf.path); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 314 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 315 | ASSERT_TRUE(output1.fd != -1); |
| 316 | ASSERT_TRUE(output2.fd != -1); |
| 317 | ASSERT_TRUE(output3.fd != -1); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 318 | ASSERT_TRUE(WriteStringToFile(VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3, tf.path)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 319 | |
| 320 | IncidentReportArgs args1, args2, args3; |
| 321 | args1.setAll(true); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 322 | args1.setPrivacyPolicy(android::os::PRIVACY_POLICY_LOCAL); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 323 | args2.setAll(true); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 324 | args2.setPrivacyPolicy(android::os::PRIVACY_POLICY_EXPLICIT); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 325 | sp<SimpleListener> l = new SimpleListener(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 326 | |
| 327 | vector<sp<ReportRequest>> requests; |
| 328 | requests.push_back(new ReportRequest(args1, l, output1.fd)); |
| 329 | requests.push_back(new ReportRequest(args2, l, output2.fd)); |
| 330 | requests.push_back(new ReportRequest(args3, l, output3.fd)); |
| 331 | ReportRequestSet requestSet(requests); |
| 332 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 333 | |
| 334 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 335 | ASSERT_EQ(NO_ERROR, fs.Execute(&requestSet)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 336 | EXPECT_THAT(GetCapturedStdout(), StrEq("\x02\r" + STRING_FIELD_2)); |
| 337 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 338 | std::string content, expect; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 339 | expect = VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 340 | char c = (char)expect.size(); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 341 | EXPECT_TRUE(ReadFileToString(output1.path, &content)); |
| 342 | EXPECT_THAT(content, StrEq(string("\x02") + c + expect)); |
| 343 | |
| 344 | expect = STRING_FIELD_2 + FIX64_FIELD_3; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 345 | c = (char)expect.size(); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 346 | EXPECT_TRUE(ReadFileToString(output2.path, &content)); |
| 347 | EXPECT_THAT(content, StrEq(string("\x02") + c + expect)); |
| 348 | |
| 349 | // because args3 doesn't set section, so it should receive nothing |
| 350 | EXPECT_TRUE(ReadFileToString(output3.path, &content)); |
| 351 | EXPECT_THAT(content, StrEq("")); |
| 352 | } |
| 353 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 354 | TEST_F(SectionTest, TestMultipleRequestsBySpec) { |
| 355 | TemporaryFile output1, output2, output3; |
| 356 | FileSection fs(NOOP_PARSER, tf.path); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 357 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 358 | ASSERT_TRUE(output1.fd != -1); |
| 359 | ASSERT_TRUE(output2.fd != -1); |
| 360 | ASSERT_TRUE(output3.fd != -1); |
| 361 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 362 | ASSERT_TRUE(WriteStringToFile(VARINT_FIELD_1 + STRING_FIELD_2 + FIX64_FIELD_3, tf.path)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 363 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 364 | IncidentReportArgs args1, args2, args3; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 365 | args1.setAll(true); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 366 | args1.setPrivacyPolicy(android::os::PRIVACY_POLICY_EXPLICIT); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 367 | args2.setAll(true); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 368 | args2.setPrivacyPolicy(android::os::PRIVACY_POLICY_EXPLICIT); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 369 | args3.setAll(true); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 370 | sp<SimpleListener> l = new SimpleListener(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 371 | |
| 372 | vector<sp<ReportRequest>> requests; |
| 373 | requests.push_back(new ReportRequest(args1, l, output1.fd)); |
| 374 | requests.push_back(new ReportRequest(args2, l, output2.fd)); |
| 375 | requests.push_back(new ReportRequest(args3, l, output3.fd)); |
| 376 | ReportRequestSet requestSet(requests); |
| 377 | requestSet.setMainFd(STDOUT_FILENO); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 378 | |
| 379 | CaptureStdout(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 380 | ASSERT_EQ(NO_ERROR, fs.Execute(&requestSet)); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 381 | EXPECT_THAT(GetCapturedStdout(), StrEq("\x02\r" + STRING_FIELD_2)); |
| 382 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 383 | std::string content, expect; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 384 | expect = STRING_FIELD_2 + FIX64_FIELD_3; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 385 | char c = (char)expect.size(); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 386 | |
| 387 | // output1 and output2 are the same |
| 388 | EXPECT_TRUE(ReadFileToString(output1.path, &content)); |
| 389 | EXPECT_THAT(content, StrEq(string("\x02") + c + expect)); |
| 390 | EXPECT_TRUE(ReadFileToString(output2.path, &content)); |
| 391 | EXPECT_THAT(content, StrEq(string("\x02") + c + expect)); |
| 392 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 393 | // output3 has only auto field |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 394 | c = (char)STRING_FIELD_2.size(); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 395 | EXPECT_TRUE(ReadFileToString(output3.path, &content)); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 396 | EXPECT_THAT(content, StrEq(string("\x02") + c + STRING_FIELD_2)); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 397 | } |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 398 | */ |