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