Home | History | Annotate | Download | only in renderer
      1 // Copyright 2013 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 "base/strings/string16.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #include "content/public/common/password_form.h"
      8 #include "content/public/renderer/password_form_conversion_utils.h"
      9 #include "content/public/test/render_view_test.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "third_party/WebKit/public/platform/WebVector.h"
     12 #include "third_party/WebKit/public/web/WebDocument.h"
     13 #include "third_party/WebKit/public/web/WebFormElement.h"
     14 #include "third_party/WebKit/public/web/WebPasswordFormData.h"
     15 
     16 using WebKit::WebFormElement;
     17 using WebKit::WebFrame;
     18 using WebKit::WebPasswordFormData;
     19 using WebKit::WebVector;
     20 
     21 namespace content {
     22 namespace {
     23 
     24 class PasswordFormConversionUtilsTest : public RenderViewTest {
     25  public:
     26   PasswordFormConversionUtilsTest() : RenderViewTest() {}
     27   virtual ~PasswordFormConversionUtilsTest() {}
     28 
     29  private:
     30   DISALLOW_COPY_AND_ASSIGN(PasswordFormConversionUtilsTest);
     31 };
     32 
     33 }  // namespace
     34 
     35 TEST_F(PasswordFormConversionUtilsTest, ValidWebFormElementToPasswordForm) {
     36   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
     37            " <INPUT type=\"text\" name=\"username\" "
     38                " id=\"username\" value=\"johnsmith\"/>"
     39            " <INPUT type=\"submit\" name=\"submit\" value=\"Submit\"/>"
     40            " <INPUT type=\"password\" name=\"password\" id=\"password\" "
     41                " value=\"secret\"/>"
     42            "</FORM>");
     43 
     44   WebFrame* frame = GetMainFrame();
     45   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
     46 
     47   WebVector<WebFormElement> forms;
     48   frame->document().forms(forms);
     49   ASSERT_EQ(1U, forms.size());
     50   WebPasswordFormData web_password_form(forms[0]);
     51   ASSERT_TRUE(web_password_form.isValid());
     52 
     53   scoped_ptr<PasswordForm> password_form = CreatePasswordForm(forms[0]);
     54   ASSERT_NE(static_cast<PasswordForm*>(NULL), password_form.get());
     55 
     56   EXPECT_EQ("data:", password_form->signon_realm);
     57   EXPECT_EQ(GURL("http://cnn.com"), password_form->action);
     58   EXPECT_EQ(UTF8ToUTF16("username"), password_form->username_element);
     59   EXPECT_EQ(UTF8ToUTF16("johnsmith"), password_form->username_value);
     60   EXPECT_EQ(UTF8ToUTF16("password"), password_form->password_element);
     61   EXPECT_EQ(UTF8ToUTF16("secret"), password_form->password_value);
     62   EXPECT_EQ(PasswordForm::SCHEME_HTML, password_form->scheme);
     63   EXPECT_FALSE(password_form->ssl_valid);
     64   EXPECT_FALSE(password_form->preferred);
     65   EXPECT_FALSE(password_form->blacklisted_by_user);
     66   EXPECT_EQ(PasswordForm::TYPE_MANUAL, password_form->type);
     67 }
     68 
     69 TEST_F(PasswordFormConversionUtilsTest, InvalidWebFormElementToPasswordForm) {
     70   LoadHTML("<FORM name=\"TestForm\" action=\"invalid\" method=\"post\">"
     71            " <INPUT type=\"text\" name=\"username\" "
     72                " id=\"username\" value=\"johnsmith\"/>"
     73            " <INPUT type=\"submit\" name=\"submit\" value=\"Submit\"/>"
     74            " <INPUT type=\"password\" name=\"password\" id=\"password\" "
     75                " value=\"secret\"/>"
     76            "</FORM>");
     77 
     78   WebFrame* frame = GetMainFrame();
     79   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
     80 
     81   WebVector<WebFormElement> forms;
     82   frame->document().forms(forms);
     83   ASSERT_EQ(1U, forms.size());
     84   WebPasswordFormData web_password_form(forms[0]);
     85   ASSERT_FALSE(web_password_form.isValid());
     86 
     87   scoped_ptr<PasswordForm> password_form = CreatePasswordForm(forms[0]);
     88   EXPECT_EQ(static_cast<PasswordForm*>(NULL), password_form.get());
     89 }
     90 
     91 }  // namespace content
     92