| 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 "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 constexpr char kDeviceDescriptionWithService[] = |
| 16 "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" |
| 17 "<specVersion>\n" |
| 18 "<major>1</major>\n" |
| 19 "<minor>0</minor>\n" |
| 20 "</specVersion>\n" |
| 21 "<URLBase>http://172.31.71.84:8008</URLBase>\n" |
| 22 "<device>\n" |
| 23 "<deviceType>urn:dial-multiscreen-org:device:dial:1</deviceType>\n" |
| 24 "<friendlyName>eureka9019</friendlyName>\n" |
| 25 "<manufacturer>Google Inc.</manufacturer>\n" |
| 26 "<modelName>Eureka Dongle</modelName>\n" |
| 27 "<serialNumber>123456789000</serialNumber>\n" |
| 28 "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e</UDN>\n" |
| 29 "<serviceList>\n" |
| 30 "<service>\n" |
| 31 "<serviceType>urn:dial-multiscreen-org:service:dial:1</serviceType>\n" |
| 32 "<serviceId>urn:dial-multiscreen-org:serviceId:dial</serviceId>\n" |
| 33 "<controlURL>/ssdp/notfound</controlURL>\n" |
| 34 "<eventSubURL>/ssdp/notfound</eventSubURL>\n" |
| 35 "<SCPDURL>/ssdp/notfound</SCPDURL>\n" |
| 36 "<servicedata xmlns=\"uri://cloudview.google.com/...\">\n" |
| 37 "</servicedata>\n" |
| 38 "</service>\n" |
| 39 "</serviceList>\n" |
| 40 "</device>\n" |
| 41 "</root>\n"; |
| 42 |
| 43 constexpr char kDeviceDescriptionWithoutService[] = |
| 44 "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" |
| 45 "<specVersion>\n" |
| 46 "<major>1</major>\n" |
| 47 "<minor>0</minor>\n" |
| 48 "</specVersion>\n" |
| 49 "<URLBase>http://172.31.71.84:8008</URLBase>\n" |
| 50 "<device>\n" |
| 51 "<deviceType>urn:dial-multiscreen-org:device:dial:1</deviceType>\n" |
| 52 "<friendlyName>eureka9020</friendlyName>\n" |
| 53 "<manufacturer>Google Inc.</manufacturer>\n" |
| 54 "<modelName>Eureka Dongle</modelName>\n" |
| 55 "<serialNumber>123456789000</serialNumber>\n" |
| 56 "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f</UDN>\n" |
| 57 "</device>\n" |
| 58 "</root>\n"; |
| 59 |
| 60 std::string& Replace(std::string& input, |
| 61 const std::string& from, |
| 62 const std::string& to) { |
| 63 size_t pos = input.find(from); |
| 64 if (pos == std::string::npos) |
| 65 return input; |
| 66 |
| 67 return input.replace(pos, from.size(), to); |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 namespace media_router { |
| 73 |
| 74 class DialDeviceDescriptionParserImplTest : public testing::Test { |
| 75 public: |
| 76 DialDeviceDescriptionParserImplTest() {} |
| 77 chrome::mojom::DialDeviceDescriptionPtr Parse(const std::string& xml) { |
| 78 return parser_.Parse(xml); |
| 79 } |
| 80 |
| 81 private: |
| 82 DialDeviceDescriptionParserImpl parser_; |
| 83 DISALLOW_COPY_AND_ASSIGN(DialDeviceDescriptionParserImplTest); |
| 84 }; |
| 85 |
| 86 TEST_F(DialDeviceDescriptionParserImplTest, TestInvalidXml) { |
| 87 chrome::mojom::DialDeviceDescriptionPtr device_description = Parse(""); |
| 88 EXPECT_FALSE(device_description); |
| 89 } |
| 90 |
| 91 TEST_F(DialDeviceDescriptionParserImplTest, TestParse) { |
| 92 std::string xml_text(kDeviceDescriptionWithService); |
| 93 |
| 94 chrome::mojom::DialDeviceDescriptionPtr device_description = Parse(xml_text); |
| 95 EXPECT_TRUE(device_description); |
| 96 |
| 97 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 98 device_description->device_type); |
| 99 EXPECT_EQ("eureka9019", device_description->friendly_name); |
| 100 EXPECT_EQ("Eureka Dongle", device_description->model_name); |
| 101 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| 102 device_description->unique_id); |
| 103 } |
| 104 |
| 105 TEST_F(DialDeviceDescriptionParserImplTest, TestParseWithSpecialCharacter) { |
| 106 std::string old_name = "<friendlyName>eureka9019</friendlyName>"; |
| 107 std::string new_name = "<friendlyName>Samsung LED40\'s</friendlyName>"; |
| 108 |
| 109 std::string xml_text(kDeviceDescriptionWithService); |
| 110 xml_text = Replace(xml_text, old_name, new_name); |
| 111 |
| 112 chrome::mojom::DialDeviceDescriptionPtr device_description = Parse(xml_text); |
| 113 EXPECT_TRUE(device_description); |
| 114 |
| 115 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 116 device_description->device_type); |
| 117 EXPECT_EQ("Samsung LED40\'s", device_description->friendly_name); |
| 118 EXPECT_EQ("Eureka Dongle", device_description->model_name); |
| 119 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| 120 device_description->unique_id); |
| 121 } |
| 122 |
| 123 TEST_F(DialDeviceDescriptionParserImplTest, |
| 124 TestParseWithoutFriendlyNameModelName) { |
| 125 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| 126 std::string model_name = "<modelName>Eureka Dongle</modelName>"; |
| 127 |
| 128 std::string xml_text(kDeviceDescriptionWithoutService); |
| 129 xml_text = Replace(xml_text, friendly_name, ""); |
| 130 xml_text = Replace(xml_text, model_name, ""); |
| 131 |
| 132 chrome::mojom::DialDeviceDescriptionPtr device_description = Parse(xml_text); |
| 133 EXPECT_FALSE(device_description); |
| 134 } |
| 135 |
| 136 TEST_F(DialDeviceDescriptionParserImplTest, TestParseWithoutFriendlyName) { |
| 137 std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| 138 |
| 139 std::string xml_text(kDeviceDescriptionWithoutService); |
| 140 xml_text = Replace(xml_text, friendly_name, ""); |
| 141 |
| 142 chrome::mojom::DialDeviceDescriptionPtr device_description = Parse(xml_text); |
| 143 EXPECT_TRUE(device_description); |
| 144 |
| 145 EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| 146 device_description->device_type); |
| 147 EXPECT_EQ("Eureka Dongle [4b0f]", device_description->friendly_name); |
| 148 EXPECT_EQ("Eureka Dongle", device_description->model_name); |
| 149 EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f", |
| 150 device_description->unique_id); |
| 151 } |
| 152 |
| 153 } // namespace media_router |
| OLD | NEW |