crash-reporter: Fix unclean_shutdown_collector_test to work by itself

The CantDisable test was failing because it couldn't properly fake the
enabling of the crash collector -- which meant that the disable didn't
actually fail.  The reason was that the "test" directory was being removed
during SetUp().  I also added/improved some error-checking that helped me
track down various unittest failures.

BUG=chromium-os:29395
TEST=Ran unittests

Change-Id: I8cf50dbd0d5187b0028c8cf16ae10a4f68bb160f
Reviewed-on: https://gerrit.chromium.org/gerrit/20129
Commit-Ready: Michael Krebs <mkrebs@chromium.org>
Tested-by: Michael Krebs <mkrebs@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
diff --git a/crash_reporter/kernel_collector_test.cc b/crash_reporter/kernel_collector_test.cc
index 59f435a..ea18c62 100644
--- a/crash_reporter/kernel_collector_test.cc
+++ b/crash_reporter/kernel_collector_test.cc
@@ -41,7 +41,11 @@
     collector_.OverridePreservedDumpPath(test_kcrash_);
     test_kcrash_ = test_kcrash_.Append("dmesg-ramoops-0");
     unlink(test_kcrash_.value().c_str());
-    mkdir(kTestCrashDirectory, 0777);
+    if (mkdir(kTestCrashDirectory, 0777)) {
+      ASSERT_EQ(EEXIST, errno)
+          << "Error while creating directory '" << kTestCrashDirectory
+          << "': " << strerror(errno);
+    }
     chromeos::ClearLog();
   }
  protected:
@@ -257,7 +261,9 @@
   static const char kNamePrefix[] = "Stored kcrash to ";
   std::string log = chromeos::GetLog();
   size_t pos = log.find(kNamePrefix);
-  ASSERT_NE(std::string::npos, pos);
+  ASSERT_NE(std::string::npos, pos)
+      << "Did not find string \"" << kNamePrefix << "\" in log: {\n"
+      << log << "}";
   pos += strlen(kNamePrefix);
   std::string filename = log.substr(pos, std::string::npos);
   // Take the name up until \n
diff --git a/crash_reporter/unclean_shutdown_collector_test.cc b/crash_reporter/unclean_shutdown_collector_test.cc
index 89b0ec9..8adb4d3 100644
--- a/crash_reporter/unclean_shutdown_collector_test.cc
+++ b/crash_reporter/unclean_shutdown_collector_test.cc
@@ -15,6 +15,7 @@
 static int s_crashes = 0;
 static bool s_metrics = true;
 
+static const char kTestDirectory[] = "test";
 static const char kTestLowBattery[] = "test/low_battery";
 static const char kTestSuspended[] = "test/suspended";
 static const char kTestUnclean[] = "test/unclean";
@@ -34,7 +35,7 @@
     s_crashes = 0;
     collector_.Initialize(CountCrash,
                           IsMetrics);
-    rmdir("test");
+    rmdir(kTestDirectory);
     test_unclean_ = FilePath(kTestUnclean);
     collector_.unclean_shutdown_file_ = kTestUnclean;
     file_util::Delete(test_unclean_, true);
@@ -60,7 +61,7 @@
 }
 
 TEST_F(UncleanShutdownCollectorTest, EnableWithParent) {
-  mkdir("test", 0777);
+  mkdir(kTestDirectory, 0777);
   ASSERT_TRUE(collector_.Enable());
   ASSERT_TRUE(file_util::PathExists(test_unclean_));
 }
@@ -121,8 +122,15 @@
 }
 
 TEST_F(UncleanShutdownCollectorTest, CantDisable) {
-  mkdir(kTestUnclean, 0700);
-  file_util::WriteFile(test_unclean_.Append("foo"), "", 0);
+  mkdir(kTestDirectory, 0700);
+  if (mkdir(kTestUnclean, 0700)) {
+    ASSERT_EQ(EEXIST, errno)
+        << "Error while creating directory '" << kTestUnclean
+        << "': " << strerror(errno);
+  }
+  ASSERT_EQ(0, file_util::WriteFile(test_unclean_.Append("foo"), "", 0))
+      << "Error while creating empty file '"
+      << test_unclean_.Append("foo").value() << "': " << strerror(errno);
   ASSERT_FALSE(collector_.Disable());
   rmdir(kTestUnclean);
 }