Chromium Code Reviews| Index: chrome/browser/password_manager/keyring_proxy/keyring_proxy_client_unittest.cc |
| =================================================================== |
| --- chrome/browser/password_manager/keyring_proxy/keyring_proxy_client_unittest.cc (revision 0) |
| +++ chrome/browser/password_manager/keyring_proxy/keyring_proxy_client_unittest.cc (revision 0) |
| @@ -0,0 +1,270 @@ |
| +// Copyright (c) 2011 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 <gnome-keyring.h> |
| +#include <unistd.h> |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/file_util.h" |
| +#include "base/stl_util.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/stringprintf.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/password_manager/keyring_proxy/keyring_proxy.h" |
| +#include "chrome/browser/password_manager/keyring_proxy/keyring_proxy_client.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/glue/password_form.h" |
| + |
| +namespace keyring_proxy { |
| + |
| +class TestKeyringProxyClient : public KeyringProxyClient { |
| + public: |
| + using KeyringProxyClient::ConnectForTesting; |
| + using KeyringProxyClient::CancelAllRequests; |
| + using KeyringProxyClient::HandleProxyReply; |
| +}; |
| + |
| +class KeyringProxyClientTest : public testing::Test { |
| + protected: |
| + virtual void SetUp() { |
| + FILE* file = file_util::CreateAndOpenTemporaryFile(&path_); |
|
vandebo (ex-Chrome)
2011/12/01 19:57:30
Would it be better to put this in a scoped temp di
|
| + ASSERT_FALSE(file == NULL); |
| + int fd = dup(fileno(file)); |
| + EXPECT_GE(fd, 0); |
| + fclose(file); |
| + // Now that we've got a file descriptor to an empty file, "connect" to it. |
| + client_.ConnectForTesting(fd, false); |
| + |
| + // Set up a sample form. |
| + sample_form_.origin = GURL("http://www.google.com/"); |
| + sample_form_.action = GURL("http://www.google.com/login"); |
| + sample_form_.username_element = UTF8ToUTF16("user"); |
| + sample_form_.username_value = UTF8ToUTF16("joeschmoe"); |
| + sample_form_.password_element = UTF8ToUTF16("pass"); |
| + sample_form_.password_value = UTF8ToUTF16("seekrit"); |
| + sample_form_.submit_element = UTF8ToUTF16("submit"); |
| + sample_form_.signon_realm = "Google"; |
| + sample_form_.ssl_valid = true; |
| + sample_form_.preferred = true; |
| + sample_form_.date_created = base::Time::FromTimeT(12345); |
| + sample_form_.blacklisted_by_user = false; |
| + sample_form_.scheme = webkit_glue::PasswordForm::SCHEME_HTML; |
| + } |
| + |
| + virtual void TearDown() { |
| + EXPECT_TRUE(file_util::Delete(path_, false)); |
| + } |
| + |
| + std::string GetRequests() { |
| + std::string requests; |
| + EXPECT_TRUE(file_util::ReadFileToString(path_, &requests)); |
| + return requests; |
| + } |
| + |
| + webkit_glue::PasswordForm sample_form_; |
| + |
| + FilePath path_; |
| + TestKeyringProxyClient client_; |
| +}; |
| + |
| +// The tests all declare RequestContexts on the stack, so we need to cancel them |
| +// before each test's body returns. Since gtest uses exceptions for some kinds |
| +// of failures, we need to use an object's destructor to make sure that happens. |
| +// IMPORTANT NOTE: Define the RequestCanceler after all RequestContexts! |
| +class RequestCanceler { |
| + public: |
| + explicit RequestCanceler(TestKeyringProxyClient* client) |
| + : client_(client) {} |
| + ~RequestCanceler() { client_->CancelAllRequests(); } |
| + private: |
| + TestKeyringProxyClient* client_; |
| +}; |
| + |
| +TEST_F(KeyringProxyClientTest, AddLogin) { |
| + KeyringProxyClient::RequestContext context; |
| + RequestCanceler canceler(&client_); |
| + client_.AddLogin(sample_form_, "chrome", &context); |
| + EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n" |
| + "+%s\n+%s\n%d\n%d\n+%s\n%d\n%d\nchrome\n\n", |
| + KeyringProxy::kAddLoginCommand, |
| + sample_form_.origin.spec().c_str(), |
| + UTF16ToUTF8(sample_form_.password_value).c_str(), |
| + sample_form_.origin.spec().c_str(), |
| + sample_form_.action.spec().c_str(), |
| + UTF16ToUTF8(sample_form_.username_element).c_str(), |
| + UTF16ToUTF8(sample_form_.username_value).c_str(), |
| + UTF16ToUTF8(sample_form_.password_element).c_str(), |
| + UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| + sample_form_.signon_realm.c_str(), |
| + sample_form_.ssl_valid, |
| + sample_form_.preferred, |
| + base::Int64ToString( |
| + sample_form_.date_created.ToTimeT()).c_str(), |
| + sample_form_.blacklisted_by_user, |
| + sample_form_.scheme), |
| + GetRequests()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, AddLoginSearch) { |
| + KeyringProxyClient::RequestContext context; |
| + RequestCanceler canceler(&client_); |
| + client_.AddLoginSearch(sample_form_, "chrome", &context); |
| + EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| + KeyringProxy::kAddLoginSearchCommand, |
| + sample_form_.origin.spec().c_str(), |
| + UTF16ToUTF8(sample_form_.username_element).c_str(), |
| + UTF16ToUTF8(sample_form_.username_value).c_str(), |
| + UTF16ToUTF8(sample_form_.password_element).c_str(), |
| + UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| + sample_form_.signon_realm.c_str()), |
| + GetRequests()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, UpdateLoginSearch) { |
| + KeyringProxyClient::RequestContext context; |
| + RequestCanceler canceler(&client_); |
| + client_.UpdateLoginSearch(sample_form_, "chrome", &context); |
| + EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| + KeyringProxy::kUpdateLoginSearchCommand, |
| + sample_form_.origin.spec().c_str(), |
| + UTF16ToUTF8(sample_form_.username_element).c_str(), |
| + UTF16ToUTF8(sample_form_.username_value).c_str(), |
| + UTF16ToUTF8(sample_form_.password_element).c_str(), |
| + sample_form_.signon_realm.c_str()), |
| + GetRequests()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, RemoveLogin) { |
| + KeyringProxyClient::RequestContext context; |
| + RequestCanceler canceler(&client_); |
| + client_.RemoveLogin(sample_form_, "chrome", &context); |
| + EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| + KeyringProxy::kRemoveLoginCommand, |
| + sample_form_.origin.spec().c_str(), |
| + sample_form_.action.spec().c_str(), |
| + UTF16ToUTF8(sample_form_.username_element).c_str(), |
| + UTF16ToUTF8(sample_form_.username_value).c_str(), |
| + UTF16ToUTF8(sample_form_.password_element).c_str(), |
| + UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| + sample_form_.signon_realm.c_str()), |
| + GetRequests()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, GetLogins) { |
| + KeyringProxyClient::RequestContext context; |
| + RequestCanceler canceler(&client_); |
| + client_.GetLogins(sample_form_, "chrome", &context); |
| + EXPECT_EQ(StringPrintf("%c1\n+%s\nchrome\n\n", |
| + KeyringProxy::kGetLoginsCommand, |
| + sample_form_.signon_realm.c_str()), |
| + GetRequests()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, GetLoginsList) { |
| + // This test also lightly tests the request ID mechanism. |
| + KeyringProxyClient::RequestContext context[2]; |
| + RequestCanceler canceler(&client_); |
| + client_.GetLoginsList(false, "chrome", &context[0]); |
| + client_.GetLoginsList(true, "chrome", &context[1]); |
| + EXPECT_EQ(StringPrintf("%c1\n0\nchrome\n\n%c2\n1\nchrome\n\n", |
| + KeyringProxy::kGetLoginsListCommand, |
| + KeyringProxy::kGetLoginsListCommand), |
| + GetRequests()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, GetAllLogins) { |
| + KeyringProxyClient::RequestContext context; |
| + RequestCanceler canceler(&client_); |
| + client_.GetAllLogins("chrome", &context); |
| + EXPECT_EQ(StringPrintf("%c1\nchrome\n\n", KeyringProxy::kGetAllLoginsCommand), |
| + GetRequests()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, GetAllLoginsEmptyReply) { |
| + std::vector<webkit_glue::PasswordForm*> forms; |
| + KeyringProxyClient::RequestContext context(&forms); |
| + RequestCanceler canceler(&client_); |
| + client_.GetAllLogins("chrome", &context); |
| + std::vector<std::string> reply; |
| + reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| + client_.HandleProxyReply(reply); |
| + EXPECT_TRUE(context.event.IsSignaled()); |
| + EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context.result_code); |
| + EXPECT_EQ(0u, forms.size()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, GetAllLoginsOneReply) { |
| + std::vector<webkit_glue::PasswordForm*> forms; |
| + KeyringProxyClient::RequestContext context(&forms); |
| + RequestCanceler canceler(&client_); |
| + client_.GetAllLogins("chrome", &context); |
| + std::vector<std::string> reply; |
| + reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_OK)); |
| + reply.push_back("+" + sample_form_.origin.spec()); |
| + reply.push_back("+" + sample_form_.action.spec()); |
| + reply.push_back("+" + UTF16ToUTF8(sample_form_.username_element)); |
| + reply.push_back("+" + UTF16ToUTF8(sample_form_.username_value)); |
| + reply.push_back("+" + UTF16ToUTF8(sample_form_.password_element)); |
| + reply.push_back("+" + UTF16ToUTF8(sample_form_.password_value)); |
| + reply.push_back("+" + UTF16ToUTF8(sample_form_.submit_element)); |
| + reply.push_back("+" + sample_form_.signon_realm); |
| + reply.push_back(sample_form_.ssl_valid ? "1" : "0"); |
| + reply.push_back(sample_form_.preferred ? "1" : "0"); |
| + reply.push_back("+" + base::Int64ToString( |
| + sample_form_.date_created.ToTimeT())); |
| + reply.push_back(sample_form_.blacklisted_by_user ? "1" : "0"); |
| + reply.push_back(StringPrintf("%d", sample_form_.scheme)); |
| + client_.HandleProxyReply(reply); |
| + EXPECT_TRUE(context.event.IsSignaled()); |
| + EXPECT_EQ(GNOME_KEYRING_RESULT_OK, context.result_code); |
| + ASSERT_EQ(1u, forms.size()); |
| + // TODO(mdm): verify the form is actually correct, i.e. == sample_form_ |
| + STLDeleteElements(&forms); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, GetAllLoginsInOrderReplies) { |
| + std::vector<webkit_glue::PasswordForm*> forms; |
| + KeyringProxyClient::RequestContext context1(&forms); |
| + KeyringProxyClient::RequestContext context2(&forms); |
| + RequestCanceler canceler(&client_); |
| + client_.GetAllLogins("chrome", &context1); |
| + client_.GetAllLogins("chrome", &context2); |
| + std::vector<std::string> reply; |
| + reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| + client_.HandleProxyReply(reply); |
| + EXPECT_TRUE(context1.event.IsSignaled()); |
| + EXPECT_FALSE(context2.event.IsSignaled()); |
| + EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context1.result_code); |
| + EXPECT_EQ(0u, forms.size()); |
| + reply[0] = StringPrintf("2 %d", GNOME_KEYRING_RESULT_NO_MATCH); |
| + client_.HandleProxyReply(reply); |
| + EXPECT_TRUE(context2.event.IsSignaled()); |
| + EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context2.result_code); |
| + EXPECT_EQ(0u, forms.size()); |
| +} |
| + |
| +TEST_F(KeyringProxyClientTest, GetAllLoginsOutOfOrderReplies) { |
| + std::vector<webkit_glue::PasswordForm*> forms; |
| + KeyringProxyClient::RequestContext context1(&forms); |
| + KeyringProxyClient::RequestContext context2(&forms); |
| + RequestCanceler canceler(&client_); |
| + client_.GetAllLogins("chrome", &context1); |
| + client_.GetAllLogins("chrome", &context2); |
| + std::vector<std::string> reply; |
| + reply.push_back(StringPrintf("2 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| + client_.HandleProxyReply(reply); |
| + EXPECT_FALSE(context1.event.IsSignaled()); |
| + EXPECT_TRUE(context2.event.IsSignaled()); |
| + EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context2.result_code); |
| + EXPECT_EQ(0u, forms.size()); |
| + reply[0] = StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH); |
| + client_.HandleProxyReply(reply); |
| + EXPECT_TRUE(context1.event.IsSignaled()); |
| + EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context1.result_code); |
| + EXPECT_EQ(0u, forms.size()); |
| +} |
| + |
| +} // namespace keyring_proxy |
| Property changes on: chrome/browser/password_manager/keyring_proxy/keyring_proxy_client_unittest.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |