3 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/gunit.h"
29 #include "talk/base/optionsfile.h"
34 static const char *kTestFile = "/sdcard/.testfile";
36 static const char *kTestFile = "/tmp/.testfile";
38 static const char *kTestFile = ".testfile";
41 static const std::string kTestOptionA = "test-option-a";
42 static const std::string kTestOptionB = "test-option-b";
43 static const std::string kTestString1 = "a string";
44 static const std::string kTestString2 = "different string";
45 static const std::string kOptionWithEquals = "foo=bar";
46 static const std::string kOptionWithNewline = "foo\nbar";
47 static const std::string kValueWithEquals = "baz=quux";
48 static const std::string kValueWithNewline = "baz\nquux";
49 static const std::string kEmptyString = "";
50 static const char kOptionWithUtf8[] = {'O', 'p', 't', '\302', '\256', 'i', 'o',
51 'n', '\342', '\204', '\242', '\0'}; // Opt(R)io(TM).
52 static const char kValueWithUtf8[] = {'V', 'a', 'l', '\302', '\256', 'v', 'e',
53 '\342', '\204', '\242', '\0'}; // Val(R)ue(TM).
54 static int kTestInt1 = 12345;
55 static int kTestInt2 = 67890;
56 static int kNegInt = -634;
59 TEST(OptionsFile, GetSetString) {
60 OptionsFile store(kTestFile);
61 // Clear contents of the file on disk.
62 EXPECT_TRUE(store.Save());
63 std::string out1, out2;
64 EXPECT_FALSE(store.GetStringValue(kTestOptionA, &out1));
65 EXPECT_FALSE(store.GetStringValue(kTestOptionB, &out2));
66 EXPECT_TRUE(store.SetStringValue(kTestOptionA, kTestString1));
67 EXPECT_TRUE(store.Save());
68 EXPECT_TRUE(store.Load());
69 EXPECT_TRUE(store.SetStringValue(kTestOptionB, kTestString2));
70 EXPECT_TRUE(store.Save());
71 EXPECT_TRUE(store.Load());
72 EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out1));
73 EXPECT_TRUE(store.GetStringValue(kTestOptionB, &out2));
74 EXPECT_EQ(kTestString1, out1);
75 EXPECT_EQ(kTestString2, out2);
76 EXPECT_TRUE(store.RemoveValue(kTestOptionA));
77 EXPECT_TRUE(store.Save());
78 EXPECT_TRUE(store.Load());
79 EXPECT_TRUE(store.RemoveValue(kTestOptionB));
80 EXPECT_TRUE(store.Save());
81 EXPECT_TRUE(store.Load());
82 EXPECT_FALSE(store.GetStringValue(kTestOptionA, &out1));
83 EXPECT_FALSE(store.GetStringValue(kTestOptionB, &out2));
86 TEST(OptionsFile, GetSetInt) {
87 OptionsFile store(kTestFile);
88 // Clear contents of the file on disk.
89 EXPECT_TRUE(store.Save());
91 EXPECT_FALSE(store.GetIntValue(kTestOptionA, &out1));
92 EXPECT_FALSE(store.GetIntValue(kTestOptionB, &out2));
93 EXPECT_TRUE(store.SetIntValue(kTestOptionA, kTestInt1));
94 EXPECT_TRUE(store.Save());
95 EXPECT_TRUE(store.Load());
96 EXPECT_TRUE(store.SetIntValue(kTestOptionB, kTestInt2));
97 EXPECT_TRUE(store.Save());
98 EXPECT_TRUE(store.Load());
99 EXPECT_TRUE(store.GetIntValue(kTestOptionA, &out1));
100 EXPECT_TRUE(store.GetIntValue(kTestOptionB, &out2));
101 EXPECT_EQ(kTestInt1, out1);
102 EXPECT_EQ(kTestInt2, out2);
103 EXPECT_TRUE(store.RemoveValue(kTestOptionA));
104 EXPECT_TRUE(store.Save());
105 EXPECT_TRUE(store.Load());
106 EXPECT_TRUE(store.RemoveValue(kTestOptionB));
107 EXPECT_TRUE(store.Save());
108 EXPECT_TRUE(store.Load());
109 EXPECT_FALSE(store.GetIntValue(kTestOptionA, &out1));
110 EXPECT_FALSE(store.GetIntValue(kTestOptionB, &out2));
111 EXPECT_TRUE(store.SetIntValue(kTestOptionA, kNegInt));
112 EXPECT_TRUE(store.GetIntValue(kTestOptionA, &out1));
113 EXPECT_EQ(kNegInt, out1);
114 EXPECT_TRUE(store.SetIntValue(kTestOptionA, kZero));
115 EXPECT_TRUE(store.GetIntValue(kTestOptionA, &out1));
116 EXPECT_EQ(kZero, out1);
119 TEST(OptionsFile, Persist) {
121 OptionsFile store(kTestFile);
122 // Clear contents of the file on disk.
123 EXPECT_TRUE(store.Save());
124 EXPECT_TRUE(store.SetStringValue(kTestOptionA, kTestString1));
125 EXPECT_TRUE(store.SetIntValue(kTestOptionB, kNegInt));
126 EXPECT_TRUE(store.Save());
129 OptionsFile store(kTestFile);
130 // Load the saved contents from above.
131 EXPECT_TRUE(store.Load());
134 EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out1));
135 EXPECT_TRUE(store.GetIntValue(kTestOptionB, &out2));
136 EXPECT_EQ(kTestString1, out1);
137 EXPECT_EQ(kNegInt, out2);
141 TEST(OptionsFile, SpecialCharacters) {
142 OptionsFile store(kTestFile);
143 // Clear contents of the file on disk.
144 EXPECT_TRUE(store.Save());
146 EXPECT_FALSE(store.SetStringValue(kOptionWithEquals, kTestString1));
147 EXPECT_FALSE(store.GetStringValue(kOptionWithEquals, &out));
148 EXPECT_FALSE(store.SetStringValue(kOptionWithNewline, kTestString1));
149 EXPECT_FALSE(store.GetStringValue(kOptionWithNewline, &out));
150 EXPECT_TRUE(store.SetStringValue(kOptionWithUtf8, kValueWithUtf8));
151 EXPECT_TRUE(store.SetStringValue(kTestOptionA, kTestString1));
152 EXPECT_TRUE(store.Save());
153 EXPECT_TRUE(store.Load());
154 EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out));
155 EXPECT_EQ(kTestString1, out);
156 EXPECT_TRUE(store.GetStringValue(kOptionWithUtf8, &out));
157 EXPECT_EQ(kValueWithUtf8, out);
158 EXPECT_FALSE(store.SetStringValue(kTestOptionA, kValueWithNewline));
159 EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out));
160 EXPECT_EQ(kTestString1, out);
161 EXPECT_TRUE(store.SetStringValue(kTestOptionA, kValueWithEquals));
162 EXPECT_TRUE(store.Save());
163 EXPECT_TRUE(store.Load());
164 EXPECT_TRUE(store.GetStringValue(kTestOptionA, &out));
165 EXPECT_EQ(kValueWithEquals, out);
166 EXPECT_TRUE(store.SetStringValue(kEmptyString, kTestString2));
167 EXPECT_TRUE(store.Save());
168 EXPECT_TRUE(store.Load());
169 EXPECT_TRUE(store.GetStringValue(kEmptyString, &out));
170 EXPECT_EQ(kTestString2, out);
171 EXPECT_TRUE(store.SetStringValue(kTestOptionB, kEmptyString));
172 EXPECT_TRUE(store.Save());
173 EXPECT_TRUE(store.Load());
174 EXPECT_TRUE(store.GetStringValue(kTestOptionB, &out));
175 EXPECT_EQ(kEmptyString, out);
178 } // namespace talk_base