| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <windows.h> | |
| 6 | |
| 7 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 9 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/time.h" |
| 10 #include "base/values.h" | 9 #include "base/values.h" |
| 11 #include "chrome/browser/template_url.h" | 10 #include "chrome/browser/template_url.h" |
| 12 #include "chrome/browser/webdata/web_database.h" | 11 #include "chrome/browser/webdata/web_database.h" |
| 13 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/stl_util-inl.h" | 13 #include "chrome/common/stl_util-inl.h" |
| 15 #include "skia/include/SkBitmap.h" | 14 #include "skia/include/SkBitmap.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webkit/glue/autofill_form.h" | 16 #include "webkit/glue/autofill_form.h" |
| 18 #include "webkit/glue/password_form.h" | 17 #include "webkit/glue/password_form.h" |
| 19 | 18 |
| 20 using base::Time; | 19 using base::Time; |
| 21 using base::TimeDelta; | 20 using base::TimeDelta; |
| 22 | 21 |
| 23 class WebDatabaseTest : public testing::Test { | 22 class WebDatabaseTest : public testing::Test { |
| 24 protected: | 23 protected: |
| 25 | 24 |
| 26 virtual void SetUp() { | 25 virtual void SetUp() { |
| 27 wchar_t b[32]; | |
| 28 _itow_s(static_cast<int>(GetTickCount()), b, arraysize(b), 10); | |
| 29 | |
| 30 PathService::Get(chrome::DIR_TEST_DATA, &file_); | 26 PathService::Get(chrome::DIR_TEST_DATA, &file_); |
| 31 file_ += FilePath::kSeparators[0]; | 27 file_ += FilePath::kSeparators[0]; |
| 32 file_ += L"TestWebDatabase"; | 28 file_ += L"TestWebDatabase"; |
| 33 file_ += b; | 29 file_ += Int64ToWString(base::Time::Now().ToInternalValue()); |
| 34 file_ += L".db"; | 30 file_ += L".db"; |
| 35 DeleteFile(file_.c_str()); | 31 file_util::Delete(file_, false); |
| 36 } | 32 } |
| 37 | 33 |
| 38 virtual void TearDown() { | 34 virtual void TearDown() { |
| 39 DeleteFile(file_.c_str()); | 35 file_util::Delete(file_, false); |
| 40 } | 36 } |
| 41 | 37 |
| 42 static int GetKeyCount(const DictionaryValue& d) { | 38 static int GetKeyCount(const DictionaryValue& d) { |
| 43 DictionaryValue::key_iterator i(d.begin_keys()); | 39 DictionaryValue::key_iterator i(d.begin_keys()); |
| 44 DictionaryValue::key_iterator e(d.end_keys()); | 40 DictionaryValue::key_iterator e(d.end_keys()); |
| 45 | 41 |
| 46 int r = 0; | 42 int r = 0; |
| 47 while (i != e) { | 43 while (i != e) { |
| 48 ++i; | 44 ++i; |
| 49 ++r; | 45 ++r; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 template_url.set_usage_count(32); | 100 template_url.set_usage_count(32); |
| 105 template_url.add_input_encoding("UTF-8"); | 101 template_url.add_input_encoding("UTF-8"); |
| 106 set_prepopulate_id(&template_url, 10); | 102 set_prepopulate_id(&template_url, 10); |
| 107 SetID(1, &template_url); | 103 SetID(1, &template_url); |
| 108 | 104 |
| 109 EXPECT_TRUE(db.AddKeyword(template_url)); | 105 EXPECT_TRUE(db.AddKeyword(template_url)); |
| 110 | 106 |
| 111 std::vector<TemplateURL*> template_urls; | 107 std::vector<TemplateURL*> template_urls; |
| 112 EXPECT_TRUE(db.GetKeywords(&template_urls)); | 108 EXPECT_TRUE(db.GetKeywords(&template_urls)); |
| 113 | 109 |
| 114 EXPECT_EQ(1, template_urls.size()); | 110 EXPECT_EQ(1U, template_urls.size()); |
| 115 const TemplateURL* restored_url = template_urls.front(); | 111 const TemplateURL* restored_url = template_urls.front(); |
| 116 | 112 |
| 117 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); | 113 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); |
| 118 | 114 |
| 119 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); | 115 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); |
| 120 | 116 |
| 121 EXPECT_FALSE(restored_url->autogenerate_keyword()); | 117 EXPECT_FALSE(restored_url->autogenerate_keyword()); |
| 122 | 118 |
| 123 EXPECT_TRUE(favicon_url == restored_url->GetFavIconURL()); | 119 EXPECT_TRUE(favicon_url == restored_url->GetFavIconURL()); |
| 124 | 120 |
| 125 EXPECT_TRUE(restored_url->safe_for_autoreplace()); | 121 EXPECT_TRUE(restored_url->safe_for_autoreplace()); |
| 126 | 122 |
| 127 EXPECT_TRUE(restored_url->show_in_default_list()); | 123 EXPECT_TRUE(restored_url->show_in_default_list()); |
| 128 | 124 |
| 129 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); | 125 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); |
| 130 | 126 |
| 131 EXPECT_TRUE(originating_url == restored_url->originating_url()); | 127 EXPECT_TRUE(originating_url == restored_url->originating_url()); |
| 132 | 128 |
| 133 EXPECT_EQ(32, restored_url->usage_count()); | 129 EXPECT_EQ(32, restored_url->usage_count()); |
| 134 | 130 |
| 135 ASSERT_EQ(1, restored_url->input_encodings().size()); | 131 ASSERT_EQ(1U, restored_url->input_encodings().size()); |
| 136 EXPECT_EQ("UTF-8", restored_url->input_encodings()[0]); | 132 EXPECT_EQ("UTF-8", restored_url->input_encodings()[0]); |
| 137 | 133 |
| 138 EXPECT_EQ(10, restored_url->prepopulate_id()); | 134 EXPECT_EQ(10, restored_url->prepopulate_id()); |
| 139 | 135 |
| 140 EXPECT_TRUE(db.RemoveKeyword(restored_url->id())); | 136 EXPECT_TRUE(db.RemoveKeyword(restored_url->id())); |
| 141 | 137 |
| 142 template_urls.clear(); | 138 template_urls.clear(); |
| 143 EXPECT_TRUE(db.GetKeywords(&template_urls)); | 139 EXPECT_TRUE(db.GetKeywords(&template_urls)); |
| 144 | 140 |
| 145 EXPECT_EQ(0, template_urls.size()); | 141 EXPECT_EQ(0U, template_urls.size()); |
| 146 | 142 |
| 147 delete restored_url; | 143 delete restored_url; |
| 148 } | 144 } |
| 149 | 145 |
| 150 TEST_F(WebDatabaseTest, KeywordMisc) { | 146 TEST_F(WebDatabaseTest, KeywordMisc) { |
| 151 WebDatabase db; | 147 WebDatabase db; |
| 152 | 148 |
| 153 EXPECT_TRUE(db.Init(file_)); | 149 EXPECT_TRUE(db.Init(file_)); |
| 154 | 150 |
| 155 ASSERT_EQ(0, db.GetDefaulSearchProviderID()); | 151 ASSERT_EQ(0, db.GetDefaulSearchProviderID()); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 185 template_url.set_originating_url(originating_url2); | 181 template_url.set_originating_url(originating_url2); |
| 186 template_url.set_autogenerate_keyword(true); | 182 template_url.set_autogenerate_keyword(true); |
| 187 EXPECT_EQ(L"url", template_url.keyword()); | 183 EXPECT_EQ(L"url", template_url.keyword()); |
| 188 template_url.add_input_encoding("Shift_JIS"); | 184 template_url.add_input_encoding("Shift_JIS"); |
| 189 set_prepopulate_id(&template_url, 5); | 185 set_prepopulate_id(&template_url, 5); |
| 190 EXPECT_TRUE(db.UpdateKeyword(template_url)); | 186 EXPECT_TRUE(db.UpdateKeyword(template_url)); |
| 191 | 187 |
| 192 std::vector<TemplateURL*> template_urls; | 188 std::vector<TemplateURL*> template_urls; |
| 193 EXPECT_TRUE(db.GetKeywords(&template_urls)); | 189 EXPECT_TRUE(db.GetKeywords(&template_urls)); |
| 194 | 190 |
| 195 EXPECT_EQ(1, template_urls.size()); | 191 EXPECT_EQ(1U, template_urls.size()); |
| 196 const TemplateURL* restored_url = template_urls.front(); | 192 const TemplateURL* restored_url = template_urls.front(); |
| 197 | 193 |
| 198 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); | 194 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); |
| 199 | 195 |
| 200 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); | 196 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); |
| 201 | 197 |
| 202 EXPECT_TRUE(restored_url->autogenerate_keyword()); | 198 EXPECT_TRUE(restored_url->autogenerate_keyword()); |
| 203 | 199 |
| 204 EXPECT_TRUE(favicon_url == restored_url->GetFavIconURL()); | 200 EXPECT_TRUE(favicon_url == restored_url->GetFavIconURL()); |
| 205 | 201 |
| 206 EXPECT_TRUE(restored_url->safe_for_autoreplace()); | 202 EXPECT_TRUE(restored_url->safe_for_autoreplace()); |
| 207 | 203 |
| 208 EXPECT_TRUE(restored_url->show_in_default_list()); | 204 EXPECT_TRUE(restored_url->show_in_default_list()); |
| 209 | 205 |
| 210 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); | 206 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); |
| 211 | 207 |
| 212 EXPECT_TRUE(originating_url2 == restored_url->originating_url()); | 208 EXPECT_TRUE(originating_url2 == restored_url->originating_url()); |
| 213 | 209 |
| 214 ASSERT_EQ(1, restored_url->input_encodings().size()); | 210 ASSERT_EQ(1U, restored_url->input_encodings().size()); |
| 215 ASSERT_EQ("Shift_JIS", restored_url->input_encodings()[0]); | 211 ASSERT_EQ("Shift_JIS", restored_url->input_encodings()[0]); |
| 216 | 212 |
| 217 EXPECT_EQ(template_url.suggestions_url()->url(), | 213 EXPECT_EQ(template_url.suggestions_url()->url(), |
| 218 restored_url->suggestions_url()->url()); | 214 restored_url->suggestions_url()->url()); |
| 219 | 215 |
| 220 EXPECT_EQ(template_url.id(), restored_url->id()); | 216 EXPECT_EQ(template_url.id(), restored_url->id()); |
| 221 | 217 |
| 222 EXPECT_EQ(template_url.prepopulate_id(), restored_url->prepopulate_id()); | 218 EXPECT_EQ(template_url.prepopulate_id(), restored_url->prepopulate_id()); |
| 223 | 219 |
| 224 delete restored_url; | 220 delete restored_url; |
| 225 } | 221 } |
| 226 | 222 |
| 227 TEST_F(WebDatabaseTest, KeywordWithNoFavicon) { | 223 TEST_F(WebDatabaseTest, KeywordWithNoFavicon) { |
| 228 WebDatabase db; | 224 WebDatabase db; |
| 229 | 225 |
| 230 EXPECT_TRUE(db.Init(file_)); | 226 EXPECT_TRUE(db.Init(file_)); |
| 231 | 227 |
| 232 TemplateURL template_url; | 228 TemplateURL template_url; |
| 233 template_url.set_short_name(L"short_name"); | 229 template_url.set_short_name(L"short_name"); |
| 234 template_url.set_keyword(L"keyword"); | 230 template_url.set_keyword(L"keyword"); |
| 235 template_url.SetURL(L"http://url/", 0, 0); | 231 template_url.SetURL(L"http://url/", 0, 0); |
| 236 template_url.set_safe_for_autoreplace(true); | 232 template_url.set_safe_for_autoreplace(true); |
| 237 SetID(-100, &template_url); | 233 SetID(-100, &template_url); |
| 238 | 234 |
| 239 EXPECT_TRUE(db.AddKeyword(template_url)); | 235 EXPECT_TRUE(db.AddKeyword(template_url)); |
| 240 | 236 |
| 241 std::vector<TemplateURL*> template_urls; | 237 std::vector<TemplateURL*> template_urls; |
| 242 EXPECT_TRUE(db.GetKeywords(&template_urls)); | 238 EXPECT_TRUE(db.GetKeywords(&template_urls)); |
| 243 EXPECT_EQ(1, template_urls.size()); | 239 EXPECT_EQ(1U, template_urls.size()); |
| 244 const TemplateURL* restored_url = template_urls.front(); | 240 const TemplateURL* restored_url = template_urls.front(); |
| 245 | 241 |
| 246 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); | 242 EXPECT_EQ(template_url.short_name(), restored_url->short_name()); |
| 247 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); | 243 EXPECT_EQ(template_url.keyword(), restored_url->keyword()); |
| 248 EXPECT_TRUE(!restored_url->GetFavIconURL().is_valid()); | 244 EXPECT_TRUE(!restored_url->GetFavIconURL().is_valid()); |
| 249 EXPECT_TRUE(restored_url->safe_for_autoreplace()); | 245 EXPECT_TRUE(restored_url->safe_for_autoreplace()); |
| 250 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); | 246 EXPECT_EQ(GetID(&template_url), GetID(restored_url)); |
| 251 delete restored_url; | 247 delete restored_url; |
| 252 } | 248 } |
| 253 | 249 |
| 254 TEST_F(WebDatabaseTest, Logins) { | 250 TEST_F(WebDatabaseTest, Logins) { |
| 255 WebDatabase db; | 251 WebDatabase db; |
| 256 | 252 |
| 257 EXPECT_TRUE(db.Init(file_)); | 253 EXPECT_TRUE(db.Init(file_)); |
| 258 | 254 |
| 259 std::vector<PasswordForm*> result; | 255 std::vector<PasswordForm*> result; |
| 260 | 256 |
| 261 // Verify the database is empty. | 257 // Verify the database is empty. |
| 262 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 258 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 263 EXPECT_EQ(0, result.size()); | 259 EXPECT_EQ(0U, result.size()); |
| 264 | 260 |
| 265 // Example password form. | 261 // Example password form. |
| 266 PasswordForm form; | 262 PasswordForm form; |
| 267 form.origin = GURL(L"http://www.google.com/accounts/LoginAuth"); | 263 form.origin = GURL("http://www.google.com/accounts/LoginAuth"); |
| 268 form.action = GURL(L"http://www.google.com/accounts/Login"); | 264 form.action = GURL("http://www.google.com/accounts/Login"); |
| 269 form.username_element = L"Email"; | 265 form.username_element = L"Email"; |
| 270 form.username_value = L"test@gmail.com"; | 266 form.username_value = L"test@gmail.com"; |
| 271 form.password_element = L"Passwd"; | 267 form.password_element = L"Passwd"; |
| 272 form.password_value = L"test"; | 268 form.password_value = L"test"; |
| 273 form.submit_element = L"signIn"; | 269 form.submit_element = L"signIn"; |
| 274 form.signon_realm = "http://www.google.com/"; | 270 form.signon_realm = "http://www.google.com/"; |
| 275 form.ssl_valid = false; | 271 form.ssl_valid = false; |
| 276 form.preferred = false; | 272 form.preferred = false; |
| 277 form.scheme = PasswordForm::SCHEME_HTML; | 273 form.scheme = PasswordForm::SCHEME_HTML; |
| 278 | 274 |
| 279 // Add it and make sure it is there. | 275 // Add it and make sure it is there. |
| 280 EXPECT_TRUE(db.AddLogin(form)); | 276 EXPECT_TRUE(db.AddLogin(form)); |
| 281 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 277 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 282 EXPECT_EQ(1, result.size()); | 278 EXPECT_EQ(1U, result.size()); |
| 283 delete result[0]; | 279 delete result[0]; |
| 284 result.clear(); | 280 result.clear(); |
| 285 | 281 |
| 286 // Match against an exact copy. | 282 // Match against an exact copy. |
| 287 EXPECT_TRUE(db.GetLogins(form, &result)); | 283 EXPECT_TRUE(db.GetLogins(form, &result)); |
| 288 EXPECT_EQ(1, result.size()); | 284 EXPECT_EQ(1U, result.size()); |
| 289 delete result[0]; | 285 delete result[0]; |
| 290 result.clear(); | 286 result.clear(); |
| 291 | 287 |
| 292 // The example site changes... | 288 // The example site changes... |
| 293 PasswordForm form2(form); | 289 PasswordForm form2(form); |
| 294 form2.origin = GURL(L"http://www.google.com/new/accounts/LoginAuth"); | 290 form2.origin = GURL("http://www.google.com/new/accounts/LoginAuth"); |
| 295 form2.submit_element = L"reallySignIn"; | 291 form2.submit_element = L"reallySignIn"; |
| 296 | 292 |
| 297 // Match against an inexact copy | 293 // Match against an inexact copy |
| 298 EXPECT_TRUE(db.GetLogins(form2, &result)); | 294 EXPECT_TRUE(db.GetLogins(form2, &result)); |
| 299 EXPECT_EQ(1, result.size()); | 295 EXPECT_EQ(1U, result.size()); |
| 300 delete result[0]; | 296 delete result[0]; |
| 301 result.clear(); | 297 result.clear(); |
| 302 | 298 |
| 303 // Uh oh, the site changed origin & action URL's all at once! | 299 // Uh oh, the site changed origin & action URL's all at once! |
| 304 PasswordForm form3(form2); | 300 PasswordForm form3(form2); |
| 305 form3.action = GURL(L"http://www.google.com/new/accounts/Login"); | 301 form3.action = GURL("http://www.google.com/new/accounts/Login"); |
| 306 | 302 |
| 307 // signon_realm is the same, should match. | 303 // signon_realm is the same, should match. |
| 308 EXPECT_TRUE(db.GetLogins(form3, &result)); | 304 EXPECT_TRUE(db.GetLogins(form3, &result)); |
| 309 EXPECT_EQ(1, result.size()); | 305 EXPECT_EQ(1U, result.size()); |
| 310 delete result[0]; | 306 delete result[0]; |
| 311 result.clear(); | 307 result.clear(); |
| 312 | 308 |
| 313 // Imagine the site moves to a secure server for login. | 309 // Imagine the site moves to a secure server for login. |
| 314 PasswordForm form4(form3); | 310 PasswordForm form4(form3); |
| 315 form4.signon_realm = "https://www.google.com/"; | 311 form4.signon_realm = "https://www.google.com/"; |
| 316 form4.ssl_valid = true; | 312 form4.ssl_valid = true; |
| 317 | 313 |
| 318 // We have only an http record, so no match for this. | 314 // We have only an http record, so no match for this. |
| 319 EXPECT_TRUE(db.GetLogins(form4, &result)); | 315 EXPECT_TRUE(db.GetLogins(form4, &result)); |
| 320 EXPECT_EQ(0, result.size()); | 316 EXPECT_EQ(0U, result.size()); |
| 321 | 317 |
| 322 // Let's imagine the user logs into the secure site. | 318 // Let's imagine the user logs into the secure site. |
| 323 EXPECT_TRUE(db.AddLogin(form4)); | 319 EXPECT_TRUE(db.AddLogin(form4)); |
| 324 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 320 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 325 EXPECT_EQ(2, result.size()); | 321 EXPECT_EQ(2U, result.size()); |
| 326 delete result[0]; | 322 delete result[0]; |
| 327 delete result[1]; | 323 delete result[1]; |
| 328 result.clear(); | 324 result.clear(); |
| 329 | 325 |
| 330 // Now the match works | 326 // Now the match works |
| 331 EXPECT_TRUE(db.GetLogins(form4, &result)); | 327 EXPECT_TRUE(db.GetLogins(form4, &result)); |
| 332 EXPECT_EQ(1, result.size()); | 328 EXPECT_EQ(1U, result.size()); |
| 333 delete result[0]; | 329 delete result[0]; |
| 334 result.clear(); | 330 result.clear(); |
| 335 | 331 |
| 336 // The user chose to forget the original but not the new. | 332 // The user chose to forget the original but not the new. |
| 337 EXPECT_TRUE(db.RemoveLogin(form)); | 333 EXPECT_TRUE(db.RemoveLogin(form)); |
| 338 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 334 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 339 EXPECT_EQ(1, result.size()); | 335 EXPECT_EQ(1U, result.size()); |
| 340 delete result[0]; | 336 delete result[0]; |
| 341 result.clear(); | 337 result.clear(); |
| 342 | 338 |
| 343 // The old form wont match the new site (http vs https). | 339 // The old form wont match the new site (http vs https). |
| 344 EXPECT_TRUE(db.GetLogins(form, &result)); | 340 EXPECT_TRUE(db.GetLogins(form, &result)); |
| 345 EXPECT_EQ(0, result.size()); | 341 EXPECT_EQ(0U, result.size()); |
| 346 | 342 |
| 347 // The user's request for the HTTPS site is intercepted | 343 // The user's request for the HTTPS site is intercepted |
| 348 // by an attacker who presents an invalid SSL cert. | 344 // by an attacker who presents an invalid SSL cert. |
| 349 PasswordForm form5(form4); | 345 PasswordForm form5(form4); |
| 350 form5.ssl_valid = 0; | 346 form5.ssl_valid = 0; |
| 351 | 347 |
| 352 // It will match in this case. | 348 // It will match in this case. |
| 353 EXPECT_TRUE(db.GetLogins(form5, &result)); | 349 EXPECT_TRUE(db.GetLogins(form5, &result)); |
| 354 EXPECT_EQ(1, result.size()); | 350 EXPECT_EQ(1U, result.size()); |
| 355 delete result[0]; | 351 delete result[0]; |
| 356 result.clear(); | 352 result.clear(); |
| 357 | 353 |
| 358 // User changes his password. | 354 // User changes his password. |
| 359 PasswordForm form6(form5); | 355 PasswordForm form6(form5); |
| 360 form6.password_value = L"test6"; | 356 form6.password_value = L"test6"; |
| 361 form6.preferred = true; | 357 form6.preferred = true; |
| 362 | 358 |
| 363 // We update, and check to make sure it matches the | 359 // We update, and check to make sure it matches the |
| 364 // old form, and there is only one record. | 360 // old form, and there is only one record. |
| 365 EXPECT_TRUE(db.UpdateLogin(form6)); | 361 EXPECT_TRUE(db.UpdateLogin(form6)); |
| 366 // matches | 362 // matches |
| 367 EXPECT_TRUE(db.GetLogins(form5, &result)); | 363 EXPECT_TRUE(db.GetLogins(form5, &result)); |
| 368 EXPECT_EQ(1, result.size()); | 364 EXPECT_EQ(1U, result.size()); |
| 369 delete result[0]; | 365 delete result[0]; |
| 370 result.clear(); | 366 result.clear(); |
| 371 // Only one record. | 367 // Only one record. |
| 372 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 368 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 373 EXPECT_EQ(1, result.size()); | 369 EXPECT_EQ(1U, result.size()); |
| 374 // password element was updated. | 370 // password element was updated. |
| 375 EXPECT_EQ(form6.password_value, result[0]->password_value); | 371 EXPECT_EQ(form6.password_value, result[0]->password_value); |
| 376 // Preferred login. | 372 // Preferred login. |
| 377 EXPECT_TRUE(form6.preferred); | 373 EXPECT_TRUE(form6.preferred); |
| 378 delete result[0]; | 374 delete result[0]; |
| 379 result.clear(); | 375 result.clear(); |
| 380 | 376 |
| 381 // Make sure everything can disappear. | 377 // Make sure everything can disappear. |
| 382 EXPECT_TRUE(db.RemoveLogin(form4)); | 378 EXPECT_TRUE(db.RemoveLogin(form4)); |
| 383 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 379 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 384 EXPECT_EQ(0, result.size()); | 380 EXPECT_EQ(0U, result.size()); |
| 385 } | 381 } |
| 386 | 382 |
| 387 TEST_F(WebDatabaseTest, Autofill) { | 383 TEST_F(WebDatabaseTest, Autofill) { |
| 388 WebDatabase db; | 384 WebDatabase db; |
| 389 | 385 |
| 390 EXPECT_TRUE(db.Init(file_)); | 386 EXPECT_TRUE(db.Init(file_)); |
| 391 | 387 |
| 392 Time t1 = Time::Now(); | 388 Time t1 = Time::Now(); |
| 393 | 389 |
| 394 // Simulate the submission of a handful of entries in a field called "Name", | 390 // Simulate the submission of a handful of entries in a field called "Name", |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 | 423 |
| 428 EXPECT_TRUE(db.GetIDAndCountOfFormElement( | 424 EXPECT_TRUE(db.GetIDAndCountOfFormElement( |
| 429 AutofillForm::Element(L"Favorite Color", L"Green"), &pair_id, &count)); | 425 AutofillForm::Element(L"Favorite Color", L"Green"), &pair_id, &count)); |
| 430 EXPECT_EQ(2, count); | 426 EXPECT_EQ(2, count); |
| 431 | 427 |
| 432 // This is meant to get a list of suggestions for Name. The empty prefix | 428 // This is meant to get a list of suggestions for Name. The empty prefix |
| 433 // in the second argument means it should return all suggestions for a name | 429 // in the second argument means it should return all suggestions for a name |
| 434 // no matter what they start with. The order that the names occur in the list | 430 // no matter what they start with. The order that the names occur in the list |
| 435 // should be decreasing order by count. | 431 // should be decreasing order by count. |
| 436 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", std::wstring(), &v, 6)); | 432 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", std::wstring(), &v, 6)); |
| 437 EXPECT_EQ(3, v.size()); | 433 EXPECT_EQ(3U, v.size()); |
| 438 if (v.size() == 3) { | 434 if (v.size() == 3) { |
| 439 EXPECT_EQ(L"Clark Kent", v[0]); | 435 EXPECT_EQ(L"Clark Kent", v[0]); |
| 440 EXPECT_EQ(L"Clark Sutter", v[1]); | 436 EXPECT_EQ(L"Clark Sutter", v[1]); |
| 441 EXPECT_EQ(L"Superman", v[2]); | 437 EXPECT_EQ(L"Superman", v[2]); |
| 442 } | 438 } |
| 443 | 439 |
| 444 // If we query again limiting the list size to 1, we should only get the most | 440 // If we query again limiting the list size to 1, we should only get the most |
| 445 // frequent entry. | 441 // frequent entry. |
| 446 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", L"", &v, 1)); | 442 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", L"", &v, 1)); |
| 447 EXPECT_EQ(1, v.size()); | 443 EXPECT_EQ(1U, v.size()); |
| 448 if (v.size() == 1) { | 444 if (v.size() == 1) { |
| 449 EXPECT_EQ(L"Clark Kent", v[0]); | 445 EXPECT_EQ(L"Clark Kent", v[0]); |
| 450 } | 446 } |
| 451 | 447 |
| 452 // Querying for suggestions given a prefix is case-insensitive, so the prefix | 448 // Querying for suggestions given a prefix is case-insensitive, so the prefix |
| 453 // "cLa" shoud get suggestions for both Clarks. | 449 // "cLa" shoud get suggestions for both Clarks. |
| 454 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", L"cLa", &v, 6)); | 450 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", L"cLa", &v, 6)); |
| 455 EXPECT_EQ(2, v.size()); | 451 EXPECT_EQ(2U, v.size()); |
| 456 if (v.size() == 2) { | 452 if (v.size() == 2) { |
| 457 EXPECT_EQ(L"Clark Kent", v[0]); | 453 EXPECT_EQ(L"Clark Kent", v[0]); |
| 458 EXPECT_EQ(L"Clark Sutter", v[1]); | 454 EXPECT_EQ(L"Clark Sutter", v[1]); |
| 459 } | 455 } |
| 460 | 456 |
| 461 // Removing all elements since the beginning of this function should remove | 457 // Removing all elements since the beginning of this function should remove |
| 462 // everything from the database. | 458 // everything from the database. |
| 463 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t1, Time())); | 459 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t1, Time())); |
| 464 | 460 |
| 465 EXPECT_TRUE(db.GetIDAndCountOfFormElement( | 461 EXPECT_TRUE(db.GetIDAndCountOfFormElement( |
| 466 AutofillForm::Element(L"Name", L"Clark Kent"), &pair_id, &count)); | 462 AutofillForm::Element(L"Name", L"Clark Kent"), &pair_id, &count)); |
| 467 EXPECT_EQ(0, count); | 463 EXPECT_EQ(0, count); |
| 468 | 464 |
| 469 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", L"", &v, 6)); | 465 EXPECT_TRUE(db.GetFormValuesForElementName(L"Name", L"", &v, 6)); |
| 470 EXPECT_EQ(0, v.size()); | 466 EXPECT_EQ(0U, v.size()); |
| 471 } | 467 } |
| 472 | 468 |
| 473 static bool AddTimestampedLogin(WebDatabase* db, std::string url, | 469 static bool AddTimestampedLogin(WebDatabase* db, std::string url, |
| 474 std::wstring unique_string, const Time& time) { | 470 std::wstring unique_string, const Time& time) { |
| 475 // Example password form. | 471 // Example password form. |
| 476 PasswordForm form; | 472 PasswordForm form; |
| 477 form.origin = GURL(url + std::string("/LoginAuth")); | 473 form.origin = GURL(url + std::string("/LoginAuth")); |
| 478 form.username_element = unique_string.c_str(); | 474 form.username_element = unique_string.c_str(); |
| 479 form.username_value = unique_string.c_str(); | 475 form.username_value = unique_string.c_str(); |
| 480 form.password_element = unique_string.c_str(); | 476 form.password_element = unique_string.c_str(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 493 | 489 |
| 494 TEST_F(WebDatabaseTest, ClearPrivateData_SavedPasswords) { | 490 TEST_F(WebDatabaseTest, ClearPrivateData_SavedPasswords) { |
| 495 WebDatabase db; | 491 WebDatabase db; |
| 496 | 492 |
| 497 EXPECT_TRUE(db.Init(file_)); | 493 EXPECT_TRUE(db.Init(file_)); |
| 498 | 494 |
| 499 std::vector<PasswordForm*> result; | 495 std::vector<PasswordForm*> result; |
| 500 | 496 |
| 501 // Verify the database is empty. | 497 // Verify the database is empty. |
| 502 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 498 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 503 EXPECT_EQ(0, result.size()); | 499 EXPECT_EQ(0U, result.size()); |
| 504 | 500 |
| 505 Time now = Time::Now(); | 501 Time now = Time::Now(); |
| 506 TimeDelta one_day = TimeDelta::FromDays(1); | 502 TimeDelta one_day = TimeDelta::FromDays(1); |
| 507 | 503 |
| 508 // Create one with a 0 time. | 504 // Create one with a 0 time. |
| 509 EXPECT_TRUE(AddTimestampedLogin(&db, "1", L"foo1", Time())); | 505 EXPECT_TRUE(AddTimestampedLogin(&db, "1", L"foo1", Time())); |
| 510 // Create one for now and +/- 1 day. | 506 // Create one for now and +/- 1 day. |
| 511 EXPECT_TRUE(AddTimestampedLogin(&db, "2", L"foo2", now - one_day)); | 507 EXPECT_TRUE(AddTimestampedLogin(&db, "2", L"foo2", now - one_day)); |
| 512 EXPECT_TRUE(AddTimestampedLogin(&db, "3", L"foo3", now)); | 508 EXPECT_TRUE(AddTimestampedLogin(&db, "3", L"foo3", now)); |
| 513 EXPECT_TRUE(AddTimestampedLogin(&db, "4", L"foo4", now + one_day)); | 509 EXPECT_TRUE(AddTimestampedLogin(&db, "4", L"foo4", now + one_day)); |
| 514 | 510 |
| 515 // Verify inserts worked. | 511 // Verify inserts worked. |
| 516 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 512 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 517 EXPECT_EQ(4, result.size()); | 513 EXPECT_EQ(4U, result.size()); |
| 518 ClearResults(&result); | 514 ClearResults(&result); |
| 519 | 515 |
| 520 // Delete everything from today's date and on. | 516 // Delete everything from today's date and on. |
| 521 db.RemoveLoginsCreatedBetween(now, Time()); | 517 db.RemoveLoginsCreatedBetween(now, Time()); |
| 522 | 518 |
| 523 // Should have deleted half of what we inserted. | 519 // Should have deleted half of what we inserted. |
| 524 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 520 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 525 EXPECT_EQ(2, result.size()); | 521 EXPECT_EQ(2U, result.size()); |
| 526 ClearResults(&result); | 522 ClearResults(&result); |
| 527 | 523 |
| 528 // Delete with 0 date (should delete all). | 524 // Delete with 0 date (should delete all). |
| 529 db.RemoveLoginsCreatedBetween(Time(), Time()); | 525 db.RemoveLoginsCreatedBetween(Time(), Time()); |
| 530 | 526 |
| 531 // Verify nothing is left. | 527 // Verify nothing is left. |
| 532 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 528 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 533 EXPECT_EQ(0, result.size()); | 529 EXPECT_EQ(0U, result.size()); |
| 534 } | 530 } |
| 535 | 531 |
| 536 TEST_F(WebDatabaseTest, BlacklistedLogins) { | 532 TEST_F(WebDatabaseTest, BlacklistedLogins) { |
| 537 WebDatabase db; | 533 WebDatabase db; |
| 538 | 534 |
| 539 EXPECT_TRUE(db.Init(file_)); | 535 EXPECT_TRUE(db.Init(file_)); |
| 540 std::vector<PasswordForm*> result; | 536 std::vector<PasswordForm*> result; |
| 541 | 537 |
| 542 // Verify the database is empty. | 538 // Verify the database is empty. |
| 543 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 539 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 544 ASSERT_EQ(0, result.size()); | 540 ASSERT_EQ(0U, result.size()); |
| 545 | 541 |
| 546 // Save a form as blacklisted. | 542 // Save a form as blacklisted. |
| 547 PasswordForm form; | 543 PasswordForm form; |
| 548 form.origin = GURL(L"http://www.google.com/accounts/LoginAuth"); | 544 form.origin = GURL("http://www.google.com/accounts/LoginAuth"); |
| 549 form.action = GURL(L"http://www.google.com/accounts/Login"); | 545 form.action = GURL("http://www.google.com/accounts/Login"); |
| 550 form.username_element = L"Email"; | 546 form.username_element = L"Email"; |
| 551 form.password_element = L"Passwd"; | 547 form.password_element = L"Passwd"; |
| 552 form.submit_element = L"signIn"; | 548 form.submit_element = L"signIn"; |
| 553 form.signon_realm = "http://www.google.com/"; | 549 form.signon_realm = "http://www.google.com/"; |
| 554 form.ssl_valid = false; | 550 form.ssl_valid = false; |
| 555 form.preferred = true; | 551 form.preferred = true; |
| 556 form.blacklisted_by_user = true; | 552 form.blacklisted_by_user = true; |
| 557 form.scheme = PasswordForm::SCHEME_HTML; | 553 form.scheme = PasswordForm::SCHEME_HTML; |
| 558 EXPECT_TRUE(db.AddLogin(form)); | 554 EXPECT_TRUE(db.AddLogin(form)); |
| 559 | 555 |
| 560 // Get all non-blacklisted logins (should be none). | 556 // Get all non-blacklisted logins (should be none). |
| 561 EXPECT_TRUE(db.GetAllLogins(&result, false)); | 557 EXPECT_TRUE(db.GetAllLogins(&result, false)); |
| 562 ASSERT_EQ(0, result.size()); | 558 ASSERT_EQ(0U, result.size()); |
| 563 | 559 |
| 564 // GetLogins should give the blacklisted result. | 560 // GetLogins should give the blacklisted result. |
| 565 EXPECT_TRUE(db.GetLogins(form, &result)); | 561 EXPECT_TRUE(db.GetLogins(form, &result)); |
| 566 EXPECT_EQ(1, result.size()); | 562 EXPECT_EQ(1U, result.size()); |
| 567 ClearResults(&result); | 563 ClearResults(&result); |
| 568 | 564 |
| 569 // So should GetAll including blacklisted. | 565 // So should GetAll including blacklisted. |
| 570 EXPECT_TRUE(db.GetAllLogins(&result, true)); | 566 EXPECT_TRUE(db.GetAllLogins(&result, true)); |
| 571 EXPECT_EQ(1, result.size()); | 567 EXPECT_EQ(1U, result.size()); |
| 572 ClearResults(&result); | 568 ClearResults(&result); |
| 573 } | 569 } |
| 574 | 570 |
| 575 TEST_F(WebDatabaseTest, WebAppHasAllImages) { | 571 TEST_F(WebDatabaseTest, WebAppHasAllImages) { |
| 576 WebDatabase db; | 572 WebDatabase db; |
| 577 | 573 |
| 578 EXPECT_TRUE(db.Init(file_)); | 574 EXPECT_TRUE(db.Init(file_)); |
| 579 GURL url("http://google.com/"); | 575 GURL url("http://google.com/"); |
| 580 | 576 |
| 581 // Initial value for unknown web app should be false. | 577 // Initial value for unknown web app should be false. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 592 | 588 |
| 593 TEST_F(WebDatabaseTest, WebAppImages) { | 589 TEST_F(WebDatabaseTest, WebAppImages) { |
| 594 WebDatabase db; | 590 WebDatabase db; |
| 595 | 591 |
| 596 ASSERT_TRUE(db.Init(file_)); | 592 ASSERT_TRUE(db.Init(file_)); |
| 597 GURL url("http://google.com/"); | 593 GURL url("http://google.com/"); |
| 598 | 594 |
| 599 // Web app should initially have no images. | 595 // Web app should initially have no images. |
| 600 std::vector<SkBitmap> images; | 596 std::vector<SkBitmap> images; |
| 601 ASSERT_TRUE(db.GetWebAppImages(url, &images)); | 597 ASSERT_TRUE(db.GetWebAppImages(url, &images)); |
| 602 ASSERT_EQ(0, images.size()); | 598 ASSERT_EQ(0U, images.size()); |
| 603 | 599 |
| 604 // Add an image. | 600 // Add an image. |
| 605 SkBitmap image; | 601 SkBitmap image; |
| 606 image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); | 602 image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
| 607 image.allocPixels(); | 603 image.allocPixels(); |
| 608 image.eraseColor(SK_ColorBLACK); | 604 image.eraseColor(SK_ColorBLACK); |
| 609 ASSERT_TRUE(db.SetWebAppImage(url, image)); | 605 ASSERT_TRUE(db.SetWebAppImage(url, image)); |
| 610 | 606 |
| 611 // Make sure we get the image back. | 607 // Make sure we get the image back. |
| 612 ASSERT_TRUE(db.GetWebAppImages(url, &images)); | 608 ASSERT_TRUE(db.GetWebAppImages(url, &images)); |
| 613 ASSERT_EQ(1, images.size()); | 609 ASSERT_EQ(1U, images.size()); |
| 614 ASSERT_EQ(16, images[0].width()); | 610 ASSERT_EQ(16, images[0].width()); |
| 615 ASSERT_EQ(16, images[0].height()); | 611 ASSERT_EQ(16, images[0].height()); |
| 616 | 612 |
| 617 // Add another 16x16 image and make sure it replaces the original. | 613 // Add another 16x16 image and make sure it replaces the original. |
| 618 image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); | 614 image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
| 619 image.allocPixels(); | 615 image.allocPixels(); |
| 620 image.eraseColor(SK_ColorBLACK); | 616 image.eraseColor(SK_ColorBLACK); |
| 621 // Some random pixels so that we can identify the image. | 617 // Some random pixels so that we can identify the image. |
| 622 *(reinterpret_cast<unsigned char*>(image.getPixels())) = 0xAB; | 618 *(reinterpret_cast<unsigned char*>(image.getPixels())) = 0xAB; |
| 623 ASSERT_TRUE(db.SetWebAppImage(url, image)); | 619 ASSERT_TRUE(db.SetWebAppImage(url, image)); |
| 624 images.clear(); | 620 images.clear(); |
| 625 ASSERT_TRUE(db.GetWebAppImages(url, &images)); | 621 ASSERT_TRUE(db.GetWebAppImages(url, &images)); |
| 626 ASSERT_EQ(1, images.size()); | 622 ASSERT_EQ(1U, images.size()); |
| 627 ASSERT_EQ(16, images[0].width()); | 623 ASSERT_EQ(16, images[0].width()); |
| 628 ASSERT_EQ(16, images[0].height()); | 624 ASSERT_EQ(16, images[0].height()); |
| 629 images[0].lockPixels(); | 625 images[0].lockPixels(); |
| 630 unsigned char* pixels = | 626 unsigned char* pixels = |
| 631 reinterpret_cast<unsigned char*>(images[0].getPixels()); | 627 reinterpret_cast<unsigned char*>(images[0].getPixels()); |
| 632 ASSERT_TRUE(pixels != NULL); | 628 ASSERT_TRUE(pixels != NULL); |
| 633 ASSERT_EQ(0xAB, *pixels); | 629 ASSERT_EQ(0xAB, *pixels); |
| 634 images[0].unlockPixels(); | 630 images[0].unlockPixels(); |
| 635 | 631 |
| 636 // Add another image at a bigger size. | 632 // Add another image at a bigger size. |
| 637 image.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 633 image.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
| 638 image.allocPixels(); | 634 image.allocPixels(); |
| 639 image.eraseColor(SK_ColorBLACK); | 635 image.eraseColor(SK_ColorBLACK); |
| 640 ASSERT_TRUE(db.SetWebAppImage(url, image)); | 636 ASSERT_TRUE(db.SetWebAppImage(url, image)); |
| 641 | 637 |
| 642 // Make sure we get both images back. | 638 // Make sure we get both images back. |
| 643 images.clear(); | 639 images.clear(); |
| 644 ASSERT_TRUE(db.GetWebAppImages(url, &images)); | 640 ASSERT_TRUE(db.GetWebAppImages(url, &images)); |
| 645 ASSERT_EQ(2, images.size()); | 641 ASSERT_EQ(2U, images.size()); |
| 646 if (images[0].width() == 16) { | 642 if (images[0].width() == 16) { |
| 647 ASSERT_EQ(16, images[0].width()); | 643 ASSERT_EQ(16, images[0].width()); |
| 648 ASSERT_EQ(16, images[0].height()); | 644 ASSERT_EQ(16, images[0].height()); |
| 649 ASSERT_EQ(32, images[1].width()); | 645 ASSERT_EQ(32, images[1].width()); |
| 650 ASSERT_EQ(32, images[1].height()); | 646 ASSERT_EQ(32, images[1].height()); |
| 651 } else { | 647 } else { |
| 652 ASSERT_EQ(32, images[0].width()); | 648 ASSERT_EQ(32, images[0].width()); |
| 653 ASSERT_EQ(32, images[0].height()); | 649 ASSERT_EQ(32, images[0].height()); |
| 654 ASSERT_EQ(16, images[1].width()); | 650 ASSERT_EQ(16, images[1].width()); |
| 655 ASSERT_EQ(16, images[1].height()); | 651 ASSERT_EQ(16, images[1].height()); |
| 656 } | 652 } |
| 657 } | 653 } |
| 658 | 654 |
| OLD | NEW |