blob: 838b385b1b6b97c7ff73936f8cb6e2f715a9c910 [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));
Vishnu Nair20cf5032018-01-05 13:15:49 -080061 MOCK_METHOD4(onSectionComplete, binder::Status(const ::std::string& name, int32_t status,
62 int32_t size, int32_t durationMs));
Felipe Leme75876a22016-10-27 16:31:27 -070063
64 protected:
65 MOCK_METHOD0(onAsBinder, IBinder*());
66};
67
Felipe Leme46b85da2016-11-21 17:40:45 -080068static int calls_;
69
Felipe Leme7447d7c2016-11-03 18:12:22 -070070// Base class for all tests in this file
71class DumpstateBaseTest : public Test {
Felipe Leme46b85da2016-11-21 17:40:45 -080072 public:
73 virtual void SetUp() override {
74 calls_++;
Felipe Lemef0292972016-11-22 13:57:05 -080075 SetDryRun(false);
Felipe Leme46b85da2016-11-21 17:40:45 -080076 }
77
Felipe Lemef0292972016-11-22 13:57:05 -080078 void SetDryRun(bool dry_run) const {
79 PropertiesHelper::dry_run_ = dry_run;
80 }
81
82 void SetBuildType(const std::string& build_type) const {
83 PropertiesHelper::build_type_ = build_type;
84 }
85
86 bool IsStandalone() const {
Felipe Leme46b85da2016-11-21 17:40:45 -080087 return calls_ == 1;
88 }
89
Felipe Lemef0292972016-11-22 13:57:05 -080090 void DropRoot() const {
91 DropRootUser();
Felipe Leme46b85da2016-11-21 17:40:45 -080092 uid_t uid = getuid();
93 ASSERT_EQ(2000, (int)uid);
94 }
95
Felipe Leme7447d7c2016-11-03 18:12:22 -070096 protected:
97 const std::string kTestPath = dirname(android::base::GetExecutablePath().c_str());
98 const std::string kFixturesPath = kTestPath + "/../dumpstate_test_fixture/";
Felipe Leme7fb8dee2017-08-25 10:15:01 -070099 const std::string kTestDataPath = kFixturesPath + "tests/testdata/";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700100 const std::string kSimpleCommand = kFixturesPath + "dumpstate_test_fixture";
101 const std::string kEchoCommand = "/system/bin/echo";
102
103 /*
104 * Copies a text file fixture to a temporary file, returning it's path.
105 *
106 * Useful in cases where the test case changes the content of the tile.
107 */
108 std::string CopyTextFileFixture(const std::string& relative_name) {
109 std::string from = kTestDataPath + relative_name;
110 // Not using TemporaryFile because it's deleted at the end, and it's useful to keep it
111 // around for poking when the test fails.
112 std::string to = kTestDataPath + relative_name + ".tmp";
113 ALOGD("CopyTextFileFixture: from %s to %s\n", from.c_str(), to.c_str());
114 android::base::RemoveFileIfExists(to);
115 CopyTextFile(from, to);
116 return to.c_str();
117 }
118
Felipe Leme46b85da2016-11-21 17:40:45 -0800119 // Need functions that returns void to use assertions -
Felipe Leme7447d7c2016-11-03 18:12:22 -0700120 // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#assertion-placement
Felipe Leme46b85da2016-11-21 17:40:45 -0800121 void ReadFileToString(const std::string& path, std::string* content) {
122 ASSERT_TRUE(android::base::ReadFileToString(path, content))
123 << "could not read contents from " << path;
124 }
125 void WriteStringToFile(const std::string& content, const std::string& path) {
126 ASSERT_TRUE(android::base::WriteStringToFile(content, path))
127 << "could not write contents to " << path;
128 }
129
130 private:
Felipe Leme7447d7c2016-11-03 18:12:22 -0700131 void CopyTextFile(const std::string& from, const std::string& to) {
132 std::string content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800133 ReadFileToString(from, &content);
134 WriteStringToFile(content, to);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700135 }
136};
137
138class DumpstateTest : public DumpstateBaseTest {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700139 public:
140 void SetUp() {
Felipe Leme46b85da2016-11-21 17:40:45 -0800141 DumpstateBaseTest::SetUp();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700142 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700143 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700144 ds.progress_.reset(new Progress());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700145 ds.update_progress_ = false;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800146 ds.update_progress_threshold_ = 0;
Felipe Leme4c2d6632016-09-28 14:32:00 -0700147 }
148
149 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700150 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -0700151 const CommandOptions& options = CommandOptions::DEFAULT) {
152 CaptureStdout();
153 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -0700154 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -0700155 out = GetCapturedStdout();
156 err = GetCapturedStderr();
157 return status;
158 }
159
Felipe Lemecef02982016-10-03 17:22:22 -0700160 // Dumps a file and capture `stdout` and `stderr`.
161 int DumpFile(const std::string& title, const std::string& path) {
162 CaptureStdout();
163 CaptureStderr();
164 int status = ds.DumpFile(title, path);
165 out = GetCapturedStdout();
166 err = GetCapturedStderr();
167 return status;
168 }
169
Felipe Leme009ecbb2016-11-07 10:18:44 -0800170 void SetProgress(long progress, long initial_max, long threshold = 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700171 ds.update_progress_ = true;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800172 ds.update_progress_threshold_ = threshold;
173 ds.last_updated_progress_ = 0;
Felipe Leme7447d7c2016-11-03 18:12:22 -0700174 ds.progress_.reset(new Progress(initial_max, progress, 1.2));
175 }
176
Felipe Leme7447d7c2016-11-03 18:12:22 -0700177 std::string GetProgressMessage(const std::string& listener_name, int progress, int max,
Felipe Leme009ecbb2016-11-07 10:18:44 -0800178 int old_max = 0, bool update_progress = true) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700179 EXPECT_EQ(progress, ds.progress_->Get()) << "invalid progress";
180 EXPECT_EQ(max, ds.progress_->GetMax()) << "invalid max";
Felipe Leme75876a22016-10-27 16:31:27 -0700181
Felipe Leme7447d7c2016-11-03 18:12:22 -0700182 bool max_increased = old_max > 0;
Felipe Leme75876a22016-10-27 16:31:27 -0700183
Felipe Leme009ecbb2016-11-07 10:18:44 -0800184 std::string message = "";
Felipe Leme75876a22016-10-27 16:31:27 -0700185 if (max_increased) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800186 message =
Felipe Leme7447d7c2016-11-03 18:12:22 -0700187 android::base::StringPrintf("Adjusting max progress from %d to %d\n", old_max, max);
Felipe Leme75876a22016-10-27 16:31:27 -0700188 }
189
Felipe Leme009ecbb2016-11-07 10:18:44 -0800190 if (update_progress) {
191 message += android::base::StringPrintf("Setting progress (%s): %d/%d\n",
192 listener_name.c_str(), progress, max);
193 }
194
195 return message;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700196 }
197
Felipe Leme4c2d6632016-09-28 14:32:00 -0700198 // `stdout` and `stderr` from the last command ran.
199 std::string out, err;
200
Felipe Lemefd8affa2016-09-30 17:38:57 -0700201 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700202};
203
204TEST_F(DumpstateTest, RunCommandNoArgs) {
205 EXPECT_EQ(-1, RunCommand("", {}));
206}
207
208TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700209 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700210 EXPECT_THAT(out, StrEq("stdout\n"));
211 EXPECT_THAT(err, StrEq("stderr\n"));
212}
213
214TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700215 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700216 EXPECT_THAT(err, StrEq("stderr\n"));
217 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700218 EXPECT_THAT(out,
Felipe Leme7447d7c2016-11-03 18:12:22 -0700219 StartsWith("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700220 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
221}
222
Felipe Lemefd8affa2016-09-30 17:38:57 -0700223TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700224 EXPECT_EQ(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700225 0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700226 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
227 EXPECT_THAT(out, StrEq("stdout\n"));
228 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
229}
230
231TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700232 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700233 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700234 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700235 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700236}
237
238TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700239 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700240 EXPECT_THAT(err, IsEmpty());
241 EXPECT_THAT(out, StrEq("one\n"));
242}
243
Felipe Lemefd8affa2016-09-30 17:38:57 -0700244TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700245 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700246 EXPECT_THAT(err, IsEmpty());
247 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
248}
249
250TEST_F(DumpstateTest, RunCommandDryRun) {
251 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700252 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700253 // We don't know the exact duration, so we check the prefix and suffix
Felipe Leme7447d7c2016-11-03 18:12:22 -0700254 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + kSimpleCommand +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700255 ") ------\n\t(skipped on dry run)\n------"));
256 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
257 EXPECT_THAT(err, IsEmpty());
258}
259
260TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
261 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700262 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700263 EXPECT_THAT(out, IsEmpty());
264 EXPECT_THAT(err, IsEmpty());
265}
266
267TEST_F(DumpstateTest, RunCommandDryRunAlways) {
268 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700269 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700270 EXPECT_THAT(out, StrEq("stdout\n"));
271 EXPECT_THAT(err, StrEq("stderr\n"));
272}
273
Felipe Lemefd8affa2016-09-30 17:38:57 -0700274TEST_F(DumpstateTest, RunCommandNotFound) {
275 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
276 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
277 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
278}
279
280TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700281 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
282 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700283 " --exit 42' failed: exit code 42\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700284 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
Felipe Leme9a523ae2016-10-20 15:10:33 -0700285 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700286}
287
288TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700289 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700290 // We don't know the exit code, so check just the prefix.
291 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700292 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700293 EXPECT_THAT(
Felipe Leme7447d7c2016-11-03 18:12:22 -0700294 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700295}
296
297TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700298 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700299 CommandOptions::WithTimeout(1).Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700300 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700301 " --sleep 2' timed out after 1"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700302 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700303 " --sleep 2' timed out after 1"));
304}
305
306TEST_F(DumpstateTest, RunCommandIsKilled) {
307 CaptureStdout();
308 CaptureStderr();
309
310 std::thread t([=]() {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700311 EXPECT_EQ(SIGTERM, ds.RunCommand("", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700312 CommandOptions::WithTimeout(100).Always().Build()));
313 });
314
315 // Capture pid and pre-sleep output.
316 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
317 std::string err = GetCapturedStderr();
318 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
319
320 std::string out = GetCapturedStdout();
321 std::vector<std::string> lines = android::base::Split(out, "\n");
322 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
323
324 int pid = atoi(lines[0].c_str());
325 EXPECT_THAT(lines[1], StrEq("stdout line1"));
326 EXPECT_THAT(lines[2], IsEmpty()); // \n
327
328 // Then kill the process.
329 CaptureStdout();
330 CaptureStderr();
331 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
332 t.join();
333
334 // Finally, check output after murder.
335 out = GetCapturedStdout();
336 err = GetCapturedStderr();
337
Felipe Leme7447d7c2016-11-03 18:12:22 -0700338 EXPECT_THAT(out, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700339 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700340 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700341 " --pid --sleep 20' failed: killed by signal 15\n"));
342}
343
Felipe Leme75876a22016-10-27 16:31:27 -0700344TEST_F(DumpstateTest, RunCommandProgress) {
345 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
346 ds.listener_ = listener;
347 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700348 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700349
350 EXPECT_CALL(*listener, onProgressUpdated(20));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700351 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(20).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700352 std::string progress_message = GetProgressMessage(ds.listener_name_, 20, 30);
353 EXPECT_THAT(out, StrEq("stdout\n"));
354 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
355
356 EXPECT_CALL(*listener, onProgressUpdated(30));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700357 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Build()));
Felipe Leme75876a22016-10-27 16:31:27 -0700358 progress_message = GetProgressMessage(ds.listener_name_, 30, 30);
359 EXPECT_THAT(out, StrEq("stdout\n"));
360 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
361
362 // Run a command that will increase maximum timeout.
363 EXPECT_CALL(*listener, onProgressUpdated(31));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700364 EXPECT_CALL(*listener, onMaxProgressUpdated(37));
365 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
366 progress_message = GetProgressMessage(ds.listener_name_, 31, 37, 30); // 20% increase
Felipe Leme75876a22016-10-27 16:31:27 -0700367 EXPECT_THAT(out, StrEq("stdout\n"));
368 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
369
370 // Make sure command ran while in dry_run is counted.
371 SetDryRun(true);
372 EXPECT_CALL(*listener, onProgressUpdated(35));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700373 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
374 progress_message = GetProgressMessage(ds.listener_name_, 35, 37);
Felipe Leme75876a22016-10-27 16:31:27 -0700375 EXPECT_THAT(out, IsEmpty());
376 EXPECT_THAT(err, StrEq(progress_message));
377
378 ds.listener_.clear();
379}
380
Felipe Leme009ecbb2016-11-07 10:18:44 -0800381TEST_F(DumpstateTest, RunCommandProgressIgnoreThreshold) {
382 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
383 ds.listener_ = listener;
384 ds.listener_name_ = "FoxMulder";
385 SetProgress(0, 8, 5); // 8 max, 5 threshold
386
387 // First update should always be sent.
388 EXPECT_CALL(*listener, onProgressUpdated(1));
389 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
390 std::string progress_message = GetProgressMessage(ds.listener_name_, 1, 8);
391 EXPECT_THAT(out, StrEq("stdout\n"));
392 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
393
394 // Fourth update should be ignored because it's between the threshold (5 -1 = 4 < 5).
395 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(4).Build()));
396 EXPECT_THAT(out, StrEq("stdout\n"));
397 EXPECT_THAT(err, StrEq("stderr\n"));
398
399 // Third update should be sent because it reaches threshold (6 - 1 = 5).
400 EXPECT_CALL(*listener, onProgressUpdated(6));
401 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).Build()));
402 progress_message = GetProgressMessage(ds.listener_name_, 6, 8);
403 EXPECT_THAT(out, StrEq("stdout\n"));
404 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
405
406 // Fourth update should be ignored because it's between the threshold (9 - 6 = 3 < 5).
407 // But max update should be sent.
408 EXPECT_CALL(*listener, onMaxProgressUpdated(10)); // 9 * 120% = 10.8 = 10
409 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(3).Build()));
410 progress_message = GetProgressMessage(ds.listener_name_, 9, 10, 8, false);
411 EXPECT_THAT(out, StrEq("stdout\n"));
412 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
413
414 ds.listener_.clear();
415}
416
Felipe Lemed80e6b62016-10-03 13:08:14 -0700417TEST_F(DumpstateTest, RunCommandDropRoot) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800418 if (!IsStandalone()) {
419 // TODO: temporarily disabled because it might cause other tests to fail after dropping
420 // to Shell - need to refactor tests to avoid this problem)
421 MYLOGE("Skipping DumpstateTest.RunCommandDropRoot() on test suite\n")
422 return;
423 }
Felipe Lemed80e6b62016-10-03 13:08:14 -0700424 // First check root case - only available when running with 'adb root'.
425 uid_t uid = getuid();
426 if (uid == 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700427 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700428 EXPECT_THAT(out, StrEq("0\nstdout\n"));
429 EXPECT_THAT(err, StrEq("stderr\n"));
430 return;
431 }
Felipe Leme7447d7c2016-11-03 18:12:22 -0700432 // Then run dropping root.
433 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700434 CommandOptions::WithTimeout(1).DropRoot().Build()));
435 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700436 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700437}
438
439TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800440 if (!IsStandalone()) {
441 // TODO: temporarily disabled because it might cause other tests to fail after dropping
442 // to Shell - need to refactor tests to avoid this problem)
443 MYLOGE("Skipping DumpstateTest.RunCommandAsRootUserBuild() on test suite\n")
444 return;
445 }
Felipe Lemef0292972016-11-22 13:57:05 -0800446 if (!PropertiesHelper::IsUserBuild()) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700447 // Emulates user build if necessarily.
448 SetBuildType("user");
449 }
450
451 DropRoot();
452
Felipe Leme7447d7c2016-11-03 18:12:22 -0700453 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700454
455 // We don't know the exact path of su, so we just check for the 'root ...' commands
456 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700457 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700458 EXPECT_THAT(err, IsEmpty());
459}
460
Felipe Leme46b85da2016-11-21 17:40:45 -0800461TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
462 if (!IsStandalone()) {
463 // TODO: temporarily disabled because it might cause other tests to fail after dropping
464 // to Shell - need to refactor tests to avoid this problem)
465 MYLOGE("Skipping DumpstateTest.RunCommandAsRootNonUserBuild() on test suite\n")
466 return;
467 }
Felipe Lemef0292972016-11-22 13:57:05 -0800468 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800469 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
470 return;
471 }
472
473 DropRoot();
474
475 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
476 CommandOptions::WithTimeout(1).AsRoot().Build()));
477
478 EXPECT_THAT(out, StrEq("0\nstdout\n"));
479 EXPECT_THAT(err, StrEq("stderr\n"));
480}
481
Yifan Hong48e83a12017-10-03 14:10:07 -0700482TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnUserBuild) {
483 if (!IsStandalone()) {
484 // TODO: temporarily disabled because it might cause other tests to fail after dropping
485 // to Shell - need to refactor tests to avoid this problem)
486 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
487 return;
488 }
489 if (!PropertiesHelper::IsUserBuild()) {
490 // Emulates user build if necessarily.
491 SetBuildType("user");
492 }
493
494 DropRoot();
495
496 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
497 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
498
499 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
500 EXPECT_THAT(err, StrEq("stderr\n"));
501}
502
503TEST_F(DumpstateTest, RunCommandAsRootIfAvailableOnDebugBuild) {
504 if (!IsStandalone()) {
505 // TODO: temporarily disabled because it might cause other tests to fail after dropping
506 // to Shell - need to refactor tests to avoid this problem)
507 MYLOGE("Skipping DumpstateTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
508 return;
509 }
510 if (PropertiesHelper::IsUserBuild()) {
511 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
512 return;
513 }
514
515 DropRoot();
516
517 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
518 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
519
520 EXPECT_THAT(out, StrEq("0\nstdout\n"));
521 EXPECT_THAT(err, StrEq("stderr\n"));
522}
523
Felipe Lemecef02982016-10-03 17:22:22 -0700524TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
525 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
526 EXPECT_THAT(out,
527 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
528 EXPECT_THAT(err, IsEmpty());
529}
530
531TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
532 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
533 EXPECT_THAT(err, IsEmpty());
534 // We don't know the exact duration, so we check the prefix and suffix
535 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
536 "such file or directory\n"));
537 EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n"));
538}
539
540TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700541 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700542 EXPECT_THAT(err, IsEmpty());
543 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
544}
545
546TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700547 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700548 EXPECT_THAT(err, IsEmpty());
549 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
550}
551
552TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700553 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700554 EXPECT_THAT(err, IsEmpty());
555 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
556}
557
558TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700559 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700560 EXPECT_THAT(err, IsEmpty());
561 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
562}
563
564TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
565 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700566 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700567 EXPECT_THAT(err, IsEmpty());
568 EXPECT_THAT(out, IsEmpty());
569}
570
571TEST_F(DumpstateTest, DumpFileOnDryRun) {
572 SetDryRun(true);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700573 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700574 EXPECT_THAT(err, IsEmpty());
Felipe Leme46b85da2016-11-21 17:40:45 -0800575 EXPECT_THAT(
576 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
577 EXPECT_THAT(out, HasSubstr("\n\t(skipped on dry run)\n------"));
Felipe Lemecef02982016-10-03 17:22:22 -0700578 EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n"));
Felipe Lemecef02982016-10-03 17:22:22 -0700579}
580
Felipe Leme75876a22016-10-27 16:31:27 -0700581TEST_F(DumpstateTest, DumpFileUpdateProgress) {
582 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
583 ds.listener_ = listener;
584 ds.listener_name_ = "FoxMulder";
Felipe Leme7447d7c2016-11-03 18:12:22 -0700585 SetProgress(0, 30);
Felipe Leme75876a22016-10-27 16:31:27 -0700586
587 EXPECT_CALL(*listener, onProgressUpdated(5));
Felipe Leme7447d7c2016-11-03 18:12:22 -0700588 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme75876a22016-10-27 16:31:27 -0700589
590 std::string progress_message =
591 GetProgressMessage(ds.listener_name_, 5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
592 EXPECT_THAT(err, StrEq(progress_message));
593 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
594
595 ds.listener_.clear();
596}
597
Felipe Leme7447d7c2016-11-03 18:12:22 -0700598class DumpstateServiceTest : public DumpstateBaseTest {
Felipe Leme75876a22016-10-27 16:31:27 -0700599 public:
600 DumpstateService dss;
601};
602
603TEST_F(DumpstateServiceTest, SetListenerNoName) {
604 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800605 sp<IDumpstateToken> token;
Vishnu Nair20cf5032018-01-05 13:15:49 -0800606 EXPECT_TRUE(dss.setListener("", listener, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800607 ASSERT_THAT(token, IsNull());
Felipe Leme75876a22016-10-27 16:31:27 -0700608}
609
610TEST_F(DumpstateServiceTest, SetListenerNoPointer) {
Felipe Leme009ecbb2016-11-07 10:18:44 -0800611 sp<IDumpstateToken> token;
Vishnu Nair20cf5032018-01-05 13:15:49 -0800612 EXPECT_TRUE(
613 dss.setListener("whatever", nullptr, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800614 ASSERT_THAT(token, IsNull());
Felipe Leme75876a22016-10-27 16:31:27 -0700615}
616
617TEST_F(DumpstateServiceTest, SetListenerTwice) {
618 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800619 sp<IDumpstateToken> token;
Vishnu Nair20cf5032018-01-05 13:15:49 -0800620 EXPECT_TRUE(
621 dss.setListener("whatever", listener, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800622 ASSERT_THAT(token, NotNull());
Felipe Leme75876a22016-10-27 16:31:27 -0700623 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
Vishnu Nair20cf5032018-01-05 13:15:49 -0800624 EXPECT_FALSE(Dumpstate::GetInstance().report_section_);
Felipe Leme75876a22016-10-27 16:31:27 -0700625
Felipe Leme009ecbb2016-11-07 10:18:44 -0800626 token.clear();
Vishnu Nair20cf5032018-01-05 13:15:49 -0800627 EXPECT_TRUE(
628 dss.setListener("whatsoever", listener, /* getSectionDetails = */ false, &token).isOk());
Felipe Leme009ecbb2016-11-07 10:18:44 -0800629 ASSERT_THAT(token, IsNull());
630 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
Vishnu Nair20cf5032018-01-05 13:15:49 -0800631 EXPECT_FALSE(Dumpstate::GetInstance().report_section_);
632}
633
634TEST_F(DumpstateServiceTest, SetListenerWithSectionDetails) {
635 sp<DumpstateListenerMock> listener(new DumpstateListenerMock());
636 sp<IDumpstateToken> token;
637 Dumpstate::GetInstance().listener_ = nullptr;
638 EXPECT_TRUE(
639 dss.setListener("whatever", listener, /* getSectionDetails = */ true, &token).isOk());
640 ASSERT_THAT(token, NotNull());
641 EXPECT_THAT(Dumpstate::GetInstance().listener_name_, StrEq("whatever"));
642 EXPECT_TRUE(Dumpstate::GetInstance().report_section_);
Felipe Leme75876a22016-10-27 16:31:27 -0700643}
Felipe Leme7447d7c2016-11-03 18:12:22 -0700644
645class ProgressTest : public DumpstateBaseTest {
646 public:
647 Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {
648 return Progress(max, growth_factor, path);
649 }
650
651 void AssertStats(const std::string& path, int32_t expected_runs, int32_t expected_average) {
652 std::string expected_content =
653 android::base::StringPrintf("%d %d\n", expected_runs, expected_average);
654 std::string actual_content;
Felipe Leme46b85da2016-11-21 17:40:45 -0800655 ReadFileToString(path, &actual_content);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700656 ASSERT_THAT(actual_content, StrEq(expected_content)) << "invalid stats on " << path;
657 }
658};
659
660TEST_F(ProgressTest, SimpleTest) {
661 Progress progress;
662 EXPECT_EQ(0, progress.Get());
663 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
664 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
665
666 bool max_increased = progress.Inc(1);
667 EXPECT_EQ(1, progress.Get());
668 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
669 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
670 EXPECT_FALSE(max_increased);
671
672 // Ignore negative increase.
673 max_increased = progress.Inc(-1);
674 EXPECT_EQ(1, progress.Get());
675 EXPECT_EQ(Progress::kDefaultMax, progress.GetInitialMax());
676 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
677 EXPECT_FALSE(max_increased);
678}
679
680TEST_F(ProgressTest, MaxGrowsInsideNewRange) {
681 Progress progress = GetInstance(10, 1.2); // 20% growth factor
682 EXPECT_EQ(0, progress.Get());
683 EXPECT_EQ(10, progress.GetInitialMax());
684 EXPECT_EQ(10, progress.GetMax());
685
686 // No increase
687 bool max_increased = progress.Inc(10);
688 EXPECT_EQ(10, progress.Get());
689 EXPECT_EQ(10, progress.GetMax());
690 EXPECT_FALSE(max_increased);
691
692 // Increase, with new value < max*20%
693 max_increased = progress.Inc(1);
694 EXPECT_EQ(11, progress.Get());
695 EXPECT_EQ(13, progress.GetMax()); // 11 average * 20% growth = 13.2 = 13
696 EXPECT_TRUE(max_increased);
697}
698
699TEST_F(ProgressTest, MaxGrowsOutsideNewRange) {
700 Progress progress = GetInstance(10, 1.2); // 20% growth factor
701 EXPECT_EQ(0, progress.Get());
702 EXPECT_EQ(10, progress.GetInitialMax());
703 EXPECT_EQ(10, progress.GetMax());
704
705 // No increase
706 bool max_increased = progress.Inc(10);
707 EXPECT_EQ(10, progress.Get());
708 EXPECT_EQ(10, progress.GetMax());
709 EXPECT_FALSE(max_increased);
710
711 // Increase, with new value > max*20%
712 max_increased = progress.Inc(5);
713 EXPECT_EQ(15, progress.Get());
714 EXPECT_EQ(18, progress.GetMax()); // 15 average * 20% growth = 18
715 EXPECT_TRUE(max_increased);
716}
717
718TEST_F(ProgressTest, InvalidPath) {
719 Progress progress("/devil/null");
720 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
721}
722
723TEST_F(ProgressTest, EmptyFile) {
724 Progress progress(CopyTextFileFixture("empty-file.txt"));
725 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
726}
727
728TEST_F(ProgressTest, InvalidLine1stEntryNAN) {
729 Progress progress(CopyTextFileFixture("stats-invalid-1st-NAN.txt"));
730 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
731}
732
733TEST_F(ProgressTest, InvalidLine2ndEntryNAN) {
734 Progress progress(CopyTextFileFixture("stats-invalid-2nd-NAN.txt"));
735 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
736}
737
738TEST_F(ProgressTest, InvalidLineBothNAN) {
739 Progress progress(CopyTextFileFixture("stats-invalid-both-NAN.txt"));
740 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
741}
742
743TEST_F(ProgressTest, InvalidLine1stEntryNegative) {
744 Progress progress(CopyTextFileFixture("stats-invalid-1st-negative.txt"));
745 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
746}
747
748TEST_F(ProgressTest, InvalidLine2ndEntryNegative) {
749 Progress progress(CopyTextFileFixture("stats-invalid-2nd-negative.txt"));
750 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
751}
752
753TEST_F(ProgressTest, InvalidLine1stEntryTooBig) {
754 Progress progress(CopyTextFileFixture("stats-invalid-1st-too-big.txt"));
755 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
756}
757
758TEST_F(ProgressTest, InvalidLine2ndEntryTooBig) {
759 Progress progress(CopyTextFileFixture("stats-invalid-2nd-too-big.txt"));
760 EXPECT_EQ(Progress::kDefaultMax, progress.GetMax());
761}
762
763// Tests stats are properly saved when the file does not exists.
764TEST_F(ProgressTest, FirstTime) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800765 if (!IsStandalone()) {
766 // TODO: temporarily disabled because it's failing when running as suite
767 MYLOGE("Skipping ProgressTest.FirstTime() on test suite\n")
768 return;
769 }
770
Felipe Leme7447d7c2016-11-03 18:12:22 -0700771 std::string path = kTestDataPath + "FirstTime.txt";
772 android::base::RemoveFileIfExists(path);
773
774 Progress run1(path);
775 EXPECT_EQ(0, run1.Get());
776 EXPECT_EQ(Progress::kDefaultMax, run1.GetInitialMax());
777 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
778
779 bool max_increased = run1.Inc(20);
780 EXPECT_EQ(20, run1.Get());
781 EXPECT_EQ(Progress::kDefaultMax, run1.GetMax());
782 EXPECT_FALSE(max_increased);
783
784 run1.Save();
785 AssertStats(path, 1, 20);
786}
787
788// Tests what happens when the persistent settings contains the average duration of 1 run.
789// Data on file is 1 run and 109 average.
790TEST_F(ProgressTest, SecondTime) {
791 std::string path = CopyTextFileFixture("stats-one-run-no-newline.txt");
792
793 Progress run1 = GetInstance(-42, 1.2, path);
794 EXPECT_EQ(0, run1.Get());
795 EXPECT_EQ(10, run1.GetInitialMax());
796 EXPECT_EQ(10, run1.GetMax());
797
798 bool max_increased = run1.Inc(20);
799 EXPECT_EQ(20, run1.Get());
800 EXPECT_EQ(24, run1.GetMax());
801 EXPECT_TRUE(max_increased);
802
803 // Average now is 2 runs and (10 + 20)/ 2 = 15
804 run1.Save();
805 AssertStats(path, 2, 15);
806
807 Progress run2 = GetInstance(-42, 1.2, path);
808 EXPECT_EQ(0, run2.Get());
809 EXPECT_EQ(15, run2.GetInitialMax());
810 EXPECT_EQ(15, run2.GetMax());
811
812 max_increased = run2.Inc(25);
813 EXPECT_EQ(25, run2.Get());
814 EXPECT_EQ(30, run2.GetMax());
815 EXPECT_TRUE(max_increased);
816
817 // Average now is 3 runs and (15 * 2 + 25)/ 3 = 18.33 = 18
818 run2.Save();
819 AssertStats(path, 3, 18);
820
821 Progress run3 = GetInstance(-42, 1.2, path);
822 EXPECT_EQ(0, run3.Get());
823 EXPECT_EQ(18, run3.GetInitialMax());
824 EXPECT_EQ(18, run3.GetMax());
825
826 // Make sure average decreases as well
827 max_increased = run3.Inc(5);
828 EXPECT_EQ(5, run3.Get());
829 EXPECT_EQ(18, run3.GetMax());
830 EXPECT_FALSE(max_increased);
831
832 // Average now is 4 runs and (18 * 3 + 5)/ 4 = 14.75 = 14
833 run3.Save();
834 AssertStats(path, 4, 14);
835}
836
837// Tests what happens when the persistent settings contains the average duration of 2 runs.
838// Data on file is 2 runs and 15 average.
839TEST_F(ProgressTest, ThirdTime) {
840 std::string path = CopyTextFileFixture("stats-two-runs.txt");
841 AssertStats(path, 2, 15); // Sanity check
842
843 Progress run1 = GetInstance(-42, 1.2, path);
844 EXPECT_EQ(0, run1.Get());
845 EXPECT_EQ(15, run1.GetInitialMax());
846 EXPECT_EQ(15, run1.GetMax());
847
848 bool max_increased = run1.Inc(20);
849 EXPECT_EQ(20, run1.Get());
850 EXPECT_EQ(24, run1.GetMax());
851 EXPECT_TRUE(max_increased);
852
853 // Average now is 3 runs and (15 * 2 + 20)/ 3 = 16.66 = 16
854 run1.Save();
855 AssertStats(path, 3, 16);
856}
857
Felipe Leme46b85da2016-11-21 17:40:45 -0800858class DumpstateUtilTest : public DumpstateBaseTest {
859 public:
860 void SetUp() {
861 DumpstateBaseTest::SetUp();
862 SetDryRun(false);
863 }
864
Felipe Leme46b85da2016-11-21 17:40:45 -0800865 void CaptureFdOut() {
Felipe Lemef0292972016-11-22 13:57:05 -0800866 ReadFileToString(path_, &out);
Felipe Leme46b85da2016-11-21 17:40:45 -0800867 }
868
869 void CreateFd(const std::string& name) {
870 path_ = kTestDataPath + name;
871 MYLOGD("Creating fd for file %s\n", path_.c_str());
872
873 fd = TEMP_FAILURE_RETRY(open(path_.c_str(),
874 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
875 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
876 ASSERT_GE(fd, 0) << "could not create FD for path " << path_;
877 }
878
879 // Runs a command into the `fd` and capture `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -0800880 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -0800881 const CommandOptions& options = CommandOptions::DEFAULT) {
882 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -0800883 int status = RunCommandToFd(fd, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -0800884 close(fd);
885
886 CaptureFdOut();
887 err = GetCapturedStderr();
888 return status;
889 }
890
891 // Dumps a file and into the `fd` and `stderr`.
Felipe Lemef0292972016-11-22 13:57:05 -0800892 int DumpFile(const std::string& title, const std::string& path) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800893 CaptureStderr();
Felipe Lemef0292972016-11-22 13:57:05 -0800894 int status = DumpFileToFd(fd, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -0800895 close(fd);
896
897 CaptureFdOut();
898 err = GetCapturedStderr();
899 return status;
900 }
901
Ecco Park61ffcf72016-10-27 15:46:26 -0700902 // Find out the pid of the process_name
903 int FindPidOfProcess(const std::string& process_name) {
904 CaptureStderr();
905 int status = GetPidByName(process_name);
906 err = GetCapturedStderr();
907 return status;
908 }
909
Felipe Leme46b85da2016-11-21 17:40:45 -0800910 int fd;
911
912 // 'fd` output and `stderr` from the last command ran.
913 std::string out, err;
914
915 private:
916 std::string path_;
917};
918
919TEST_F(DumpstateUtilTest, RunCommandNoArgs) {
Felipe Lemef0292972016-11-22 13:57:05 -0800920 CreateFd("RunCommandNoArgs.txt");
921 EXPECT_EQ(-1, RunCommand("", {}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800922}
923
Felipe Lemef0292972016-11-22 13:57:05 -0800924TEST_F(DumpstateUtilTest, RunCommandNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -0800925 CreateFd("RunCommandWithNoArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800926 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800927 EXPECT_THAT(out, StrEq("stdout\n"));
928 EXPECT_THAT(err, StrEq("stderr\n"));
929}
930
Felipe Lemef0292972016-11-22 13:57:05 -0800931TEST_F(DumpstateUtilTest, RunCommandWithTitle) {
932 CreateFd("RunCommandWithNoArgs.txt");
933 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
934 EXPECT_THAT(out, StrEq("------ I AM GROOT (" + kSimpleCommand + ") ------\nstdout\n"));
935 EXPECT_THAT(err, StrEq("stderr\n"));
936}
937
Felipe Leme46b85da2016-11-21 17:40:45 -0800938TEST_F(DumpstateUtilTest, RunCommandWithOneArg) {
939 CreateFd("RunCommandWithOneArg.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800940 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800941 EXPECT_THAT(err, IsEmpty());
942 EXPECT_THAT(out, StrEq("one\n"));
943}
944
945TEST_F(DumpstateUtilTest, RunCommandWithMultipleArgs) {
946 CreateFd("RunCommandWithMultipleArgs.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800947 EXPECT_EQ(0, RunCommand("", {kEchoCommand, "one", "is", "the", "loniest", "number"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800948 EXPECT_THAT(err, IsEmpty());
949 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
950}
951
952TEST_F(DumpstateUtilTest, RunCommandWithLoggingMessage) {
953 CreateFd("RunCommandWithLoggingMessage.txt");
954 EXPECT_EQ(
Felipe Lemef0292972016-11-22 13:57:05 -0800955 0, RunCommand("", {kSimpleCommand},
Felipe Leme46b85da2016-11-21 17:40:45 -0800956 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
957 EXPECT_THAT(out, StrEq("stdout\n"));
958 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
959}
960
961TEST_F(DumpstateUtilTest, RunCommandRedirectStderr) {
962 CreateFd("RunCommandRedirectStderr.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800963 EXPECT_EQ(0, RunCommand("", {kSimpleCommand},
964 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -0800965 EXPECT_THAT(out, IsEmpty());
966 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
967}
968
969TEST_F(DumpstateUtilTest, RunCommandDryRun) {
970 CreateFd("RunCommandDryRun.txt");
971 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -0800972 EXPECT_EQ(0, RunCommand("I AM GROOT", {kSimpleCommand}));
973 EXPECT_THAT(out, StrEq(android::base::StringPrintf(
974 "------ I AM GROOT (%s) ------\n\t(skipped on dry run)\n",
975 kSimpleCommand.c_str())));
976 EXPECT_THAT(err, IsEmpty());
977}
978
979TEST_F(DumpstateUtilTest, RunCommandDryRunNoTitle) {
980 CreateFd("RunCommandDryRun.txt");
981 SetDryRun(true);
982 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800983 EXPECT_THAT(
984 out, StrEq(android::base::StringPrintf("%s: skipped on dry run\n", kSimpleCommand.c_str())));
985 EXPECT_THAT(err, IsEmpty());
986}
987
988TEST_F(DumpstateUtilTest, RunCommandDryRunAlways) {
989 CreateFd("RunCommandDryRunAlways.txt");
990 SetDryRun(true);
Felipe Lemef0292972016-11-22 13:57:05 -0800991 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -0800992 EXPECT_THAT(out, StrEq("stdout\n"));
993 EXPECT_THAT(err, StrEq("stderr\n"));
994}
995
996TEST_F(DumpstateUtilTest, RunCommandNotFound) {
997 CreateFd("RunCommandNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -0800998 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
Felipe Leme46b85da2016-11-21 17:40:45 -0800999 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
1000 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
1001}
1002
1003TEST_F(DumpstateUtilTest, RunCommandFails) {
1004 CreateFd("RunCommandFails.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001005 EXPECT_EQ(42, RunCommand("", {kSimpleCommand, "--exit", "42"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001006 EXPECT_THAT(out, StrEq("stdout\n*** command '" + kSimpleCommand +
1007 " --exit 42' failed: exit code 42\n"));
1008 EXPECT_THAT(err, StrEq("stderr\n*** command '" + kSimpleCommand +
1009 " --exit 42' failed: exit code 42\n"));
1010}
1011
1012TEST_F(DumpstateUtilTest, RunCommandCrashes) {
1013 CreateFd("RunCommandCrashes.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001014 EXPECT_NE(0, RunCommand("", {kSimpleCommand, "--crash"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001015 // We don't know the exit code, so check just the prefix.
1016 EXPECT_THAT(
1017 out, StartsWith("stdout\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1018 EXPECT_THAT(
1019 err, StartsWith("stderr\n*** command '" + kSimpleCommand + " --crash' failed: exit code"));
1020}
1021
Vishnu Nair6921f802017-11-22 09:17:23 -08001022TEST_F(DumpstateUtilTest, RunCommandTimesoutWithSec) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001023 CreateFd("RunCommandTimesout.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001024 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1025 CommandOptions::WithTimeout(1).Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001026 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1027 " --sleep 2' timed out after 1"));
1028 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1029 " --sleep 2' timed out after 1"));
1030}
1031
Vishnu Nair6921f802017-11-22 09:17:23 -08001032TEST_F(DumpstateUtilTest, RunCommandTimesoutWithMsec) {
1033 CreateFd("RunCommandTimesout.txt");
1034 EXPECT_EQ(-1, RunCommand("", {kSimpleCommand, "--sleep", "2"},
1035 CommandOptions::WithTimeoutInMs(1000).Build()));
1036 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + kSimpleCommand +
1037 " --sleep 2' timed out after 1"));
1038 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + kSimpleCommand +
1039 " --sleep 2' timed out after 1"));
1040}
1041
1042
Felipe Leme46b85da2016-11-21 17:40:45 -08001043TEST_F(DumpstateUtilTest, RunCommandIsKilled) {
1044 CreateFd("RunCommandIsKilled.txt");
1045 CaptureStderr();
1046
1047 std::thread t([=]() {
Felipe Lemef0292972016-11-22 13:57:05 -08001048 EXPECT_EQ(SIGTERM, RunCommandToFd(fd, "", {kSimpleCommand, "--pid", "--sleep", "20"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001049 CommandOptions::WithTimeout(100).Always().Build()));
1050 });
1051
1052 // Capture pid and pre-sleep output.
1053 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
1054 std::string err = GetCapturedStderr();
1055 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
1056
1057 CaptureFdOut();
1058 std::vector<std::string> lines = android::base::Split(out, "\n");
1059 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
1060
1061 int pid = atoi(lines[0].c_str());
1062 EXPECT_THAT(lines[1], StrEq("stdout line1"));
1063 EXPECT_THAT(lines[2], IsEmpty()); // \n
1064
1065 // Then kill the process.
1066 CaptureFdOut();
1067 CaptureStderr();
1068 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
1069 t.join();
1070
1071 // Finally, check output after murder.
1072 CaptureFdOut();
1073 err = GetCapturedStderr();
1074
1075 // out starts with the pid, which is an unknown
1076 EXPECT_THAT(out, EndsWith("stdout line1\n*** command '" + kSimpleCommand +
1077 " --pid --sleep 20' failed: killed by signal 15\n"));
1078 EXPECT_THAT(err, StrEq("*** command '" + kSimpleCommand +
1079 " --pid --sleep 20' failed: killed by signal 15\n"));
1080}
1081
1082TEST_F(DumpstateUtilTest, RunCommandAsRootUserBuild) {
1083 if (!IsStandalone()) {
1084 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1085 // to Shell - need to refactor tests to avoid this problem)
1086 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootUserBuild() on test suite\n")
1087 return;
1088 }
1089 CreateFd("RunCommandAsRootUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001090 if (!PropertiesHelper::IsUserBuild()) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001091 // Emulates user build if necessarily.
1092 SetBuildType("user");
1093 }
1094
1095 DropRoot();
1096
Felipe Lemef0292972016-11-22 13:57:05 -08001097 EXPECT_EQ(0, RunCommand("", {kSimpleCommand}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme46b85da2016-11-21 17:40:45 -08001098
1099 // We don't know the exact path of su, so we just check for the 'root ...' commands
1100 EXPECT_THAT(out, StartsWith("Skipping"));
1101 EXPECT_THAT(out, EndsWith("root " + kSimpleCommand + "' on user build.\n"));
1102 EXPECT_THAT(err, IsEmpty());
1103}
1104
1105TEST_F(DumpstateUtilTest, RunCommandAsRootNonUserBuild) {
1106 if (!IsStandalone()) {
1107 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1108 // to Shell - need to refactor tests to avoid this problem)
1109 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootNonUserBuild() on test suite\n")
1110 return;
1111 }
1112 CreateFd("RunCommandAsRootNonUserBuild.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001113 if (PropertiesHelper::IsUserBuild()) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001114 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1115 return;
1116 }
1117
1118 DropRoot();
1119
Felipe Lemef0292972016-11-22 13:57:05 -08001120 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1121 CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Leme7447d7c2016-11-03 18:12:22 -07001122
1123 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1124 EXPECT_THAT(err, StrEq("stderr\n"));
1125}
Felipe Leme46b85da2016-11-21 17:40:45 -08001126
Yifan Hong48e83a12017-10-03 14:10:07 -07001127
1128TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnUserBuild) {
1129 if (!IsStandalone()) {
1130 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1131 // to Shell - need to refactor tests to avoid this problem)
1132 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnUserBuild() on test suite\n")
1133 return;
1134 }
1135 CreateFd("RunCommandAsRootIfAvailableOnUserBuild.txt");
1136 if (!PropertiesHelper::IsUserBuild()) {
1137 // Emulates user build if necessarily.
1138 SetBuildType("user");
1139 }
1140
1141 DropRoot();
1142
1143 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1144 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1145
1146 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1147 EXPECT_THAT(err, StrEq("stderr\n"));
1148}
1149
1150TEST_F(DumpstateUtilTest, RunCommandAsRootIfAvailableOnDebugBuild) {
1151 if (!IsStandalone()) {
1152 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1153 // to Shell - need to refactor tests to avoid this problem)
1154 MYLOGE("Skipping DumpstateUtilTest.RunCommandAsRootIfAvailableOnDebugBuild() on test suite\n")
1155 return;
1156 }
1157 CreateFd("RunCommandAsRootIfAvailableOnDebugBuild.txt");
1158 if (PropertiesHelper::IsUserBuild()) {
1159 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
1160 return;
1161 }
1162
1163 DropRoot();
1164
1165 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
1166 CommandOptions::WithTimeout(1).AsRootIfAvailable().Build()));
1167
1168 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1169 EXPECT_THAT(err, StrEq("stderr\n"));
1170}
1171
Felipe Leme46b85da2016-11-21 17:40:45 -08001172TEST_F(DumpstateUtilTest, RunCommandDropRoot) {
1173 if (!IsStandalone()) {
1174 // TODO: temporarily disabled because it might cause other tests to fail after dropping
1175 // to Shell - need to refactor tests to avoid this problem)
1176 MYLOGE("Skipping DumpstateUtilTest.RunCommandDropRoot() on test suite\n")
1177 return;
1178 }
1179 CreateFd("RunCommandDropRoot.txt");
1180 // First check root case - only available when running with 'adb root'.
1181 uid_t uid = getuid();
1182 if (uid == 0) {
Felipe Lemef0292972016-11-22 13:57:05 -08001183 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"}));
Felipe Leme46b85da2016-11-21 17:40:45 -08001184 EXPECT_THAT(out, StrEq("0\nstdout\n"));
1185 EXPECT_THAT(err, StrEq("stderr\n"));
1186 return;
1187 }
1188 // Then run dropping root.
Felipe Lemef0292972016-11-22 13:57:05 -08001189 EXPECT_EQ(0, RunCommand("", {kSimpleCommand, "--uid"},
Felipe Leme46b85da2016-11-21 17:40:45 -08001190 CommandOptions::WithTimeout(1).DropRoot().Build()));
1191 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
1192 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
1193}
1194
Felipe Lemef0292972016-11-22 13:57:05 -08001195TEST_F(DumpstateUtilTest, DumpFileNotFoundNoTitle) {
Felipe Leme46b85da2016-11-21 17:40:45 -08001196 CreateFd("DumpFileNotFound.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001197 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001198 EXPECT_THAT(out,
1199 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
1200 EXPECT_THAT(err, IsEmpty());
1201}
1202
Felipe Lemef0292972016-11-22 13:57:05 -08001203TEST_F(DumpstateUtilTest, DumpFileNotFoundWithTitle) {
1204 CreateFd("DumpFileNotFound.txt");
1205 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
1206 EXPECT_THAT(out, StrEq("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No such "
1207 "file or directory\n"));
1208 EXPECT_THAT(err, IsEmpty());
1209}
1210
Felipe Leme46b85da2016-11-21 17:40:45 -08001211TEST_F(DumpstateUtilTest, DumpFileSingleLine) {
1212 CreateFd("DumpFileSingleLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001213 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001214 EXPECT_THAT(err, IsEmpty());
1215 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
1216}
1217
1218TEST_F(DumpstateUtilTest, DumpFileSingleLineWithNewLine) {
1219 CreateFd("DumpFileSingleLineWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001220 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001221 EXPECT_THAT(err, IsEmpty());
1222 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
1223}
1224
1225TEST_F(DumpstateUtilTest, DumpFileMultipleLines) {
1226 CreateFd("DumpFileMultipleLines.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001227 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001228 EXPECT_THAT(err, IsEmpty());
1229 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1230}
1231
1232TEST_F(DumpstateUtilTest, DumpFileMultipleLinesWithNewLine) {
1233 CreateFd("DumpFileMultipleLinesWithNewLine.txt");
Felipe Lemef0292972016-11-22 13:57:05 -08001234 EXPECT_EQ(0, DumpFile("", kTestDataPath + "multiple-lines-with-newline.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001235 EXPECT_THAT(err, IsEmpty());
1236 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
1237}
1238
Felipe Lemef0292972016-11-22 13:57:05 -08001239TEST_F(DumpstateUtilTest, DumpFileOnDryRunNoTitle) {
1240 CreateFd("DumpFileOnDryRun.txt");
1241 SetDryRun(true);
1242 std::string path = kTestDataPath + "single-line.txt";
1243 EXPECT_EQ(0, DumpFile("", kTestDataPath + "single-line.txt"));
1244 EXPECT_THAT(err, IsEmpty());
1245 EXPECT_THAT(out, StrEq(path + ": skipped on dry run\n"));
1246}
1247
Felipe Leme46b85da2016-11-21 17:40:45 -08001248TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
1249 CreateFd("DumpFileOnDryRun.txt");
1250 SetDryRun(true);
1251 std::string path = kTestDataPath + "single-line.txt";
Felipe Lemef0292972016-11-22 13:57:05 -08001252 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", kTestDataPath + "single-line.txt"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001253 EXPECT_THAT(err, IsEmpty());
Felipe Lemef0292972016-11-22 13:57:05 -08001254 EXPECT_THAT(
1255 out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
1256 EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
Felipe Leme46b85da2016-11-21 17:40:45 -08001257}
Ecco Park61ffcf72016-10-27 15:46:26 -07001258
1259TEST_F(DumpstateUtilTest, FindingPidWithExistingProcess) {
1260 // init process always has pid 1.
1261 EXPECT_EQ(1, FindPidOfProcess("init"));
1262 EXPECT_THAT(err, IsEmpty());
1263}
1264
1265TEST_F(DumpstateUtilTest, FindingPidWithNotExistingProcess) {
1266 // find the process with abnormal name.
1267 EXPECT_EQ(-1, FindPidOfProcess("abcdef12345-543"));
1268 EXPECT_THAT(err, StrEq("can't find the pid\n"));
1269}
Felipe Leme47e9be22016-12-21 15:37:07 -08001270
1271} // namespace dumpstate
1272} // namespace os
1273} // namespace android