Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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. | |
| 4 #include "chrome/utility/media_router/dial_device_description_parser.h" | |
| 5 | |
| 6 #include <string> | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "chrome/common/media_router/dial_device_description.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 constexpr char kDeviceDescriptionWithService[] = | |
| 17 "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" | |
| 18 "<specVersion>\n" | |
| 19 "<major>1</major>\n" | |
| 20 "<minor>0</minor>\n" | |
| 21 "</specVersion>\n" | |
| 22 "<URLBase>http://172.31.71.84:8008</URLBase>\n" | |
| 23 "<device>\n" | |
| 24 "<deviceType>urn:dial-multiscreen-org:device:dial:1</deviceType>\n" | |
| 25 "<friendlyName>eureka9019</friendlyName>\n" | |
| 26 "<manufacturer>Google Inc.</manufacturer>\n" | |
| 27 "<modelName>Eureka Dongle</modelName>\n" | |
| 28 "<serialNumber>123456789000</serialNumber>\n" | |
| 29 "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e</UDN>\n" | |
| 30 "<serviceList>\n" | |
| 31 "<service>\n" | |
| 32 "<serviceType>urn:dial-multiscreen-org:service:dial:1</serviceType>\n" | |
| 33 "<serviceId>urn:dial-multiscreen-org:serviceId:dial</serviceId>\n" | |
| 34 "<controlURL>/ssdp/notfound</controlURL>\n" | |
| 35 "<eventSubURL>/ssdp/notfound</eventSubURL>\n" | |
| 36 "<SCPDURL>/ssdp/notfound</SCPDURL>\n" | |
| 37 "<servicedata xmlns=\"uri://cloudview.google.com/...\">\n" | |
| 38 "</servicedata>\n" | |
| 39 "</service>\n" | |
| 40 "</serviceList>\n" | |
| 41 "</device>\n" | |
| 42 "</root>\n"; | |
| 43 | |
| 44 constexpr char kDeviceDescriptionWithoutService[] = | |
| 45 "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" | |
| 46 "<specVersion>\n" | |
| 47 "<major>1</major>\n" | |
| 48 "<minor>0</minor>\n" | |
| 49 "</specVersion>\n" | |
| 50 "<URLBase>http://172.31.71.84:8008</URLBase>\n" | |
| 51 "<device>\n" | |
| 52 "<deviceType>urn:dial-multiscreen-org:device:dial:1</deviceType>\n" | |
| 53 "<friendlyName>eureka9020</friendlyName>\n" | |
| 54 "<manufacturer>Google Inc.</manufacturer>\n" | |
| 55 "<modelName>Eureka Dongle</modelName>\n" | |
| 56 "<serialNumber>123456789000</serialNumber>\n" | |
| 57 "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f</UDN>\n" | |
| 58 "</device>\n" | |
| 59 "</root>\n"; | |
| 60 } // namespace | |
| 61 | |
| 62 namespace media_router { | |
| 63 | |
| 64 class DialDeviceDescriptionParserTest : public testing::Test { | |
| 65 public: | |
| 66 DialDeviceDescriptionParserTest() {} | |
| 67 | |
| 68 std::string& Replace(std::string& input, | |
|
mark a. foltz
2017/03/18 18:56:57
This doesn't need to be part of a class; you can d
zhaobin
2017/03/20 21:25:46
Done.
| |
| 69 const std::string& from, | |
| 70 const std::string& to) { | |
| 71 size_t pos = input.find(from); | |
| 72 if (pos == std::string::npos) | |
| 73 return input; | |
| 74 | |
| 75 return input.replace(pos, from.size(), to); | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 DISALLOW_COPY_AND_ASSIGN(DialDeviceDescriptionParserTest); | |
| 80 }; | |
| 81 | |
| 82 TEST_F(DialDeviceDescriptionParserTest, TestInvalidXml) { | |
| 83 DialDeviceDescriptionParser parser; | |
| 84 DialDeviceDescription device_description; | |
| 85 EXPECT_FALSE(parser.Parse("", &device_description)); | |
| 86 } | |
| 87 | |
| 88 TEST_F(DialDeviceDescriptionParserTest, TestScrubXmlForLogging) { | |
| 89 std::string xml_text(kDeviceDescriptionWithService); | |
| 90 | |
| 91 DialDeviceDescriptionParser parser; | |
| 92 std::string xml_logging = parser.ScrubXmlForLogging(xml_text); | |
| 93 | |
| 94 std::string serial_number = "<serialNumber>123456789000</serialNumber>"; | |
| 95 std::string new_serial_number = "<serialNumber>***</serialNumber>"; | |
| 96 std::string udn = "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e</UDN>"; | |
| 97 std::string new_udn = "<UDN>***</UDN>"; | |
| 98 | |
| 99 std::string expected_xml = "<?xml version=\"1.0\"?>\n" + xml_text; | |
| 100 Replace(expected_xml, serial_number, new_serial_number); | |
| 101 Replace(expected_xml, udn, new_udn); | |
| 102 EXPECT_EQ(expected_xml, xml_logging); | |
| 103 } | |
| 104 | |
| 105 TEST_F(DialDeviceDescriptionParserTest, TestParse) { | |
| 106 std::string xml_text(kDeviceDescriptionWithService); | |
| 107 | |
| 108 DialDeviceDescriptionParser parser; | |
| 109 DialDeviceDescription device_description; | |
| 110 EXPECT_TRUE(parser.Parse(xml_text, &device_description)); | |
| 111 | |
| 112 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", | |
| 113 device_description.device_type); | |
| 114 EXPECT_EQ("eureka9019", device_description.friendly_name); | |
| 115 EXPECT_EQ("Eureka Dongle", device_description.model_name); | |
| 116 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", | |
| 117 device_description.unique_id); | |
| 118 } | |
| 119 | |
| 120 TEST_F(DialDeviceDescriptionParserTest, TestParseWithSpecialCharacter) { | |
| 121 std::string old_name = "<friendlyName>eureka9019</friendlyName>"; | |
| 122 std::string new_name = "<friendlyName>Samsung LED40\'s</friendlyName>"; | |
| 123 | |
| 124 std::string xml_text(kDeviceDescriptionWithService); | |
| 125 xml_text = Replace(xml_text, old_name, new_name); | |
| 126 | |
| 127 DialDeviceDescriptionParser parser; | |
| 128 DialDeviceDescription device_description; | |
| 129 EXPECT_TRUE(parser.Parse(xml_text, &device_description)); | |
| 130 | |
| 131 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", | |
| 132 device_description.device_type); | |
| 133 EXPECT_EQ("Samsung LED40\'s", device_description.friendly_name); | |
| 134 EXPECT_EQ("Eureka Dongle", device_description.model_name); | |
| 135 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", | |
| 136 device_description.unique_id); | |
| 137 } | |
| 138 | |
| 139 TEST_F(DialDeviceDescriptionParserTest, TestParseWithoutFriendlyNameModelName) { | |
| 140 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; | |
| 141 std::string model_name = "<modelName>Eureka Dongle</modelName>"; | |
| 142 | |
| 143 std::string xml_text(kDeviceDescriptionWithoutService); | |
| 144 xml_text = Replace(xml_text, friendly_name, ""); | |
| 145 xml_text = Replace(xml_text, model_name, ""); | |
| 146 | |
| 147 DialDeviceDescriptionParser parser; | |
| 148 DialDeviceDescription device_description; | |
| 149 EXPECT_FALSE(parser.Parse(xml_text, &device_description)); | |
| 150 } | |
| 151 | |
| 152 TEST_F(DialDeviceDescriptionParserTest, TestParseWithoutFriendlyName) { | |
| 153 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; | |
| 154 | |
| 155 std::string xml_text(kDeviceDescriptionWithoutService); | |
| 156 xml_text = Replace(xml_text, friendly_name, ""); | |
| 157 | |
| 158 DialDeviceDescriptionParser parser; | |
| 159 DialDeviceDescription device_description; | |
| 160 EXPECT_TRUE(parser.Parse(xml_text, &device_description)); | |
| 161 | |
| 162 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", | |
| 163 device_description.device_type); | |
| 164 EXPECT_EQ("Eureka Dongle[4b0f]", device_description.friendly_name); | |
| 165 EXPECT_EQ("Eureka Dongle", device_description.model_name); | |
| 166 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f", | |
| 167 device_description.unique_id); | |
| 168 } | |
| 169 | |
| 170 } // namespace media_router | |
| OLD | NEW |