From e883ea5de1c75ce52b69a11fade5c6a523a94a0f Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Thu, 12 Oct 2023 12:33:01 +0200 Subject: [PATCH 1/2] test/kv-store: add test case for quoted values Useful to test when the value contains whitespaces surrounded with quotes/double-quotes. Signed-off-by: Mathieu Tortuyaux --- src/update_engine/simple_key_value_store_unittest.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/update_engine/simple_key_value_store_unittest.cc b/src/update_engine/simple_key_value_store_unittest.cc index 341bfd4..2987537 100644 --- a/src/update_engine/simple_key_value_store_unittest.cc +++ b/src/update_engine/simple_key_value_store_unittest.cc @@ -18,7 +18,7 @@ namespace chromeos_update_engine { class SimpleKeyValueStoreTest : public ::testing::Test {}; TEST(SimpleKeyValueStoreTest, SimpleTest) { - string blob = "A=B\nC=\n=\nFOO=BAR=BAZ\nBAR=BAX\nMISSING=NEWLINE"; + string blob = "A=B\nC=\n=\nFOO=BAR=BAZ\nALIAS='my alias'\nEMPTY=''\nD=\"\"\nTEST=\"this is a test\"\nBAR=BAX\nMISSING=NEWLINE"; map parts = simple_key_value_store::ParseString(blob); string combined = simple_key_value_store::AssembleString(parts); map combined_parts = @@ -26,13 +26,17 @@ TEST(SimpleKeyValueStoreTest, SimpleTest) { map* maps[] = { &parts, &combined_parts }; for (size_t i = 0; i < arraysize(maps); i++) { map* test_map = maps[i]; - EXPECT_EQ(6, test_map->size()) << "i = " << i; + EXPECT_EQ(10, test_map->size()) << "i = " << i; EXPECT_EQ("B", (*test_map)["A"]) << "i = " << i; EXPECT_EQ("", (*test_map)["C"]) << "i = " << i; EXPECT_EQ("", (*test_map)[""]) << "i = " << i; EXPECT_EQ("BAR=BAZ", (*test_map)["FOO"]) << "i = " << i; EXPECT_EQ("BAX", (*test_map)["BAR"]) << "i = " << i; EXPECT_EQ("NEWLINE", (*test_map)["MISSING"]) << "i = " << i; + EXPECT_EQ("my alias", (*test_map)["ALIAS"]) << "i = " << i; + EXPECT_EQ("this is a test", (*test_map)["TEST"]) << "i = " << i; + EXPECT_EQ("", (*test_map)["EMPTY"]) << "i = " << i; + EXPECT_EQ("", (*test_map)["D"]) << "i = " << i; } } From db448f97e93c10580100bf6a22a1924396288bdf Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Thu, 12 Oct 2023 12:36:02 +0200 Subject: [PATCH 2/2] kv-store: support quoted values In the case of the value contains whitespaces, it has to be surrounded with quotes (single or double). We should exclude these characters from the final values. Signed-off-by: Mathieu Tortuyaux --- src/update_engine/simple_key_value_store.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/update_engine/simple_key_value_store.cc b/src/update_engine/simple_key_value_store.cc index eb6a952..7042917 100644 --- a/src/update_engine/simple_key_value_store.cc +++ b/src/update_engine/simple_key_value_store.cc @@ -27,7 +27,14 @@ map ParseString(const string& str) { string::size_type pos = it->find('='); if (pos == string::npos) continue; - ret[it->substr(0, pos)] = it->substr(pos + 1); + string val = it->substr(pos + 1); + if ((val.length() >= 2) && + ((val.front() == '\"' && val.back() == '\"') || + (val.front() == '\'' && val.back() == '\''))) { + val = val.substr(1, val.length() - 2); + } + + ret[it->substr(0, pos)] = val; } return ret; }