blob: 87c41b6e25429ea14db298cd56e5be6a7e44f520 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov30030592010-07-27 13:53:20 -07002// 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"
Mike Frysinger8155d082012-04-06 15:23:18 -040012#include "base/stringprintf.h"
Darin Petkov30030592010-07-27 13:53:20 -070013#include "gtest/gtest.h"
14#include "update_engine/prefs.h"
15
16using std::string;
17
18namespace chromeos_update_engine {
19
20class PrefsTest : public ::testing::Test {
21 protected:
22 virtual void SetUp() {
23 ASSERT_TRUE(file_util::CreateNewTempDirectory("auprefs", &prefs_dir_));
24 ASSERT_TRUE(prefs_.Init(prefs_dir_));
25 }
26
27 virtual void TearDown() {
28 file_util::Delete(prefs_dir_, true); // recursive
29 }
30
31 bool SetValue(const string& key, const string& value) {
32 return file_util::WriteFile(prefs_dir_.Append(key),
33 value.data(), value.length()) ==
34 static_cast<int>(value.length());
35 }
36
37 FilePath prefs_dir_;
38 Prefs prefs_;
39};
40
41TEST_F(PrefsTest, GetFileNameForKey) {
42 const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
43 FilePath path;
44 EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path));
45 EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value());
46}
47
48TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
49 FilePath path;
50 EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path));
51}
52
53TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
54 FilePath path;
55 EXPECT_FALSE(prefs_.GetFileNameForKey("", &path));
56}
57
58TEST_F(PrefsTest, GetString) {
59 const char kKey[] = "test-key";
60 const string test_data = "test data";
61 ASSERT_TRUE(SetValue(kKey, test_data));
62 string value;
63 EXPECT_TRUE(prefs_.GetString(kKey, &value));
64 EXPECT_EQ(test_data, value);
65}
66
67TEST_F(PrefsTest, GetStringBadKey) {
68 string value;
69 EXPECT_FALSE(prefs_.GetString(",bad", &value));
70}
71
72TEST_F(PrefsTest, GetStringNonExistentKey) {
73 string value;
74 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
75}
76
77TEST_F(PrefsTest, SetString) {
78 const char kKey[] = "my_test_key";
79 const char kValue[] = "some test value\non 2 lines";
80 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
81 string value;
82 EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value));
83 EXPECT_EQ(kValue, value);
84}
85
86TEST_F(PrefsTest, SetStringBadKey) {
87 const char kKey[] = ".no-dots";
88 EXPECT_FALSE(prefs_.SetString(kKey, "some value"));
89 EXPECT_FALSE(file_util::PathExists(prefs_dir_.Append(kKey)));
90}
91
92TEST_F(PrefsTest, SetStringCreateDir) {
93 const char kKey[] = "a-test-key";
94 const char kValue[] = "test value";
Darin Petkov1cbd78f2010-07-29 12:38:34 -070095 FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2");
96 EXPECT_TRUE(prefs_.Init(subdir));
Darin Petkov30030592010-07-27 13:53:20 -070097 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
98 string value;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070099 EXPECT_TRUE(file_util::ReadFileToString(subdir.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700100 EXPECT_EQ(kValue, value);
101}
102
103TEST_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
109TEST_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
116TEST_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
124TEST_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
131TEST_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
139TEST_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
147TEST_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
155TEST_F(PrefsTest, GetInt64NonExistentKey) {
156 int64_t value;
157 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
158}
159
160TEST_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
168TEST_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
174TEST_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
182TEST_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
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700190TEST_F(PrefsTest, ExistsWorks) {
191 const char kKey[] = "exists-key";
192
193 // test that the key doesn't exist before we set it.
194 EXPECT_FALSE(prefs_.Exists(kKey));
195
196 // test that the key exists after we set it.
197 ASSERT_TRUE(prefs_.SetInt64(kKey, 8));
198 EXPECT_TRUE(prefs_.Exists(kKey));
199}
200
201TEST_F(PrefsTest, DeleteWorks) {
202 const char kKey[] = "delete-key";
203
204 // test that it's alright to delete a non-existent key.
205 EXPECT_TRUE(prefs_.Delete(kKey));
206
207 // delete the key after we set it.
208 ASSERT_TRUE(prefs_.SetInt64(kKey, 0));
209 EXPECT_TRUE(prefs_.Delete(kKey));
210
211 // make sure it doesn't exist anymore.
212 EXPECT_FALSE(prefs_.Exists(kKey));
213}
214
Darin Petkov30030592010-07-27 13:53:20 -0700215} // namespace chromeos_update_engine