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 testing::Test { |
| 22 public: |
| 23 CredentialCacheServiceTest() |
| 24 : CredentialCacheService(NULL), |
| 25 file_message_loop_(MessageLoop::TYPE_IO) {} |
| 26 |
| 27 virtual ~CredentialCacheServiceTest() {} |
| 28 |
| 29 // testing::Test implementation. |
| 30 virtual void SetUp() OVERRIDE { |
| 31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 32 set_local_store(new JsonPrefStore( |
| 33 temp_dir_.path().Append(chrome::kSyncCredentialsFilename), |
| 34 file_message_loop_.message_loop_proxy())); |
| 35 } |
| 36 |
| 37 // testing::Test implementation. |
| 38 virtual void TearDown() OVERRIDE { |
| 39 file_message_loop_.RunAllPending(); |
| 40 } |
| 41 |
| 42 // PrefStore::Observer implementation. |
| 43 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {} |
| 44 |
| 45 private: |
| 46 ScopedTempDir temp_dir_; |
| 47 MessageLoop file_message_loop_; |
| 48 DISALLOW_COPY_AND_ASSIGN(CredentialCacheServiceTest); |
| 49 }; |
| 50 |
| 51 TEST_F(CredentialCacheServiceTest, TestPackAndUnpack) { |
| 52 // Pack a sample credential string. |
| 53 std::string original = "sample_credential"; |
| 54 scoped_ptr<base::StringValue> packed( |
| 55 syncer::CredentialCacheService::PackCredential(original)); |
| 56 |
| 57 // Unpack the result and make sure it matches the original. |
| 58 std::string unpacked = |
| 59 syncer::CredentialCacheService::UnpackCredential(*packed); |
| 60 ASSERT_EQ(original, unpacked); |
| 61 } |
| 62 |
| 63 TEST_F(CredentialCacheServiceTest, TestPackAndUnpackEmpty) { |
| 64 // Pack an empty credential string. |
| 65 std::string original = ""; |
| 66 scoped_ptr<base::StringValue> packed( |
| 67 syncer::CredentialCacheService::PackCredential(original)); |
| 68 |
| 69 // Make sure the packed value is an empty string. |
| 70 std::string packed_string; |
| 71 packed->GetAsString(&packed_string); |
| 72 ASSERT_EQ(original, packed_string); |
| 73 |
| 74 // Unpack it and make sure it matches the original. |
| 75 std::string unpacked = |
| 76 syncer::CredentialCacheService::UnpackCredential(*packed); |
| 77 ASSERT_EQ(original, unpacked); |
| 78 } |
| 79 |
| 80 TEST_F(CredentialCacheServiceTest, TestWriteAndReadCredentials) { |
| 81 std::string username = "user@gmail.com"; |
| 82 std::string lsid = "lsid"; |
| 83 bool sync_everything = true; |
| 84 |
| 85 // Write a string pref, a token and a boolean pref. |
| 86 PackAndUpdateStringPref(prefs::kGoogleServicesUsername, username); |
| 87 PackAndUpdateStringPref(GaiaConstants::kGaiaLsid, lsid); |
| 88 UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything); |
| 89 |
| 90 // Verify that they can be read, and that they contain the original values. |
| 91 ASSERT_TRUE(HasPref(local_store(), prefs::kGoogleServicesUsername)); |
| 92 ASSERT_EQ(username, GetAndUnpackStringPref(local_store(), |
| 93 prefs::kGoogleServicesUsername)); |
| 94 ASSERT_TRUE(HasPref(local_store(), GaiaConstants::kGaiaLsid)); |
| 95 ASSERT_EQ(lsid, GetAndUnpackStringPref(local_store(), |
| 96 GaiaConstants::kGaiaLsid)); |
| 97 ASSERT_TRUE(HasPref(local_store(), prefs::kSyncKeepEverythingSynced)); |
| 98 ASSERT_TRUE(sync_everything == |
| 99 GetBooleanPref(local_store(), |
| 100 prefs::kSyncKeepEverythingSynced)); |
| 101 } |
| 102 |
| 103 TEST_F(CredentialCacheServiceTest, TestWriteAndReadCredentialsAfterSignOut) { |
| 104 std::string username = "user@gmail.com"; |
| 105 std::string lsid = "lsid"; |
| 106 bool sync_everything = true; |
| 107 |
| 108 // Write a non-empty username, indicating that the user is signed in. |
| 109 PackAndUpdateStringPref(prefs::kGoogleServicesUsername, username); |
| 110 ASSERT_TRUE(HasPref(local_store(), prefs::kGoogleServicesUsername)); |
| 111 ASSERT_EQ(username, GetAndUnpackStringPref(local_store(), |
| 112 prefs::kGoogleServicesUsername)); |
| 113 |
| 114 // Write an empty username, indicating that the user is signed out. |
| 115 PackAndUpdateStringPref(prefs::kGoogleServicesUsername, ""); |
| 116 ASSERT_TRUE(HasPref(local_store(), prefs::kGoogleServicesUsername)); |
| 117 ASSERT_EQ(std::string(), |
| 118 GetAndUnpackStringPref(local_store(), |
| 119 prefs::kGoogleServicesUsername)); |
| 120 |
| 121 // Try to write a non-empty string and make sure an empty string is written in |
| 122 // its place. |
| 123 PackAndUpdateStringPref(GaiaConstants::kGaiaLsid, lsid); |
| 124 ASSERT_TRUE(HasPref(local_store(), GaiaConstants::kGaiaLsid)); |
| 125 ASSERT_EQ(std::string(), GetAndUnpackStringPref(local_store(), |
| 126 GaiaConstants::kGaiaLsid)); |
| 127 |
| 128 // Try to write a boolean pref with value true, and make sure an the default |
| 129 // value of false is written in its place. |
| 130 UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, sync_everything); |
| 131 ASSERT_TRUE(HasPref(local_store(), prefs::kSyncKeepEverythingSynced)); |
| 132 ASSERT_TRUE(false == GetBooleanPref(local_store(), |
| 133 prefs::kSyncKeepEverythingSynced)); |
| 134 } |
| 135 |
| 136 } // namespace syncer |
OLD | NEW |