Use the prefix in product_id for system_id.

If product id is "android-things-test:123", but system id is
"android-things:som-rpi3", we change the system id to
"android-things-test:som-rpi3".

This is needed for devconsole testing to isolate test environment
with production.

Also added unittest for image_properties_android.cc

Bug: 62466250
Test: check system_id in Omaha request
Test: update_engine_unittests

Change-Id: Ib97573e117c0fecf85922260e6e75e63e36c18e3
(cherry picked from commit 98c0751f0dc970cadfc8014883018524dd8899f1)
diff --git a/Android.mk b/Android.mk
index 16c6381..67a1de4 100644
--- a/Android.mk
+++ b/Android.mk
@@ -984,6 +984,7 @@
 LOCAL_SRC_FILES += \
     common_service_unittest.cc \
     fake_system_state.cc \
+    image_properties_android_unittest.cc \
     metrics_utils_unittest.cc \
     omaha_request_action_unittest.cc \
     omaha_request_params_unittest.cc \
diff --git a/image_properties_android.cc b/image_properties_android.cc
index e815dbf..d52c40b 100644
--- a/image_properties_android.cc
+++ b/image_properties_android.cc
@@ -20,6 +20,7 @@
 
 #include <base/logging.h>
 #include <brillo/osrelease_reader.h>
+#include <brillo/strings/string_utils.h>
 #include <cutils/properties.h>
 
 #include "update_engine/common/boot_control_interface.h"
@@ -28,6 +29,8 @@
 #include "update_engine/common/prefs_interface.h"
 #include "update_engine/system_state.h"
 
+using std::string;
+
 namespace chromeos_update_engine {
 
 namespace {
@@ -47,10 +50,13 @@
 const char kPropBuildFingerprint[] = "ro.build.fingerprint";
 const char kPropBuildType[] = "ro.build.type";
 
-std::string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
-                                 const std::string& key,
-                                 const std::string& default_value) {
-  std::string result;
+// A prefix added to the path, used for testing.
+const char* root_prefix = nullptr;
+
+string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
+                            const string& key,
+                            const string& default_value) {
+  string result;
   if (osrelease.GetString(key, &result))
     return result;
   LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
@@ -61,25 +67,35 @@
 }  // namespace
 
 namespace test {
-void SetImagePropertiesRootPrefix(const char* /* test_root_prefix */) {}
+void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
+  root_prefix = test_root_prefix;
+}
 }  // namespace test
 
 ImageProperties LoadImageProperties(SystemState* system_state) {
   ImageProperties result;
 
   brillo::OsReleaseReader osrelease;
-  osrelease.Load();
+  if (root_prefix)
+    osrelease.LoadTestingOnly(base::FilePath(root_prefix));
+  else
+    osrelease.Load();
   result.product_id =
       GetStringWithDefault(osrelease, kProductId, "invalid-product");
   result.system_id = GetStringWithDefault(
       osrelease, kSystemId, "developer-boards:brillo-starter-board");
+  // Update the system id to match the prefix of product id for testing.
+  string prefix, not_used, system_id;
+  if (brillo::string_utils::SplitAtFirst(
+          result.product_id, ":", &prefix, &not_used, false) &&
+      brillo::string_utils::SplitAtFirst(
+          result.system_id, ":", &not_used, &system_id, false)) {
+    result.system_id = prefix + ":" + system_id;
+  }
   result.canary_product_id = result.product_id;
-  std::string system_version =
+  result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
+  result.system_version =
       GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0");
-  std::string product_version =
-      GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
-  result.version = product_version;
-  result.system_version = system_version;
 
   char prop[PROPERTY_VALUE_MAX];
   property_get(kPropProductName, prop, "brillo");
@@ -95,10 +111,10 @@
   // channel where we got the image from in prefs at the time of the update, so
   // we use that as the current channel if available. During provisioning, there
   // is no value assigned, so we default to the "stable-channel".
-  std::string current_channel_key =
+  string current_channel_key =
       kPrefsChannelOnSlotPrefix +
       std::to_string(system_state->boot_control()->GetCurrentSlot());
-  std::string current_channel;
+  string current_channel;
   if (!system_state->prefs()->Exists(current_channel_key) ||
       !system_state->prefs()->GetString(current_channel_key, &current_channel))
     current_channel = "stable-channel";
diff --git a/image_properties_android_unittest.cc b/image_properties_android_unittest.cc
new file mode 100644
index 0000000..9bbb8b0
--- /dev/null
+++ b/image_properties_android_unittest.cc
@@ -0,0 +1,90 @@
+//
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/image_properties.h"
+
+#include <string>
+
+#include <base/files/file_util.h>
+#include <base/files/scoped_temp_dir.h>
+#include <gtest/gtest.h>
+
+#include "update_engine/common/constants.h"
+#include "update_engine/common/test_utils.h"
+#include "update_engine/fake_system_state.h"
+
+using chromeos_update_engine::test_utils::WriteFileString;
+using std::string;
+
+namespace chromeos_update_engine {
+
+class ImagePropertiesTest : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    // Create a uniquely named test directory.
+    ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
+    osrelease_dir_ = tempdir_.path().Append("etc/os-release.d");
+    EXPECT_TRUE(base::CreateDirectory(osrelease_dir_));
+    test::SetImagePropertiesRootPrefix(tempdir_.path().value().c_str());
+  }
+
+  void WriteOsRelease(const string& key, const string& value) {
+    ASSERT_TRUE(WriteFileString(osrelease_dir_.Append(key).value(), value));
+  }
+
+  FakeSystemState fake_system_state_;
+
+  base::ScopedTempDir tempdir_;
+  base::FilePath osrelease_dir_;
+};
+
+TEST_F(ImagePropertiesTest, SimpleTest) {
+  WriteOsRelease("product_id", "abc");
+  WriteOsRelease("system_id", "def");
+  WriteOsRelease("product_version", "1.2.3.4");
+  WriteOsRelease("system_version", "5.6.7.8");
+  ImageProperties props = LoadImageProperties(&fake_system_state_);
+  EXPECT_EQ("abc", props.product_id);
+  EXPECT_EQ("def", props.system_id);
+  EXPECT_EQ("1.2.3.4", props.version);
+  EXPECT_EQ("5.6.7.8", props.system_version);
+  EXPECT_EQ("stable-channel", props.current_channel);
+  EXPECT_EQ(constants::kOmahaDefaultProductionURL, props.omaha_url);
+}
+
+TEST_F(ImagePropertiesTest, IDPrefixTest) {
+  WriteOsRelease("product_id", "abc:def");
+  WriteOsRelease("system_id", "foo:bar");
+  ImageProperties props = LoadImageProperties(&fake_system_state_);
+  EXPECT_EQ("abc:def", props.product_id);
+  EXPECT_EQ("abc:bar", props.system_id);
+}
+
+TEST_F(ImagePropertiesTest, IDInvalidPrefixTest) {
+  WriteOsRelease("product_id", "def");
+  WriteOsRelease("system_id", "foo:bar");
+  ImageProperties props = LoadImageProperties(&fake_system_state_);
+  EXPECT_EQ("def", props.product_id);
+  EXPECT_EQ("foo:bar", props.system_id);
+
+  WriteOsRelease("product_id", "abc:def");
+  WriteOsRelease("system_id", "bar");
+  props = LoadImageProperties(&fake_system_state_);
+  EXPECT_EQ("abc:def", props.product_id);
+  EXPECT_EQ("bar", props.system_id);
+}
+
+}  // namespace chromeos_update_engine