| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE entry. | 3 // found in the LICENSE entry. |
| 4 | 4 |
| 5 #include "base/scoped_ptr.h" | 5 #include "base/scoped_ptr.h" |
| 6 #include "base/scoped_temp_dir.h" | 6 #include "base/scoped_temp_dir.h" |
| 7 #include "base/test/test_file_util.h" | 7 #include "base/test/test_file_util.h" |
| 8 #include "base/waitable_event.h" | 8 #include "base/waitable_event.h" |
| 9 #include "chrome/browser/sync/engine/all_status.h" | 9 #include "chrome/browser/sync/engine/all_status.h" |
| 10 #include "chrome/browser/sync/engine/auth_watcher.h" | 10 #include "chrome/browser/sync/engine/auth_watcher.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 static const char* kUserDisplayName = "Mr. Auth Watcher"; | 25 static const char* kUserDisplayName = "Mr. Auth Watcher"; |
| 26 static const char* kUserDisplayEmail = "authwatcherdisplay@gmail.com"; | 26 static const char* kUserDisplayEmail = "authwatcherdisplay@gmail.com"; |
| 27 static const char* kTestEmail = "authwatchertest@gmail.com"; | 27 static const char* kTestEmail = "authwatchertest@gmail.com"; |
| 28 static const char* kWrongPassword = "wrongpassword"; | 28 static const char* kWrongPassword = "wrongpassword"; |
| 29 static const char* kCorrectPassword = "correctpassword"; | 29 static const char* kCorrectPassword = "correctpassword"; |
| 30 static const char* kValidSID = "validSID"; | 30 static const char* kValidSID = "validSID"; |
| 31 static const char* kValidLSID = "validLSID"; | 31 static const char* kValidLSID = "validLSID"; |
| 32 static const char* kInvalidAuthToken = "invalidAuthToken"; | 32 static const char* kInvalidAuthToken = "invalidAuthToken"; |
| 33 static const char* kValidAuthToken = "validAuthToken"; | 33 static const char* kValidAuthToken = "validAuthToken"; |
| 34 | 34 |
| 35 namespace { | 35 namespace browser_sync { |
| 36 | 36 |
| 37 class GaiaAuthMock : public browser_sync::GaiaAuthenticator { | 37 class GaiaAuthMockForAuthWatcher : public browser_sync::GaiaAuthenticator { |
| 38 public: | 38 public: |
| 39 GaiaAuthMock() : browser_sync::GaiaAuthenticator( | 39 GaiaAuthMockForAuthWatcher() : browser_sync::GaiaAuthenticator( |
| 40 kTestUserAgent, kTestServiceId, kTestGaiaURL), | 40 kTestUserAgent, kTestServiceId, kTestGaiaURL), |
| 41 use_bad_auth_token_(false) {} | 41 use_bad_auth_token_(false) {} |
| 42 virtual ~GaiaAuthMock() {} | 42 virtual ~GaiaAuthMockForAuthWatcher() {} |
| 43 | 43 |
| 44 void SendBadAuthTokenForNextRequest() { use_bad_auth_token_ = true; } | 44 void SendBadAuthTokenForNextRequest() { use_bad_auth_token_ = true; } |
| 45 | 45 |
| 46 protected: | 46 protected: |
| 47 bool PerformGaiaRequest(const AuthParams& params, AuthResults* results) { | 47 bool PerformGaiaRequest(const AuthParams& params, AuthResults* results) { |
| 48 if (params.password == kWrongPassword) { | 48 if (params.password == kWrongPassword) { |
| 49 results->auth_error = browser_sync::BadAuthentication; | 49 results->auth_error = browser_sync::BadAuthentication; |
| 50 return false; | 50 return false; |
| 51 } | 51 } |
| 52 if (params.password == kCorrectPassword) { | 52 if (params.password == kCorrectPassword) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 64 bool LookupEmail(AuthResults* results) { | 64 bool LookupEmail(AuthResults* results) { |
| 65 results->signin = GMAIL_SIGNIN; | 65 results->signin = GMAIL_SIGNIN; |
| 66 return true; | 66 return true; |
| 67 } | 67 } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 // Whether we should send an invalid auth token on the next request. | 70 // Whether we should send an invalid auth token on the next request. |
| 71 bool use_bad_auth_token_; | 71 bool use_bad_auth_token_; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 } // namespace | |
| 75 | |
| 76 namespace browser_sync { | |
| 77 | |
| 78 class AuthWatcherTest : public testing::Test { | 74 class AuthWatcherTest : public testing::Test { |
| 79 public: | 75 public: |
| 80 AuthWatcherTest() : metadb_(kUserDisplayEmail), | 76 AuthWatcherTest() : metadb_(kUserDisplayEmail), |
| 81 consumer_ready(false, false), | 77 consumer_ready(false, false), |
| 82 event_produced(false, false) {} | 78 event_produced(false, false) {} |
| 83 virtual void SetUp() { | 79 virtual void SetUp() { |
| 84 metadb_.SetUp(); | 80 metadb_.SetUp(); |
| 85 connection_.reset(new MockConnectionManager(metadb_.manager(), | 81 connection_.reset(new MockConnectionManager(metadb_.manager(), |
| 86 metadb_.name())); | 82 metadb_.name())); |
| 87 // Mock out data that would normally be sent back from a server. | 83 // Mock out data that would normally be sent back from a server. |
| 88 connection()->SetAuthenticationResponseInfo(kValidAuthToken, | 84 connection()->SetAuthenticationResponseInfo(kValidAuthToken, |
| 89 kUserDisplayName, kUserDisplayEmail, "ID"); | 85 kUserDisplayName, kUserDisplayEmail, "ID"); |
| 90 allstatus_.reset(new AllStatus()); | 86 allstatus_.reset(new AllStatus()); |
| 91 user_settings_.reset(new UserSettings()); | 87 user_settings_.reset(new UserSettings()); |
| 92 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 93 PathString user_settings_path = temp_dir_.path().value() + kUserSettingsDB; | 89 PathString user_settings_path = temp_dir_.path().value() + kUserSettingsDB; |
| 94 user_settings_->Init(user_settings_path); | 90 user_settings_->Init(user_settings_path); |
| 95 gaia_auth_ = new GaiaAuthMock(); | 91 gaia_auth_ = new GaiaAuthMockForAuthWatcher(); |
| 96 talk_mediator_.reset(new TalkMediatorImpl()); | 92 talk_mediator_.reset(new TalkMediatorImpl()); |
| 97 auth_watcher_ = new AuthWatcher(metadb_.manager(), connection_.get(), | 93 auth_watcher_ = new AuthWatcher(metadb_.manager(), connection_.get(), |
| 98 allstatus_.get(), kTestUserAgent, kTestServiceId, kTestGaiaURL, | 94 allstatus_.get(), kTestUserAgent, kTestServiceId, kTestGaiaURL, |
| 99 user_settings_.get(), gaia_auth_, talk_mediator_.get()); | 95 user_settings_.get(), gaia_auth_, talk_mediator_.get()); |
| 100 authwatcher_hookup_.reset(NewEventListenerHookup(auth_watcher_->channel(), | 96 authwatcher_hookup_.reset(NewEventListenerHookup(auth_watcher_->channel(), |
| 101 this, &AuthWatcherTest::HandleAuthWatcherEvent)); | 97 this, &AuthWatcherTest::HandleAuthWatcherEvent)); |
| 102 } | 98 } |
| 103 | 99 |
| 104 virtual void TearDown() { | 100 virtual void TearDown() { |
| 105 metadb_.TearDown(); | 101 metadb_.TearDown(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 120 } | 116 } |
| 121 | 117 |
| 122 AuthWatcherEvent::WhatHappened ConsumeNextEvent() { | 118 AuthWatcherEvent::WhatHappened ConsumeNextEvent() { |
| 123 consumer_ready.Signal(); | 119 consumer_ready.Signal(); |
| 124 event_produced.Wait(); | 120 event_produced.Wait(); |
| 125 return last_event_reason_; | 121 return last_event_reason_; |
| 126 } | 122 } |
| 127 | 123 |
| 128 AuthWatcher* auth_watcher() { return auth_watcher_.get(); } | 124 AuthWatcher* auth_watcher() { return auth_watcher_.get(); } |
| 129 MockConnectionManager* connection() { return connection_.get(); } | 125 MockConnectionManager* connection() { return connection_.get(); } |
| 130 GaiaAuthMock* gaia_auth() { return gaia_auth_; } | 126 GaiaAuthMockForAuthWatcher* gaia_auth() { return gaia_auth_; } |
| 131 const std::string& user_email() { return user_email_; } | 127 const std::string& user_email() { return user_email_; } |
| 132 | 128 |
| 133 private: | 129 private: |
| 134 // Responsible for creating / deleting a temp dir containing user settings DB. | 130 // Responsible for creating / deleting a temp dir containing user settings DB. |
| 135 ScopedTempDir temp_dir_; | 131 ScopedTempDir temp_dir_; |
| 136 | 132 |
| 137 // The event listener hookup registered for HandleAuthWatcherEvent. | 133 // The event listener hookup registered for HandleAuthWatcherEvent. |
| 138 scoped_ptr<EventListenerHookup> authwatcher_hookup_; | 134 scoped_ptr<EventListenerHookup> authwatcher_hookup_; |
| 139 | 135 |
| 140 // The sync engine pieces necessary to run an AuthWatcher. | 136 // The sync engine pieces necessary to run an AuthWatcher. |
| 141 TriggeredOpenTestDirectorySetterUpper metadb_; | 137 TriggeredOpenTestDirectorySetterUpper metadb_; |
| 142 scoped_ptr<MockConnectionManager> connection_; | 138 scoped_ptr<MockConnectionManager> connection_; |
| 143 scoped_ptr<AllStatus> allstatus_; | 139 scoped_ptr<AllStatus> allstatus_; |
| 144 scoped_ptr<UserSettings> user_settings_; | 140 scoped_ptr<UserSettings> user_settings_; |
| 145 GaiaAuthMock* gaia_auth_; // Owned by auth_watcher_. | 141 GaiaAuthMockForAuthWatcher* gaia_auth_; // Owned by auth_watcher_. |
| 146 scoped_ptr<TalkMediator> talk_mediator_; | 142 scoped_ptr<TalkMediator> talk_mediator_; |
| 147 scoped_refptr<AuthWatcher> auth_watcher_; | 143 scoped_refptr<AuthWatcher> auth_watcher_; |
| 148 | 144 |
| 149 // This is used to block the AuthWatcherThread when it raises events until we | 145 // This is used to block the AuthWatcherThread when it raises events until we |
| 150 // are ready to read the event. It is not a manual-reset event, so it goes | 146 // are ready to read the event. It is not a manual-reset event, so it goes |
| 151 // straight back to non-signaled after one thread (the main thread) is | 147 // straight back to non-signaled after one thread (the main thread) is |
| 152 // signaled (or "consumes" the signaled state). | 148 // signaled (or "consumes" the signaled state). |
| 153 base::WaitableEvent consumer_ready; | 149 base::WaitableEvent consumer_ready; |
| 154 | 150 |
| 155 // This is signaled by the AuthWatcherThread after it sets last_event_reason_ | 151 // This is signaled by the AuthWatcherThread after it sets last_event_reason_ |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 EXPECT_EQ(AuthWatcherEvent::SERVICE_AUTH_FAILED, ConsumeNextEvent()); | 202 EXPECT_EQ(AuthWatcherEvent::SERVICE_AUTH_FAILED, ConsumeNextEvent()); |
| 207 } | 203 } |
| 208 | 204 |
| 209 TEST_F(AuthWatcherTest, AuthenticateWithTokenSuccess) { | 205 TEST_F(AuthWatcherTest, AuthenticateWithTokenSuccess) { |
| 210 auth_watcher()->AuthenticateWithToken(kTestEmail, kValidAuthToken); | 206 auth_watcher()->AuthenticateWithToken(kTestEmail, kValidAuthToken); |
| 211 EXPECT_EQ(AuthWatcherEvent::AUTH_SUCCEEDED, ConsumeNextEvent()); | 207 EXPECT_EQ(AuthWatcherEvent::AUTH_SUCCEEDED, ConsumeNextEvent()); |
| 212 EXPECT_EQ(kUserDisplayEmail, user_email()); | 208 EXPECT_EQ(kUserDisplayEmail, user_email()); |
| 213 } | 209 } |
| 214 | 210 |
| 215 } // namespace browser_sync | 211 } // namespace browser_sync |
| OLD | NEW |