| 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_impl.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 |
| 61 std::string& Replace(std::string& input, |
| 62 const std::string& from, |
| 63 const std::string& to) { |
| 64 size_t pos = input.find(from); |
| 65 if (pos == std::string::npos) |
| 66 return input; |
| 67 |
| 68 return input.replace(pos, from.size(), to); |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 namespace media_router { |
| 74 |
| 75 TEST(DialDeviceDescriptionParserImplTest, TestInvalidXml) { |
| 76 DialDeviceDescriptionParserImpl parser; |
| 77 DialDeviceDescription device_description; |
| 78 EXPECT_FALSE(parser.Parse("", &device_description)); |
| 79 } |
| 80 |
| 81 TEST(DialDeviceDescriptionParserImplTest, TestParse) { |
| 82 std::string xml_text(kDeviceDescriptionWithService); |
| 83 |
| 84 DialDeviceDescriptionParserImpl parser; |
| 85 DialDeviceDescription device_description; |
| 86 EXPECT_TRUE(parser.Parse(xml_text, &device_description)); |
| 87 |
| 88 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 89 device_description.device_type); |
| 90 EXPECT_EQ("eureka9019", device_description.friendly_name); |
| 91 EXPECT_EQ("Eureka Dongle", device_description.model_name); |
| 92 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| 93 device_description.unique_id); |
| 94 } |
| 95 |
| 96 TEST(DialDeviceDescriptionParserImplTest, TestParseWithSpecialCharacter) { |
| 97 std::string old_name = "<friendlyName>eureka9019</friendlyName>"; |
| 98 std::string new_name = "<friendlyName>Samsung LED40\'s</friendlyName>"; |
| 99 |
| 100 std::string xml_text(kDeviceDescriptionWithService); |
| 101 xml_text = Replace(xml_text, old_name, new_name); |
| 102 |
| 103 DialDeviceDescriptionParserImpl parser; |
| 104 DialDeviceDescription device_description; |
| 105 EXPECT_TRUE(parser.Parse(xml_text, &device_description)); |
| 106 |
| 107 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 108 device_description.device_type); |
| 109 EXPECT_EQ("Samsung LED40\'s", device_description.friendly_name); |
| 110 EXPECT_EQ("Eureka Dongle", device_description.model_name); |
| 111 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| 112 device_description.unique_id); |
| 113 } |
| 114 |
| 115 TEST(DialDeviceDescriptionParserImplTest, |
| 116 TestParseWithoutFriendlyNameModelName) { |
| 117 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| 118 std::string model_name = "<modelName>Eureka Dongle</modelName>"; |
| 119 |
| 120 std::string xml_text(kDeviceDescriptionWithoutService); |
| 121 xml_text = Replace(xml_text, friendly_name, ""); |
| 122 xml_text = Replace(xml_text, model_name, ""); |
| 123 |
| 124 DialDeviceDescriptionParserImpl parser; |
| 125 DialDeviceDescription device_description; |
| 126 EXPECT_FALSE(parser.Parse(xml_text, &device_description)); |
| 127 } |
| 128 |
| 129 TEST(DialDeviceDescriptionParserImplTest, TestParseWithoutFriendlyName) { |
| 130 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| 131 |
| 132 std::string xml_text(kDeviceDescriptionWithoutService); |
| 133 xml_text = Replace(xml_text, friendly_name, ""); |
| 134 |
| 135 DialDeviceDescriptionParserImpl parser; |
| 136 DialDeviceDescription device_description; |
| 137 EXPECT_TRUE(parser.Parse(xml_text, &device_description)); |
| 138 |
| 139 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 140 device_description.device_type); |
| 141 EXPECT_EQ("Eureka Dongle [4b0f]", device_description.friendly_name); |
| 142 EXPECT_EQ("Eureka Dongle", device_description.model_name); |
| 143 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f", |
| 144 device_description.unique_id); |
| 145 } |
| 146 |
| 147 } // namespace media_router |
| OLD | NEW |