shill: profile: Mark invalid profiles as corrupted and ignore

If we fail to open a profile due to a failure in loading
the keyfile store, move the broken file out of the way, since
we will never be able to load this file in later attempts.
In situations where this is the default profile, this will
allow us to start up successfully the next time.

BUG=chromium-os:33822
TEST=Unit tests + manual (copy default.profile from the bug
and ensure shill restarts itself and moves the corrupted
profile out of the way.)

Change-Id: Ia3b7716b36b52c559193f6f6e28c9b185a77c01e
Reviewed-on: https://gerrit.chromium.org/gerrit/31374
Commit-Ready: Paul Stewart <pstew@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/key_file_store_unittest.cc b/key_file_store_unittest.cc
index 1cc3edd..bd0c166 100644
--- a/key_file_store_unittest.cc
+++ b/key_file_store_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -102,6 +102,19 @@
   EXPECT_FALSE(store_.key_file_);
 }
 
+TEST_F(KeyFileStoreTest, MarkAsCorrupted) {
+  EXPECT_FALSE(store_.MarkAsCorrupted());
+  EXPECT_FALSE(store_.IsNonEmpty());
+  WriteKeyFile("garbage\n");
+  EXPECT_TRUE(store_.IsNonEmpty());
+  EXPECT_TRUE(file_util::PathExists(store_.path()));
+  EXPECT_TRUE(store_.MarkAsCorrupted());
+  EXPECT_FALSE(store_.IsNonEmpty());
+  EXPECT_FALSE(file_util::PathExists(store_.path()));
+  EXPECT_TRUE(
+      file_util::PathExists(FilePath(store_.path().value() + ".corrupted")));
+}
+
 TEST_F(KeyFileStoreTest, GetGroups) {
   static const char kGroupA[] = "g-a";
   static const char kGroupB[] = "g-b";