From a2f6bd1d0bd35bf467a6eaea1224e5d0a8d7582f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cristi=C3=A1n=20Donoso?= <donosoc@google.com>
Date: Mon, 13 May 2019 17:31:24 +0000
Subject: [PATCH] [debugger] Storing list now validates against the schema
 options.

TEST=unit

Change-Id: I95bc9a9b73387ea4a2b06cce212a1b8b8a8b4ad8
---
 .../debug/zxdb/client/setting_schema.cc        | 14 ++++++++++++++
 .../zxdb/client/setting_store_unittest.cc      | 18 ++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/src/developer/debug/zxdb/client/setting_schema.cc b/src/developer/debug/zxdb/client/setting_schema.cc
index ed852094dec..c0a29a4e413 100644
--- a/src/developer/debug/zxdb/client/setting_schema.cc
+++ b/src/developer/debug/zxdb/client/setting_schema.cc
@@ -96,6 +96,20 @@ Err SettingSchema::ValidateSetting(const std::string& key,
         SettingTypeToString(setting.setting.value.type));
   }
 
+  if (setting.setting.value.is_list() && !setting.options.empty()) {
+    for (auto& item : value.get_list()) {
+      bool found = false;
+      for (auto& option : setting.options) {
+        if (item == option) {
+          found = true;
+          break;
+        }
+      }
+      if (!found)
+        return Err("Option \"%s\" is not a valid option", item.c_str());
+    }
+  }
+
   return Err();
 }
 
diff --git a/src/developer/debug/zxdb/client/setting_store_unittest.cc b/src/developer/debug/zxdb/client/setting_store_unittest.cc
index 61c03660a85..271d203d778 100644
--- a/src/developer/debug/zxdb/client/setting_store_unittest.cc
+++ b/src/developer/debug/zxdb/client/setting_store_unittest.cc
@@ -27,6 +27,11 @@ fxl::RefPtr<SettingSchema> GetSchema() {
     FXL_NOTREACHED() << "Schema should be valid!";
     return nullptr;
   }
+  if (!schema->AddList("list_with_options", "list_with_options", {},
+                       DefaultList())) {
+    FXL_NOTREACHED() << "Schema should be valid!";
+    return nullptr;
+  }
   return schema;
 }
 
@@ -98,6 +103,19 @@ TEST(SettingStore, Overrides) {
   EXPECT_EQ(store.GetInt("int"), kNewInt);
 }
 
+TEST(SettingStore, ListOptions) {
+  Err err;
+  SettingStore store(GetSchema(), nullptr);
+
+  // Attemp to add a valid item to the list with options.
+  err = store.SetList("list_with_options", {kDefaultString});
+  EXPECT_FALSE(err.has_error()) << err.msg();
+
+  // Add an option that doesn't exist.
+  err = store.SetList("list_with_options", {"some_weird_option"});
+  EXPECT_TRUE(err.has_error());
+}
+
 TEST(SettingStore, Fallback) {
   SettingStore fallback2(GetSchema(), nullptr);
   std::vector<std::string> new_list = {"new", "list"};
-- 
GitLab