summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/credential_cache_service_win_unittest.cc
blob: 60b7ab75dd722ca7c20ff9a1c597a9ac3cf22275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/scoped_temp_dir.h"
#include "base/time.h"
#include "base/values.h"
#include "chrome/browser/sync/credential_cache_service_win.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "google_apis/gaia/gaia_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

class CredentialCacheServiceTest : public CredentialCacheService,
                                   public testing::Test {
 public:
  CredentialCacheServiceTest()
      : CredentialCacheService(NULL),
        file_message_loop_(MessageLoop::TYPE_IO) {}

  virtual ~CredentialCacheServiceTest() {}

  // testing::Test implementation.
  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    set_local_store(new JsonPrefStore(
        temp_dir_.path().Append(chrome::kSyncCredentialsFilename),
        file_message_loop_.message_loop_proxy()));
  }

  // testing::Test implementation.
  virtual void TearDown() OVERRIDE {
    file_message_loop_.RunAllPending();
  }

 private:
  ScopedTempDir temp_dir_;
  MessageLoop file_message_loop_;
  DISALLOW_COPY_AND_ASSIGN(CredentialCacheServiceTest);
};

TEST_F(CredentialCacheServiceTest, TestPackAndUnpack) {
  // Pack a sample credential string.
  std::string original = "sample_credential";
  scoped_ptr<base::StringValue> packed(
      syncer::CredentialCacheService::PackCredential(original));

  // Unpack the result and make sure it matches the original.
  std::string unpacked =
      syncer::CredentialCacheService::UnpackCredential(*packed);
  ASSERT_EQ(original, unpacked);
}

TEST_F(CredentialCacheServiceTest, TestPackAndUnpackEmpty) {
  // Pack an empty credential string.
  std::string original = "";
  scoped_ptr<base::StringValue> packed(
      syncer::CredentialCacheService::PackCredential(original));

  // Make sure the packed value is an empty string.
  std::string packed_string;
  packed->GetAsString(&packed_string);
  ASSERT_EQ(original, packed_string);

  // Unpack it and make sure it matches the original.
  std::string unpacked =
      syncer::CredentialCacheService::UnpackCredential(*packed);
  ASSERT_EQ(original, unpacked);
}

TEST_F(CredentialCacheServiceTest, TestWriteAndReadCredentials) {
  std::string username = "user@gmail.com";
  std::string lsid = "lsid";
  bool sync_everything = true;

  // Write a string pref, a token and a boolean pref.
  PackAndUpdateStringPref(prefs::kGoogleServicesUsername, username);
  PackAndUpdateStringPref(GaiaConstants::kGaiaLsid, lsid);
  UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything);

  // Verify that they can be read, and that they contain the original values.
  ASSERT_TRUE(HasPref(local_store(), prefs::kGoogleServicesUsername));
  ASSERT_EQ(username, GetAndUnpackStringPref(local_store(),
                                             prefs::kGoogleServicesUsername));
  ASSERT_TRUE(HasPref(local_store(), GaiaConstants::kGaiaLsid));
  ASSERT_EQ(lsid, GetAndUnpackStringPref(local_store(),
                                         GaiaConstants::kGaiaLsid));
  ASSERT_TRUE(HasPref(local_store(), prefs::kSyncKeepEverythingSynced));
  ASSERT_TRUE(sync_everything ==
              GetBooleanPref(local_store(),
                             prefs::kSyncKeepEverythingSynced));
}

TEST_F(CredentialCacheServiceTest, TestWriteAndReadCredentialsAfterSignOut) {
  std::string username = "user@gmail.com";
  std::string lsid = "lsid";
  bool sync_everything = true;

  // Write a non-empty username, indicating that the user is signed in.
  PackAndUpdateStringPref(prefs::kGoogleServicesUsername, username);
  ASSERT_TRUE(HasPref(local_store(), prefs::kGoogleServicesUsername));
  ASSERT_EQ(username, GetAndUnpackStringPref(local_store(),
                                             prefs::kGoogleServicesUsername));

  // Write an empty username, indicating that the user is signed out.
  PackAndUpdateStringPref(prefs::kGoogleServicesUsername, "");
  ASSERT_TRUE(HasPref(local_store(), prefs::kGoogleServicesUsername));
  ASSERT_EQ(std::string(),
            GetAndUnpackStringPref(local_store(),
                                   prefs::kGoogleServicesUsername));

  // Try to write a non-empty string and make sure an empty string is written in
  // its place.
  PackAndUpdateStringPref(GaiaConstants::kGaiaLsid, lsid);
  ASSERT_TRUE(HasPref(local_store(), GaiaConstants::kGaiaLsid));
  ASSERT_EQ(std::string(), GetAndUnpackStringPref(local_store(),
                                                  GaiaConstants::kGaiaLsid));

  // Try to write a boolean pref with value true, and make sure an the default
  // value of false is written in its place.
  UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything);
  ASSERT_TRUE(HasPref(local_store(), prefs::kSyncKeepEverythingSynced));
  ASSERT_TRUE(false == GetBooleanPref(local_store(),
                                      prefs::kSyncKeepEverythingSynced));
}

TEST_F(CredentialCacheServiceTest, TestWriteAndReadTimestamps) {
  // Write a timestamp.
  WriteLastCacheUpdateTime();

  // Make sure that the correct field was written, and it is not in the future.
  base::Time time(GetLastCacheUpdateTime(local_store()));
  ASSERT_LE(time, base::Time::Now());
}

}  // namespace syncer