AU: Add support for persistent update engine preferences store.

BUG=5140
TEST=emerge-x86-generic, attached unit tests

Review URL: http://codereview.chromium.org/3014035
diff --git a/SConstruct b/SConstruct
index 216eb1d..cf51f47 100644
--- a/SConstruct
+++ b/SConstruct
@@ -214,6 +214,7 @@
                    omaha_request_params.cc
                    omaha_response_handler_action.cc
                    postinstall_runner_action.cc
+                   prefs.cc
                    set_bootable_flag_action.cc
                    simple_key_value_store.cc
                    split_file_writer.cc
@@ -247,6 +248,7 @@
                             omaha_request_params_unittest.cc
                             omaha_response_handler_action_unittest.cc
                             postinstall_runner_action_unittest.cc
+                            prefs_unittest.cc
                             set_bootable_flag_action_unittest.cc
                             simple_key_value_store_unittest.cc
                             split_file_writer_unittest.cc
diff --git a/prefs.cc b/prefs.cc
new file mode 100644
index 0000000..38129c9
--- /dev/null
+++ b/prefs.cc
@@ -0,0 +1,65 @@
+// Copyright (c) 2010 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.
+
+#include "update_engine/prefs.h"
+
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/string_util.h"
+#include "update_engine/utils.h"
+
+using std::string;
+
+namespace chromeos_update_engine {
+
+bool Prefs::Init(const FilePath& prefs_dir) {
+  prefs_dir_ = prefs_dir;
+  return true;
+}
+
+bool Prefs::GetString(const string& key, string* value) {
+  LOG(INFO) << "Getting key \"" << key << "\"";
+  FilePath filename;
+  TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
+  TEST_AND_RETURN_FALSE(file_util::ReadFileToString(filename, value));
+  LOG(INFO) << "Key \"" << key << "\" value \"" << *value << "\"";
+  return true;
+}
+
+bool Prefs::SetString(const std::string& key, const std::string& value) {
+  LOG(INFO) << "Setting key \"" << key << "\" value \"" << value << "\"";
+  FilePath filename;
+  TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
+  TEST_AND_RETURN_FALSE(file_util::CreateDirectory(filename.DirName()));
+  TEST_AND_RETURN_FALSE(
+      file_util::WriteFile(filename, value.data(), value.size()) ==
+      static_cast<int>(value.size()));
+  return true;
+}
+
+bool Prefs::GetInt64(const string& key, int64_t* value) {
+  string str_value;
+  TEST_AND_RETURN_FALSE(GetString(key, &str_value));
+  TrimWhitespaceASCII(str_value, TRIM_ALL, &str_value);
+  TEST_AND_RETURN_FALSE(StringToInt64(str_value, value));
+  return true;
+}
+
+bool Prefs::SetInt64(const string& key, const int64_t value) {
+  return SetString(key, Int64ToString(value));
+}
+
+bool Prefs::GetFileNameForKey(const std::string& key, FilePath* filename) {
+  // Allows only non-empty keys containing [A-Za-z0-9_-].
+  TEST_AND_RETURN_FALSE(!key.empty());
+  for (size_t i = 0; i < key.size(); ++i) {
+    char c = key.at(i);
+    TEST_AND_RETURN_FALSE(IsAsciiAlpha(c) || IsAsciiDigit(c) ||
+                          c == '_' || c == '-');
+  }
+  *filename = prefs_dir_.Append(key);
+  return true;
+}
+
+}  // namespace chromeos_update_engine
diff --git a/prefs.h b/prefs.h
new file mode 100644
index 0000000..5926d97
--- /dev/null
+++ b/prefs.h
@@ -0,0 +1,46 @@
+// Copyright (c) 2010 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.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PREFS_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PREFS_H__
+
+#include "base/file_path.h"
+#include "gtest/gtest_prod.h"  // for FRIEND_TEST
+#include "update_engine/prefs_interface.h"
+
+namespace chromeos_update_engine {
+
+// Implements a preference store by storing the value associated with
+// a key in a separate file named after the key under a preference
+// store directory.
+
+class Prefs : public PrefsInterface {
+ public:
+  // Initializes the store by associating this object with |prefs_dir|
+  // as the preference store directory. Returns true on success, false
+  // otherwise.
+  bool Init(const FilePath& prefs_dir);
+
+  // PrefsInterface methods.
+  bool GetString(const std::string& key, std::string* value);
+  bool SetString(const std::string& key, const std::string& value);
+  bool GetInt64(const std::string& key, int64_t* value);
+  bool SetInt64(const std::string& key, const int64_t value);
+
+ private:
+  FRIEND_TEST(PrefsTest, GetFileNameForKey);
+  FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
+  FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
+
+  // Sets |filename| to the full path to the file containing the data
+  // associated with |key|. Returns true on success, false otherwise.
+  bool GetFileNameForKey(const std::string& key, FilePath* filename);
+
+  // Preference store directory.
+  FilePath prefs_dir_;
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_PREFS_H__
diff --git a/prefs_interface.h b/prefs_interface.h
new file mode 100644
index 0000000..5f75479
--- /dev/null
+++ b/prefs_interface.h
@@ -0,0 +1,40 @@
+// Copyright (c) 2010 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.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PREFS_INTERFACE_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PREFS_INTERFACE_H__
+
+#include <string>
+
+namespace chromeos_update_engine {
+
+// The prefs interface allows access to a persistent preferences
+// store. The two reasons for providing this as an interface are
+// testing as well as easier switching to a new implementation in the
+// future, if necessary.
+
+class PrefsInterface {
+ public:
+  // Gets a string |value| associated with |key|. Returns true on
+  // success, false on failure (including when the |key| is not
+  // present in the store).
+  virtual bool GetString(const std::string& key, std::string* value) = 0;
+
+  // Associates |key| with a string |value|. Returns true on success,
+  // false otherwise.
+  virtual bool SetString(const std::string& key, const std::string& value) = 0;
+
+  // Gets an int64 |value| associated with |key|. Returns true on
+  // success, false on failure (including when the |key| is not
+  // present in the store).
+  virtual bool GetInt64(const std::string& key, int64_t* value) = 0;
+
+  // Associates |key| with an int64 |value|. Returns true on success,
+  // false otherwise.
+  virtual bool SetInt64(const std::string& key, const int64_t value) = 0;
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_PREFS_INTERFACE_H__
diff --git a/prefs_unittest.cc b/prefs_unittest.cc
new file mode 100644
index 0000000..fc492fb
--- /dev/null
+++ b/prefs_unittest.cc
@@ -0,0 +1,190 @@
+// Copyright (c) 2010 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.
+
+#include <inttypes.h>
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/file_util.h"
+#include "base/string_util.h"
+#include "gtest/gtest.h"
+#include "update_engine/prefs.h"
+
+using std::string;
+
+namespace chromeos_update_engine {
+
+class PrefsTest : public ::testing::Test {
+ protected:
+  virtual void SetUp() {
+    ASSERT_TRUE(file_util::CreateNewTempDirectory("auprefs", &prefs_dir_));
+    ASSERT_TRUE(prefs_.Init(prefs_dir_));
+  }
+
+  virtual void TearDown() {
+    file_util::Delete(prefs_dir_, true);  // recursive
+  }
+
+  bool SetValue(const string& key, const string& value) {
+    return file_util::WriteFile(prefs_dir_.Append(key),
+                                value.data(), value.length()) ==
+        static_cast<int>(value.length());
+  }
+
+  FilePath prefs_dir_;
+  Prefs prefs_;
+};
+
+TEST_F(PrefsTest, GetFileNameForKey) {
+  const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
+  FilePath path;
+  EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path));
+  EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value());
+}
+
+TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
+  FilePath path;
+  EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path));
+}
+
+TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
+  FilePath path;
+  EXPECT_FALSE(prefs_.GetFileNameForKey("", &path));
+}
+
+TEST_F(PrefsTest, GetString) {
+  const char kKey[] = "test-key";
+  const string test_data = "test data";
+  ASSERT_TRUE(SetValue(kKey, test_data));
+  string value;
+  EXPECT_TRUE(prefs_.GetString(kKey, &value));
+  EXPECT_EQ(test_data, value);
+}
+
+TEST_F(PrefsTest, GetStringBadKey) {
+  string value;
+  EXPECT_FALSE(prefs_.GetString(",bad", &value));
+}
+
+TEST_F(PrefsTest, GetStringNonExistentKey) {
+  string value;
+  EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
+}
+
+TEST_F(PrefsTest, SetString) {
+  const char kKey[] = "my_test_key";
+  const char kValue[] = "some test value\non 2 lines";
+  EXPECT_TRUE(prefs_.SetString(kKey, kValue));
+  string value;
+  EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
+  EXPECT_EQ(kValue, value);
+}
+
+TEST_F(PrefsTest, SetStringBadKey) {
+  const char kKey[] = ".no-dots";
+  EXPECT_FALSE(prefs_.SetString(kKey, "some value"));
+  EXPECT_FALSE(file_util::PathExists(prefs_dir_.Append(kKey)));
+}
+
+TEST_F(PrefsTest, SetStringCreateDir) {
+  const char kKey[] = "a-test-key";
+  const char kValue[] = "test value";
+  EXPECT_TRUE(prefs_.Init(FilePath(prefs_dir_.Append("subdir"))));
+  EXPECT_TRUE(prefs_.SetString(kKey, kValue));
+  string value;
+  EXPECT_TRUE(
+      file_util::ReadFileToString(prefs_dir_.Append("subdir").Append(kKey),
+                                  &value));
+  EXPECT_EQ(kValue, value);
+}
+
+TEST_F(PrefsTest, SetStringDirCreationFailure) {
+  EXPECT_TRUE(prefs_.Init(FilePath("/dev/null")));
+  const char kKey[] = "test-key";
+  EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
+}
+
+TEST_F(PrefsTest, SetStringFileCreationFailure) {
+  const char kKey[] = "a-test-key";
+  file_util::CreateDirectory(prefs_dir_.Append(kKey));
+  EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
+  EXPECT_TRUE(file_util::DirectoryExists(prefs_dir_.Append(kKey)));
+}
+
+TEST_F(PrefsTest, GetInt64) {
+  const char kKey[] = "test-key";
+  ASSERT_TRUE(SetValue(kKey, " \n 25 \t "));
+  int64_t value;
+  EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
+  EXPECT_EQ(25, value);
+}
+
+TEST_F(PrefsTest, GetInt64BadValue) {
+  const char kKey[] = "test-key";
+  ASSERT_TRUE(SetValue(kKey, "30a"));
+  int64_t value;
+  EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
+}
+
+TEST_F(PrefsTest, GetInt64Max) {
+  const char kKey[] = "test-key";
+  ASSERT_TRUE(SetValue(kKey, StringPrintf("%" PRIi64, kint64max)));
+  int64_t value;
+  EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
+  EXPECT_EQ(kint64max, value);
+}
+
+TEST_F(PrefsTest, GetInt64Min) {
+  const char kKey[] = "test-key";
+  ASSERT_TRUE(SetValue(kKey, StringPrintf("%" PRIi64, kint64min)));
+  int64_t value;
+  EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
+  EXPECT_EQ(kint64min, value);
+}
+
+TEST_F(PrefsTest, GetInt64Negative) {
+  const char kKey[] = "test-key";
+  ASSERT_TRUE(SetValue(kKey, " \t -100 \n "));
+  int64_t value;
+  EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
+  EXPECT_EQ(-100, value);
+}
+
+TEST_F(PrefsTest, GetInt64NonExistentKey) {
+  int64_t value;
+  EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
+}
+
+TEST_F(PrefsTest, SetInt64) {
+  const char kKey[] = "test_int";
+  EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
+  string value;
+  EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
+  EXPECT_EQ("-123", value);
+}
+
+TEST_F(PrefsTest, SetInt64BadKey) {
+  const char kKey[] = "s p a c e s";
+  EXPECT_FALSE(prefs_.SetInt64(kKey, 20));
+  EXPECT_FALSE(file_util::PathExists(prefs_dir_.Append(kKey)));
+}
+
+TEST_F(PrefsTest, SetInt64Max) {
+  const char kKey[] = "test-max-int";
+  EXPECT_TRUE(prefs_.SetInt64(kKey, kint64max));
+  string value;
+  EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
+  EXPECT_EQ(StringPrintf("%" PRIi64, kint64max), value);
+}
+
+TEST_F(PrefsTest, SetInt64Min) {
+  const char kKey[] = "test-min-int";
+  EXPECT_TRUE(prefs_.SetInt64(kKey, kint64min));
+  string value;
+  EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
+  EXPECT_EQ(StringPrintf("%" PRIi64, kint64min), value);
+}
+
+}  // namespace chromeos_update_engine