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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_server_field_info.h" |
10 #include "components/autofill/core/browser/autofill_xml_parser.h" | 11 #include "components/autofill/core/browser/autofill_xml_parser.h" |
11 #include "components/autofill/core/browser/field_types.h" | 12 #include "components/autofill/core/browser/field_types.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/webrtc/libjingle/xmllite/xmlparser.h" | |
14 | 14 |
15 namespace autofill { | 15 namespace autofill { |
16 namespace { | 16 namespace { |
17 | 17 |
18 class AutofillQueryXmlParserTest : public testing::Test { | 18 class AutofillQueryXmlParserTest : public testing::Test { |
19 public: | 19 public: |
20 AutofillQueryXmlParserTest(): upload_required_(USE_UPLOAD_RATES) {} | 20 AutofillQueryXmlParserTest(): upload_required_(USE_UPLOAD_RATES) {} |
21 ~AutofillQueryXmlParserTest() override{}; | 21 ~AutofillQueryXmlParserTest() override{}; |
22 | 22 |
23 protected: | 23 protected: |
24 void ParseQueryXML(const std::string& xml, bool should_succeed) { | 24 void ParseQueryXML(std::string xml, bool should_succeed) { |
25 // Create a parser. | 25 EXPECT_EQ(should_succeed, |
26 AutofillQueryXmlParser parse_handler(&field_infos_, | 26 ParseAutofillQueryXml(std::move(xml), &field_infos_, |
27 &upload_required_); | 27 &upload_required_)); |
28 buzz::XmlParser parser(&parse_handler); | |
29 parser.Parse(xml.c_str(), xml.length(), true); | |
30 EXPECT_EQ(should_succeed, parse_handler.succeeded()); | |
31 } | 28 } |
32 | 29 |
33 std::vector<AutofillServerFieldInfo> field_infos_; | 30 std::vector<AutofillServerFieldInfo> field_infos_; |
34 UploadRequired upload_required_; | 31 UploadRequired upload_required_; |
35 }; | 32 }; |
36 | 33 |
37 class AutofillUploadXmlParserTest : public testing::Test { | 34 class AutofillUploadXmlParserTest : public testing::Test { |
38 public: | 35 public: |
39 AutofillUploadXmlParserTest(): positive_(0), negative_(0) {} | 36 AutofillUploadXmlParserTest(): positive_(0), negative_(0) {} |
40 ~AutofillUploadXmlParserTest() override{}; | 37 ~AutofillUploadXmlParserTest() override{}; |
41 | 38 |
42 protected: | 39 protected: |
43 void ParseUploadXML(const std::string& xml, bool should_succeed) { | 40 void ParseUploadXML(std::string xml, bool should_succeed) { |
44 // Create a parser. | 41 EXPECT_EQ(should_succeed, |
45 AutofillUploadXmlParser parse_handler(&positive_, &negative_); | 42 ParseAutofillUploadXml(std::move(xml), &positive_, &negative_)); |
46 buzz::XmlParser parser(&parse_handler); | |
47 parser.Parse(xml.c_str(), xml.length(), true); | |
48 | |
49 EXPECT_EQ(should_succeed, parse_handler.succeeded()); | |
50 } | 43 } |
51 | 44 |
52 double positive_; | 45 double positive_; |
53 double negative_; | 46 double negative_; |
54 }; | 47 }; |
55 | 48 |
56 TEST_F(AutofillQueryXmlParserTest, BasicQuery) { | 49 TEST_F(AutofillQueryXmlParserTest, BasicQuery) { |
57 // An XML string representing a basic query response. | 50 // An XML string representing a basic query response. |
58 std::string xml = "<autofillqueryresponse>" | 51 std::string xml = "<autofillqueryresponse>" |
59 "<field autofilltype=\"0\" />" | 52 "<field autofilltype=\"0\" />" |
60 "<field autofilltype=\"1\" />" | 53 "<field autofilltype=\"1\" />" |
61 "<field autofilltype=\"3\" />" | 54 "<field autofilltype=\"3\" />" |
62 "<field autofilltype=\"2\" />" | 55 "<field autofilltype=\"2\" />" |
63 "<field autofilltype=\"61\" defaultvalue=\"default\"/>" | 56 "<field autofilltype=\"61\" defaultvalue=\"default\"/>" |
64 "</autofillqueryresponse>"; | 57 "</autofillqueryresponse>"; |
65 ParseQueryXML(xml, true); | 58 ParseQueryXML(std::move(xml), true); |
66 | 59 |
67 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | 60 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); |
68 ASSERT_EQ(5U, field_infos_.size()); | 61 ASSERT_EQ(5U, field_infos_.size()); |
69 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | 62 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); |
70 EXPECT_EQ(UNKNOWN_TYPE, field_infos_[1].field_type); | 63 EXPECT_EQ(UNKNOWN_TYPE, field_infos_[1].field_type); |
71 EXPECT_EQ(NAME_FIRST, field_infos_[2].field_type); | 64 EXPECT_EQ(NAME_FIRST, field_infos_[2].field_type); |
72 EXPECT_EQ(EMPTY_TYPE, field_infos_[3].field_type); | 65 EXPECT_EQ(EMPTY_TYPE, field_infos_[3].field_type); |
73 EXPECT_TRUE(field_infos_[3].default_value.empty()); | 66 EXPECT_TRUE(field_infos_[3].default_value.empty()); |
74 EXPECT_EQ(FIELD_WITH_DEFAULT_VALUE, field_infos_[4].field_type); | 67 EXPECT_EQ(FIELD_WITH_DEFAULT_VALUE, field_infos_[4].field_type); |
75 EXPECT_EQ("default", field_infos_[4].default_value); | 68 EXPECT_EQ("default", field_infos_[4].default_value); |
76 } | 69 } |
77 | 70 |
78 // Test parsing the upload required attribute. | 71 // Test parsing the upload required attribute. |
79 TEST_F(AutofillQueryXmlParserTest, TestUploadRequired) { | 72 TEST_F(AutofillQueryXmlParserTest, TestUploadRequired) { |
80 std::string xml = "<autofillqueryresponse uploadrequired=\"true\">" | 73 std::string xml = "<autofillqueryresponse uploadrequired=\"true\">" |
81 "<field autofilltype=\"0\" />" | 74 "<field autofilltype=\"0\" />" |
82 "</autofillqueryresponse>"; | 75 "</autofillqueryresponse>"; |
83 | 76 |
84 ParseQueryXML(xml, true); | 77 ParseQueryXML(std::move(xml), true); |
85 | 78 |
86 EXPECT_EQ(upload_required_, upload_required_); | 79 EXPECT_EQ(upload_required_, upload_required_); |
87 ASSERT_EQ(1U, field_infos_.size()); | 80 ASSERT_EQ(1U, field_infos_.size()); |
88 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | 81 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); |
89 | 82 |
90 field_infos_.clear(); | 83 field_infos_.clear(); |
91 xml = "<autofillqueryresponse uploadrequired=\"false\">" | 84 xml = "<autofillqueryresponse uploadrequired=\"false\">" |
92 "<field autofilltype=\"0\" />" | 85 "<field autofilltype=\"0\" />" |
93 "</autofillqueryresponse>"; | 86 "</autofillqueryresponse>"; |
94 | 87 |
95 ParseQueryXML(xml, true); | 88 ParseQueryXML(std::move(xml), true); |
96 | 89 |
97 EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required_); | 90 EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required_); |
98 ASSERT_EQ(1U, field_infos_.size()); | 91 ASSERT_EQ(1U, field_infos_.size()); |
99 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | 92 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); |
100 | 93 |
101 field_infos_.clear(); | 94 field_infos_.clear(); |
102 xml = "<autofillqueryresponse uploadrequired=\"bad_value\">" | 95 xml = "<autofillqueryresponse uploadrequired=\"bad_value\">" |
103 "<field autofilltype=\"0\" />" | 96 "<field autofilltype=\"0\" />" |
104 "</autofillqueryresponse>"; | 97 "</autofillqueryresponse>"; |
105 | 98 |
106 ParseQueryXML(xml, true); | 99 ParseQueryXML(std::move(xml), true); |
107 | 100 |
108 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | 101 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); |
109 ASSERT_EQ(1U, field_infos_.size()); | 102 ASSERT_EQ(1U, field_infos_.size()); |
110 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | 103 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); |
111 } | 104 } |
112 | 105 |
113 // Test badly formed XML queries. | 106 // Test badly formed XML queries. |
114 TEST_F(AutofillQueryXmlParserTest, ParseErrors) { | 107 TEST_F(AutofillQueryXmlParserTest, ParseErrors) { |
115 // Test no Autofill type. | 108 // Test no Autofill type. |
116 std::string xml = "<autofillqueryresponse>" | 109 std::string xml = "<autofillqueryresponse>" |
117 "<field/>" | 110 "<field/>" |
118 "</autofillqueryresponse>"; | 111 "</autofillqueryresponse>"; |
119 | 112 |
120 ParseQueryXML(xml, false); | 113 ParseQueryXML(std::move(xml), false); |
121 | |
122 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
123 EXPECT_EQ(0U, field_infos_.size()); | |
124 | 114 |
125 // Test an incorrect Autofill type. | 115 // Test an incorrect Autofill type. |
126 xml = "<autofillqueryresponse>" | 116 xml = "<autofillqueryresponse>" |
127 "<field autofilltype=\"-1\"/>" | 117 "<field autofilltype=\"-1\"/>" |
128 "</autofillqueryresponse>"; | 118 "</autofillqueryresponse>"; |
129 | 119 |
130 ParseQueryXML(xml, true); | 120 ParseQueryXML(std::move(xml), true); |
131 | 121 |
132 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | 122 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); |
133 ASSERT_EQ(1U, field_infos_.size()); | 123 ASSERT_EQ(1U, field_infos_.size()); |
134 // AutofillType was out of range and should be set to NO_SERVER_DATA. | 124 // AutofillType was out of range and should be set to NO_SERVER_DATA. |
135 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | 125 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); |
136 | 126 |
137 // Test upper bound for the field type, MAX_VALID_FIELD_TYPE. | 127 // Test upper bound for the field type, MAX_VALID_FIELD_TYPE. |
138 field_infos_.clear(); | 128 field_infos_.clear(); |
139 xml = "<autofillqueryresponse><field autofilltype=\"" + | 129 xml = "<autofillqueryresponse><field autofilltype=\"" + |
140 base::IntToString(MAX_VALID_FIELD_TYPE) + "\"/></autofillqueryresponse>"; | 130 base::IntToString(MAX_VALID_FIELD_TYPE) + "\"/></autofillqueryresponse>"; |
141 | 131 |
142 ParseQueryXML(xml, true); | 132 ParseQueryXML(std::move(xml), true); |
143 | 133 |
144 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | 134 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); |
145 ASSERT_EQ(1U, field_infos_.size()); | 135 ASSERT_EQ(1U, field_infos_.size()); |
146 // AutofillType was out of range and should be set to NO_SERVER_DATA. | 136 // AutofillType was out of range and should be set to NO_SERVER_DATA. |
147 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | 137 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); |
148 | 138 |
149 // Test an incorrect Autofill type. | 139 // Test an incorrect Autofill type. |
150 field_infos_.clear(); | 140 field_infos_.clear(); |
151 xml = "<autofillqueryresponse>" | 141 xml = "<autofillqueryresponse>" |
152 "<field autofilltype=\"No Type\"/>" | 142 "<field autofilltype=\"No Type\"/>" |
153 "</autofillqueryresponse>"; | 143 "</autofillqueryresponse>"; |
154 | 144 |
155 // Parse fails but an entry is still added to field_infos_. | 145 // Unknown autofill type is handled gracefully. |
156 ParseQueryXML(xml, false); | 146 ParseQueryXML(std::move(xml), true); |
157 | 147 |
158 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | 148 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); |
159 ASSERT_EQ(1U, field_infos_.size()); | 149 ASSERT_EQ(1U, field_infos_.size()); |
160 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | 150 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); |
161 } | 151 } |
162 | 152 |
163 // Test successful upload response. | 153 // Test successful upload response. |
164 TEST_F(AutofillUploadXmlParserTest, TestSuccessfulResponse) { | 154 TEST_F(AutofillUploadXmlParserTest, TestSuccessfulResponse) { |
165 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" " | 155 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" " |
166 "negativeuploadrate=\"0.3\"/>", | 156 "negativeuploadrate=\"0.3\"/>", |
167 true); | 157 true); |
168 | 158 |
169 EXPECT_DOUBLE_EQ(0.5, positive_); | 159 EXPECT_DOUBLE_EQ(0.5, positive_); |
170 EXPECT_DOUBLE_EQ(0.3, negative_); | 160 EXPECT_DOUBLE_EQ(0.3, negative_); |
171 } | 161 } |
172 | 162 |
173 // Test failed upload response. | 163 // Test failed upload response. |
174 TEST_F(AutofillUploadXmlParserTest, TestFailedResponse) { | 164 TEST_F(AutofillUploadXmlParserTest, TestFailedResponse) { |
175 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"\" " | 165 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"\" " |
176 "negativeuploadrate=\"0.3\"/>", | 166 "negativeuploadrate=\"0.3\"/>", |
177 false); | 167 false); |
178 | 168 |
179 EXPECT_DOUBLE_EQ(0, positive_); | |
180 EXPECT_DOUBLE_EQ(0.3, negative_); // Partially parsed. | |
181 negative_ = 0; | |
182 | |
183 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" " | 169 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" " |
184 "negativeuploadrate=\"0.3\"", | 170 "negativeuploadrate=\"0.3\"", |
185 false); | 171 false); |
186 | 172 |
187 EXPECT_DOUBLE_EQ(0, positive_); | |
188 EXPECT_DOUBLE_EQ(0, negative_); | |
189 | |
190 ParseUploadXML("bad data", false); | 173 ParseUploadXML("bad data", false); |
191 | 174 |
192 EXPECT_DOUBLE_EQ(0, positive_); | |
193 EXPECT_DOUBLE_EQ(0, negative_); | |
194 | |
195 ParseUploadXML(std::string(), false); | 175 ParseUploadXML(std::string(), false); |
196 | |
197 EXPECT_DOUBLE_EQ(0, positive_); | |
198 EXPECT_DOUBLE_EQ(0, negative_); | |
199 } | 176 } |
200 | 177 |
201 } // namespace | 178 } // namespace |
202 } // namespace autofill | 179 } // namespace autofill |
OLD | NEW |