/* Copyright (c) 2012, Arvid Norberg All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/xml_parse.hpp" #include "libtorrent/upnp.hpp" #include "test.hpp" #include #include namespace { char upnp_xml[] = "" "" "1" "0" "" "http://192.168.0.1:5678" "" "" "urn:schemas-upnp-org:device:InternetGatewayDevice:1" "" "http://192.168.0.1:80" "D-Link Router" "D-Link" "http://www.dlink.com" "Internet Access Router" "D-Link Router" "uuid:upnp-InternetGatewayDevice-1_0-12345678900001" "123456789001" "" "" "urn:schemas-upnp-org:service:Layer3Forwarding:1" "urn:upnp-org:serviceId:L3Forwarding1" "/Layer3Forwarding" "/Layer3Forwarding" "/Layer3Forwarding.xml" "" "" "" "" "urn:schemas-upnp-org:device:WANDevice:1" "WANDevice" "D-Link" "http://www.dlink.com" "Internet Access Router" "D-Link Router" "1" "http://support.dlink.com" "12345678900001" "uuid:upnp-WANDevice-1_0-12345678900001" "123456789001" "" "" "" "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" "" "urn:upnp-org:serviceId:WANCommonInterfaceConfig" "/WANCommonInterfaceConfig" "/WANCommonInterfaceConfig" "/WANCommonInterfaceConfig.xml" "" "" "" "" "urn:schemas-upnp-org:device:WANConnectionDevice:1" "WAN Connection Device" "D-Link" "http://www.dlink.com" "Internet Access Router" "D-Link Router" "1" "http://support.dlink.com" "12345678900001" "uuid:upnp-WANConnectionDevice-1_0-12345678900001" "123456789001" "" "" "urn:schemas-upnp-org:service:WANIPConnection:1" "urn:upnp-org:serviceId:WANIPConnection" "/WANIPConnection" "/WANIPConnection" "/WANIPConnection.xml" "" "" "" "" "" "" "" ""; char upnp_xml2[] = "" "" "1" "0" "" "http://192.168.1.1:49152" "" "" "urn:schemas-upnp-org:device:InternetGatewayDevice:1" "" "LINKSYS WAG200G Gateway" "LINKSYS" "http://www.linksys.com" "LINKSYS WAG200G Gateway" "Wireless-G ADSL Home Gateway" "WAG200G" "http://www.linksys.com" "123456789" "uuid:8d401597-1dd2-11b2-a7d4-001ee5947cac" "WAG200G" "" "" "urn:schemas-upnp-org:service:Layer3Forwarding:1" "urn:upnp-org:serviceId:L3Forwarding1" "/upnp/control/L3Forwarding1" "/upnp/event/L3Forwarding1" "/l3frwd.xml" "" "" "" "" "urn:schemas-upnp-org:device:WANDevice:1" "WANDevice" "LINKSYS" "http://www.linksys.com/" "Residential Gateway" "Internet Connection Sharing" "1" "http://www.linksys.com/" "0000001" "uuid:8d401596-1dd2-11b2-a7d4-001ee5947cac" "WAG200G" "" "" "" "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" "" "urn:upnp-org:serviceId:WANCommonIFC1" "/upnp/control/WANCommonIFC1" "/upnp/event/WANCommonIFC1" "/cmnicfg.xml" "" "" "" "" "urn:schemas-upnp-org:device:WANConnectionDevice:1" "WANConnectionDevice" "LINKSYS" "http://www.linksys.com/" "Residential Gateway" "Internet Connection Sharing" "1" "http://www.linksys.com/" "0000001" "uuid:8d401597-1dd2-11b2-a7d3-001ee5947cac" "WAG200G" "" "" "" "urn:schemas-upnp-org:service:WANEthernetLinkConfig:1" "" "urn:upnp-org:serviceId:WANEthLinkC1" "/upnp/control/WANEthLinkC1" "/upnp/event/WANEthLinkC1" "/wanelcfg.xml" "" "" "urn:schemas-upnp-org:service:WANPPPConnection:1" "urn:upnp-org:serviceId:WANPPPConn1" "/upnp/control/WANPPPConn1" "/upnp/event/WANPPPConn1" "/pppcfg.xml" "" "" "" "" "" "" "urn:schemas-upnp-org:device:LANDevice:1" "LANDevice" "LINKSYS" "http://www.linksys.com/" "Residential Gateway" "Residential Gateway" "1" "http://www.linksys.com/" "0000001" "uuid:8d401596-1dd2-11b2-a7d3-001ee5947cac" "WAG200G" "" "" "" "urn:schemas-upnp-org:service:LANHostConfigManagement:1" "" "urn:upnp-org:serviceId:LANHostCfg1" "/upnp/control/LANHostCfg1" "/upnp/event/LANHostCfg1" "/lanhostc.xml" "" "" "" "" "http://192.168.1.1/index.htm" "" ""; char upnp_xml3[] = "" "" "" "s:Client" "UPnPError" "" "" "402" "Invalid Args" "" "" "" "" ""; char upnp_xml4[] = "" "" "" "" "123.10.20.30" "" "" ""; using namespace lt; using namespace std::placeholders; void parser_callback(std::string& out, int token, string_view s , string_view val) { switch (token) { case xml_start_tag: out += "B"; break; case xml_end_tag: out += "F"; break; case xml_empty_tag: out += "E"; break; case xml_declaration_tag: out += "D"; break; case xml_comment: out += "C"; break; case xml_string: out += "S"; break; case xml_attribute: out += "A"; break; case xml_parse_error: out += "P"; break; case xml_tag_content: out += "T"; break; default: TEST_CHECK(false); } out.append(s.begin(), s.end()); if (token == xml_attribute) { TEST_CHECK(!val.empty()); out += "V"; out.append(val.begin(), val.end()); } else { TEST_CHECK(val.empty()); } } void test_parse(char const* in, char const* expected) { std::string out; xml_parse(in, std::bind(&parser_callback , std::ref(out), _1, _2, _3)); std::printf("in: %s\n out: %s\nexpected: %s\n" , in, out.c_str(), expected); TEST_EQUAL(out, expected); } } // anonymous namespace TORRENT_TEST(upnp_parser1) { parse_state xml_s; xml_parse(upnp_xml, std::bind(&find_control_url, _1, _2, std::ref(xml_s))); std::cout << "namespace " << xml_s.service_type << std::endl; std::cout << "url_base: " << xml_s.url_base << std::endl; std::cout << "control_url: " << xml_s.control_url << std::endl; std::cout << "model: " << xml_s.model << std::endl; TEST_EQUAL(xml_s.url_base, "http://192.168.0.1:5678"); TEST_EQUAL(xml_s.control_url, "/WANIPConnection"); TEST_EQUAL(xml_s.model, "D-Link Router"); } TORRENT_TEST(upnp_parser2) { parse_state xml_s; xml_parse(upnp_xml2, std::bind(&find_control_url, _1, _2, std::ref(xml_s))); std::cout << "namespace " << xml_s.service_type << std::endl; std::cout << "url_base: " << xml_s.url_base << std::endl; std::cout << "control_url: " << xml_s.control_url << std::endl; std::cout << "model: " << xml_s.model << std::endl; TEST_EQUAL(xml_s.url_base, "http://192.168.1.1:49152"); TEST_EQUAL(xml_s.control_url, "/upnp/control/WANPPPConn1"); TEST_EQUAL(xml_s.model, "Wireless-G ADSL Home Gateway"); } TORRENT_TEST(upnp_parser3) { error_code_parse_state xml_s; xml_parse(upnp_xml3, std::bind(&find_error_code, _1, _2, std::ref(xml_s))); std::cout << "error_code " << xml_s.error_code << std::endl; TEST_EQUAL(xml_s.error_code, 402); } TORRENT_TEST(upnp_parser4) { ip_address_parse_state xml_s; xml_parse(upnp_xml4, std::bind(&find_ip_address, _1, _2, std::ref(xml_s))); std::cout << "error_code " << xml_s.error_code << std::endl; std::cout << "ip_address " << xml_s.ip_address << std::endl; TEST_EQUAL(xml_s.error_code, -1); TEST_EQUAL(xml_s.ip_address, "123.10.20.30"); } TORRENT_TEST(tags) { test_parse("foobar", "BaSfooEbSbarFa"); } TORRENT_TEST(xml_tag_comment) { test_parse("" , "DxmlAversionV1.0EcAxV1AyV3BdAfooVbarFdAbooVfooCcomment"); } TORRENT_TEST(empty_tag) { test_parse("", "Efoo"); } TORRENT_TEST(empty_tag_whitespace) { test_parse("", "Efoo"); } TORRENT_TEST(xml_tag_no_attribute) { test_parse("", "Dxml"); } TORRENT_TEST(xml_tag_no_attribute_whitespace) { test_parse("", "Dxml"); } TORRENT_TEST(attribute_missing_qoute) { test_parse("foo