From 1936667a53b96b1de5e6af841be827dac43eff39 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Tue, 16 Jan 2018 14:29:42 +0100 Subject: [PATCH] test/xml_node: test Xml_node::decoded_content Issue #2644 --- repos/os/run/xml_node.run | 7 ++++ repos/os/src/test/xml_node/test.cc | 65 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/repos/os/run/xml_node.run b/repos/os/run/xml_node.run index 3705cb4c73..2e29583139 100644 --- a/repos/os/run/xml_node.run +++ b/repos/os/run/xml_node.run @@ -124,5 +124,12 @@ compare_output_to { [init -> test-xml_node] XML node: name = "visible-tag", leaf content = "" [init -> test-xml_node] XML node: name = "visible-tag", leaf content = "" [init -> test-xml_node] +[init -> test-xml_node] -- Test exporting decoded content from XML node -- +[init -> test-xml_node] step 1 +[init -> test-xml_node] step 2 +[init -> test-xml_node] step 3 +[init -> test-xml_node] step 4 +[init -> test-xml_node] step 5 +[init -> test-xml_node] [init -> test-xml_node] --- End of XML-parser test --- } diff --git a/repos/os/src/test/xml_node/test.cc b/repos/os/src/test/xml_node/test.cc index ecb7c8fddf..6f16e20103 100644 --- a/repos/os/src/test/xml_node/test.cc +++ b/repos/os/src/test/xml_node/test.cc @@ -1,6 +1,7 @@ /* * \brief Test for XML parser * \author Norman Feske + * \author Martin Stein * \date 2007-08-21 */ @@ -11,7 +12,9 @@ * under the terms of the GNU Affero General Public License version 3. */ +/* Genode includes */ #include +#include #include #include @@ -314,6 +317,60 @@ static void log_xml_info(const char *xml_string) } +template +static void test_decoded_content(Env &env, + unsigned step, + const char *xml_string, + size_t content_off, + size_t content_sz) +{ + log("step ", step); + + + /* + * Test Xml_node::decoded_content + */ + + size_t const buf_sz = content_sz + 1; + Attached_ram_dataspace buf1_ds(env.ram(), env.rm(), buf_sz); + Attached_ram_dataspace buf2_ds(env.ram(), env.rm(), buf_sz); + char * const buf1 = buf1_ds.local_addr(); + char * const buf2 = buf2_ds.local_addr(); + + Xml_node xml(xml_string); + size_t sz = xml.decoded_content(buf1, max_content_sz); + + if (sz > content_sz) + error("content decoding states to have accessed memory it was not allowed to"); + + memcpy(buf2, &xml_string[content_off], min(content_sz, max_content_sz)); + + if (memcmp(buf1, buf2, buf_sz)) { + error("resulting string of Xml_node::decoded_content is erroneous"); + log("----- should be -----"); + log(Cstring(buf2)); + log("----- is -----"); + log(Cstring(buf1)); + } + + /* + * Test Xml_node::decoded_content > + */ + + enum { MAX_OUT_STRING_SZ = 256 }; + using Out_string = String; + Out_string str = xml.decoded_content(); + + if (memcmp(str.string(), buf2, min(str.size(), buf_sz))) { + error("resulting string of Xml_node::decoded_content > is erroneous"); + log("----- should be -----"); + log(Cstring(buf2)); + log("----- is -----"); + log(str); + } +} + + void Component::construct(Genode::Env &env) { log("--- XML-token test ---"); @@ -351,6 +408,14 @@ void Component::construct(Genode::Env &env) log("-- Test parsing XML with comments --"); log_xml_info(xml_test_comments); + log("-- Test exporting decoded content from XML node --"); + test_decoded_content<~0UL>(env, 1, xml_test_comments, 8, 119); + test_decoded_content<119 >(env, 2, xml_test_comments, 8, 119); + test_decoded_content<11 >(env, 3, xml_test_comments, 8, 119); + test_decoded_content<1 >(env, 4, xml_test_comments, 8, 119); + test_decoded_content<0 >(env, 5, xml_test_comments, 8, 119); + log(""); + log("--- End of XML-parser test ---"); env.parent().exit(0); }