diff options
Diffstat (limited to 'server/src/templateheaderparser.cc')
-rw-r--r-- | server/src/templateheaderparser.cc | 106 |
1 files changed, 105 insertions, 1 deletions
diff --git a/server/src/templateheaderparser.cc b/server/src/templateheaderparser.cc index 0b274ee..0a68a6a 100644 --- a/server/src/templateheaderparser.cc +++ b/server/src/templateheaderparser.cc @@ -135,10 +135,114 @@ void TemplateHeaderParser::parseError(char *buf, size_t len, std::string error, throw Exception(error + slineno); free(slineno); } - } Template *TemplateHeaderParser::getTemplate() { return t; } + +#ifdef TEST_TEMPLATEHEADERPARSER + +#define XMLFILE "/tmp/test_templateheaderparser.xml" + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> +#include <stdio.h> +#include <memory.h> + +static char xml[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<template name=\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</template>" +; + +static char xml_nontemplate[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<dims name=\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</dims>" +; + +static char xml_fail[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<template name\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</template>" +; + +int main() +{ + 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. + TemplateHeaderParser 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). + { + TemplateHeaderParser 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). + { + TemplateHeaderParser 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; +} + +#endif/*TEST_TEMPLATEHEADERPARSER*/ |