1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/login/signed_settings_temp_storage.h"
6
7#include <algorithm>
8#include <iterator>
9#include <map>
10#include <vector>
11
12#include "base/file_util.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/scoped_temp_dir.h"
15#include "chrome/browser/prefs/pref_service.h"
16#include "chrome/common/logging_chrome.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace chromeos {
20
21class SignedSettingsTempStorageTest : public ::testing::Test {
22 protected:
23  virtual void SetUp() {
24    ref_map_["some_stuff"] = "a=35;code=64";
25    ref_map_["another_stuff"] = "";
26    ref_map_["name"] = "value";
27    ref_map_["2bc6aa16-e0ea-11df-b13d-18a90520e2e5"] = "512";
28
29    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30    FilePath temp_file;
31    ASSERT_TRUE(
32        file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file));
33    local_state_.reset(PrefService::CreatePrefService(temp_file, NULL, NULL));
34    ASSERT_TRUE(NULL != local_state_.get());
35    SignedSettingsTempStorage::RegisterPrefs(local_state_.get());
36  }
37
38  std::map<std::string, std::string> ref_map_;
39  ScopedTempDir temp_dir_;
40  scoped_ptr<PrefService> local_state_;
41};
42
43TEST_F(SignedSettingsTempStorageTest, Basic) {
44  EXPECT_GT(ref_map_.size(), 3u);  // Number above 3 is many.
45  typedef std::map<std::string, std::string>::iterator It;
46  std::vector<It> a_list;
47  for (It it = ref_map_.begin(); it != ref_map_.end(); ++it) {
48    a_list.push_back(it);
49  }
50  std::random_shuffle(a_list.begin(), a_list.end());
51  std::vector<It> b_list(a_list);
52  std::copy(b_list.begin(),
53            b_list.begin() + b_list.size() / 2,
54            std::back_inserter(a_list));
55  std::random_shuffle(a_list.begin(), a_list.end());
56  for (size_t i = 0; i < a_list.size(); ++i) {
57    EXPECT_TRUE(SignedSettingsTempStorage::Store(a_list[i]->first,
58                                                 a_list[i]->second,
59                                                 local_state_.get()));
60  }
61  for (int i = 0; i < 3; ++i) {
62    std::copy(a_list.begin(), a_list.end(), std::back_inserter(b_list));
63  }
64  std::random_shuffle(b_list.begin(), b_list.end());
65  std::string value;
66  for (size_t i = 0; i < b_list.size(); ++i) {
67    EXPECT_TRUE(SignedSettingsTempStorage::Retrieve(b_list[i]->first, &value,
68                                                    local_state_.get()));
69    EXPECT_EQ(b_list[i]->second, value);
70    EXPECT_FALSE(SignedSettingsTempStorage::Retrieve("non-existent tv-series",
71                                                     &value,
72                                                     local_state_.get()));
73  }
74}
75
76}  // namespace chromeos
77