libchromeos: add API to query /etc/os-release

/etc/os-release fields can come from two different places depending on how we
set them, how the package is installed, etc...
This creates a common API to query field in /etc/os-release and
/etc/os-release.d.

BUG=chromium:420784
TEST=Unittests.

Change-Id: Ic91fd873cd4f6c5e3357991e72d055a12b54c9d1
Reviewed-on: https://chromium-review.googlesource.com/221963
Tested-by: Bertrand Simonnet <bsimonnet@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Bertrand Simonnet <bsimonnet@chromium.org>
diff --git a/chromeos/osrelease_reader.cc b/chromeos/osrelease_reader.cc
new file mode 100644
index 0000000..e6129fe
--- /dev/null
+++ b/chromeos/osrelease_reader.cc
@@ -0,0 +1,58 @@
+// Copyright 2014 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 <chromeos/osrelease_reader.h>
+
+#include <base/files/file_enumerator.h>
+#include <base/files/file_util.h>
+#include <base/logging.h>
+#include <chromeos/strings/string_utils.h>
+
+namespace chromeos {
+
+bool OsReleaseReader::Load() {
+  return Load(base::FilePath("/"));
+}
+
+bool OsReleaseReader::GetString(const std::string& key,
+                                std::string* value) const {
+  CHECK(initialized_) << "OsReleaseReader.Load() must be called first.";
+  return store_.GetString(key, value);
+}
+
+bool OsReleaseReader::LoadTestingOnly(const base::FilePath& root_dir) {
+  return Load(root_dir);
+}
+
+bool OsReleaseReader::Load(const base::FilePath& root_dir) {
+  base::FilePath osrelease = root_dir.Append("etc").Append("os-release");
+  if (!store_.Load(osrelease)) {
+    // /etc/os-release might not be present (cros deploying a new configuration
+    // or no fields set at all). Just print a debug message and continue.
+    DLOG(INFO) << "Could not load fields from " << osrelease.value();
+  }
+
+  base::FilePath osreleased = root_dir.Append("etc").Append("os-release.d");
+  base::FileEnumerator enumerator(osreleased,
+                                  false,
+                                  base::FileEnumerator::FILES);
+
+  for (base::FilePath path = enumerator.Next(); !path.empty();
+       path = enumerator.Next()) {
+    std::string content;
+    if (!base::ReadFileToString(path, &content)) {
+      // The only way to fail is if a file exist in /etc/os-release.d but we
+      // cannot read it.
+      PLOG(FATAL) << "Could not read " << path.value();
+    }
+    // There might be a trailing new line. Strip it to keep only the first line
+    // of the file.
+    content = chromeos::string_utils::SplitAtFirst(content, '\n', true).first;
+    store_.SetString(path.BaseName().value(), content);
+  }
+  initialized_ = true;
+  return true;
+}
+
+}  // namespace chromeos
diff --git a/chromeos/osrelease_reader.h b/chromeos/osrelease_reader.h
new file mode 100644
index 0000000..8e8c4d0
--- /dev/null
+++ b/chromeos/osrelease_reader.h
@@ -0,0 +1,55 @@
+// Copyright 2014 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.
+
+// Wrapper around /etc/os-release and /etc/os-release.d.
+// Standard fields can come from both places depending on how we set them. They
+// should always be accessed through this interface.
+
+#ifndef LIBCHROMEOS_CHROMEOS_OSRELEASE_READER_H_
+#define LIBCHROMEOS_CHROMEOS_OSRELEASE_READER_H_
+
+#include <string>
+
+#include <chromeos/chromeos_export.h>
+#include <chromeos/key_value_store.h>
+#include <gtest/gtest_prod.h>
+
+namespace chromeos {
+
+class CHROMEOS_EXPORT OsReleaseReader {
+ public:
+  // Create an empty reader
+  OsReleaseReader() = default;
+
+  // Loads the key=value pairs from either /etc/os-release.d/<KEY> or
+  // /etc/os-release.
+  // Returns false on errors.
+  bool Load();
+
+  // Same as the private Load method.
+  // This need to be public so that services can use it in testing mode (for
+  // autotest tests for example).
+  // This should not be used in production so suffix it with TestingOnly to
+  // make it obvious.
+  bool LoadTestingOnly(const base::FilePath& root_dir);
+
+  // Getter for the given key. Returns whether the key was found on the store.
+  bool GetString(const std::string& key, std::string* value) const;
+
+ private:
+  // The map storing all the key-value pairs.
+  KeyValueStore store_;
+
+  // os-release can be lazily loaded if need be.
+  bool initialized_;
+
+  // Load the data from a given root_dir. Return false on errors.
+  CHROMEOS_PRIVATE bool Load(const base::FilePath& root_dir);
+
+  DISALLOW_COPY_AND_ASSIGN(OsReleaseReader);
+};
+
+}  // namespace chromeos
+
+#endif  // LIBCHROMEOS_CHROMEOS_OSRELEASE_READER_H_
diff --git a/chromeos/osrelease_reader_unittest.cc b/chromeos/osrelease_reader_unittest.cc
new file mode 100644
index 0000000..92bfde6
--- /dev/null
+++ b/chromeos/osrelease_reader_unittest.cc
@@ -0,0 +1,96 @@
+// Copyright 2014 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 <chromeos/osrelease_reader.h>
+
+#include <base/files/file_util.h>
+#include <base/files/scoped_temp_dir.h>
+#include <gtest/gtest.h>
+
+using std::string;
+
+namespace chromeos {
+
+class OsReleaseReaderTest : public ::testing::Test {
+ public:
+  void SetUp() override {
+    CHECK(temp_dir_.CreateUniqueTempDir());
+    osreleased_ = temp_dir_.path().Append("etc").Append("os-release.d");
+    osrelease_ = temp_dir_.path().Append("etc").Append("os-release");
+    base::CreateDirectory(osreleased_);
+  }
+
+ protected:
+  base::FilePath temp_file_, osrelease_, osreleased_;
+  base::ScopedTempDir temp_dir_;
+  OsReleaseReader store_;  // reader under test.
+};
+
+TEST_F(OsReleaseReaderTest, MissingOsReleaseTest) {
+  ASSERT_TRUE(store_.LoadTestingOnly(temp_dir_.path()));
+}
+
+TEST_F(OsReleaseReaderTest, MissingOsReleaseDTest) {
+  base::DeleteFile(osreleased_, true);
+  ASSERT_TRUE(store_.LoadTestingOnly(temp_dir_.path()));
+}
+
+TEST_F(OsReleaseReaderTest, CompleteTest) {
+  string hello = "hello";
+  string ola = "ola";
+  string bob = "bob";
+  string osreleasecontent = "TEST_KEY=bonjour\nNAME=bob\n";
+
+  base::WriteFile(osreleased_.Append("TEST_KEY"), hello.data(), hello.size());
+  base::WriteFile(osreleased_.Append("GREETINGS"), ola.data(), ola.size());
+  base::WriteFile(osrelease_, osreleasecontent.data(), osreleasecontent.size());
+
+  ASSERT_TRUE(store_.LoadTestingOnly(temp_dir_.path()));
+
+  string test_key_value;
+  ASSERT_TRUE(store_.GetString("TEST_KEY", &test_key_value));
+
+  string greetings_value;
+  ASSERT_TRUE(store_.GetString("GREETINGS", &greetings_value));
+
+  string name_value;
+  ASSERT_TRUE(store_.GetString("NAME", &name_value));
+
+  string nonexistent_value;
+  // Getting the string should fail if the key does not exist.
+  ASSERT_FALSE(store_.GetString("DOES_NOT_EXIST", &nonexistent_value));
+
+  // hello in chosen (from os-release.d) instead of bonjour from os-release.
+  ASSERT_EQ(hello, test_key_value);
+
+  // greetings is set to ola.
+  ASSERT_EQ(ola, greetings_value);
+
+  // Name from os-release is set.
+  ASSERT_EQ(bob, name_value);
+}
+
+TEST_F(OsReleaseReaderTest, NoNewLine) {
+  // New lines should be stripped from os-release.d files.
+  string hello = "hello\n";
+  string bonjour = "bonjour\ngarbage";
+
+  base::WriteFile(osreleased_.Append("HELLO"), hello.data(), hello.size());
+  base::WriteFile(osreleased_.Append("BONJOUR"),
+                  bonjour.data(),
+                  bonjour.size());
+
+  ASSERT_TRUE(store_.LoadTestingOnly(temp_dir_.path()));
+
+  string hello_value;
+  string bonjour_value;
+
+  ASSERT_TRUE(store_.GetString("HELLO", &hello_value));
+  ASSERT_TRUE(store_.GetString("BONJOUR", &bonjour_value));
+
+  ASSERT_EQ("hello", hello_value);
+  ASSERT_EQ("bonjour", bonjour_value);
+}
+
+}  // namespace chromeos