Chromium Code Reviews| Index: chrome/utility/media_router/dial_device_description_parser_unittest.cc |
| diff --git a/chrome/utility/media_router/dial_device_description_parser_unittest.cc b/chrome/utility/media_router/dial_device_description_parser_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c23624d89eaef10a89bb7ec3f1febd7fadaa664c |
| --- /dev/null |
| +++ b/chrome/utility/media_router/dial_device_description_parser_unittest.cc |
| @@ -0,0 +1,170 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +#include "chrome/utility/media_router/dial_device_description_parser.h" |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/strings/string_util.h" |
| +#include "chrome/common/media_router/dial_device_description.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +constexpr char kDeviceDescriptionWithService[] = |
| + "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" |
| + "<specVersion>\n" |
| + "<major>1</major>\n" |
| + "<minor>0</minor>\n" |
| + "</specVersion>\n" |
| + "<URLBase>http://172.31.71.84:8008</URLBase>\n" |
| + "<device>\n" |
| + "<deviceType>urn:dial-multiscreen-org:device:dial:1</deviceType>\n" |
| + "<friendlyName>eureka9019</friendlyName>\n" |
| + "<manufacturer>Google Inc.</manufacturer>\n" |
| + "<modelName>Eureka Dongle</modelName>\n" |
| + "<serialNumber>123456789000</serialNumber>\n" |
| + "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e</UDN>\n" |
| + "<serviceList>\n" |
| + "<service>\n" |
| + "<serviceType>urn:dial-multiscreen-org:service:dial:1</serviceType>\n" |
| + "<serviceId>urn:dial-multiscreen-org:serviceId:dial</serviceId>\n" |
| + "<controlURL>/ssdp/notfound</controlURL>\n" |
| + "<eventSubURL>/ssdp/notfound</eventSubURL>\n" |
| + "<SCPDURL>/ssdp/notfound</SCPDURL>\n" |
| + "<servicedata xmlns=\"uri://cloudview.google.com/...\">\n" |
| + "</servicedata>\n" |
| + "</service>\n" |
| + "</serviceList>\n" |
| + "</device>\n" |
| + "</root>\n"; |
| + |
| +constexpr char kDeviceDescriptionWithoutService[] = |
| + "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" |
| + "<specVersion>\n" |
| + "<major>1</major>\n" |
| + "<minor>0</minor>\n" |
| + "</specVersion>\n" |
| + "<URLBase>http://172.31.71.84:8008</URLBase>\n" |
| + "<device>\n" |
| + "<deviceType>urn:dial-multiscreen-org:device:dial:1</deviceType>\n" |
| + "<friendlyName>eureka9020</friendlyName>\n" |
| + "<manufacturer>Google Inc.</manufacturer>\n" |
| + "<modelName>Eureka Dongle</modelName>\n" |
| + "<serialNumber>123456789000</serialNumber>\n" |
| + "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f</UDN>\n" |
| + "</device>\n" |
| + "</root>\n"; |
| +} // namespace |
| + |
| +namespace media_router { |
| + |
| +class DialDeviceDescriptionParserTest : public testing::Test { |
| + public: |
| + DialDeviceDescriptionParserTest() {} |
| + |
| + 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.
|
| + const std::string& from, |
| + const std::string& to) { |
| + size_t pos = input.find(from); |
| + if (pos == std::string::npos) |
| + return input; |
| + |
| + return input.replace(pos, from.size(), to); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(DialDeviceDescriptionParserTest); |
| +}; |
| + |
| +TEST_F(DialDeviceDescriptionParserTest, TestInvalidXml) { |
| + DialDeviceDescriptionParser parser; |
| + DialDeviceDescription device_description; |
| + EXPECT_FALSE(parser.Parse("", &device_description)); |
| +} |
| + |
| +TEST_F(DialDeviceDescriptionParserTest, TestScrubXmlForLogging) { |
| + std::string xml_text(kDeviceDescriptionWithService); |
| + |
| + DialDeviceDescriptionParser parser; |
| + std::string xml_logging = parser.ScrubXmlForLogging(xml_text); |
| + |
| + std::string serial_number = "<serialNumber>123456789000</serialNumber>"; |
| + std::string new_serial_number = "<serialNumber>***</serialNumber>"; |
| + std::string udn = "<UDN>uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e</UDN>"; |
| + std::string new_udn = "<UDN>***</UDN>"; |
| + |
| + std::string expected_xml = "<?xml version=\"1.0\"?>\n" + xml_text; |
| + Replace(expected_xml, serial_number, new_serial_number); |
| + Replace(expected_xml, udn, new_udn); |
| + EXPECT_EQ(expected_xml, xml_logging); |
| +} |
| + |
| +TEST_F(DialDeviceDescriptionParserTest, TestParse) { |
| + std::string xml_text(kDeviceDescriptionWithService); |
| + |
| + DialDeviceDescriptionParser parser; |
| + DialDeviceDescription device_description; |
| + EXPECT_TRUE(parser.Parse(xml_text, &device_description)); |
| + |
| + EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| + device_description.device_type); |
| + EXPECT_EQ("eureka9019", device_description.friendly_name); |
| + EXPECT_EQ("Eureka Dongle", device_description.model_name); |
| + EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| + device_description.unique_id); |
| +} |
| + |
| +TEST_F(DialDeviceDescriptionParserTest, TestParseWithSpecialCharacter) { |
| + std::string old_name = "<friendlyName>eureka9019</friendlyName>"; |
| + std::string new_name = "<friendlyName>Samsung LED40\'s</friendlyName>"; |
| + |
| + std::string xml_text(kDeviceDescriptionWithService); |
| + xml_text = Replace(xml_text, old_name, new_name); |
| + |
| + DialDeviceDescriptionParser parser; |
| + DialDeviceDescription device_description; |
| + EXPECT_TRUE(parser.Parse(xml_text, &device_description)); |
| + |
| + EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| + device_description.device_type); |
| + EXPECT_EQ("Samsung LED40\'s", device_description.friendly_name); |
| + EXPECT_EQ("Eureka Dongle", device_description.model_name); |
| + EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0e", |
| + device_description.unique_id); |
| +} |
| + |
| +TEST_F(DialDeviceDescriptionParserTest, TestParseWithoutFriendlyNameModelName) { |
| + std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| + std::string model_name = "<modelName>Eureka Dongle</modelName>"; |
| + |
| + std::string xml_text(kDeviceDescriptionWithoutService); |
| + xml_text = Replace(xml_text, friendly_name, ""); |
| + xml_text = Replace(xml_text, model_name, ""); |
| + |
| + DialDeviceDescriptionParser parser; |
| + DialDeviceDescription device_description; |
| + EXPECT_FALSE(parser.Parse(xml_text, &device_description)); |
| +} |
| + |
| +TEST_F(DialDeviceDescriptionParserTest, TestParseWithoutFriendlyName) { |
| + std::string friendly_name = "<friendlyName>eureka9020</friendlyName>"; |
| + |
| + std::string xml_text(kDeviceDescriptionWithoutService); |
| + xml_text = Replace(xml_text, friendly_name, ""); |
| + |
| + DialDeviceDescriptionParser parser; |
| + DialDeviceDescription device_description; |
| + EXPECT_TRUE(parser.Parse(xml_text, &device_description)); |
| + |
| + EXPECT_EQ("urn:dial-multiscreen-org:device:dial:1", |
| + device_description.device_type); |
| + EXPECT_EQ("Eureka Dongle[4b0f]", device_description.friendly_name); |
| + EXPECT_EQ("Eureka Dongle", device_description.model_name); |
| + EXPECT_EQ("uuid:d90dda41-8fa0-61ac-0567-f949d3e34b0f", |
| + device_description.unique_id); |
| +} |
| + |
| +} // namespace media_router |