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.
5 #include "components/autofill/core/common/form_data.h"
7 #include "base/pickle.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/autofill/core/common/form_field_data.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 // This function serializes the form data into the pickle in version one format.
17 // It should always be possible to deserialize it using DeserializeFormData(),
18 // even when version changes. See kPickleVersion in form_data.cc.
19 void SerializeInVersion1Format(const FormData
& form_data
, Pickle
* pickle
) {
20 DCHECK_EQ(true, form_data
.is_form_tag
);
22 pickle
->WriteString16(form_data
.name
);
23 base::string16
method(base::ASCIIToUTF16("POST"));
24 pickle
->WriteString16(method
);
25 pickle
->WriteString(form_data
.origin
.spec());
26 pickle
->WriteString(form_data
.action
.spec());
27 pickle
->WriteBool(form_data
.user_submitted
);
28 pickle
->WriteInt(static_cast<int>(form_data
.fields
.size()));
29 for (size_t i
= 0; i
< form_data
.fields
.size(); ++i
) {
30 SerializeFormFieldData(form_data
.fields
[i
], pickle
);
34 void SerializeInVersion2Format(const FormData
& form_data
, Pickle
* pickle
) {
35 DCHECK_EQ(true, form_data
.is_form_tag
);
37 pickle
->WriteString16(form_data
.name
);
38 pickle
->WriteString(form_data
.origin
.spec());
39 pickle
->WriteString(form_data
.action
.spec());
40 pickle
->WriteBool(form_data
.user_submitted
);
41 pickle
->WriteInt(static_cast<int>(form_data
.fields
.size()));
42 for (size_t i
= 0; i
< form_data
.fields
.size(); ++i
) {
43 SerializeFormFieldData(form_data
.fields
[i
], pickle
);
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
);
60 void FillInDummyFormData(FormData
* data
) {
61 data
->name
= base::ASCIIToUTF16("name");
62 data
->origin
= GURL("origin");
63 data
->action
= GURL("action");
64 data
->user_submitted
= true;
66 FormFieldData field_data
;
67 field_data
.label
= base::ASCIIToUTF16("label");
68 field_data
.name
= base::ASCIIToUTF16("name");
69 field_data
.value
= base::ASCIIToUTF16("value");
70 field_data
.form_control_type
= "password";
71 field_data
.autocomplete_attribute
= "off";
72 field_data
.max_length
= 200;
73 field_data
.is_autofilled
= true;
74 field_data
.is_checked
= true;
75 field_data
.is_checkable
= true;
76 field_data
.is_focusable
= true;
77 field_data
.should_autocomplete
= false;
78 field_data
.text_direction
= base::i18n::RIGHT_TO_LEFT
;
79 field_data
.option_values
.push_back(base::ASCIIToUTF16("First"));
80 field_data
.option_values
.push_back(base::ASCIIToUTF16("Second"));
81 field_data
.option_contents
.push_back(base::ASCIIToUTF16("First"));
82 field_data
.option_contents
.push_back(base::ASCIIToUTF16("Second"));
84 data
->fields
.push_back(field_data
);
86 // Change a few fields.
87 field_data
.max_length
= 150;
88 field_data
.option_values
.push_back(base::ASCIIToUTF16("Third"));
89 field_data
.option_contents
.push_back(base::ASCIIToUTF16("Third"));
90 data
->fields
.push_back(field_data
);
95 TEST(FormDataTest
, SerializeAndDeserialize
) {
97 FillInDummyFormData(&data
);
98 data
.is_form_tag
= false;
101 SerializeFormData(data
, &pickle
);
103 PickleIterator
iter(pickle
);
105 EXPECT_TRUE(DeserializeFormData(&iter
, &actual
));
107 EXPECT_TRUE(actual
.SameFormAs(data
));
110 TEST(FormDataTest
, Serialize_v1_Deserialize_vCurrent
) {
112 FillInDummyFormData(&data
);
115 SerializeInVersion1Format(data
, &pickle
);
117 PickleIterator
iter(pickle
);
119 EXPECT_TRUE(DeserializeFormData(&iter
, &actual
));
121 EXPECT_TRUE(actual
.SameFormAs(data
));
124 TEST(FormDataTest
, Serialize_v2_Deserialize_vCurrent
) {
126 FillInDummyFormData(&data
);
129 SerializeInVersion2Format(data
, &pickle
);
131 PickleIterator
iter(pickle
);
133 EXPECT_TRUE(DeserializeFormData(&iter
, &actual
));
135 EXPECT_TRUE(actual
.SameFormAs(data
));
138 TEST(FormDataTest
, SerializeIncorrectFormatAndDeserialize
) {
140 FillInDummyFormData(&data
);
143 SerializeIncorrectFormat(data
, &pickle
);
145 PickleIterator
iter(pickle
);
147 EXPECT_FALSE(DeserializeFormData(&iter
, &actual
));
150 TEST(FormDataTest
, SerializeAndDeserializeInStrings
) {
152 FillInDummyFormData(&data
);
153 data
.is_form_tag
= false;
155 std::string serialized_data
;
156 SerializeFormDataToBase64String(data
, &serialized_data
);
159 EXPECT_TRUE(DeserializeFormDataFromBase64String(serialized_data
, &actual
));
161 EXPECT_TRUE(actual
.SameFormAs(data
));
164 } // namespace autofill