blob: a2e94538c2f281f92c024137770e3f12dc13baea [file] [log] [blame]
Felipe Leme4c2d6632016-09-28 14:32:00 -07001/*
2 * Copyright (C) 2016 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
Felipe Leme75876a22016-10-27 16:31:27 -070017#define LOG_TAG "dumpstate"
18#include <cutils/log.h>
19
Felipe Lemef0292972016-11-22 13:57:05 -080020#include "DumpstateInternal.h"
Felipe Leme75876a22016-10-27 16:31:27 -070021#include "DumpstateService.h"
22#include "android/os/BnDumpstate.h"
Felipe Leme4c2d6632016-09-28 14:32:00 -070023#include "dumpstate.h"
24
25#include <gmock/gmock.h>
26#include <gtest/gtest.h>
27
Felipe Leme46b85da2016-11-21 17:40:45 -080028#include <fcntl.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070029#include <libgen.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070030#include <signal.h>
31#include <sys/types.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070032#include <unistd.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070033#include <thread>
Felipe Leme4c2d6632016-09-28 14:32:00 -070034
35#include <android-base/file.h>
Felipe Lemed80e6b62016-10-03 13:08:14 -070036#include <android-base/properties.h>
37#include <android-base/stringprintf.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070038#include <android-base/strings.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070039
Felipe Leme47e9be22016-12-21 15:37:07 -080040namespace android {
41namespace os {
42namespace dumpstate {
Felipe Lemed80e6b62016-10-03 13:08:14 -070043
Felipe Leme4c2d6632016-09-28 14:32:00 -070044using ::testing::EndsWith;
Felipe Leme46b85da2016-11-21 17:40:45 -080045using ::testing::HasSubstr;
Felipe Leme009ecbb2016-11-07 10:18:44 -080046using ::testing::IsNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070047using ::testing::IsEmpty;
Felipe Leme009ecbb2016-11-07 10:18:44 -080048using ::testing::NotNull;
Felipe Leme4c2d6632016-09-28 14:32:00 -070049using ::testing::StrEq;
50using ::testing::StartsWith;
51using ::testing::Test;
52using ::testing::internal::CaptureStderr;
53using ::testing::internal::CaptureStdout;
54using ::testing::internal::GetCapturedStderr;
55using ::testing::internal::GetCapturedStdout;
56
Felipe Leme75876a22016-10-27 16:31:27 -070057class DumpstateListenerMock : public IDumpstateListener {
58 public:
59 MOCK_METHOD1(onProgressUpdated, binder::Status(int32_t progress));
60 MOCK_METHOD1(onMaxProgressUpdated, binder::Status(int32_t max_progress));
61
62 protected:
63 MOCK_METHOD0(onAsBinder, IBinder*());
64};
65
Felipe Leme46b85da2016-11-21 17:40:45 -080066static int calls_;
67
Felipe Leme7447d7c2016-11-03 18:12:22 -070068// Base class for all tests in this file
69class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080070 public:
71 virtual void SetUp() override {
72 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080073 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080074 }
75
Felipe Lemef0292972016-11-22 13:57:05 -080076 void SetDryRun(bool dry_run) const {
77 PropertiesHelper::dry_run_ = dry_run;
78 }
79
80 void SetBuildType(const std::string& build_type) const {
81 PropertiesHelper::build_type_ = build_type;
82 }
83
84 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -080085 return calls_ == 1;
86 }
87
Felipe Lemef0292972016-11-22 13:57:05 -080088 void DropRoot() const {
89 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -080090 uid_t uid = getuid();
91 ASSERT_EQ(2000, (int)uid);
92 }
93
Felipe Leme7447d7c2016-11-03 18:12:22 -070094 protected:
95 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
96 const std::string kFixturesPath = kTestPath + "/../dumpstate_test_fixture/";
Felipe Leme7fb8dee2017-08-25 10:15:01 -070097 const std::string kTestDataPath = kFixturesPath + "tests/testdata/";
Felipe Leme7447d7c2016-11-03 18:12:22 -070098 const std::string kSimpleCommand = kFixturesPath + "dumpstate_test_fixture";
99 const std::string kEchoCommand = "/system/bin/echo";
100
101 /*
102 * Copies a text file fixture to a temporary file, returning it's path.
103 *
104 * Useful in cases where the test case changes the content of the tile.
105 */
106 std::string CopyTextFileFixture(const std::string& relative_name) {
107 std::string from = kTestDataPath + relative_name;
108 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
109 // around for poking when the test fails.
110 std::string to = kTestDataPath + relative_name + ".tmp";
111 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
112 android::base::RemoveFileIfExists(to);
113 CopyTextFile(from, to);
114 return to.c_str();
115 }
116
Felipe Leme46b85da2016-11-21 17:40:45 -0800117 // Need functions that returns void to use assertions -
Felipe Leme7447d7c2016-11-03 18:12:22 -0700118 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
Felipe Leme46b85da2016-11-21 17:40:45 -0800119 void ReadFileToString(const std::string& path, std::string* content) {
120 ASSERT_TRUE(android::base::ReadFileToString(path, content))
121 << "could not read contents from " << path;
122 }
123 void WriteStringToFile(const std::string& content, const std::string& path) {
124 ASSERT_TRUE(android::base::WriteStringToFile(content, path))
125 << "could not write contents to " << path;
126 }
127
128 private:
Felipe Leme7447d7c2016-11-03 18:12:22 -0700129 void CopyTextFile(const std::string& from, const std::string& to) {
130 std::string content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800131 ReadFileToString(from, &content);
132 WriteStringToFile(content, to);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700133 }
134};
135
136class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700137 public:
138 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800139 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700140 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700141 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700142 ds.progress_.reset(new Progress());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700143 ds.update_progress_ = false;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800144 ds.update_progress_threshold_ = 0;
Felipe Leme4c2d6632016-09-28 14:32:00 -0700145 }
146
147 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700148 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700149 const CommandOptions& options = CommandOptions::DEFAULT) {
150 CaptureStdout();
151 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700152 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700153 out = GetCapturedStdout();
154 err = GetCapturedStderr();
155 return status;
156 }
157
Felipe Lemecef02982016-10-03 17:22:22 -0700158 // Dumps a file and capture `stdout` and `stderr`.
159 int DumpFile(const std::string& title, const std::string& path) {
160 CaptureStdout();
161 CaptureStderr();
162 int status = ds.DumpFile(title, path);
163 out = GetCapturedStdout();
164 err = GetCapturedStderr();
165 return status;
166 }
167
Felipe Leme009ecbb2016-11-07 10:18:44 -0800168 void SetProgress(long progress, long initial_max, long threshold = 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700169 ds.update_progress_ = true;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800170 ds.update_progress_threshold_ = threshold;
171 ds.last_updated_progress_ = 0;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700172 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
173 }
174
Felipe Leme7447d7c2016-11-03 18:12:22 -0700175 std::string GetProgressMessage(const std::string& listener_name, int progress, int max,
Felipe Leme009ecbb2016-11-07 10:18:44 -0800176 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700177 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
178 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700179
Felipe Leme7447d7c2016-11-03 18:12:22 -0700180 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700181
Felipe Leme009ecbb2016-11-07 10:18:44 -0800182 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700183 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800184 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700185 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700186 }
187
Felipe Leme009ecbb2016-11-07 10:18:44 -0800188 if (update_progress) {
189 message += android::base::StringPrintf("Setting progress (%s): %d/%d\n",
190 listener_name.c_str(), progress, max);
191 }
192
193 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700194 }
195
Felipe Leme4c2d6632016-09-28 14:32:00 -0700196 // `stdout` and `stderr` from the last command ran.
197 std::string out, err;
198
Felipe Lemefd8affa2016-09-30 17:38:57 -0700199 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700200};
201
202TEST_F(DumpstateTest, RunCommandNoArgs) {
203 EXPECT_EQ(-1, RunCommand("", {}));
204}
205
206TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700207 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700208 EXPECT_THAT(out, StrEq("stdout\n"));
209 EXPECT_THAT(err, StrEq("stderr\n"));
210}
211
212TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700213 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700214 EXPECT_THAT(err, StrEq("stderr\n"));
215 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700216 EXPECT_THAT(out,
Felipe Leme7447d7c2016-11-03 18:12:22 -0700217 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700218 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
219}
220
Felipe Lemefd8affa2016-09-30 17:38:57 -0700221TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700222 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700223 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700224 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
225 EXPECT_THAT(out, StrEq("stdout\n"));
226 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
227}
228
229TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700230 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700231 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700232 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700233 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700234}
235
236TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700237 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700238 EXPECT_THAT(err, IsEmpty());
239 EXPECT_THAT(out, StrEq("one\n"));
240}
241
Felipe Lemefd8affa2016-09-30 17:38:57 -0700242TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700243 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700244 EXPECT_THAT(err, IsEmpty());
245 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
246}
247
248TEST_F(DumpstateTest, RunCommandDryRun) {
249 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700250 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700251 // We don't know the exact duration, so we check the prefix and suffix
Felipe Leme7447d7c2016-11-03 18:12:22 -0700252 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700253 ") ------\n\t(skipped on dry run)\n------"));
254 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
255 EXPECT_THAT(err, IsEmpty());
256}
257
258TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
259 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700260 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700261 EXPECT_THAT(out, IsEmpty());
262 EXPECT_THAT(err, IsEmpty());
263}
264
265TEST_F(DumpstateTest, RunCommandDryRunAlways) {
266 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700267 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700268 EXPECT_THAT(out, StrEq("stdout\n"));
269 EXPECT_THAT(err, StrEq("stderr\n"));
270}
271
Felipe Lemefd8affa2016-09-30 17:38:57 -0700272TEST_F(DumpstateTest, RunCommandNotFound) {
273 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
274 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
275 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
276}
277
278TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700279 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
280 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700281 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700282 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700283 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700284}
285
286TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700287 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700288 // We don't know the exit code, so check just the prefix.
289 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700290 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700291 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700292 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700293}
294
295TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700296 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700297 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700298 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700299 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700300 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700301 " --sleep 2' timed out after 1"));
302}
303
304TEST_F(DumpstateTest, RunCommandIsKilled) {
305 CaptureStdout();
306 CaptureStderr();
307
308 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700309 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700310 CommandOptions::WithTimeout(100).Always().Build()));
311 });
312
313 // Capture pid and pre-sleep output.
314 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
315 std::string err = GetCapturedStderr();
316 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
317
318 std::string out = GetCapturedStdout();
319 std::vector<std::string> lines = android::base::Split(out, "\n");
320 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
321
322 int pid = atoi(lines[0].c_str());
323 EXPECT_THAT(lines[1], StrEq("stdout line1"));
324 EXPECT_THAT(lines[2], IsEmpty()); // \n
325
326 // Then kill the process.
327 CaptureStdout();
328 CaptureStderr();
329 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
330 t.join();
331
332 // Finally, check output after murder.
333 out = GetCapturedStdout();
334 err = GetCapturedStderr();
335
Felipe Leme7447d7c2016-11-03 18:12:22 -0700336 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700337 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700338 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700339 " --pid --sleep 20' failed: killed by signal 15\n"));
340}
341
Felipe Leme75876a22016-10-27 16:31:27 -0700342TEST_F(DumpstateTest, RunCommandProgress) {
343 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
344 ds.listener_ = listener;
345 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700346 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700347
348 EXPECT_CALL(*listener, onProgressUpdated(20));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700349 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700350 std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30);
351 EXPECT_THAT(out, StrEq("stdout\n"));
352 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
353
354 EXPECT_CALL(*listener, onProgressUpdated(30));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700355 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700356 progress_message = GetProgressMessage(ds.listener_name_, 30, 30);
357 EXPECT_THAT(out, StrEq("stdout\n"));
358 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
359
360 // Run a command that will increase maximum timeout.
361 EXPECT_CALL(*listener, onProgressUpdated(31));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700362 EXPECT_CALL(*listener, onMaxProgressUpdated(37));
363 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
364 progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30); // 20% increase
Felipe Leme75876a22016-10-27 16:31:27 -0700365 EXPECT_THAT(out, StrEq("stdout\n"));
366 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
367
368 // Make sure command ran while in dry_run is counted.
369 SetDryRun(true);
370 EXPECT_CALL(*listener, onProgressUpdated(35));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700371 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
372 progress_message = GetProgressMessage(ds.listener_name_, 35, 37);
Felipe Leme75876a22016-10-27 16:31:27 -0700373 EXPECT_THAT(out, IsEmpty());
374 EXPECT_THAT(err, StrEq(progress_message));
375
376 ds.listener_.clear();
377}
378
Felipe Leme009ecbb2016-11-07 10:18:44 -0800379TEST_F(DumpstateTest, RunCommandProgressIgnoreThreshold) {
380 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
381 ds.listener_ = listener;
382 ds.listener_name_ = "FoxMulder";
383 SetProgress(0, 8, 5); // 8 max, 5 threshold
384
385 // First update should always be sent.
386 EXPECT_CALL(*listener, onProgressUpdated(1));
387 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
388 std::string progress_message = GetProgressMessage(ds.listener_name_, 1, 8);
389 EXPECT_THAT(out, StrEq("stdout\n"));
390 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
391
392 // Fourth update should be ignored because it's between the threshold (5 -1 = 4 < 5).
393 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
394 EXPECT_THAT(out, StrEq("stdout\n"));
395 EXPECT_THAT(err, StrEq("stderr\n"));
396
397 // Third update should be sent because it reaches threshold (6 - 1 = 5).
398 EXPECT_CALL(*listener, onProgressUpdated(6));
399 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
400 progress_message = GetProgressMessage(ds.listener_name_, 6, 8);
401 EXPECT_THAT(out, StrEq("stdout\n"));
402 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
403
404 // Fourth update should be ignored because it's between the threshold (9 - 6 = 3 < 5).
405 // But max update should be sent.
406 EXPECT_CALL(*listener, onMaxProgressUpdated(10)); // 9 * 120% = 10.8 = 10
407 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
408 progress_message = GetProgressMessage(ds.listener_name_, 9, 10, 8, false);
409 EXPECT_THAT(out, StrEq("stdout\n"));
410 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
411
412 ds.listener_.clear();
413}
414
Felipe Lemed80e6b62016-10-03 13:08:14 -0700415TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800416 if (!IsStandalone()) {
417 // TODO: temporarily disabled because it might cause other tests to fail after dropping
418 // to Shell - need to refactor tests to avoid this problem)
419 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
420 return;
421 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700422 // First check root case - only available when running with 'adb root'.
423 uid_t uid = getuid();
424 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700425 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700426 EXPECT_THAT(out, StrEq("0\nstdout\n"));
427 EXPECT_THAT(err, StrEq("stderr\n"));
428 return;
429 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700430 // Then run dropping root.
431 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700432 CommandOptions::WithTimeout(1).DropRoot().Build()));
433 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700434 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700435}
436
437TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800438 if (!IsStandalone()) {
439 // TODO: temporarily disabled because it might cause other tests to fail after dropping
440 // to Shell - need to refactor tests to avoid this problem)
441 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
442 return;
443 }
Felipe Lemef0292972016-11-22 13:57:05 -0800444 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700445 // Emulates user build if necessarily.
446 SetBuildType("user");
447 }
448
449 DropRoot();
450
Felipe Leme7447d7c2016-11-03 18:12:22 -0700451 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700452
453 // We don't know the exact path of su, so we just check for the 'root ...' commands
454 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700455 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700456 EXPECT_THAT(err, IsEmpty());
457}
458
Felipe Leme46b85da2016-11-21 17:40:45 -0800459TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
460 if (!IsStandalone()) {
461 // TODO: temporarily disabled because it might cause other tests to fail after dropping
462 // to Shell - need to refactor tests to avoid this problem)
463 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
464 return;
465 }
Felipe Lemef0292972016-11-22 13:57:05 -0800466 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800467 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
468 return;
469 }
470
471 DropRoot();
472
473 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
474 CommandOptions::WithTimeout(1).AsRoot().Build()));
475
476 EXPECT_THAT(out, StrEq("0\nstdout\n"));
477 EXPECT_THAT(err, StrEq("stderr\n"));
478}
479
Yifan Hong48e83a12017-10-03 14:10:07 -0700480TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
481 if (!IsStandalone()) {
482 // TODO: temporarily disabled because it might cause other tests to fail after dropping
483 // to Shell - need to refactor tests to avoid this problem)
484 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
485 return;
486 }
487 if (!PropertiesHelper::IsUserBuild()) {
488 // Emulates user build if necessarily.
489 SetBuildType("user");
490 }
491
492 DropRoot();
493
494 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
495 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
496
497 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
498 EXPECT_THAT(err, StrEq("stderr\n"));
499}
500
501TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
502 if (!IsStandalone()) {
503 // TODO: temporarily disabled because it might cause other tests to fail after dropping
504 // to Shell - need to refactor tests to avoid this problem)
505 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
506 return;
507 }
508 if (PropertiesHelper::IsUserBuild()) {
509 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
510 return;
511 }
512
513 DropRoot();
514
515 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
516 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
517
518 EXPECT_THAT(out, StrEq("0\nstdout\n"));
519 EXPECT_THAT(err, StrEq("stderr\n"));
520}
521
Felipe Lemecef02982016-10-03 17:22:22 -0700522TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
523 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
524 EXPECT_THAT(out,
525 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
526 EXPECT_THAT(err, IsEmpty());
527}
528
529TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
530 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
531 EXPECT_THAT(err, IsEmpty());
532 // We don't know the exact duration, so we check the prefix and suffix
533 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
534 "such file or directory\n"));
535 EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n"));
536}
537
538TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700539 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700540 EXPECT_THAT(err, IsEmpty());
541 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
542}
543
544TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700545 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700546 EXPECT_THAT(err, IsEmpty());
547 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
548}
549
550TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700551 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700552 EXPECT_THAT(err, IsEmpty());
553 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
554}
555
556TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700557 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700558 EXPECT_THAT(err, IsEmpty());
559 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
560}
561
562TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
563 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700564 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700565 EXPECT_THAT(err, IsEmpty());
566 EXPECT_THAT(out, IsEmpty());
567}
568
569TEST_F(DumpstateTest, DumpFileOnDryRun) {
570 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700571 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700572 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -0800573 EXPECT_THAT(
574 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
575 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n------"));
Felipe Lemecef02982016-10-03 17:22:22 -0700576 EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700577}
578
Felipe Leme75876a22016-10-27 16:31:27 -0700579TEST_F(DumpstateTest, DumpFileUpdateProgress) {
580 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
581 ds.listener_ = listener;
582 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700583 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700584
585 EXPECT_CALL(*listener, onProgressUpdated(5));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700586 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -0700587
588 std::string progress_message =
589 GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
590 EXPECT_THAT(err, StrEq(progress_message));
591 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
592
593 ds.listener_.clear();
594}
595
Felipe Leme7447d7c2016-11-03 18:12:22 -0700596class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -0700597 public:
598 DumpstateService dss;
599};
600
601TEST_F(DumpstateServiceTest, SetListenerNoName) {
602 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800603 sp<IDumpstateToken> token;
604 EXPECT_TRUE(dss.setListener("", listener, &token).isOk());
605 ASSERT_THAT(token, IsNull());
Felipe Leme75876a22016-10-27 16:31:27 -0700606}
607
608TEST_F(DumpstateServiceTest, SetListenerNoPointer) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800609 sp<IDumpstateToken> token;
610 EXPECT_TRUE(dss.setListener("whatever", nullptr, &token).isOk());
611 ASSERT_THAT(token, IsNull());
Felipe Leme75876a22016-10-27 16:31:27 -0700612}
613
614TEST_F(DumpstateServiceTest, SetListenerTwice) {
615 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800616 sp<IDumpstateToken> token;
617 EXPECT_TRUE(dss.setListener("whatever", listener, &token).isOk());
618 ASSERT_THAT(token, NotNull());
Felipe Leme75876a22016-10-27 16:31:27 -0700619 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
620
Felipe Leme009ecbb2016-11-07 10:18:44 -0800621 token.clear();
622 EXPECT_TRUE(dss.setListener("whatsoever", listener, &token).isOk());
623 ASSERT_THAT(token, IsNull());
624 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
Felipe Leme75876a22016-10-27 16:31:27 -0700625}
Felipe Leme7447d7c2016-11-03 18:12:22 -0700626
627class ProgressTest : public DumpstateBaseTest {
628 public:
629 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
630 return Progress(max, growth_factor, path);
631 }
632
633 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
634 std::string expected_content =
635 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
636 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800637 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700638 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
639 }
640};
641
642TEST_F(ProgressTest, SimpleTest) {
643 Progress progress;
644 EXPECT_EQ(0, progress.Get());
645 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
646 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
647
648 bool max_increased = progress.Inc(1);
649 EXPECT_EQ(1, progress.Get());
650 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
651 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
652 EXPECT_FALSE(max_increased);
653
654 // Ignore negative increase.
655 max_increased = progress.Inc(-1);
656 EXPECT_EQ(1, progress.Get());
657 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
658 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
659 EXPECT_FALSE(max_increased);
660}
661
662TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
663 Progress progress = GetInstance(10, 1.2); // 20% growth factor
664 EXPECT_EQ(0, progress.Get());
665 EXPECT_EQ(10, progress.GetInitialMax());
666 EXPECT_EQ(10, progress.GetMax());
667
668 // No increase
669 bool max_increased = progress.Inc(10);
670 EXPECT_EQ(10, progress.Get());
671 EXPECT_EQ(10, progress.GetMax());
672 EXPECT_FALSE(max_increased);
673
674 // Increase, with new value < max*20%
675 max_increased = progress.Inc(1);
676 EXPECT_EQ(11, progress.Get());
677 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
678 EXPECT_TRUE(max_increased);
679}
680
681TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
682 Progress progress = GetInstance(10, 1.2); // 20% growth factor
683 EXPECT_EQ(0, progress.Get());
684 EXPECT_EQ(10, progress.GetInitialMax());
685 EXPECT_EQ(10, progress.GetMax());
686
687 // No increase
688 bool max_increased = progress.Inc(10);
689 EXPECT_EQ(10, progress.Get());
690 EXPECT_EQ(10, progress.GetMax());
691 EXPECT_FALSE(max_increased);
692
693 // Increase, with new value > max*20%
694 max_increased = progress.Inc(5);
695 EXPECT_EQ(15, progress.Get());
696 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
697 EXPECT_TRUE(max_increased);
698}
699
700TEST_F(ProgressTest, InvalidPath) {
701 Progress progress("/devil/null");
702 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
703}
704
705TEST_F(ProgressTest, EmptyFile) {
706 Progress progress(CopyTextFileFixture("empty-file.txt"));
707 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
708}
709
710TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
711 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
712 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
713}
714
715TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
716 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
717 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
718}
719
720TEST_F(ProgressTest, InvalidLineBothNAN) {
721 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
722 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
723}
724
725TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
726 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
727 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
728}
729
730TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
731 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
732 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
733}
734
735TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
736 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
737 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
738}
739
740TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
741 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
742 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
743}
744
745// Tests stats are properly saved when the file does not exists.
746TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800747 if (!IsStandalone()) {
748 // TODO: temporarily disabled because it's failing when running as suite
749 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
750 return;
751 }
752
Felipe Leme7447d7c2016-11-03 18:12:22 -0700753 std::string path = kTestDataPath + "FirstTime.txt";
754 android::base::RemoveFileIfExists(path);
755
756 Progress run1(path);
757 EXPECT_EQ(0, run1.Get());
758 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
759 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
760
761 bool max_increased = run1.Inc(20);
762 EXPECT_EQ(20, run1.Get());
763 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
764 EXPECT_FALSE(max_increased);
765
766 run1.Save();
767 AssertStats(path, 1, 20);
768}
769
770// Tests what happens when the persistent settings contains the average duration of 1 run.
771// Data on file is 1 run and 109 average.
772TEST_F(ProgressTest, SecondTime) {
773 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
774
775 Progress run1 = GetInstance(-42, 1.2, path);
776 EXPECT_EQ(0, run1.Get());
777 EXPECT_EQ(10, run1.GetInitialMax());
778 EXPECT_EQ(10, run1.GetMax());
779
780 bool max_increased = run1.Inc(20);
781 EXPECT_EQ(20, run1.Get());
782 EXPECT_EQ(24, run1.GetMax());
783 EXPECT_TRUE(max_increased);
784
785 // Average now is 2 runs and (10 + 20)/ 2 = 15
786 run1.Save();
787 AssertStats(path, 2, 15);
788
789 Progress run2 = GetInstance(-42, 1.2, path);
790 EXPECT_EQ(0, run2.Get());
791 EXPECT_EQ(15, run2.GetInitialMax());
792 EXPECT_EQ(15, run2.GetMax());
793
794 max_increased = run2.Inc(25);
795 EXPECT_EQ(25, run2.Get());
796 EXPECT_EQ(30, run2.GetMax());
797 EXPECT_TRUE(max_increased);
798
799 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
800 run2.Save();
801 AssertStats(path, 3, 18);
802
803 Progress run3 = GetInstance(-42, 1.2, path);
804 EXPECT_EQ(0, run3.Get());
805 EXPECT_EQ(18, run3.GetInitialMax());
806 EXPECT_EQ(18, run3.GetMax());
807
808 // Make sure average decreases as well
809 max_increased = run3.Inc(5);
810 EXPECT_EQ(5, run3.Get());
811 EXPECT_EQ(18, run3.GetMax());
812 EXPECT_FALSE(max_increased);
813
814 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
815 run3.Save();
816 AssertStats(path, 4, 14);
817}
818
819// Tests what happens when the persistent settings contains the average duration of 2 runs.
820// Data on file is 2 runs and 15 average.
821TEST_F(ProgressTest, ThirdTime) {
822 std::string path = CopyTextFileFixture("stats-two-runs.txt");
823 AssertStats(path, 2, 15); // Sanity check
824
825 Progress run1 = GetInstance(-42, 1.2, path);
826 EXPECT_EQ(0, run1.Get());
827 EXPECT_EQ(15, run1.GetInitialMax());
828 EXPECT_EQ(15, run1.GetMax());
829
830 bool max_increased = run1.Inc(20);
831 EXPECT_EQ(20, run1.Get());
832 EXPECT_EQ(24, run1.GetMax());
833 EXPECT_TRUE(max_increased);
834
835 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
836 run1.Save();
837 AssertStats(path, 3, 16);
838}
839
Felipe Leme46b85da2016-11-21 17:40:45 -0800840class DumpstateUtilTest : public DumpstateBaseTest {
841 public:
842 void SetUp() {
843 DumpstateBaseTest::SetUp();
844 SetDryRun(false);
845 }
846
Felipe Leme46b85da2016-11-21 17:40:45 -0800847 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -0800848 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -0800849 }
850
851 void CreateFd(const std::string& name) {
852 path_ = kTestDataPath + name;
853 MYLOGD("Creating fd for file %s\n", path_.c_str());
854
855 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
856 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
857 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
858 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
859 }
860
861 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -0800862 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -0800863 const CommandOptions& options = CommandOptions::DEFAULT) {
864 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -0800865 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -0800866 close(fd);
867
868 CaptureFdOut();
869 err = GetCapturedStderr();
870 return status;
871 }
872
873 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -0800874 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800875 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -0800876 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -0800877 close(fd);
878
879 CaptureFdOut();
880 err = GetCapturedStderr();
881 return status;
882 }
883
Ecco Park61ffcf72016-10-27 15:46:26 -0700884 // Find out the pid of the process_name
885 int FindPidOfProcess(const std::string& process_name) {
886 CaptureStderr();
887 int status = GetPidByName(process_name);
888 err = GetCapturedStderr();
889 return status;
890 }
891
Felipe Leme46b85da2016-11-21 17:40:45 -0800892 int fd;
893
894 // 'fd` output and `stderr` from the last command ran.
895 std::string out, err;
896
897 private:
898 std::string path_;
899};
900
901TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -0800902 CreateFd("RunCommandNoArgs.txt");
903 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800904}
905
Felipe Lemef0292972016-11-22 13:57:05 -0800906TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800907 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800908 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800909 EXPECT_THAT(out, StrEq("stdout\n"));
910 EXPECT_THAT(err, StrEq("stderr\n"));
911}
912
Felipe Lemef0292972016-11-22 13:57:05 -0800913TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
914 CreateFd("RunCommandWithNoArgs.txt");
915 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
916 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
917 EXPECT_THAT(err, StrEq("stderr\n"));
918}
919
Felipe Leme46b85da2016-11-21 17:40:45 -0800920TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
921 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800922 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800923 EXPECT_THAT(err, IsEmpty());
924 EXPECT_THAT(out, StrEq("one\n"));
925}
926
927TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
928 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800929 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800930 EXPECT_THAT(err, IsEmpty());
931 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
932}
933
934TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
935 CreateFd("RunCommandWithLoggingMessage.txt");
936 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -0800937 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -0800938 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
939 EXPECT_THAT(out, StrEq("stdout\n"));
940 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
941}
942
943TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
944 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800945 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
946 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -0800947 EXPECT_THAT(out, IsEmpty());
948 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
949}
950
951TEST_F(DumpstateUtilTest, RunCommandDryRun) {
952 CreateFd("RunCommandDryRun.txt");
953 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -0800954 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
955 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
956 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
957 kSimpleCommand.c_str())));
958 EXPECT_THAT(err, IsEmpty());
959}
960
961TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
962 CreateFd("RunCommandDryRun.txt");
963 SetDryRun(true);
964 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800965 EXPECT_THAT(
966 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
967 EXPECT_THAT(err, IsEmpty());
968}
969
970TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
971 CreateFd("RunCommandDryRunAlways.txt");
972 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -0800973 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -0800974 EXPECT_THAT(out, StrEq("stdout\n"));
975 EXPECT_THAT(err, StrEq("stderr\n"));
976}
977
978TEST_F(DumpstateUtilTest, RunCommandNotFound) {
979 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800980 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800981 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
982 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
983}
984
985TEST_F(DumpstateUtilTest, RunCommandFails) {
986 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800987 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800988 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
989 " --exit 42' failed: exit code 42\n"));
990 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
991 " --exit 42' failed: exit code 42\n"));
992}
993
994TEST_F(DumpstateUtilTest, RunCommandCrashes) {
995 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800996 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800997 // We don't know the exit code, so check just the prefix.
998 EXPECT_THAT(
999 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1000 EXPECT_THAT(
1001 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1002}
1003
Vishnu Nair6921f802017-11-22 09:17:23 -08001004TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001005 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001006 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1007 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001008 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1009 " --sleep 2' timed out after 1"));
1010 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1011 " --sleep 2' timed out after 1"));
1012}
1013
Vishnu Nair6921f802017-11-22 09:17:23 -08001014TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1015 CreateFd("RunCommandTimesout.txt");
1016 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1017 CommandOptions::WithTimeoutInMs(1000).Build()));
1018 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1019 " --sleep 2' timed out after 1"));
1020 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1021 " --sleep 2' timed out after 1"));
1022}
1023
1024
Felipe Leme46b85da2016-11-21 17:40:45 -08001025TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1026 CreateFd("RunCommandIsKilled.txt");
1027 CaptureStderr();
1028
1029 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001030 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001031 CommandOptions::WithTimeout(100).Always().Build()));
1032 });
1033
1034 // Capture pid and pre-sleep output.
1035 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1036 std::string err = GetCapturedStderr();
1037 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1038
1039 CaptureFdOut();
1040 std::vector<std::string> lines = android::base::Split(out, "\n");
1041 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1042
1043 int pid = atoi(lines[0].c_str());
1044 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1045 EXPECT_THAT(lines[2], IsEmpty()); // \n
1046
1047 // Then kill the process.
1048 CaptureFdOut();
1049 CaptureStderr();
1050 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1051 t.join();
1052
1053 // Finally, check output after murder.
1054 CaptureFdOut();
1055 err = GetCapturedStderr();
1056
1057 // out starts with the pid, which is an unknown
1058 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1059 " --pid --sleep 20' failed: killed by signal 15\n"));
1060 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1061 " --pid --sleep 20' failed: killed by signal 15\n"));
1062}
1063
1064TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1065 if (!IsStandalone()) {
1066 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1067 // to Shell - need to refactor tests to avoid this problem)
1068 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1069 return;
1070 }
1071 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001072 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001073 // Emulates user build if necessarily.
1074 SetBuildType("user");
1075 }
1076
1077 DropRoot();
1078
Felipe Lemef0292972016-11-22 13:57:05 -08001079 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001080
1081 // We don't know the exact path of su, so we just check for the 'root ...' commands
1082 EXPECT_THAT(out, StartsWith("Skipping"));
1083 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1084 EXPECT_THAT(err, IsEmpty());
1085}
1086
1087TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1088 if (!IsStandalone()) {
1089 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1090 // to Shell - need to refactor tests to avoid this problem)
1091 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1092 return;
1093 }
1094 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001095 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001096 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1097 return;
1098 }
1099
1100 DropRoot();
1101
Felipe Lemef0292972016-11-22 13:57:05 -08001102 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1103 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001104
1105 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1106 EXPECT_THAT(err, StrEq("stderr\n"));
1107}
Felipe Leme46b85da2016-11-21 17:40:45 -08001108
Yifan Hong48e83a12017-10-03 14:10:07 -07001109
1110TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1111 if (!IsStandalone()) {
1112 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1113 // to Shell - need to refactor tests to avoid this problem)
1114 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1115 return;
1116 }
1117 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1118 if (!PropertiesHelper::IsUserBuild()) {
1119 // Emulates user build if necessarily.
1120 SetBuildType("user");
1121 }
1122
1123 DropRoot();
1124
1125 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1126 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1127
1128 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1129 EXPECT_THAT(err, StrEq("stderr\n"));
1130}
1131
1132TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1133 if (!IsStandalone()) {
1134 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1135 // to Shell - need to refactor tests to avoid this problem)
1136 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1137 return;
1138 }
1139 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1140 if (PropertiesHelper::IsUserBuild()) {
1141 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1142 return;
1143 }
1144
1145 DropRoot();
1146
1147 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1148 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1149
1150 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1151 EXPECT_THAT(err, StrEq("stderr\n"));
1152}
1153
Felipe Leme46b85da2016-11-21 17:40:45 -08001154TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1155 if (!IsStandalone()) {
1156 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1157 // to Shell - need to refactor tests to avoid this problem)
1158 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1159 return;
1160 }
1161 CreateFd("RunCommandDropRoot.txt");
1162 // First check root case - only available when running with 'adb root'.
1163 uid_t uid = getuid();
1164 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001165 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001166 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1167 EXPECT_THAT(err, StrEq("stderr\n"));
1168 return;
1169 }
1170 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001171 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001172 CommandOptions::WithTimeout(1).DropRoot().Build()));
1173 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1174 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1175}
1176
Felipe Lemef0292972016-11-22 13:57:05 -08001177TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001178 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001179 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001180 EXPECT_THAT(out,
1181 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1182 EXPECT_THAT(err, IsEmpty());
1183}
1184
Felipe Lemef0292972016-11-22 13:57:05 -08001185TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1186 CreateFd("DumpFileNotFound.txt");
1187 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1188 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1189 "file or directory\n"));
1190 EXPECT_THAT(err, IsEmpty());
1191}
1192
Felipe Leme46b85da2016-11-21 17:40:45 -08001193TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1194 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001195 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001196 EXPECT_THAT(err, IsEmpty());
1197 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1198}
1199
1200TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1201 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001202 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001203 EXPECT_THAT(err, IsEmpty());
1204 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1205}
1206
1207TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1208 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001209 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001210 EXPECT_THAT(err, IsEmpty());
1211 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1212}
1213
1214TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1215 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001216 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001217 EXPECT_THAT(err, IsEmpty());
1218 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1219}
1220
Felipe Lemef0292972016-11-22 13:57:05 -08001221TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1222 CreateFd("DumpFileOnDryRun.txt");
1223 SetDryRun(true);
1224 std::string path = kTestDataPath + "single-line.txt";
1225 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1226 EXPECT_THAT(err, IsEmpty());
1227 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1228}
1229
Felipe Leme46b85da2016-11-21 17:40:45 -08001230TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1231 CreateFd("DumpFileOnDryRun.txt");
1232 SetDryRun(true);
1233 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001234 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001235 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001236 EXPECT_THAT(
1237 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1238 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001239}
Ecco Park61ffcf72016-10-27 15:46:26 -07001240
1241TEST_F(DumpstateUtilTest, FindingPidWithExistingProcess) {
1242 // init process always has pid 1.
1243 EXPECT_EQ(1, FindPidOfProcess("init"));
1244 EXPECT_THAT(err, IsEmpty());
1245}
1246
1247TEST_F(DumpstateUtilTest, FindingPidWithNotExistingProcess) {
1248 // find the process with abnormal name.
1249 EXPECT_EQ(-1, FindPidOfProcess("abcdef12345-543"));
1250 EXPECT_THAT(err, StrEq("can't find the pid\n"));
1251}
Felipe Leme47e9be22016-12-21 15:37:07 -08001252
1253} // namespace dumpstate
1254} // namespace os
1255} // namespace android