diff options
-rw-r--r-- | server/src/Makefile.am | 8 | ||||
-rw-r--r-- | server/src/transactionparser.cc | 128 |
2 files changed, 132 insertions, 4 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am index f970117..baecd73 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -103,6 +103,7 @@ EXTRA_DIST = \ TESTFILES = \ test_saxparser \ + test_transactionparser \ test_versionstr \ test_macrolist \ test_queryhandlerpentominos \ @@ -139,6 +140,13 @@ TEST_SAXPARSER_FILES = \ test_saxparser: $(TEST_SAXPARSER_FILES) @../../tools/test $(TEST_SAXPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS) +TEST_TRANSACTIONPARSER_FILES = \ + transactionparser.cc \ + $(BASICFILES) \ + $(PARSERFILES) +test_transactionparser: $(TEST_TRANSACTIONPARSER_FILES) + @../../tools/test $(TEST_TRANSACTIONPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS) + TEST_VERSIONSTR_FILES = \ versionstr.cc \ $(BASICFILES) diff --git a/server/src/transactionparser.cc b/server/src/transactionparser.cc index 9e3d952..08c0a42 100644 --- a/server/src/transactionparser.cc +++ b/server/src/transactionparser.cc @@ -33,6 +33,34 @@ #include <string> #include <map> +#include "debug.h" +#include "exception.h" + +static void error(const char* fmt, ...) +{ + PRACRO_ERR_LOG(transactionparser, "Error in TransactionParser: "); + + { + va_list argp; + va_start(argp, fmt); + PRACRO_ERR_LOG_VA(macro, fmt, argp); + va_end(argp); + + fprintf(stderr, "\n"); + } + + { + char *p; + va_list argp; + va_start(argp, fmt); + if(vasprintf(&p, fmt, argp) != -1) { + throw Exception("Error in TransactionParser: " + std::string(p)); + free(p); + } + va_end(argp); + } +} + TransactionParser::TransactionParser(Transaction *transaction) { this->transaction = transaction; @@ -68,15 +96,107 @@ void TransactionParser::startTag(std::string name, std::map< std::string, std::s } if(name == "field") { + if(!transaction->commits.size()) error("Field without a commit tag!"); + if(attributes.find("name") == attributes.end()) error("Field is missing 'name' attribute"); + if(attributes.find("value") == attributes.end()) error("Field is missing 'value' attribute"); transaction->commits.back().fields[attributes["name"]] = attributes["value"]; } } void TransactionParser::parseError(char *buf, size_t len, std::string error, int lineno) { - fprintf(stderr, "TransactionParser error at line %d: %s\n", lineno, error.c_str()); - fprintf(stderr, "\tBuffer %u bytes: [", len); + PRACRO_ERR_LOG(transactionparser, "TransactionParser error at line %d: %s\n", + lineno, error.c_str()); + PRACRO_ERR_LOG(transactionparser, "\tBuffer %u bytes: [", len); if(fwrite(buf, len, 1, stderr) != len) {} - fprintf(stderr, "]\n"); - fflush(stderr); + PRACRO_ERR_LOG(transactionparser, "]\n"); + + char *slineno; + if(asprintf(&slineno, " at line %d\n", lineno) != -1) { + throw Exception(error + slineno); + free(slineno); + } } + +#ifdef TEST_TRANSACTIONPARSER + +static char xml_minimal[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<pracro/>\n" + ; + +static char xml_request[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n" +" <request macro=\"test\" course=\"test\"/>\n" +"</pracro>\n" + ; + +static char xml_commit[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n" +" <commit version=\"\" macro=\"referral\" course=\"amd_forunders\" >\n" +" <field value=\"Some docs\" name=\"referral.doctor\"/>\n" +" <field value=\"DIMS\" name=\"referral.diagnosecode\"/>\n" +" <field value=\"Avs\" name=\"referral.diagnose\"/>\n" +" </commit>\n" +"</pracro>\n" + ; + +static char xml_fail[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n" +" <request macro=\"test course=\"test\"/>\n" +"</pracro>\n" + ; + +int main() +{ + // Test minimal + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_minimal, strlen(xml_minimal)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + return 1; + } + + // Test request + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_request, strlen(xml_request)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + return 1; + } + + // Test commit + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_commit, strlen(xml_commit)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + return 1; + } + + // Test parse error (should throw an exception) + try { + Transaction transaction; + TransactionParser parser(&transaction); + parser.parse(xml_fail, strlen(xml_fail)); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + goto onandon; + } + printf("We should fail here...\n"); + return 1; + + onandon: + + return 0; +} + +#endif/*TEST_TRANSACTIONPARSER*/ |