/* 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 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" "" ""; using namespace libtorrent; namespace libtorrent { struct parse_state { parse_state(): in_service(false), service_type("") {} void reset(char const* st) { in_service = false; service_type = st; tag_stack.clear(); control_url.clear(); model.clear(); url_base.clear(); } bool in_service; std::list tag_stack; std::string control_url; char const* service_type; std::string model; std::string url_base; }; TORRENT_EXTRA_EXPORT void find_control_url(int type\ , char const* string, parse_state& state); } void parser_callback(std::string& out, int token, char const* s, char const* 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 += s; if (token == xml_attribute) { TEST_CHECK(val != 0); out += "V"; out += val; } else { TEST_CHECK(val == 0); } } int test_main() { // test upnp xml parser parse_state xml_s; xml_s.reset("urn:schemas-upnp-org:service:WANIPConnection:1"); xml_parse(upnp_xml, upnp_xml + sizeof(upnp_xml) , boost::bind(&find_control_url, _1, _2, boost::ref(xml_s))); std::cerr << "namespace " << xml_s.service_type << std::endl; std::cerr << "url_base: " << xml_s.url_base << std::endl; std::cerr << "control_url: " << xml_s.control_url << std::endl; std::cerr << "model: " << xml_s.model << std::endl; TEST_CHECK(xml_s.url_base == "http://192.168.0.1:5678"); TEST_CHECK(xml_s.control_url == "/WANIPConnection"); TEST_CHECK(xml_s.model == "D-Link Router"); xml_s.reset("urn:schemas-upnp-org:service:WANPPPConnection:1"); xml_parse(upnp_xml2, upnp_xml2 + sizeof(upnp_xml2) , boost::bind(&find_control_url, _1, _2, boost::ref(xml_s))); std::cerr << "namespace " << xml_s.service_type << std::endl; std::cerr << "url_base: " << xml_s.url_base << std::endl; std::cerr << "control_url: " << xml_s.control_url << std::endl; std::cerr << "model: " << xml_s.model << std::endl; TEST_CHECK(xml_s.url_base == "http://192.168.1.1:49152"); TEST_CHECK(xml_s.control_url == "/upnp/control/WANPPPConn1"); TEST_CHECK(xml_s.model == "Wireless-G ADSL Home Gateway"); // test xml parser char xml1[] = "foobar"; std::string out1; xml_parse(xml1, xml1 + sizeof(xml1) - 1, boost::bind(&parser_callback , boost::ref(out1), _1, _2, _3)); std::cerr << out1 << std::endl; TEST_CHECK(out1 == "BaSfooEbSbarFa"); char xml2[] = ""; std::string out2; xml_parse(xml2, xml2 + sizeof(xml2) - 1, boost::bind(&parser_callback , boost::ref(out2), _1, _2, _3)); std::cerr << out2 << std::endl; TEST_CHECK(out2 == "DxmlAversionV1.0EcAxV1AyV3BdAfooVbarFdAbooVfooCcomment"); char xml3[] = "foo