| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "components/autofill/core/common/form_data.h" | 5 #include "components/autofill/core/common/form_data.h" |
| 6 | 6 |
| 7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/autofill/core/common/form_field_data.h" | 9 #include "components/autofill/core/common/form_field_data.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace autofill { |
| 13 |
| 12 namespace { | 14 namespace { |
| 13 | 15 |
| 14 // This function serializes the form data into the pickle in version one format. | 16 // This function serializes the form data into the pickle in version one format. |
| 15 // It should always be possible to deserialize it using DeserializeFormData(), | 17 // It should always be possible to deserialize it using DeserializeFormData(), |
| 16 // even when version changes. See kPickleVersion in form_data.cc. | 18 // even when version changes. See kPickleVersion in form_data.cc. |
| 17 void SerializeInVersion1Format(const autofill::FormData& form_data, | 19 void SerializeInVersion1Format(const FormData& form_data, Pickle* pickle) { |
| 18 Pickle* pickle) { | 20 DCHECK_EQ(true, form_data.is_in_form_tag); |
| 19 pickle->WriteInt(1); | 21 pickle->WriteInt(1); |
| 20 pickle->WriteString16(form_data.name); | 22 pickle->WriteString16(form_data.name); |
| 21 base::string16 method(base::ASCIIToUTF16("POST")); | 23 base::string16 method(base::ASCIIToUTF16("POST")); |
| 22 pickle->WriteString16(method); | 24 pickle->WriteString16(method); |
| 23 pickle->WriteString(form_data.origin.spec()); | 25 pickle->WriteString(form_data.origin.spec()); |
| 24 pickle->WriteString(form_data.action.spec()); | 26 pickle->WriteString(form_data.action.spec()); |
| 25 pickle->WriteBool(form_data.user_submitted); | 27 pickle->WriteBool(form_data.user_submitted); |
| 26 pickle->WriteInt(static_cast<int>(form_data.fields.size())); | 28 pickle->WriteInt(static_cast<int>(form_data.fields.size())); |
| 27 for (size_t i = 0; i < form_data.fields.size(); ++i) { | 29 for (size_t i = 0; i < form_data.fields.size(); ++i) { |
| 28 SerializeFormFieldData(form_data.fields[i], pickle); | 30 SerializeFormFieldData(form_data.fields[i], pickle); |
| 29 } | 31 } |
| 30 } | 32 } |
| 31 | 33 |
| 32 // This function serializes the form data into the pickle in incorrect format | 34 void SerializeInVersion2Format(const FormData& form_data, Pickle* pickle) { |
| 33 // (no version number). | 35 DCHECK_EQ(true, form_data.is_in_form_tag); |
| 34 void SerializeIncorrectFormat(const autofill::FormData& form_data, | 36 pickle->WriteInt(2); |
| 35 Pickle* pickle) { | |
| 36 pickle->WriteString16(form_data.name); | 37 pickle->WriteString16(form_data.name); |
| 37 pickle->WriteString(form_data.origin.spec()); | 38 pickle->WriteString(form_data.origin.spec()); |
| 38 pickle->WriteString(form_data.action.spec()); | 39 pickle->WriteString(form_data.action.spec()); |
| 39 pickle->WriteBool(form_data.user_submitted); | 40 pickle->WriteBool(form_data.user_submitted); |
| 40 pickle->WriteInt(static_cast<int>(form_data.fields.size())); | 41 pickle->WriteInt(static_cast<int>(form_data.fields.size())); |
| 41 for (size_t i = 0; i < form_data.fields.size(); ++i) { | 42 for (size_t i = 0; i < form_data.fields.size(); ++i) { |
| 42 SerializeFormFieldData(form_data.fields[i], pickle); | 43 SerializeFormFieldData(form_data.fields[i], pickle); |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 47 // This function serializes the form data into the pickle in incorrect format |
| 48 // (no version number). |
| 49 void SerializeIncorrectFormat(const FormData& form_data, Pickle* pickle) { |
| 50 pickle->WriteString16(form_data.name); |
| 51 pickle->WriteString(form_data.origin.spec()); |
| 52 pickle->WriteString(form_data.action.spec()); |
| 53 pickle->WriteBool(form_data.user_submitted); |
| 54 pickle->WriteInt(static_cast<int>(form_data.fields.size())); |
| 55 for (size_t i = 0; i < form_data.fields.size(); ++i) { |
| 56 SerializeFormFieldData(form_data.fields[i], pickle); |
| 57 } |
| 46 } | 58 } |
| 47 | 59 |
| 48 namespace autofill { | 60 void FillInDummyFormData(FormData* data) { |
| 49 | 61 data->name = base::ASCIIToUTF16("name"); |
| 50 TEST(FormDataTest, SerializeAndDeserialize) { | 62 data->origin = GURL("origin"); |
| 51 FormData data; | 63 data->action = GURL("action"); |
| 52 data.name = base::ASCIIToUTF16("name"); | 64 data->user_submitted = true; |
| 53 data.origin = GURL("origin"); | |
| 54 data.action = GURL("action"); | |
| 55 data.user_submitted = true; | |
| 56 | 65 |
| 57 FormFieldData field_data; | 66 FormFieldData field_data; |
| 58 field_data.label = base::ASCIIToUTF16("label"); | 67 field_data.label = base::ASCIIToUTF16("label"); |
| 59 field_data.name = base::ASCIIToUTF16("name"); | 68 field_data.name = base::ASCIIToUTF16("name"); |
| 60 field_data.value = base::ASCIIToUTF16("value"); | 69 field_data.value = base::ASCIIToUTF16("value"); |
| 61 field_data.form_control_type = "password"; | 70 field_data.form_control_type = "password"; |
| 62 field_data.autocomplete_attribute = "off"; | 71 field_data.autocomplete_attribute = "off"; |
| 63 field_data.max_length = 200; | 72 field_data.max_length = 200; |
| 64 field_data.is_autofilled = true; | 73 field_data.is_autofilled = true; |
| 65 field_data.is_checked = true; | 74 field_data.is_checked = true; |
| 66 field_data.is_checkable = true; | 75 field_data.is_checkable = true; |
| 67 field_data.is_focusable = true; | 76 field_data.is_focusable = true; |
| 68 field_data.should_autocomplete = false; | 77 field_data.should_autocomplete = false; |
| 69 field_data.text_direction = base::i18n::RIGHT_TO_LEFT; | 78 field_data.text_direction = base::i18n::RIGHT_TO_LEFT; |
| 70 field_data.option_values.push_back(base::ASCIIToUTF16("First")); | 79 field_data.option_values.push_back(base::ASCIIToUTF16("First")); |
| 71 field_data.option_values.push_back(base::ASCIIToUTF16("Second")); | 80 field_data.option_values.push_back(base::ASCIIToUTF16("Second")); |
| 72 field_data.option_contents.push_back(base::ASCIIToUTF16("First")); | 81 field_data.option_contents.push_back(base::ASCIIToUTF16("First")); |
| 73 field_data.option_contents.push_back(base::ASCIIToUTF16("Second")); | 82 field_data.option_contents.push_back(base::ASCIIToUTF16("Second")); |
| 74 | 83 |
| 75 data.fields.push_back(field_data); | 84 data->fields.push_back(field_data); |
| 76 | 85 |
| 77 // Change a few fields. | 86 // Change a few fields. |
| 78 field_data.max_length = 150; | 87 field_data.max_length = 150; |
| 79 field_data.option_values.push_back(base::ASCIIToUTF16("Third")); | 88 field_data.option_values.push_back(base::ASCIIToUTF16("Third")); |
| 80 field_data.option_contents.push_back(base::ASCIIToUTF16("Third")); | 89 field_data.option_contents.push_back(base::ASCIIToUTF16("Third")); |
| 81 data.fields.push_back(field_data); | 90 data->fields.push_back(field_data); |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 TEST(FormDataTest, SerializeAndDeserialize) { |
| 96 FormData data; |
| 97 FillInDummyFormData(&data); |
| 98 data.is_in_form_tag = false; |
| 82 | 99 |
| 83 Pickle pickle; | 100 Pickle pickle; |
| 84 SerializeFormData(data, &pickle); | 101 SerializeFormData(data, &pickle); |
| 85 | 102 |
| 86 PickleIterator iter(pickle); | 103 PickleIterator iter(pickle); |
| 87 FormData actual; | 104 FormData actual; |
| 88 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); | 105 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); |
| 89 | 106 |
| 90 EXPECT_TRUE(actual.SameFormAs(data)); | 107 EXPECT_TRUE(actual.SameFormAs(data)); |
| 91 } | 108 } |
| 92 | 109 |
| 93 TEST(FormDataTest, Serialize_v1_Deserialize_vCurrent) { | 110 TEST(FormDataTest, Serialize_v1_Deserialize_vCurrent) { |
| 94 FormData data; | 111 FormData data; |
| 95 data.name = base::ASCIIToUTF16("name"); | 112 FillInDummyFormData(&data); |
| 96 data.origin = GURL("origin"); | |
| 97 data.action = GURL("action"); | |
| 98 data.user_submitted = true; | |
| 99 | |
| 100 FormFieldData field_data; | |
| 101 field_data.label = base::ASCIIToUTF16("label"); | |
| 102 field_data.name = base::ASCIIToUTF16("name"); | |
| 103 field_data.value = base::ASCIIToUTF16("value"); | |
| 104 field_data.form_control_type = "password"; | |
| 105 field_data.autocomplete_attribute = "off"; | |
| 106 field_data.max_length = 200; | |
| 107 field_data.is_autofilled = true; | |
| 108 field_data.is_checked = true; | |
| 109 field_data.is_checkable = true; | |
| 110 field_data.is_focusable = true; | |
| 111 field_data.should_autocomplete = false; | |
| 112 field_data.text_direction = base::i18n::RIGHT_TO_LEFT; | |
| 113 field_data.option_values.push_back(base::ASCIIToUTF16("First")); | |
| 114 field_data.option_values.push_back(base::ASCIIToUTF16("Second")); | |
| 115 field_data.option_contents.push_back(base::ASCIIToUTF16("First")); | |
| 116 field_data.option_contents.push_back(base::ASCIIToUTF16("Second")); | |
| 117 | |
| 118 data.fields.push_back(field_data); | |
| 119 | |
| 120 // Change a few fields. | |
| 121 field_data.max_length = 150; | |
| 122 field_data.option_values.push_back(base::ASCIIToUTF16("Third")); | |
| 123 field_data.option_contents.push_back(base::ASCIIToUTF16("Third")); | |
| 124 data.fields.push_back(field_data); | |
| 125 | 113 |
| 126 Pickle pickle; | 114 Pickle pickle; |
| 127 SerializeInVersion1Format(data, &pickle); | 115 SerializeInVersion1Format(data, &pickle); |
| 128 | 116 |
| 129 PickleIterator iter(pickle); | 117 PickleIterator iter(pickle); |
| 130 FormData actual; | 118 FormData actual; |
| 131 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); | 119 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); |
| 132 | 120 |
| 133 EXPECT_TRUE(actual.SameFormAs(data)); | 121 EXPECT_TRUE(actual.SameFormAs(data)); |
| 134 } | 122 } |
| 135 | 123 |
| 124 TEST(FormDataTest, Serialize_v2_Deserialize_vCurrent) { |
| 125 FormData data; |
| 126 FillInDummyFormData(&data); |
| 127 |
| 128 Pickle pickle; |
| 129 SerializeInVersion2Format(data, &pickle); |
| 130 |
| 131 PickleIterator iter(pickle); |
| 132 FormData actual; |
| 133 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); |
| 134 |
| 135 EXPECT_TRUE(actual.SameFormAs(data)); |
| 136 } |
| 137 |
| 136 TEST(FormDataTest, SerializeIncorrectFormatAndDeserialize) { | 138 TEST(FormDataTest, SerializeIncorrectFormatAndDeserialize) { |
| 137 FormData data; | 139 FormData data; |
| 138 data.name = base::ASCIIToUTF16("name"); | 140 FillInDummyFormData(&data); |
| 139 data.origin = GURL("origin"); | |
| 140 data.action = GURL("action"); | |
| 141 data.user_submitted = true; | |
| 142 | |
| 143 FormFieldData field_data; | |
| 144 field_data.label = base::ASCIIToUTF16("label"); | |
| 145 field_data.name = base::ASCIIToUTF16("name"); | |
| 146 field_data.value = base::ASCIIToUTF16("value"); | |
| 147 field_data.form_control_type = "password"; | |
| 148 field_data.autocomplete_attribute = "off"; | |
| 149 field_data.max_length = 200; | |
| 150 field_data.is_autofilled = true; | |
| 151 field_data.is_checked = true; | |
| 152 field_data.is_checkable = true; | |
| 153 field_data.is_focusable = true; | |
| 154 field_data.should_autocomplete = false; | |
| 155 field_data.text_direction = base::i18n::RIGHT_TO_LEFT; | |
| 156 field_data.option_values.push_back(base::ASCIIToUTF16("First")); | |
| 157 field_data.option_values.push_back(base::ASCIIToUTF16("Second")); | |
| 158 field_data.option_contents.push_back(base::ASCIIToUTF16("First")); | |
| 159 field_data.option_contents.push_back(base::ASCIIToUTF16("Second")); | |
| 160 | |
| 161 data.fields.push_back(field_data); | |
| 162 | |
| 163 // Change a few fields. | |
| 164 field_data.max_length = 150; | |
| 165 field_data.option_values.push_back(base::ASCIIToUTF16("Third")); | |
| 166 field_data.option_contents.push_back(base::ASCIIToUTF16("Third")); | |
| 167 data.fields.push_back(field_data); | |
| 168 | 141 |
| 169 Pickle pickle; | 142 Pickle pickle; |
| 170 SerializeIncorrectFormat(data, &pickle); | 143 SerializeIncorrectFormat(data, &pickle); |
| 171 | 144 |
| 172 PickleIterator iter(pickle); | 145 PickleIterator iter(pickle); |
| 173 FormData actual; | 146 FormData actual; |
| 174 EXPECT_FALSE(DeserializeFormData(&iter, &actual)); | 147 EXPECT_FALSE(DeserializeFormData(&iter, &actual)); |
| 175 } | 148 } |
| 176 | 149 |
| 177 } // namespace autofill | 150 } // namespace autofill |
| OLD | NEW |