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
,
20 base::Pickle
* pickle
) {
21 DCHECK_EQ(true, form_data
.is_form_tag
);
23 pickle
->WriteString16(form_data
.name
);
24 base::string16
method(base::ASCIIToUTF16("POST"));
25 pickle
->WriteString16(method
);
26 pickle
->WriteString(form_data
.origin
.spec());
27 pickle
->WriteString(form_data
.action
.spec());
28 pickle
->WriteBool(form_data
.user_submitted
);
29 pickle
->WriteInt(static_cast<int>(form_data
.fields
.size()));
30 for (size_t i
= 0; i
< form_data
.fields
.size(); ++i
) {
31 SerializeFormFieldData(form_data
.fields
[i
], pickle
);
35 void SerializeInVersion2Format(const FormData
& form_data
,
36 base::Pickle
* pickle
) {
37 DCHECK_EQ(true, form_data
.is_form_tag
);
39 pickle
->WriteString16(form_data
.name
);
40 pickle
->WriteString(form_data
.origin
.spec());
41 pickle
->WriteString(form_data
.action
.spec());
42 pickle
->WriteBool(form_data
.user_submitted
);
43 pickle
->WriteInt(static_cast<int>(form_data
.fields
.size()));
44 for (size_t i
= 0; i
< form_data
.fields
.size(); ++i
) {
45 SerializeFormFieldData(form_data
.fields
[i
], pickle
);
49 // This function serializes the form data into the pickle in incorrect format
50 // (no version number).
51 void SerializeIncorrectFormat(const FormData
& form_data
, base::Pickle
* pickle
) {
52 pickle
->WriteString16(form_data
.name
);
53 pickle
->WriteString(form_data
.origin
.spec());
54 pickle
->WriteString(form_data
.action
.spec());
55 pickle
->WriteBool(form_data
.user_submitted
);
56 pickle
->WriteInt(static_cast<int>(form_data
.fields
.size()));
57 for (size_t i
= 0; i
< form_data
.fields
.size(); ++i
) {
58 SerializeFormFieldData(form_data
.fields
[i
], pickle
);
62 void FillInDummyFormData(FormData
* data
) {
63 data
->name
= base::ASCIIToUTF16("name");
64 data
->origin
= GURL("origin");
65 data
->action
= GURL("action");
66 data
->user_submitted
= true;
68 FormFieldData field_data
;
69 field_data
.label
= base::ASCIIToUTF16("label");
70 field_data
.name
= base::ASCIIToUTF16("name");
71 field_data
.value
= base::ASCIIToUTF16("value");
72 field_data
.form_control_type
= "password";
73 field_data
.autocomplete_attribute
= "off";
74 field_data
.max_length
= 200;
75 field_data
.is_autofilled
= true;
76 field_data
.is_checked
= true;
77 field_data
.is_checkable
= true;
78 field_data
.is_focusable
= true;
79 field_data
.should_autocomplete
= false;
80 field_data
.text_direction
= base::i18n::RIGHT_TO_LEFT
;
81 field_data
.option_values
.push_back(base::ASCIIToUTF16("First"));
82 field_data
.option_values
.push_back(base::ASCIIToUTF16("Second"));
83 field_data
.option_contents
.push_back(base::ASCIIToUTF16("First"));
84 field_data
.option_contents
.push_back(base::ASCIIToUTF16("Second"));
86 data
->fields
.push_back(field_data
);
88 // Change a few fields.
89 field_data
.max_length
= 150;
90 field_data
.option_values
.push_back(base::ASCIIToUTF16("Third"));
91 field_data
.option_contents
.push_back(base::ASCIIToUTF16("Third"));
92 data
->fields
.push_back(field_data
);
97 TEST(FormDataTest
, SerializeAndDeserialize
) {
99 FillInDummyFormData(&data
);
100 data
.is_form_tag
= false;
103 SerializeFormData(data
, &pickle
);
105 base::PickleIterator
iter(pickle
);
107 EXPECT_TRUE(DeserializeFormData(&iter
, &actual
));
109 EXPECT_TRUE(actual
.SameFormAs(data
));
112 TEST(FormDataTest
, Serialize_v1_Deserialize_vCurrent
) {
114 FillInDummyFormData(&data
);
117 SerializeInVersion1Format(data
, &pickle
);
119 base::PickleIterator
iter(pickle
);
121 EXPECT_TRUE(DeserializeFormData(&iter
, &actual
));
123 EXPECT_TRUE(actual
.SameFormAs(data
));
126 TEST(FormDataTest
, Serialize_v2_Deserialize_vCurrent
) {
128 FillInDummyFormData(&data
);
131 SerializeInVersion2Format(data
, &pickle
);
133 base::PickleIterator
iter(pickle
);
135 EXPECT_TRUE(DeserializeFormData(&iter
, &actual
));
137 EXPECT_TRUE(actual
.SameFormAs(data
));
140 TEST(FormDataTest
, SerializeIncorrectFormatAndDeserialize
) {
142 FillInDummyFormData(&data
);
145 SerializeIncorrectFormat(data
, &pickle
);
147 base::PickleIterator
iter(pickle
);
149 EXPECT_FALSE(DeserializeFormData(&iter
, &actual
));
152 EXPECT_TRUE(actual
.SameFormAs(empty
));
155 TEST(FormDataTest
, SerializeAndDeserializeInStrings
) {
157 FillInDummyFormData(&data
);
158 data
.is_form_tag
= false;
160 std::string serialized_data
;
161 SerializeFormDataToBase64String(data
, &serialized_data
);
164 EXPECT_TRUE(DeserializeFormDataFromBase64String(serialized_data
, &actual
));
166 EXPECT_TRUE(actual
.SameFormAs(data
));
169 } // namespace autofill