summaryrefslogtreecommitdiffstats
path: root/content/renderer/password_form_conversion_utils_browsertest.cc
blob: 5c70d3612636d59f3f5387136ae6cd981a84d2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "content/public/common/password_form.h"
#include "content/public/renderer/password_form_conversion_utils.h"
#include "content/public/test/render_view_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPasswordFormData.h"

using WebKit::WebFormElement;
using WebKit::WebFrame;
using WebKit::WebPasswordFormData;
using WebKit::WebVector;

namespace content {
namespace {

class PasswordFormConversionUtilsTest : public RenderViewTest {
 public:
  PasswordFormConversionUtilsTest() : RenderViewTest() {}
  virtual ~PasswordFormConversionUtilsTest() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(PasswordFormConversionUtilsTest);
};

}  // namespace

TEST_F(PasswordFormConversionUtilsTest, ValidWebFormElementToPasswordForm) {
  LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
           " <INPUT type=\"text\" name=\"username\" "
               " id=\"username\" value=\"johnsmith\"/>"
           " <INPUT type=\"submit\" name=\"submit\" value=\"Submit\"/>"
           " <INPUT type=\"password\" name=\"password\" id=\"password\" "
               " value=\"secret\"/>"
           "</FORM>");

  WebFrame* frame = GetMainFrame();
  ASSERT_NE(static_cast<WebFrame*>(NULL), frame);

  WebVector<WebFormElement> forms;
  frame->document().forms(forms);
  ASSERT_EQ(1U, forms.size());
  WebPasswordFormData web_password_form(forms[0]);
  ASSERT_TRUE(web_password_form.isValid());

  scoped_ptr<PasswordForm> password_form = CreatePasswordForm(forms[0]);
  ASSERT_NE(static_cast<PasswordForm*>(NULL), password_form.get());

  EXPECT_EQ("data:", password_form->signon_realm);
  EXPECT_EQ(GURL("http://cnn.com"), password_form->action);
  EXPECT_EQ(UTF8ToUTF16("username"), password_form->username_element);
  EXPECT_EQ(UTF8ToUTF16("johnsmith"), password_form->username_value);
  EXPECT_EQ(UTF8ToUTF16("password"), password_form->password_element);
  EXPECT_EQ(UTF8ToUTF16("secret"), password_form->password_value);
  EXPECT_EQ(PasswordForm::SCHEME_HTML, password_form->scheme);
  EXPECT_FALSE(password_form->ssl_valid);
  EXPECT_FALSE(password_form->preferred);
  EXPECT_FALSE(password_form->blacklisted_by_user);
  EXPECT_EQ(PasswordForm::TYPE_MANUAL, password_form->type);
}

TEST_F(PasswordFormConversionUtilsTest, InvalidWebFormElementToPasswordForm) {
  LoadHTML("<FORM name=\"TestForm\" action=\"invalid\" method=\"post\">"
           " <INPUT type=\"text\" name=\"username\" "
               " id=\"username\" value=\"johnsmith\"/>"
           " <INPUT type=\"submit\" name=\"submit\" value=\"Submit\"/>"
           " <INPUT type=\"password\" name=\"password\" id=\"password\" "
               " value=\"secret\"/>"
           "</FORM>");

  WebFrame* frame = GetMainFrame();
  ASSERT_NE(static_cast<WebFrame*>(NULL), frame);

  WebVector<WebFormElement> forms;
  frame->document().forms(forms);
  ASSERT_EQ(1U, forms.size());
  WebPasswordFormData web_password_form(forms[0]);
  ASSERT_FALSE(web_password_form.isValid());

  scoped_ptr<PasswordForm> password_form = CreatePasswordForm(forms[0]);
  EXPECT_EQ(static_cast<PasswordForm*>(NULL), password_form.get());
}

}  // namespace content