Home | History | Annotate | Download | only in browser
      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 <vector>
      6 
      7 #include "base/memory/ref_counted.h"
      8 #include "base/string16.h"
      9 #include "base/task.h"
     10 #include "base/utf_string_conversions.h"
     11 #include "chrome/browser/autocomplete_history_manager.h"
     12 #include "chrome/browser/webdata/web_data_service.h"
     13 #include "chrome/test/testing_browser_process.h"
     14 #include "chrome/test/testing_browser_process_test.h"
     15 #include "chrome/test/testing_profile.h"
     16 #include "content/browser/renderer_host/test_render_view_host.h"
     17 #include "content/browser/tab_contents/test_tab_contents.h"
     18 #include "testing/gmock/include/gmock/gmock.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 #include "webkit/glue/form_data.h"
     21 
     22 using testing::_;
     23 using webkit_glue::FormData;
     24 
     25 class MockWebDataService : public WebDataService {
     26  public:
     27   MOCK_METHOD1(AddFormFields,
     28                void(const std::vector<webkit_glue::FormField>&));  // NOLINT
     29 };
     30 
     31 class AutocompleteHistoryManagerTest : public RenderViewHostTestHarness {
     32  protected:
     33   AutocompleteHistoryManagerTest()
     34       : ui_thread_(BrowserThread::UI, MessageLoopForUI::current()) {
     35   }
     36 
     37   virtual void SetUp() {
     38     RenderViewHostTestHarness::SetUp();
     39     web_data_service_ = new MockWebDataService();
     40     autocomplete_manager_.reset(new AutocompleteHistoryManager(
     41         contents(), &profile_, web_data_service_));
     42   }
     43 
     44   BrowserThread ui_thread_;
     45 
     46   TestingProfile profile_;
     47   scoped_refptr<MockWebDataService> web_data_service_;
     48   scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_;
     49 };
     50 
     51 // Tests that credit card numbers are not sent to the WebDatabase to be saved.
     52 TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) {
     53   FormData form;
     54   form.name = ASCIIToUTF16("MyForm");
     55   form.method = ASCIIToUTF16("POST");
     56   form.origin = GURL("http://myform.com/form.html");
     57   form.action = GURL("http://myform.com/submit.html");
     58   form.user_submitted = true;
     59 
     60   // Valid Visa credit card number pulled from the paypal help site.
     61   webkit_glue::FormField valid_cc(ASCIIToUTF16("Credit Card"),
     62                                   ASCIIToUTF16("ccnum"),
     63                                   ASCIIToUTF16("4012888888881881"),
     64                                   ASCIIToUTF16("text"),
     65                                   20,
     66                                   false);
     67   form.fields.push_back(valid_cc);
     68 
     69   EXPECT_CALL(*web_data_service_, AddFormFields(_)).Times(0);
     70   autocomplete_manager_->OnFormSubmitted(form);
     71 }
     72 
     73 // Contrary test to AutocompleteHistoryManagerTest.CreditCardNumberValue.  The
     74 // value being submitted is not a valid credit card number, so it will be sent
     75 // to the WebDatabase to be saved.
     76 TEST_F(AutocompleteHistoryManagerTest, NonCreditCardNumberValue) {
     77   FormData form;
     78   form.name = ASCIIToUTF16("MyForm");
     79   form.method = ASCIIToUTF16("POST");
     80   form.origin = GURL("http://myform.com/form.html");
     81   form.action = GURL("http://myform.com/submit.html");
     82   form.user_submitted = true;
     83 
     84   // Invalid credit card number.
     85   webkit_glue::FormField invalid_cc(ASCIIToUTF16("Credit Card"),
     86                                     ASCIIToUTF16("ccnum"),
     87                                     ASCIIToUTF16("4580123456789012"),
     88                                     ASCIIToUTF16("text"),
     89                                     20,
     90                                     false);
     91   form.fields.push_back(invalid_cc);
     92 
     93   EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1);
     94   autocomplete_manager_->OnFormSubmitted(form);
     95 }
     96 
     97 // Tests that SSNs are not sent to the WebDatabase to be saved.
     98 TEST_F(AutocompleteHistoryManagerTest, SSNValue) {
     99   FormData form;
    100   form.name = ASCIIToUTF16("MyForm");
    101   form.method = ASCIIToUTF16("POST");
    102   form.origin = GURL("http://myform.com/form.html");
    103   form.action = GURL("http://myform.com/submit.html");
    104   form.user_submitted = true;
    105 
    106   webkit_glue::FormField ssn(ASCIIToUTF16("Social Security Number"),
    107                              ASCIIToUTF16("ssn"),
    108                              ASCIIToUTF16("078-05-1120"),
    109                              ASCIIToUTF16("text"),
    110                              20,
    111                              false);
    112   form.fields.push_back(ssn);
    113 
    114   EXPECT_CALL(*web_data_service_, AddFormFields(_)).Times(0);
    115   autocomplete_manager_->OnFormSubmitted(form);
    116 }
    117