buffet: Added local_discovery_enabled and local_pairing_enabled options

Options will be added to base state and modified by
base.updateBaseConfiguration command.
Renamed anonymous_access_role to local_anonymous_access_role for
consistency.

BUG=brillo:810
TEST='FEATURES=test emerge-storm buffet'

Change-Id: I97476d75260898fea110652b85019d6eaa9cc58a
Reviewed-on: https://chromium-review.googlesource.com/270808
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/buffet_config.cc b/buffet/buffet_config.cc
index bcab1ef..a97b654 100644
--- a/buffet/buffet_config.cc
+++ b/buffet/buffet_config.cc
@@ -57,7 +57,9 @@
 const char kName[] = "name";
 const char kDescription[] = "description";
 const char kLocation[] = "location";
-const char kAnonymousAccessRole[] = "anonymous_access_role";
+const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role";
+const char kLocalDiscoveryEnabled[] = "local_discovery_enabled";
+const char kLocalPairingEnabled[] = "local_pairing_enabled";
 const char kOemName[] = "oem_name";
 const char kModelName[] = "model_name";
 const char kModelId[] = "model_id";
@@ -107,22 +109,32 @@
   store.GetString(config_keys::kDescription, &description_);
   store.GetString(config_keys::kLocation, &location_);
 
-  store.GetString(config_keys::kAnonymousAccessRole, &anonymous_access_role_);
-  CHECK(IsValidAccessRole(anonymous_access_role_))
-      << "Invalid role: " << anonymous_access_role_;
+  store.GetString(config_keys::kLocalAnonymousAccessRole,
+                  &local_anonymous_access_role_);
+  CHECK(IsValidAccessRole(local_anonymous_access_role_))
+      << "Invalid role: " << local_anonymous_access_role_;
+
+  store.GetBoolean(config_keys::kLocalDiscoveryEnabled,
+                   &local_discovery_enabled_);
+  store.GetBoolean(config_keys::kLocalPairingEnabled, &local_pairing_enabled_);
 }
 
-void BuffetConfig::set_name(const std::string& name) {
-  CHECK(!name.empty());
-  name_ = name;
-}
-
-void BuffetConfig::set_anonymous_access_role(const std::string& role) {
-  if (IsValidAccessRole(role)) {
-    anonymous_access_role_ = role;
-  } else {
-    LOG(ERROR) << "Invalid role: " << role;
+bool BuffetConfig::set_name(const std::string& name) {
+  if (name.empty()) {
+    LOG(ERROR) << "Invalid name: " << name;
+    return false;
   }
+  name_ = name;
+  return true;
+}
+
+bool BuffetConfig::set_local_anonymous_access_role(const std::string& role) {
+  if (!IsValidAccessRole(role)) {
+    LOG(ERROR) << "Invalid role: " << role;
+    return false;
+  }
+  local_anonymous_access_role_ = role;
+  return true;
 }
 
 }  // namespace buffet
diff --git a/buffet/buffet_config.h b/buffet/buffet_config.h
index e3fc6ba..0ebdf8e 100644
--- a/buffet/buffet_config.h
+++ b/buffet/buffet_config.h
@@ -33,14 +33,25 @@
   const std::string& name() const { return name_; }
   const std::string& description() const { return description_; }
   const std::string& location() const { return location_; }
-  std::string anonymous_access_role() const { return anonymous_access_role_; }
+  std::string local_anonymous_access_role() const {
+    return local_anonymous_access_role_;
+  }
+  bool local_pairing_enabled() const { return local_pairing_enabled_; }
+  bool local_discovery_enabled() const { return local_discovery_enabled_; }
 
-  void set_name(const std::string& name);
+  bool set_name(const std::string& name);
   void set_description(const std::string& description) {
     description_ = description;
   }
   void set_location(const std::string& location) { location_ = location; }
-  void set_anonymous_access_role(const std::string& role);
+
+  bool set_local_anonymous_access_role(const std::string& role);
+  void set_local_discovery_enabled(bool enabled) {
+    local_discovery_enabled_ = enabled;
+  }
+  void set_local_pairing_enabled(bool enabled) {
+    local_pairing_enabled_ = enabled;
+  }
 
  private:
   std::string client_id_{"58855907228.apps.googleusercontent.com"};
@@ -51,7 +62,9 @@
   std::string name_{"Developer device"};
   std::string description_;
   std::string location_;
-  std::string anonymous_access_role_{"viewer"};
+  std::string local_anonymous_access_role_{"viewer"};
+  bool local_discovery_enabled_{true};
+  bool local_pairing_enabled_{true};
   std::string oem_name_{"Chromium"};
   std::string model_name_{"Brillo"};
   std::string model_id_{"AAAAA"};
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index 80e7e9b..2fd2bfb 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -45,7 +45,9 @@
 const char kName[] = "name";
 const char kDescription[] = "description";
 const char kLocation[] = "location";
-const char kAnonymousAccessRole[] = "anonymous_access_role";
+const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role";
+const char kLocalDiscoveryEnabled[] = "local_discovery_enabled";
+const char kLocalPairingEnabled[] = "local_pairing_enabled";
 
 }  // namespace storage_keys
 }  // namespace buffet
@@ -189,7 +191,7 @@
 
   // Read all available data before failing.
   std::string name;
-  if (dict->GetString(storage_keys::kName, &name) && !name.empty())
+  if (dict->GetString(storage_keys::kName, &name))
     config_->set_name(name);
 
   std::string description;
@@ -201,8 +203,18 @@
     config_->set_location(location);
 
   std::string access_role;
-  if (dict->GetString(storage_keys::kAnonymousAccessRole, &access_role))
-    config_->set_anonymous_access_role(access_role);
+  if (dict->GetString(storage_keys::kLocalAnonymousAccessRole, &access_role))
+    config_->set_local_anonymous_access_role(access_role);
+
+  bool local_discovery_enabled{false};
+  if (dict->GetBoolean(storage_keys::kLocalDiscoveryEnabled,
+                       &local_discovery_enabled))
+    config_->set_local_discovery_enabled(local_discovery_enabled);
+
+  bool local_pairing_enabled{false};
+  if (dict->GetBoolean(storage_keys::kLocalPairingEnabled,
+                       &local_pairing_enabled))
+    config_->set_local_pairing_enabled(local_pairing_enabled);
 
   dict->GetString(storage_keys::kRefreshToken, &refresh_token_);
   dict->GetString(storage_keys::kRobotAccount, &device_robot_account_);
@@ -233,8 +245,12 @@
   dict.SetString(storage_keys::kName, config_->name());
   dict.SetString(storage_keys::kDescription, config_->description());
   dict.SetString(storage_keys::kLocation, config_->location());
-  dict.SetString(storage_keys::kAnonymousAccessRole,
-                 config_->anonymous_access_role());
+  dict.SetString(storage_keys::kLocalAnonymousAccessRole,
+                 config_->local_anonymous_access_role());
+  dict.SetBoolean(storage_keys::kLocalDiscoveryEnabled,
+                  config_->local_discovery_enabled());
+  dict.SetBoolean(storage_keys::kLocalPairingEnabled,
+                  config_->local_pairing_enabled());
 
   return storage_->Save(dict);
 }
@@ -765,17 +781,16 @@
                                               const std::string& description,
                                               const std::string& location,
                                               chromeos::ErrorPtr* error) {
-  if (name.empty()) {
-    chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainBuffet,
-                           "invalid_parameter", "Empty device name");
+  if (!config_->set_name(name)) {
+    chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomainBuffet,
+                                 "invalid_parameter", "Invalid name: %s",
+                                 name.c_str());
     return false;
   }
-  config_->set_name(name);
   config_->set_description(description);
   config_->set_location(location);
 
   Save();
-
   OnConfigChanged();
 
   if (HaveRegistrationCredentials(nullptr)) {
@@ -1040,7 +1055,7 @@
   manager_->SetName(config_->name());
   manager_->SetDescription(config_->description());
   manager_->SetLocation(config_->location());
-  manager_->SetAnonymousAccessRole(config_->anonymous_access_role());
+  manager_->SetAnonymousAccessRole(config_->local_anonymous_access_role());
 }
 
 void DeviceRegistrationInfo::OnCommandDefsChanged() {
diff --git a/buffet/device_registration_info_unittest.cc b/buffet/device_registration_info_unittest.cc
index 417aff4..934ae51 100644
--- a/buffet/device_registration_info_unittest.cc
+++ b/buffet/device_registration_info_unittest.cc
@@ -198,7 +198,9 @@
     config_store.SetString("name",  "Coffee Pot");
     config_store.SetString("description", "Easy to clean");
     config_store.SetString("location", "Kitchen");
-    config_store.SetString("anonymous_access_role", "viewer");
+    config_store.SetString("local_anonymous_access_role", "viewer");
+    config_store.SetBoolean("local_local_discovery_enabled", true);
+    config_store.SetBoolean("local_local_pairing_enabled", false);
     config_store.SetString("model_id", "AAAAA");
     config_store.SetString("oauth_url", test_data::kOAuthURL);
     config_store.SetString("service_url", test_data::kServiceURL);
@@ -241,16 +243,19 @@
 }
 
 TEST_F(DeviceRegistrationInfoTest, VerifySave) {
-  base::DictionaryValue data;
-  data.SetString(storage_keys::kRefreshToken, "d");
-  data.SetString(storage_keys::kDeviceId, "e");
-  data.SetString(storage_keys::kRobotAccount, "h");
-  data.SetString(storage_keys::kName, "k");
-  data.SetString(storage_keys::kDescription, "l");
-  data.SetString(storage_keys::kLocation, "m");
-  data.SetString(storage_keys::kAnonymousAccessRole, "user");
+  auto expected = R"({
+     'description': 'l',
+     'device_id': 'e',
+     'local_anonymous_access_role': 'user',
+     'local_discovery_enabled': true,
+     'local_pairing_enabled': true,
+     'location': 'm',
+     'name': 'k',
+     'refresh_token': 'd',
+     'robot_account': 'h'
+  })";
 
-  storage_->Save(data);
+  storage_->Save(*unittests::CreateDictionaryValue(expected));
 
   // This test isn't really trying to test Load, it is just the easiest
   // way to initialize the properties in dev_reg_.
@@ -260,7 +265,8 @@
   base::DictionaryValue empty;
   storage_->Save(empty);
   EXPECT_TRUE(DeviceRegistrationInfo::TestHelper::Save(dev_reg_.get()));
-  EXPECT_TRUE(storage_->Load()->Equals(&data));
+
+  EXPECT_JSON_EQ(expected, *storage_->Load());
 }
 
 TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) {
diff --git a/buffet/device_registration_storage_keys.h b/buffet/device_registration_storage_keys.h
index d48bbf8..5ce8083 100644
--- a/buffet/device_registration_storage_keys.h
+++ b/buffet/device_registration_storage_keys.h
@@ -17,7 +17,9 @@
 extern const char kName[];
 extern const char kDescription[];
 extern const char kLocation[];
-extern const char kAnonymousAccessRole[];
+extern const char kLocalAnonymousAccessRole[];
+extern const char kLocalDiscoveryEnabled[];
+extern const char kLocalPairingEnabled[];
 
 }  // namespace storage_keys
 }  // namespace buffet