OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 <gnome-keyring.h> |
| 6 #include <unistd.h> |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/file_util.h" |
| 12 #include "base/stl_util.h" |
| 13 #include "base/string_number_conversions.h" |
| 14 #include "base/stringprintf.h" |
| 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/password_manager/keyring_proxy/keyring_proxy.h" |
| 17 #include "chrome/browser/password_manager/keyring_proxy/keyring_proxy_client.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "webkit/glue/password_form.h" |
| 20 |
| 21 namespace keyring_proxy { |
| 22 |
| 23 class TestKeyringProxyClient : public KeyringProxyClient { |
| 24 public: |
| 25 using KeyringProxyClient::ConnectForTesting; |
| 26 using KeyringProxyClient::CancelAllRequests; |
| 27 using KeyringProxyClient::HandleProxyReply; |
| 28 }; |
| 29 |
| 30 class KeyringProxyClientTest : public testing::Test { |
| 31 protected: |
| 32 virtual void SetUp() { |
| 33 FILE* file = file_util::CreateAndOpenTemporaryFile(&path_); |
| 34 ASSERT_FALSE(file == NULL); |
| 35 int fd = dup(fileno(file)); |
| 36 EXPECT_GE(fd, 0); |
| 37 fclose(file); |
| 38 // Now that we've got a file descriptor to an empty file, "connect" to it. |
| 39 client_.ConnectForTesting(fd, false); |
| 40 |
| 41 // Set up a sample form. |
| 42 sample_form_.origin = GURL("http://www.google.com/"); |
| 43 sample_form_.action = GURL("http://www.google.com/login"); |
| 44 sample_form_.username_element = UTF8ToUTF16("user"); |
| 45 sample_form_.username_value = UTF8ToUTF16("joeschmoe"); |
| 46 sample_form_.password_element = UTF8ToUTF16("pass"); |
| 47 sample_form_.password_value = UTF8ToUTF16("seekrit"); |
| 48 sample_form_.submit_element = UTF8ToUTF16("submit"); |
| 49 sample_form_.signon_realm = "Google"; |
| 50 sample_form_.ssl_valid = true; |
| 51 sample_form_.preferred = true; |
| 52 sample_form_.date_created = base::Time::FromTimeT(12345); |
| 53 sample_form_.blacklisted_by_user = false; |
| 54 sample_form_.scheme = webkit_glue::PasswordForm::SCHEME_HTML; |
| 55 } |
| 56 |
| 57 virtual void TearDown() { |
| 58 EXPECT_TRUE(file_util::Delete(path_, false)); |
| 59 } |
| 60 |
| 61 std::string GetRequests() { |
| 62 std::string requests; |
| 63 EXPECT_TRUE(file_util::ReadFileToString(path_, &requests)); |
| 64 return requests; |
| 65 } |
| 66 |
| 67 webkit_glue::PasswordForm sample_form_; |
| 68 |
| 69 FilePath path_; |
| 70 TestKeyringProxyClient client_; |
| 71 }; |
| 72 |
| 73 // The tests all declare RequestContexts on the stack, so we need to cancel them |
| 74 // before each test's body returns. Since gtest uses exceptions for some kinds |
| 75 // of failures, we need to use an object's destructor to make sure that happens. |
| 76 // IMPORTANT NOTE: Define the RequestCanceler after all RequestContexts! |
| 77 class RequestCanceler { |
| 78 public: |
| 79 explicit RequestCanceler(TestKeyringProxyClient* client) |
| 80 : client_(client) {} |
| 81 ~RequestCanceler() { client_->CancelAllRequests(); } |
| 82 private: |
| 83 TestKeyringProxyClient* client_; |
| 84 }; |
| 85 |
| 86 TEST_F(KeyringProxyClientTest, AddLogin) { |
| 87 KeyringProxyClient::RequestContext context; |
| 88 RequestCanceler canceler(&client_); |
| 89 client_.AddLogin(sample_form_, "chrome", &context); |
| 90 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n" |
| 91 "+%s\n+%s\n%d\n%d\n+%s\n%d\n%d\nchrome\n\n", |
| 92 KeyringProxy::kAddLoginCommand, |
| 93 sample_form_.origin.spec().c_str(), |
| 94 UTF16ToUTF8(sample_form_.password_value).c_str(), |
| 95 sample_form_.origin.spec().c_str(), |
| 96 sample_form_.action.spec().c_str(), |
| 97 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 98 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 99 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 100 UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| 101 sample_form_.signon_realm.c_str(), |
| 102 sample_form_.ssl_valid, |
| 103 sample_form_.preferred, |
| 104 base::Int64ToString( |
| 105 sample_form_.date_created.ToTimeT()).c_str(), |
| 106 sample_form_.blacklisted_by_user, |
| 107 sample_form_.scheme), |
| 108 GetRequests()); |
| 109 } |
| 110 |
| 111 TEST_F(KeyringProxyClientTest, AddLoginSearch) { |
| 112 KeyringProxyClient::RequestContext context; |
| 113 RequestCanceler canceler(&client_); |
| 114 client_.AddLoginSearch(sample_form_, "chrome", &context); |
| 115 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| 116 KeyringProxy::kAddLoginSearchCommand, |
| 117 sample_form_.origin.spec().c_str(), |
| 118 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 119 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 120 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 121 UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| 122 sample_form_.signon_realm.c_str()), |
| 123 GetRequests()); |
| 124 } |
| 125 |
| 126 TEST_F(KeyringProxyClientTest, UpdateLoginSearch) { |
| 127 KeyringProxyClient::RequestContext context; |
| 128 RequestCanceler canceler(&client_); |
| 129 client_.UpdateLoginSearch(sample_form_, "chrome", &context); |
| 130 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| 131 KeyringProxy::kUpdateLoginSearchCommand, |
| 132 sample_form_.origin.spec().c_str(), |
| 133 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 134 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 135 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 136 sample_form_.signon_realm.c_str()), |
| 137 GetRequests()); |
| 138 } |
| 139 |
| 140 TEST_F(KeyringProxyClientTest, RemoveLogin) { |
| 141 KeyringProxyClient::RequestContext context; |
| 142 RequestCanceler canceler(&client_); |
| 143 client_.RemoveLogin(sample_form_, "chrome", &context); |
| 144 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| 145 KeyringProxy::kRemoveLoginCommand, |
| 146 sample_form_.origin.spec().c_str(), |
| 147 sample_form_.action.spec().c_str(), |
| 148 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 149 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 150 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 151 UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| 152 sample_form_.signon_realm.c_str()), |
| 153 GetRequests()); |
| 154 } |
| 155 |
| 156 TEST_F(KeyringProxyClientTest, GetLogins) { |
| 157 KeyringProxyClient::RequestContext context; |
| 158 RequestCanceler canceler(&client_); |
| 159 client_.GetLogins(sample_form_, "chrome", &context); |
| 160 EXPECT_EQ(StringPrintf("%c1\n+%s\nchrome\n\n", |
| 161 KeyringProxy::kGetLoginsCommand, |
| 162 sample_form_.signon_realm.c_str()), |
| 163 GetRequests()); |
| 164 } |
| 165 |
| 166 TEST_F(KeyringProxyClientTest, GetLoginsList) { |
| 167 // This test also lightly tests the request ID mechanism. |
| 168 KeyringProxyClient::RequestContext context[2]; |
| 169 RequestCanceler canceler(&client_); |
| 170 client_.GetLoginsList(false, "chrome", &context[0]); |
| 171 client_.GetLoginsList(true, "chrome", &context[1]); |
| 172 EXPECT_EQ(StringPrintf("%c1\n0\nchrome\n\n%c2\n1\nchrome\n\n", |
| 173 KeyringProxy::kGetLoginsListCommand, |
| 174 KeyringProxy::kGetLoginsListCommand), |
| 175 GetRequests()); |
| 176 } |
| 177 |
| 178 TEST_F(KeyringProxyClientTest, GetAllLogins) { |
| 179 KeyringProxyClient::RequestContext context; |
| 180 RequestCanceler canceler(&client_); |
| 181 client_.GetAllLogins("chrome", &context); |
| 182 EXPECT_EQ(StringPrintf("%c1\nchrome\n\n", KeyringProxy::kGetAllLoginsCommand), |
| 183 GetRequests()); |
| 184 } |
| 185 |
| 186 TEST_F(KeyringProxyClientTest, GetAllLoginsEmptyReply) { |
| 187 std::vector<webkit_glue::PasswordForm*> forms; |
| 188 KeyringProxyClient::RequestContext context(&forms); |
| 189 RequestCanceler canceler(&client_); |
| 190 client_.GetAllLogins("chrome", &context); |
| 191 std::vector<std::string> reply; |
| 192 reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| 193 client_.HandleProxyReply(reply); |
| 194 EXPECT_TRUE(context.event.IsSignaled()); |
| 195 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context.result_code); |
| 196 EXPECT_EQ(0u, forms.size()); |
| 197 } |
| 198 |
| 199 TEST_F(KeyringProxyClientTest, GetAllLoginsOneReply) { |
| 200 std::vector<webkit_glue::PasswordForm*> forms; |
| 201 KeyringProxyClient::RequestContext context(&forms); |
| 202 RequestCanceler canceler(&client_); |
| 203 client_.GetAllLogins("chrome", &context); |
| 204 std::vector<std::string> reply; |
| 205 reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_OK)); |
| 206 reply.push_back("+" + sample_form_.origin.spec()); |
| 207 reply.push_back("+" + sample_form_.action.spec()); |
| 208 reply.push_back("+" + UTF16ToUTF8(sample_form_.username_element)); |
| 209 reply.push_back("+" + UTF16ToUTF8(sample_form_.username_value)); |
| 210 reply.push_back("+" + UTF16ToUTF8(sample_form_.password_element)); |
| 211 reply.push_back("+" + UTF16ToUTF8(sample_form_.password_value)); |
| 212 reply.push_back("+" + UTF16ToUTF8(sample_form_.submit_element)); |
| 213 reply.push_back("+" + sample_form_.signon_realm); |
| 214 reply.push_back(sample_form_.ssl_valid ? "1" : "0"); |
| 215 reply.push_back(sample_form_.preferred ? "1" : "0"); |
| 216 reply.push_back("+" + base::Int64ToString( |
| 217 sample_form_.date_created.ToTimeT())); |
| 218 reply.push_back(sample_form_.blacklisted_by_user ? "1" : "0"); |
| 219 reply.push_back(StringPrintf("%d", sample_form_.scheme)); |
| 220 client_.HandleProxyReply(reply); |
| 221 EXPECT_TRUE(context.event.IsSignaled()); |
| 222 EXPECT_EQ(GNOME_KEYRING_RESULT_OK, context.result_code); |
| 223 ASSERT_EQ(1u, forms.size()); |
| 224 // TODO(mdm): verify the form is actually correct, i.e. == sample_form_ |
| 225 STLDeleteElements(&forms); |
| 226 } |
| 227 |
| 228 TEST_F(KeyringProxyClientTest, GetAllLoginsInOrderReplies) { |
| 229 std::vector<webkit_glue::PasswordForm*> forms; |
| 230 KeyringProxyClient::RequestContext context1(&forms); |
| 231 KeyringProxyClient::RequestContext context2(&forms); |
| 232 RequestCanceler canceler(&client_); |
| 233 client_.GetAllLogins("chrome", &context1); |
| 234 client_.GetAllLogins("chrome", &context2); |
| 235 std::vector<std::string> reply; |
| 236 reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| 237 client_.HandleProxyReply(reply); |
| 238 EXPECT_TRUE(context1.event.IsSignaled()); |
| 239 EXPECT_FALSE(context2.event.IsSignaled()); |
| 240 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context1.result_code); |
| 241 EXPECT_EQ(0u, forms.size()); |
| 242 reply[0] = StringPrintf("2 %d", GNOME_KEYRING_RESULT_NO_MATCH); |
| 243 client_.HandleProxyReply(reply); |
| 244 EXPECT_TRUE(context2.event.IsSignaled()); |
| 245 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context2.result_code); |
| 246 EXPECT_EQ(0u, forms.size()); |
| 247 } |
| 248 |
| 249 TEST_F(KeyringProxyClientTest, GetAllLoginsOutOfOrderReplies) { |
| 250 std::vector<webkit_glue::PasswordForm*> forms; |
| 251 KeyringProxyClient::RequestContext context1(&forms); |
| 252 KeyringProxyClient::RequestContext context2(&forms); |
| 253 RequestCanceler canceler(&client_); |
| 254 client_.GetAllLogins("chrome", &context1); |
| 255 client_.GetAllLogins("chrome", &context2); |
| 256 std::vector<std::string> reply; |
| 257 reply.push_back(StringPrintf("2 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| 258 client_.HandleProxyReply(reply); |
| 259 EXPECT_FALSE(context1.event.IsSignaled()); |
| 260 EXPECT_TRUE(context2.event.IsSignaled()); |
| 261 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context2.result_code); |
| 262 EXPECT_EQ(0u, forms.size()); |
| 263 reply[0] = StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH); |
| 264 client_.HandleProxyReply(reply); |
| 265 EXPECT_TRUE(context1.event.IsSignaled()); |
| 266 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context1.result_code); |
| 267 EXPECT_EQ(0u, forms.size()); |
| 268 } |
| 269 |
| 270 } // namespace keyring_proxy |
OLD | NEW |