henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/gunit.h" |
| 15 | #include "rtc_base/optionsfile.h" |
| 16 | #include "test/testsupport/fileutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | |
| 20 | static const std::string kTestOptionA = "test-option-a"; |
| 21 | static const std::string kTestOptionB = "test-option-b"; |
| 22 | static const std::string kTestString1 = "a string"; |
| 23 | static const std::string kTestString2 = "different string"; |
| 24 | static const std::string kOptionWithEquals = "foo=bar"; |
| 25 | static const std::string kOptionWithNewline = "foo\nbar"; |
| 26 | static const std::string kValueWithEquals = "baz=quux"; |
| 27 | static const std::string kValueWithNewline = "baz\nquux"; |
| 28 | static const std::string kEmptyString = ""; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | static const char kOptionWithUtf8[] = {'O', 'p', 't', '\302', '\256', |
| 30 | 'i', 'o', 'n', '\342', '\204', |
| 31 | '\242', '\0'}; // Opt(R)io(TM). |
| 32 | static const char kValueWithUtf8[] = { |
| 33 | 'V', 'a', 'l', '\302', '\256', 'v', |
| 34 | 'e', '\342', '\204', '\242', '\0'}; // Val(R)ue(TM). |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | static int kTestInt1 = 12345; |
| 36 | static int kTestInt2 = 67890; |
| 37 | static int kNegInt = -634; |
| 38 | static int kZero = 0; |
| 39 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 40 | #if defined(WEBRTC_ANDROID) |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 41 | // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 42 | #define MAYBE_OptionsFileTest DISABLED_OptionsFileTest |
| 43 | #else |
| 44 | #define MAYBE_OptionsFileTest OptionsFileTest |
| 45 | #endif |
| 46 | |
| 47 | class MAYBE_OptionsFileTest : public testing::Test { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | public: |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 49 | MAYBE_OptionsFileTest() { |
nisse | a0c8887 | 2017-08-24 02:20:46 -0700 | [diff] [blame] | 50 | test_file_ = |
| 51 | webrtc::test::TempFilename(webrtc::test::OutputPath(), ".testfile"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | OpenStore(); |
| 53 | } |
| 54 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | ~MAYBE_OptionsFileTest() override { webrtc::test::RemoveFile(test_file_); } |
jbauch | 541f186 | 2016-02-10 09:09:54 -0800 | [diff] [blame] | 56 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | protected: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | void OpenStore() { store_.reset(new OptionsFile(test_file_)); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 60 | std::unique_ptr<OptionsFile> store_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | std::string test_file_; |
| 64 | }; |
| 65 | |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 66 | TEST_F(MAYBE_OptionsFileTest, GetSetString) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | // Clear contents of the file on disk. |
| 68 | EXPECT_TRUE(store_->Save()); |
| 69 | std::string out1, out2; |
| 70 | EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1)); |
| 71 | EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2)); |
| 72 | EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1)); |
| 73 | EXPECT_TRUE(store_->Save()); |
| 74 | EXPECT_TRUE(store_->Load()); |
| 75 | EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kTestString2)); |
| 76 | EXPECT_TRUE(store_->Save()); |
| 77 | EXPECT_TRUE(store_->Load()); |
| 78 | EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1)); |
| 79 | EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out2)); |
| 80 | EXPECT_EQ(kTestString1, out1); |
| 81 | EXPECT_EQ(kTestString2, out2); |
| 82 | EXPECT_TRUE(store_->RemoveValue(kTestOptionA)); |
| 83 | EXPECT_TRUE(store_->Save()); |
| 84 | EXPECT_TRUE(store_->Load()); |
| 85 | EXPECT_TRUE(store_->RemoveValue(kTestOptionB)); |
| 86 | EXPECT_TRUE(store_->Save()); |
| 87 | EXPECT_TRUE(store_->Load()); |
| 88 | EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1)); |
| 89 | EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2)); |
| 90 | } |
| 91 | |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 92 | TEST_F(MAYBE_OptionsFileTest, GetSetInt) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | // Clear contents of the file on disk. |
| 94 | EXPECT_TRUE(store_->Save()); |
| 95 | int out1, out2; |
| 96 | EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1)); |
| 97 | EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2)); |
| 98 | EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kTestInt1)); |
| 99 | EXPECT_TRUE(store_->Save()); |
| 100 | EXPECT_TRUE(store_->Load()); |
| 101 | EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kTestInt2)); |
| 102 | EXPECT_TRUE(store_->Save()); |
| 103 | EXPECT_TRUE(store_->Load()); |
| 104 | EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1)); |
| 105 | EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2)); |
| 106 | EXPECT_EQ(kTestInt1, out1); |
| 107 | EXPECT_EQ(kTestInt2, out2); |
| 108 | EXPECT_TRUE(store_->RemoveValue(kTestOptionA)); |
| 109 | EXPECT_TRUE(store_->Save()); |
| 110 | EXPECT_TRUE(store_->Load()); |
| 111 | EXPECT_TRUE(store_->RemoveValue(kTestOptionB)); |
| 112 | EXPECT_TRUE(store_->Save()); |
| 113 | EXPECT_TRUE(store_->Load()); |
| 114 | EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1)); |
| 115 | EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2)); |
| 116 | EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kNegInt)); |
| 117 | EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1)); |
| 118 | EXPECT_EQ(kNegInt, out1); |
| 119 | EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kZero)); |
| 120 | EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1)); |
| 121 | EXPECT_EQ(kZero, out1); |
| 122 | } |
| 123 | |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 124 | TEST_F(MAYBE_OptionsFileTest, Persist) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | // Clear contents of the file on disk. |
| 126 | EXPECT_TRUE(store_->Save()); |
| 127 | EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1)); |
| 128 | EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kNegInt)); |
| 129 | EXPECT_TRUE(store_->Save()); |
| 130 | |
| 131 | // Load the saved contents from above. |
| 132 | OpenStore(); |
| 133 | EXPECT_TRUE(store_->Load()); |
| 134 | std::string out1; |
| 135 | int out2; |
| 136 | EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1)); |
| 137 | EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2)); |
| 138 | EXPECT_EQ(kTestString1, out1); |
| 139 | EXPECT_EQ(kNegInt, out2); |
| 140 | } |
| 141 | |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 142 | TEST_F(MAYBE_OptionsFileTest, SpecialCharacters) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | // Clear contents of the file on disk. |
| 144 | EXPECT_TRUE(store_->Save()); |
| 145 | std::string out; |
| 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); |
| 176 | } |
| 177 | |
| 178 | } // namespace rtc |