| OLD | NEW |
| (Empty) |
| 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 <memory> | |
| 12 | |
| 13 #include "webrtc/base/checks.h" | |
| 14 #include "webrtc/base/fileutils.h" | |
| 15 #include "webrtc/base/gunit.h" | |
| 16 #include "webrtc/base/optionsfile.h" | |
| 17 #include "webrtc/base/pathutils.h" | |
| 18 | |
| 19 namespace rtc { | |
| 20 | |
| 21 static const std::string kTestOptionA = "test-option-a"; | |
| 22 static const std::string kTestOptionB = "test-option-b"; | |
| 23 static const std::string kTestString1 = "a string"; | |
| 24 static const std::string kTestString2 = "different string"; | |
| 25 static const std::string kOptionWithEquals = "foo=bar"; | |
| 26 static const std::string kOptionWithNewline = "foo\nbar"; | |
| 27 static const std::string kValueWithEquals = "baz=quux"; | |
| 28 static const std::string kValueWithNewline = "baz\nquux"; | |
| 29 static const std::string kEmptyString = ""; | |
| 30 static const char kOptionWithUtf8[] = {'O', 'p', 't', '\302', '\256', 'i', 'o', | |
| 31 'n', '\342', '\204', '\242', '\0'}; // Opt(R)io(TM). | |
| 32 static const char kValueWithUtf8[] = {'V', 'a', 'l', '\302', '\256', 'v', 'e', | |
| 33 '\342', '\204', '\242', '\0'}; // Val(R)ue(TM). | |
| 34 static int kTestInt1 = 12345; | |
| 35 static int kTestInt2 = 67890; | |
| 36 static int kNegInt = -634; | |
| 37 static int kZero = 0; | |
| 38 | |
| 39 #if defined (WEBRTC_ANDROID) | |
| 40 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. | |
| 41 #define MAYBE_OptionsFileTest DISABLED_OptionsFileTest | |
| 42 #else | |
| 43 #define MAYBE_OptionsFileTest OptionsFileTest | |
| 44 #endif | |
| 45 | |
| 46 class MAYBE_OptionsFileTest : public testing::Test { | |
| 47 public: | |
| 48 MAYBE_OptionsFileTest() { | |
| 49 Pathname dir; | |
| 50 RTC_CHECK(Filesystem::GetTemporaryFolder(dir, true, nullptr)); | |
| 51 test_file_ = Filesystem::TempFilename(dir, ".testfile"); | |
| 52 OpenStore(); | |
| 53 } | |
| 54 | |
| 55 ~MAYBE_OptionsFileTest() override { | |
| 56 remove(test_file_.c_str()); | |
| 57 } | |
| 58 | |
| 59 protected: | |
| 60 void OpenStore() { | |
| 61 store_.reset(new OptionsFile(test_file_)); | |
| 62 } | |
| 63 | |
| 64 std::unique_ptr<OptionsFile> store_; | |
| 65 | |
| 66 private: | |
| 67 std::string test_file_; | |
| 68 }; | |
| 69 | |
| 70 TEST_F(MAYBE_OptionsFileTest, GetSetString) { | |
| 71 // Clear contents of the file on disk. | |
| 72 EXPECT_TRUE(store_->Save()); | |
| 73 std::string out1, out2; | |
| 74 EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1)); | |
| 75 EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2)); | |
| 76 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1)); | |
| 77 EXPECT_TRUE(store_->Save()); | |
| 78 EXPECT_TRUE(store_->Load()); | |
| 79 EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kTestString2)); | |
| 80 EXPECT_TRUE(store_->Save()); | |
| 81 EXPECT_TRUE(store_->Load()); | |
| 82 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1)); | |
| 83 EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out2)); | |
| 84 EXPECT_EQ(kTestString1, out1); | |
| 85 EXPECT_EQ(kTestString2, out2); | |
| 86 EXPECT_TRUE(store_->RemoveValue(kTestOptionA)); | |
| 87 EXPECT_TRUE(store_->Save()); | |
| 88 EXPECT_TRUE(store_->Load()); | |
| 89 EXPECT_TRUE(store_->RemoveValue(kTestOptionB)); | |
| 90 EXPECT_TRUE(store_->Save()); | |
| 91 EXPECT_TRUE(store_->Load()); | |
| 92 EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1)); | |
| 93 EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2)); | |
| 94 } | |
| 95 | |
| 96 TEST_F(MAYBE_OptionsFileTest, GetSetInt) { | |
| 97 // Clear contents of the file on disk. | |
| 98 EXPECT_TRUE(store_->Save()); | |
| 99 int out1, out2; | |
| 100 EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1)); | |
| 101 EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2)); | |
| 102 EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kTestInt1)); | |
| 103 EXPECT_TRUE(store_->Save()); | |
| 104 EXPECT_TRUE(store_->Load()); | |
| 105 EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kTestInt2)); | |
| 106 EXPECT_TRUE(store_->Save()); | |
| 107 EXPECT_TRUE(store_->Load()); | |
| 108 EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1)); | |
| 109 EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2)); | |
| 110 EXPECT_EQ(kTestInt1, out1); | |
| 111 EXPECT_EQ(kTestInt2, out2); | |
| 112 EXPECT_TRUE(store_->RemoveValue(kTestOptionA)); | |
| 113 EXPECT_TRUE(store_->Save()); | |
| 114 EXPECT_TRUE(store_->Load()); | |
| 115 EXPECT_TRUE(store_->RemoveValue(kTestOptionB)); | |
| 116 EXPECT_TRUE(store_->Save()); | |
| 117 EXPECT_TRUE(store_->Load()); | |
| 118 EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1)); | |
| 119 EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2)); | |
| 120 EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kNegInt)); | |
| 121 EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1)); | |
| 122 EXPECT_EQ(kNegInt, out1); | |
| 123 EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kZero)); | |
| 124 EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1)); | |
| 125 EXPECT_EQ(kZero, out1); | |
| 126 } | |
| 127 | |
| 128 TEST_F(MAYBE_OptionsFileTest, Persist) { | |
| 129 // Clear contents of the file on disk. | |
| 130 EXPECT_TRUE(store_->Save()); | |
| 131 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1)); | |
| 132 EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kNegInt)); | |
| 133 EXPECT_TRUE(store_->Save()); | |
| 134 | |
| 135 // Load the saved contents from above. | |
| 136 OpenStore(); | |
| 137 EXPECT_TRUE(store_->Load()); | |
| 138 std::string out1; | |
| 139 int out2; | |
| 140 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1)); | |
| 141 EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2)); | |
| 142 EXPECT_EQ(kTestString1, out1); | |
| 143 EXPECT_EQ(kNegInt, out2); | |
| 144 } | |
| 145 | |
| 146 TEST_F(MAYBE_OptionsFileTest, SpecialCharacters) { | |
| 147 // Clear contents of the file on disk. | |
| 148 EXPECT_TRUE(store_->Save()); | |
| 149 std::string out; | |
| 150 EXPECT_FALSE(store_->SetStringValue(kOptionWithEquals, kTestString1)); | |
| 151 EXPECT_FALSE(store_->GetStringValue(kOptionWithEquals, &out)); | |
| 152 EXPECT_FALSE(store_->SetStringValue(kOptionWithNewline, kTestString1)); | |
| 153 EXPECT_FALSE(store_->GetStringValue(kOptionWithNewline, &out)); | |
| 154 EXPECT_TRUE(store_->SetStringValue(kOptionWithUtf8, kValueWithUtf8)); | |
| 155 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1)); | |
| 156 EXPECT_TRUE(store_->Save()); | |
| 157 EXPECT_TRUE(store_->Load()); | |
| 158 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out)); | |
| 159 EXPECT_EQ(kTestString1, out); | |
| 160 EXPECT_TRUE(store_->GetStringValue(kOptionWithUtf8, &out)); | |
| 161 EXPECT_EQ(kValueWithUtf8, out); | |
| 162 EXPECT_FALSE(store_->SetStringValue(kTestOptionA, kValueWithNewline)); | |
| 163 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out)); | |
| 164 EXPECT_EQ(kTestString1, out); | |
| 165 EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kValueWithEquals)); | |
| 166 EXPECT_TRUE(store_->Save()); | |
| 167 EXPECT_TRUE(store_->Load()); | |
| 168 EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out)); | |
| 169 EXPECT_EQ(kValueWithEquals, out); | |
| 170 EXPECT_TRUE(store_->SetStringValue(kEmptyString, kTestString2)); | |
| 171 EXPECT_TRUE(store_->Save()); | |
| 172 EXPECT_TRUE(store_->Load()); | |
| 173 EXPECT_TRUE(store_->GetStringValue(kEmptyString, &out)); | |
| 174 EXPECT_EQ(kTestString2, out); | |
| 175 EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kEmptyString)); | |
| 176 EXPECT_TRUE(store_->Save()); | |
| 177 EXPECT_TRUE(store_->Load()); | |
| 178 EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out)); | |
| 179 EXPECT_EQ(kEmptyString, out); | |
| 180 } | |
| 181 | |
| 182 } // namespace rtc | |
| OLD | NEW |