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/proxy/chrome_keyring_proxy.h" |
| 17 #include "chrome/browser/password_manager/proxy/chrome_keyring_proxy_client.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "webkit/glue/password_form.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 class TestChromeKeyringProxyClient : public ChromeKeyringProxyClient { |
| 24 public: |
| 25 using ChromeKeyringProxyClient::ConnectForTesting; |
| 26 using ChromeKeyringProxyClient::CancelAllRequests; |
| 27 using ChromeKeyringProxyClient::HandleProxyReply; |
| 28 }; |
| 29 |
| 30 class ChromeKeyringProxyClientTest : 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 TestChromeKeyringProxyClient 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(TestChromeKeyringProxyClient* client) |
| 80 : client_(client) {} |
| 81 ~RequestCanceler() { client_->CancelAllRequests(); } |
| 82 private: |
| 83 TestChromeKeyringProxyClient* client_; |
| 84 }; |
| 85 |
| 86 } // anonymous namespace |
| 87 |
| 88 TEST_F(ChromeKeyringProxyClientTest, AddLogin) { |
| 89 ChromeKeyringProxyClient::RequestContext context; |
| 90 RequestCanceler canceler(&client_); |
| 91 client_.AddLogin(sample_form_, "chrome", &context); |
| 92 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n" |
| 93 "+%s\n+%s\n%d\n%d\n+%s\n%d\n%d\nchrome\n\n", |
| 94 ChromeKeyringProxyCommands::kAddLogin, |
| 95 sample_form_.origin.spec().c_str(), |
| 96 UTF16ToUTF8(sample_form_.password_value).c_str(), |
| 97 sample_form_.origin.spec().c_str(), |
| 98 sample_form_.action.spec().c_str(), |
| 99 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 100 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 101 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 102 UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| 103 sample_form_.signon_realm.c_str(), |
| 104 sample_form_.ssl_valid, |
| 105 sample_form_.preferred, |
| 106 base::Int64ToString( |
| 107 sample_form_.date_created.ToTimeT()).c_str(), |
| 108 sample_form_.blacklisted_by_user, |
| 109 sample_form_.scheme), |
| 110 GetRequests()); |
| 111 } |
| 112 |
| 113 TEST_F(ChromeKeyringProxyClientTest, AddLoginSearch) { |
| 114 ChromeKeyringProxyClient::RequestContext context; |
| 115 RequestCanceler canceler(&client_); |
| 116 client_.AddLoginSearch(sample_form_, "chrome", &context); |
| 117 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| 118 ChromeKeyringProxyCommands::kAddLoginSearch, |
| 119 sample_form_.origin.spec().c_str(), |
| 120 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 121 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 122 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 123 UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| 124 sample_form_.signon_realm.c_str()), |
| 125 GetRequests()); |
| 126 } |
| 127 |
| 128 TEST_F(ChromeKeyringProxyClientTest, UpdateLoginSearch) { |
| 129 ChromeKeyringProxyClient::RequestContext context; |
| 130 RequestCanceler canceler(&client_); |
| 131 client_.UpdateLoginSearch(sample_form_, "chrome", &context); |
| 132 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| 133 ChromeKeyringProxyCommands::kUpdateLoginSearch, |
| 134 sample_form_.origin.spec().c_str(), |
| 135 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 136 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 137 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 138 sample_form_.signon_realm.c_str()), |
| 139 GetRequests()); |
| 140 } |
| 141 |
| 142 TEST_F(ChromeKeyringProxyClientTest, RemoveLogin) { |
| 143 ChromeKeyringProxyClient::RequestContext context; |
| 144 RequestCanceler canceler(&client_); |
| 145 client_.RemoveLogin(sample_form_, "chrome", &context); |
| 146 EXPECT_EQ(StringPrintf("%c1\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\n+%s\nchrome\n\n", |
| 147 ChromeKeyringProxyCommands::kRemoveLogin, |
| 148 sample_form_.origin.spec().c_str(), |
| 149 sample_form_.action.spec().c_str(), |
| 150 UTF16ToUTF8(sample_form_.username_element).c_str(), |
| 151 UTF16ToUTF8(sample_form_.username_value).c_str(), |
| 152 UTF16ToUTF8(sample_form_.password_element).c_str(), |
| 153 UTF16ToUTF8(sample_form_.submit_element).c_str(), |
| 154 sample_form_.signon_realm.c_str()), |
| 155 GetRequests()); |
| 156 } |
| 157 |
| 158 TEST_F(ChromeKeyringProxyClientTest, GetLogins) { |
| 159 ChromeKeyringProxyClient::RequestContext context; |
| 160 RequestCanceler canceler(&client_); |
| 161 client_.GetLogins(sample_form_, "chrome", &context); |
| 162 EXPECT_EQ(StringPrintf("%c1\n+%s\nchrome\n\n", |
| 163 ChromeKeyringProxyCommands::kGetLogins, |
| 164 sample_form_.signon_realm.c_str()), |
| 165 GetRequests()); |
| 166 } |
| 167 |
| 168 TEST_F(ChromeKeyringProxyClientTest, GetLoginsList) { |
| 169 // This test also lightly tests the request ID mechanism. |
| 170 ChromeKeyringProxyClient::RequestContext context[2]; |
| 171 RequestCanceler canceler(&client_); |
| 172 client_.GetLoginsList(false, "chrome", &context[0]); |
| 173 client_.GetLoginsList(true, "chrome", &context[1]); |
| 174 EXPECT_EQ(StringPrintf("%c1\n0\nchrome\n\n%c2\n1\nchrome\n\n", |
| 175 ChromeKeyringProxyCommands::kGetLoginsList, |
| 176 ChromeKeyringProxyCommands::kGetLoginsList), |
| 177 GetRequests()); |
| 178 } |
| 179 |
| 180 TEST_F(ChromeKeyringProxyClientTest, GetAllLogins) { |
| 181 ChromeKeyringProxyClient::RequestContext context; |
| 182 RequestCanceler canceler(&client_); |
| 183 client_.GetAllLogins("chrome", &context); |
| 184 EXPECT_EQ(StringPrintf("%c1\nchrome\n\n", |
| 185 ChromeKeyringProxyCommands::kGetAllLogins), |
| 186 GetRequests()); |
| 187 } |
| 188 |
| 189 TEST_F(ChromeKeyringProxyClientTest, GetAllLoginsEmptyReply) { |
| 190 std::vector<webkit_glue::PasswordForm*> forms; |
| 191 ChromeKeyringProxyClient::RequestContext context(&forms); |
| 192 RequestCanceler canceler(&client_); |
| 193 client_.GetAllLogins("chrome", &context); |
| 194 std::vector<std::string> reply; |
| 195 reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| 196 client_.HandleProxyReply(reply); |
| 197 EXPECT_TRUE(context.event.IsSignaled()); |
| 198 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context.result_code); |
| 199 EXPECT_EQ(0u, forms.size()); |
| 200 } |
| 201 |
| 202 TEST_F(ChromeKeyringProxyClientTest, GetAllLoginsOneReply) { |
| 203 std::vector<webkit_glue::PasswordForm*> forms; |
| 204 ChromeKeyringProxyClient::RequestContext context(&forms); |
| 205 RequestCanceler canceler(&client_); |
| 206 client_.GetAllLogins("chrome", &context); |
| 207 std::vector<std::string> reply; |
| 208 reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_OK)); |
| 209 reply.push_back("+" + sample_form_.origin.spec()); |
| 210 reply.push_back("+" + sample_form_.action.spec()); |
| 211 reply.push_back("+" + UTF16ToUTF8(sample_form_.username_element)); |
| 212 reply.push_back("+" + UTF16ToUTF8(sample_form_.username_value)); |
| 213 reply.push_back("+" + UTF16ToUTF8(sample_form_.password_element)); |
| 214 reply.push_back("+" + UTF16ToUTF8(sample_form_.password_value)); |
| 215 reply.push_back("+" + UTF16ToUTF8(sample_form_.submit_element)); |
| 216 reply.push_back("+" + sample_form_.signon_realm); |
| 217 reply.push_back(sample_form_.ssl_valid ? "1" : "0"); |
| 218 reply.push_back(sample_form_.preferred ? "1" : "0"); |
| 219 reply.push_back("+" + base::Int64ToString( |
| 220 sample_form_.date_created.ToTimeT())); |
| 221 reply.push_back(sample_form_.blacklisted_by_user ? "1" : "0"); |
| 222 reply.push_back(StringPrintf("%d", sample_form_.scheme)); |
| 223 client_.HandleProxyReply(reply); |
| 224 EXPECT_TRUE(context.event.IsSignaled()); |
| 225 EXPECT_EQ(GNOME_KEYRING_RESULT_OK, context.result_code); |
| 226 ASSERT_EQ(1u, forms.size()); |
| 227 // TODO(mdm): verify the form is actually correct, i.e. == sample_form_ |
| 228 STLDeleteElements(&forms); |
| 229 } |
| 230 |
| 231 TEST_F(ChromeKeyringProxyClientTest, GetAllLoginsInOrderReplies) { |
| 232 std::vector<webkit_glue::PasswordForm*> forms; |
| 233 ChromeKeyringProxyClient::RequestContext context1(&forms); |
| 234 ChromeKeyringProxyClient::RequestContext context2(&forms); |
| 235 RequestCanceler canceler(&client_); |
| 236 client_.GetAllLogins("chrome", &context1); |
| 237 client_.GetAllLogins("chrome", &context2); |
| 238 std::vector<std::string> reply; |
| 239 reply.push_back(StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| 240 client_.HandleProxyReply(reply); |
| 241 EXPECT_TRUE(context1.event.IsSignaled()); |
| 242 EXPECT_FALSE(context2.event.IsSignaled()); |
| 243 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context1.result_code); |
| 244 EXPECT_EQ(0u, forms.size()); |
| 245 reply[0] = StringPrintf("2 %d", GNOME_KEYRING_RESULT_NO_MATCH); |
| 246 client_.HandleProxyReply(reply); |
| 247 EXPECT_TRUE(context2.event.IsSignaled()); |
| 248 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context2.result_code); |
| 249 EXPECT_EQ(0u, forms.size()); |
| 250 } |
| 251 |
| 252 TEST_F(ChromeKeyringProxyClientTest, GetAllLoginsOutOfOrderReplies) { |
| 253 std::vector<webkit_glue::PasswordForm*> forms; |
| 254 ChromeKeyringProxyClient::RequestContext context1(&forms); |
| 255 ChromeKeyringProxyClient::RequestContext context2(&forms); |
| 256 RequestCanceler canceler(&client_); |
| 257 client_.GetAllLogins("chrome", &context1); |
| 258 client_.GetAllLogins("chrome", &context2); |
| 259 std::vector<std::string> reply; |
| 260 reply.push_back(StringPrintf("2 %d", GNOME_KEYRING_RESULT_NO_MATCH)); |
| 261 client_.HandleProxyReply(reply); |
| 262 EXPECT_FALSE(context1.event.IsSignaled()); |
| 263 EXPECT_TRUE(context2.event.IsSignaled()); |
| 264 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context2.result_code); |
| 265 EXPECT_EQ(0u, forms.size()); |
| 266 reply[0] = StringPrintf("1 %d", GNOME_KEYRING_RESULT_NO_MATCH); |
| 267 client_.HandleProxyReply(reply); |
| 268 EXPECT_TRUE(context1.event.IsSignaled()); |
| 269 EXPECT_EQ(GNOME_KEYRING_RESULT_NO_MATCH, context1.result_code); |
| 270 EXPECT_EQ(0u, forms.size()); |
| 271 } |
OLD | NEW |