Logo Search packages:      
Sourcecode: chromium-browser version File versions  Download package

mock_gaia_authenticator_unittest.cc

// Copyright (c) 2009 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.
//
// Unit tests for MockGaiaAuthenticator.

#include "base/basictypes.h"
#include "base/port.h"
#include "chrome/browser/sync/engine/net/gaia_authenticator.h"
#include "chrome/browser/sync/protocol/service_constants.h"
#include "chrome/test/sync/engine/mock_gaia_authenticator.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Test if authentication succeeds for a mock user added earlier.
TEST(MockGaiaAuthenticatorTest, TestAuthenticationSuccess) {
  browser_sync::MockGaiaAuthenticator
    mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME,
                   "some random url");

  // Initialize a mock user, and add to mock authenticator.
  browser_sync::MockUser mock_user;
  mock_user.email = "test";
  mock_user.passwd = "passwd";
  mock_user.auth_token = "SomeAuthToken";
  mock_user.lsid = "SomeLSID";
  mock_user.sid = "SomeSID";
  mock_user.auth_error = browser_sync::None;
  mock_gaia_auth.AddMockUser(mock_user);

  // Assert away ...
  ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd"));
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID");
}

// Test if authentication fails for a mock user that was never added.
TEST(MockGaiaAuthenticatorTest, TestAuthenticationFailure) {
  browser_sync::MockGaiaAuthenticator
    mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME,
                   "some random url");

  // At this point, in real code, we would be adding mock users to our mock
  // object. However, in this unittest, we exercise the path where this step is
  // missing, and assert that the outcome is still consistent with that of the
  // real GaiaAuthenticator.

  // Assert away ...
  ASSERT_FALSE(mock_gaia_auth.Authenticate("test", "passwd"));
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "");
}

// Test if authentication fails after a mock user is removed.
TEST(MockGaiaAuthenticatorTest, TestRemoveMockUser) {
  // Instantiate authenticator.
  browser_sync::MockGaiaAuthenticator
    mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME,
                   "some random url");

  // Add our mock user
  mock_gaia_auth.AddMockUser("test", "passwd", "SomeAuthToken", "SomeLSID",
      "SomeSID", browser_sync::None);

  // Make sure authentication succeeds.
  ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd"));
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID");

  // Remove the just-added user from our list.
  mock_gaia_auth.RemoveMockUser("test");

  // ... and authentication should fail.
  ASSERT_FALSE(mock_gaia_auth.Authenticate("test", "passwd"));
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "");
}

// Test if authentication fails after all mock users are removed.
TEST(MockGaiaAuthenticatorTest, TestRemoveAllMockUsers) {
  // Instantiate authenticator.
  browser_sync::MockGaiaAuthenticator
    mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME,
                   "some random url");

  // Add our sample mock user.
  mock_gaia_auth.AddMockUser("test", "passwd", "SomeAuthToken", "SomeLSID",
      "SomeSID", browser_sync::None);

  // Make sure authentication succeeds
  ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd"));
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID");

  // Now remove all mock users.
  mock_gaia_auth.RemoveAllMockUsers();

  // And confirm that authentication fails.
  ASSERT_FALSE(mock_gaia_auth.Authenticate("test", "passwd"));
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "");
}

// Test authentication with saved credentials.
TEST(MockGaiaAuthenticatorTest, TestSavedCredentials) {
  // Instantiate authenticator.
  browser_sync::MockGaiaAuthenticator
    mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME,
                   "some random url");

  // Add our sample mock user.
  mock_gaia_auth.AddMockUser("test", "passwd", "SomeAuthToken", "SomeLSID",
      "SomeSID", browser_sync::None);

  // Ask to save credentials.
  ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd", true));
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID");

  // Now make a call that uses saved credentials, and assert that we get the
  // same tokens back.
  ASSERT_TRUE(mock_gaia_auth.Authenticate());
  ASSERT_STREQ(mock_gaia_auth.email().c_str(), "test");
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID");

  // Now clear the saved credentials by toggling the flag while authenticating.
  ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd", false));

  // Test if saved credentials have been cleared.
  ASSERT_STREQ(mock_gaia_auth.email().c_str(), "");

  // Assert that current authentication session still succeeds (we only asked
  // not to save it for future requests.)
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID");

  // Now try to use saved credentials:
  ASSERT_STREQ(mock_gaia_auth.email().c_str(), "");
  ASSERT_FALSE(mock_gaia_auth.Authenticate());

  // And assert that any future requests that rely on saved credentials fail.
  ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "");
  ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "");
}

}  // namespace

Generated by  Doxygen 1.6.0   Back to index