summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/Makefile.am34
-rw-r--r--server/src/macroheaderparser.cc106
-rw-r--r--server/src/templateheaderparser.cc106
3 files changed, 235 insertions, 11 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am
index c8e1c51..2276347 100644
--- a/server/src/Makefile.am
+++ b/server/src/Makefile.am
@@ -106,6 +106,8 @@ EXTRA_DIST = \
################
TESTFILES = \
+ test_templateheaderparser \
+ test_macroheaderparser \
test_templatelist \
test_saxparser \
test_transactionparser \
@@ -139,6 +141,20 @@ test: $(TESTFILES)
test_clean:
rm -f $(TESTFILES) $(TESTLOGS)
+TEST_TEMPLATEHEADERPARSER_FILES = \
+ templateheaderparser.cc \
+ $(PARSERFILES) \
+ $(BASICFILES)
+test_templateheaderparser: $(TEST_TEMPLATEHEADERPARSER_FILES)
+ @../../tools/test $(TEST_TEMPLATEHEADERPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS)
+
+TEST_MACROHEADERPARSER_FILES = \
+ macroheaderparser.cc \
+ $(PARSERFILES) \
+ $(BASICFILES)
+test_macroheaderparser: $(TEST_MACROHEADERPARSER_FILES)
+ @../../tools/test $(TEST_MACROHEADERPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS)
+
TEST_TEMPLATELIST_FILES = \
templatelist.cc \
versionstr.cc \
@@ -148,6 +164,15 @@ TEST_TEMPLATELIST_FILES = \
test_templatelist: $(TEST_TEMPLATELIST_FILES)
@../../tools/test $(TEST_TEMPLATELIST_FILES) $(PARSERFLAGS) $(BASICFLAGS)
+TEST_MACROLIST_FILES = \
+ macrolist.cc \
+ versionstr.cc \
+ macroheaderparser.cc \
+ $(PARSERFILES) \
+ $(BASICFILES)
+test_macrolist: $(TEST_MACROLIST_FILES)
+ @../../tools/test $(TEST_MACROLIST_FILES) $(PARSERFLAGS) $(BASICFLAGS)
+
TEST_SAXPARSER_FILES = \
saxparser.cc \
$(BASICFILES)
@@ -167,15 +192,6 @@ TEST_VERSIONSTR_FILES = \
test_versionstr: $(TEST_VERSIONSTR_FILES)
@../../tools/test $(TEST_VERSIONSTR_FILES) $(BASICFLAGS)
-TEST_MACROLIST_FILES = \
- macrolist.cc \
- versionstr.cc \
- macroheaderparser.cc \
- $(PARSERFILES) \
- $(BASICFILES)
-test_macrolist: $(TEST_MACROLIST_FILES)
- @../../tools/test $(TEST_MACROLIST_FILES) $(PARSERFLAGS) $(BASICFLAGS)
-
TEST_QUERYHANDLERPENTOMINOS_FILES = \
queryhandlerpentominos.cc \
tcpsocket.cc \
diff --git a/server/src/macroheaderparser.cc b/server/src/macroheaderparser.cc
index def80c3..6193e94 100644
--- a/server/src/macroheaderparser.cc
+++ b/server/src/macroheaderparser.cc
@@ -135,10 +135,114 @@ void MacroHeaderParser::parseError(char *buf, size_t len, std::string error, int
throw Exception(error + slineno);
free(slineno);
}
-
}
Macro *MacroHeaderParser::getMacro()
{
return m;
}
+
+#ifdef TEST_MACROHEADERPARSER
+
+#define XMLFILE "/tmp/test_macroheaderparser.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"
+"<macro name=\"testmacro\" version=\"1.0\">\n"
+" <sometag/>\n"
+" <someothertag/>\n"
+"</macro>"
+;
+
+static char xml_nonmacro[] =
+"<?xml version='1.0' encoding='UTF-8'?>\n"
+"<dims name=\"testmacro\" version=\"1.0\">\n"
+" <sometag/>\n"
+" <someothertag/>\n"
+"</dims>"
+;
+
+static char xml_fail[] =
+"<?xml version='1.0' encoding='UTF-8'?>\n"
+"<macro name\"testmacro\" version=\"1.0\">\n"
+" <sometag/>\n"
+" <someothertag/>\n"
+"</macro>"
+;
+
+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 macro xml data.
+ MacroHeaderParser 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_nonmacro);
+ fclose(fp);
+
+ // Test parsing of correct xml data, but not macro (should throw an exception).
+ {
+ MacroHeaderParser 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).
+ {
+ MacroHeaderParser 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_MACROHEADERPARSER*/
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*/