| 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, |
| 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 EXPECT_FALSE(parser.Parse("")); |
| 85 } |
| 86 |
| 87 TEST_F(DialDeviceDescriptionParserTest, TestScrubXmlForLogging) { |
| 88 std::string xml_text(kDeviceDescriptionWithService); |
| 89 |
| 90 DialDeviceDescriptionParser parser; |
| 91 std::string xml_logging = parser.ScrubXmlForLogging(xml_text); |
| 92 |
| 93 std::string serial_number = "<serialNumber>123456789000</serialNumber>"; |
| 94 std::string new_serial_number = "<serialNumber>***</serialNumber>"; |
| 95 std::string udn = "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e</UDN>"; |
| 96 std::string new_udn = "<UDN>***</UDN>"; |
| 97 |
| 98 std::string expected_xml = "<?xml version=\"1.0\"?>\n" + xml_text; |
| 99 Replace(expected_xml, serial_number, new_serial_number); |
| 100 Replace(expected_xml, udn, new_udn); |
| 101 EXPECT_EQ(expected_xml, xml_logging); |
| 102 } |
| 103 |
| 104 TEST_F(DialDeviceDescriptionParserTest, TestParse) { |
| 105 std::string xml_text(kDeviceDescriptionWithService); |
| 106 |
| 107 DialDeviceDescriptionParser parser; |
| 108 EXPECT_TRUE(parser.Parse(xml_text)); |
| 109 |
| 110 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 111 parser.device_description().device_type); |
| 112 EXPECT_EQ("eureka9019", parser.device_description().friendly_name); |
| 113 EXPECT_EQ("Eureka Dongle", parser.device_description().model_name); |
| 114 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| 115 parser.device_description().unique_id); |
| 116 } |
| 117 |
| 118 TEST_F(DialDeviceDescriptionParserTest, TestParseWithSpecialCharacter) { |
| 119 std::string old_name = "<friendlyName>eureka9019</friendlyName>"; |
| 120 std::string new_name = "<friendlyName>Samsung LED40\'s</friendlyName>"; |
| 121 |
| 122 std::string xml_text(kDeviceDescriptionWithService); |
| 123 xml_text = Replace(xml_text, old_name, new_name); |
| 124 |
| 125 DialDeviceDescriptionParser parser; |
| 126 EXPECT_TRUE(parser.Parse(xml_text)); |
| 127 |
| 128 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 129 parser.device_description().device_type); |
| 130 EXPECT_EQ("Samsung LED40\'s", parser.device_description().friendly_name); |
| 131 EXPECT_EQ("Eureka Dongle", parser.device_description().model_name); |
| 132 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| 133 parser.device_description().unique_id); |
| 134 } |
| 135 |
| 136 TEST_F(DialDeviceDescriptionParserTest, TestParseWithoutFriendlyNameModelName) { |
| 137 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| 138 std::string model_name = "<modelName>Eureka Dongle</modelName>"; |
| 139 |
| 140 std::string xml_text(kDeviceDescriptionWithoutService); |
| 141 xml_text = Replace(xml_text, friendly_name, ""); |
| 142 xml_text = Replace(xml_text, model_name, ""); |
| 143 |
| 144 DialDeviceDescriptionParser parser; |
| 145 EXPECT_FALSE(parser.Parse(xml_text)); |
| 146 } |
| 147 |
| 148 TEST_F(DialDeviceDescriptionParserTest, TestParseWithoutFriendlyName) { |
| 149 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| 150 |
| 151 std::string xml_text(kDeviceDescriptionWithoutService); |
| 152 xml_text = Replace(xml_text, friendly_name, ""); |
| 153 |
| 154 DialDeviceDescriptionParser parser; |
| 155 EXPECT_TRUE(parser.Parse(xml_text)); |
| 156 |
| 157 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 158 parser.device_description().device_type); |
| 159 EXPECT_EQ("Eureka Dongle[4b0f]", parser.device_description().friendly_name); |
| 160 EXPECT_EQ("Eureka Dongle", parser.device_description().model_name); |
| 161 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f", |
| 162 parser.device_description().unique_id); |
| 163 } |
| 164 |
| 165 } // namespace media_router |
| OLD | NEW |