From f23b20281a5d0badd8c2d2904b44365418ad46a5 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 7 Aug 2009 13:21:27 +0000 Subject: New test program. --- server/src/Makefile.am | 7 +++ server/src/macroparser.cc | 118 ++++++++++++++++++++++++++--------- server/src/tcpsocket.cc | 5 +- server/src/templateparser.cc | 142 +++++++++++++++++++++++++++++-------------- 4 files changed, 198 insertions(+), 74 deletions(-) diff --git a/server/src/Makefile.am b/server/src/Makefile.am index eb52775..fa6b7cf 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -109,6 +109,7 @@ EXTRA_DIST = \ ################ TESTFILES = \ + test_tcpsocket \ test_pracrodaotest \ test_widgetgenerator \ test_configurationparser \ @@ -148,6 +149,12 @@ test: $(TESTFILES) test_clean: rm -f $(TESTFILES) $(TESTLOGS) +TEST_TCPSOCKET_FILES = \ + tcpsocket.cc \ + $(BASICFILES) +test_tcpsocket: $(TEST_TCPSOCKET_FILES) + @../../tools/test $(TEST_TCPSOCKET_FILES) $(BASICFLAGS) + TEST_PRACRODAOTEST_FILES = \ pracrodaotest.cc \ pracrodao.cc \ diff --git a/server/src/macroparser.cc b/server/src/macroparser.cc index b91462a..0b0257e 100644 --- a/server/src/macroparser.cc +++ b/server/src/macroparser.cc @@ -325,44 +325,106 @@ Macro *MacroParser::getMacro() #ifdef TEST_MACROPARSER -void print_attributes(std::string prefix, - std::map< std::string, std::string > &att) -{ - std::map< std::string, std::string >::iterator i = att.begin(); - while(i != att.end()) { - printf("%s %s = \"%s\"\n", prefix.c_str(), (*i).first.c_str(), (*i).second.c_str()); - i++; - } -} +#define XMLFILE "/tmp/test_macroheaderparser.xml" + +#include +#include +#include +#include +#include +#include + +static char xml[] = +"\n" +"\n" +" \n" +" \n" +" \n" +" \n" +" \n" +"" +; + +static char xml_nonmacro[] = +"\n" +"\n" +" \n" +" \n" +"" +; + +static char xml_fail[] = +"\n" +"\n" +" \n" +" \n" +"" +; int main() { - try { - MacroParser parser("../xml/macros/example.xml"); - parser.parse(); + FILE *fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml); + fclose(fp); - Macro *m = parser.getMacro(); + { + // Test parsing of correct macro xml data. + MacroParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + return 1; + } + } - printf("\t\t\t[Macro]:\n"); - print_attributes("\t\t\t\t-", m->attributes); - - std::vector< Query >::iterator qi = m->queries.begin(); - while(qi != m->queries.end()) { - printf("\t\t\t\t[Query]:\n"); - print_attributes("\t\t\t\t\t-", (*qi).attributes); - qi++; + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_nonmacro); + fclose(fp); + + // Test parsing of correct xml data, but not macro (should throw an exception). + { + MacroParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto onandon; + } + return 1; } + onandon: - std::vector< Map >::iterator mi = m->maps.begin(); - while(mi != m->maps.end()) { - printf("\t\t\t\t[Map]:\n"); - print_attributes("\t\t\t\t\t-", (*mi).attributes); - mi++; + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; } - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); + fprintf(fp, xml_fail); + fclose(fp); + + // Test parsing of invalid xml data (should throw an exception). + { + MacroParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto yetonandon; + } return 1; } + yetonandon: + + unlink(XMLFILE); return 0; } diff --git a/server/src/tcpsocket.cc b/server/src/tcpsocket.cc index f72d7b6..f748e0a 100644 --- a/server/src/tcpsocket.cc +++ b/server/src/tcpsocket.cc @@ -392,9 +392,10 @@ int main() try { TCPSocket listen_sock; listen_sock.listen(12345); - TCPSocket sock = listen_sock.accept(); - sock.read(buf, sizeof(buf)); + TCPSocket *sock = listen_sock.accept(); + sock->read(buf, sizeof(buf)); printf("Got: [%s]\n", buf); + delete sock; if(std::string(buf) != "hello") return 1; } catch( Exception &e ) { fprintf(stderr, "%s\n", e.what()); diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc index d01aa31..277a8bd 100644 --- a/server/src/templateparser.cc +++ b/server/src/templateparser.cc @@ -144,6 +144,12 @@ void TemplateParser::parseError(char *buf, size_t len, std::string error, int li if(fwrite(buf, len, 1, stderr) != len) {} fprintf(stderr, "]\n"); fflush(stderr); + + char *slineno; + if(asprintf(&slineno, " at line %d\n", lineno) != -1) { + throw Exception(error + slineno); + free(slineno); + } } Template *TemplateParser::getTemplate() @@ -153,56 +159,104 @@ Template *TemplateParser::getTemplate() #ifdef TEST_TEMPLATEPARSER -void print_attributes(std::string prefix, - std::map< std::string, std::string > &att) -{ - std::map< std::string, std::string >::iterator i = att.begin(); - while(i != att.end()) { - printf("%s %s = \"%s\"\n", prefix.c_str(), (*i).first.c_str(), (*i).second.c_str()); - i++; - } -} +#define XMLFILE "/tmp/test_templateparser.xml" + +#include +#include +#include +#include +#include +#include + +static char xml[] = +"\n" +"" +; + +static char xml_nontemplate[] = +"\n" +"\n" +" \n" +" \n" +"" +; + +static char xml_fail[] = +"\n" +"" +; int main() { - Conf::xml_basedir = "../xml/"; - - try { - TemplateParser parser("../xml/templates/example.xml"); - parser.parse(); - - Template *t = parser.getTemplate(); - - printf("\t[Template]:\n"); - print_attributes("\t\t-", t->attributes); - - printf("\t\t[Macros]:\n"); - std::vector< Macro >::iterator i = t->macros.begin(); - - while(i != t->macros.end()) { - printf("\t\t\t[Macro]:\n"); - print_attributes("\t\t\t\t-", (*i).attributes); - - std::vector< Query >::iterator qi = (*i).queries.begin(); - while(qi != (*i).queries.end()) { - printf("\t\t\t\t[Query]:\n"); - print_attributes("\t\t\t\t\t-", (*qi).attributes); - qi++; - } - - std::vector< Map >::iterator mi = (*i).maps.begin(); - while(mi != (*i).maps.end()) { - printf("\t\t\t\t[Map]:\n"); - print_attributes("\t\t\t\t\t-", (*mi).attributes); - mi++; - } - - i++; + FILE *fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml); + fclose(fp); + + { + // Test parsing of correct template xml data. + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + return 1; + } + } + + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_nontemplate); + fclose(fp); + + // Test parsing of correct xml data, but not template (should throw an exception). + { + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto onandon; + } + return 1; + } + onandon: + + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_fail); + fclose(fp); + + // Test parsing of invalid xml data (should throw an exception). + { + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto yetonandon; } - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); return 1; } + yetonandon: + + unlink(XMLFILE); + return 0; } -- cgit v1.2.3