blob: afb79cf9a7bbea39ad2990c811419e41dcf5e419 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2008, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000028#include "talk/base/fileutils.h"
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000029#include "talk/base/gunit.h"
30#include "talk/base/optionsfile.h"
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000031#include "talk/base/pathutils.h"
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000032
33namespace talk_base {
34
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000035static const std::string kTestOptionA = "test-option-a";
36static const std::string kTestOptionB = "test-option-b";
37static const std::string kTestString1 = "a string";
38static const std::string kTestString2 = "different string";
39static const std::string kOptionWithEquals = "foo=bar";
40static const std::string kOptionWithNewline = "foo\nbar";
41static const std::string kValueWithEquals = "baz=quux";
42static const std::string kValueWithNewline = "baz\nquux";
43static const std::string kEmptyString = "";
44static const char kOptionWithUtf8[] = {'O', 'p', 't', '\302', '\256', 'i', 'o',
45 'n', '\342', '\204', '\242', '\0'}; // Opt(R)io(TM).
46static const char kValueWithUtf8[] = {'V', 'a', 'l', '\302', '\256', 'v', 'e',
47 '\342', '\204', '\242', '\0'}; // Val(R)ue(TM).
48static int kTestInt1 = 12345;
49static int kTestInt2 = 67890;
50static int kNegInt = -634;
51static int kZero = 0;
52
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000053class OptionsFileTest : public testing::Test {
54 public:
55 OptionsFileTest() {
56 Pathname dir;
57 ASSERT(Filesystem::GetTemporaryFolder(dir, true, NULL));
58 test_file_ = Filesystem::TempFilename(dir, ".testfile");
59 OpenStore();
60 }
61
62 protected:
63 void OpenStore() {
64 store_.reset(new OptionsFile(test_file_));
65 }
66
67 talk_base::scoped_ptr<OptionsFile> store_;
68
69 private:
70 std::string test_file_;
71};
72
73TEST_F(OptionsFileTest, GetSetString) {
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000074 // Clear contents of the file on disk.
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000075 EXPECT_TRUE(store_->Save());
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000076 std::string out1, out2;
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000077 EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1));
78 EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2));
79 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
80 EXPECT_TRUE(store_->Save());
81 EXPECT_TRUE(store_->Load());
82 EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kTestString2));
83 EXPECT_TRUE(store_->Save());
84 EXPECT_TRUE(store_->Load());
85 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1));
86 EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out2));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000087 EXPECT_EQ(kTestString1, out1);
88 EXPECT_EQ(kTestString2, out2);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000089 EXPECT_TRUE(store_->RemoveValue(kTestOptionA));
90 EXPECT_TRUE(store_->Save());
91 EXPECT_TRUE(store_->Load());
92 EXPECT_TRUE(store_->RemoveValue(kTestOptionB));
93 EXPECT_TRUE(store_->Save());
94 EXPECT_TRUE(store_->Load());
95 EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1));
96 EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000097}
98
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000099TEST_F(OptionsFileTest, GetSetInt) {
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000100 // Clear contents of the file on disk.
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000101 EXPECT_TRUE(store_->Save());
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000102 int out1, out2;
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000103 EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1));
104 EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2));
105 EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kTestInt1));
106 EXPECT_TRUE(store_->Save());
107 EXPECT_TRUE(store_->Load());
108 EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kTestInt2));
109 EXPECT_TRUE(store_->Save());
110 EXPECT_TRUE(store_->Load());
111 EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
112 EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000113 EXPECT_EQ(kTestInt1, out1);
114 EXPECT_EQ(kTestInt2, out2);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000115 EXPECT_TRUE(store_->RemoveValue(kTestOptionA));
116 EXPECT_TRUE(store_->Save());
117 EXPECT_TRUE(store_->Load());
118 EXPECT_TRUE(store_->RemoveValue(kTestOptionB));
119 EXPECT_TRUE(store_->Save());
120 EXPECT_TRUE(store_->Load());
121 EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1));
122 EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2));
123 EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kNegInt));
124 EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000125 EXPECT_EQ(kNegInt, out1);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000126 EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kZero));
127 EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000128 EXPECT_EQ(kZero, out1);
129}
130
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000131TEST_F(OptionsFileTest, Persist) {
132 // Clear contents of the file on disk.
133 EXPECT_TRUE(store_->Save());
134 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
135 EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kNegInt));
136 EXPECT_TRUE(store_->Save());
137
138 // Load the saved contents from above.
139 OpenStore();
140 EXPECT_TRUE(store_->Load());
141 std::string out1;
142 int out2;
143 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1));
144 EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2));
145 EXPECT_EQ(kTestString1, out1);
146 EXPECT_EQ(kNegInt, out2);
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000147}
148
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000149TEST_F(OptionsFileTest, SpecialCharacters) {
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000150 // Clear contents of the file on disk.
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000151 EXPECT_TRUE(store_->Save());
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000152 std::string out;
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000153 EXPECT_FALSE(store_->SetStringValue(kOptionWithEquals, kTestString1));
154 EXPECT_FALSE(store_->GetStringValue(kOptionWithEquals, &out));
155 EXPECT_FALSE(store_->SetStringValue(kOptionWithNewline, kTestString1));
156 EXPECT_FALSE(store_->GetStringValue(kOptionWithNewline, &out));
157 EXPECT_TRUE(store_->SetStringValue(kOptionWithUtf8, kValueWithUtf8));
158 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
159 EXPECT_TRUE(store_->Save());
160 EXPECT_TRUE(store_->Load());
161 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000162 EXPECT_EQ(kTestString1, out);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000163 EXPECT_TRUE(store_->GetStringValue(kOptionWithUtf8, &out));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000164 EXPECT_EQ(kValueWithUtf8, out);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000165 EXPECT_FALSE(store_->SetStringValue(kTestOptionA, kValueWithNewline));
166 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000167 EXPECT_EQ(kTestString1, out);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000168 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kValueWithEquals));
169 EXPECT_TRUE(store_->Save());
170 EXPECT_TRUE(store_->Load());
171 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000172 EXPECT_EQ(kValueWithEquals, out);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000173 EXPECT_TRUE(store_->SetStringValue(kEmptyString, kTestString2));
174 EXPECT_TRUE(store_->Save());
175 EXPECT_TRUE(store_->Load());
176 EXPECT_TRUE(store_->GetStringValue(kEmptyString, &out));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000177 EXPECT_EQ(kTestString2, out);
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +0000178 EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kEmptyString));
179 EXPECT_TRUE(store_->Save());
180 EXPECT_TRUE(store_->Load());
181 EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out));
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000182 EXPECT_EQ(kEmptyString, out);
183}
184
185} // namespace talk_base