summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/engine/auth_watcher_unittest.cc
diff options
context:
space:
mode:
authorjohnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-08 01:38:35 +0000
committerjohnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-08 01:38:35 +0000
commit265d8c32bd4dbb4b1a142c8204b434a03e8a8c8f (patch)
tree105095d5eead4db62bbf60ed0b3f1d243b202e76 /chrome/browser/sync/engine/auth_watcher_unittest.cc
parent8b20191a72da085319a68e4953e9f2b355218825 (diff)
downloadchromium_src-265d8c32bd4dbb4b1a142c8204b434a03e8a8c8f.zip
chromium_src-265d8c32bd4dbb4b1a142c8204b434a03e8a8c8f.tar.gz
chromium_src-265d8c32bd4dbb4b1a142c8204b434a03e8a8c8f.tar.bz2
Revert 58778 - New authorization framework for sync.
To quote chron's original patch (http://codereview.chromium.org/3148036/show) <blockquote> This patch removes: authenticator.cc, auth_watcher.cc removes calls to user_settings.cc, removes an authenticate PB request to the server, and moves token storage into the Chrome TokenService. This patch introduces the SigninManager, which is an interim solution for user management prior to moving the system into chrome. Other changes include removing the dependency on the sync backend to be running while the sync wizard is intially displayed. This means that the backend can be brought up in response to credentials becoming available. The backend now is always provided credentials on startup. If an auth error occurs, it propogates it up via a notification. Some event handlers were removed and streamlined for more straightforward sync system startup. </blockquote> BUG=51001, 50293, 35158 TEST=Unit tests && Start up sync, log in, log out, run with expired credentials, run with new gaia credentials, run with gaia credentials updated while system is syncing. Try logging in with incorrect username. Trigger CAPTCHA. Try logging out and in repeatedly. Check about:sync works. Try going offline and back online again. Expire gaia credentials and try renewing it with the UI dialog. Review URL: http://codereview.chromium.org/3305003 TBR=johnnyg@chromium.org Review URL: http://codereview.chromium.org/3310019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58782 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync/engine/auth_watcher_unittest.cc')
-rw-r--r--chrome/browser/sync/engine/auth_watcher_unittest.cc235
1 files changed, 235 insertions, 0 deletions
diff --git a/chrome/browser/sync/engine/auth_watcher_unittest.cc b/chrome/browser/sync/engine/auth_watcher_unittest.cc
new file mode 100644
index 0000000..cc840c7
--- /dev/null
+++ b/chrome/browser/sync/engine/auth_watcher_unittest.cc
@@ -0,0 +1,235 @@
+// Copyright (c) 2010 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 "base/scoped_ptr.h"
+#include "base/scoped_temp_dir.h"
+#include "base/test/test_file_util.h"
+#include "base/waitable_event.h"
+#include "chrome/browser/password_manager/encryptor.h"
+#include "chrome/browser/sync/engine/auth_watcher.h"
+#include "chrome/browser/sync/engine/syncer_thread.h"
+#include "chrome/browser/sync/util/user_settings.h"
+#include "chrome/common/deprecated/event_sys-inl.h"
+#include "chrome/common/net/http_return.h"
+#include "chrome/common/net/gaia/gaia_authenticator.h"
+#include "chrome/test/sync/engine/mock_connection_manager.h"
+#include "chrome/test/sync/engine/test_directory_setter_upper.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+static FilePath::CharType kUserSettingsDB[] =
+ FILE_PATH_LITERAL("Settings.sqlite3");
+static const char* kTestUserAgent = "useragent";
+static const char* kTestServiceId = "serviceid";
+static const char* kTestGaiaURL = "http://gaia_url";
+static const char* kUserDisplayName = "Mr. Auth Watcher";
+static const char* kUserDisplayEmail = "authwatcherdisplay@gmail.com";
+static const char* kTestEmail = "authwatchertest@gmail.com";
+static const char* kWrongPassword = "wrongpassword";
+static const char* kCorrectPassword = "correctpassword";
+static const char* kValidSID = "validSID";
+static const char* kValidLSID = "validLSID";
+static const char* kInvalidAuthToken = "invalidAuthToken";
+static const char* kValidAuthToken = "validAuthToken";
+
+namespace browser_sync {
+
+class GaiaAuthMockForAuthWatcher : public gaia::GaiaAuthenticator {
+ public:
+ GaiaAuthMockForAuthWatcher() : GaiaAuthenticator(
+ kTestUserAgent, kTestServiceId, kTestGaiaURL),
+ use_bad_auth_token_(false) {}
+ virtual ~GaiaAuthMockForAuthWatcher() {}
+
+ virtual int GetBackoffDelaySeconds(
+ int current_backoff_delay) {
+ return SyncerThread::GetRecommendedDelaySeconds(current_backoff_delay);
+ }
+
+ void SendBadAuthTokenForNextRequest() { use_bad_auth_token_ = true; }
+
+ std::string renewed_token() {
+ return renewed_token_;
+ }
+
+ protected:
+ bool PerformGaiaRequest(const AuthParams& params, AuthResults* results) {
+ if (params.password == kWrongPassword) {
+ results->auth_error = gaia::BadAuthentication;
+ return false;
+ }
+ if (params.password == kCorrectPassword) {
+ results->sid = kValidSID;
+ results->lsid = kValidLSID;
+ results->auth_token = kValidAuthToken;
+ }
+ if (use_bad_auth_token_) {
+ results->auth_token = kInvalidAuthToken;
+ use_bad_auth_token_ = false;
+ }
+ return true;
+ }
+
+ void RenewAuthToken(const std::string& auth_token) {
+ renewed_token_ = auth_token;
+ }
+
+ private:
+ // Whether we should send an invalid auth token on the next request.
+ bool use_bad_auth_token_;
+ std::string renewed_token_;
+};
+
+class AuthWatcherTest : public testing::Test {
+ public:
+ AuthWatcherTest() : metadb_(kUserDisplayEmail),
+ consumer_ready(false, false),
+ event_produced(false, false),
+ last_event_reason_(AuthWatcherEvent::ILLEGAL_VALUE) {}
+ virtual void SetUp() {
+#if defined(OS_MACOSX)
+ // Need to mock the Keychain for unit tests on Mac to avoid possible
+ // blocking UI. |SetAuthTokenForService| uses Encryptor.
+ Encryptor::UseMockKeychain(true);
+#endif
+ metadb_.SetUp();
+ connection_.reset(new MockConnectionManager(metadb_.manager(),
+ metadb_.name()));
+ // Mock out data that would normally be sent back from a server.
+ connection()->SetAuthenticationResponseInfo(kValidAuthToken,
+ kUserDisplayName, kUserDisplayEmail, "ID");
+ user_settings_.reset(new UserSettings());
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ FilePath user_settings_path = temp_dir_.path().Append(kUserSettingsDB);
+ user_settings_->Init(user_settings_path);
+ gaia_auth_ = new GaiaAuthMockForAuthWatcher();
+ auth_watcher_ = new AuthWatcher(metadb_.manager(), connection_.get(),
+ kTestUserAgent, kTestServiceId, kTestGaiaURL,
+ user_settings_.get(), gaia_auth_);
+ authwatcher_hookup_.reset(NewEventListenerHookup(auth_watcher_->channel(),
+ this, &AuthWatcherTest::HandleAuthWatcherEvent));
+ }
+
+ virtual void TearDown() {
+ metadb_.TearDown();
+ auth_watcher_->Shutdown();
+ EXPECT_FALSE(auth_watcher()->message_loop());
+ }
+
+ void HandleAuthWatcherEvent(const AuthWatcherEvent& event) {
+ if (event.what_happened == AuthWatcherEvent::AUTHWATCHER_DESTROYED)
+ return;
+ consumer_ready.Wait(); // Block progress until the test is ready.
+
+ last_event_reason_ = event.what_happened;
+ if (event.what_happened == AuthWatcherEvent::AUTH_SUCCEEDED)
+ user_email_ = event.user_email;
+
+ event_produced.Signal();
+ }
+
+ AuthWatcherEvent::WhatHappened ConsumeNextEvent() {
+ consumer_ready.Signal();
+ event_produced.Wait();
+ return last_event_reason_;
+ }
+
+ AuthWatcher* auth_watcher() { return auth_watcher_.get(); }
+ MockConnectionManager* connection() { return connection_.get(); }
+ GaiaAuthMockForAuthWatcher* gaia_auth() { return gaia_auth_; }
+ const std::string& user_email() { return user_email_; }
+
+ private:
+ // Responsible for creating / deleting a temp dir containing user settings DB.
+ ScopedTempDir temp_dir_;
+
+ // The event listener hookup registered for HandleAuthWatcherEvent.
+ scoped_ptr<EventListenerHookup> authwatcher_hookup_;
+
+ // The sync engine pieces necessary to run an AuthWatcher.
+ TriggeredOpenTestDirectorySetterUpper metadb_;
+ scoped_ptr<MockConnectionManager> connection_;
+ scoped_ptr<UserSettings> user_settings_;
+ GaiaAuthMockForAuthWatcher* gaia_auth_; // Owned by auth_watcher_.
+ scoped_refptr<AuthWatcher> auth_watcher_;
+
+ // This is used to block the AuthWatcherThread when it raises events until we
+ // are ready to read the event. It is not a manual-reset event, so it goes
+ // straight back to non-signaled after one thread (the main thread) is
+ // signaled (or "consumes" the signaled state).
+ base::WaitableEvent consumer_ready;
+
+ // This is signaled by the AuthWatcherThread after it sets last_event_reason_
+ // and possibly user_email_ for us to read.
+ base::WaitableEvent event_produced;
+
+ // The 'WhatHappened' value from the last AuthWatcherEvent we handled.
+ AuthWatcherEvent::WhatHappened last_event_reason_;
+
+ // Set when we receive an AUTH_SUCCEEDED event.
+ std::string user_email_;
+
+ DISALLOW_COPY_AND_ASSIGN(AuthWatcherTest);
+};
+
+TEST_F(AuthWatcherTest, Construction) {
+ EXPECT_TRUE(auth_watcher()->message_loop());
+ EXPECT_EQ("SyncEngine_AuthWatcherThread",
+ auth_watcher()->message_loop()->thread_name());
+ EXPECT_TRUE(auth_watcher()->auth_backend_thread_.IsRunning());
+ EXPECT_EQ(AuthWatcher::NOT_AUTHENTICATED, auth_watcher()->status());
+}
+
+TEST_F(AuthWatcherTest, AuthenticateGaiaAuthFailure) {
+ auth_watcher()->Authenticate(kTestEmail, kWrongPassword,
+ std::string(), // captcha_token
+ std::string()); // captcha_value
+
+ EXPECT_EQ(AuthWatcherEvent::AUTHENTICATION_ATTEMPT_START, ConsumeNextEvent());
+ EXPECT_EQ(AuthWatcherEvent::GAIA_AUTH_FAILED, ConsumeNextEvent());
+}
+
+TEST_F(AuthWatcherTest, AuthenticateBadAuthToken) {
+ gaia_auth()->SendBadAuthTokenForNextRequest();
+ auth_watcher()->Authenticate(kTestEmail, kCorrectPassword, std::string(),
+ std::string());
+ EXPECT_EQ(AuthWatcherEvent::AUTHENTICATION_ATTEMPT_START, ConsumeNextEvent());
+ EXPECT_EQ(AuthWatcherEvent::SERVICE_AUTH_FAILED, ConsumeNextEvent());
+}
+
+TEST_F(AuthWatcherTest, AuthenticateSuccess) {
+ auth_watcher()->Authenticate(kTestEmail, kCorrectPassword, std::string(),
+ std::string());
+ EXPECT_EQ(AuthWatcherEvent::AUTHENTICATION_ATTEMPT_START, ConsumeNextEvent());
+ EXPECT_EQ(AuthWatcherEvent::AUTH_SUCCEEDED, ConsumeNextEvent());
+
+ // The server responds with a different email than what we used in the call
+ // to Authenticate, and the AuthWatcher should have told us about.
+ EXPECT_EQ(kUserDisplayEmail, user_email());
+}
+
+TEST_F(AuthWatcherTest, AuthenticateWithTokenBadAuthToken) {
+ auth_watcher()->AuthenticateWithToken(kTestEmail, kInvalidAuthToken);
+ EXPECT_EQ(AuthWatcherEvent::SERVICE_AUTH_FAILED, ConsumeNextEvent());
+}
+
+TEST_F(AuthWatcherTest, AuthenticateWithTokenSuccess) {
+ auth_watcher()->AuthenticateWithToken(kTestEmail, kValidAuthToken);
+ EXPECT_EQ(AuthWatcherEvent::AUTH_SUCCEEDED, ConsumeNextEvent());
+ EXPECT_EQ(kUserDisplayEmail, user_email());
+}
+
+// Just check that the thread task was properly issued.
+TEST_F(AuthWatcherTest, RenewAuthToken) {
+ auth_watcher()->Authenticate(kTestEmail, kCorrectPassword, std::string(),
+ std::string());
+ EXPECT_EQ(AuthWatcherEvent::AUTHENTICATION_ATTEMPT_START, ConsumeNextEvent());
+ EXPECT_EQ(AuthWatcherEvent::AUTH_SUCCEEDED, ConsumeNextEvent());
+
+ auth_watcher()->RenewAuthToken("updated_token");
+ EXPECT_EQ(AuthWatcherEvent::AUTH_RENEWED, ConsumeNextEvent());
+ EXPECT_EQ(gaia_auth()->renewed_token(), "updated_token");
+ EXPECT_EQ(connection()->auth_token(), "updated_token");
+}
+
+} // namespace browser_sync