fix const correctness in xml_parse(). update unit tests rss and upnp to use new signature for parser callback
This commit is contained in:
@@ -235,7 +235,8 @@ char upnp_xml2[] =
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
void parser_callback(std::string& out, int token, char const* s, char const* val)
|
||||
void parser_callback(std::string& out, int token, char const* s, int len
|
||||
, char const* val, int val_len)
|
||||
{
|
||||
switch (token)
|
||||
{
|
||||
@@ -250,154 +251,135 @@ void parser_callback(std::string& out, int token, char const* s, char const* val
|
||||
case xml_tag_content: out += "T"; break;
|
||||
default: TEST_CHECK(false);
|
||||
}
|
||||
out += s;
|
||||
out.append(s, len);
|
||||
if (token == xml_attribute)
|
||||
{
|
||||
TEST_CHECK(val != 0);
|
||||
TEST_CHECK(val != NULL);
|
||||
out += "V";
|
||||
out += val;
|
||||
out.append(val, val_len);
|
||||
}
|
||||
else
|
||||
{
|
||||
TEST_CHECK(val == 0);
|
||||
TEST_CHECK(val == NULL);
|
||||
}
|
||||
}
|
||||
|
||||
TORRENT_TEST(xml)
|
||||
void test_parse(char const* in, char const* expected)
|
||||
{
|
||||
// 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)));
|
||||
|
||||
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.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 xml[] = "<a>foo<b/>bar</a>";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "BaSfooEbSbarFa");
|
||||
}
|
||||
|
||||
{
|
||||
char xml[] = "<?xml version = \"1.0\"?><c x=\"1\" \t y=\"3\"/><d foo='bar'></d boo='foo'><!--comment-->";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "DxmlAversionV1.0EcAxV1AyV3BdAfooVbarFdAbooVfooCcomment");
|
||||
}
|
||||
|
||||
{
|
||||
char xml[] = "<a f=1>foo</a f='b>";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "BaPunquoted attribute valueSfooFaPmissing end quote on attribute");
|
||||
}
|
||||
|
||||
{
|
||||
char xml[] = "<a f>foo</a v >";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "BaTfSfooFaTv ");
|
||||
}
|
||||
|
||||
{
|
||||
// test unterminated CDATA tags
|
||||
char xml[] = "<![CDATA[foo";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "Punexpected end of file");
|
||||
}
|
||||
|
||||
{
|
||||
// test CDATA tag
|
||||
char xml[] = "<![CDATA[verbatim tag that can have > and < in it]]>";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "Sverbatim tag that can have > and < in it");
|
||||
}
|
||||
|
||||
{
|
||||
// test unterminated tags
|
||||
char xml[] = "<foo";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "Punexpected end of file");
|
||||
}
|
||||
|
||||
{
|
||||
// test unquoted attribute values
|
||||
char xml[] = "<foo a=bar>";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "BfooPunquoted attribute value");
|
||||
}
|
||||
|
||||
{
|
||||
// test unterminated attribute value
|
||||
char xml[] = "<foo a=\"bar>";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "BfooPmissing end quote on attribute");
|
||||
}
|
||||
|
||||
{
|
||||
// test unterminated tag
|
||||
char xml[] = "<foo a=\"bar";
|
||||
std::string out;
|
||||
|
||||
xml_parse(xml, xml + sizeof(xml) - 1, boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3));
|
||||
std::cerr << out << std::endl;
|
||||
TEST_CHECK(out == "Punexpected end of file");
|
||||
}
|
||||
std::string out;
|
||||
xml_parse(in, in + strlen(in), boost::bind(&parser_callback
|
||||
, boost::ref(out), _1, _2, _3, _4, _5));
|
||||
fprintf(stderr, "in: %s\n out: %s\nexpected: %s\n"
|
||||
, in, out.c_str(), expected);
|
||||
TEST_EQUAL(out, expected);
|
||||
}
|
||||
|
||||
TORRENT_TEST(upnp_parser1)
|
||||
{
|
||||
parse_state xml_s;
|
||||
xml_parse(upnp_xml, upnp_xml + sizeof(upnp_xml)
|
||||
, boost::bind(&find_control_url, _1, _2, _3, 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_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, upnp_xml2 + sizeof(upnp_xml2)
|
||||
, boost::bind(&find_control_url, _1, _2, _3, 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_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(tags)
|
||||
{
|
||||
test_parse("<a>foo<b/>bar</a>", "BaSfooEbSbarFa");
|
||||
}
|
||||
|
||||
TORRENT_TEST(xml_tag_comment)
|
||||
{
|
||||
test_parse("<?xml version = \"1.0\"?><c x=\"1\" \t y=\"3\"/><d foo='bar'></d boo='foo'><!--comment-->"
|
||||
, "DxmlAversionV1.0EcAxV1AyV3BdAfooVbarFdAbooVfooCcomment");
|
||||
}
|
||||
|
||||
TORRENT_TEST(empty_tag)
|
||||
{
|
||||
test_parse("<foo/>", "Efoo");
|
||||
}
|
||||
|
||||
TORRENT_TEST(empty_tag_whitespace)
|
||||
{
|
||||
test_parse("<foo />", "Efoo");
|
||||
}
|
||||
|
||||
TORRENT_TEST(xml_tag_no_attribute)
|
||||
{
|
||||
test_parse("<?xml?>", "Dxml");
|
||||
}
|
||||
|
||||
TORRENT_TEST(xml_tag_no_attribute_whitespace)
|
||||
{
|
||||
test_parse("<?xml ?>", "Dxml");
|
||||
}
|
||||
|
||||
TORRENT_TEST(attribute_missing_qoute)
|
||||
{
|
||||
test_parse("<a f=1>foo</a f='b>"
|
||||
, "BaPunquoted attribute valueSfooFaPmissing end quote on attribute");
|
||||
}
|
||||
|
||||
TORRENT_TEST(attribute_whitespace)
|
||||
{
|
||||
test_parse("<a f>foo</a v >", "BaTfSfooFaTv ");
|
||||
}
|
||||
|
||||
TORRENT_TEST(unterminated_cdata)
|
||||
{
|
||||
// test unterminated CDATA tags
|
||||
test_parse("<![CDATA[foo", "Punexpected end of file");
|
||||
}
|
||||
|
||||
TORRENT_TEST(cdata)
|
||||
{
|
||||
// test CDATA tag
|
||||
test_parse("<![CDATA[verbatim tag that can have > and < in it]]>"
|
||||
, "Sverbatim tag that can have > and < in it");
|
||||
}
|
||||
|
||||
TORRENT_TEST(unterminated_tag)
|
||||
{
|
||||
// test unterminated tags
|
||||
test_parse("<foo", "Punexpected end of file");
|
||||
}
|
||||
|
||||
TORRENT_TEST(unqouted_attribute_value)
|
||||
{
|
||||
// test unquoted attribute values
|
||||
test_parse("<foo a=bar>", "BfooPunquoted attribute value");
|
||||
}
|
||||
|
||||
TORRENT_TEST(unterminated_attribute)
|
||||
{
|
||||
// test unterminated attribute value
|
||||
test_parse("<foo a=\"bar>", "BfooPmissing end quote on attribute");
|
||||
}
|
||||
|
||||
TORRENT_TEST(unterminated_tag_with_attribute)
|
||||
{
|
||||
// test unterminated tag
|
||||
test_parse("<foo a=\"bar", "Punexpected end of file");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user