Add test case for igd_version

This commit is contained in:
Chocobo1 2015-07-08 19:44:54 +08:00
parent 70fa9098a8
commit 329d797342
3 changed files with 46 additions and 45 deletions

1
test/root3.xml Normal file
View File

@ -0,0 +1 @@
<root><specVersion><major>1</major><minor>0</minor></specVersion><URLBase>http://127.0.0.1:%d</URLBase><device><deviceType>urn:schemas-upnp-org:device:InternetGatewayDevice:2</deviceType><presentationURL>http://192.168.0.1:80</presentationURL><friendlyName>D-Link Router</friendlyName><manufacturer>D-Link</manufacturer><manufacturerURL>http://www.dlink.com</manufacturerURL><modelDescription>Internet Access Router</modelDescription><modelName>D-Link Router</modelName><UDN>uuid:upnp-InternetGatewayDevice-2_0-12345678900001</UDN><UPC>123456789001</UPC><serviceList><service><serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType><serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId><controlURL>/Layer3Forwarding</controlURL><eventSubURL>/Layer3Forwarding</eventSubURL><SCPDURL>/Layer3Forwarding.xml</SCPDURL></service></serviceList><deviceList><device><deviceType>urn:schemas-upnp-org:device:WANDevice:2</deviceType><friendlyName>WANDevice</friendlyName><manufacturer>D-Link</manufacturer><manufacturerURL>http://www.dlink.com</manufacturerURL><modelDescription>Internet Access Router</modelDescription><modelName>D-Link Router</modelName><modelNumber>1</modelNumber><modelURL>http://support.dlink.com</modelURL><serialNumber>12345678900001</serialNumber><UDN>uuid:upnp-WANDevice-1_0-12345678900001</UDN><UPC>123456789001</UPC><serviceList><service><serviceType>urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1</serviceType><serviceId>urn:upnp-org:serviceId:WANCommonInterfaceConfig</serviceId><controlURL>/WANCommonInterfaceConfig</controlURL><eventSubURL>/WANCommonInterfaceConfig</eventSubURL><SCPDURL>/WANCommonInterfaceConfig.xml</SCPDURL></service></serviceList><deviceList><device><deviceType>urn:schemas-upnp-org:device:WANConnectionDevice:2</deviceType><friendlyName>WAN Connection Device</friendlyName><manufacturer>D-Link</manufacturer><manufacturerURL>http://www.dlink.com</manufacturerURL><modelDescription>Internet Access Router</modelDescription><modelName>D-Link Router</modelName><modelNumber>1</modelNumber><modelURL>http://support.dlink.com</modelURL><serialNumber>12345678900001</serialNumber><UDN>uuid:upnp-WANConnectionDevice-2_0-12345678900001</UDN><UPC>123456789001</UPC><serviceList><service><serviceType>urn:schemas-upnp-org:service:WANIPConnection:2</serviceType><serviceId>urn:upnp-org:serviceId:WANIPConnection</serviceId><controlURL>/WANIPConnection</controlURL><eventSubURL>/WANIPConnection</eventSubURL><SCPDURL>/WANIPConnection.xml</SCPDURL></service></serviceList></device></deviceList></device></deviceList></device></root>

View File

@ -46,17 +46,25 @@ using namespace libtorrent;
broadcast_socket* sock = 0;
int g_port = 0;
char soap_add_response[] =
char const* soap_add_response[] = {
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<s:Body><u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">"
"</u:AddPortMapping></s:Body></s:Envelope>";
"</u:AddPortMapping></s:Body></s:Envelope>",
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<s:Body><u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:2\">"
"</u:AddPortMapping></s:Body></s:Envelope>"};
char soap_delete_response[] =
char const* soap_delete_response[] = {
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<s:Body><u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">"
"</u:DeletePortMapping></s:Body></s:Envelope>";
"</u:DeletePortMapping></s:Body></s:Envelope>",
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<s:Body><u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:2\">"
"</u:DeletePortMapping></s:Body></s:Envelope>"};
void incoming_msearch(udp::endpoint const& from, char* buffer
, int size)
@ -119,7 +127,7 @@ void callback(int mapping, address const& ip, int port, error_code const& err)
<< ", error: \"" << err.message() << "\"\n";
}
void run_upnp_test(char const* root_filename, char const* router_model, char const* control_name)
void run_upnp_test(char const* root_filename, char const* router_model, char const* control_name, int igd_version)
{
libtorrent::io_service ios;
@ -141,7 +149,7 @@ void run_upnp_test(char const* root_filename, char const* router_model, char con
fclose(xml_file);
std::ofstream xml(control_name, std::ios::trunc);
xml.write(soap_add_response, sizeof(soap_add_response)-1);
xml.write(soap_add_response[igd_version-1], sizeof(soap_add_response[igd_version-1])-1);
xml.close();
sock = new broadcast_socket(udp::endpoint(address_v4::from_string("239.255.255.250")
@ -197,7 +205,7 @@ void run_upnp_test(char const* root_filename, char const* router_model, char con
TEST_EQUAL(std::count(callbacks.begin(), callbacks.end(), expected2), 1);
xml.open(control_name, std::ios::trunc);
xml.write(soap_delete_response, sizeof(soap_delete_response)-1);
xml.write(soap_delete_response[igd_version-1], sizeof(soap_delete_response[igd_version-1])-1);
xml.close();
upnp_handler->close();
@ -229,7 +237,7 @@ void run_upnp_test(char const* root_filename, char const* router_model, char con
TORRENT_TEST(upnp)
{
run_upnp_test(combine_path("..", "root1.xml").c_str(), "Xtreme N GIGABIT Router", "wipconn");
run_upnp_test(combine_path("..", "root2.xml").c_str(), "D-Link Router", "WANIPConnection");
run_upnp_test(combine_path("..", "root1.xml").c_str(), "Xtreme N GIGABIT Router", "wipconn", 1);
run_upnp_test(combine_path("..", "root2.xml").c_str(), "D-Link Router", "WANIPConnection", 1);
run_upnp_test(combine_path("..", "root3.xml").c_str(), "D-Link Router", "WANIPConnection_2", 2);
}

View File

@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "test.hpp"
#include <iostream>
char upnp_xml[] =
char upnp_xml[] =
"<root>"
"<specVersion>"
"<major>1</major>"
@ -239,20 +239,11 @@ 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();
}
parse_state(): in_service(false){}
bool in_service;
std::list<std::string> tag_stack;
std::string control_url;
char const* service_type;
std::string service_type;
std::string model;
std::string url_base;
};
@ -292,31 +283,32 @@ void parser_callback(std::string& out, int token, char const* s, char const* val
TORRENT_TEST(xml)
{
// test upnp xml parser
{
parse_state xml_s;
xml_parse(upnp_xml, upnp_xml + sizeof(upnp_xml)
, boost::bind(&find_control_url, _1, _2, boost::ref(xml_s)));
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");
}
{
parse_state xml_s;
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.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");
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