blob: 649832ba57da54a8457e0ad0f5e4a2c1b1c0b3ef [file] [log] [blame]
Alex Deymoedf1a2b2014-09-23 12:05:56 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <chromeos/key_value_store.h>
6
7#include <map>
8#include <string>
Aaron Kemp35babde2015-02-25 11:08:20 -05009#include <vector>
Alex Deymoedf1a2b2014-09-23 12:05:56 -070010
11#include <base/files/file_util.h>
12#include <base/files/scoped_temp_dir.h>
Daniel Erat9ee4e712015-01-27 10:29:07 -070013#include <base/logging.h>
Alex Deymoedf1a2b2014-09-23 12:05:56 -070014#include <base/strings/string_util.h>
Aaron Kemp35babde2015-02-25 11:08:20 -050015#include <chromeos/map_utils.h>
Alex Deymoedf1a2b2014-09-23 12:05:56 -070016#include <gtest/gtest.h>
17
18using base::FilePath;
19using base::ReadFileToString;
20using std::map;
21using std::string;
Aaron Kemp35babde2015-02-25 11:08:20 -050022using std::vector;
Alex Deymoedf1a2b2014-09-23 12:05:56 -070023
24namespace chromeos {
25
26class KeyValueStoreTest : public ::testing::Test {
Alex Deymoedf1a2b2014-09-23 12:05:56 -070027 protected:
Daniel Erat9ee4e712015-01-27 10:29:07 -070028 // Returns the value from |store_| corresponding to |key|, or an empty string
29 // if the key is not present. Crashes if the store returns an empty value.
30 string GetNonemptyStringValue(const string& key) {
31 string value;
32 if (store_.GetString(key, &value))
33 CHECK(!value.empty());
34 return value;
35 }
36
Alex Deymoedf1a2b2014-09-23 12:05:56 -070037 KeyValueStore store_; // KeyValueStore under test.
38};
39
Alex Deymo665bc532015-06-26 19:34:26 -070040TEST_F(KeyValueStoreTest, LoadAndSaveFromFile) {
41 base::ScopedTempDir temp_dir_;
42 CHECK(temp_dir_.CreateUniqueTempDir());
43 base::FilePath temp_file_ = temp_dir_.path().Append("temp.conf");
44 base::FilePath saved_temp_file_ = temp_dir_.path().Append("saved_temp.conf");
45
46 string blob = "A=B\n# Comment\n";
Alex Deymoedf1a2b2014-09-23 12:05:56 -070047 ASSERT_EQ(blob.size(), base::WriteFile(temp_file_, blob.data(), blob.size()));
Daniel Erat9ee4e712015-01-27 10:29:07 -070048 ASSERT_TRUE(store_.Load(temp_file_));
Alex Deymoedf1a2b2014-09-23 12:05:56 -070049
Alex Deymo665bc532015-06-26 19:34:26 -070050 string value;
51 EXPECT_TRUE(store_.GetString("A", &value));
52 EXPECT_EQ("B", value);
53
54 ASSERT_TRUE(store_.Save(saved_temp_file_));
Alex Deymoedf1a2b2014-09-23 12:05:56 -070055 string read_blob;
Alex Deymo665bc532015-06-26 19:34:26 -070056 ASSERT_TRUE(ReadFileToString(FilePath(saved_temp_file_), &read_blob));
Alex Deymoedf1a2b2014-09-23 12:05:56 -070057 EXPECT_EQ("A=B\n", read_blob);
58}
59
Alex Deymo665bc532015-06-26 19:34:26 -070060TEST_F(KeyValueStoreTest, CommentsAreIgnored) {
61 EXPECT_TRUE(store_.LoadFromString(
62 "# comment\nA=B\n\n\n#another=comment\n # leading spaces\n"));
63 EXPECT_EQ("A=B\n", store_.SaveToString());
64}
Alex Deymoedf1a2b2014-09-23 12:05:56 -070065
Alex Deymo665bc532015-06-26 19:34:26 -070066TEST_F(KeyValueStoreTest, EmptyTest) {
67 EXPECT_TRUE(store_.LoadFromString(""));
68 EXPECT_EQ("", store_.SaveToString());
Alex Deymoedf1a2b2014-09-23 12:05:56 -070069}
70
71TEST_F(KeyValueStoreTest, LoadAndReloadTest) {
Alex Deymo665bc532015-06-26 19:34:26 -070072 EXPECT_TRUE(store_.LoadFromString(
73 "A=B\nC=\nFOO=BAR=BAZ\nBAR=BAX\nMISSING=NEWLINE"));
Alex Deymoedf1a2b2014-09-23 12:05:56 -070074
Alex Vakulenko05d29042015-01-13 09:39:25 -080075 map<string, string> expected = {{"A", "B"},
76 {"C", ""},
Alex Vakulenko05d29042015-01-13 09:39:25 -080077 {"FOO", "BAR=BAZ"},
78 {"BAR", "BAX"},
79 {"MISSING", "NEWLINE"}};
Alex Deymoedf1a2b2014-09-23 12:05:56 -070080
Daniel Erat9ee4e712015-01-27 10:29:07 -070081 // Test expected values.
Alex Deymoedf1a2b2014-09-23 12:05:56 -070082 string value;
83 for (const auto& it : expected) {
84 EXPECT_TRUE(store_.GetString(it.first, &value));
85 EXPECT_EQ(it.second, value) << "Testing key: " << it.first;
86 }
87
88 // Save, load and test again.
Alex Deymoedf1a2b2014-09-23 12:05:56 -070089 KeyValueStore new_store;
Alex Deymo665bc532015-06-26 19:34:26 -070090 ASSERT_TRUE(new_store.LoadFromString(store_.SaveToString()));
Alex Deymoedf1a2b2014-09-23 12:05:56 -070091
92 for (const auto& it : expected) {
93 EXPECT_TRUE(new_store.GetString(it.first, &value)) << "key: " << it.first;
94 EXPECT_EQ(it.second, value) << "key: " << it.first;
95 }
96}
97
98TEST_F(KeyValueStoreTest, SimpleBooleanTest) {
99 bool result;
100 EXPECT_FALSE(store_.GetBoolean("A", &result));
101
102 store_.SetBoolean("A", true);
103 EXPECT_TRUE(store_.GetBoolean("A", &result));
104 EXPECT_TRUE(result);
105
106 store_.SetBoolean("A", false);
107 EXPECT_TRUE(store_.GetBoolean("A", &result));
108 EXPECT_FALSE(result);
109}
110
111TEST_F(KeyValueStoreTest, BooleanParsingTest) {
112 string blob = "TRUE=true\nfalse=false\nvar=false\nDONT_SHOUT=TRUE\n";
Alex Deymo665bc532015-06-26 19:34:26 -0700113 EXPECT_TRUE(store_.LoadFromString(blob));
Alex Deymoedf1a2b2014-09-23 12:05:56 -0700114
115 map<string, bool> expected = {
116 {"TRUE", true}, {"false", false}, {"var", false}};
117 bool value;
118 EXPECT_FALSE(store_.GetBoolean("DONT_SHOUT", &value));
119 string str_value;
120 EXPECT_TRUE(store_.GetString("DONT_SHOUT", &str_value));
121
Daniel Erat9ee4e712015-01-27 10:29:07 -0700122 // Test expected values.
Alex Deymoedf1a2b2014-09-23 12:05:56 -0700123 for (const auto& it : expected) {
124 EXPECT_TRUE(store_.GetBoolean(it.first, &value)) << "key: " << it.first;
125 EXPECT_EQ(it.second, value) << "key: " << it.first;
126 }
127}
128
Daniel Erat9ee4e712015-01-27 10:29:07 -0700129TEST_F(KeyValueStoreTest, TrimWhitespaceAroundKey) {
Alex Deymo665bc532015-06-26 19:34:26 -0700130 EXPECT_TRUE(store_.LoadFromString(" a=1\nb =2\n c =3\n"));
Daniel Erat9ee4e712015-01-27 10:29:07 -0700131
132 EXPECT_EQ("1", GetNonemptyStringValue("a"));
133 EXPECT_EQ("2", GetNonemptyStringValue("b"));
134 EXPECT_EQ("3", GetNonemptyStringValue("c"));
135
136 // Keys should also be trimmed when setting new values.
137 store_.SetString(" foo ", "4");
138 EXPECT_EQ("4", GetNonemptyStringValue("foo"));
139
140 store_.SetBoolean(" bar ", true);
141 bool value = false;
142 ASSERT_TRUE(store_.GetBoolean("bar", &value));
143 EXPECT_TRUE(value);
144}
145
146TEST_F(KeyValueStoreTest, IgnoreWhitespaceLine) {
Alex Deymo665bc532015-06-26 19:34:26 -0700147 EXPECT_TRUE(store_.LoadFromString("a=1\n \t \nb=2"));
Daniel Erat9ee4e712015-01-27 10:29:07 -0700148
149 EXPECT_EQ("1", GetNonemptyStringValue("a"));
150 EXPECT_EQ("2", GetNonemptyStringValue("b"));
151}
152
153TEST_F(KeyValueStoreTest, RejectEmptyKeys) {
Alex Deymo665bc532015-06-26 19:34:26 -0700154 EXPECT_FALSE(store_.LoadFromString("=1"));
155 EXPECT_FALSE(store_.LoadFromString(" =2"));
Daniel Erat9ee4e712015-01-27 10:29:07 -0700156
157 // Trying to set an empty (after trimming) key should fail an assert.
158 EXPECT_DEATH(store_.SetString(" ", "3"), "");
159 EXPECT_DEATH(store_.SetBoolean(" ", "4"), "");
160}
161
162TEST_F(KeyValueStoreTest, RejectBogusLines) {
Alex Deymo665bc532015-06-26 19:34:26 -0700163 EXPECT_FALSE(store_.LoadFromString("a=1\nbogus\nb=2"));
Daniel Erat9ee4e712015-01-27 10:29:07 -0700164}
165
166TEST_F(KeyValueStoreTest, MultilineValue) {
Alex Deymo665bc532015-06-26 19:34:26 -0700167 EXPECT_TRUE(store_.LoadFromString("a=foo\nb=bar\\\n baz \\ \nc=3\n"));
Daniel Erat9ee4e712015-01-27 10:29:07 -0700168
169 EXPECT_EQ("foo", GetNonemptyStringValue("a"));
170 EXPECT_EQ("bar baz \\ ", GetNonemptyStringValue("b"));
171 EXPECT_EQ("3", GetNonemptyStringValue("c"));
172}
173
174TEST_F(KeyValueStoreTest, UnterminatedMultilineValue) {
Alex Deymo665bc532015-06-26 19:34:26 -0700175 EXPECT_FALSE(store_.LoadFromString("a=foo\\"));
176 EXPECT_FALSE(store_.LoadFromString("a=foo\\\n"));
177 EXPECT_FALSE(store_.LoadFromString("a=foo\\\n\n# blah\n"));
Daniel Erat9ee4e712015-01-27 10:29:07 -0700178}
179
Aaron Kemp35babde2015-02-25 11:08:20 -0500180TEST_F(KeyValueStoreTest, GetKeys) {
181 map<string, string> entries = {
182 {"1", "apple"}, {"2", "banana"}, {"3", "cherry"}
183 };
184 for (const auto& it : entries) {
185 store_.SetString(it.first, it.second);
186 }
187
188 vector<string> keys = GetMapKeysAsVector(entries);
189 EXPECT_EQ(keys, store_.GetKeys());
190}
191
Alex Deymoedf1a2b2014-09-23 12:05:56 -0700192} // namespace chromeos