OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 <string> |
| 6 |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/scoped_temp_dir.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/sync/credential_cache_service_win.h" |
| 13 #include "chrome/common/chrome_constants.h" |
| 14 #include "chrome/common/net/gaia/gaia_constants.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace syncer { |
| 19 |
| 20 class CredentialCacheServiceTest : public CredentialCacheService { |
| 21 public: |
| 22 CredentialCacheServiceTest() |
| 23 : CredentialCacheService(NULL), |
| 24 file_message_loop_(MessageLoop::TYPE_IO) { |
| 25 SetUp(); |
| 26 } |
| 27 |
| 28 virtual ~CredentialCacheServiceTest() { |
| 29 file_message_loop_.RunAllPending(); |
| 30 } |
| 31 |
| 32 void SetUp() { |
| 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 34 local_store_ = new JsonPrefStore( |
| 35 temp_dir_.path().Append(chrome::kSyncCredentialsFilename), |
| 36 file_message_loop_.message_loop_proxy()); |
| 37 } |
| 38 |
| 39 // ProfileKeyedService implementation. |
| 40 virtual void Shutdown() OVERRIDE {} |
| 41 |
| 42 // PrefStore::Observer implementation. |
| 43 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {} |
| 44 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE {} |
| 45 |
| 46 scoped_refptr<JsonPrefStore> local_store() { return local_store_; } |
| 47 |
| 48 private: |
| 49 ScopedTempDir temp_dir_; |
| 50 MessageLoop file_message_loop_; |
| 51 DISALLOW_COPY_AND_ASSIGN(CredentialCacheServiceTest); |
| 52 }; |
| 53 |
| 54 } // namespace syncer |
| 55 |
| 56 namespace { |
| 57 |
| 58 TEST(CredentialCacheServiceTest, TestPackAndUnpack) { |
| 59 // Pack a sample credential string. |
| 60 std::string original = "sample_credential"; |
| 61 scoped_ptr<base::StringValue> packed( |
| 62 syncer::CredentialCacheService::PackCredential(original)); |
| 63 |
| 64 // Unpack the result and make sure it matches the original. |
| 65 std::string unpacked = |
| 66 syncer::CredentialCacheService::UnpackCredential(*packed); |
| 67 ASSERT_EQ(original, unpacked); |
| 68 } |
| 69 |
| 70 TEST(CredentialCacheServiceTest, TestPackAndUnpackEmpty) { |
| 71 // Pack an empty credential string. |
| 72 std::string original = ""; |
| 73 scoped_ptr<base::StringValue> packed( |
| 74 syncer::CredentialCacheService::PackCredential(original)); |
| 75 |
| 76 // Make sure the packed value is an empty string. |
| 77 std::string packed_string; |
| 78 packed->GetAsString(&packed_string); |
| 79 ASSERT_EQ(original, packed_string); |
| 80 |
| 81 // Unpack it and make sure it matches the original. |
| 82 std::string unpacked = |
| 83 syncer::CredentialCacheService::UnpackCredential(*packed); |
| 84 ASSERT_EQ(original, unpacked); |
| 85 } |
| 86 |
| 87 TEST(CredentialCacheServiceTest, TestWriteAndReadCredentials) { |
| 88 syncer::CredentialCacheServiceTest ccs; |
| 89 std::string username = "user@gmail.com"; |
| 90 std::string lsid = "lsid"; |
| 91 bool sync_everything = true; |
| 92 |
| 93 // Write a string pref, a token and a boolean pref. |
| 94 ccs.UpdateStringPref(prefs::kGoogleServicesUsername, username); |
| 95 ccs.UpdateStringPref(GaiaConstants::kGaiaLsid, lsid); |
| 96 ccs.UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything); |
| 97 |
| 98 // Verify that they can be read, and that they contain the original values. |
| 99 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kGoogleServicesUsername)); |
| 100 ASSERT_EQ(username, ccs.GetStringPref(ccs.local_store(), |
| 101 prefs::kGoogleServicesUsername)); |
| 102 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), GaiaConstants::kGaiaLsid)); |
| 103 ASSERT_EQ(lsid, ccs.GetStringPref(ccs.local_store(), |
| 104 GaiaConstants::kGaiaLsid)); |
| 105 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kSyncKeepEverythingSynced)); |
| 106 ASSERT_TRUE(sync_everything == |
| 107 ccs.GetBooleanPref(ccs.local_store(), |
| 108 prefs::kSyncKeepEverythingSynced)); |
| 109 } |
| 110 |
| 111 TEST(CredentialCacheServiceTest, TestWriteAndReadCredentialsAfterSignOut) { |
| 112 syncer::CredentialCacheServiceTest ccs; |
| 113 std::string username = "user@gmail.com"; |
| 114 std::string lsid = "lsid"; |
| 115 bool sync_everything = true; |
| 116 |
| 117 // Write a non-empty username, indicating that the user is signed in. |
| 118 ccs.UpdateStringPref(prefs::kGoogleServicesUsername, username); |
| 119 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kGoogleServicesUsername)); |
| 120 ASSERT_EQ(username, ccs.GetStringPref(ccs.local_store(), |
| 121 prefs::kGoogleServicesUsername)); |
| 122 |
| 123 // Write an empty username, indicating that the user is signed out. |
| 124 ccs.UpdateStringPref(prefs::kGoogleServicesUsername, ""); |
| 125 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kGoogleServicesUsername)); |
| 126 ASSERT_EQ(std::string(), ccs.GetStringPref(ccs.local_store(), |
| 127 prefs::kGoogleServicesUsername)); |
| 128 |
| 129 // Try to write a non-empty string and make sure an empty string is written in |
| 130 // its place. |
| 131 ccs.UpdateStringPref(GaiaConstants::kGaiaLsid, lsid); |
| 132 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), GaiaConstants::kGaiaLsid)); |
| 133 ASSERT_EQ(std::string(), ccs.GetStringPref(ccs.local_store(), |
| 134 GaiaConstants::kGaiaLsid)); |
| 135 |
| 136 // Try to write a boolean pref with value true, and make sure an the default |
| 137 // value of false is written in its place. |
| 138 ccs.UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything); |
| 139 ASSERT_TRUE(ccs.HasPref(ccs.local_store(), prefs::kSyncKeepEverythingSynced)); |
| 140 ASSERT_TRUE(false == ccs.GetBooleanPref(ccs.local_store(), |
| 141 prefs::kSyncKeepEverythingSynced)); |
| 142 } |
| 143 |
| 144 } // namespace |
OLD | NEW |