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 "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
| 12 namespace { |
| 13 |
| 14 // This function serializes the form data into the pickle in version one format. |
| 15 // It should always be possible to deserialize it using DeserializeFormData(), |
| 16 // even when version changes. See kPickleVersion in form_data.cc. |
| 17 void SerializeInVersion1Format(const autofill::FormData& form_data, |
| 18 Pickle* pickle) { |
| 19 pickle->WriteInt(1); |
| 20 pickle->WriteString16(form_data.name); |
| 21 base::string16 method(base::ASCIIToUTF16("POST")); |
| 22 pickle->WriteString16(method); |
| 23 pickle->WriteString(form_data.origin.spec()); |
| 24 pickle->WriteString(form_data.action.spec()); |
| 25 pickle->WriteBool(form_data.user_submitted); |
| 26 pickle->WriteInt(static_cast<int>(form_data.fields.size())); |
| 27 for (size_t i = 0; i < form_data.fields.size(); ++i) { |
| 28 SerializeFormFieldData(form_data.fields[i], pickle); |
| 29 } |
| 30 } |
| 31 |
| 32 // This function serializes the form data into the pickle in incorrect format |
| 33 // (no version number). |
| 34 void SerializeIncorrectFormat(const autofill::FormData& form_data, |
| 35 Pickle* pickle) { |
| 36 pickle->WriteString16(form_data.name); |
| 37 pickle->WriteString(form_data.origin.spec()); |
| 38 pickle->WriteString(form_data.action.spec()); |
| 39 pickle->WriteBool(form_data.user_submitted); |
| 40 pickle->WriteInt(static_cast<int>(form_data.fields.size())); |
| 41 for (size_t i = 0; i < form_data.fields.size(); ++i) { |
| 42 SerializeFormFieldData(form_data.fields[i], pickle); |
| 43 } |
| 44 } |
| 45 |
| 46 } |
| 47 |
11 namespace autofill { | 48 namespace autofill { |
12 | 49 |
13 TEST(FormDataTest, SerializeAndDeserialize) { | 50 TEST(FormDataTest, SerializeAndDeserialize) { |
14 FormData data; | 51 FormData data; |
15 data.name = base::ASCIIToUTF16("name"); | 52 data.name = base::ASCIIToUTF16("name"); |
16 data.origin = GURL("origin"); | 53 data.origin = GURL("origin"); |
17 data.action = GURL("action"); | 54 data.action = GURL("action"); |
18 data.user_submitted = true; | 55 data.user_submitted = true; |
19 | 56 |
20 FormFieldData field_data; | 57 FormFieldData field_data; |
(...skipping 25 matching lines...) Expand all Loading... |
46 Pickle pickle; | 83 Pickle pickle; |
47 SerializeFormData(data, &pickle); | 84 SerializeFormData(data, &pickle); |
48 | 85 |
49 PickleIterator iter(pickle); | 86 PickleIterator iter(pickle); |
50 FormData actual; | 87 FormData actual; |
51 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); | 88 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); |
52 | 89 |
53 EXPECT_EQ(actual, data); | 90 EXPECT_EQ(actual, data); |
54 } | 91 } |
55 | 92 |
| 93 TEST(FormDataTest, Serialize_v1_Deserialize_vCurrent) { |
| 94 FormData data; |
| 95 data.name = base::ASCIIToUTF16("name"); |
| 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 |
| 126 Pickle pickle; |
| 127 SerializeInVersion1Format(data, &pickle); |
| 128 |
| 129 PickleIterator iter(pickle); |
| 130 FormData actual; |
| 131 EXPECT_TRUE(DeserializeFormData(&iter, &actual)); |
| 132 |
| 133 EXPECT_EQ(actual, data); |
| 134 } |
| 135 |
| 136 TEST(FormDataTest, SerializeIncorrectFormatAndDeserialize) { |
| 137 FormData data; |
| 138 data.name = base::ASCIIToUTF16("name"); |
| 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 |
| 169 Pickle pickle; |
| 170 SerializeIncorrectFormat(data, &pickle); |
| 171 |
| 172 PickleIterator iter(pickle); |
| 173 FormData actual; |
| 174 EXPECT_FALSE(DeserializeFormData(&iter, &actual)); |
| 175 } |
| 176 |
56 } // namespace autofill | 177 } // namespace autofill |
OLD | NEW |