| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/credentialmanager/PasswordCredential.h" | 5 #include "modules/credentialmanager/PasswordCredential.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 9 #include "core/dom/ExceptionCode.h" | 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "core/dom/URLSearchParams.h" |
| 10 #include "core/frame/FrameView.h" | 11 #include "core/frame/FrameView.h" |
| 11 #include "core/html/FormData.h" | 12 #include "core/html/FormData.h" |
| 12 #include "core/html/HTMLDocument.h" | 13 #include "core/html/HTMLDocument.h" |
| 13 #include "core/html/HTMLFormElement.h" | 14 #include "core/html/HTMLFormElement.h" |
| 14 #include "core/html/forms/FormController.h" | 15 #include "core/html/forms/FormController.h" |
| 15 #include "core/testing/DummyPageHolder.h" | 16 #include "core/testing/DummyPageHolder.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "wtf/text/StringBuilder.h" | 18 #include "wtf/text/StringBuilder.h" |
| 18 | 19 |
| 19 namespace blink { | 20 namespace blink { |
| 20 | 21 |
| 21 class PasswordCredentialTest : public ::testing::Test { | 22 class PasswordCredentialTest : public ::testing::Test { |
| 22 protected: | 23 protected: |
| 23 void SetUp() override | 24 void SetUp() override |
| 24 { | 25 { |
| 25 m_dummyPageHolder = DummyPageHolder::create(); | 26 m_dummyPageHolder = DummyPageHolder::create(); |
| 26 m_document = toHTMLDocument(&m_dummyPageHolder->document()); | 27 m_document = toHTMLDocument(&m_dummyPageHolder->document()); |
| 27 } | 28 } |
| 28 | 29 |
| 29 HTMLDocument& document() const { return *m_document; } | 30 HTMLDocument& document() const { return *m_document; } |
| 30 | 31 |
| 31 HTMLFormElement* populateForm(const char* html) | 32 HTMLFormElement* populateForm(const char* enctype, const char* html) |
| 32 { | 33 { |
| 33 StringBuilder b; | 34 StringBuilder b; |
| 34 b.appendLiteral("<!DOCTYPE html><html><body><form id='theForm'>"); | 35 b.appendLiteral("<!DOCTYPE html><html><body><form id='theForm' enctype='
"); |
| 36 b.append(enctype); |
| 37 b.appendLiteral("'>"); |
| 35 b.append(html); | 38 b.append(html); |
| 36 b.appendLiteral("</form></body></html>"); | 39 b.appendLiteral("</form></body></html>"); |
| 37 document().documentElement()->setInnerHTML(b.toString(), ASSERT_NO_EXCEP
TION); | 40 document().documentElement()->setInnerHTML(b.toString(), ASSERT_NO_EXCEP
TION); |
| 38 document().view()->updateAllLifecyclePhases(); | 41 document().view()->updateAllLifecyclePhases(); |
| 39 HTMLFormElement* form = toHTMLFormElement(document().getElementById("the
Form")); | 42 HTMLFormElement* form = toHTMLFormElement(document().getElementById("the
Form")); |
| 40 EXPECT_NE(nullptr, form); | 43 EXPECT_NE(nullptr, form); |
| 41 return form; | 44 return form; |
| 42 } | 45 } |
| 43 | 46 |
| 44 private: | 47 private: |
| 45 OwnPtr<DummyPageHolder> m_dummyPageHolder; | 48 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 46 RefPtrWillBePersistent<HTMLDocument> m_document; | 49 RefPtrWillBePersistent<HTMLDocument> m_document; |
| 47 }; | 50 }; |
| 48 | 51 |
| 49 TEST_F(PasswordCredentialTest, CreateFromForm) | 52 TEST_F(PasswordCredentialTest, CreateFromMultipartForm) |
| 50 { | 53 { |
| 51 HTMLFormElement* form = populateForm( | 54 HTMLFormElement* form = populateForm("multipart/form-data", |
| 52 "<input type='text' name='theId' value='musterman' autocomplete='usernam
e'>" | 55 "<input type='text' name='theId' value='musterman' autocomplete='usernam
e'>" |
| 53 "<input type='text' name='thePassword' value='sekrit' autocomplete='curr
ent-password'>" | 56 "<input type='text' name='thePassword' value='sekrit' autocomplete='curr
ent-password'>" |
| 54 "<input type='text' name='theIcon' value='https://example.com/photo' aut
ocomplete='photo'>" | 57 "<input type='text' name='theIcon' value='https://example.com/photo' aut
ocomplete='photo'>" |
| 55 "<input type='text' name='theExtraField' value='extra'>" | 58 "<input type='text' name='theExtraField' value='extra'>" |
| 56 "<input type='text' name='theName' value='friendly name' autocomplete='n
ame'>"); | 59 "<input type='text' name='theName' value='friendly name' autocomplete='n
ame'>"); |
| 57 PasswordCredential* credential = PasswordCredential::create(form, ASSERT_NO_
EXCEPTION); | 60 PasswordCredential* credential = PasswordCredential::create(form, ASSERT_NO_
EXCEPTION); |
| 58 ASSERT_NE(nullptr, credential); | 61 ASSERT_NE(nullptr, credential); |
| 59 EXPECT_EQ("theId", credential->idName()); | 62 EXPECT_EQ("theId", credential->idName()); |
| 60 EXPECT_EQ("thePassword", credential->passwordName()); | 63 EXPECT_EQ("thePassword", credential->passwordName()); |
| 61 | 64 |
| 62 EXPECT_EQ("musterman", credential->id()); | 65 EXPECT_EQ("musterman", credential->id()); |
| 63 EXPECT_EQ("sekrit", credential->password()); | 66 EXPECT_EQ("sekrit", credential->password()); |
| 64 EXPECT_EQ(KURL(ParsedURLString, "https://example.com/photo"), credential->ic
onURL()); | 67 EXPECT_EQ(KURL(ParsedURLString, "https://example.com/photo"), credential->ic
onURL()); |
| 65 EXPECT_EQ("friendly name", credential->name()); | 68 EXPECT_EQ("friendly name", credential->name()); |
| 66 EXPECT_EQ("password", credential->type()); | 69 EXPECT_EQ("password", credential->type()); |
| 67 | 70 |
| 68 FormDataOrURLSearchParams additionalData; | 71 FormDataOrURLSearchParams additionalData; |
| 69 credential->additionalData(additionalData); | 72 credential->additionalData(additionalData); |
| 70 ASSERT_TRUE(additionalData.isFormData()); | 73 ASSERT_TRUE(additionalData.isFormData()); |
| 71 EXPECT_TRUE(additionalData.getAsFormData()->has("theId")); | 74 EXPECT_TRUE(additionalData.getAsFormData()->has("theId")); |
| 72 EXPECT_TRUE(additionalData.getAsFormData()->has("thePassword")); | 75 EXPECT_TRUE(additionalData.getAsFormData()->has("thePassword")); |
| 73 EXPECT_TRUE(additionalData.getAsFormData()->has("theIcon")); | 76 EXPECT_TRUE(additionalData.getAsFormData()->has("theIcon")); |
| 74 EXPECT_TRUE(additionalData.getAsFormData()->has("theName")); | 77 EXPECT_TRUE(additionalData.getAsFormData()->has("theName")); |
| 75 EXPECT_TRUE(additionalData.getAsFormData()->has("theExtraField")); | 78 EXPECT_TRUE(additionalData.getAsFormData()->has("theExtraField")); |
| 76 } | 79 } |
| 77 | 80 |
| 81 TEST_F(PasswordCredentialTest, CreateFromURLEncodedForm) |
| 82 { |
| 83 HTMLFormElement* form = populateForm("application/x-www-form-urlencoded", |
| 84 "<input type='text' name='theId' value='musterman' autocomplete='usernam
e'>" |
| 85 "<input type='text' name='thePassword' value='sekrit' autocomplete='curr
ent-password'>" |
| 86 "<input type='text' name='theIcon' value='https://example.com/photo' aut
ocomplete='photo'>" |
| 87 "<input type='text' name='theExtraField' value='extra'>" |
| 88 "<input type='text' name='theName' value='friendly name' autocomplete='n
ame'>"); |
| 89 PasswordCredential* credential = PasswordCredential::create(form, ASSERT_NO_
EXCEPTION); |
| 90 ASSERT_NE(nullptr, credential); |
| 91 EXPECT_EQ("theId", credential->idName()); |
| 92 EXPECT_EQ("thePassword", credential->passwordName()); |
| 93 |
| 94 EXPECT_EQ("musterman", credential->id()); |
| 95 EXPECT_EQ("sekrit", credential->password()); |
| 96 EXPECT_EQ(KURL(ParsedURLString, "https://example.com/photo"), credential->ic
onURL()); |
| 97 EXPECT_EQ("friendly name", credential->name()); |
| 98 EXPECT_EQ("password", credential->type()); |
| 99 |
| 100 FormDataOrURLSearchParams additionalData; |
| 101 credential->additionalData(additionalData); |
| 102 ASSERT_TRUE(additionalData.isURLSearchParams()); |
| 103 EXPECT_TRUE(additionalData.getAsURLSearchParams()->has("theId")); |
| 104 EXPECT_TRUE(additionalData.getAsURLSearchParams()->has("thePassword")); |
| 105 EXPECT_TRUE(additionalData.getAsURLSearchParams()->has("theIcon")); |
| 106 EXPECT_TRUE(additionalData.getAsURLSearchParams()->has("theName")); |
| 107 EXPECT_TRUE(additionalData.getAsURLSearchParams()->has("theExtraField")); |
| 108 } |
| 109 |
| 78 TEST_F(PasswordCredentialTest, CreateFromFormNoPassword) | 110 TEST_F(PasswordCredentialTest, CreateFromFormNoPassword) |
| 79 { | 111 { |
| 80 HTMLFormElement* form = populateForm( | 112 HTMLFormElement* form = populateForm("multipart/form-data", |
| 81 "<input type='text' name='theId' value='musterman' autocomplete='usernam
e'>" | 113 "<input type='text' name='theId' value='musterman' autocomplete='usernam
e'>" |
| 82 "<!-- No password field -->" | 114 "<!-- No password field -->" |
| 83 "<input type='text' name='theIcon' value='https://example.com/photo' aut
ocomplete='photo'>" | 115 "<input type='text' name='theIcon' value='https://example.com/photo' aut
ocomplete='photo'>" |
| 84 "<input type='text' name='theName' value='friendly name' autocomplete='n
ame'>"); | 116 "<input type='text' name='theName' value='friendly name' autocomplete='n
ame'>"); |
| 85 TrackExceptionState exceptionState; | 117 TrackExceptionState exceptionState; |
| 86 PasswordCredential* credential = PasswordCredential::create(form, exceptionS
tate); | 118 PasswordCredential* credential = PasswordCredential::create(form, exceptionS
tate); |
| 87 EXPECT_EQ(nullptr, credential); | 119 EXPECT_EQ(nullptr, credential); |
| 88 EXPECT_TRUE(exceptionState.hadException()); | 120 EXPECT_TRUE(exceptionState.hadException()); |
| 89 EXPECT_EQ(V8TypeError, exceptionState.code()); | 121 EXPECT_EQ(V8TypeError, exceptionState.code()); |
| 90 EXPECT_EQ("'password' must not be empty.", exceptionState.message()); | 122 EXPECT_EQ("'password' must not be empty.", exceptionState.message()); |
| 91 } | 123 } |
| 92 | 124 |
| 93 TEST_F(PasswordCredentialTest, CreateFromFormNoId) | 125 TEST_F(PasswordCredentialTest, CreateFromFormNoId) |
| 94 { | 126 { |
| 95 HTMLFormElement* form = populateForm( | 127 HTMLFormElement* form = populateForm("multipart/form-data", |
| 96 "<!-- No username field. -->" | 128 "<!-- No username field. -->" |
| 97 "<input type='text' name='thePassword' value='sekrit' autocomplete='curr
ent-password'>" | 129 "<input type='text' name='thePassword' value='sekrit' autocomplete='curr
ent-password'>" |
| 98 "<input type='text' name='theIcon' value='https://example.com/photo' aut
ocomplete='photo'>" | 130 "<input type='text' name='theIcon' value='https://example.com/photo' aut
ocomplete='photo'>" |
| 99 "<input type='text' name='theName' value='friendly name' autocomplete='n
ame'>"); | 131 "<input type='text' name='theName' value='friendly name' autocomplete='n
ame'>"); |
| 100 TrackExceptionState exceptionState; | 132 TrackExceptionState exceptionState; |
| 101 PasswordCredential* credential = PasswordCredential::create(form, exceptionS
tate); | 133 PasswordCredential* credential = PasswordCredential::create(form, exceptionS
tate); |
| 102 EXPECT_EQ(nullptr, credential); | 134 EXPECT_EQ(nullptr, credential); |
| 103 EXPECT_TRUE(exceptionState.hadException()); | 135 EXPECT_TRUE(exceptionState.hadException()); |
| 104 EXPECT_EQ(V8TypeError, exceptionState.code()); | 136 EXPECT_EQ(V8TypeError, exceptionState.code()); |
| 105 EXPECT_EQ("'id' must not be empty.", exceptionState.message()); | 137 EXPECT_EQ("'id' must not be empty.", exceptionState.message()); |
| 106 } | 138 } |
| 107 | 139 |
| 108 } // namespace blink | 140 } // namespace blink |
| OLD | NEW |