blob: 4073c3c3a1ed41c5050d3cf2d4dd00e82b6ce6ed [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
17#include "dumpstate.h"
18
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
22#include <libgen.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070023#include <signal.h>
24#include <sys/types.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070025#include <unistd.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070026#include <thread>
Felipe Leme4c2d6632016-09-28 14:32:00 -070027
28#include <android-base/file.h>
Felipe Lemed80e6b62016-10-03 13:08:14 -070029#include <android-base/properties.h>
30#include <android-base/stringprintf.h>
Felipe Lemefd8affa2016-09-30 17:38:57 -070031#include <android-base/strings.h>
Felipe Leme4c2d6632016-09-28 14:32:00 -070032
Felipe Lemed80e6b62016-10-03 13:08:14 -070033#define LOG_TAG "dumpstate"
34#include <cutils/log.h>
35
Felipe Leme4c2d6632016-09-28 14:32:00 -070036using ::testing::EndsWith;
37using ::testing::IsEmpty;
38using ::testing::StrEq;
39using ::testing::StartsWith;
40using ::testing::Test;
41using ::testing::internal::CaptureStderr;
42using ::testing::internal::CaptureStdout;
43using ::testing::internal::GetCapturedStderr;
44using ::testing::internal::GetCapturedStdout;
45
46// Not used on test cases yet...
47void dumpstate_board(void) {
48}
49
50class DumpstateTest : public Test {
51 public:
52 void SetUp() {
53 SetDryRun(false);
Felipe Lemed80e6b62016-10-03 13:08:14 -070054 SetBuildType(android::base::GetProperty("ro.build.type", "(unknown)"));
Felipe Leme9a523ae2016-10-20 15:10:33 -070055 ds.update_progress_ = false;
Felipe Leme4c2d6632016-09-28 14:32:00 -070056 }
57
58 // Runs a command and capture `stdout` and `stderr`.
Felipe Leme9a523ae2016-10-20 15:10:33 -070059 int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme4c2d6632016-09-28 14:32:00 -070060 const CommandOptions& options = CommandOptions::DEFAULT) {
61 CaptureStdout();
62 CaptureStderr();
Felipe Leme9a523ae2016-10-20 15:10:33 -070063 int status = ds.RunCommand(title, full_command, options);
Felipe Leme4c2d6632016-09-28 14:32:00 -070064 out = GetCapturedStdout();
65 err = GetCapturedStderr();
66 return status;
67 }
68
Felipe Lemecef02982016-10-03 17:22:22 -070069 // Dumps a file and capture `stdout` and `stderr`.
70 int DumpFile(const std::string& title, const std::string& path) {
71 CaptureStdout();
72 CaptureStderr();
73 int status = ds.DumpFile(title, path);
74 out = GetCapturedStdout();
75 err = GetCapturedStderr();
76 return status;
77 }
78
Felipe Leme9a523ae2016-10-20 15:10:33 -070079 void SetDryRun(bool dry_run) {
80 ALOGD("Setting dry_run_ to %s\n", dry_run ? "true" : "false");
81 ds.dry_run_ = dry_run;
Felipe Lemed80e6b62016-10-03 13:08:14 -070082 }
83
Felipe Leme9a523ae2016-10-20 15:10:33 -070084 void SetBuildType(const std::string& build_type) {
85 ALOGD("Setting build_type_ to '%s'\n", build_type.c_str());
86 ds.build_type_ = build_type;
Felipe Lemed80e6b62016-10-03 13:08:14 -070087 }
88
89 bool IsUserBuild() {
90 return "user" == android::base::GetProperty("ro.build.type", "(unknown)");
91 }
92
93 void DropRoot() {
94 drop_root_user();
95 uid_t uid = getuid();
96 ASSERT_EQ(2000, (int)uid);
97 }
98
99 // TODO: remove when progress is set by Binder callbacks.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700100 void AssertSystemProperty(const std::string& key, const std::string& expected_value) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700101 std::string actualValue = android::base::GetProperty(key, "not set");
Felipe Leme9a523ae2016-10-20 15:10:33 -0700102 EXPECT_THAT(expected_value, StrEq(actualValue)) << "invalid value for property " << key;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700103 }
104
Felipe Leme9a523ae2016-10-20 15:10:33 -0700105 std::string GetProgressMessage(int progress, int weigh_total, int old_weigh_total = 0) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700106 EXPECT_EQ(progress, ds.progress_) << "invalid progress";
Felipe Leme9a523ae2016-10-20 15:10:33 -0700107 EXPECT_EQ(weigh_total, ds.weight_total_) << "invalid weigh_total";
Felipe Lemed80e6b62016-10-03 13:08:14 -0700108
109 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.progress", getpid()),
110 std::to_string(progress));
111
Felipe Leme9a523ae2016-10-20 15:10:33 -0700112 bool max_increased = old_weigh_total > 0;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700113
Felipe Leme9a523ae2016-10-20 15:10:33 -0700114 std::string adjustment_message = "";
115 if (max_increased) {
Felipe Lemed80e6b62016-10-03 13:08:14 -0700116 AssertSystemProperty(android::base::StringPrintf("dumpstate.%d.max", getpid()),
Felipe Leme9a523ae2016-10-20 15:10:33 -0700117 std::to_string(weigh_total));
118 adjustment_message = android::base::StringPrintf(
119 "Adjusting total weight from %d to %d\n", old_weigh_total, weigh_total);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700120 }
121
122 return android::base::StringPrintf("%sSetting progress (dumpstate.%d.progress): %d/%d\n",
Felipe Leme9a523ae2016-10-20 15:10:33 -0700123 adjustment_message.c_str(), getpid(), progress,
124 weigh_total);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700125 }
126
Felipe Leme4c2d6632016-09-28 14:32:00 -0700127 // `stdout` and `stderr` from the last command ran.
128 std::string out, err;
129
Felipe Leme9a523ae2016-10-20 15:10:33 -0700130 std::string test_path = dirname(android::base::GetExecutablePath().c_str());
131 std::string fixtures_path = test_path + "/../dumpstate_test_fixture/";
132 std::string test_data_path = fixtures_path + "/testdata/";
133 std::string simple_command = fixtures_path + "dumpstate_test_fixture";
134 std::string echo_command = "/system/bin/echo";
Felipe Lemefd8affa2016-09-30 17:38:57 -0700135
136 Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme4c2d6632016-09-28 14:32:00 -0700137};
138
139TEST_F(DumpstateTest, RunCommandNoArgs) {
140 EXPECT_EQ(-1, RunCommand("", {}));
141}
142
143TEST_F(DumpstateTest, RunCommandNoTitle) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700144 EXPECT_EQ(0, RunCommand("", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700145 EXPECT_THAT(out, StrEq("stdout\n"));
146 EXPECT_THAT(err, StrEq("stderr\n"));
147}
148
149TEST_F(DumpstateTest, RunCommandWithTitle) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700150 EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700151 EXPECT_THAT(err, StrEq("stderr\n"));
152 // We don't know the exact duration, so we check the prefix and suffix
Felipe Lemefd8affa2016-09-30 17:38:57 -0700153 EXPECT_THAT(out,
Felipe Leme9a523ae2016-10-20 15:10:33 -0700154 StartsWith("------ I AM GROOT (" + simple_command + ") ------\nstdout\n------"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700155 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
156}
157
Felipe Lemefd8affa2016-09-30 17:38:57 -0700158TEST_F(DumpstateTest, RunCommandWithLoggingMessage) {
Felipe Leme4c2d6632016-09-28 14:32:00 -0700159 EXPECT_EQ(
Felipe Leme9a523ae2016-10-20 15:10:33 -0700160 0, RunCommand("", {simple_command},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700161 CommandOptions::WithTimeout(10).Log("COMMAND, Y U NO LOG FIRST?").Build()));
162 EXPECT_THAT(out, StrEq("stdout\n"));
163 EXPECT_THAT(err, StrEq("COMMAND, Y U NO LOG FIRST?stderr\n"));
164}
165
166TEST_F(DumpstateTest, RunCommandRedirectStderr) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700167 EXPECT_EQ(0, RunCommand("", {simple_command},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700168 CommandOptions::WithTimeout(10).RedirectStderr().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700169 EXPECT_THAT(out, IsEmpty());
Felipe Lemefd8affa2016-09-30 17:38:57 -0700170 EXPECT_THAT(err, StrEq("stdout\nstderr\n"));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700171}
172
173TEST_F(DumpstateTest, RunCommandWithOneArg) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700174 EXPECT_EQ(0, RunCommand("", {echo_command, "one"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700175 EXPECT_THAT(err, IsEmpty());
176 EXPECT_THAT(out, StrEq("one\n"));
177}
178
Felipe Lemefd8affa2016-09-30 17:38:57 -0700179TEST_F(DumpstateTest, RunCommandWithMultipleArgs) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700180 EXPECT_EQ(0, RunCommand("", {echo_command, "one", "is", "the", "loniest", "number"}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700181 EXPECT_THAT(err, IsEmpty());
182 EXPECT_THAT(out, StrEq("one is the loniest number\n"));
183}
184
185TEST_F(DumpstateTest, RunCommandDryRun) {
186 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700187 EXPECT_EQ(0, RunCommand("I AM GROOT", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700188 // We don't know the exact duration, so we check the prefix and suffix
Felipe Leme9a523ae2016-10-20 15:10:33 -0700189 EXPECT_THAT(out, StartsWith("------ I AM GROOT (" + simple_command +
Felipe Leme4c2d6632016-09-28 14:32:00 -0700190 ") ------\n\t(skipped on dry run)\n------"));
191 EXPECT_THAT(out, EndsWith("s was the duration of 'I AM GROOT' ------\n"));
192 EXPECT_THAT(err, IsEmpty());
193}
194
195TEST_F(DumpstateTest, RunCommandDryRunNoTitle) {
196 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700197 EXPECT_EQ(0, RunCommand("", {simple_command}));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700198 EXPECT_THAT(out, IsEmpty());
199 EXPECT_THAT(err, IsEmpty());
200}
201
202TEST_F(DumpstateTest, RunCommandDryRunAlways) {
203 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700204 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Always().Build()));
Felipe Leme4c2d6632016-09-28 14:32:00 -0700205 EXPECT_THAT(out, StrEq("stdout\n"));
206 EXPECT_THAT(err, StrEq("stderr\n"));
207}
208
Felipe Lemefd8affa2016-09-30 17:38:57 -0700209TEST_F(DumpstateTest, RunCommandNotFound) {
210 EXPECT_NE(0, RunCommand("", {"/there/cannot/be/such/command"}));
211 EXPECT_THAT(out, StartsWith("*** command '/there/cannot/be/such/command' failed: exit code"));
212 EXPECT_THAT(err, StartsWith("execvp on command '/there/cannot/be/such/command' failed"));
213}
214
215TEST_F(DumpstateTest, RunCommandFails) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700216 EXPECT_EQ(42, RunCommand("", {simple_command, "--exit", "42"}));
217 EXPECT_THAT(out, StrEq("stdout\n*** command '" + simple_command +
218 " --exit 42' failed: exit code 42\n"));
219 EXPECT_THAT(err, StrEq("stderr\n*** command '" + simple_command +
220 " --exit 42' failed: exit code 42\n"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700221}
222
223TEST_F(DumpstateTest, RunCommandCrashes) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700224 EXPECT_NE(0, RunCommand("", {simple_command, "--crash"}));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700225 // We don't know the exit code, so check just the prefix.
226 EXPECT_THAT(
Felipe Leme9a523ae2016-10-20 15:10:33 -0700227 out, StartsWith("stdout\n*** command '" + simple_command + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700228 EXPECT_THAT(
Felipe Leme9a523ae2016-10-20 15:10:33 -0700229 err, StartsWith("stderr\n*** command '" + simple_command + " --crash' failed: exit code"));
Felipe Lemefd8affa2016-09-30 17:38:57 -0700230}
231
232TEST_F(DumpstateTest, RunCommandTimesout) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700233 EXPECT_EQ(-1, RunCommand("", {simple_command, "--sleep", "2"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700234 CommandOptions::WithTimeout(1).Build()));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700235 EXPECT_THAT(out, StartsWith("stdout line1\n*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700236 " --sleep 2' timed out after 1"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700237 EXPECT_THAT(err, StartsWith("sleeping for 2s\n*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700238 " --sleep 2' timed out after 1"));
239}
240
241TEST_F(DumpstateTest, RunCommandIsKilled) {
242 CaptureStdout();
243 CaptureStderr();
244
245 std::thread t([=]() {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700246 EXPECT_EQ(SIGTERM, ds.RunCommand("", {simple_command, "--pid", "--sleep", "20"},
Felipe Lemefd8affa2016-09-30 17:38:57 -0700247 CommandOptions::WithTimeout(100).Always().Build()));
248 });
249
250 // Capture pid and pre-sleep output.
251 sleep(1); // Wait a little bit to make sure pid and 1st line were printed.
252 std::string err = GetCapturedStderr();
253 EXPECT_THAT(err, StrEq("sleeping for 20s\n"));
254
255 std::string out = GetCapturedStdout();
256 std::vector<std::string> lines = android::base::Split(out, "\n");
257 ASSERT_EQ(3, (int)lines.size()) << "Invalid lines before sleep: " << out;
258
259 int pid = atoi(lines[0].c_str());
260 EXPECT_THAT(lines[1], StrEq("stdout line1"));
261 EXPECT_THAT(lines[2], IsEmpty()); // \n
262
263 // Then kill the process.
264 CaptureStdout();
265 CaptureStderr();
266 ASSERT_EQ(0, kill(pid, SIGTERM)) << "failed to kill pid " << pid;
267 t.join();
268
269 // Finally, check output after murder.
270 out = GetCapturedStdout();
271 err = GetCapturedStderr();
272
Felipe Leme9a523ae2016-10-20 15:10:33 -0700273 EXPECT_THAT(out, StrEq("*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700274 " --pid --sleep 20' failed: killed by signal 15\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700275 EXPECT_THAT(err, StrEq("*** command '" + simple_command +
Felipe Lemefd8affa2016-09-30 17:38:57 -0700276 " --pid --sleep 20' failed: killed by signal 15\n"));
277}
278
Felipe Lemed80e6b62016-10-03 13:08:14 -0700279TEST_F(DumpstateTest, RunCommandProgress) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700280 ds.update_progress_ = true;
Felipe Lemecef02982016-10-03 17:22:22 -0700281 ds.progress_ = 0;
Felipe Leme9a523ae2016-10-20 15:10:33 -0700282 ds.weight_total_ = 30;
Felipe Lemed80e6b62016-10-03 13:08:14 -0700283
Felipe Leme9a523ae2016-10-20 15:10:33 -0700284 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(20).Build()));
285 std::string progress_message = GetProgressMessage(20, 30);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700286 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700287 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700288
Felipe Leme9a523ae2016-10-20 15:10:33 -0700289 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(10).Build()));
290 progress_message = GetProgressMessage(30, 30);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700291 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700292 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700293
294 // Run a command that will increase maximum timeout.
Felipe Leme9a523ae2016-10-20 15:10:33 -0700295 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).Build()));
296 progress_message = GetProgressMessage(31, 36, 30); // 20% increase
Felipe Lemed80e6b62016-10-03 13:08:14 -0700297 EXPECT_THAT(out, StrEq("stdout\n"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700298 EXPECT_THAT(err, StrEq("stderr\n" + progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700299
Felipe Leme9a523ae2016-10-20 15:10:33 -0700300 // Make sure command ran while in dry_run is counted.
Felipe Lemed80e6b62016-10-03 13:08:14 -0700301 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700302 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(4).Build()));
303 progress_message = GetProgressMessage(35, 36);
Felipe Lemed80e6b62016-10-03 13:08:14 -0700304 EXPECT_THAT(out, IsEmpty());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700305 EXPECT_THAT(err, StrEq(progress_message));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700306}
307
308TEST_F(DumpstateTest, RunCommandDropRoot) {
309 // First check root case - only available when running with 'adb root'.
310 uid_t uid = getuid();
311 if (uid == 0) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700312 EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"}));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700313 EXPECT_THAT(out, StrEq("0\nstdout\n"));
314 EXPECT_THAT(err, StrEq("stderr\n"));
315 return;
316 }
317 // Then drop root.
318
Felipe Leme9a523ae2016-10-20 15:10:33 -0700319 EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700320 CommandOptions::WithTimeout(1).DropRoot().Build()));
321 EXPECT_THAT(out, StrEq("2000\nstdout\n"));
Felipe Leme26c41572016-10-06 14:34:43 -0700322 EXPECT_THAT(err, StrEq("drop_root_user(): already running as Shell\nstderr\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700323}
324
325TEST_F(DumpstateTest, RunCommandAsRootUserBuild) {
326 if (!IsUserBuild()) {
327 // Emulates user build if necessarily.
328 SetBuildType("user");
329 }
330
331 DropRoot();
332
Felipe Leme9a523ae2016-10-20 15:10:33 -0700333 EXPECT_EQ(0, RunCommand("", {simple_command}, CommandOptions::WithTimeout(1).AsRoot().Build()));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700334
335 // We don't know the exact path of su, so we just check for the 'root ...' commands
336 EXPECT_THAT(out, StartsWith("Skipping"));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700337 EXPECT_THAT(out, EndsWith("root " + simple_command + "' on user build.\n"));
Felipe Lemed80e6b62016-10-03 13:08:14 -0700338 EXPECT_THAT(err, IsEmpty());
339}
340
341TEST_F(DumpstateTest, RunCommandAsRootNonUserBuild) {
342 if (IsUserBuild()) {
343 ALOGI("Skipping RunCommandAsRootNonUserBuild on user builds\n");
344 return;
345 }
346
347 DropRoot();
348
Felipe Leme9a523ae2016-10-20 15:10:33 -0700349 EXPECT_EQ(0, RunCommand("", {simple_command, "--uid"},
Felipe Lemed80e6b62016-10-03 13:08:14 -0700350 CommandOptions::WithTimeout(1).AsRoot().Build()));
351
352 EXPECT_THAT(out, StrEq("0\nstdout\n"));
353 EXPECT_THAT(err, StrEq("stderr\n"));
354}
Felipe Leme4c2d6632016-09-28 14:32:00 -0700355
Felipe Lemecef02982016-10-03 17:22:22 -0700356TEST_F(DumpstateTest, DumpFileNotFoundNoTitle) {
357 EXPECT_EQ(-1, DumpFile("", "/I/cant/believe/I/exist"));
358 EXPECT_THAT(out,
359 StrEq("*** Error dumping /I/cant/believe/I/exist: No such file or directory\n"));
360 EXPECT_THAT(err, IsEmpty());
361}
362
363TEST_F(DumpstateTest, DumpFileNotFoundWithTitle) {
364 EXPECT_EQ(-1, DumpFile("Y U NO EXIST?", "/I/cant/believe/I/exist"));
365 EXPECT_THAT(err, IsEmpty());
366 // We don't know the exact duration, so we check the prefix and suffix
367 EXPECT_THAT(out, StartsWith("*** Error dumping /I/cant/believe/I/exist (Y U NO EXIST?): No "
368 "such file or directory\n"));
369 EXPECT_THAT(out, EndsWith("s was the duration of 'Y U NO EXIST?' ------\n"));
370}
371
372TEST_F(DumpstateTest, DumpFileSingleLine) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700373 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700374 EXPECT_THAT(err, IsEmpty());
375 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
376}
377
378TEST_F(DumpstateTest, DumpFileSingleLineWithNewLine) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700379 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700380 EXPECT_THAT(err, IsEmpty());
381 EXPECT_THAT(out, StrEq("I AM LINE1\n"));
382}
383
384TEST_F(DumpstateTest, DumpFileMultipleLines) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700385 EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700386 EXPECT_THAT(err, IsEmpty());
387 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
388}
389
390TEST_F(DumpstateTest, DumpFileMultipleLinesWithNewLine) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700391 EXPECT_EQ(0, DumpFile("", test_data_path + "multiple-lines-with-newline.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700392 EXPECT_THAT(err, IsEmpty());
393 EXPECT_THAT(out, StrEq("I AM LINE1\nI AM LINE2\nI AM LINE3\n"));
394}
395
396TEST_F(DumpstateTest, DumpFileOnDryRunNoTitle) {
397 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700398 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700399 EXPECT_THAT(err, IsEmpty());
400 EXPECT_THAT(out, IsEmpty());
401}
402
403TEST_F(DumpstateTest, DumpFileOnDryRun) {
404 SetDryRun(true);
Felipe Leme9a523ae2016-10-20 15:10:33 -0700405 EXPECT_EQ(0, DumpFile("Might as well dump. Dump!", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700406 EXPECT_THAT(err, IsEmpty());
Felipe Leme9a523ae2016-10-20 15:10:33 -0700407 EXPECT_THAT(out, StartsWith("------ Might as well dump. Dump! (" + test_data_path +
Felipe Lemecef02982016-10-03 17:22:22 -0700408 "single-line.txt) ------\n\t(skipped on dry run)\n------"));
409 EXPECT_THAT(out, EndsWith("s was the duration of 'Might as well dump. Dump!' ------\n"));
410 EXPECT_THAT(err, IsEmpty());
411}
412
413TEST_F(DumpstateTest, DumpFileUpdateProgress) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700414 ds.update_progress_ = true;
Felipe Lemecef02982016-10-03 17:22:22 -0700415 ds.progress_ = 0;
Felipe Leme9a523ae2016-10-20 15:10:33 -0700416 ds.weight_total_ = 30;
Felipe Lemecef02982016-10-03 17:22:22 -0700417
Felipe Leme9a523ae2016-10-20 15:10:33 -0700418 EXPECT_EQ(0, DumpFile("", test_data_path + "single-line.txt"));
Felipe Lemecef02982016-10-03 17:22:22 -0700419
Felipe Leme9a523ae2016-10-20 15:10:33 -0700420 std::string progress_message = GetProgressMessage(5, 30); // TODO: unhardcode WEIGHT_FILE (5)?
Felipe Lemecef02982016-10-03 17:22:22 -0700421
Felipe Leme9a523ae2016-10-20 15:10:33 -0700422 EXPECT_THAT(err, StrEq(progress_message));
Felipe Lemecef02982016-10-03 17:22:22 -0700423 EXPECT_THAT(out, StrEq("I AM LINE1\n")); // dumpstate adds missing newline
424}