147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2008 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/fileutils.h"
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/gunit.h"
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/optionsfile.h"
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/pathutils.h"
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kTestOptionA = "test-option-a";
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kTestOptionB = "test-option-b";
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kTestString1 = "a string";
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kTestString2 = "different string";
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kOptionWithEquals = "foo=bar";
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kOptionWithNewline = "foo\nbar";
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kValueWithEquals = "baz=quux";
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kValueWithNewline = "baz\nquux";
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const std::string kEmptyString = "";
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const char kOptionWithUtf8[] = {'O', 'p', 't', '\302', '\256', 'i', 'o',
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    'n', '\342', '\204', '\242', '\0'};  // Opt(R)io(TM).
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const char kValueWithUtf8[] = {'V', 'a', 'l', '\302', '\256', 'v', 'e',
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    '\342', '\204', '\242', '\0'};  // Val(R)ue(TM).
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic int kTestInt1 = 12345;
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic int kTestInt2 = 67890;
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic int kNegInt = -634;
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic int kZero = 0;
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass OptionsFileTest : public testing::Test {
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  OptionsFileTest() {
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Pathname dir;
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    ASSERT(Filesystem::GetTemporaryFolder(dir, true, NULL));
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    test_file_ = Filesystem::TempFilename(dir, ".testfile");
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    OpenStore();
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org protected:
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void OpenStore() {
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    store_.reset(new OptionsFile(test_file_));
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::scoped_ptr<OptionsFile> store_;
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string test_file_;
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST_F(OptionsFileTest, GetSetString) {
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Clear contents of the file on disk.
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string out1, out2;
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1));
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2));
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kTestString2));
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1));
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out2));
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestString1, out1);
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestString2, out2);
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->RemoveValue(kTestOptionA));
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->RemoveValue(kTestOptionB));
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetStringValue(kTestOptionA, &out1));
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetStringValue(kTestOptionB, &out2));
8047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
8147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
8247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST_F(OptionsFileTest, GetSetInt) {
8347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Clear contents of the file on disk.
8447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
8547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int out1, out2;
8647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1));
8747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2));
8847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kTestInt1));
8947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
9047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
9147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kTestInt2));
9247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
9347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
9447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
9547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2));
9647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestInt1, out1);
9747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestInt2, out2);
9847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->RemoveValue(kTestOptionA));
9947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
10047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
10147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->RemoveValue(kTestOptionB));
10247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
10347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
10447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetIntValue(kTestOptionA, &out1));
10547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetIntValue(kTestOptionB, &out2));
10647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kNegInt));
10747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
10847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kNegInt, out1);
10947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetIntValue(kTestOptionA, kZero));
11047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetIntValue(kTestOptionA, &out1));
11147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kZero, out1);
11247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
11347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
11447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST_F(OptionsFileTest, Persist) {
11547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Clear contents of the file on disk.
11647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
11747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
11847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetIntValue(kTestOptionB, kNegInt));
11947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
12047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Load the saved contents from above.
12247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  OpenStore();
12347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
12447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string out1;
12547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int out2;
12647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out1));
12747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetIntValue(kTestOptionB, &out2));
12847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestString1, out1);
12947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kNegInt, out2);
13047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
13147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST_F(OptionsFileTest, SpecialCharacters) {
13347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Clear contents of the file on disk.
13447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
13547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string out;
13647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->SetStringValue(kOptionWithEquals, kTestString1));
13747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetStringValue(kOptionWithEquals, &out));
13847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->SetStringValue(kOptionWithNewline, kTestString1));
13947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->GetStringValue(kOptionWithNewline, &out));
14047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kOptionWithUtf8, kValueWithUtf8));
14147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kTestString1));
14247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
14347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
14447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
14547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestString1, out);
14647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kOptionWithUtf8, &out));
14747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kValueWithUtf8, out);
14847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(store_->SetStringValue(kTestOptionA, kValueWithNewline));
14947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
15047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestString1, out);
15147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kTestOptionA, kValueWithEquals));
15247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
15347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
15447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kTestOptionA, &out));
15547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kValueWithEquals, out);
15647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kEmptyString, kTestString2));
15747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
15847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
15947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kEmptyString, &out));
16047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kTestString2, out);
16147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->SetStringValue(kTestOptionB, kEmptyString));
16247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Save());
16347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->Load());
16447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(store_->GetStringValue(kTestOptionB, &out));
16547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(kEmptyString, out);
16647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
16747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
16847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}  // namespace rtc
169