Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1063)

Side by Side Diff: prefs_unittest.cc

Issue 3014035: AU: Add support for persistent update engine preferences store. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/update_engine.git
Patch Set: test directory creation Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « prefs_interface.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // 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 <inttypes.h>
6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/file_util.h"
11 #include "base/string_util.h"
12 #include "gtest/gtest.h"
13 #include "update_engine/prefs.h"
14
15 using std::string;
16
17 namespace chromeos_update_engine {
18
19 class PrefsTest : public ::testing::Test {
20 protected:
21 virtual void SetUp() {
22 ASSERT_TRUE(file_util::CreateNewTempDirectory("auprefs", &prefs_dir_));
23 ASSERT_TRUE(prefs_.Init(prefs_dir_));
24 }
25
26 virtual void TearDown() {
27 file_util::Delete(prefs_dir_, true); // recursive
28 }
29
30 bool SetValue(const string& key, const string& value) {
31 return file_util::WriteFile(prefs_dir_.Append(key),
32 value.data(), value.length()) ==
33 static_cast<int>(value.length());
34 }
35
36 FilePath prefs_dir_;
37 Prefs prefs_;
38 };
39
40 TEST_F(PrefsTest, GetFileNameForKey) {
41 const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
42 FilePath path;
43 EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path));
44 EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value());
45 }
46
47 TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
48 FilePath path;
49 EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path));
50 }
51
52 TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
53 FilePath path;
54 EXPECT_FALSE(prefs_.GetFileNameForKey("", &path));
55 }
56
57 TEST_F(PrefsTest, GetString) {
58 const char kKey[] = "test-key";
59 const string test_data = "test data";
60 ASSERT_TRUE(SetValue(kKey, test_data));
61 string value;
62 EXPECT_TRUE(prefs_.GetString(kKey, &value));
63 EXPECT_EQ(test_data, value);
64 }
65
66 TEST_F(PrefsTest, GetStringBadKey) {
67 string value;
68 EXPECT_FALSE(prefs_.GetString(",bad", &value));
69 }
70
71 TEST_F(PrefsTest, GetStringNonExistentKey) {
72 string value;
73 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
74 }
75
76 TEST_F(PrefsTest, SetString) {
77 const char kKey[] = "my_test_key";
78 const char kValue[] = "some test value\non 2 lines";
79 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
80 string value;
81 EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
82 EXPECT_EQ(kValue, value);
83 }
84
85 TEST_F(PrefsTest, SetStringBadKey) {
86 const char kKey[] = ".no-dots";
87 EXPECT_FALSE(prefs_.SetString(kKey, "some value"));
88 EXPECT_FALSE(file_util::PathExists(prefs_dir_.Append(kKey)));
89 }
90
91 TEST_F(PrefsTest, SetStringCreateDir) {
92 const char kKey[] = "a-test-key";
93 const char kValue[] = "test value";
94 EXPECT_TRUE(prefs_.Init(FilePath(prefs_dir_.Append("subdir"))));
95 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
96 string value;
97 EXPECT_TRUE(
98 file_util::ReadFileToString(prefs_dir_.Append("subdir").Append(kKey),
99 &value));
100 EXPECT_EQ(kValue, value);
101 }
102
103 TEST_F(PrefsTest, SetStringDirCreationFailure) {
104 EXPECT_TRUE(prefs_.Init(FilePath("/dev/null")));
105 const char kKey[] = "test-key";
106 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
107 }
108
109 TEST_F(PrefsTest, SetStringFileCreationFailure) {
110 const char kKey[] = "a-test-key";
111 file_util::CreateDirectory(prefs_dir_.Append(kKey));
112 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
113 EXPECT_TRUE(file_util::DirectoryExists(prefs_dir_.Append(kKey)));
114 }
115
116 TEST_F(PrefsTest, GetInt64) {
117 const char kKey[] = "test-key";
118 ASSERT_TRUE(SetValue(kKey, " \n 25 \t "));
119 int64_t value;
120 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
121 EXPECT_EQ(25, value);
122 }
123
124 TEST_F(PrefsTest, GetInt64BadValue) {
125 const char kKey[] = "test-key";
126 ASSERT_TRUE(SetValue(kKey, "30a"));
127 int64_t value;
128 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
129 }
130
131 TEST_F(PrefsTest, GetInt64Max) {
132 const char kKey[] = "test-key";
133 ASSERT_TRUE(SetValue(kKey, StringPrintf("%" PRIi64, kint64max)));
134 int64_t value;
135 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
136 EXPECT_EQ(kint64max, value);
137 }
138
139 TEST_F(PrefsTest, GetInt64Min) {
140 const char kKey[] = "test-key";
141 ASSERT_TRUE(SetValue(kKey, StringPrintf("%" PRIi64, kint64min)));
142 int64_t value;
143 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
144 EXPECT_EQ(kint64min, value);
145 }
146
147 TEST_F(PrefsTest, GetInt64Negative) {
148 const char kKey[] = "test-key";
149 ASSERT_TRUE(SetValue(kKey, " \t -100 \n "));
150 int64_t value;
151 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
152 EXPECT_EQ(-100, value);
153 }
154
155 TEST_F(PrefsTest, GetInt64NonExistentKey) {
156 int64_t value;
157 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
158 }
159
160 TEST_F(PrefsTest, SetInt64) {
161 const char kKey[] = "test_int";
162 EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
163 string value;
164 EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
165 EXPECT_EQ("-123", value);
166 }
167
168 TEST_F(PrefsTest, SetInt64BadKey) {
169 const char kKey[] = "s p a c e s";
170 EXPECT_FALSE(prefs_.SetInt64(kKey, 20));
171 EXPECT_FALSE(file_util::PathExists(prefs_dir_.Append(kKey)));
172 }
173
174 TEST_F(PrefsTest, SetInt64Max) {
175 const char kKey[] = "test-max-int";
176 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64max));
177 string value;
178 EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
179 EXPECT_EQ(StringPrintf("%" PRIi64, kint64max), value);
180 }
181
182 TEST_F(PrefsTest, SetInt64Min) {
183 const char kKey[] = "test-min-int";
184 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64min));
185 string value;
186 EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
187 EXPECT_EQ(StringPrintf("%" PRIi64, kint64min), value);
188 }
189
190 } // namespace chromeos_update_engine
OLDNEW
« no previous file with comments | « prefs_interface.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698