• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2008 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/base/fileutils.h"
12 #include "webrtc/base/gunit.h"
13 #include "webrtc/base/optionsfile.h"
14 #include "webrtc/base/pathutils.h"
15 
16 namespace rtc {
17 
18 static const std::string kTestOptionA = "test-option-a";
19 static const std::string kTestOptionB = "test-option-b";
20 static const std::string kTestString1 = "a string";
21 static const std::string kTestString2 = "different string";
22 static const std::string kOptionWithEquals = "foo=bar";
23 static const std::string kOptionWithNewline = "foo\nbar";
24 static const std::string kValueWithEquals = "baz=quux";
25 static const std::string kValueWithNewline = "baz\nquux";
26 static const std::string kEmptyString = "";
27 static const char kOptionWithUtf8[] = {'O', 'p', 't', '\302', '\256', 'i', 'o',
28     'n', '\342', '\204', '\242', '\0'};  // Opt(R)io(TM).
29 static const char kValueWithUtf8[] = {'V', 'a', 'l', '\302', '\256', 'v', 'e',
30     '\342', '\204', '\242', '\0'};  // Val(R)ue(TM).
31 static int kTestInt1 = 12345;
32 static int kTestInt2 = 67890;
33 static int kNegInt = -634;
34 static int kZero = 0;
35 
36 class OptionsFileTest : public testing::Test {
37  public:
OptionsFileTest()38   OptionsFileTest() {
39     Pathname dir;
40     ASSERT(Filesystem::GetTemporaryFolder(dir, true, NULL));
41     test_file_ = Filesystem::TempFilename(dir, ".testfile");
42     OpenStore();
43   }
44 
45  protected:
OpenStore()46   void OpenStore() {
47     store_.reset(new OptionsFile(test_file_));
48   }
49 
50   rtc::scoped_ptr<OptionsFile> store_;
51 
52  private:
53   std::string test_file_;
54 };
55 
TEST_F(OptionsFileTest,GetSetString)56 TEST_F(OptionsFileTest, GetSetString) {
57   // Clear contents of the file on disk.
58   EXPECT_TRUE(store_->Save());
59   std::string out1, out2;
60   EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1));
61   EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2));
62   EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
63   EXPECT_TRUE(store_->Save());
64   EXPECT_TRUE(store_->Load());
65   EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kTestString2));
66   EXPECT_TRUE(store_->Save());
67   EXPECT_TRUE(store_->Load());
68   EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1));
69   EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out2));
70   EXPECT_EQ(kTestString1, out1);
71   EXPECT_EQ(kTestString2, out2);
72   EXPECT_TRUE(store_->RemoveValue(kTestOptionA));
73   EXPECT_TRUE(store_->Save());
74   EXPECT_TRUE(store_->Load());
75   EXPECT_TRUE(store_->RemoveValue(kTestOptionB));
76   EXPECT_TRUE(store_->Save());
77   EXPECT_TRUE(store_->Load());
78   EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1));
79   EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2));
80 }
81 
TEST_F(OptionsFileTest,GetSetInt)82 TEST_F(OptionsFileTest, GetSetInt) {
83   // Clear contents of the file on disk.
84   EXPECT_TRUE(store_->Save());
85   int out1, out2;
86   EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1));
87   EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2));
88   EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kTestInt1));
89   EXPECT_TRUE(store_->Save());
90   EXPECT_TRUE(store_->Load());
91   EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kTestInt2));
92   EXPECT_TRUE(store_->Save());
93   EXPECT_TRUE(store_->Load());
94   EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
95   EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2));
96   EXPECT_EQ(kTestInt1, out1);
97   EXPECT_EQ(kTestInt2, out2);
98   EXPECT_TRUE(store_->RemoveValue(kTestOptionA));
99   EXPECT_TRUE(store_->Save());
100   EXPECT_TRUE(store_->Load());
101   EXPECT_TRUE(store_->RemoveValue(kTestOptionB));
102   EXPECT_TRUE(store_->Save());
103   EXPECT_TRUE(store_->Load());
104   EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1));
105   EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2));
106   EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kNegInt));
107   EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
108   EXPECT_EQ(kNegInt, out1);
109   EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kZero));
110   EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
111   EXPECT_EQ(kZero, out1);
112 }
113 
TEST_F(OptionsFileTest,Persist)114 TEST_F(OptionsFileTest, Persist) {
115   // Clear contents of the file on disk.
116   EXPECT_TRUE(store_->Save());
117   EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
118   EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kNegInt));
119   EXPECT_TRUE(store_->Save());
120 
121   // Load the saved contents from above.
122   OpenStore();
123   EXPECT_TRUE(store_->Load());
124   std::string out1;
125   int out2;
126   EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1));
127   EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2));
128   EXPECT_EQ(kTestString1, out1);
129   EXPECT_EQ(kNegInt, out2);
130 }
131 
TEST_F(OptionsFileTest,SpecialCharacters)132 TEST_F(OptionsFileTest, SpecialCharacters) {
133   // Clear contents of the file on disk.
134   EXPECT_TRUE(store_->Save());
135   std::string out;
136   EXPECT_FALSE(store_->SetStringValue(kOptionWithEquals, kTestString1));
137   EXPECT_FALSE(store_->GetStringValue(kOptionWithEquals, &out));
138   EXPECT_FALSE(store_->SetStringValue(kOptionWithNewline, kTestString1));
139   EXPECT_FALSE(store_->GetStringValue(kOptionWithNewline, &out));
140   EXPECT_TRUE(store_->SetStringValue(kOptionWithUtf8, kValueWithUtf8));
141   EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
142   EXPECT_TRUE(store_->Save());
143   EXPECT_TRUE(store_->Load());
144   EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
145   EXPECT_EQ(kTestString1, out);
146   EXPECT_TRUE(store_->GetStringValue(kOptionWithUtf8, &out));
147   EXPECT_EQ(kValueWithUtf8, out);
148   EXPECT_FALSE(store_->SetStringValue(kTestOptionA, kValueWithNewline));
149   EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
150   EXPECT_EQ(kTestString1, out);
151   EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kValueWithEquals));
152   EXPECT_TRUE(store_->Save());
153   EXPECT_TRUE(store_->Load());
154   EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
155   EXPECT_EQ(kValueWithEquals, out);
156   EXPECT_TRUE(store_->SetStringValue(kEmptyString, kTestString2));
157   EXPECT_TRUE(store_->Save());
158   EXPECT_TRUE(store_->Load());
159   EXPECT_TRUE(store_->GetStringValue(kEmptyString, &out));
160   EXPECT_EQ(kTestString2, out);
161   EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kEmptyString));
162   EXPECT_TRUE(store_->Save());
163   EXPECT_TRUE(store_->Load());
164   EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out));
165   EXPECT_EQ(kEmptyString, out);
166 }
167 
168 }  // namespace rtc
169